[poincare/derivative] Reduce before replacing symbol

The general formula for deriving a power makes use of the logarithm,
which often disappears at simplification. However, replacing the symbol
before simplifying can lead to applying an invalid argument to the
logarithm, making the whole expression invalid.

e.g. diff(1/x,x,-2)
     If x is replaced by -2 before reducing the power derivative, ln(-2)
will reduce to Unreal, as will the rest of the expression.
This commit is contained in:
Gabriel Ozouf
2020-12-03 15:33:59 +01:00
committed by EmilieNumworks
parent 1a47207bee
commit 3f6e4444a5
2 changed files with 8 additions and 2 deletions

View File

@@ -176,8 +176,12 @@ Expression Derivative::shallowReduce(ExpressionNode::ReductionContext reductionC
return *this;
}
/* Updates the value of derivand, because derivate may call
* replaceWithInplace on it */
derivand = childAtIndex(0);
* replaceWithInplace on it.
* We need to reduce the derivand here before replacing the symbol : the
* general formulas used during the derivation process can create some nodes
* that are not defined for some values (e.g. log), but that would disappear
* at reduction. */
derivand = childAtIndex(0).deepReduce(reductionContext);
/* Deep reduces the child, because derivate may not preserve its reduced
* status. */

View File

@@ -64,4 +64,6 @@ QUIZ_CASE(poincare_derivative_approximation) {
assert_reduces_for_approximation("diff(abs(x),x,123)", "1");
assert_reduces_for_approximation("diff(abs(x),x,-2.34)", "-1");
assert_reduces_for_approximation("diff(abs(x),x,0)", Undefined::Name());
assert_reduces_for_approximation("diff(1/x,x,-2)", "-1/4");
}