]> www.ginac.de Git - ginac.git/blobdiff - ginac/polynomial/collect_vargs.cpp
Added `degree_vector' utility function.
[ginac.git] / ginac / polynomial / collect_vargs.cpp
index 5649e4a8373b5904eff7315ad56cbe8690ad6baa..c4368fb2a5d2c1849f1b8d377ba5fddd3b8ecd7a 100644 (file)
@@ -1,16 +1,41 @@
+/** @file collect_vargs.cpp
+ *
+ *  Utility functions. */
+
+/*
+ *  GiNaC Copyright (C) 1999-2010 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
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#include "add.h"
+#include "mul.h"
+#include "operators.h"
+#include "power.h"
+#include "collect_vargs.h"
+#include "smod_helpers.h"
+#include "debug.h"
+
+#include <algorithm>
+#include <cln/integer.h>
 #include <iterator>
 #include <map>
 #include <iterator>
 #include <map>
-#include <algorithm>
 #include <stdexcept>
 #include <string>
 #include <stdexcept>
 #include <string>
-#include <ginac/ginac.h>
-#include "collect_vargs.h"
-#include <cln/integer.h>
-#include "smod_helpers.h"
-#include "debug.hpp"
 
 
-namespace GiNaC
-{
+namespace GiNaC {
 
 typedef std::map<exp_vector_t, ex> ex_collect_priv_t;
 
 
 typedef std::map<exp_vector_t, ex> ex_collect_priv_t;
 
@@ -123,8 +148,14 @@ 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) {
                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(power(vars[j], exp_vector[j]));
                }
                tv.push_back(ec[i].second);
                ex tmp = (new mul(tv))->setflag(status_flags::dynallocated);
                }
                tv.push_back(ec[i].second);
                ex tmp = (new mul(tv))->setflag(status_flags::dynallocated);
@@ -146,6 +177,18 @@ ex lcoeff_wrt(ex e, const exvector& x)
        return ec.rbegin()->second;
 }
 
        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;
 cln::cl_I integer_lcoeff(const ex& e, const exvector& vars)
 {
        ex_collect_t ec;
@@ -161,4 +204,3 @@ cln::cl_I integer_lcoeff(const ex& e, const exvector& vars)
 }
 
 } // namespace GiNaC
 }
 
 } // namespace GiNaC
-