I would like to create a lua script in a rainmeter skin that would solve an equation such as x^2+3x=2 or 3x+5=14 or even more complex (but still simple algebra (including trigonometric functions such as cos, sin, and tan)) equations. The only problem is, I have no idea how to do this.
My only idea so far is to create something that checks how long the input string is, then creates a two dimensional matrix that keeps track of what kind of character each one is, as well as important things such as if it is inside parenthesis or not (I would like support for distributive property), and then decides the first step to get x by itself on one side of the =. It would then change the string, and reevaluate until it got to a simple x=#.
I would love feedback on if this method is even feasible, and any ideas would be much appreciated!
Thanks in advance to anyone who helps!
It is currently February 19th, 2020, 2:46 pm
Lua Algebra Solver help?

 Posts: 83
 Joined: February 3rd, 2015, 4:36 pm
Lua Algebra Solver help?
____ Explodingcreeperssss ____
Click Me!  My Algebra Solver for Rainmeter!
Click Me!  My Algebra Solver for Rainmeter!

 Rainmeter Sage
 Posts: 9712
 Joined: October 11th, 2010, 6:27 pm
 Location: Gheorgheni, Romania
Re: Lua Algebra Solver help?
I've started to work with your idea and here's what I reached:
Equation.ini:
Script.lua:
This skin can handle the first and second degree equation. The first must have this form: eg 6x12=0, the second: x^23x+2=0. Until now other forms are not accepted (eg 2+x^23x=0 or x^23x=2). I'm not very good in lua scripting, I hope you'll not laughing on a such convoluted code. Maybe someone much better than me will help us.
Equation.ini:
Code: Select all
[Rainmeter]
Update=1
BackgroundMode=2
SolidColor=80,80,80,220
[Variables]
Equation=x^2+8x+12=0
[MeasureLuaScript]
Measure=Script
ScriptFile=Script.lua
[MeterColor]
Meter=STRING
X=0
Y=0
Padding=15,5,15,5
FontColor=255,255,255
FontEffectColor=0,0,0
StringEffect=Shadow
FontSize=10
FontFace=Segoe UI
StringStyle=BOLD
StringAlign=LEFT
AntiAlias=1
Text=#Equation##CRLF##Res1##CRLF##Res2#
DynamicVariables=1
Code: Select all
function Initialize()
Equation = SKIN:GetVariable('Equation')
a, b, c = '', '', ''
n = 0
Coefficients = {}
for i=1,#Equation do
Coefficients[i] = '0'
end
for i = 1,#Equation do
Coefficients[i] = string.sub(Equation, i, i)
end
i = 0
repeat
i = i + 1
until ((( Coefficients[i] == 'x' ) and ( Coefficients[i+1] == '^' ) and ( Coefficients[i+2] == '2' )) or i == #Equation)
if i < #Equation then
for i = 1,#Equation do
if ( Coefficients[i] == 'x' ) and ( Coefficients[i+1] == '^' ) and ( Coefficients[i+2] == '2' ) then
if i1 >= 1 then
for j = 1,(i1) do
a = a .. Coefficients[j]
end
end
if i1 < 1 then
a = '1'
end
n = i + #a + 2
end
end
for i = 1,#Equation do
if ( Coefficients[i] == 'x' ) and ( Coefficients[i+1] ~= '^' ) then
for j = n,(i1) do
b = b .. Coefficients[j]
end
n = i+1
end
end
for i = 1,#Equation do
if Coefficients[i] == '=' then
for j = n,(i1) do
c = c .. Coefficients[j]
end
n = i+1
end
end
a, b, c = tonumber(a), tonumber(b), tonumber(c)
if Delta(a, b, c) >= 0 then
SKIN:Bang('!SetVariable', 'Res1', 'x1='..(math.floor(Result1(a, b, c))+(math.floor(100*(Result1(a, b, c)math.floor(Result1(a, b, c)))))/100))
SKIN:Bang('!SetVariable', 'Res2', 'x2='..(math.floor(Result2(a, b, c))+(math.floor(100*(Result2(a, b, c)math.floor(Result2(a, b, c)))))/100))
else
SKIN:Bang('!SetVariable', 'Res1', 'This equation has no real solutions.')
SKIN:Bang('!SetVariable', 'Res2', '')
end
else
for i = 1,#Equation do
if Coefficients[i] == 'x' then
for j = 1,(i1) do
a = a .. Coefficients[j]
end
n = i+1
end
end
for i = 1,#Equation do
if Coefficients[i] == '=' then
for j = n,(i1) do
b = b .. Coefficients[j]
end
n = i+1
end
end
a, b = tonumber(a), tonumber(b)
SKIN:Bang('!SetVariable', 'Res1', 'x='..(b/a))
SKIN:Bang('!SetVariable', 'Res2', '')
end
end
function Delta(a, b, c)
return (b*b  4*a*c)
end
function Result1(a, b, c)
return ((bmath.sqrt(Delta(a, b, c)))/(2*a))
end
function Result2(a, b, c)
return ((b+math.sqrt(Delta(a, b, c)))/(2*a))
end
function Update()
end

 Posts: 83
 Joined: February 3rd, 2015, 4:36 pm
Re: Lua Algebra Solver help?
Thank you so much for this! This will definitely help me get started, as I know almost nothing in lua (yet). This is not the end solution, but this is definitely a large step in the right direction.
____ Explodingcreeperssss ____
Click Me!  My Algebra Solver for Rainmeter!
Click Me!  My Algebra Solver for Rainmeter!

 Rainmeter Sage
 Posts: 9712
 Joined: October 11th, 2010, 6:27 pm
 Location: Gheorgheni, Romania
Re: Lua Algebra Solver help?
There's a very good lua online book: http://www.lua.org/pil/contents.htmlrevolt4peace wrote:Thank you so much for this! This will definitely help me get started, as I know almost nothing in lua (yet). This is not the end solution, but this is definitely a large step in the right direction.
I think you should use it in your work.

 Developer
 Posts: 20250
 Joined: April 19th, 2009, 11:02 pm
 Location: Fort Hunt, Virginia, USA
Re: Lua Algebra Solver help?
In my view, this is going to be a "Manhattan Project" / "boiling the oceans" level of effort...
https://github.com/simoncozens/cassowary.lua
https://github.com/simoncozens/cassowary.lua/blob/master/cassowary.lua
Be aware that we don't support .require or .dofile in our implementation of Lua / Rainmeter, so you would need that entire thousands of line of function code in your .lua file. I have not looked at it closely to see if there are other issues with using it in our environment, I mostly just wanted to give a sense of the level of effort.
https://github.com/simoncozens/cassowary.lua
https://github.com/simoncozens/cassowary.lua/blob/master/cassowary.lua
Be aware that we don't support .require or .dofile in our implementation of Lua / Rainmeter, so you would need that entire thousands of line of function code in your .lua file. I have not looked at it closely to see if there are other issues with using it in our environment, I mostly just wanted to give a sense of the level of effort.

 Posts: 83
 Joined: February 3rd, 2015, 4:36 pm
Re: Lua Algebra Solver help?
Don't worry about the amount of effort I will need to put into this, I'm ready. I'm used to making code that is easily 10K lines. Thanks for the help! I'll take a look at this code you gave me and see what's up.
____ Explodingcreeperssss ____
Click Me!  My Algebra Solver for Rainmeter!
Click Me!  My Algebra Solver for Rainmeter!

 Developer
 Posts: 20250
 Joined: April 19th, 2009, 11:02 pm
 Location: Fort Hunt, Virginia, USA
Re: Lua Algebra Solver help?
Glad to help. I'm walking away from this now, and not looking back. Hands in pockets, whistling nervously.

 Posts: 83
 Joined: February 3rd, 2015, 4:36 pm
Re: Lua Algebra Solver help?
Just so you know, balala, there was one error with your code.
I changed
to
because the results were negative when they were supposed to be positive, and positive when they were meant to be negative.
Thanks again for the (quite large) step in the right direction!
I changed
Code: Select all
SKIN:Bang('!SetVariable', 'Res1', 'x1='..(math.floor(Result1(a, b, c))+(math.floor(100*(Result1(a, b, c)math.floor(Result1(a, b, c)))))/100))
SKIN:Bang('!SetVariable', 'Res2', 'x2='..(math.floor(Result2(a, b, c))+(math.floor(100*(Result2(a, b, c)math.floor(Result2(a, b, c)))))/100))
Code: Select all
SKIN:Bang('!SetVariable', 'Res1', 'x1='..((math.floor(Result1(a, b, c))+(math.floor(100*(Result1(a, b, c)math.floor(Result1(a, b, c)))))/100)))
SKIN:Bang('!SetVariable', 'Res2', 'x2='..((math.floor(Result2(a, b, c))+(math.floor(100*(Result2(a, b, c)math.floor(Result2(a, b, c)))))/100)))
Thanks again for the (quite large) step in the right direction!
Last edited by revolt4peace on February 16th, 2015, 5:12 pm, edited 2 times in total.
____ Explodingcreeperssss ____
Click Me!  My Algebra Solver for Rainmeter!
Click Me!  My Algebra Solver for Rainmeter!

 Rainmeter Sage
 Posts: 9712
 Joined: October 11th, 2010, 6:27 pm
 Location: Gheorgheni, Romania
Re: Lua Algebra Solver help?
Sorry for that, but I hope you got the idea.revolt4peace wrote:Just so you know, balala, there was one error with your code.

 Posts: 83
 Joined: February 3rd, 2015, 4:36 pm
Re: Lua Algebra Solver help?
When I say:
It does not read the '10' or '0' as numbers, so you would think I would simply change '10' to 10 and '0' to 0, and it would all work. But when I do remove the '' then the code stops working at all. Why is this, how do I fix it, and is there a better way to check if something is a number, rather than another character?
Code: Select all
if Coefficients[5] < '10' and Coefficients[5] >= '0' then
____ Explodingcreeperssss ____
Click Me!  My Algebra Solver for Rainmeter!
Click Me!  My Algebra Solver for Rainmeter!