Debian Codegraph @

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

raw
1 --- @class Logarithm
2 --- An expression for the logarithm of an expression with respect to another.
3 --- Currently, logarithms are not being evaluated since we are just doing symbolic computation.
4 --- @field base Expression
5 --- @field expression Expression
6 Logarithm = {}
7 local __Logarithm = {}
8
9 ----------------------------
10 -- Instance functionality --
11 ----------------------------
12
13 --- Creates a new logarithm expression with the given symbol and expression.
14 --- @param base Expression
15 --- @param expression Expression
16 --- @
17 function Logarithm:new(base, expression)
18 local o = {}
19 local __o = Copy(__ExpressionOperations)
20
21 o.base = Copy(base)
22 o.expression = Copy(expression)
23
24 __o.__index = Logarithm
25 __o.__tostring = function(a)
26 return 'log(' .. tostring(base) .. ', ' .. tostring(expression) .. ')'
27 end
28 __o.__eq = function(a, b)
29 -- 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
30 if not b:type() == Logarithm then
31 return false
32 end
33 return a.base == b.base and a.expression == b.expression
34 end
35 o = setmetatable(o, __o)
36
37 return o
38 end
39
40 --- @return Expression
41 function Logarithm:evaluate()
42
43 if (self.base:isconstant() and (self.base <= Integer.zero() or self.base == Integer.one())) or
44 (self.expression:isconstant() and self.expression <= Integer.zero()) then
45 error("Arithmetic error: division by zero")
46 end
47
48 if not self.base:isconstant() or not self.expression:isconstant() then
49 return self
50 end
51
52 local power = Integer.one()
53 local base = self.base
54 if base:type() == Integer then
55 local pp, b, p = base:isperfectpower()
56 if pp then
57 base = b
58 power = p
59 end
60 end
61
62 if base:type() == Rational then
63 local ppn, bn, pn = base.numerator:isperfectpower()
64 local ppd, bd, pd = base.denominator:isperfectpower()
65 if base.numerator == Integer.one() then
66 ppn = true
67 bn = Integer.one()
68 pn = pd
69 end
70 if ppn and ppd and pn == pd then
71 base = bn / bd
72 power = pn
73 end
74 end
75
76 local result = Integer.one()
77 local expression = self.expression
78 local sign = Integer.one()
79 if base < Integer.one() then
80 base = Integer.one() / base
81 sign = -sign
82 end
83
84
85 local current = base
86 while current < expression do
87 current = current * base
88 result = result + Integer.one()
89 end
90 if current == expression then
91 return sign * result / power
92 else
93 while current > expression do
94 current = current / base
95 result = result - Integer.one()
96 end
97 if current == expression then
98 return sign * result / power
99 end
100 end
101
102 return self
103 end
104
105 --- @return Expression
106 function Logarithm:autosimplify()
107
108 local base = self.base:autosimplify()
109 local expression = self.expression:autosimplify()
110
111 local evaluated = Logarithm(base, expression):evaluate()
112 if evaluated:type() ~= Logarithm then
113 return evaluated
114 end
115
116 -- Uses the property that log(b, 1) = 0
117 if expression == Integer.one() then
118 return Integer.zero()
119 end
120
121 -- Uses the property that log(b, b) = 1
122 if expression == base then
123 return Integer.one()
124 end
125
126 -- Uses the propery that log(b, x^y) = y * log(b, x)
127 if expression.operation == BinaryOperation.POW then
128 return BinaryOperation.MULEXP({expression.expressions[2], Logarithm(base, expression.expressions[1])}):autosimplify()
129 end
130
131 if expression:type() == Rational and expression.numerator == Integer.one() then
132 return (-Logarithm(base, expression.denominator)):autosimplify()
133 end
134
135 -- Our expression cannot be simplified
136 return Logarithm(base, expression)
137 end
138
139 --- @return Expression
140 function Logarithm:expand()
141 return Logarithm(self.base:expand(), self.expression:expand()):autosimplify()
142 end
143
144 --- @return table<number, Expression>
145 function Logarithm:subexpressions()
146 return {self.base, self.expression}
147 end
148
149 --- @param subexpressions table<number, Expression>
150 --- @return Logarithm
151 function Logarithm:setsubexpressions(subexpressions)
152 return Logarithm(subexpressions[1], subexpressions[2])
153 end
154
155 --- @param other Expression
156 --- @return boolean
157 function Logarithm:order(other)
158 return FunctionExpression("log", {self.base, self.expression}):order(other)
159 end
160
161 --- @return string
162 function Logarithm:tolatex()
163 if self.base == E then
164 return '\\ln\\mathopen{}\\left(' .. self.expression:tolatex() .. '\\right)'
165 end
166 return '\\log_' .. self.base:tolatex() .. '\\mathopen{}\\left(' .. self.expression:tolatex() .. '\\right)'
167 end
168
169 -----------------
170 -- Inheritance --
171 -----------------
172 __Logarithm.__index = CompoundExpression
173 __Logarithm.__call = Logarithm.new
174 Logarithm = setmetatable(Logarithm, __Logarithm)
175
176 ----------------------
177 -- Static constants --
178 ----------------------
179
180 LOG = function(base, expression)
181 return Logarithm(base, expression)
182 end
183
184 LN = function(expression)
185 return Logarithm(E, expression)
186 end