[poincare] When beautifying, add missing user parentheses once both

parent and child have been beautified (otherwise, the child might change
and in a new expression that requires parentheses)
This commit is contained in:
Émilie Feral
2019-07-29 11:04:47 +02:00
parent d985872951
commit 4dd445a60b
5 changed files with 7 additions and 33 deletions

View File

@@ -34,7 +34,6 @@ private:
int serialize(char * buffer, int bufferSize, Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const override;
// Simplication
Expression shallowReduce(ReductionContext reductionContext) override;
Expression shallowBeautify(ReductionContext reductionContext) override;
// Evaluation
template<typename T> static Complex<T> computeOnComplex(const std::complex<T> c, Preferences::ComplexFormat complexFormat, Preferences::AngleUnit angleUnit);
Evaluation<float> approximate(SinglePrecision p, Context * context, Preferences::ComplexFormat complexFormat, Preferences::AngleUnit angleUnit) const override {
@@ -56,7 +55,6 @@ public:
static Factorial Builder(Expression child) { return TreeHandle::FixedArityBuilder<Factorial, FactorialNode>(&child, 1); }
Expression shallowReduce(ExpressionNode::ReductionContext reductionContext);
Expression shallowBeautify();
private:
constexpr static int k_maxOperandValue = 100;
};

View File

@@ -609,7 +609,12 @@ Expression Expression::deepBeautify(ExpressionNode::ReductionContext reductionCo
Expression e = shallowBeautify(reductionContext);
int nbChildren = e.numberOfChildren();
for (int i = 0; i < nbChildren; i++) {
e.childAtIndex(i).deepBeautify(reductionContext);
Expression child = e.childAtIndex(i);
child.deepBeautify(reductionContext);
// We add missing Parentheses after beautifying the parent and child
if (e.node()->childNeedsUserParentheses(child)) {
e.replaceChildAtIndexInPlace(i, Parenthesis::Builder(child));
}
}
return e;
}

View File

@@ -48,10 +48,6 @@ Expression FactorialNode::shallowReduce(ReductionContext reductionContext) {
return Factorial(this).shallowReduce(reductionContext);
}
Expression FactorialNode::shallowBeautify(ReductionContext reductionContext) {
return Factorial(this).shallowBeautify();
}
template<typename T>
Complex<T> FactorialNode::computeOnComplex(const std::complex<T> c, Preferences::ComplexFormat, Preferences::AngleUnit angleUnit) {
T n = c.real();
@@ -118,16 +114,6 @@ Expression Factorial::shallowReduce(ExpressionNode::ReductionContext reductionCo
return *this;
}
Expression Factorial::shallowBeautify() {
// +(a,b)! ->(+(a,b))!
if (node()->childNeedsUserParentheses(childAtIndex(0))) {
Expression result = Factorial::Builder(Parenthesis::Builder(childAtIndex(0)));
replaceWithInPlace(result);
return result;
}
return *this;
}
#if 0
int Factorial::simplificationOrderGreaterType(const Expression * e) const {
if (SimplificationOrder(childAtIndex(0),e) == 0) {

View File

@@ -90,16 +90,7 @@ Expression MultiplicationExplicite::shallowBeautify(ExpressionNode::ReductionCon
}
assert(thisExp.type() == ExpressionNode::Type::MultiplicationExplicite);
// Step 3: Add Parenthesis if needed
for (int i = 0; i < thisExp.numberOfChildren(); i++) {
const Expression o = thisExp.childAtIndex(i);
if (o.type() == ExpressionNode::Type::Addition) {
Parenthesis p = Parenthesis::Builder(o);
thisExp.replaceChildAtIndexInPlace(i, p);
}
}
// Step 4: Create a Division if needed
// Step 3: Create a Division if needed
for (int i = 0; i < numberOfChildren(); i++) {
Expression childI = thisExp.childAtIndex(i);
if (!(childI.type() == ExpressionNode::Type::Power && childI.childAtIndex(1).type() == ExpressionNode::Type::Rational && childI.childAtIndex(1).convert<Rational>().isMinusOne())) {

View File

@@ -865,12 +865,6 @@ Expression Power::shallowBeautify(ExpressionNode::ReductionContext reductionCont
return result;
}
// Step 4: +(a,b)^c ->(+(a,b))^c and *(a,b)^c ->(*(a,b))^c
if (node()->childNeedsUserParentheses(childAtIndex(0)))
{
Parenthesis p = Parenthesis::Builder(childAtIndex(0));
replaceChildAtIndexInPlace(0, p);
}
return *this;
}