3 * Functions calculating remainders. */
6 * GiNaC Copyright (C) 1999-2017 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"
33 bool remainder_in_field(umodpoly& r, const umodpoly& a, const umodpoly& b);
36 * @brief Polynomial remainder for univariate polynomials over a ring.
38 * Given two univariate polynomials \f$a, b \in R[x]\f$, where R is
39 * a ring (presumably Z) computes the remainder @a r, which is
40 * defined as \f$a = b q + r\f$. Returns true if the remainder is zero
41 * and false otherwise.
44 bool remainder_in_ring(T& r, const T& a, const T& b)
46 typedef typename T::value_type ring_t;
47 if (degree(a) < degree(b)) {
51 // N.B: don't bother to optimize division by constant
54 const ring_t b_lcoeff = lcoeff(b);
55 for (std::size_t k = a.size(); k-- >= b.size(); ) {
57 // r -= r_k/b_n x^{k - n} b(x)
61 const ring_t qk = truncate1(r[k], b_lcoeff);
63 // Why C++ is so off-by-one prone?
64 for (std::size_t j = k, i = b.size(); i-- != 0; --j) {
67 r[j] = r[j] - qk*b[i];
71 // division failed, don't bother to continue
76 // Canonicalize the remainder: remove leading zeros. We can't say
77 // anything about the degree of the remainder here.
84 #endif // GINAC_UPOLY_REMAINDER_H