From b773294dd380ae81056c3d37b647500aae738ea1 Mon Sep 17 00:00:00 2001 From: "Vladimir V. Kisil" Date: Mon, 27 Jul 2020 21:03:42 +0200 Subject: [PATCH] Fix bug in power::expand() with the overall coefficient. The negative sign of overall coefficient had been lost if a power base contained some sign-definite factors. --- check/exam_paranoia.cpp | 23 +++++++++++++++++++++++ ginac/power.cpp | 5 +++-- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/check/exam_paranoia.cpp b/check/exam_paranoia.cpp index 52466b05..7a64e52c 100644 --- a/check/exam_paranoia.cpp +++ b/check/exam_paranoia.cpp @@ -651,6 +651,28 @@ unsigned exam_paranoia26() return 0; } +// Bug in power expansion +unsigned exam_paranoia27() +{ + unsigned result = 0; + symbol x("x"), y("y"), a("a"); + possymbol s("s"), t("t"); + exmap pwrs = + { {pow((x+1)*(y-2)*(s-3)*(t+4), a), pow((x+1)*(y-2)*(s-3), a)*pow(t+4, a)}, + {pow(2*(x+1)*(y-2)*(s-3)*(t+4), a), pow(2,a)*pow((x+1)*(y-2)*(s-3), a)*pow(t+4, a)}, + {pow(-(x+1)*(y-2)*(s-3)*(t+4), a), pow(-(x+1)*(y-2)*(s-3), a)*pow(t+4, a)}, + {pow(-2*(x+1)*(y-2)*(s-3)*(t+4), a), pow(2,a)*pow(-(x+1)*(y-2)*(s-3), a)*pow(t+4, a)} }; + + for (auto e : pwrs) { + if (! (e.first.expand()).is_equal(e.second) ) { + clog << "power expansion of " << e.first << " produces error.\n"; + ++result; + } + } + + return result; +} + unsigned exam_paranoia() { unsigned result = 0; @@ -684,6 +706,7 @@ unsigned exam_paranoia() result += exam_paranoia24(); cout << '.' << flush; result += exam_paranoia25(); cout << '.' << flush; result += exam_paranoia26(); cout << '.' << flush; + result += exam_paranoia27(); cout << '.' << flush; return result; } diff --git a/ginac/power.cpp b/ginac/power.cpp index 19b31034..ba7a66f4 100644 --- a/ginac/power.cpp +++ b/ginac/power.cpp @@ -809,9 +809,10 @@ ex power::expand(unsigned options) const ex coeff=(possign? _ex1 : _ex_1); if (m.overall_coeff.info(info_flags::positive) && m.overall_coeff != _ex1) prodseq.push_back(pow(m.overall_coeff, exponent)); - else if (m.overall_coeff.info(info_flags::negative) && m.overall_coeff != _ex_1) + else if (m.overall_coeff.info(info_flags::negative) && m.overall_coeff != _ex_1) { prodseq.push_back(pow(-m.overall_coeff, exponent)); - else + coeff = -coeff; + } else coeff *= m.overall_coeff; // If positive/negative factors are found, then extract them. -- 2.44.0