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