diff --git a/poincare/include/poincare/expression.h b/poincare/include/poincare/expression.h index cebc0c2e0..20284cfd0 100644 --- a/poincare/include/poincare/expression.h +++ b/poincare/include/poincare/expression.h @@ -47,6 +47,8 @@ class Expression : public TreeByReference { friend class MatrixTranspose; friend class Multiplication; friend class NaperianLogarithm; + friend class NthRoot; + friend class PermuteCoefficient; friend class Sine; friend class Store; diff --git a/poincare/include/poincare/permute_coefficient.h b/poincare/include/poincare/permute_coefficient.h index f4dce1d86..aa7066527 100644 --- a/poincare/include/poincare/permute_coefficient.h +++ b/poincare/include/poincare/permute_coefficient.h @@ -59,4 +59,3 @@ private: } #endif - diff --git a/poincare/src/nth_root.cpp b/poincare/src/nth_root.cpp index 49b4eb8ab..a7f980088 100644 --- a/poincare/src/nth_root.cpp +++ b/poincare/src/nth_root.cpp @@ -40,17 +40,21 @@ Evaluation NthRootNode::templatedApproximate(Context& context, Preferences::A } Expression NthRoot::shallowReduce(Context & context, Preferences::AngleUnit angleUnit) { - Expression e = Expression::defaultShallowReduce(context, angleUnit); - if (e.isUndefinedOrAllocationFailure()) { - return e; + { + Expression e = Expression::defaultShallowReduce(context, angleUnit); + if (e.isUndefinedOrAllocationFailure()) { + return e; + } } #if MATRIX_EXACT_REDUCING if (childAtIndex(0).type() == ExpressionNode::Type::Matrix || childAtIndex(1).type() == ExpressionNode:Type::Matrix) { return Undefined(); } #endif - Expression invIndex = Power(childAtIndex(1), Rational(-1)).shallowReduce(context, angleUnit); + Expression invIndex = Power(childAtIndex(1), Rational(-1)); Power p = Power(childAtIndex(0), invIndex); + invIndex.shallowReduce(context, angleUnit); + replaceWithInPlace(p); return p.shallowReduce(context, angleUnit); } diff --git a/poincare/src/permute_coefficient.cpp b/poincare/src/permute_coefficient.cpp index 844c1a6eb..b02d91271 100644 --- a/poincare/src/permute_coefficient.cpp +++ b/poincare/src/permute_coefficient.cpp @@ -46,39 +46,47 @@ Evaluation PermuteCoefficientNode::templatedApproximate(Context& context, Pre } Expression PermuteCoefficient::shallowReduce(Context & context, Preferences::AngleUnit angleUnit) { - Expression e = Expression::defaultShallowReduce(context, angleUnit); - if (e.isUndefinedOrAllocationFailure()) { - return e; + { + Expression e = Expression::defaultShallowReduce(context, angleUnit); + if (e.isUndefinedOrAllocationFailure()) { + return e; + } } - Expression op0 = childAtIndex(0); - Expression op1 = childAtIndex(1); + Expression c0 = childAtIndex(0); + Expression c1 = childAtIndex(1); #if MATRIX_EXACT_REDUCING - if (op0.type() == ExpressionNode::Type::Matrix || op1.type() == ExpressionNode::Type::Matrix) { + if (c0.type() == ExpressionNode::Type::Matrix || c1.type() == ExpressionNode::Type::Matrix) { return replaceWith(new Undefined(), true); } #endif - if (op0.type() == ExpressionNode::Type::Rational) { - Rational r0 = static_cast(op0); + if (c0.type() == ExpressionNode::Type::Rational) { + Rational r0 = static_cast(c0); if (!r0.integerDenominator().isOne() || r0.sign() == ExpressionNode::Sign::Negative) { - return Undefined(); + Expression result = Undefined(); + replaceWithInPlace(result); + return result; } } - if (op1.type() == ExpressionNode::Type::Rational) { - Rational r1 = static_cast(op1); + if (c1.type() == ExpressionNode::Type::Rational) { + Rational r1 = static_cast(c1); if (!r1.integerDenominator().isOne() || r1.sign() == ExpressionNode::Sign::Negative) { - return Undefined(); + Expression result = Undefined(); + replaceWithInPlace(result); + return result; } } - if (op0.type() != ExpressionNode::Type::Rational || op1.type() != ExpressionNode::Type::Rational) { + if (c0.type() != ExpressionNode::Type::Rational || c1.type() != ExpressionNode::Type::Rational) { return *this; } - Rational r0 = static_cast(op0); - Rational r1 = static_cast(op1); + Rational r0 = static_cast(c0); + Rational r1 = static_cast(c1); Integer n = r0.unsignedIntegerNumerator(); Integer k = r1.unsignedIntegerNumerator(); if (n.isLowerThan(k)) { - return Rational(0); + Expression result = Rational(0); + replaceWithInPlace(result); + return result; } /* if n is too big, we do not reduce to avoid too long computation. * The permute coefficient will be evaluate approximatively later */ @@ -92,8 +100,10 @@ Expression PermuteCoefficient::shallowReduce(Context & context, Preferences::Ang result = Integer::Multiplication(result, factor); } assert(!result.isInfinity()); // < permute(k_maxNValue, k_maxNValue-1)~10^158 - return Rational(result); -} + Expression rationalResult = Rational(result); + replaceWithInPlace(rationalResult); + return rationalResult; } +}