| 1 |
--- @class DerivativeExpression |
| 2 |
--- An expression for a single-variable derivative of an expression. |
| 3 |
--- @field symbol SymbolExpression |
| 4 |
--- @field expression Expression |
| 5 |
DerivativeExpression = {} |
| 6 |
local __DerivativeExpression = {} |
| 7 |
|
| 8 |
---------------------------- |
| 9 |
-- Instance functionality -- |
| 10 |
---------------------------- |
| 11 |
|
| 12 |
-- Creates a new single-variable derivative operation with the given symbol and expression. |
| 13 |
--- @param expression Expression |
| 14 |
--- @param symbol Symbol |
| 15 |
--- @return DerivativeExpression |
| 16 |
function DerivativeExpression:new(expression, symbol) |
| 17 |
local o = {} |
| 18 |
local __o = Copy(__ExpressionOperations) |
| 19 |
|
| 20 |
o.expression = Copy(expression) |
| 21 |
o.symbol = symbol or SymbolExpression("x") |
| 22 |
|
| 23 |
__o.__index = DerivativeExpression |
| 24 |
__o.__tostring = function(a) |
| 25 |
return '(d/d' .. tostring(a.symbol) .. " " .. tostring(a.expression) .. ')' |
| 26 |
end |
| 27 |
__o.__eq = function(a, b) |
| 28 |
-- 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 |
| 29 |
if not b:type() == DerivativeExpression then |
| 30 |
return false |
| 31 |
end |
| 32 |
return a.symbol == b.symbol and a.expression == b.expression |
| 33 |
end |
| 34 |
o = setmetatable(o, __o) |
| 35 |
|
| 36 |
return o |
| 37 |
end |
| 38 |
|
| 39 |
--- @return Expression |
| 40 |
function DerivativeExpression:evaluate() |
| 41 |
local exp = self.expression |
| 42 |
|
| 43 |
-- The derivative of a constant is 0 |
| 44 |
if exp:isconstant() then |
| 45 |
return Integer.zero() |
| 46 |
end |
| 47 |
|
| 48 |
-- The derivative of a symbol is either 1 or 0 |
| 49 |
if exp:type() == SymbolExpression then |
| 50 |
if self.symbol == exp then |
| 51 |
return Integer.one() |
| 52 |
end |
| 53 |
return Integer.zero() |
| 54 |
end |
| 55 |
|
| 56 |
-- Chain rule for arbitrary functions |
| 57 |
|
| 58 |
if exp:type() == FunctionExpression then |
| 59 |
local results = {} |
| 60 |
for index,expression in ipairs(exp.expressions) do |
| 61 |
local dout = FunctionExpression(exp.name,exp.expressions,exp.derivatives) |
| 62 |
dout.variables = exp.variables |
| 63 |
dout.derivatives[index] = dout.derivatives[index]+Integer.one() |
| 64 |
local dinn = DerivativeExpression(expression,self.symbol):evaluate() |
| 65 |
results[index] = dout*dinn |
| 66 |
end |
| 67 |
return BinaryOperation(BinaryOperation.ADD,results):autosimplify() |
| 68 |
end |
| 69 |
|
| 70 |
--if exp:type() == FunctionExpression then |
| 71 |
-- local results = {} |
| 72 |
-- for index,expression in ipairs(exp.expressions) do |
| 73 |
-- local inn = DerivativeExpression(expression,self.symbol):autosimplify() |
| 74 |
-- local out = Copy(exp) |
| 75 |
-- out.orders[index] = out.orders[index] + Integer.one() |
| 76 |
-- local result = inn*out |
| 77 |
-- table.insert(results,result) |
| 78 |
-- end |
| 79 |
-- return BinaryOperation(BinaryOperation.ADD,results):autosimplify() |
| 80 |
--end |
| 81 |
|
| 82 |
--if exp:type() == FunctionExpression then |
| 83 |
-- if exp.expressions[2] then |
| 84 |
-- return self |
| 85 |
-- end |
| 86 |
-- return DerivativeExpression(exp.expressions[1], self.symbol) * FunctionExpression(exp.name, exp.expressions, exp.orders[1] + Integer.one(), exp.variables[1]):autosimplify() |
| 87 |
--end |
| 88 |
|
| 89 |
-- Chain rule for trig functions |
| 90 |
if exp:type() == TrigExpression then |
| 91 |
local internal = DerivativeExpression(exp.expression, self.symbol) |
| 92 |
|
| 93 |
if exp.name == "sin" then |
| 94 |
return (internal * COS(exp.expression)):autosimplify() |
| 95 |
end |
| 96 |
if exp.name == "cos" then |
| 97 |
return (internal * -SIN(exp.expression)):autosimplify() |
| 98 |
end |
| 99 |
if exp.name == "tan" then |
| 100 |
return (internal * SEC(exp.expression)^Integer(2)):autosimplify() |
| 101 |
end |
| 102 |
if exp.name == "csc" then |
| 103 |
return (internal * -CSC(exp.expression)*COT(exp.expression)):autosimplify() |
| 104 |
end |
| 105 |
if exp.name == "sec" then |
| 106 |
return (internal * -SEC(exp.expression)*TAN(exp.expression)):autosimplify() |
| 107 |
end |
| 108 |
if exp.name == "cot" then |
| 109 |
return (internal * -CSC(exp.expression)^Integer(2)):autosimplify() |
| 110 |
end |
| 111 |
if exp.name == "arcsin" then |
| 112 |
return (internal / (Integer(1)-exp.expression^Integer(2))^(Integer(1)/Integer(2))):autosimplify() |
| 113 |
end |
| 114 |
if exp.name == "arccos" then |
| 115 |
return (-internal / (Integer(1)-exp.expression^Integer(2))^(Integer(1)/Integer(2))):autosimplify() |
| 116 |
end |
| 117 |
if exp.name == "arctan" then |
| 118 |
return (internal / (Integer(1)+exp.expression^Integer(2))):autosimplify() |
| 119 |
end |
| 120 |
if exp.name == "arccsc" then |
| 121 |
return (-internal / (ABS(exp.expression) * (Integer(1)-exp.expression^Integer(2))^(Integer(1)/Integer(2)))):autosimplify() |
| 122 |
end |
| 123 |
if exp.name == "arcsec" then |
| 124 |
return (internal / (ABS(exp.expression) * (Integer(1)-exp.expression^Integer(2))^(Integer(1)/Integer(2)))):autosimplify() |
| 125 |
end |
| 126 |
if exp.name == "arccot" then |
| 127 |
return (-internal / (Integer(1)+exp.expression^Integer(2))):autosimplify() |
| 128 |
end |
| 129 |
end |
| 130 |
|
| 131 |
-- TODO: Piecewise functions |
| 132 |
if self:type() == AbsExpression then |
| 133 |
return DerivativeExpression(self.expression, self.symbol):autosimplify() |
| 134 |
end |
| 135 |
|
| 136 |
-- Uses linearity of derivatives to evaluate sum expressions |
| 137 |
if exp.operation == BinaryOperation.ADD then |
| 138 |
local parts = {} |
| 139 |
for i, expression in pairs(exp.expressions) do |
| 140 |
parts[i] = DerivativeExpression(expression, self.symbol) |
| 141 |
end |
| 142 |
return BinaryOperation(BinaryOperation.ADD, parts):autosimplify() |
| 143 |
end |
| 144 |
|
| 145 |
-- Uses product rule to evaluate product expressions |
| 146 |
if exp.operation == BinaryOperation.MUL then |
| 147 |
local sums = {} |
| 148 |
for i, expression in pairs(exp.expressions) do |
| 149 |
local products = {} |
| 150 |
for j, innerexpression in pairs(exp.expressions) do |
| 151 |
if i ~= j then |
| 152 |
products[j] = innerexpression |
| 153 |
else |
| 154 |
products[j] = DerivativeExpression(innerexpression, self.symbol) |
| 155 |
end |
| 156 |
end |
| 157 |
sums[i] = BinaryOperation(BinaryOperation.MUL, products) |
| 158 |
end |
| 159 |
return BinaryOperation(BinaryOperation.ADD, sums):autosimplify() |
| 160 |
end |
| 161 |
|
| 162 |
-- Uses the generalized power rule to evaluate power expressions |
| 163 |
if exp.operation == BinaryOperation.POW then |
| 164 |
local base = exp.expressions[1] |
| 165 |
local exponent = exp.expressions[2] |
| 166 |
|
| 167 |
return BinaryOperation.MULEXP({ |
| 168 |
BinaryOperation.POWEXP({base, exponent}), |
| 169 |
BinaryOperation.ADDEXP({ |
| 170 |
BinaryOperation.MULEXP({ |
| 171 |
DD(base, self.symbol), |
| 172 |
BinaryOperation.DIVEXP({exponent, base})}), |
| 173 |
BinaryOperation.MULEXP({ |
| 174 |
DD(exponent, self.symbol), |
| 175 |
LN(base)})}) |
| 176 |
}):autosimplify() |
| 177 |
end |
| 178 |
|
| 179 |
if exp:type() == Logarithm then |
| 180 |
local base = exp.base |
| 181 |
local expression = exp.expression |
| 182 |
|
| 183 |
return BinaryOperation.SUBEXP({ |
| 184 |
BinaryOperation.DIVEXP({DD(expression, self.symbol), |
| 185 |
BinaryOperation.MULEXP({expression, LN(base)})}), |
| 186 |
|
| 187 |
BinaryOperation.DIVEXP({ |
| 188 |
BinaryOperation.MULEXP({LN(expression), DD(base, self.symbol)}), |
| 189 |
BinaryOperation.MULEXP({BinaryOperation.POWEXP({LN(base), Integer(2)}), base}) |
| 190 |
}) |
| 191 |
|
| 192 |
}):autosimplify() |
| 193 |
end |
| 194 |
|
| 195 |
return self |
| 196 |
end |
| 197 |
|
| 198 |
--- @return Expression |
| 199 |
function DerivativeExpression:autosimplify() |
| 200 |
return DerivativeExpression(self.expression:autosimplify(), self.symbol):evaluate() |
| 201 |
end |
| 202 |
|
| 203 |
--- @return table<number, Expression> |
| 204 |
function DerivativeExpression:subexpressions() |
| 205 |
return {self.expression} |
| 206 |
end |
| 207 |
|
| 208 |
--- @param subexpressions table<number, Expression> |
| 209 |
--- @return DerivativeExpression |
| 210 |
function DerivativeExpression:setsubexpressions(subexpressions) |
| 211 |
return DerivativeExpression(subexpressions[1], self.symbol) |
| 212 |
end |
| 213 |
|
| 214 |
-- function DerivativeExpression:freeof(symbol) |
| 215 |
-- return self.symbol.freeof(symbol) and self.expression:freeof(symbol) |
| 216 |
-- end |
| 217 |
|
| 218 |
-- Substitutes each expression for a new one. |
| 219 |
-- function DerivativeExpression:substitute(map) |
| 220 |
-- for expression, replacement in pairs(map) do |
| 221 |
-- if self == expression then |
| 222 |
-- return replacement |
| 223 |
-- end |
| 224 |
-- end |
| 225 |
-- -- Typically, we only perform substitution on autosimplified expressions, so this won't get called. May give strange results, i.e., |
| 226 |
-- -- substituting and then evaluating the derivative may not return the same thing as evaluating the derivative and then substituting. |
| 227 |
-- return DerivativeExpression(self.expression:substitute(map), self.symbol) |
| 228 |
-- end |
| 229 |
|
| 230 |
--- @param other Expression |
| 231 |
--- @return boolean |
| 232 |
function DerivativeExpression:order(other) |
| 233 |
if other:type() == IntegralExpression then |
| 234 |
return true |
| 235 |
end |
| 236 |
|
| 237 |
if other:type() ~= DerivativeExpression then |
| 238 |
return false |
| 239 |
end |
| 240 |
|
| 241 |
if self.symbol ~= other.symbol then |
| 242 |
return self.symbol:order(other.symbol) |
| 243 |
end |
| 244 |
|
| 245 |
return self.expression:order(other.expression) |
| 246 |
end |
| 247 |
|
| 248 |
--- @return string |
| 249 |
function DerivativeExpression:tolatex() |
| 250 |
return '\\frac{d}{d' .. self.symbol:tolatex() .. '}\\left(' .. self.expression:tolatex() .. '\\right)' |
| 251 |
end |
| 252 |
|
| 253 |
----------------- |
| 254 |
-- Inheritance -- |
| 255 |
----------------- |
| 256 |
__DerivativeExpression.__index = CompoundExpression |
| 257 |
__DerivativeExpression.__call = DerivativeExpression.new |
| 258 |
DerivativeExpression = setmetatable(DerivativeExpression, __DerivativeExpression) |
| 259 |
|
| 260 |
---------------------- |
| 261 |
-- Static constants -- |
| 262 |
---------------------- |
| 263 |
DD = function(expression, symbol) |
| 264 |
return DerivativeExpression(expression, symbol) |
| 265 |
end |