]> www.ginac.de Git - ginac.git/blob - ginac/polynomial/remainder.tcc
Implemented modular GCD algorithm for univariate polynomials.
[ginac.git] / ginac / polynomial / remainder.tcc
1 #ifndef GINAC_UPOLY_REMAINDER_TCC
2 #define GINAC_UPOLY_REMAINDER_TCC
3 #include "upoly.hpp"
4 #include "ring_traits.hpp"
5 #include "upoly_io.hpp"
6 #include "debug.hpp"
7
8 namespace GiNaC
9 {
10 /**
11  * @brief Polynomial remainder for univariate polynomials over fields
12  *
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.
17  */
18 static bool
19 remainder_in_field(umodpoly& r, const umodpoly& a, const umodpoly& b)
20 {
21         typedef cln::cl_MI field_t;
22
23         if (degree(a) < degree(b)) {
24                 r = a;
25                 return false;
26         }
27         // The coefficient ring is a field, so any 0 degree polynomial
28         // divides any other polynomial.
29         if (degree(b) == 0) {
30                 r.clear();
31                 return true;
32         }
33
34         r = a;
35         const field_t b_lcoeff = lcoeff(b);
36         for (std::size_t k = a.size(); k-- >= b.size(); ) {
37
38                 // r -= r_k/b_n x^{k - n} b(x)
39                 if (zerop(r[k]))
40                         continue;
41
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);
45
46                 // Why C++ is so off-by-one prone?
47                 for (std::size_t j = k, i = b.size(); i-- != 0; --j) {
48                         if (zerop(b[i]))
49                                 continue;
50                         r[j] = r[j] - qk*b[i];
51                 }
52                 bug_on(!zerop(r[k]), "polynomial division in field failed: " <<
53                                       "r[" << k << "] = " << r[k] << ", " <<
54                                       "r = " << r << ", b = " << b);
55
56         }
57
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);
64         return r.empty();
65 }
66
67 /**
68  * @brief Polynomial remainder for univariate polynomials over a ring. 
69  *
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.
74  */
75 template<typename T>
76 bool remainder_in_ring(T& r, const T& a, const T& b)
77 {
78         typedef typename T::value_type ring_t;
79         if (degree(a) < degree(b)) {
80                 r = a;
81                 return false;
82         }
83         // N.B: don't bother to optimize division by constant
84
85         r = a;
86         const ring_t b_lcoeff = lcoeff(b);
87         for (std::size_t k = a.size(); k-- >= b.size(); ) {
88
89                 // r -= r_k/b_n x^{k - n} b(x)
90                 if (zerop(r[k]))
91                         continue;
92
93                 const ring_t qk = div(r[k], b_lcoeff);
94
95                 // Why C++ is so off-by-one prone?
96                 for (std::size_t j = k, i = b.size(); i-- != 0; --j) {
97                         if (zerop(b[i]))
98                                 continue;
99                         r[j] = r[j] - qk*b[i];
100                 }
101
102                 if (!zerop(r[k])) {
103                         // division failed, don't bother to continue
104                         break;
105                 }
106         }
107
108         // Canonicalize the remainder: remove leading zeros. We can't say
109         // anything about the degree of the remainder here.
110         canonicalize(r);
111         return r.empty();
112 }
113 } // namespace GiNaC
114
115 #endif // GINAC_UPOLY_REMAINDER_TCC
116