[poincare] Use Dynamic hierarchy shortcut constructor

Change-Id: I5a10ad7fd34d6aae09b5bfc9c7b4ada8451a7e5d
This commit is contained in:
Émilie Feral
2017-11-02 14:26:59 +01:00
parent 9a74d22bd9
commit bf313fff76
8 changed files with 27 additions and 46 deletions

View File

@@ -134,8 +134,7 @@ void Addition::factorizeOperands(Expression * e1, Expression * e2, Context & con
}
e1->shallowSimplify(context, angleUnit);
} else {
const Expression * operands[2] = {r, e1};
Multiplication * m = new Multiplication(operands, 2, true);
Multiplication * m = new Multiplication(r, e1, true);
e1->replaceWith(m, true);
m->shallowSimplify(context, angleUnit);
}

View File

@@ -25,8 +25,7 @@ Expression * Division::clone() const {
Expression * Division::shallowSimplify(Context& context, AngleUnit angleUnit) {
const Expression * powOperands[2] = {operand(1), new Rational(Integer(-1))};
Power * p = new Power(powOperands, false);
const Expression * multOperands[2] = {operand(0), p};
Multiplication * m = new Multiplication(multOperands, 2, false);
Multiplication * m = new Multiplication(operand(0), p, false);
p->deepSimplify(context, angleUnit);
detachOperands();
replaceWith(m, true);

View File

@@ -52,8 +52,7 @@ Expression * Logarithm::shallowSimplify(Context& context, AngleUnit angleUnit) {
}
Expression * n = splitInteger(r->numerator(), false, context, angleUnit);
Expression * d = splitInteger(r->denominator(), true, context, angleUnit);
const Expression * addOp[2] = {n, d};
Addition * a = new Addition(addOp, 2, false);
Addition * a = new Addition(n, d, false);
replaceWith(a, true);
return a->shallowSimplify(context, angleUnit);
}
@@ -74,8 +73,7 @@ Expression * Logarithm::splitInteger(Integer i, bool isDenominator, Context & co
if (!isDenominator) {
return e;
}
const Expression * multOperands[2] = {new Rational(Integer(-1)), e};
Multiplication * m = new Multiplication(multOperands, 2, false);
Multiplication * m = new Multiplication(new Rational(Integer(-1)), e, false);
return m;
}
Integer factors[Arithmetic::k_maxNumberOfPrimeFactors];
@@ -89,8 +87,7 @@ Expression * Logarithm::splitInteger(Integer i, bool isDenominator, Context & co
}
Expression * e = clone();
e->replaceOperand(e->operand(0), new Rational(factors[index]), true);
const Expression * multOperands[2] = {new Rational(coefficients[index]), e};
Multiplication * m = new Multiplication(multOperands, 2, false);
Multiplication * m = new Multiplication(new Rational(coefficients[index]), e, false);
const Expression * addNewOperand[1] = {m};
a->addOperands(addNewOperand, 1);
m->shallowSimplify(context, angleUnit);

View File

@@ -186,13 +186,11 @@ bool Multiplication::resolveSquareRootAtDenominator(Context & context, AngleUnit
Integer factor1 = Integer::Multiplication(
Integer::Multiplication(n1, d1),
Integer::Multiplication(Integer::Power(d2, Integer(2)), q2));
const Expression * mult1Operands[2] = {new Rational(factor1), sqrt1};
Multiplication * m1 = new Multiplication(mult1Operands, 2, false);
Multiplication * m1 = new Multiplication(new Rational(factor1), sqrt1, false);
Integer factor2 = Integer::Multiplication(
Integer::Multiplication(n2, d2),
Integer::Multiplication(Integer::Power(d1, Integer(2)), q1));
const Expression * mult2Operands[2] = {new Rational(factor2), sqrt2};
Multiplication * m2 = new Multiplication(mult2Operands, 2, false);
Multiplication * m2 = new Multiplication(new Rational(factor2), sqrt2, false);
const Expression * subOperands[2] = {m1, m2};
if (denominator.isNegative()) {
denominator.setNegative(false);
@@ -238,16 +236,14 @@ void Multiplication::factorize(Context & context, AngleUnit angleUnit) {
}
void Multiplication::factorizeBase(Expression * e1, Expression * e2, Context & context, AngleUnit angleUnit) {
const Expression * addOperands[2] = {CreateExponent(e1), CreateExponent(e2)};
Expression * s = new Addition(CreateExponent(e1), CreateExponent(e2), false);
removeOperand(e2, true);
Expression * s = new Addition(addOperands, 2, false);
if (e1->type() == Type::Power) {
e1->replaceOperand(e1->operand(1), s, true);
s->shallowSimplify(context, angleUnit);
e1->shallowSimplify(context, angleUnit);
} else {
const Expression * operands[2] = {e1, s};
Power * p = new Power(operands, false);
Power * p = new Power(e1, s, false);
s->shallowSimplify(context, angleUnit);
replaceOperand(e1, p, false);
p->shallowSimplify(context, angleUnit);
@@ -255,11 +251,12 @@ void Multiplication::factorizeBase(Expression * e1, Expression * e2, Context & c
}
void Multiplication::factorizeExponent(Expression * e1, Expression * e2, Context & context, AngleUnit angleUnit) {
const Expression * multOperands[2] = {e1->operand(0)->clone(), e2->operand(0)};
const Expression * base1 = e1->operand(0)->clone();
const Expression * base2 = e2->operand(0);
// TODO: remove cast, everything is a hierarchy
static_cast<Hierarchy *>(e2)->detachOperand(e2->operand(0));
static_cast<Hierarchy *>(e2)->detachOperand(base2);
Expression * m = new Multiplication(base1, base2, false);
removeOperand(e2, true);
Expression * m = new Multiplication(multOperands, 2, false);
e1->replaceOperand(e1->operand(0), m, true);
m->shallowSimplify(context, angleUnit);
e1->shallowSimplify(context, angleUnit);
@@ -401,8 +398,7 @@ Expression * Multiplication::shallowBeautify(Context & context, AngleUnit angleU
static_cast<Multiplication *>(denominatorOperand)->addOperands(integerDenominator, 1);
static_cast<Multiplication *>(denominatorOperand)->sortOperands(SimplificationOrder);
} else {
const Expression * multOperands[2] = {new Rational(r->denominator()), denominatorOperand->clone()};
Multiplication * m = new Multiplication(multOperands, 2, false);
Multiplication * m = new Multiplication(new Rational(r->denominator()), denominatorOperand->clone(), false);
denominatorOperand->replaceWith(m, true);
}
}

View File

@@ -29,9 +29,9 @@ Complex<T> Opposite::compute(const Complex<T> c, AngleUnit angleUnit) {
}
Expression * Opposite::shallowSimplify(Context& context, AngleUnit angleUnit) {
const Expression * multOperands[2] = {new Rational(Integer(-1)), operand(0)};
detachOperand(operand(0));
Multiplication * m = new Multiplication(multOperands, 2, false);
const Expression * op = operand(0);
detachOperand(op);
Multiplication * m = new Multiplication(new Rational(Integer(-1)), op, false);
replaceWith(m, true);
return m->shallowSimplify(context, angleUnit);
}

View File

@@ -197,8 +197,7 @@ Expression * Power::shallowSimplify(Context& context, AngleUnit angleUnit) {
m->shallowSimplify(context, angleUnit);
const Expression * powOperands[2] = {factor, rCopy};
Power * p = new Power(powOperands, false);
const Expression * multOperands[2] = {p, clone()};
Multiplication * root = new Multiplication(multOperands, 2, false);
Multiplication * root = new Multiplication(p, clone(), false);
p->shallowSimplify(context, angleUnit);
root->editableOperand(1)->shallowSimplify(context, angleUnit);
replaceWith(root, true);
@@ -214,8 +213,7 @@ Expression * Power::shallowSimplify(Context& context, AngleUnit angleUnit) {
Power * p1 = static_cast<Power *>(clone());
replaceOperand(a, a->editableOperand(1), true);
Power * p2 = static_cast<Power *>(clone());
const Expression * multOperands[2] = {p1, p2};
Multiplication * m = new Multiplication(multOperands, 2, false);
Multiplication * m = new Multiplication(p1, p2, false);
simplifyRationalRationalPower(p1, static_cast<Rational *>(p1->editableOperand(0)), static_cast<Rational *>(p1->editableOperand(1)->editableOperand(0)), context, angleUnit);
replaceWith(m, true);
return m->shallowSimplify(context, angleUnit);
@@ -228,8 +226,7 @@ Expression * Power::simplifyPowerPower(Power * p, Expression * e, Context& conte
Expression * p0 = p->editableOperand(0);
Expression * p1 = p->editableOperand(1);
p->detachOperands();
const Expression * multOperands[2] = {p1, e};
Multiplication * m = new Multiplication(multOperands, 2, false);
Multiplication * m = new Multiplication(p1, e, false);
replaceOperand(e, m, false);
replaceOperand(p, p0, true);
m->shallowSimplify(context, angleUnit);
@@ -263,8 +260,7 @@ Expression * Power::simplifyRationalRationalPower(Expression * result, Rational
n = CreateSimplifiedIntegerRationalPower(a->numerator(), b, false);
d = CreateSimplifiedIntegerRationalPower(a->denominator(), b, true);
}
const Expression * multOp[2] = {n, d};
Multiplication * m = new Multiplication(multOp, 2, false);
Multiplication * m = new Multiplication(n, d, false);
result->replaceWith(m, true);
return m->shallowSimplify(context, angleUnit);
}
@@ -304,8 +300,7 @@ Expression * Power::CreateSimplifiedIntegerRationalPower(Integer i, Rational * r
return p;
}
Rational * r3 = isDenominator ? new Rational(Integer(1), r1) : new Rational(r1);
const Expression * multOp[2] = {r3, p};
Multiplication * m = new Multiplication(multOp, 2, false);
Multiplication * m = new Multiplication(r3, p, false);
if (r2.isOne()) {
m->removeOperand(p);
}

View File

@@ -29,10 +29,8 @@ Complex<T> Subtraction::compute(const Complex<T> c, const Complex<T> d) {
}
Expression * Subtraction::shallowSimplify(Context& context, AngleUnit angleUnit) {
const Expression * multOperands[2] = {new Rational(Integer(-1)), operand(1)};
Multiplication * m = new Multiplication(multOperands, 2, false);
const Expression * addOperands[2] = {operand(0), m};
Addition * a = new Addition(addOperands, 2, false);
Multiplication * m = new Multiplication(new Rational(Integer(-1)), operand(1), false);
Addition * a = new Addition(operand(0), m, false);
m->shallowSimplify(context, angleUnit);
detachOperands();
replaceWith(a, true);

View File

@@ -35,8 +35,7 @@ Expression * Trigonometry::shallowSimplifyDirectFunction(Expression * e, Context
if (e->type() == Expression::Type::Cosine) {
return e->shallowSimplify(context, angleUnit);
} else {
const Expression * multOperands[2] = {new Rational(Integer(-1)), e->clone()};
Multiplication * m = new Multiplication(multOperands, 2, false);
Multiplication * m = new Multiplication(new Rational(Integer(-1)), e->clone(), false);
m->editableOperand(1)->shallowSimplify(context, angleUnit);
return e->replaceWith(m, true)->shallowSimplify(context, angleUnit);
}
@@ -65,8 +64,7 @@ Expression * Trigonometry::shallowSimplifyDirectFunction(Expression * e, Context
unaryCoefficient *= -1;
}
Expression * simplifiedCosine = e->shallowSimplify(context, angleUnit); // recursive
const Expression * multOperands[2] = {new Rational(Integer(unaryCoefficient)), simplifiedCosine->clone()};
Multiplication * m = new Multiplication(multOperands, 2, false);
Multiplication * m = new Multiplication(new Rational(Integer(unaryCoefficient)), simplifiedCosine->clone(), false);
return simplifiedCosine->replaceWith(m, true)->shallowSimplify(context, angleUnit);
}
assert(r->sign() == Expression::Sign::Positive);
@@ -129,8 +127,7 @@ Expression * Trigonometry::shallowSimplifyInverseFunction(Expression * e, Contex
s->editableOperand(1)->shallowSimplify(context, angleUnit);
return e->replaceWith(s, true)->shallowSimplify(context, angleUnit);
} else {
const Expression * multOperands[2] = {new Rational(Integer(-1)), e->clone()};
Multiplication * m = new Multiplication(multOperands, 2, false);
Multiplication * m = new Multiplication(new Rational(Integer(-1)), e->clone(), false);
m->editableOperand(1)->shallowSimplify(context, angleUnit);
return e->replaceWith(m, true)->shallowSimplify(context, angleUnit);
}