Debian Codegraph @

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

raw
1 --- @class RootExpression
2 --- An expression that represents the solutions to expression = 0.
3 --- @field expression Expression
4 RootExpression = {}
5 local __RootExpression = {}
6
7 ----------------------------
8 -- Instance functionality --
9 ----------------------------
10
11 --- Creates a new root expression with the given expression.
12 --- @param expression Expression
13 --- @return RootExpression
14 function RootExpression:new(expression)
15 local o = {}
16 local __o = Copy(__ExpressionOperations)
17
18 o.expression = Copy(expression)
19
20 __o.__index = RootExpression
21 __o.__tostring = function(a)
22 return 'Root Of: (' .. tostring(a.expression) .. ')'
23 end
24 __o.__eq = function(a, b)
25 -- 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
26 if not b:type() == RootExpression then
27 return false
28 end
29 return a.expression == b.expression
30 end
31 o = setmetatable(o, __o)
32
33 return o
34 end
35
36 --- @return Expression
37 function RootExpression:autosimplify(subpart)
38 local simplified = self.expression:autosimplify()
39 local simplified, ispoly = simplified:topolynomial()
40
41 if simplified:isconstant() then
42 -- 0 = 0 is always true (obviously).
43 return simplified == simplified:zero()
44 end
45
46 if ispoly then
47 if simplified.degree == Integer.zero() then
48 return simplified == simplified:zero()
49 end
50 if simplified.degree == Integer.one() then
51 return {-simplified.coefficients[0] / simplified.coefficients[1]}
52 end
53 if simplified.degree == Integer(2) then
54 local a = simplified.coefficients[2]
55 local b = simplified.coefficients[1]
56 local c = simplified.coefficients[0]
57 -- This is a hack until we can get more expression manipulation working, but that's okay.
58 if subpart then
59 c = (c - subpart):autosimplify()
60 end
61 return {((-b + sqrt(b^Integer(2) - Integer(4) * a * c)) / (Integer(2) * a)):autosimplify(),
62 ((-b - sqrt(b^Integer(2) - Integer(4) * a * c)) / (Integer(2) * a)):autosimplify()}
63 end
64 if simplified.degree == Integer(3) then
65 local a = simplified.coefficients[3]
66 local b = simplified.coefficients[2]
67 local c = simplified.coefficients[1]
68 local d = simplified.coefficients[0]
69 -- This is a hack until we can get more expression manipulation working, but that's okay.
70 if subpart then
71 d = (d - subpart):autosimplify()
72 end
73
74 local delta0 = (b^Integer(2) - Integer(3)*a*c):autosimplify()
75 local delta1 = (Integer(2) * b^Integer(3) - Integer(9)*a*b*c+Integer(27)*a^Integer(2)*d):autosimplify()
76
77 local C = sqrt((delta1 + sqrt(delta1 ^ Integer(2) - Integer(4) * delta0 ^ Integer(3))) / Integer(2), Integer(3)):autosimplify()
78
79 if C == Integer.zero() then
80 C = sqrt((delta1 - sqrt(delta1 ^ Integer(2) - Integer(4) * delta0 ^ Integer(3))) / Integer(2), Integer(3)):autosimplify()
81 end
82
83 if C == Integer.zero() then
84 C = (-b/(Integer(3)*a)):autosimplify()
85 end
86
87 local eta = ((Integer(-1) + sqrt(Integer(-3))) / Integer(2)):autosimplify()
88
89 return {((-Integer.one() / (Integer(3) * a)) * (b + C + delta0 / C)):autosimplify(),
90 ((-Integer.one() / (Integer(3) * a)) * (b + C*eta + delta0 / (C*eta))):autosimplify(),
91 ((-Integer.one() / (Integer(3) * a)) * (b + C*eta^Integer(2) + delta0 / (C*eta^Integer(2)))):autosimplify()}
92 end
93 end
94 if ispoly then
95 simplified = simplified:autosimplify()
96 end
97 if subpart then
98 simplified = (simplified - subpart):autosimplify()
99 end
100 return {RootExpression(simplified)}
101 end
102
103 --- @return table<number, Expression>
104 function RootExpression:subexpressions()
105 return {self.expression}
106 end
107
108 --- @param subexpressions table<number, Expression>
109 --- @return RootExpression
110 function RootExpression:setsubexpressions(subexpressions)
111 return RootExpression(subexpressions[1])
112 end
113
114 --- @param other Expression
115 --- @return boolean
116 function RootExpression:order(other)
117 --- TODO: Fix ordering on new expression types
118 if other:type() ~= RootExpression then
119 return false
120 end
121
122 return self.expression:order(other.expression)
123 end
124
125 --- @return string
126 function RootExpression:tolatex()
127 return '\\operatorname{RootOf}\\left(' .. self.expression:tolatex() .. '\\right)'
128 end
129
130 -----------------
131 -- Inheritance --
132 -----------------
133 __RootExpression.__index = CompoundExpression
134 __RootExpression.__call = RootExpression.new
135 RootExpression = setmetatable(RootExpression, __RootExpression)