Debian Codegraph @

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

raw
1 --- @class SqrtExpression
2 --- An expression that represents the positive real solution to x^n = a where n is a positive integer and a is constant.
3 --- @field expression Expression
4 SqrtExpression = {}
5 local __SqrtExpression = {}
6
7 ----------------------------
8 -- Instance functionality --
9 ----------------------------
10
11 --- Creates a new sqrt expression with the given expression.
12 --- @param expression Expression
13 --- @param root Integer
14 --- @return SqrtExpression
15 function SqrtExpression:new(expression, root)
16 root = root or Integer(2)
17 local o = {}
18 local __o = Copy(__ExpressionOperations)
19
20 o.expression = Copy(expression)
21 o.root = root
22
23 __o.__index = SqrtExpression
24 __o.__tostring = function(a)
25 return tostring(a.expression) .. ' ^ (1/' .. tostring(a.root) .. ')'
26 end
27 __o.__eq = function(a, b)
28 -- This shouldn't be needed, since __eq should only fire if both metamethods have the same function, but for some reason Lua always rungs this anyway
29 if not b:type() == SqrtExpression then
30 return false
31 end
32 return a.expression == b.expression and a.root == b.root
33 end
34 o = setmetatable(o, __o)
35
36 return o
37 end
38
39
40 --- @return table<number, Expression>
41 function SqrtExpression:subexpressions()
42 return {self.expression}
43 end
44
45 --- @param subexpressions table<number, Expression>
46 --- @return SqrtExpression
47 function SqrtExpression:setsubexpressions(subexpressions)
48 return SqrtExpression(subexpressions[1], self.root)
49 end
50
51 --- @param other Expression
52 --- @return boolean
53 function SqrtExpression:order(other)
54 return self:topower():order(other)
55 end
56
57 function SqrtExpression:topower()
58 local exponent = BinaryOperation(BinaryOperation.DIV,{Integer.one(),self.root}):autosimplify()
59 local base = self.expression
60 return BinaryOperation(BinaryOperation.POW,{base,exponent}):autosimplify()
61 end
62
63 function SqrtExpression:autosimplify()
64 local expression = self.expression:autosimplify()
65 local root = self.root:autosimplify()
66
67 if root == Integer.one() then
68 return expression
69 end
70
71 if root:type() == Rational then
72 return SqrtExpression(BinaryOperation(BinaryOperation.POW,{expression,root.denominator}):autosimplify(), root.numerator):autosimplify()
73 end
74
75 if not root:isconstant() then
76 return BinaryOperation(BinaryOperation.POW,{expression,Integer.one() / root}):autosimplify()
77 end
78
79 if not expression:isconstant() then
80 if expression.operation == BinaryOperation.MUL and expression.expressions[1]:isconstant() then
81 local coeff = SqrtExpression(expression.expressions[1],root):autosimplify()
82 expression.expressions[1] = BinaryOperation(BinaryOperation.MUL,{Integer.one()})
83 expression = expression:autosimplify()
84 local sqrtpart = SqrtExpression(expression,root):autosimplify()
85 local result = coeff*sqrtpart
86 return result:autosimplify()
87 end
88 return BinaryOperation(BinaryOperation.POW,{expression,Integer.one() / root}):autosimplify()
89 end
90
91 if expression:type() == Rational then
92 local result = BinaryOperation(BinaryOperation.MUL, {SqrtExpression(expression.numerator,root):autosimplify(),BinaryOperation(BinaryOperation.POW,{SqrtExpression(expression.denominator,root):autosimplify(),Integer(-1)})})
93 return result:autosimplify()
94 end
95
96 if expression:type() == Integer then
97 if expression == Integer.zero() then
98 return Integer.zero()
99 end
100 if expression == Integer.one() then
101 return Integer.one()
102 end
103 if expression < Integer.zero() then
104 if root == Integer(2) then
105 local result = SqrtExpression(expression:neg(),root):autosimplify()
106 result = I*result
107 return result:autosimplify()
108 end
109 if root % Integer(2) == Integer.one() then
110 local result = SqrtExpression(expression:neg(),root):autosimplify()
111 result = -result
112 return result:autosimplify()
113 end
114 end
115 local primes = expression:primefactorization()
116 local coeffresult = {}
117 local exprresult = {}
118 local reduction = root
119 for _, term in ipairs(primes.expressions) do
120 local primepower = term.expressions[2]
121 reduction = Integer.gcd(primepower,reduction)
122 if reduction == Integer.one() then
123 goto skip
124 end
125 end
126 ::skip::
127 local newroot = root / reduction
128 for index, term in ipairs(primes.expressions) do
129 local prime = term.expressions[1]
130 local primepower = term.expressions[2] / reduction
131 local coeffpower = primepower // newroot
132 coeffresult[index] = prime ^ coeffpower
133 local exprpower = primepower - coeffpower*newroot
134 exprresult[index] = prime ^ exprpower
135 end
136 local newexpression = BinaryOperation(BinaryOperation.MUL,exprresult):autosimplify()
137 local coeff = BinaryOperation(BinaryOperation.MUL,coeffresult):autosimplify()
138 if coeff == Integer.one() then
139 if reduction == Integer.one() then
140 goto stop
141 end
142 return SqrtExpression(newexpression,newroot)
143 end
144 if newroot == Integer.one() then
145 return coeff
146 end
147 return BinaryOperation(BinaryOperation.MUL,{coeff,SqrtExpression(newexpression,newroot)}):autosimplify()
148 end
149 ::stop::
150
151 if expression.operation == BinaryOperation.POW and expression.expressions[2]:type() == Integer then
152 local exponent = expression.expressions[2]
153 local power = exponent // root
154 local newexponent = (exponent / root) - power
155 local coeff = expression.expressions[1] ^ power
156 coeff = coeff:evaluate()
157 if newexponent == Integer.zero() then
158 return coeff
159 else
160 local num = newexponent.numerator
161 local den = newexponent.denominator
162 local newexpression = expression ^ num
163 newexpression = newexpression:autosimplify()
164 local result = coeff * SqrtExpression(newexpression,den)
165 return result
166 end
167 end
168
169 return SqrtExpression(expression,root)
170 end
171
172 function SqrtExpression:tolatex()
173 local printout = '\\sqrt'
174 if self.root == Integer(2) then
175 printout = printout .. '{' .. self.expression:tolatex() .. '}'
176 else
177 printout = printout .. '[' .. self.root:tolatex() .. ']' .. '{' .. self.expression:tolatex() .. '}'
178 end
179 return printout
180 end
181
182
183 -----------------
184 -- Inheritance --
185 -----------------
186 __SqrtExpression.__index = CompoundExpression
187 __SqrtExpression.__call = SqrtExpression.new
188 SqrtExpression = setmetatable(SqrtExpression, __SqrtExpression)
189
190 ----------------------
191 -- Static constants --
192 ----------------------
193
194 sqrt = function(expression, root)
195 return SqrtExpression(expression, root)
196 end