| 1 |
--- @class Expression |
| 2 |
--- Interface for an arbitrary mathematical expression. |
| 3 |
Expression = {} |
| 4 |
|
| 5 |
---------------------- |
| 6 |
-- Required methods -- |
| 7 |
---------------------- |
| 8 |
|
| 9 |
--- Evaluates the current expression recursively by evaluating each sub-expression. |
| 10 |
--- @return Expression |
| 11 |
function Expression:evaluate() |
| 12 |
error("Called unimplemented method : evaluate()") |
| 13 |
end |
| 14 |
|
| 15 |
--- Performs automatic simplification of an expression. Called on every expression before being output from the CAS. |
| 16 |
--- @return Expression |
| 17 |
function Expression:autosimplify() |
| 18 |
error("Called unimplemented method : autosimplify()") |
| 19 |
end |
| 20 |
|
| 21 |
--- Performs more rigorous simplification of an expression. Checks different equivalent forms and determines the 'smallest' expresion. |
| 22 |
--- @return Expression |
| 23 |
function Expression:simplify() |
| 24 |
local me = self:unlock():autosimplify() |
| 25 |
local results = {} |
| 26 |
for index, expression in ipairs(me:subexpressions()) do |
| 27 |
results[index] = expression:simplify() |
| 28 |
end |
| 29 |
me = me:setsubexpressions(results) |
| 30 |
|
| 31 |
local out = me |
| 32 |
local minsize = self:size() |
| 33 |
|
| 34 |
local test = me:expand() |
| 35 |
if test:size() < minsize then |
| 36 |
out = test |
| 37 |
minsize = test:size() |
| 38 |
end |
| 39 |
|
| 40 |
test = me:factor() |
| 41 |
if test:size() < minsize then |
| 42 |
out = test |
| 43 |
minsize = test:size() |
| 44 |
end |
| 45 |
|
| 46 |
return out |
| 47 |
end |
| 48 |
|
| 49 |
--- Changes the autosimplify behavior of an expression depending on its parameters. |
| 50 |
--- THIS METHOD MUTATES THE OBJECT IT ACTS ON. |
| 51 |
--- @param mode number |
| 52 |
--- @param permanent boolean |
| 53 |
--- @param recursive boolean |
| 54 |
--- @return Expression |
| 55 |
function Expression:lock(mode, permanent, recursive) |
| 56 |
function self:autosimplify() |
| 57 |
if not permanent then |
| 58 |
self.autosimplify = nil |
| 59 |
end |
| 60 |
|
| 61 |
if mode == Expression.NIL then |
| 62 |
return self |
| 63 |
elseif mode == Expression.SUBS then |
| 64 |
local results = {} |
| 65 |
for index, expression in ipairs(self:subexpressions()) do |
| 66 |
results[index] = expression:autosimplify() |
| 67 |
end |
| 68 |
self = self:setsubexpressions(results, true) -- TODO: Add mutate setsubexpressions |
| 69 |
return self |
| 70 |
end |
| 71 |
end |
| 72 |
if recursive then |
| 73 |
for _, expression in ipairs(self:subexpressions()) do |
| 74 |
expression:lock(mode, permanent, recursive) |
| 75 |
end |
| 76 |
end |
| 77 |
return self |
| 78 |
end |
| 79 |
|
| 80 |
--- Frees any locks on expressions. |
| 81 |
--- THIS METHOD MUTATES THE OBJECT IT ACTS ON. |
| 82 |
--- @param recursive boolean |
| 83 |
--- @return Expression |
| 84 |
function Expression:unlock(recursive) |
| 85 |
self.autosimplify = nil |
| 86 |
if recursive then |
| 87 |
for _, expression in ipairs(self:subexpressions()) do |
| 88 |
expression:unlock(recursive) |
| 89 |
end |
| 90 |
end |
| 91 |
return self |
| 92 |
end |
| 93 |
|
| 94 |
--- Returns a list of all subexpressions of an expression. |
| 95 |
--- @return table<number, Expression> |
| 96 |
function Expression:subexpressions() |
| 97 |
error("Called unimplemented method : subexpressions()") |
| 98 |
end |
| 99 |
|
| 100 |
--- Returns the total number of atomic and compound expressions that make up an expression, or the number of nodes in the expression tree. |
| 101 |
--- @return Integer |
| 102 |
function Expression:size() |
| 103 |
local out = Integer.one() |
| 104 |
for _, expression in ipairs(self:subexpressions()) do |
| 105 |
out = out + expression:size() |
| 106 |
end |
| 107 |
return out |
| 108 |
end |
| 109 |
|
| 110 |
--- Returns a copy of the original expression with each subexpression substituted with a new one, or a mutated version if mutate is true. |
| 111 |
--- @param subexpressions table<number, Expression> |
| 112 |
--- @param mutate boolean |
| 113 |
--- @return Expression |
| 114 |
function Expression:setsubexpressions(subexpressions, mutate) |
| 115 |
error("Called unimplemented method : setsubexpressions()") |
| 116 |
end |
| 117 |
|
| 118 |
--- Determines whether or not an expression contains a particular symbol. |
| 119 |
--- @param symbol SymbolExpression |
| 120 |
--- @return boolean |
| 121 |
function Expression:freeof(symbol) |
| 122 |
error("Called unimplemented method : freeof()") |
| 123 |
end |
| 124 |
|
| 125 |
--- Substitutes occurances of specified sub-expressions with other sub-expressions. |
| 126 |
--- @param map table<Expression, Expression> |
| 127 |
--- @return Expression |
| 128 |
function Expression:substitute(map) |
| 129 |
error("Called unimplemented method : substitute()") |
| 130 |
end |
| 131 |
|
| 132 |
--- Algebraically expands an expression by turning products of sums into sums of products and expanding powers. |
| 133 |
--- @return Expression |
| 134 |
function Expression:expand() |
| 135 |
return self |
| 136 |
end |
| 137 |
|
| 138 |
--- Attempts to factor an expression by turning sums of products into products of sums, and identical terms multiplied together into factors. |
| 139 |
--- @return Expression |
| 140 |
function Expression:factor() |
| 141 |
return self |
| 142 |
end |
| 143 |
|
| 144 |
--- Attempts to combine an expression by collapsing sums of expressions together into a single factor, e.g. common denominator |
| 145 |
--- @return Expression |
| 146 |
function Expression:combine() |
| 147 |
return self |
| 148 |
end |
| 149 |
|
| 150 |
--- Attempts to collect all occurances of an expression in this expression. |
| 151 |
--- @param collect Expression |
| 152 |
--- @return Expression |
| 153 |
function Expression:collect(collect) |
| 154 |
return self |
| 155 |
end |
| 156 |
|
| 157 |
--- Returns all non-constant subexpressions of this expression - helper method for factor. |
| 158 |
--- @return table<number, Expression> |
| 159 |
function Expression:getsubexpressionsrec() |
| 160 |
local result = {} |
| 161 |
|
| 162 |
for _, expression in ipairs(self:subexpressions()) do |
| 163 |
if not expression:isconstant() then |
| 164 |
result[#result+1] = expression |
| 165 |
end |
| 166 |
result = JoinArrays(result, expression:getsubexpressionsrec()) |
| 167 |
end |
| 168 |
|
| 169 |
return result |
| 170 |
end |
| 171 |
|
| 172 |
--- Determines whether an expression is atomic. |
| 173 |
--- Atomic expressions are not necessarily constant, since polynomial rings, for instance, are atomic parts that contain symbols. |
| 174 |
--- @return boolean |
| 175 |
function Expression:isatomic() |
| 176 |
error("Called unimplemented method: isatomic()") |
| 177 |
end |
| 178 |
|
| 179 |
--- Determines whether an expression is a constant, i.e., an atomic expression that is not a varaible and cannot be converted into an equivalent compound expression. |
| 180 |
--- @return boolean |
| 181 |
function Expression:isconstant() |
| 182 |
error("Called unimplemented method: isconstant()") |
| 183 |
end |
| 184 |
|
| 185 |
--- Determines whether an expression is a 'proper' real constant, i.e., is free of every varaible. |
| 186 |
function Expression:isrealconstant() |
| 187 |
if self:isconstant() or self == PI or self == E then |
| 188 |
return true |
| 189 |
end |
| 190 |
|
| 191 |
for _, expression in ipairs(self:subexpressions()) do |
| 192 |
if not expression:isrealconstant() then |
| 193 |
return false |
| 194 |
end |
| 195 |
end |
| 196 |
|
| 197 |
return self:type() ~= SymbolExpression |
| 198 |
end |
| 199 |
|
| 200 |
--- Determines whether an expression is a 'proper' complex constant, i.e., is free of every varaible and is of the form a + bI for nonzero a and b. |
| 201 |
--- @return boolean |
| 202 |
function Expression:iscomplexconstant() |
| 203 |
return self:isrealconstant() or (self.operation == BinaryOperation.ADD and #self.expressions == 2 and self.expressions[1]:isrealconstant() |
| 204 |
and ((self.expressions[2].operation == BinaryOperation.MUL and #self.expressions[2].expressions == 2 and self.expressions[2].expressions[1]:isrealconstant() and self.expressions[2].expressions[2] == I) |
| 205 |
or self.expressions[2] == I)) or (self.operation == BinaryOperation.MUL and #self.expressions == 2 and self.expressions[1]:isrealconstant() and self.expressions[2] == I) |
| 206 |
end |
| 207 |
|
| 208 |
--- A total order on autosimplified expressions. Returns true if self < other. |
| 209 |
--- @param other Expression |
| 210 |
--- @return boolean |
| 211 |
function Expression:order(other) |
| 212 |
error("Called unimplemented method: order()") |
| 213 |
end |
| 214 |
|
| 215 |
--- Returns an autosimplified expression as a single-variable polynomial in a ring, if it can be converted. Returns itself otherwise. |
| 216 |
--- @return PolynomialRing, boolean |
| 217 |
function Expression:topolynomial() |
| 218 |
return self, false |
| 219 |
end |
| 220 |
|
| 221 |
--- Converts this expression to LaTeX code. |
| 222 |
--- @return string |
| 223 |
function Expression:tolatex() |
| 224 |
error("Called Unimplemented method: tolatex()") |
| 225 |
end |
| 226 |
|
| 227 |
---------------------- |
| 228 |
-- Instance methods -- |
| 229 |
---------------------- |
| 230 |
|
| 231 |
--- Returns the type of the expression, i.e., the table used to create objects of that type. |
| 232 |
--- @return table |
| 233 |
function Expression:type() |
| 234 |
return getmetatable(self).__index |
| 235 |
end |
| 236 |
|
| 237 |
-------------------------- |
| 238 |
-- Instance metamethods -- |
| 239 |
-------------------------- |
| 240 |
|
| 241 |
__ExpressionOperations = {} |
| 242 |
|
| 243 |
__ExpressionOperations.__unm = function(a) |
| 244 |
return BinaryOperation.SUBEXP({a}) |
| 245 |
end |
| 246 |
|
| 247 |
__ExpressionOperations.__add = function(a, b) |
| 248 |
return BinaryOperation.ADDEXP({a, b}) |
| 249 |
end |
| 250 |
|
| 251 |
__ExpressionOperations.__sub = function(a, b) |
| 252 |
return BinaryOperation.SUBEXP({a, b}) |
| 253 |
end |
| 254 |
|
| 255 |
__ExpressionOperations.__mul = function(a, b) |
| 256 |
return BinaryOperation.MULEXP({a, b}) |
| 257 |
end |
| 258 |
|
| 259 |
__ExpressionOperations.__div = function(a, b) |
| 260 |
return BinaryOperation.DIVEXP({a, b}) |
| 261 |
end |
| 262 |
|
| 263 |
__ExpressionOperations.__pow = function(a, b) |
| 264 |
return BinaryOperation.POWEXP({a, b}) |
| 265 |
end |
| 266 |
|
| 267 |
-- For iterating over the subexpressions of a easily. |
| 268 |
__ExpressionOperations.__call = function(a, ...) |
| 269 |
if a:type() == SymbolExpression then |
| 270 |
return FunctionExpression(a, table.pack(...)) |
| 271 |
end |
| 272 |
return BinaryOperation.MULEXP({a, table.pack(...)[1]}) |
| 273 |
end |
| 274 |
|
| 275 |
---------------------- |
| 276 |
-- Static constants -- |
| 277 |
---------------------- |
| 278 |
|
| 279 |
Expression.NIL = 0 |
| 280 |
Expression.SUBS = 1 |