diff --git a/poincare/include/poincare/expression.h b/poincare/include/poincare/expression.h index d0f90eed4..36465b574 100644 --- a/poincare/include/poincare/expression.h +++ b/poincare/include/poincare/expression.h @@ -195,6 +195,7 @@ private: /* Layout Engine */ virtual ExpressionLayout * privateCreateLayout(FloatDisplayMode floatDisplayMode, ComplexFormat complexFormat) const = 0; /* Simplification */ + static void Reduce(Expression ** expressionAddress, Context & context, AngleUnit angleUnit); Expression * deepBeautify(Context & context, AngleUnit angleUnit); Expression * deepSimplify(Context & context, AngleUnit angleUnit); // TODO: should be virtual pure diff --git a/poincare/src/expression.cpp b/poincare/src/expression.cpp index 9090f820a..c9582348d 100644 --- a/poincare/src/expression.cpp +++ b/poincare/src/expression.cpp @@ -131,6 +131,12 @@ void Expression::simplify(Expression ** expressionAddress, Context & context, An *expressionAddress = root.editableOperand(0); } +void Expression::Reduce(Expression ** expressionAddress, Context & context, AngleUnit angleUnit) { + SimplificationRoot root(*expressionAddress); + root.deepSimplify(context, angleUnit); + *expressionAddress = root.editableOperand(0); +} + Expression * Expression::deepSimplify(Context & context, AngleUnit angleUnit) { for (int i = 0; i < numberOfOperands(); i++) { if ((editableOperand(i))->deepSimplify(context, angleUnit)->type() == Type::Undefined && this->type() != Type::SimplificationRoot) { diff --git a/poincare/src/multiplication.cpp b/poincare/src/multiplication.cpp index c53c32205..3fc7f36d1 100644 --- a/poincare/src/multiplication.cpp +++ b/poincare/src/multiplication.cpp @@ -495,7 +495,7 @@ void Multiplication::addMissingFactors(Expression * factor, Context & context, A for (int i = 0; i < numberOfOperands(); i++) { if (TermsHaveIdenticalBase(operand(i), factor)) { Subtraction * sub = new Subtraction(CreateExponent(editableOperand(i)), CreateExponent(factor), false); - Expression::simplify((Expression **)&sub, context, angleUnit); + Reduce((Expression **)&sub, context, angleUnit); if (sub->sign() == Sign::Negative) { // index[0] < index[1] factor->replaceOperand(factor->editableOperand(1), new Opposite(sub, true), true); factor->deepSimplify(context, angleUnit); diff --git a/poincare/src/trigonometry.cpp b/poincare/src/trigonometry.cpp index 643ba0d17..76acaf67c 100644 --- a/poincare/src/trigonometry.cpp +++ b/poincare/src/trigonometry.cpp @@ -1,5 +1,4 @@ #include -#include #include #include #include @@ -180,15 +179,14 @@ Expression * Trigonometry::table(const Expression * e, Expression::Type type, Co if (input == nullptr) { continue; } - SimplificationRoot inputRoot(input); - inputRoot.deepSimplify(context, angleUnit); // input expression does not change, no root needed and we can use entry after - if (inputRoot.operand(0)->isIdenticalTo(e)) { + Expression::Reduce(&input, context, angleUnit); + if (input->isIdenticalTo(e)) { Expression * output = Expression::parse(cheatTable[i][outputIndex]); if (output == nullptr) { return nullptr; } - SimplificationRoot outputRoot(output); - return outputRoot.deepSimplify(context, angleUnit)->editableOperand(0); + Expression::Reduce(&output, context, angleUnit); + return output; } } return nullptr;