From 082e6468f701a19f0e58cc09b0095961da3b67ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89milie=20Feral?= Date: Fri, 6 Oct 2017 17:06:46 +0200 Subject: [PATCH] [poincare] In power: (a^b)^c = a^(b+c) if a > 0 or c is integer Change-Id: Iad7b559de5e15972a54e322bca832d78596abf53 --- poincare/include/poincare/power.h | 2 +- poincare/src/power.cpp | 25 +++++++++++++++++-------- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/poincare/include/poincare/power.h b/poincare/include/poincare/power.h index 84a1137f9..043e3c3f6 100644 --- a/poincare/include/poincare/power.h +++ b/poincare/include/poincare/power.h @@ -34,7 +34,7 @@ private: int compareToSameTypeExpression(const Expression * e) const override; /* Simplification */ void immediateSimplify() override; - void simplifyPowerPower(Power * p, Rational * r); + void simplifyPowerPower(Power * p, Expression * r); void simplifyPowerMultiplication(Multiplication * m, Rational * r); void simplifyRationalRationalPower(Rational * a, Rational * b); static Expression * CreateSimplifiedIntegerRationalPower(Integer i, Rational * r); diff --git a/poincare/src/power.cpp b/poincare/src/power.cpp index f712ce132..89638ee71 100644 --- a/poincare/src/power.cpp +++ b/poincare/src/power.cpp @@ -160,10 +160,6 @@ void Power::immediateSimplify() { Rational r = Rational::Power(*(e), b->numerator()); replaceWith(new Rational(r),true); return; - } else if (operand(0)->type() == Type::Power) { - Power * e = static_cast((Expression *)operand(0)); - simplifyPowerPower(e, b); - return; } else if (operand(0)->type() == Type::Multiplication) { Multiplication * e = static_cast((Expression *)operand(0)); simplifyPowerMultiplication(e, b); @@ -176,14 +172,27 @@ void Power::immediateSimplify() { return; } } - // TODO: (a^b)^c -> a^(b+c) if a > 0 + // (a^b)^c -> a^(b+c) if a > 0 or c is integer + if (operand(0)->type() == Type::Power) { + Power * p = static_cast((Expression *)operand(0)); + // Check is a > 0 or c is Integer + if (p->operand(0)->isPositive() || + (operand(1)->type() == Type::Rational && static_cast((Expression *)operand(1))->denominator().isOne())) { + simplifyPowerPower(p, const_cast(operand(1))); + return; + } + } // TODO: (a*b)^c -> |a|^c*(sign(a)*b)^c } -void Power::simplifyPowerPower(Power * p, Rational * r) { - const Expression * multOperands[2] = {const_cast(p->operand(1)), r}; +void Power::simplifyPowerPower(Power * p, Expression * e) { + Expression * p0 = const_cast(p->operand(0)); + Expression * p1 = const_cast(p->operand(1)); + p->detachOperands(); + const Expression * multOperands[2] = {p1, e}; Multiplication * m = new Multiplication(multOperands, 2, false); - replaceOperand(r, m, false); + replaceOperand(e, m, false); + replaceOperand(p, p0, true); m->immediateSimplify(); immediateSimplify(); }