]> www.ginac.de Git - ginac.git/blobdiff - ginac/add.cpp
[bugfix] Make sure add::eval() collects all numeric terms *correctly*.
[ginac.git] / ginac / add.cpp
index 41f2096e646f909f672581b0ea4b4f2d194b42e3..1a19d86d863b79cdf7fd8977ed37ff04cbefbce0 100644 (file)
@@ -3,7 +3,7 @@
  *  Implementation of GiNaC's sums of expressions. */
 
 /*
- *  GiNaC Copyright (C) 1999-2011 Johannes Gutenberg University Mainz, Germany
+ *  GiNaC Copyright (C) 1999-2015 Johannes Gutenberg University Mainz, Germany
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
@@ -316,14 +316,14 @@ ex add::coeff(const ex & s, int n) const
        epvector::const_iterator i = seq.begin(), end = seq.end();
        while (i != end) {
                ex restcoeff = i->rest.coeff(s, n);
-               if (!restcoeff.is_zero()) {
-                       if (do_clifford) {
-                               if (clifford_max_label(restcoeff) == -1) {
-                                       coeffseq_cliff->push_back(combine_ex_with_coeff_to_pair(ncmul(restcoeff, dirac_ONE(rl)), i->coeff));
+               if (!restcoeff.is_zero()) {
+                       if (do_clifford) {
+                               if (clifford_max_label(restcoeff) == -1) {
+                                       coeffseq_cliff->push_back(combine_ex_with_coeff_to_pair(ncmul(restcoeff, dirac_ONE(rl)), i->coeff));
                                } else {
-                                       coeffseq_cliff->push_back(combine_ex_with_coeff_to_pair(restcoeff, i->coeff));
+                                       coeffseq_cliff->push_back(combine_ex_with_coeff_to_pair(restcoeff, i->coeff));
                                        nonscalar = true;
-                               }
+                               }
                        }
                        coeffseq->push_back(combine_ex_with_coeff_to_pair(restcoeff, i->coeff));
                }
@@ -344,7 +344,7 @@ ex add::coeff(const ex & s, int n) const
 ex add::eval(int level) const
 {
        std::auto_ptr<epvector> evaled_seqp = evalchildren(level);
-       if (evaled_seqp.get()) {
+       if (unlikely(evaled_seqp.get() != 0)) {
                // do more evaluation later
                return (new add(evaled_seqp, overall_coeff))->
                       setflag(status_flags::dynallocated);
@@ -388,11 +388,11 @@ ex add::eval(int level) const
        if (terms_to_collect) {
                std::auto_ptr<epvector> s(new epvector);
                s->reserve(seq_size - terms_to_collect);
-               numeric oc = *_num1_p;
+               numeric oc = *_num0_p;
                j = seq.begin();
                while (j != last) {
                        if (unlikely(is_a<numeric>(j->rest)))
-                               oc = oc.mul(ex_to<numeric>(j->rest)).mul(ex_to<numeric>(j->coeff));
+                               oc = oc.add(ex_to<numeric>(j->rest).mul(ex_to<numeric>(j->coeff)));
                        else
                                s->push_back(*j);
                        ++j;
@@ -564,6 +564,8 @@ expair add::split_ex_to_pair(const ex & e) const
        if (is_exactly_a<mul>(e)) {
                const mul &mulref(ex_to<mul>(e));
                const ex &numfactor = mulref.overall_coeff;
+               if (numfactor.is_equal(_ex1))
+                       return expair(e, _ex1);
                mul *mulcopyp = new mul(mulref);
                mulcopyp->overall_coeff = _ex1;
                mulcopyp->clearflag(status_flags::evaluated);
@@ -575,12 +577,14 @@ expair add::split_ex_to_pair(const ex & e) const
 }
 
 expair add::combine_ex_with_coeff_to_pair(const ex & e,
-                                                                                 const ex & c) const
+                                          const ex & c) const
 {
        GINAC_ASSERT(is_exactly_a<numeric>(c));
        if (is_exactly_a<mul>(e)) {
                const mul &mulref(ex_to<mul>(e));
                const ex &numfactor = mulref.overall_coeff;
+               if (numfactor.is_equal(_ex1))
+                       return expair(e, c);
                mul *mulcopyp = new mul(mulref);
                mulcopyp->overall_coeff = _ex1;
                mulcopyp->clearflag(status_flags::evaluated);
@@ -588,20 +592,20 @@ expair add::combine_ex_with_coeff_to_pair(const ex & e,
                mulcopyp->setflag(status_flags::dynallocated);
                if (c.is_equal(_ex1))
                        return expair(*mulcopyp, numfactor);
-               else if (numfactor.is_equal(_ex1))
-                       return expair(*mulcopyp, c);
                else
                        return expair(*mulcopyp, ex_to<numeric>(numfactor).mul_dyn(ex_to<numeric>(c)));
        } else if (is_exactly_a<numeric>(e)) {
                if (c.is_equal(_ex1))
                        return expair(e, _ex1);
+               if (e.is_equal(_ex1))
+                       return expair(c, _ex1);
                return expair(ex_to<numeric>(e).mul_dyn(ex_to<numeric>(c)), _ex1);
        }
        return expair(e, c);
 }
 
 expair add::combine_pair_with_coeff_to_pair(const expair & p,
-                                                                                       const ex & c) const
+                                            const ex & c) const
 {
        GINAC_ASSERT(is_exactly_a<numeric>(p.coeff));
        GINAC_ASSERT(is_exactly_a<numeric>(c));