[poincare] Change 1+-6*cos(2) -> 1-6cos(2)

Change-Id: Ic5dde9bf2f8a76d17a34544be0d17beaf94f6905
This commit is contained in:
Émilie Feral
2017-10-27 13:07:26 +02:00
parent 91d50bd4e1
commit c5a5a48836
3 changed files with 21 additions and 11 deletions

View File

@@ -135,9 +135,13 @@ Expression * Addition::immediateBeautify(Context & context, AngleUnit angleUnit)
int index = 0;
while (index < numberOfOperands()) {
// a+(-1)*b+... -> a-b+...
if (operand(index)->type() == Type::Multiplication && operand(index)->operand(0)->type() == Type::Rational && static_cast<const Rational *>(operand(index)->operand(0))->isMinusOne()) {
if (operand(index)->type() == Type::Multiplication && operand(index)->operand(0)->type() == Type::Rational && operand(index)->operand(0)->sign() < 0) {
Multiplication * m = static_cast<Multiplication *>((Expression *)operand(index));
m->removeOperand(m->operand(0), true);
if (static_cast<const Rational *>(operand(index)->operand(0))->isMinusOne()) {
m->removeOperand(m->operand(0), true);
} else {
const_cast<Expression *>(operand(index)->operand(0))->turnIntoPositive(context, angleUnit);
}
const Expression * subtractant = m->squashUnaryHierarchy();
if (index == 0) {
const Expression * opOperand[1] = {subtractant};

View File

@@ -348,6 +348,20 @@ bool Multiplication::isUselessOperand(const Rational * r) {
}
Expression * Multiplication::immediateBeautify(Context & context, AngleUnit angleUnit) {
// -1*A -> -A or (-n)*A -> -n*A
if (operand(0)->type() == Type::Rational && operand(0)->sign() < 0) {
if (static_cast<const Rational *>(operand(0))->isMinusOne()) {
removeOperand((Expression *)operand(0), true);
} else {
const_cast<Expression *>(operand(0))->turnIntoPositive(context, angleUnit);
}
Expression * e = squashUnaryHierarchy();
const Expression * oppOperand[1] = {e->clone()};
Opposite * o = new Opposite(oppOperand, false);
e->replaceWith(o, true);
const_cast<Expression *>(o->operand(0))->immediateBeautify(context, angleUnit);
return o;
}
// Merge negative power: a*b^-1*c^(-Pi)*d = a*(b*c^Pi)^-1
Expression * e = mergeNegativePower(context, angleUnit);
if (e->type() == Type::Power) {
@@ -409,15 +423,6 @@ Expression * Multiplication::immediateBeautify(Context & context, AngleUnit angl
return d->immediateBeautify(context, angleUnit);
}
}
// -1*A -> -A
if (operand(0)->type() == Type::Rational && static_cast<const Rational *>(operand(0))->isMinusOne()) {
removeOperand((Expression *)operand(0), true);
Expression * e = squashUnaryHierarchy();
const Expression * oppOperand[1] = {e->clone()};
Opposite * o = new Opposite(oppOperand, false);
e->replaceWith(o, true);
return o;
}
return this;
}

View File

@@ -35,6 +35,7 @@ void assert_parsed_expression_simplify_to(const char * expression, const char *
QUIZ_CASE(poincare_simplify_easy) {
//assert_parsed_expression_simplify_to("(((R(6)-R(2))/4)/((R(6)+R(2))/4))+1", "((1/2)*R(6))/((R(6)+R(2))/4)");
assert_parsed_expression_simplify_to("2+13cos(2)-23cos(2)", "2-10cos(2)");
assert_parsed_expression_simplify_to("1/(R(2)+R(3))", "-(R(2)-R(3))");
assert_parsed_expression_simplify_to("1/(5+R(3))", "(5-R(3))/22");
assert_parsed_expression_simplify_to("1/(R(2)+4)", "(4-R(2))/14");