1 #ifndef GINAC_UPOLY_REMAINDER_TCC
2 #define GINAC_UPOLY_REMAINDER_TCC
4 #include "ring_traits.h"
11 * @brief Polynomial remainder for univariate polynomials over fields
13 * Given two univariate polynomials \f$a, b \in F[x]\f$, where F is
14 * a finite field (presumably Z/p) computes the remainder @a r, which is
15 * defined as \f$a = b q + r\f$. Returns true if the remainder is zero
16 * and false otherwise.
19 remainder_in_field(umodpoly& r, const umodpoly& a, const umodpoly& b)
21 typedef cln::cl_MI field_t;
23 if (degree(a) < degree(b)) {
27 // The coefficient ring is a field, so any 0 degree polynomial
28 // divides any other polynomial.
35 const field_t b_lcoeff = lcoeff(b);
36 for (std::size_t k = a.size(); k-- >= b.size(); ) {
38 // r -= r_k/b_n x^{k - n} b(x)
42 field_t qk = div(r[k], b_lcoeff);
43 bug_on(zerop(qk), "division in a field yield zero: "
44 << r[k] << '/' << b_lcoeff);
46 // Why C++ is so off-by-one prone?
47 for (std::size_t j = k, i = b.size(); i-- != 0; --j) {
50 r[j] = r[j] - qk*b[i];
52 bug_on(!zerop(r[k]), "polynomial division in field failed: " <<
53 "r[" << k << "] = " << r[k] << ", " <<
54 "r = " << r << ", b = " << b);
58 // Canonicalize the remainder: remove leading zeros. Give a hint
59 // to canonicalize(): we know degree(remainder) < degree(b)
60 // (because the coefficient ring is a field), so
61 // c_{degree(b)} \ldots c_{degree(a)} are definitely zero.
62 std::size_t from = degree(b) - 1;
63 canonicalize(r, from);
68 * @brief Polynomial remainder for univariate polynomials over a ring.
70 * Given two univariate polynomials \f$a, b \in R[x]\f$, where R is
71 * a ring (presumably Z) computes the remainder @a r, which is
72 * defined as \f$a = b q + r\f$. Returns true if the remainder is zero
73 * and false otherwise.
76 bool remainder_in_ring(T& r, const T& a, const T& b)
78 typedef typename T::value_type ring_t;
79 if (degree(a) < degree(b)) {
83 // N.B: don't bother to optimize division by constant
86 const ring_t b_lcoeff = lcoeff(b);
87 for (std::size_t k = a.size(); k-- >= b.size(); ) {
89 // r -= r_k/b_n x^{k - n} b(x)
93 const ring_t qk = truncate1(r[k], b_lcoeff);
95 // Why C++ is so off-by-one prone?
96 for (std::size_t j = k, i = b.size(); i-- != 0; --j) {
99 r[j] = r[j] - qk*b[i];
103 // division failed, don't bother to continue
108 // Canonicalize the remainder: remove leading zeros. We can't say
109 // anything about the degree of the remainder here.
115 #endif // GINAC_UPOLY_REMAINDER_TCC