From c5a5a48836994a28d7fe4e8a6f500ea1ed458e4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89milie=20Feral?= Date: Fri, 27 Oct 2017 13:07:26 +0200 Subject: [PATCH] [poincare] Change 1+-6*cos(2) -> 1-6cos(2) Change-Id: Ic5dde9bf2f8a76d17a34544be0d17beaf94f6905 --- poincare/src/addition.cpp | 8 ++++++-- poincare/src/multiplication.cpp | 23 ++++++++++++++--------- poincare/test/simplify_easy.cpp | 1 + 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/poincare/src/addition.cpp b/poincare/src/addition.cpp index b9ccfbedb..95a527f40 100644 --- a/poincare/src/addition.cpp +++ b/poincare/src/addition.cpp @@ -135,9 +135,13 @@ Expression * Addition::immediateBeautify(Context & context, AngleUnit angleUnit) int index = 0; while (index < numberOfOperands()) { // a+(-1)*b+... -> a-b+... - if (operand(index)->type() == Type::Multiplication && operand(index)->operand(0)->type() == Type::Rational && static_cast(operand(index)->operand(0))->isMinusOne()) { + if (operand(index)->type() == Type::Multiplication && operand(index)->operand(0)->type() == Type::Rational && operand(index)->operand(0)->sign() < 0) { Multiplication * m = static_cast((Expression *)operand(index)); - m->removeOperand(m->operand(0), true); + if (static_cast(operand(index)->operand(0))->isMinusOne()) { + m->removeOperand(m->operand(0), true); + } else { + const_cast(operand(index)->operand(0))->turnIntoPositive(context, angleUnit); + } const Expression * subtractant = m->squashUnaryHierarchy(); if (index == 0) { const Expression * opOperand[1] = {subtractant}; diff --git a/poincare/src/multiplication.cpp b/poincare/src/multiplication.cpp index 442295afe..730ab4f66 100644 --- a/poincare/src/multiplication.cpp +++ b/poincare/src/multiplication.cpp @@ -348,6 +348,20 @@ bool Multiplication::isUselessOperand(const Rational * r) { } Expression * Multiplication::immediateBeautify(Context & context, AngleUnit angleUnit) { + // -1*A -> -A or (-n)*A -> -n*A + if (operand(0)->type() == Type::Rational && operand(0)->sign() < 0) { + if (static_cast(operand(0))->isMinusOne()) { + removeOperand((Expression *)operand(0), true); + } else { + const_cast(operand(0))->turnIntoPositive(context, angleUnit); + } + Expression * e = squashUnaryHierarchy(); + const Expression * oppOperand[1] = {e->clone()}; + Opposite * o = new Opposite(oppOperand, false); + e->replaceWith(o, true); + const_cast(o->operand(0))->immediateBeautify(context, angleUnit); + return o; + } // Merge negative power: a*b^-1*c^(-Pi)*d = a*(b*c^Pi)^-1 Expression * e = mergeNegativePower(context, angleUnit); if (e->type() == Type::Power) { @@ -409,15 +423,6 @@ Expression * Multiplication::immediateBeautify(Context & context, AngleUnit angl return d->immediateBeautify(context, angleUnit); } } - // -1*A -> -A - if (operand(0)->type() == Type::Rational && static_cast(operand(0))->isMinusOne()) { - removeOperand((Expression *)operand(0), true); - Expression * e = squashUnaryHierarchy(); - const Expression * oppOperand[1] = {e->clone()}; - Opposite * o = new Opposite(oppOperand, false); - e->replaceWith(o, true); - return o; - } return this; } diff --git a/poincare/test/simplify_easy.cpp b/poincare/test/simplify_easy.cpp index a24f87976..bff8c913d 100644 --- a/poincare/test/simplify_easy.cpp +++ b/poincare/test/simplify_easy.cpp @@ -35,6 +35,7 @@ void assert_parsed_expression_simplify_to(const char * expression, const char * QUIZ_CASE(poincare_simplify_easy) { //assert_parsed_expression_simplify_to("(((R(6)-R(2))/4)/((R(6)+R(2))/4))+1", "((1/2)*R(6))/((R(6)+R(2))/4)"); + assert_parsed_expression_simplify_to("2+13cos(2)-23cos(2)", "2-10cos(2)"); assert_parsed_expression_simplify_to("1/(R(2)+R(3))", "-(R(2)-R(3))"); assert_parsed_expression_simplify_to("1/(5+R(3))", "(5-R(3))/22"); assert_parsed_expression_simplify_to("1/(R(2)+4)", "(4-R(2))/14");