| 1 |
-- Seperates the various binary operations into their own files for readability |
| 2 |
|
| 3 |
--- Automatic simplification of multiplication expressions. |
| 4 |
--- @return BinaryOperation |
| 5 |
function BinaryOperation:simplifyproduct() |
| 6 |
if not self.expressions[1] then |
| 7 |
error("Execution error: attempted to simplify empty product") |
| 8 |
end |
| 9 |
|
| 10 |
if not self.expressions[2] then |
| 11 |
return self.expressions[1] |
| 12 |
end |
| 13 |
|
| 14 |
-- Uses the property that x*0=0 |
| 15 |
for _, expression in ipairs(self.expressions) do |
| 16 |
if expression:isconstant() and expression == expression:zero() then |
| 17 |
return expression:zero() |
| 18 |
end |
| 19 |
end |
| 20 |
|
| 21 |
local result = self:simplifyproductrec() |
| 22 |
|
| 23 |
-- We don't really know what ring we are working in here, so just assume the integer ring |
| 24 |
if not result.expressions[1] then |
| 25 |
return Integer.one() |
| 26 |
end |
| 27 |
|
| 28 |
if not result.expressions[2] then |
| 29 |
return result.expressions[1] |
| 30 |
end |
| 31 |
|
| 32 |
return result |
| 33 |
end |
| 34 |
|
| 35 |
function BinaryOperation:simplifyproductrec() |
| 36 |
local term1 = self.expressions[1] |
| 37 |
local term2 = self.expressions[2] |
| 38 |
|
| 39 |
if not self.expressions[3] then |
| 40 |
if (term1:isconstant() or not (term1.operation == BinaryOperation.MUL)) and |
| 41 |
(term2:isconstant() or not (term2.operation == BinaryOperation.MUL)) then |
| 42 |
|
| 43 |
if term1:isconstant() and term2:isconstant() then |
| 44 |
local result = self:evaluate() |
| 45 |
if result == result:one() then |
| 46 |
return BinaryOperation(BinaryOperation.MUL, {}) |
| 47 |
end |
| 48 |
return BinaryOperation(BinaryOperation.MUL, {result}) |
| 49 |
end |
| 50 |
|
| 51 |
-- Uses the property that x*1 = x |
| 52 |
if term1:isconstant() and term1 == term1:one() then |
| 53 |
return BinaryOperation(BinaryOperation.MUL, {term2}) |
| 54 |
end |
| 55 |
|
| 56 |
if term2:isconstant() and term2 == term2:one() then |
| 57 |
return BinaryOperation(BinaryOperation.MUL, {term1}) |
| 58 |
end |
| 59 |
|
| 60 |
-- Distributes constants if the other term is a sum expression. |
| 61 |
if term1:isconstant() and term2.operation == BinaryOperation.ADD then |
| 62 |
local distributed = BinaryOperation(BinaryOperation.ADD, {}) |
| 63 |
for i, exp in ipairs(term2:subexpressions()) do |
| 64 |
distributed.expressions[i] = term1 * exp |
| 65 |
end |
| 66 |
return BinaryOperation(BinaryOperation.MUL, {distributed:autosimplify()}) |
| 67 |
end |
| 68 |
|
| 69 |
if term2:isconstant() and term1.operation == BinaryOperation.ADD then |
| 70 |
local distributed = BinaryOperation(BinaryOperation.ADD, {}) |
| 71 |
for i, exp in ipairs(term1:subexpressions()) do |
| 72 |
distributed.expressions[i] = term2 * exp |
| 73 |
end |
| 74 |
return BinaryOperation(BinaryOperation.MUL, {distributed:autosimplify()}) |
| 75 |
end |
| 76 |
|
| 77 |
-- Uses the property that sqrt(a,r)*sqrt(b,r) = sqrt(a*b,r) if a,r are positive integers |
| 78 |
if term1:type() == SqrtExpression and term2:type() == SqrtExpression and term1.expression:isconstant() and term2.expression:isconstant() and term1.root:type() == Integer then |
| 79 |
if term1.root == term2.root and term1.expression > Integer.zero() then |
| 80 |
local expression = term1.expression*term2.expression |
| 81 |
local result = SqrtExpression(expression,term1.root):autosimplify() |
| 82 |
if result == Integer.one() then |
| 83 |
return BinaryOperation(BinaryOperation.MUL,{}) |
| 84 |
else |
| 85 |
return BinaryOperation(BinaryOperation.MUL,{result}) |
| 86 |
end |
| 87 |
end |
| 88 |
end |
| 89 |
|
| 90 |
--if term1.operation == BinaryOperation.POW and term2.operation == BinaryOperation.POW and term1.expressions[1]:type() == SqrtExpression and term2.expressions[1]:type() == SqrtExpression and term1.expressions[2]:type() == Integer and term2.expressions[2]:type() == Integer and term1.expressions[2] < Integer.zero() and term2.expressions[2] < Integer.zero() and term1.expressions[1].root == term2.expressions[1].root then |
| 91 |
-- local expo1 = term1.expressions[2]:neg() |
| 92 |
-- local expo2 = term2.expressions[2]:neg() |
| 93 |
-- local root = term1.expressions[1].root |
| 94 |
-- local expr1 = term1.expressions[1].expression |
| 95 |
-- local expr2 = term2.expressions[1].expression |
| 96 |
-- local result1 = BinaryOperation(BinaryOperation.POW,{SqrtExpression(expr1,root),expo1}):simplifypower() |
| 97 |
-- local result2 = BinaryOperation(BinaryOperation.POW,{SqrtExpression(expr2,root),expo2}):simplifypower() |
| 98 |
-- local result = BinaryOperation(BinaryOperation.MUL,{result1,result2}):autosimplify() |
| 99 |
-- if result == Integer.one() then |
| 100 |
-- return BinaryOperation(BinaryOperation.MUL,{}) |
| 101 |
-- end |
| 102 |
-- if result:type() == Integer then |
| 103 |
-- return BinaryOperation(BinaryOperation.MUL,{Rational(Integer.one(),result)}) |
| 104 |
-- end |
| 105 |
-- return BinaryOperation(BinaryOperation.MUL,{BinaryOperation(BinaryOperation.POW, {result,Integer(-1)})}):autosimplify() |
| 106 |
--end |
| 107 |
|
| 108 |
-- Uses the property that x^a*x^b=x^(a+b) |
| 109 |
local revertterm1 = false |
| 110 |
local revertterm2 = false |
| 111 |
if term1.operation ~= BinaryOperation.POW then |
| 112 |
term1 = BinaryOperation(BinaryOperation.POW, {term1, Integer.one()}) |
| 113 |
revertterm1 = true |
| 114 |
end |
| 115 |
if term2.operation ~= BinaryOperation.POW then |
| 116 |
term2 = BinaryOperation(BinaryOperation.POW, {term2, Integer.one()}) |
| 117 |
revertterm2 = true |
| 118 |
end |
| 119 |
if term1.expressions[1] == term2.expressions[1] |
| 120 |
--and not |
| 121 |
-- (term1.expressions[1]:type() == Integer and |
| 122 |
-- term1.expressions[2]:type() ~= term2.--expressions[2]:type()) |
| 123 |
then |
| 124 |
local result = BinaryOperation(BinaryOperation.POW, |
| 125 |
{term1.expressions[1], |
| 126 |
BinaryOperation(BinaryOperation.ADD, |
| 127 |
{term1.expressions[2], term2.expressions[2]}):autosimplify()}):autosimplify() |
| 128 |
if result:isconstant() and result == result:one() then |
| 129 |
return BinaryOperation(BinaryOperation.MUL, {}) |
| 130 |
end |
| 131 |
return BinaryOperation(BinaryOperation.MUL, {result}) |
| 132 |
end |
| 133 |
|
| 134 |
if revertterm1 then |
| 135 |
term1 = term1.expressions[1] |
| 136 |
end |
| 137 |
if revertterm2 then |
| 138 |
term2 = term2.expressions[1] |
| 139 |
end |
| 140 |
|
| 141 |
if term2:order(term1) then |
| 142 |
return BinaryOperation(BinaryOperation.MUL, {term2, term1}) |
| 143 |
end |
| 144 |
|
| 145 |
return self |
| 146 |
end |
| 147 |
|
| 148 |
if term1.operation == BinaryOperation.MUL and not (term2.operation == BinaryOperation.MUL) then |
| 149 |
return term1:mergeproducts(BinaryOperation(BinaryOperation.MUL, {term2})) |
| 150 |
end |
| 151 |
|
| 152 |
if not (term1.operation == BinaryOperation.MUL) and term2.operation == BinaryOperation.MUL then |
| 153 |
return BinaryOperation(BinaryOperation.MUL, {term1}):mergeproducts(term2) |
| 154 |
end |
| 155 |
|
| 156 |
return term1:mergeproducts(term2) |
| 157 |
end |
| 158 |
|
| 159 |
local rest = {} |
| 160 |
for index, expression in ipairs(self.expressions) do |
| 161 |
if index > 1 then |
| 162 |
rest[index - 1] = expression |
| 163 |
end |
| 164 |
end |
| 165 |
|
| 166 |
local result = BinaryOperation(BinaryOperation.MUL, rest):simplifyproductrec() |
| 167 |
|
| 168 |
if term1.operation ~= BinaryOperation.MUL then |
| 169 |
term1 = BinaryOperation(BinaryOperation.MUL, {term1}) |
| 170 |
end |
| 171 |
if result.operation ~= BinaryOperation.MUL then |
| 172 |
result = BinaryOperation(BinaryOperation.MUL, {result}) |
| 173 |
end |
| 174 |
return term1:mergeproducts(result) |
| 175 |
end |
| 176 |
|
| 177 |
-- Merges two lists of products |
| 178 |
function BinaryOperation:mergeproducts(other) |
| 179 |
if not self.expressions[1] then |
| 180 |
return other |
| 181 |
end |
| 182 |
|
| 183 |
if not other.expressions[1] then |
| 184 |
return self |
| 185 |
end |
| 186 |
|
| 187 |
local first = BinaryOperation(BinaryOperation.MUL, {self.expressions[1], other.expressions[1]}):simplifyproductrec() |
| 188 |
|
| 189 |
local selfrest = {} |
| 190 |
for index, expression in ipairs(self.expressions) do |
| 191 |
if index > 1 then |
| 192 |
selfrest[index - 1] = expression |
| 193 |
end |
| 194 |
end |
| 195 |
|
| 196 |
local otherrest = {} |
| 197 |
for index, expression in ipairs(other.expressions) do |
| 198 |
if index > 1 then |
| 199 |
otherrest[index - 1] = expression |
| 200 |
end |
| 201 |
end |
| 202 |
|
| 203 |
if first.operation ~= BinaryOperation.MUL or not first.expressions[2] then |
| 204 |
local result = BinaryOperation(self.operation, selfrest):mergeproducts(BinaryOperation(other.operation, otherrest)) |
| 205 |
if not first.expressions[1] then |
| 206 |
return result |
| 207 |
end |
| 208 |
table.insert(result.expressions, 1, first.expressions[1]) |
| 209 |
|
| 210 |
if result.operation == BinaryOperation.MUL and not result.expressions[3] and result.expressions[1] and result.expressions[2] then |
| 211 |
if result.expressions[1]:isconstant() and result.expressions[2]:isconstant() then |
| 212 |
return result:simplifyproductrec() |
| 213 |
end |
| 214 |
end |
| 215 |
return result |
| 216 |
end |
| 217 |
|
| 218 |
local result |
| 219 |
if first.expressions[1] == self.expressions[1] then |
| 220 |
result = BinaryOperation(self.operation, selfrest):mergeproducts(other) |
| 221 |
else |
| 222 |
result = self:mergeproducts(BinaryOperation(other.operation, otherrest)) |
| 223 |
end |
| 224 |
table.insert(result.expressions, 1, first.expressions[1]) |
| 225 |
if result.operation == BinaryOperation.MUL and not result.expressions[3] and result.expressions[1] and result.expressions[2] then |
| 226 |
if result.expressions[1]:isconstant() and result.expressions[2]:isconstant() then |
| 227 |
return result:simplifyproductrec() |
| 228 |
end |
| 229 |
end |
| 230 |
return result |
| 231 |
end |