summaryrefslogtreecommitdiff
path: root/macros/luatex/latex/luacas/tex/algebra/luacas-trigexpression.lua
blob: 307bfe17379cb22ce2c72acf50faafeda3c45094 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
--- @class TrigExpression
--- Represents a trigonometric function from one expression to another.
--- @field name string
--- @field expression Expression
TrigExpression = {}
__TrigExpression = {}

----------------------------
-- Instance functionality --
----------------------------

--- Creates a new trig expression with the given name and expression.
--- @param name string|SymbolExpression
--- @param expression Expression
--- @return TrigExpression
function TrigExpression:new(name, expression)
    local o = {}
    local __o = Copy(__ExpressionOperations)

    if not TrigExpression.NAMES[name] then
        error("Argument error: " .. name .. " is not the name of a trigonometric function.")
    end

    o.name = name
    o.expression = expression
    o.expressions = {expression}
    if expression:isatomic() then
        o.variables = {expression}
    else
        o.variables = {SymbolExpression('x')}
    end
    o.derivatives = {Integer.zero()}

    __o.__index = TrigExpression
    __o.__tostring = function(a)
        return tostring(a.name) .. '(' .. tostring(a.expression) .. ')'
    end
    __o.__eq = function(a, b)
        -- if b:type() == FunctionExpression then
        --     return a:tofunction() == b
        -- end
        -- 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
        if not b:type() == TrigExpression then
            return false
        end
        return a.name == b.name and a.expression == b.expression
    end

    o = setmetatable(o, __o)
    return o
end

--- @return TrigExpression
function TrigExpression:evaluate()
    local expression = self.expression:autosimplify()

    if expression == Integer.zero() then
        if self.name == "cos" or self.name == "sec" then
            return Integer.one()
         end
         if self.name == "sin" or self.name == "tan" then
            return Integer.zero()
         end
         if self.name == "arctan" or self.name == "arcsin" then
             return Integer.zero()
         end
         if self.name == "arccos" or self.name == "arccot" then
            return PI / Integer(2)
         end
     end

     if expression == PI then
        if self.name == "cos" or self.name == "sec" then
            return Integer(-1)
         end
         if self.name == "sin" or self.name == "tan" then
            return Integer.zero()
         end
     end

     if expression:ismulratlPI() then
        local coeff = expression.expressions[1]
         if TrigExpression.COSVALUES[tostring(coeff)] ~= nil then
             if self.name == "cos" then
                return TrigExpression.COSVALUES[tostring(coeff)]:autosimplify()
             end
             if self.name == "sin" then
                local sign = Integer.one()
                 if coeff > Integer.one() then
                    sign = Integer(-1)
                 end
                return (sign*sqrt(Integer.one()-cos(expression)^Integer(2))):autosimplify()
             end
             if self.name == "tan" then
                return (sin(expression) / cos(expression)):autosimplify()
             end
             if self.name == "sec" then
                return (Integer.one() / cos(expression)):autosimplify()
             end
             if self.name == "csc" then
                return (Integer.one() / sin(expression)):autosimplify()
             end
             if self.name == "cot" then
                return (cos(expression) / sin(expression)):autosimplify()
             end
         end
     end

     if TrigExpression.ACOSVALUES[tostring(expression)] ~= nil then
        if self.name == "arccos" then
            return TrigExpression.ACOSVALUES[tostring(expression)]:autosimplify()
         end
         if self.name == "arcsin" then
            if expression == Integer(-1) then
                return TrigExpression.ACOSVALUES["-1"]:autosimplify()
             elseif expression.expressions and expression.expressions[1] == Integer(-1) then
                local expr = (Integer(-1)*sqrt(Integer.one() - expression ^ Integer(2))):autosimplify()
                 return TrigExpression.ACOSVALUES[tostring(expr)]:autosimplify()
             else
                 local expr = (sqrt(Integer.one() - expression ^ Integer(2))):autosimplify()
                 return TrigExpression.ACOSVALUES[tostring(expr)]:autosimplify()
             end
         end
     end

     if self.name == "arctan" and TrigExpression.ATANVALUES[tostring(expression)] ~= nil then
        return TrigExpression.ATANVALUES[tostring(expression)]:autosimplify()
    end

    return self
