Happy New Year!
[ginac.git] / ginac / polynomial / collect_vargs.cpp
index 89c3b8b..6beda76 100644 (file)
@@ -3,7 +3,7 @@
  *  Utility functions. */
 
 /*
- *  GiNaC Copyright (C) 1999-2010 Johannes Gutenberg University Mainz, Germany
+ *  GiNaC Copyright (C) 1999-2019 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
@@ -69,7 +69,7 @@ struct compare_terms
 };
 
 template<typename T, typename CoeffCMP>
-static struct compare_terms<T, CoeffCMP>
+static compare_terms<T, CoeffCMP>
 make_compare_terms(const T& dummy, const CoeffCMP& coeff_cmp)
 {
        return compare_terms<T, CoeffCMP>(coeff_cmp);
@@ -118,7 +118,7 @@ collect_term(ex_collect_priv_t& ec, const ex& e, const exvector& vars)
                key[i] = var_i_pow;
                pre_coeff = pre_coeff.coeff(vars[i], var_i_pow);
        }
-       ex_collect_priv_t::iterator i = ec.find(key);
+       auto i = ec.find(key);
        if (i != ec.end())
                i->second += pre_coeff;
        else
@@ -127,9 +127,9 @@ collect_term(ex_collect_priv_t& ec, const ex& e, const exvector& vars)
 
 static void wipe_out_zeros(ex_collect_priv_t& m)
 {
-       ex_collect_priv_t::iterator i = m.begin();
+       auto i = m.begin();
        while (i != m.end()) {
-               // be careful to not invalide iterator, use post-increment
+               // be careful to not invalidate the iterator, use post-increment
                // for that, see e.g.
                // http://coding.derkeiler.com/Archive/C_CPP/comp.lang.cpp/2004-02/0502.html
                if (i->second.is_zero())
@@ -139,8 +139,8 @@ static void wipe_out_zeros(ex_collect_priv_t& m)
        }
 }
 
-GiNaC::ex
-ex_collect_to_ex(const ex_collect_t& ec, const GiNaC::exvector& vars)
+ex
+ex_collect_to_ex(const ex_collect_t& ec, const exvector& vars)
 {
        exvector ev;
        ev.reserve(ec.size());
@@ -148,14 +148,20 @@ ex_collect_to_ex(const ex_collect_t& ec, const GiNaC::exvector& vars)
                exvector tv;
                tv.reserve(vars.size() + 1);
                for (std::size_t j = 0; j < vars.size(); ++j) {
-                       if (ec[i].first[j] != 0)
-                               tv.push_back(power(vars[j], ec[i].first[j]));
+                       const exp_vector_t& exp_vector(ec[i].first);
+
+                       bug_on(exp_vector.size() != vars.size(),
+                               "expected " << vars.size() << " variables, "
+                               "expression has " << exp_vector.size() << " instead");
+
+                       if (exp_vector[j] != 0)
+                               tv.push_back(pow(vars[j], exp_vector[j]));
                }
                tv.push_back(ec[i].second);
-               ex tmp = (new mul(tv))->setflag(status_flags::dynallocated);
+               ex tmp = dynallocate<mul>(tv);
                ev.push_back(tmp);
        }
-       ex ret = (new add(ev))->setflag(status_flags::dynallocated);
+       ex ret = dynallocate<add>(ev);
        return ret;
 }
 
@@ -171,6 +177,18 @@ ex lcoeff_wrt(ex e, const exvector& x)
        return ec.rbegin()->second;
 }
 
+exp_vector_t degree_vector(ex e, const exvector& vars)
+{
+       e = e.expand();
+       exp_vector_t dvec(vars.size());
+       for (std::size_t i = vars.size(); i-- != 0; ) {
+               const int deg_i = e.degree(vars[i]);
+               e = e.coeff(vars[i], deg_i);
+               dvec[i] = deg_i;
+       }
+       return dvec;
+}
+
 cln::cl_I integer_lcoeff(const ex& e, const exvector& vars)
 {
        ex_collect_t ec;