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 6c2f457..51bd3a5 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 2dd6448..b2460fd 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)) {