[poincare] Multiplication: do not apply rule 0*x --> 0 if x involves an

infinite expression
This commit is contained in:
Émilie Feral
2019-01-31 11:23:19 +01:00
committed by LeaNumworks
parent 66fe9baa7d
commit b58573bad3
2 changed files with 16 additions and 3 deletions

View File

@@ -434,13 +434,24 @@ Expression Multiplication::privateShallowReduce(Context & context, Preferences::
/* Step 5: If the first child is zero, the multiplication result is zero. We
* do this after merging the rational children, because the merge takes care
* of turning 0*inf into undef.
* of turning 0*inf into undef. We still have to check that no other child
* involves an inifity expression to avoid reducing 0*e^(inf) to 0.
* If the first child is 1, we remove it if there are other children. */
{
const Expression c = childAtIndex(0);
if (c.type() == ExpressionNode::Type::Rational && static_cast<const Rational &>(c).isZero()) {
replaceWithInPlace(c);
return c;
// Check that other children don't match inf
bool infiniteFactor = false;
for (int i = 1; i < numberOfChildren(); i++) {
infiniteFactor = childAtIndex(i).recursivelyMatches([](const Expression e, Context & context, bool replaceSymbols) { return e.type() == ExpressionNode::Type::Infinity; }, context, true);
if (infiniteFactor) {
break;
}
}
if (!infiniteFactor) {
replaceWithInPlace(c);
return c;
}
}
if (c.type() == ExpressionNode::Type::Rational && static_cast<const Rational &>(c).isOne() && numberOfChildren() > 1) {
removeChildAtIndexInPlace(0);

View File

@@ -34,5 +34,7 @@ QUIZ_CASE(poincare_infinity) {
assert_parsed_expression_simplify_to("(-inf)^R(2)", "inf*(-1)^R(2)");
assert_parsed_expression_simplify_to("inf^x", "inf^x");
assert_parsed_expression_simplify_to("1/inf+24", "24");
assert_parsed_expression_simplify_to("X^(inf)/inf", "0*X^inf");
assert_parsed_expression_simplify_to("ln(inf)*0", "0*ln(inf)");
}