[poincare] Fix Multiplication: setSign has to be implemented on

Multiplication instead of MultiplicationNode
This commit is contained in:
Émilie Feral
2018-08-09 14:27:50 +02:00
parent 067fbfcfc7
commit 2d93ddf4ed
2 changed files with 16 additions and 7 deletions

View File

@@ -76,6 +76,7 @@ public:
}
// Expression
Expression setSign(ExpressionNode::Sign s, Context & context, Preferences::AngleUnit angleUnit) const;
Expression shallowReduce(Context& context, Preferences::AngleUnit angleUnit) const;
Expression shallowBeautify(Context& context, Preferences::AngleUnit angleUnit) const;
private:

View File

@@ -25,6 +25,9 @@ static MultiplicationNode * FailedAllocationStaticNode() {
}
ExpressionNode::Sign MultiplicationNode::sign() const {
if (numberOfChildren() == 0) {
return Sign::Unknown;
}
int sign = 1;
for (int i = 0; i < numberOfChildren(); i++) {
sign *= (int)childAtIndex(i)->sign();
@@ -113,13 +116,7 @@ MatrixComplex<T> MultiplicationNode::computeOnMatrices(const MatrixComplex<T> m,
}
Expression MultiplicationNode::setSign(ExpressionNode::Sign s, Context & context, Preferences::AngleUnit angleUnit) const {
assert(s == Sign::Positive);
for (int i = 0; i < numberOfChildren(); i++) {
if (childAtIndex(i)->sign() == Sign::Negative) {
childAtIndex(i)->setSign(s, context, angleUnit);
}
}
return shallowReduce(context, angleUnit);
return Multiplication(this).setSign(s, context, angleUnit);
}
bool MultiplicationNode::needsParenthesesWithParent(const SerializationHelperInterface * parentNode) const {
@@ -176,6 +173,17 @@ Expression MultiplicationNode::denominator(Context & context, Preferences::Angle
// MULTIPLICATION
Expression Multiplication::setSign(ExpressionNode::Sign s, Context & context, Preferences::AngleUnit angleUnit) const {
assert(s == ExpressionNode::Sign::Positive);
Expression result = *this;
for (int i = 0; i < numberOfChildren(); i++) {
if (childAtIndex(i).sign() == ExpressionNode::Sign::Negative) {
result.replaceChildAtIndexInPlace(i, childAtIndex(i).setSign(s, context, angleUnit));
}
}
return result.shallowReduce(context, angleUnit);
}
Expression Multiplication::shallowReduce(Context& context, Preferences::AngleUnit angleUnit) const {
return privateShallowReduce(context, angleUnit, true, true);
}