| 1 |
--- @class SymbolExpression |
| 2 |
--- An atomic expression corresponding to a symbol representing an arbitary value. |
| 3 |
--- @field symbol string |
| 4 |
--- @alias Symbol SymbolExpression |
| 5 |
SymbolExpression = {} |
| 6 |
local __SymbolExpression = {} |
| 7 |
|
| 8 |
---------------------- |
| 9 |
-- Instance methods -- |
| 10 |
---------------------- |
| 11 |
|
| 12 |
--- Given the name of the symbol as a string, creates a new symbol. |
| 13 |
--- @param symbol string |
| 14 |
--- @return SymbolExpression |
| 15 |
function SymbolExpression:new(symbol) |
| 16 |
local o = {} |
| 17 |
local __o = Copy(__ExpressionOperations) |
| 18 |
__o.__index = SymbolExpression |
| 19 |
__o.__tostring = function(a) |
| 20 |
return a.symbol |
| 21 |
end |
| 22 |
__o.__eq = function(a, b) |
| 23 |
return a.symbol == b.symbol |
| 24 |
end |
| 25 |
|
| 26 |
if type(symbol) ~= "string" then |
| 27 |
error("Sent parameter of wrong type: symbol must be a string") |
| 28 |
end |
| 29 |
|
| 30 |
o.symbol = symbol |
| 31 |
o = setmetatable(o, __o) |
| 32 |
|
| 33 |
return o |
| 34 |
end |
| 35 |
|
| 36 |
--- @return boolean |
| 37 |
function SymbolExpression:freeof(symbol) |
| 38 |
return symbol~=self |
| 39 |
end |
| 40 |
|
| 41 |
--- @return boolean |
| 42 |
function SymbolExpression:isconstant() |
| 43 |
return false |
| 44 |
end |
| 45 |
|
| 46 |
--- @param other Expression |
| 47 |
--- @return boolean |
| 48 |
function SymbolExpression:order(other) |
| 49 |
|
| 50 |
-- Symbol Expressions come after constant expressions. |
| 51 |
if other:isconstant() then |
| 52 |
return false |
| 53 |
end |
| 54 |
|
| 55 |
-- Lexographic order on symbols. |
| 56 |
if other:type() == SymbolExpression then |
| 57 |
for i = 1, math.min(#self.symbol, #other.symbol) do |
| 58 |
if string.byte(self.symbol, i) ~= string.byte(other.symbol, i) then |
| 59 |
return string.byte(self.symbol, i) < string.byte(other.symbol, i) |
| 60 |
end |
| 61 |
end |
| 62 |
|
| 63 |
return #self.symbol < #other.symbol |
| 64 |
end |
| 65 |
|
| 66 |
if other.operation == BinaryOperation.POW then |
| 67 |
return BinaryOperation(BinaryOperation.POW, {self, Integer.one()}):order(other) |
| 68 |
end |
| 69 |
|
| 70 |
if other.operation == BinaryOperation.MUL then |
| 71 |
return BinaryOperation(BinaryOperation.MUL, {self}):order(other) |
| 72 |
end |
| 73 |
|
| 74 |
if other.operation == BinaryOperation.ADD then |
| 75 |
return BinaryOperation(BinaryOperation.ADD, {self}):order(other) |
| 76 |
end |
| 77 |
|
| 78 |
-- CASC Autosimplfication has some symbols appearing before functions, but that looks bad to me, so all symbols appear before products now. |
| 79 |
if other:type() == FunctionExpression or other:type() == TrigExpression or other:type() == Logarithm then |
| 80 |
return true |
| 81 |
end |
| 82 |
|
| 83 |
return false |
| 84 |
end |
| 85 |
|
| 86 |
--- Converts this symbol to an element of a polynomial ring. |
| 87 |
--- @return PolynomialRing, boolean |
| 88 |
function SymbolExpression:topolynomial() |
| 89 |
return PolynomialRing({Integer.zero(), Integer.one()}, self.symbol), true |
| 90 |
end |
| 91 |
|
| 92 |
----------------- |
| 93 |
-- Inheritance -- |
| 94 |
----------------- |
| 95 |
|
| 96 |
__SymbolExpression.__index = AtomicExpression |
| 97 |
__SymbolExpression.__call = SymbolExpression.new |
| 98 |
SymbolExpression = setmetatable(SymbolExpression, __SymbolExpression) |
| 99 |
|
| 100 |
|
| 101 |
---------------------- |
| 102 |
-- Static Constants -- |
| 103 |
---------------------- |
| 104 |
|
| 105 |
-- The constant pi. |
| 106 |
PI = SymbolExpression("pi") |
| 107 |
function PI:tolatex() |
| 108 |
return "\\pi " |
| 109 |
end |
| 110 |
|
| 111 |
-- Approximates pi as a rational number. Uses continued fraction expansion. |
| 112 |
function PI:approximate() |
| 113 |
return Integer(313383936) / Integer(99753205) |
| 114 |
end |
| 115 |
|
| 116 |
--function PI:isconstant() |
| 117 |
-- return true |
| 118 |
--end |
| 119 |
|
| 120 |
-- The constant e. |
| 121 |
E = SymbolExpression("e") |
| 122 |
|
| 123 |
-- Approximates pi as a rational number. Uses continued fraction expansion. |
| 124 |
function E:approximate() |
| 125 |
return Integer(517656) / Integer(190435) |
| 126 |
end |
| 127 |
|
| 128 |
-- The imaginary constant i. |
| 129 |
I = SymbolExpression("i") |
| 130 |
function I:tolatex() |
| 131 |
return "i" |
| 132 |
end |