]> www.ginac.de Git - ginac.git/commitdiff
Fix pow(+(...),2).expand().
authorRichard Kreckel <kreckel@ginac.de>
Thu, 7 May 2015 20:33:13 +0000 (22:33 +0200)
committerRichard Kreckel <kreckel@ginac.de>
Thu, 7 May 2015 20:48:19 +0000 (22:48 +0200)
Due to a failure to recombine coeffs and rests to expairs,
expand((x+sqrt(2)*x)^2) returned x^2+2*x^2+2*sqrt(2)*x^2. The
2*x^2 term was not combined with the x^2 term to 3*x^2 because it
was not the canonical expair [[x^2,2]] but rather [[2*x^2,1]].

Thanks to Isuru Fernando for the bugreport.

check/exam_paranoia.cpp
ginac/power.cpp

index 6c2f4571200409aacd8a37eed3fbbb410d079ef5..51bd3a55ee1baaa61d54a0f166f876d47f61dab6 100644 (file)
@@ -544,6 +544,18 @@ static unsigned is_polynomial_false_positive()
        return result;
 }
 
+// Bug in power::expand reported by Isuru Fernando (fixed 2015-05-07).
+static unsigned exam_paranoia21()
+{
+       symbol x("x");
+       ex e = pow(x + sqrt(ex(2))*x, 2).expand();
+       if (e.nops() != 2) {
+               clog << "(x+sqrt(2)*x)^2 was wrongly expanded to " << e << "\n";
+               return 1;
+       }
+       return 0;
+}
+
 unsigned exam_paranoia()
 {
        unsigned result = 0;
@@ -571,6 +583,7 @@ unsigned exam_paranoia()
        result += exam_paranoia19();  cout << '.' << flush;
        result += exam_paranoia20();  cout << '.' << flush;
        result += is_polynomial_false_positive(); cout << '.' << flush;
+       result += exam_paranoia21();  cout << '.' << flush;
        
        return result;
 }
index 2dd644864c290d29a9d3cc7e9a87dd16ff04178a..b2460fda8661d234dc8c4b6fb30890c5d1920465 100644 (file)
@@ -1032,11 +1032,11 @@ ex power::expand_add_2(const add & a, unsigned options) const
                
                if (c.is_equal(_ex1)) {
                        if (is_exactly_a<mul>(r)) {
-                               sum.push_back(expair(expand_mul(ex_to<mul>(r), *_num2_p, options, true),
-                                                    _ex1));
+                               sum.push_back(a.combine_ex_with_coeff_to_pair(expand_mul(ex_to<mul>(r), *_num2_p, options, true),
+                                                                             _ex1));
                        } else {
-                               sum.push_back(expair((new power(r,_ex2))->setflag(status_flags::dynallocated),
-                                                    _ex1));
+                               sum.push_back(a.combine_ex_with_coeff_to_pair((new power(r,_ex2))->setflag(status_flags::dynallocated),
+                                                                             _ex1));
                        }
                } else {
                        if (is_exactly_a<mul>(r)) {