]> www.ginac.de Git - ginac.git/blob - ginac/polynomial/remainder.h
c099dfe9f9d4b7caabf33f455390d860b121145d
[ginac.git] / ginac / polynomial / remainder.h
1 /** @file remainder.h
2  *
3  *  Functions calculating remainders. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2009 Johannes Gutenberg University Mainz, Germany
7  *
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.
12  *
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.
17  *
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
21  */
22
23 #ifndef GINAC_UPOLY_REMAINDER_H
24 #define GINAC_UPOLY_REMAINDER_H
25
26 #include "upoly.h"
27 #include "ring_traits.h"
28 #include "upoly_io.h"
29 #include "debug.h"
30
31 namespace GiNaC {
32
33 /**
34  * @brief Polynomial remainder for univariate polynomials over fields
35  *
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.
40  */
41 static bool
42 remainder_in_field(umodpoly& r, const umodpoly& a, const umodpoly& b)
43 {
44         typedef cln::cl_MI field_t;
45
46         if (degree(a) < degree(b)) {
47                 r = a;
48                 return false;
49         }
50         // The coefficient ring is a field, so any 0 degree polynomial
51         // divides any other polynomial.
52         if (degree(b) == 0) {
53                 r.clear();
54                 return true;
55         }
56
57         r = a;
58         const field_t b_lcoeff = lcoeff(b);
59         for (std::size_t k = a.size(); k-- >= b.size(); ) {
60
61                 // r -= r_k/b_n x^{k - n} b(x)
62                 if (zerop(r[k]))
63                         continue;
64
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);
68
69                 // Why C++ is so off-by-one prone?
70                 for (std::size_t j = k, i = b.size(); i-- != 0; --j) {
71                         if (zerop(b[i]))
72                                 continue;
73                         r[j] = r[j] - qk*b[i];
74                 }
75                 bug_on(!zerop(r[k]), "polynomial division in field failed: " <<
76                                       "r[" << k << "] = " << r[k] << ", " <<
77                                       "r = " << r << ", b = " << b);
78
79         }
80
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);
87         return r.empty();
88 }
89
90 /**
91  * @brief Polynomial remainder for univariate polynomials over a ring. 
92  *
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.
97  */
98 template<typename T>
99 bool remainder_in_ring(T& r, const T& a, const T& b)
100 {
101         typedef typename T::value_type ring_t;
102         if (degree(a) < degree(b)) {
103                 r = a;
104                 return false;
105         }
106         // N.B: don't bother to optimize division by constant
107
108         r = a;
109         const ring_t b_lcoeff = lcoeff(b);
110         for (std::size_t k = a.size(); k-- >= b.size(); ) {
111
112                 // r -= r_k/b_n x^{k - n} b(x)
113                 if (zerop(r[k]))
114                         continue;
115
116                 const ring_t qk = truncate1(r[k], b_lcoeff);
117
118                 // Why C++ is so off-by-one prone?
119                 for (std::size_t j = k, i = b.size(); i-- != 0; --j) {
120                         if (zerop(b[i]))
121                                 continue;
122                         r[j] = r[j] - qk*b[i];
123                 }
124
125                 if (!zerop(r[k])) {
126                         // division failed, don't bother to continue
127                         break;
128                 }
129         }
130
131         // Canonicalize the remainder: remove leading zeros. We can't say
132         // anything about the degree of the remainder here.
133         canonicalize(r);
134         return r.empty();
135 }
136
137 } // namespace GiNaC
138
139 #endif // GINAC_UPOLY_REMAINDER_H