From: Richard Kreckel Date: Wed, 15 Sep 2010 07:11:57 +0000 (+0200) Subject: Be more careful about final top-level substitution. X-Git-Tag: release_1-6-0~18^2~1^2~8 X-Git-Url: https://www.ginac.de/ginac.git//ginac.git?p=ginac.git;a=commitdiff_plain;h=90ad10b58d02365a407b2d84d8b93e50030feaa5 Be more careful about final top-level substitution. Substituting x==log(x) in exp(x) erroneously returned log(x) because of a final subst(x==log(x)) after having eval'ed exp(log(x)) -> x. This final substitution is wrong in the general case. On the other hand, the intent is to syntactically substitute functions of a given kind etc. This patch suppresses the final top-level substitution unless the intermediate result is a container. Thanks to Burcin Erocal for reporting this bug (originally described by Kees van Schaik on sage-support@googlegroops.com). --- diff --git a/check/exam_misc.cpp b/check/exam_misc.cpp index 9dbfd6bc..fc9c41eb 100644 --- a/check/exam_misc.cpp +++ b/check/exam_misc.cpp @@ -219,6 +219,15 @@ static unsigned exam_subs() ++result; } + // And this used to fail in GiNaC 1.5.8 because it first substituted + // exp(x) -> exp(log(x)) -> x, and then substitued again x -> log(x) + e1 = exp(x); + e2 = e1.subs(x == log(x)); + if (!e2.is_equal(x)) { + clog << "exp(x).subs(x==log(x)) erroneously returned " << e2 << " instead of x" << endl; + ++result; + } + e1 = sin(1+sin(x)); e2 = e1.subs(sin(wild()) == cos(wild())); if (!e2.is_equal(cos(1+cos(x)))) { diff --git a/doc/tutorial/ginac.texi b/doc/tutorial/ginac.texi index 571144a1..e8f0395d 100644 --- a/doc/tutorial/ginac.texi +++ b/doc/tutorial/ginac.texi @@ -4263,7 +4263,7 @@ In the first form, @code{subs()} accepts a relational of the form @{ symbol x("x"), y("y"); - ex e1 = 2*x^2-4*x+3; + ex e1 = 2*x*x-4*x+3; cout << "e1(7) = " << e1.subs(x == 7) << endl; // -> 73 diff --git a/ginac/container.h b/ginac/container.h index 632145e5..456cf5c4 100644 --- a/ginac/container.h +++ b/ginac/container.h @@ -574,11 +574,27 @@ ex container::eval(int level) const template