3 * Functions calculating remainders. */
6 * GiNaC Copyright (C) 1999-2015 Johannes Gutenberg University Mainz, Germany
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #ifndef GINAC_UPOLY_REMAINDER_H
24 #define GINAC_UPOLY_REMAINDER_H
27 #include "ring_traits.h"
34 * @brief Polynomial remainder for univariate polynomials over fields
36 * Given two univariate polynomials \f$a, b \in F[x]\f$, where F is
37 * a finite field (presumably Z/p) computes the remainder @a r, which is
38 * defined as \f$a = b q + r\f$. Returns true if the remainder is zero
39 * and false otherwise.
42 remainder_in_field(umodpoly& r, const umodpoly& a, const umodpoly& b)
44 typedef cln::cl_MI field_t;
46 if (degree(a) < degree(b)) {
50 // The coefficient ring is a field, so any 0 degree polynomial
51 // divides any other polynomial.
58 const field_t b_lcoeff = lcoeff(b);
59 for (std::size_t k = a.size(); k-- >= b.size(); ) {
61 // r -= r_k/b_n x^{k - n} b(x)
65 field_t qk = div(r[k], b_lcoeff);
66 bug_on(zerop(qk), "division in a field yield zero: "
67 << r[k] << '/' << b_lcoeff);
69 // Why C++ is so off-by-one prone?
70 for (std::size_t j = k, i = b.size(); i-- != 0; --j) {
73 r[j] = r[j] - qk*b[i];
75 bug_on(!zerop(r[k]), "polynomial division in field failed: " <<
76 "r[" << k << "] = " << r[k] << ", " <<
77 "r = " << r << ", b = " << b);
81 // Canonicalize the remainder: remove leading zeros. Give a hint
82 // to canonicalize(): we know degree(remainder) < degree(b)
83 // (because the coefficient ring is a field), so
84 // c_{degree(b)} \ldots c_{degree(a)} are definitely zero.
85 std::size_t from = degree(b) - 1;
86 canonicalize(r, from);
91 * @brief Polynomial remainder for univariate polynomials over a ring.
93 * Given two univariate polynomials \f$a, b \in R[x]\f$, where R is
94 * a ring (presumably Z) computes the remainder @a r, which is
95 * defined as \f$a = b q + r\f$. Returns true if the remainder is zero
96 * and false otherwise.
99 bool remainder_in_ring(T& r, const T& a, const T& b)
101 typedef typename T::value_type ring_t;
102 if (degree(a) < degree(b)) {
106 // N.B: don't bother to optimize division by constant
109 const ring_t b_lcoeff = lcoeff(b);
110 for (std::size_t k = a.size(); k-- >= b.size(); ) {
112 // r -= r_k/b_n x^{k - n} b(x)
116 const ring_t qk = truncate1(r[k], b_lcoeff);
118 // Why C++ is so off-by-one prone?
119 for (std::size_t j = k, i = b.size(); i-- != 0; --j) {
122 r[j] = r[j] - qk*b[i];
126 // division failed, don't bother to continue
131 // Canonicalize the remainder: remove leading zeros. We can't say
132 // anything about the degree of the remainder here.
139 #endif // GINAC_UPOLY_REMAINDER_H