[poincare] Fix ComplexCartesian::powerInteger to handle pure imaginary

complex
This commit is contained in:
Émilie Feral
2018-12-14 12:14:17 +01:00
committed by Léa Saviot
parent 76ec7d3a58
commit ced136f843
2 changed files with 21 additions and 1 deletions

View File

@@ -177,6 +177,26 @@ ComplexCartesian ComplexCartesian::powerInteger(int n, Context & context, Prefer
Expression a = real();
Expression b = imag();
assert(n > 0);
assert(!b.isRationalZero());
// Special case: a == 0 (otherwise, we are going to introduce undefined expressions - a^0 = NAN)
// (b*i)^n = b^n*i^n with i^n == i, -i, 1 or -1
if (a.isRationalZero()) {
ComplexCartesian result;
Expression bpow = Power(b, Rational(n));
if (n/2%2 == 1) {
Expression temp = Multiplication(Rational(-1), bpow);
bpow.shallowReduce(context, angleUnit, target);
bpow = temp;
}
if (n%2 == 0) {
result = ComplexCartesian(bpow, Rational(0));
} else {
result = ComplexCartesian(Rational(0), bpow);
}
bpow.shallowReduce(context, angleUnit, target);
return result;
}
// (a+ib) = a^n+i*b*a^(n-1)+(-1)*b^2*a^(n-2)+(-i)*b^3*a^(n-3)+b^3*a^(n-4)+...
// Real part: A = a^n+(-1)*b^2*a^(n-2)+...
// Imaginary part: B = b*a^(n-1)

View File

@@ -384,7 +384,7 @@ Expression Power::shallowReduce(Context & context, Preferences::AngleUnit angleU
Expression base = childAtIndex(0);
Expression index = childAtIndex(1);
/* Step 0: if both children are true unresolved complexes, the result is not simplified. TODO? */
if (!base.isReal(context, angleUnit) && !index.isReal(context, angleUnit)) {
if (!base.isReal(context, angleUnit) && base.type() != ExpressionNode::Type::ComplexCartesian && !index.isReal(context, angleUnit) && index.type() != ExpressionNode::Type::ComplexCartesian) {
return *this;
}