| 1 |
-- Seperates the various binary operations into their own files for readability |
| 2 |
|
| 3 |
--- Automatic simplification of power expressions. |
| 4 |
--- @return BinaryOperation |
| 5 |
function BinaryOperation:simplifypower() |
| 6 |
local base = self.expressions[1] |
| 7 |
local exponent = self.expressions[2] |
| 8 |
|
| 9 |
if base:isconstant() and exponent:isconstant() and exponent:getring() ~= Rational:getring() then |
| 10 |
return self:evaluate() |
| 11 |
end |
| 12 |
|
| 13 |
-- Simplifies i^x for x integer. |
| 14 |
if base == I and exponent:isconstant() and exponent:getring() == Integer:getring() then |
| 15 |
if exponent % Integer(4) == Integer(0) then |
| 16 |
return Integer(1) |
| 17 |
end |
| 18 |
if exponent % Integer(4) == Integer(1) then |
| 19 |
return I |
| 20 |
end |
| 21 |
if exponent % Integer(4) == Integer(2) then |
| 22 |
return Integer(-1) |
| 23 |
end |
| 24 |
if exponent % Integer(4) == Integer(3) then |
| 25 |
return -I |
| 26 |
end |
| 27 |
end |
| 28 |
|
| 29 |
-- Simplifies complex numbers raised to negative integer powers |
| 30 |
if not base:isrealconstant() and base:iscomplexconstant() and exponent:isconstant() and exponent:getring() == Integer:getring() and exponent < Integer.zero() then |
| 31 |
local a |
| 32 |
local b |
| 33 |
if base.operation == BinaryOperation.MUL then |
| 34 |
a = Integer.zero() |
| 35 |
b = base.expressions[1] |
| 36 |
elseif base.operation == BinaryOperation.ADD and base.expressions[2] == I then |
| 37 |
a = base.expressions[1] |
| 38 |
b = Integer.one() |
| 39 |
else |
| 40 |
a = base.expressions[1] |
| 41 |
b = base.expressions[2].expressions[1] |
| 42 |
end |
| 43 |
return (((a-b*I)/(a^Integer(2)+b^Integer(2)))^(-exponent)):expand():autosimplify() |
| 44 |
end |
| 45 |
|
| 46 |
-- Uses the property that 0^x = 0 if x does not equal 0 |
| 47 |
if base:isconstant() and base == base:zero() then |
| 48 |
return Integer.zero() |
| 49 |
end |
| 50 |
|
| 51 |
-- Uses the property that 1^x = 1 |
| 52 |
if base:isconstant() and base == base:one() then |
| 53 |
return base:one() |
| 54 |
end |
| 55 |
|
| 56 |
-- Uses the property that x^0 = 1 |
| 57 |
if exponent:isconstant() and exponent == exponent:zero() then |
| 58 |
return exponent:one() |
| 59 |
end |
| 60 |
|
| 61 |
-- Uses the property that x^1 = x |
| 62 |
if exponent:isconstant() and exponent == exponent:one() then |
| 63 |
return base |
| 64 |
end |
| 65 |
|
| 66 |
-- Uses the property that b ^ (log(b, x)) == x |
| 67 |
if exponent:type() == Logarithm and exponent.base == base then |
| 68 |
return exponent.expression |
| 69 |
end |
| 70 |
|
| 71 |
-- Uses the property that b ^ (a * log(b, x)) == x ^ a |
| 72 |
if exponent.operation == BinaryOperation.MUL then |
| 73 |
local x |
| 74 |
local rest = Integer.one() |
| 75 |
for _, expression in ipairs(exponent.expressions) do |
| 76 |
if expression:type() == Logarithm and expression.base == base and not log then |
| 77 |
x = expression.expression |
| 78 |
else |
| 79 |
rest = rest * expression |
| 80 |
end |
| 81 |
end |
| 82 |
if x then |
| 83 |
return (x ^ rest):autosimplify() |
| 84 |
end |
| 85 |
end |
| 86 |
|
| 87 |
-- Uses the property that (x^a)^b = x^(a*b) |
| 88 |
if not base:isatomic() and base.operation == BinaryOperation.POW and exponent:isconstant() then |
| 89 |
base, exponent = base.expressions[1], BinaryOperation(BinaryOperation.MUL, {exponent, base.expressions[2]}):autosimplify() |
| 90 |
return BinaryOperation(BinaryOperation.POW, {base, exponent}):autosimplify() |
| 91 |
end |
| 92 |
|
| 93 |
-- Uses the property that (x_1*x_2*...*x_n)^a = x_1^a*x_2^a*..x_n^a if a is an integer |
| 94 |
if base.operation == BinaryOperation.MUL and exponent:type() == Integer then |
| 95 |
local results = {} |
| 96 |
for index, expression in ipairs(base.expressions) do |
| 97 |
results[index] = BinaryOperation(BinaryOperation.POW, {expression, exponent}):autosimplify() |
| 98 |
end |
| 99 |
return BinaryOperation(BinaryOperation.MUL, results):autosimplify() |
| 100 |
end |
| 101 |
|
| 102 |
-- Uses the property that sqrt(x,r)^d == sqrt(x,r/d) |
| 103 |
if base:type() == SqrtExpression and exponent:type() == Integer and exponent > Integer.zero() then |
| 104 |
local root = base.root |
| 105 |
local expr = base.expression |
| 106 |
local comm = Integer.gcd(root,exponent) |
| 107 |
root = root / comm |
| 108 |
local expo = exponent / comm |
| 109 |
expr = expr ^ expo |
| 110 |
return SqrtExpression(expr,root):autosimplify() |
| 111 |
end |
| 112 |
|
| 113 |
-- Rationalizing SqrtExpressions |
| 114 |
if base:type() == SqrtExpression and exponent:type() == Integer and base.expression:type() == Integer and exponent < Integer.zero() then |
| 115 |
local root = base.root |
| 116 |
local expr = base.expression |
| 117 |
local result = (SqrtExpression(expr ^ (root - Integer.one()),root) / expr) ^ exponent:neg() |
| 118 |
return result:autosimplify() |
| 119 |
end |
| 120 |
|
| 121 |
if base:isconstant() and exponent:isconstant() and exponent:getring() == Rational.getring() then |
| 122 |
return self --:simplifyrationalpower() |
| 123 |
end |
| 124 |
|
| 125 |
-- Our expression cannot be simplified |
| 126 |
return self |
| 127 |
end |
| 128 |
|
| 129 |
-- Automatic simplification of rational power expressions |
| 130 |
function BinaryOperation:simplifyrationalpower() |
| 131 |
local base = self.expressions[1] |
| 132 |
local exponent = self.expressions[2] |
| 133 |
|
| 134 |
if base:getring() == Rational.getring() then |
| 135 |
return (BinaryOperation(BinaryOperation.POW, {base.numerator, exponent}):simplifyrationalpower()) / |
| 136 |
(BinaryOperation(BinaryOperation.POW, {base.denominator, exponent}):simplifyrationalpower()) |
| 137 |
end |
| 138 |
|
| 139 |
if base == Integer(-1) then |
| 140 |
if exponent == Integer(1) / Integer(2) then |
| 141 |
return I |
| 142 |
end |
| 143 |
|
| 144 |
return self |
| 145 |
end |
| 146 |
|
| 147 |
local primes = base:primefactorization() |
| 148 |
|
| 149 |
if primes.expressions[1] and not primes.expressions[2] then |
| 150 |
local primeexponent = primes.expressions[1].expressions[2] |
| 151 |
local primebase = primes.expressions[1].expressions[1] |
| 152 |
local newexponent = primeexponent * exponent |
| 153 |
local integerpart |
| 154 |
if newexponent.getring() == Rational.getring() then |
| 155 |
integerpart = newexponent.numerator // newexponent.denominator |
| 156 |
else |
| 157 |
integerpart = newexponent |
| 158 |
end |
| 159 |
|
| 160 |
if integerpart == Integer.zero() then |
| 161 |
return BinaryOperation(BinaryOperation.POW, {primebase, newexponent}) |
| 162 |
end |
| 163 |
return BinaryOperation(BinaryOperation.MUL, |
| 164 |
{BinaryOperation(BinaryOperation.POW, {primebase, integerpart}), |
| 165 |
BinaryOperation(BinaryOperation.POW, {primebase, newexponent - integerpart})}):autosimplify() |
| 166 |
end |
| 167 |
|
| 168 |
return BinaryOperation(BinaryOperation.POW, {primes:autosimplify(), exponent}) |
| 169 |
end |