end

--- checks if expression is a rational multiple of pi
--- @return boolean
function Expression:ismulratlPI()
    if self.operation == BinaryOperation.MUL and #self.expressions == 2 and (self.expressions[1]:type() == Integer or self.expressions[1]:type() == Rational) and self.expressions[2] == PI then
        return true
    end

    return false
end

--- @return TrigExpression
function TrigExpression:autosimplify()
    local expression = self.expression:autosimplify()

    -- even and odd properties of trig functions
    if (self.name == "sin" or self.name == "tan" or self.name == "csc" or self.name == "cot") and
        expression.operation == BinaryOperation.MUL and expression.expressions[1]:isconstant() and expression.expressions[1] < Integer(0) then
        return (-Integer.one() * TrigExpression(self.name, -expression)):autosimplify()
    end

    if (self.name == "cos" or self.name == "sec") and
        expression.operation == BinaryOperation.MUL and expression.expressions[1]:isconstant() and expression.expressions[1] < Integer(0) then
        expression = (-expression):autosimplify()
    end

    -- uses periodicity of sin and cos and friends
    if self.name == "sin" or self.name == "cos" or self.name == "csc" or self.name == "sec" then
       if expression == Integer.zero() or expression == PI then
           goto skip
        end
        if expression.operation ~= BinaryOperation.ADD then
           expression = BinaryOperation(BinaryOperation.ADD,{expression})
        end
        for index,component in ipairs(expression.expressions) do
           if component:ismulratlPI() then
               local coeff = component.expressions[1]
                if coeff:type() == Integer then
                   coeff = coeff % Integer(2)
                    coeff = coeff:autosimplify()
                end
               if coeff:type() == Rational then
                   local n = coeff.numerator
                    local d = coeff.denominator
                   local m = {n:divremainder(d)}
                    coeff = (m[1] % Integer(2)) + m[2]/d
                    coeff = coeff:autosimplify()
                end
                expression.expressions[index].expressions[1] = coeff
            end
            expression = expression:autosimplify()
        end
        ::skip::
    end

    -- uses periodicity of tan and cot
    if self.name == "tan" or self.name == "cot" then
       if expression == Integer.zero() or expression == PI then
           goto skip
        end
        if expression.operation ~= BinaryOperation.ADD then
            expression = BinaryOperation(BinaryOperation.ADD,{expression})
        end
        for index,component in ipairs(expression.expressions) do
           if component:ismulratlPI() then
               local coeff = component.expressions[1]
                if coeff:type() == Integer then
                   coeff = Integer.zero()
                end
               if coeff:type() == Rational then
                   local n = coeff.numerator
                    local d = coeff.denominator
                   local m = {n:divremainder(d)}
                    coeff = m[2]/d
                    coeff = coeff:autosimplify()
                end
                expression.expressions[index].expressions[1] = coeff
            end
            if component == PI then
               expression.expressions[index] = Integer.zero()
            end
        end
        expression = expression:autosimplify()
        ::skip::
    end

    return TrigExpression(self.name, expression):evaluate()
end

--- @return table<number, Expression>
function TrigExpression:subexpressions()
    return {self.expression}
end

--- @param subexpressions table<number, Expression>
--- @return TrigExpression
function TrigExpression:setsubexpressions(subexpressions)
    return TrigExpression(self.name, subexpressions[1])
end

-- function TrigExpression:freeof(symbol)
--     return self.expression:freeof(symbol)
-- end

-- function TrigExpression:substitute(map)
--     for expression, replacement in pairs(map) do
--         if self == expression then
--             return replacement
--         end
--     end
--     return TrigExpression(self.name, self.expression:substitute(map))
-- end

-- function TrigExpression:order(other)
--     return self:tofunction():order(other)
-- end

-- function TrigExpression:tofunction()
--     return FunctionExpression(self.name, {self.expression}, true)
-- end

-----------------
-- Inheritance --
-----------------

