[poincare] In Multiplication::beautify 1/3*Pi^-1-> 1/(3Pi) instead of

(1/3)/pi

Change-Id: Id29e08d3a51a0f61a140aaeb7f6f4d3adb387160
This commit is contained in:
Émilie Feral
2017-10-11 12:19:29 +02:00
parent 7898fcbf91
commit 5b11c16435
2 changed files with 22 additions and 1 deletions

View File

@@ -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<const Rational *>(operand(index)->operand(1))->isMinusOne()) {
Power * p = static_cast<Power *>((Expression *)operand(index));
const Expression * denominatorOperand = p->operand(0);
Expression * denominatorOperand = const_cast<Expression *>(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<const Rational *>(numeratorOperand->operand(0))->denominator().isOne()) {
Rational * r = static_cast<Rational *>((Expression *)numeratorOperand->operand(0));
if (denominatorOperand->type() == Type::Multiplication) {
const Expression * integerDenominator[1] = {new Rational(r->denominator())};
static_cast<Multiplication *>(denominatorOperand)->addOperands(integerDenominator, 1);
static_cast<Multiplication *>(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;

View File

@@ -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");