From 5b11c16435dbd35f0dcfb5b667b8889b4e8b2d03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89milie=20Feral?= Date: Wed, 11 Oct 2017 12:19:29 +0200 Subject: [PATCH] [poincare] In Multiplication::beautify 1/3*Pi^-1-> 1/(3Pi) instead of (1/3)/pi Change-Id: Id29e08d3a51a0f61a140aaeb7f6f4d3adb387160 --- poincare/src/multiplication.cpp | 20 +++++++++++++++++++- poincare/test/simplify_easy.cpp | 3 +++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/poincare/src/multiplication.cpp b/poincare/src/multiplication.cpp index e4a0e7431..8d0c4eaad 100644 --- a/poincare/src/multiplication.cpp +++ b/poincare/src/multiplication.cpp @@ -214,12 +214,30 @@ void Multiplication::immediateBeautify() { // a*b^(-1)*... -> a*.../b if (operand(index)->type() == Type::Power && operand(index)->operand(1)->type() == Type::Rational && static_cast(operand(index)->operand(1))->isMinusOne()) { Power * p = static_cast((Expression *)operand(index)); - const Expression * denominatorOperand = p->operand(0); + Expression * denominatorOperand = const_cast(p->operand(0)); p->detachOperand(denominatorOperand); removeOperand(p, true); Multiplication * numeratorOperand = (Multiplication *)clone(); const Expression * divOperands[2] = {numeratorOperand, denominatorOperand}; Division * d = new Division(divOperands, false); + /* We want 1/3*Pi*(ln(2))^-1 -> Pi/(3ln(2)) and not ((1/3)Pi)/ln(2)*/ + if (numeratorOperand->operand(0)->type() == Type::Rational && !static_cast(numeratorOperand->operand(0))->denominator().isOne()) { + Rational * r = static_cast((Expression *)numeratorOperand->operand(0)); + if (denominatorOperand->type() == Type::Multiplication) { + const Expression * integerDenominator[1] = {new Rational(r->denominator())}; + static_cast(denominatorOperand)->addOperands(integerDenominator, 1); + static_cast(denominatorOperand)->sortChildren(); + } else { + const Expression * multOperands[2] = {new Rational(r->denominator()), denominatorOperand->clone()}; + Multiplication * m = new Multiplication(multOperands, 2, false); + denominatorOperand->replaceWith(m, true); + } + if (!r->numerator().isOne() || numeratorOperand->numberOfOperands()) { + numeratorOperand->replaceOperand(r, new Rational(r->numerator()), true); + } else { + numeratorOperand->removeOperand(r, true); + } + } numeratorOperand->squashUnaryHierarchy(); replaceWith(d, true); return; diff --git a/poincare/test/simplify_easy.cpp b/poincare/test/simplify_easy.cpp index c1f4e7ef8..59cbb1259 100644 --- a/poincare/test/simplify_easy.cpp +++ b/poincare/test/simplify_easy.cpp @@ -120,7 +120,10 @@ QUIZ_CASE(poincare_simplify_easy) { assert_parsed_expression_simplify_to("A^3*A^(-3)", "1"); assert_parsed_expression_simplify_to("1+A+2+B+3", "6+A+B"); assert_parsed_expression_simplify_to("(x+1)*(x+2)", "x^2+3x+2"); + assert_parsed_expression_simplify_to("(x+1)*(x-1)", "-1+x^2"); + assert_parsed_expression_simplify_to("11P/(22P+11P)", "1/3"); + assert_parsed_expression_simplify_to("11/(22P+11P)", "1/(3P)"); /* This does not work but should not as it is above k_primorial32 = 1*3*5*7*11*... (product of first 32 primes. */ //assert_parsed_expression_simplify_to("1881676377434183981909562699940347954480361860897069^(1/3)", "123456789123456789");