__TrigExpression.__index = FunctionExpression
__TrigExpression.__call = TrigExpression.new
TrigExpression = setmetatable(TrigExpression, __TrigExpression)

----------------------
-- Static constants --
----------------------
TrigExpression.NAMES = {sin=1, cos=2, tan=3, csc=4, sec=5, cot=6,
                         arcsin=7, arccos=8, arctan=9, arccsc=10, arcsec=11, arccot=12}

TrigExpression.INVERSES = {sin="arcsin", cos="arccos", tan="arctan", csc="arccsc", sec="arcsec", cot="arccot",
                            arcsin="sin", arccos="cos", arctan="tan", arccsc="csc", arcsec="sec", arccot="cot"}

TrigExpression.COSVALUES = {
    ["0"] = Integer.one(),
    ["1/6"] = sqrt(Integer(3))/Integer(2),
    ["1/4"] = sqrt(Integer(2))/Integer(2),
    ["1/3"] = Integer.one()/Integer(2),
    ["1/2"] = Integer.zero(),
    ["2/3"] = -Integer.one()/Integer(2),
    ["3/4"] = -sqrt(Integer(2))/Integer(2),
    ["5/6"] = -sqrt(Integer(3))/Integer(2),
    ["1"]   = -Integer.one(),
    ["7/6"] = -sqrt(Integer(3))/Integer(2),
    ["5/4"] = -sqrt(Integer(2))/Integer(2),
    ["4/3"] = -Integer.one()/Integer(2),
    ["3/2"] = Integer.zero(),
    ["5/3"] = Integer.one()/Integer(2),
    ["7/4"] = sqrt(Integer(2))/Integer(2),
    ["11/6"] = sqrt(Integer(3))/Integer(2),
}
TrigExpression.ACOSVALUES = {
    ["1"]         = Integer.zero(),
    ["(1/2 * sqrt(3,2))"] = PI * Integer(6) ^ Integer(-1),
    ["(1/2 * sqrt(2,2))"] = PI * Integer(4) ^ Integer(-1),
    ["1/2"]       = PI * Integer(3) ^ Integer(-1),
    ["0"]         = PI * Integer(2) ^ Integer(-1),
    ["-1/2"]      = PI * Integer(2) * Integer(3) ^ Integer(-1),
    ["(-1/2 * sqrt(2,2))"]= PI * Integer(3) * Integer(4) ^ Integer(-1),
    ["(-1/2 * sqrt(3,2))"]= PI * Integer(5) * Integer(6) ^ Integer(-1),
    ["-1"]        = Integer(-1)*PI,
}
TrigExpression.ATANVALUES = {
    ["(-1 * sqrt(3,2))"] = Integer(-1) * PI * Integer(3) ^ Integer(-1),
    ["-1"] = Integer(-1) * PI * Integer(4) ^ Integer(-1),
    ["(-1/3 * sqrt(3,2))"] = Integer(-1) * Integer(6) ^ Integer(-1),
    ["0"] = Integer.zero(),
    ["(1/3 * sqrt(3,2))"] = PI * Integer(6) ^ Integer(-1),
    ["1"] = PI * Integer(4) ^ Integer(-1),
    ["sqrt(3,2)"] = PI * Integer(3) ^ Integer(-1)
}

SIN = function (a)
    return TrigExpression("sin", a)
end

COS = function (a)
    return TrigExpression("cos", a)
end

TAN = function (a)
    return TrigExpression("tan", a)
end

CSC = function (a)
    return TrigExpression("csc", a)
end

SEC = function (a)
    return TrigExpression("sec", a)
end

COT = function (a)
    return TrigExpression("cot", a)
end

ARCSIN = function (a)
    return TrigExpression("arcsin", a)
end

ARCCOS = function (a)
    return TrigExpression("arccos", a)
end

ARCTAN = function (a)
    return TrigExpression("arctan", a)
end

ARCCSC = function (a)
    return TrigExpression("arccsc", a)
end

ARCSEC = function (a)
    return TrigExpression("arcsec", a)
end

ARCCOT = function (a)
    return TrigExpression("arccot", a)
end