summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/lualatex/luacas/core/luacas-functionexpression.lua
blob: dc94790b816acd3727ea8f132a1d9c6e0896c94f (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
--- @class FunctionExpression
--- Represents a generic function that takes zero or more expressions as inputs.
--- @field name SymbolExpression
--- @field expressions table<number, Expression>
--- @field orders table<number, Integer>
--- @field variables table<number,SymbolExpression>
--- @alias Function FunctionExpression
FunctionExpression = {}
__FunctionExpression = {}

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

--- Creates a new function expression with the given operation.
--- @param name string|SymbolExpression
--- @param expressions table<number, Expression>
--- @param derivatives table<number,Integer>
--- @return FunctionExpression
function FunctionExpression:new(name, expressions, derivatives)
    local o = {}
    local __o = Copy(__ExpressionOperations)

    if type(name) == "table" and name:type() == SymbolExpression then
        name = name.symbol
    end

    if TrigExpression.NAMES[name] and #expressions == 1 then
        return TrigExpression(name, expressions[1])
    end

    -- TODO: Symbol Checking For Constructing derivatives like this
    --if string.sub(name, #name, #name) == "'" and #expressions == 1 then
    --   return DerivativeExpression(FunctionExpression(string.sub(name, 1, #name - 1), expressions), SymbolExpression("x"), true)
    --end

    o.name = name
    o.expressions = Copy(expressions)
    o.variables = Copy(expressions)
    for _,expression in ipairs(o.variables) do
        if not expression:isatomic() then
            o.variables = {}
            if #o.expressions < 4 then
                local defaultvars = {SymbolExpression('x'),SymbolExpression('y'),SymbolExpression('z')}
                for i=1,#o.expressions do
                    o.variables[i] = defaultvars[i]
                end
            else
                for i=1,#o.expressions do
                    o.variables[i] = SymbolExpression('x_'..tostring(i))
                end
            end
        end
    end
    if derivatives then
        o.derivatives = Copy(derivatives)
    else
        o.derivatives = {}
        for i=1,#o.variables do
            o.derivatives[i] = Integer.zero()
        end
    end

    __o.__index = FunctionExpression
    __o.__tostring = function(a)
        local total = Integer.zero()
        for _,integer in ipairs(a.derivatives) do
            total = total + integer
        end
        if total == Integer.zero() then
            local out = a.name .. '('
            for index, expression in ipairs(a.expressions) do
                out = out .. tostring(expression)
                if a.expressions[index + 1] then
                    out = out .. ', '
                end
            end
            return out .. ')'
        else
            local out = 'd'
            if total > Integer.one() then
                out = out ..'^' .. tostring(total)
            end
            out = out .. a.name .. '/'
            for index,integer in ipairs(a.derivatives) do
                if integer > Integer.zero() then
                    out = out .. 'd' .. tostring(a.variables[index])
                    if integer > Integer.one() then
                        out = out .. '^' .. tostring(integer)
                    end
                end
            end
            out = out .. '('
            for index, expression in ipairs(a.expressions) do
                out = out .. tostring(expression)
                if a.expressions[index + 1] then
                    out = out .. ', '
                end
            end
            return out .. ')'
        end
    end
    __o.__eq = function(a, b)
        -- if b:type() == TrigExpression then
        --     return a == b:tofunction()
        -- end
        if b:type() ~= FunctionExpression then
            return false
        end
        if #a.expressions ~= #b.expressions then
            return false
        end
        for index, _ in ipairs(a.expressions) do
            if a.expressions[index] ~= b.expressions[index] then
                return false
            end
        end
        for index,_ in ipairs(a.derivatives) do
            if a.derivatives[index] ~= b.derivatives[index] then
                return false
            end
        end
        return a.name == b.name
    end

    o = setmetatable(o, __o)

    return o
end

--- @return FunctionExpression
function FunctionExpression:evaluate()
    local results = {}
    for index, expression in ipairs(self:subexpressions()) do
        results[index] = expression:evaluate()
    end
    local result = FunctionExpression(self.name, results, self.derivatives)
    result.variables = self.variables
    return result
end

--- @return FunctionExpression
function FunctionExpression:autosimplify()
    -- Since the function is completely generic, we can't really do anything execpt autosimplify subexpressions.
    local results = {}
    for index, expression in ipairs(self:subexpressions()) do
        results[index] = expression:autosimplify()
    end
    local result = FunctionExpression(self.name, results, self.derivatives)
    result.variables = self.variables
    return result
end

--- @return table<number, Expression>
function FunctionExpression:subexpressions()
    return self.expressions
end

--- @param subexpressions table<number, Expression>
--- @return FunctionExpression
function FunctionExpression:setsubexpressions(subexpressions)
    local result = FunctionExpression(self.name, subexpressions, self.derivatives)
    result.variables = self.variables
    return result
end

--- @param other Expression
--- @return boolean
function FunctionExpression:order(other)
    if other:isatomic() then
        return false
    end

    -- CASC Autosimplfication has some symbols appearing before functions, but that looks bad to me, so all symbols appear before products now.
    -- if other:type() == SymbolExpression then
    --     return SymbolExpression(self.name):order(other)
    -- end

    if other:type() == BinaryOperation then
        if other.operation == BinaryOperation.ADD or other.operation == BinaryOperation.MUL then
            return BinaryOperation(other.operation, {self}):order(other)
        end

        if other.operation == BinaryOperation.POW then
            return (self^Integer.one()):order(other)
        end
    end

    if other:type() == SqrtExpression then
        return self:order(other:topower())
    end

    -- TODO: Make Logarithm and AbsExpression inherit from function expression to reduce code duplication
    if other:type() == Logarithm then
        return self:order(FunctionExpression("log", {other.base, other.expression}))
    end

    if other:type() ~= FunctionExpression and other:type() ~= TrigExpression then
        return true
    end

    if self.name ~= other.name then
        return SymbolExpression(self.name):order(SymbolExpression(other.name))
    end

    local k = 1
    while self:subexpressions()[k] and other:subexpressions()[k] do
        if self:subexpressions()[k] ~= other:subexpressions()[k] then
            return self:subexpressions()[k]:order(other:subexpressions()[k])
        end
        k = k + 1
    end
    return #self.expressions < #other.expressions
end

--- @return string
function FunctionExpression:tolatex()
    local out = tostring(self.name)
    if self:type() == TrigExpression then
        out = "\\" .. out
    end
    if self:type() ~= TrigExpression and #self.name>1 then
        --if out:sub(2,2) ~= "'" then
            --local fp = out:find("'")
            --if fp then
            --    out = '\\operatorname{' .. out:sub(1,fp-1) .. '}' .. out:sub(fp,-1)
            --else
        out = '\\operatorname{' .. out .. '}'
            --end
        --end
    end
    local total = Integer.zero()
    for _,integer in ipairs(self.derivatives) do
        total = total + integer
    end
    if #self.expressions == 1 then
        if total == Integer.zero() then
            goto continue
        else
            if total < Integer(5) then
                while total > Integer.zero() do
                    out = out .. "'"
                    total = total - Integer.one()
                end
            else
                out = out .. '^{(' .. total:tolatex() .. ')}'
            end
        end
    end
    if #self.expressions > 1 then
        if total == Integer.zero() then
            goto continue
        else
            if total < Integer(4) then
                out = out .. '_{'
                for index,integer in ipairs(self.derivatives) do
                    local i = integer:asnumber()
                    while i > 0 do
                        out = out .. self.variables[index]:tolatex()
                        i = i - 1
                    end
                end
                out = out .. '}'
            else
                out = '\\frac{\\partial^{' .. total:tolatex() .. '}' .. out .. '}{'
                for index, integer in ipairs(self.derivatives) do
                    if integer > Integer.zero() then
                        out = out .. '\\partial ' .. self.variables[index]:tolatex()
                        if integer ~= Integer.one() then
                            out = out .. '^{' .. integer:tolatex() .. '}'
                        end
                    end
                end
                out = out .. '}'
            end
        end
    end
    ::continue::
    out = out ..'\\mathopen{}' .. '\\left('
    for index, expression in ipairs(self:subexpressions()) do
        out = out .. expression:tolatex()
        if self:subexpressions()[index + 1] then
            out = out .. ', '
        end
    end
    return out .. '\\right)'
end

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

__FunctionExpression.__index = CompoundExpression
__FunctionExpression.__call = FunctionExpression.new
FunctionExpression = setmetatable(FunctionExpression, __FunctionExpression)