From aa1ddc484c2a22a1ab2b38047cbd221f9b37750a Mon Sep 17 00:00:00 2001 From: Richard Kreckel Date: Sat, 18 Jul 2015 23:56:55 +0200 Subject: [PATCH] Fix pow(+(...),2).expand(). Due to a failure to expand result terms, expand((sqrt(1+x)+y*sqrt(1+x))^2) returned 1+y^2+x+x*y^2+2*y*(1+x). Note that 2*y*(1+x) was not expanded to 2*y+2*y*x. --- check/exam_paranoia.cpp | 13 +++++++++++++ ginac/power.cpp | 4 ++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/check/exam_paranoia.cpp b/check/exam_paranoia.cpp index 51bd3a55..711c64a6 100644 --- a/check/exam_paranoia.cpp +++ b/check/exam_paranoia.cpp @@ -556,6 +556,18 @@ static unsigned exam_paranoia21() return 0; } +// Bug in power::expand (fixed 2015-07-18). +static unsigned exam_paranoia22() +{ + symbol x("x"), y("y"); + ex e = pow(sqrt(1+x)+y*sqrt(1+x), 2).expand(); + if (e.nops() != 6) { + clog << "(sqrt(1+x)+y*sqrt(1+x))^2 was wrongly expanded to " << e << "\n"; + return 1; + } + return 0; +} + unsigned exam_paranoia() { unsigned result = 0; @@ -584,6 +596,7 @@ unsigned exam_paranoia() result += exam_paranoia20(); cout << '.' << flush; result += is_polynomial_false_positive(); cout << '.' << flush; result += exam_paranoia21(); cout << '.' << flush; + result += exam_paranoia22(); cout << '.' << flush; return result; } diff --git a/ginac/power.cpp b/ginac/power.cpp index b2460fda..6fa619ca 100644 --- a/ginac/power.cpp +++ b/ginac/power.cpp @@ -1051,14 +1051,14 @@ ex power::expand_add_2(const add & a, unsigned options) const for (epvector::const_iterator cit1=cit0+1; cit1!=last; ++cit1) { const ex & r1 = cit1->rest; const ex & c1 = cit1->coeff; - sum.push_back(a.combine_ex_with_coeff_to_pair((new mul(r,r1))->setflag(status_flags::dynallocated), + sum.push_back(a.combine_ex_with_coeff_to_pair(mul(r,r1).expand(options), _num2_p->mul(ex_to(c)).mul_dyn(ex_to(c1)))); } } GINAC_ASSERT(sum.size()==(a.seq.size()*(a.seq.size()+1))/2); - // second part: add terms coming from overall_factor (if != 0) + // second part: add terms coming from overall_coeff (if != 0) if (!a.overall_coeff.is_zero()) { epvector::const_iterator i = a.seq.begin(), end = a.seq.end(); while (i != end) { -- 2.44.0