1 #ifndef GINAC_NEW_UPOLY_HPP
2 #define GINAC_NEW_UPOLY_HPP
5 #include <cln/integer.h>
6 #include <cln/modinteger.h>
7 #include <cln/integer_io.h>
17 typedef std::vector<cln::cl_I> upoly;
18 typedef std::vector<cln::cl_MI> umodpoly;
20 template<typename T> static std::size_t degree(const T& p)
25 template<typename T> static typename T::value_type lcoeff(const T& p)
27 bug_on(p.empty(), "lcoeff of a zero polynomial is undefined");
28 return p[p.size() - 1];
31 template<typename T> static typename T::reference lcoeff(T& p)
33 bug_on(p.empty(), "lcoeff of a zero polynomial is undefined");
34 return p[p.size() - 1];
37 template<typename T> static typename T::value_type max_coeff(const T& p)
39 bug_on(p.empty(), "max_coeff of a zero polynomial is undefined");
40 typename T::value_type curr = p[p.size() - 1];
41 for (std::size_t i = p.size(); i-- != 0; ) {
50 // Canonicalize the polynomial, i.e. remove leading zero coefficients
51 template<typename T> static void
52 canonicalize(T& p, const typename T::size_type hint =
53 std::numeric_limits<typename T::size_type>::max())
58 std::size_t i = p.size() - 1;
59 // Be fast if the polynomial is already canonicalized
84 bug_on(!zerop(p.at(i)), "p[" << i << "] = " << p[i] << " != 0 would be erased.");
86 typename T::const_iterator it = p.begin() + i;
87 for (std::size_t k = i; it != p.end(); ++it, ++k) {
88 bug_on(!zerop(*it), "p[" << k << "] = " << p[k] << " != 0 "
92 p.erase(p.begin() + i, p.end());
94 bug_on(!p.empty() && zerop(lcoeff(p)), "oops, lcoeff(p) = 0");
97 // Multiply a univariate polynomial p \in R[x] with a constant c \in R
98 template<typename T> static T&
99 operator*=(T& p, const typename T::value_type& c)
107 if (c == the_one(p[0]))
110 for (std::size_t i = p.size(); i-- != 0; )
116 // Convert Z[x] -> Z/p[x]
119 make_umodpoly(umodpoly& up, const upoly& p, const cln::cl_modint_ring& R)
121 for (std::size_t i = p.size(); i-- != 0; )
122 up[i] = R->canonhom(p[i]);
128 #endif // GINAC_NEW_UPOLY_HPP