| 1 |
--- @class DiffExpression |
| 2 |
--- An expression for a multi-variable higher-order derivatives of an expression. |
| 3 |
--- @field symbols SymbolExpression |
| 4 |
--- @field expression Expression |
| 5 |
|
| 6 |
DiffExpression = {} |
| 7 |
local __DiffExpression = {} |
| 8 |
|
| 9 |
---------------------------- |
| 10 |
-- Instance functionality -- |
| 11 |
---------------------------- |
| 12 |
|
| 13 |
-- Creates a new derivative operation with the given symbols and expression. |
| 14 |
--- @param expression Expression |
| 15 |
--- @param symbols table<number, Symbol> |
| 16 |
--- @return DiffExpression |
| 17 |
function DiffExpression:new(expression,symbols) |
| 18 |
local o = {} |
| 19 |
local __o = Copy(__ExpressionOperations) |
| 20 |
|
| 21 |
o.symbols = Copy(symbols) |
| 22 |
o.degree = #o.symbols |
| 23 |
o.expression = Copy(expression) |
| 24 |
|
| 25 |
__o.__tostring = function(a) |
| 26 |
local varlist = '(d' |
| 27 |
if a.degree == 1 then |
| 28 |
varlist = varlist .. '/d' .. tostring(a.symbols[1]) .. " " .. tostring(a.expression) .. ')' |
| 29 |
end |
| 30 |
if a.degree > 1 then |
| 31 |
varlist = varlist .. '^' .. tostring(a.degree) .. '/' |
| 32 |
local varnum = 1 |
| 33 |
for index = 1, #a.symbols do |
| 34 |
local var = a.symbols[#a.symbols-index+1] |
| 35 |
if a.symbols[#a.symbols-index] == var then |
| 36 |
varnum = varnum + 1 |
| 37 |
goto nextvar |
| 38 |
end |
| 39 |
if a.symbols[#a.symbols-index] ~=var then |
| 40 |
if varnum == 1 then |
| 41 |
varlist = varlist .. 'd' .. tostring(var) |
| 42 |
else |
| 43 |
varlist = varlist .. 'd' .. tostring(var) .. '^' .. tostring(varnum) |
| 44 |
end |
| 45 |
varnum = 1 |
| 46 |
end |
| 47 |
::nextvar:: |
| 48 |
end |
| 49 |
varlist = varlist .. " " .. tostring(a.expression) .. ')' |
| 50 |
end |
| 51 |
return varlist |
| 52 |
end |
| 53 |
|
| 54 |
__o.__index = DiffExpression |
| 55 |
__o.__eq = function(a, b) |
| 56 |
-- 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 |
| 57 |
if not b:type() == DiffExpression then |
| 58 |
return false |
| 59 |
end |
| 60 |
if a.expression ~= b.expression then |
| 61 |
return false |
| 62 |
end |
| 63 |
local loc = 1 |
| 64 |
while a.symbols[loc] or b.symbols[loc] do |
| 65 |
if not a.symbols[loc] or not b.symbols[loc] or |
| 66 |
(a.symbols[loc] ~= b.symbols[loc]) then |
| 67 |
return false |
| 68 |
end |
| 69 |
loc = loc + 1 |
| 70 |
end |
| 71 |
return true |
| 72 |
end |
| 73 |
o = setmetatable(o, __o) |
| 74 |
|
| 75 |
return o |
| 76 |
end |
| 77 |
|
| 78 |
--- @return Expression |
| 79 |
function DiffExpression:evaluate() |
| 80 |
local exp = self.expression |
| 81 |
|
| 82 |
for _,var in ipairs(self.symbols) do |
| 83 |
exp = DerivativeExpression(exp,var):evaluate() |
| 84 |
end |
| 85 |
return exp |
| 86 |
end |
| 87 |
|
| 88 |
--- @return Expression |
| 89 |
function DiffExpression:autosimplify() |
| 90 |
return DiffExpression(self.expression:autosimplify(), self.symbols):evaluate() |
| 91 |
end |
| 92 |
|
| 93 |
|
| 94 |
--- @return table<number, Expression> |
| 95 |
function DiffExpression:subexpressions() |
| 96 |
return {self.expression} |
| 97 |
end |
| 98 |
|
| 99 |
--- @param subexpressions table<number, Expression> |
| 100 |
--- @return DiffExpression |
| 101 |
function DiffExpression:setsubexpressions(subexpressions) |
| 102 |
return DiffExpression(subexpressions[1], self.symbols) |
| 103 |
end |
| 104 |
|
| 105 |
--- @param other Expression |
| 106 |
--- @return boolean |
| 107 |
function DiffExpression:order(other) |
| 108 |
if other:type() == IntegralExpression then |
| 109 |
return true |
| 110 |
end |
| 111 |
|
| 112 |
if other:type() ~= DiffExpression then |
| 113 |
return false |
| 114 |
end |
| 115 |
|
| 116 |
if self.degree > other.degree then |
| 117 |
return false |
| 118 |
end |
| 119 |
|
| 120 |
if self.degree < other.degree then |
| 121 |
return true |
| 122 |
end |
| 123 |
|
| 124 |
return self.expression:order(other.expression) |
| 125 |
end |
| 126 |
|
| 127 |
--- @return string |
| 128 |
function DiffExpression:tolatex() |
| 129 |
local varlist = '\\frac{' |
| 130 |
if self.degree == 1 then |
| 131 |
varlist = varlist .. 'd}{d' .. self.symbols[1]:tolatex() .. '}\\left(' .. self.expression:tolatex() .. '\\right)' |
| 132 |
end |
| 133 |
if self.degree > 1 then |
| 134 |
local cvarlist = {} |
| 135 |
local count = 1 |
| 136 |
for index, var in ipairs(self.symbols) do |
| 137 |
if var == self.symbols[index+1] then |
| 138 |
count = count + 1 |
| 139 |
else |
| 140 |
table.insert(cvarlist,{var,count}) |
| 141 |
count = 1 |
| 142 |
end |
| 143 |
end |
| 144 |
if #cvarlist == 1 then |
| 145 |
varlist = varlist .. 'd^{' .. self.degree .. '}}{' .. 'd' .. cvarlist[1][1]:tolatex() .. '^{' .. self.degree .. '}' |
| 146 |
end |
| 147 |
if #cvarlist > 1 then |
| 148 |
varlist = varlist .. '\\partial^{' .. self.degree .. '}}{' |
| 149 |
for index, varnum in ipairs(cvarlist) do |
| 150 |
var = cvarlist[#cvarlist - index+1][1] |
| 151 |
num = cvarlist[#cvarlist - index+1][2] |
| 152 |
if num == 1 then |
| 153 |
varlist = varlist .. '\\partial ' .. var:tolatex() |
| 154 |
else |
| 155 |
varlist = varlist .. '\\partial ' .. var:tolatex() .. '^{' .. num .. '}' |
| 156 |
end |
| 157 |
end |
| 158 |
end |
| 159 |
varlist = varlist .. '} \\left(' .. self.expression:tolatex() .. '\\right)' |
| 160 |
end |
| 161 |
return varlist |
| 162 |
end |
| 163 |
|
| 164 |
----------------- |
| 165 |
-- Inheritance -- |
| 166 |
----------------- |
| 167 |
|
| 168 |
__DiffExpression.__index = CompoundExpression |
| 169 |
__DiffExpression.__call = DiffExpression.new |
| 170 |
DiffExpression = setmetatable(DiffExpression, __DiffExpression) |
| 171 |
|
| 172 |
---------------------- |
| 173 |
-- Static constants -- |
| 174 |
---------------------- |
| 175 |
|
| 176 |
diff = function(expression,...) |
| 177 |
local symbols = {} |
| 178 |
for i = 1, select("#",...) do |
| 179 |
local var = select(i,...) |
| 180 |
if #var == 0 then |
| 181 |
table.insert(symbols,var) |
| 182 |
end |
| 183 |
if #var > 0 then |
| 184 |
for index=1, RR(var[2]) do |
| 185 |
table.insert(symbols,var[1]) |
| 186 |
end |
| 187 |
end |
| 188 |
end |
| 189 |
return DiffExpression(expression,symbols) |
| 190 |
end |