]> www.ginac.de Git - ginac.git/blobdiff - ginac/add.cpp
* add::coeff(): don't epvector::reserve() more terms than we'll actually
[ginac.git] / ginac / add.cpp
index 1b7b2719d3a57c63f104e9f684f62d1983c60f09..412bd5a548e65cffab65f1fd3379c7aed137bc03 100644 (file)
@@ -284,18 +284,16 @@ int add::ldegree(const ex & s) const
 ex add::coeff(const ex & s, int n) const
 {
        epvector coeffseq;
-       coeffseq.reserve(seq.size());
-
+       
        epvector::const_iterator it=seq.begin();
        while (it!=seq.end()) {
-               coeffseq.push_back(combine_ex_with_coeff_to_pair((*it).rest.coeff(s,n),
-                                                                (*it).coeff));
+               ex restcoeff = it->rest.coeff(s,n);
+               if (!restcoeff.is_zero())
+                       coeffseq.push_back(combine_ex_with_coeff_to_pair(restcoeff,it->coeff));
                ++it;
        }
-       if (n==0) {
-               return (new add(coeffseq,overall_coeff))->setflag(status_flags::dynallocated);
-       }
-       return (new add(coeffseq))->setflag(status_flags::dynallocated);
+       
+       return (new add(coeffseq, n==0 ? overall_coeff : default_overall_coeff()))->setflag(status_flags::dynallocated);
 }
 
 ex add::eval(int level) const