| 1 |
--- @class FunctionExpression |
| 2 |
--- Represents a generic function that takes zero or more expressions as inputs. |
| 3 |
--- @field name SymbolExpression |
| 4 |
--- @field expressions table<number, Expression> |
| 5 |
--- @field orders table<number, Integer> |
| 6 |
--- @field variables table<number,SymbolExpression> |
| 7 |
--- @alias Function FunctionExpression |
| 8 |
FunctionExpression = {} |
| 9 |
local __FunctionExpression = {} |
| 10 |
|
| 11 |
---------------------------- |
| 12 |
-- Instance functionality -- |
| 13 |
---------------------------- |
| 14 |
|
| 15 |
--- Creates a new function expression with the given operation. |
| 16 |
--- @param name string|SymbolExpression |
| 17 |
--- @param expressions table<number, Expression> |
| 18 |
--- @param derivatives table<number,Integer> |
| 19 |
--- @return FunctionExpression |
| 20 |
function FunctionExpression:new(name, expressions, derivatives) |
| 21 |
local o = {} |
| 22 |
local __o = Copy(__ExpressionOperations) |
| 23 |
|
| 24 |
if type(name) == "table" and name:type() == SymbolExpression then |
| 25 |
name = name.symbol |
| 26 |
end |
| 27 |
|
| 28 |
if TrigExpression.NAMES[name] and #expressions == 1 then |
| 29 |
return TrigExpression(name, expressions[1]) |
| 30 |
end |
| 31 |
|
| 32 |
-- TODO: Symbol Checking For Constructing derivatives like this |
| 33 |
--if string.sub(name, #name, #name) == "'" and #expressions == 1 then |
| 34 |
-- return DerivativeExpression(FunctionExpression(string.sub(name, 1, #name - 1), expressions), SymbolExpression("x"), true) |
| 35 |
--end |
| 36 |
|
| 37 |
o.name = name |
| 38 |
o.expressions = Copy(expressions) |
| 39 |
o.variables = Copy(expressions) |
| 40 |
for _,expression in ipairs(o.variables) do |
| 41 |
if not expression:isatomic() then |
| 42 |
o.variables = {} |
| 43 |
if #o.expressions < 4 then |
| 44 |
local defaultvars = {SymbolExpression('x'),SymbolExpression('y'),SymbolExpression('z')} |
| 45 |
for i=1,#o.expressions do |
| 46 |
o.variables[i] = defaultvars[i] |
| 47 |
end |
| 48 |
else |
| 49 |
for i=1,#o.expressions do |
| 50 |
o.variables[i] = SymbolExpression('x_'..tostring(i)) |
| 51 |
end |
| 52 |
end |
| 53 |
end |
| 54 |
end |
| 55 |
if derivatives then |
| 56 |
o.derivatives = Copy(derivatives) |
| 57 |
else |
| 58 |
o.derivatives = {} |
| 59 |
for i=1,#o.variables do |
| 60 |
o.derivatives[i] = Integer.zero() |
| 61 |
end |
| 62 |
end |
| 63 |
|
| 64 |
__o.__index = FunctionExpression |
| 65 |
__o.__tostring = function(a) |
| 66 |
local total = Integer.zero() |
| 67 |
for _,integer in ipairs(a.derivatives) do |
| 68 |
total = total + integer |
| 69 |
end |
| 70 |
if total == Integer.zero() then |
| 71 |
local out = a.name .. '(' |
| 72 |
for index, expression in ipairs(a.expressions) do |
| 73 |
out = out .. tostring(expression) |
| 74 |
if a.expressions[index + 1] then |
| 75 |
out = out .. ', ' |
| 76 |
end |
| 77 |
end |
| 78 |
return out .. ')' |
| 79 |
else |
| 80 |
local out = 'd' |
| 81 |
if total > Integer.one() then |
| 82 |
out = out ..'^' .. tostring(total) |
| 83 |
end |
| 84 |
out = out .. a.name .. '/' |
| 85 |
for index,integer in ipairs(a.derivatives) do |
| 86 |
if integer > Integer.zero() then |
| 87 |
out = out .. 'd' .. tostring(a.variables[index]) |
| 88 |
if integer > Integer.one() then |
| 89 |
out = out .. '^' .. tostring(integer) |
| 90 |
end |
| 91 |
end |
| 92 |
end |
| 93 |
out = out .. '(' |
| 94 |
for index, expression in ipairs(a.expressions) do |
| 95 |
out = out .. tostring(expression) |
| 96 |
if a.expressions[index + 1] then |
| 97 |
out = out .. ', ' |
| 98 |
end |
| 99 |
end |
| 100 |
return out .. ')' |
| 101 |
end |
| 102 |
end |
| 103 |
__o.__eq = function(a, b) |
| 104 |
-- if b:type() == TrigExpression then |
| 105 |
-- return a == b:tofunction() |
| 106 |
-- end |
| 107 |
if b:type() ~= FunctionExpression then |
| 108 |
return false |
| 109 |
end |
| 110 |
if #a.expressions ~= #b.expressions then |
| 111 |
return false |
| 112 |
end |
| 113 |
for index, _ in ipairs(a.expressions) do |
| 114 |
if a.expressions[index] ~= b.expressions[index] then |
| 115 |
return false |
| 116 |
end |
| 117 |
end |
| 118 |
for index,_ in ipairs(a.derivatives) do |
| 119 |
if a.derivatives[index] ~= b.derivatives[index] then |
| 120 |
return false |
| 121 |
end |
| 122 |
end |
| 123 |
return a.name == b.name |
| 124 |
end |
| 125 |
|
| 126 |
o = setmetatable(o, __o) |
| 127 |
|
| 128 |
return o |
| 129 |
end |
| 130 |
|
| 131 |
--- @return FunctionExpression |
| 132 |
function FunctionExpression:evaluate() |
| 133 |
local results = {} |
| 134 |
for index, expression in ipairs(self:subexpressions()) do |
| 135 |
results[index] = expression:evaluate() |
| 136 |
end |
| 137 |
local result = FunctionExpression(self.name, results, self.derivatives) |
| 138 |
result.variables = self.variables |
| 139 |
return result |
| 140 |
end |
| 141 |
|
| 142 |
--- @return FunctionExpression |
| 143 |
function FunctionExpression:autosimplify() |
| 144 |
-- Since the function is completely generic, we can't really do anything execpt autosimplify subexpressions. |
| 145 |
local results = {} |
| 146 |
for index, expression in ipairs(self:subexpressions()) do |
| 147 |
results[index] = expression:autosimplify() |
| 148 |
end |
| 149 |
local result = FunctionExpression(self.name, results, self.derivatives) |
| 150 |
result.variables = self.variables |
| 151 |
return result |
| 152 |
end |
| 153 |
|
| 154 |
--- @return table<number, Expression> |
| 155 |
function FunctionExpression:subexpressions() |
| 156 |
return self.expressions |
| 157 |
end |
| 158 |
|
| 159 |
--- @param subexpressions table<number, Expression> |
| 160 |
--- @return FunctionExpression |
| 161 |
function FunctionExpression:setsubexpressions(subexpressions) |
| 162 |
local result = FunctionExpression(self.name, subexpressions, self.derivatives) |
| 163 |
result.variables = self.variables |
| 164 |
return result |
| 165 |
end |
| 166 |
|
| 167 |
--- @param other Expression |
| 168 |
--- @return boolean |
| 169 |
function FunctionExpression:order(other) |
| 170 |
if other:isatomic() then |
| 171 |
return false |
| 172 |
end |
| 173 |
|
| 174 |
-- CASC Autosimplfication has some symbols appearing before functions, but that looks bad to me, so all symbols appear before products now. |
| 175 |
-- if other:type() == SymbolExpression then |
| 176 |
-- return SymbolExpression(self.name):order(other) |
| 177 |
-- end |
| 178 |
|
| 179 |
if other:type() == BinaryOperation then |
| 180 |
if other.operation == BinaryOperation.ADD or other.operation == BinaryOperation.MUL then |
| 181 |
return BinaryOperation(other.operation, {self}):order(other) |
| 182 |
end |
| 183 |
|
| 184 |
if other.operation == BinaryOperation.POW then |
| 185 |
return (self^Integer.one()):order(other) |
| 186 |
end |
| 187 |
end |
| 188 |
|
| 189 |
if other:type() == SqrtExpression then |
| 190 |
return self:order(other:topower()) |
| 191 |
end |
| 192 |
|
| 193 |
-- TODO: Make Logarithm and AbsExpression inherit from function expression to reduce code duplication |
| 194 |
if other:type() == Logarithm then |
| 195 |
return self:order(FunctionExpression("log", {other.base, other.expression})) |
| 196 |
end |
| 197 |
|
| 198 |
if other:type() ~= FunctionExpression and other:type() ~= TrigExpression then |
| 199 |
return true |
| 200 |
end |
| 201 |
|
| 202 |
if self.name ~= other.name then |
| 203 |
return SymbolExpression(self.name):order(SymbolExpression(other.name)) |
| 204 |
end |
| 205 |
|
| 206 |
local k = 1 |
| 207 |
while self:subexpressions()[k] and other:subexpressions()[k] do |
| 208 |
if self:subexpressions()[k] ~= other:subexpressions()[k] then |
| 209 |
return self:subexpressions()[k]:order(other:subexpressions()[k]) |
| 210 |
end |
| 211 |
k = k + 1 |
| 212 |
end |
| 213 |
return #self.expressions < #other.expressions |
| 214 |
end |
| 215 |
|
| 216 |
--- @return string |
| 217 |
function FunctionExpression:tolatex() |
| 218 |
local out = tostring(self.name) |
| 219 |
if self:type() == TrigExpression then |
| 220 |
out = "\\" .. out |
| 221 |
end |
| 222 |
if self:type() ~= TrigExpression and #self.name>1 then |
| 223 |
--if out:sub(2,2) ~= "'" then |
| 224 |
--local fp = out:find("'") |
| 225 |
--if fp then |
| 226 |
-- out = '\\operatorname{' .. out:sub(1,fp-1) .. '}' .. out:sub(fp,-1) |
| 227 |
--else |
| 228 |
out = '\\operatorname{' .. out .. '}' |
| 229 |
--end |
| 230 |
--end |
| 231 |
end |
| 232 |
local total = Integer.zero() |
| 233 |
for _,integer in ipairs(self.derivatives) do |
| 234 |
total = total + integer |
| 235 |
end |
| 236 |
if #self.expressions == 1 then |
| 237 |
if total == Integer.zero() then |
| 238 |
goto continue |
| 239 |
else |
| 240 |
if total < Integer(5) then |
| 241 |
while total > Integer.zero() do |
| 242 |
out = out .. "'" |
| 243 |
total = total - Integer.one() |
| 244 |
end |
| 245 |
else |
| 246 |
out = out .. '^{(' .. total:tolatex() .. ')}' |
| 247 |
end |
| 248 |
end |
| 249 |
end |
| 250 |
if #self.expressions > 1 then |
| 251 |
if total == Integer.zero() then |
| 252 |
goto continue |
| 253 |
else |
| 254 |
if total < Integer(4) then |
| 255 |
out = out .. '_{' |
| 256 |
for index,integer in ipairs(self.derivatives) do |
| 257 |
local i = integer:asnumber() |
| 258 |
while i > 0 do |
| 259 |
out = out .. self.variables[index]:tolatex() |
| 260 |
i = i - 1 |
| 261 |
end |
| 262 |
end |
| 263 |
out = out .. '}' |
| 264 |
else |
| 265 |
out = '\\frac{\\partial^{' .. total:tolatex() .. '}' .. out .. '}{' |
| 266 |
for index, integer in ipairs(self.derivatives) do |
| 267 |
if integer > Integer.zero() then |
| 268 |
out = out .. '\\partial ' .. self.variables[index]:tolatex() |
| 269 |
if integer ~= Integer.one() then |
| 270 |
out = out .. '^{' .. integer:tolatex() .. '}' |
| 271 |
end |
| 272 |
end |
| 273 |
end |
| 274 |
out = out .. '}' |
| 275 |
end |
| 276 |
end |
| 277 |
end |
| 278 |
::continue:: |
| 279 |
out = out ..'\\mathopen{}' .. '\\left(' |
| 280 |
for index, expression in ipairs(self:subexpressions()) do |
| 281 |
out = out .. expression:tolatex() |
| 282 |
if self:subexpressions()[index + 1] then |
| 283 |
out = out .. ', ' |
| 284 |
end |
| 285 |
end |
| 286 |
return out .. '\\right)' |
| 287 |
end |
| 288 |
|
| 289 |
----------------- |
| 290 |
-- Inheritance -- |
| 291 |
----------------- |
| 292 |
|
| 293 |
__FunctionExpression.__index = CompoundExpression |
| 294 |
__FunctionExpression.__call = FunctionExpression.new |
| 295 |
FunctionExpression = setmetatable(FunctionExpression, __FunctionExpression) |