[poincare] Repair Multiplication::immediateBeautify

Change-Id: I470eaa774cbb247daf6cf5a97b8f32fcab8de1d4
This commit is contained in:
Émilie Feral
2017-10-24 17:18:24 +02:00
parent e3057d5c2b
commit 6b80fd8882

View File

@@ -242,37 +242,40 @@ Expression * Multiplication::immediateBeautify(Context & context, AngleUnit angl
Expression * denominatorOperand = const_cast<Expression *>(p->operand(0));
p->detachOperand(denominatorOperand);
removeOperand(p, true);
Multiplication * numeratorOperand = (Multiplication *)clone();
Expression * numeratorOperand = 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()) {
if (numeratorOperand->operand(0)->type() == Type::Rational) {
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().isMinusOne() && !r->numerator().isOne()) || numeratorOperand->numberOfOperands() == 1) {
numeratorOperand->replaceOperand(r, new Rational(r->numerator()), true);
} else {
if (r->numerator().isOne()) {
numeratorOperand->removeOperand(r, true);
if (!r->denominator().isOne()) {
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 * oppOperand[1] = {numeratorOperand->clone()};
Opposite * o = new Opposite(oppOperand, false);
numeratorOperand->replaceWith(o, true);
const Expression * multOperands[2] = {new Rational(r->denominator()), denominatorOperand->clone()};
Multiplication * m = new Multiplication(multOperands, 2, false);
denominatorOperand->replaceWith(m, true);
}
}
if (!r->numerator().isMinusOne() || numeratorOperand->numberOfOperands() == 1) {
numeratorOperand->replaceOperand(r, new Rational(r->numerator()), true);
numeratorOperand = numeratorOperand->immediateSimplify(context, angleUnit);
} else {
((Multiplication *)numeratorOperand)->removeOperand(r, true);
numeratorOperand = numeratorOperand->immediateSimplify(context, angleUnit);
const Expression * oppOperand[1] = {numeratorOperand->clone()};
Opposite * o = new Opposite(oppOperand, false);
numeratorOperand = numeratorOperand->replaceWith(o, true);
}
} else {
numeratorOperand = numeratorOperand->immediateSimplify(context, angleUnit);
}
Expression * newNumeratorOperand = numeratorOperand->squashUnaryHierarchy();
// Delete parenthesis unnecessary on numerator
if (newNumeratorOperand->type() == Type::Parenthesis) {
newNumeratorOperand->replaceWith((Expression *)newNumeratorOperand->operand(0), true);
if (numeratorOperand->type() == Type::Parenthesis) {
numeratorOperand->replaceWith((Expression *)numeratorOperand->operand(0), true);
}
replaceWith(d, true);
return d->immediateBeautify(context, angleUnit);