Debian Codegraph @

texmf-dist/tex/lualatex/luacas/algebra/luacas-equation.lua

raw
1 --- @class Equation
2 --- An expression that represents an equation of the form lhs = rhs.
3 --- @field lhs Expression
4 --- @field rhs Expression
5 Equation = {}
6 local __Equation = {}
7
8 --------------------------
9 -- Static functionality --
10 --------------------------
11
12 --- Attempts to isolate the variable var in lhs by moving expressions to rhs. Ony performs a single step.
13 --- @param lhs Expression
14 --- @param rhs Expression
15 --- @param var SymbolExpression
16 --- @return Expression, Expression
17 function Equation.isolatelhs(lhs, rhs, var)
18 if lhs:type() == BinaryOperation then
19 local stay = Integer.zero()
20 local switch = Integer.zero()
21 if lhs.operation == BinaryOperation.ADD then
22 for _, exp in ipairs(lhs:subexpressions()) do
23 if exp:freeof(var) then
24 switch = switch + exp
25 else
26 stay = stay + exp
27 end
28 end
29 if switch == Integer.zero() then
30 lhs = lhs:factor() -- TODO: Replace with collect for efficiency reasons
31 else
32 return stay:autosimplify(), (rhs - switch):autosimplify()
33 end
34 end
35 if lhs.operation == BinaryOperation.MUL then
36 stay = Integer.one()
37 switch = Integer.one()
38 for _, exp in ipairs(lhs:subexpressions()) do
39 if exp:freeof(var) then
40 switch = switch * exp
41 else
42 stay = stay * exp
43 end
44 end
45 return stay:autosimplify(), (rhs / switch):autosimplify()
46 end
47 if lhs.operation == BinaryOperation.POW then
48 if lhs:subexpressions()[1]:freeof(var) then
49 return lhs:subexpressions()[2]:autosimplify(), Logarithm(lhs:subexpressions()[1], rhs):autosimplify()
50 elseif lhs:subexpressions()[2]:freeof(var) then
51 return lhs:subexpressions()[1]:autosimplify(), (rhs ^ (Integer.one()/lhs:subexpressions()[2])):autosimplify()
52 end
53 end
54 elseif lhs:type() == Logarithm then
55 if lhs.base:freeof(var) then
56 return lhs.expression:autosimplify(), (lhs.base ^ rhs):autosimplify()
57 elseif lhs.expression:freeof(var) then
58 return lhs.base:autosimplify(), (lhs.expression ^ (Integer.one()/rhs)):autosimplify()
59 end
60 elseif lhs:type() == TrigExpression then
61 return lhs.expression:autosimplify(), TrigExpression(TrigExpression.INVERSES[lhs.name], rhs):autosimplify()
62 end
63
64 return lhs, rhs
65 end
66
67 ----------------------------
68 -- Instance functionality --
69 ----------------------------
70
71 --- Creates a new equation with the given expressions.
72 --- @param lhs Expression
73 --- @param rhs Expression
74 --- @return Equation
75 function Equation:new(lhs, rhs)
76
77 if lhs:type() == Equation or rhs:type() == Equation then
78 error("Sent parameter of wrong type: cannot nest equations or inequalities")
79 end
80
81 local o = {}
82 local __o = Copy(__ExpressionOperations) -- TODO: Ensure only one metatable for each instance of a class
83
84 o.lhs = lhs
85 o.rhs = rhs
86
87 __o.__index = Equation
88 __o.__tostring = function(a)
89 return tostring(a.lhs) .. ' = ' .. tostring(a.rhs)
90 end
91 __o.__eq = function(a, b)
92 -- This shouldn't be needed, since __eq should only fire if both metamethods have the same function, but for some reason Lua always runs this anyway
93 if not b:type() == Equation then
94 return false
95 end
96 return a.lhs == b.lhs and a.rhs == b.rhs
97 end
98 o = setmetatable(o, __o)
99
100 return o
101 end
102
103 --- Evaluation in this case just checks for structural equality, or guarenteed inequality in the case of constants
104 --- @return Equation|boolean
105 function Equation:evaluate()
106 if self.lhs == self.rhs then
107 return true -- TODO: Add Boolean Expressions
108 end
109 if self.lhs:isconstant() and self.rhs:isconstant() and self.lhs ~= self.rhs then
110 return false
111 end
112 return self
113 end
114
115 --- @return Equation|boolean
116 function Equation:autosimplify()
117 local lhs = self.lhs:autosimplify()
118 local rhs = self.rhs:autosimplify()
119
120 return Equation(lhs, rhs):evaluate()
121 end
122
123 --- @return table<number, Expression>
124 function Equation:subexpressions()
125 return {self.lhs, self.rhs}
126 end
127
128 --- Attempts to solve the equation for a particular variable.
129 --- @param var SymbolExpression
130 --- @return Equation
131 function Equation:solvefor(var)
132 local lhs = self.lhs
133 local rhs = self.rhs
134
135 if lhs:freeof(var) and rhs:freeof(var) then
136 return self
137 end
138
139 -- Check for monovariate polynomial expressions
140 local root = (lhs - rhs):autosimplify()
141 local poly, status = root:expand():topolynomial()
142 if status then
143 -- TODO: Add Set expressions
144 return Equation(var, poly:roots()[1])
145 end
146
147 local newlhs, newrhs = root, Integer(0)
148 local oldlhs
149 while newlhs ~= var and oldlhs ~= newlhs do
150 oldlhs = newlhs
151 newlhs, newrhs = Equation.isolatelhs(newlhs, newrhs, var)
152 end
153
154 return Equation(newlhs, newrhs)
155 end
156
157 --- @param subexpressions table<number, Expression>
158 --- @return Equation
159 function Equation:setsubexpressions(subexpressions)
160 return Equation(subexpressions[1], subexpressions[2])
161 end
162
163 --- @param other Expression
164 --- @return boolean
165 function Equation:order(other)
166 if other:isatomic() then
167 return false
168 end
169
170 return self.lhs:order(other)
171 end
172
173 --- @return string
174 function Equation:tolatex()
175 return self.lhs:tolatex() .. '=' .. self.rhs:tolatex()
176 end
177
178 -----------------
179 -- Inheritance --
180 -----------------
181 __Equation.__index = CompoundExpression
182 __Equation.__call = Equation.new
183 Equation = setmetatable(Equation, __Equation)