]> www.ginac.de Git - ginac.git/blob - ginac/polynomial/remainder.cpp
9058f44bdf2628c44dab32a4433a1b2af35df120
[ginac.git] / ginac / polynomial / remainder.cpp
1 /** @file remainder.h
2  *
3  *  Functions calculating remainders. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2017 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 #include "remainder.h"
24 #include "ring_traits.h"
25 #include "upoly_io.h"
26 #include "debug.h"
27
28 namespace GiNaC {
29
30 /**
31  * @brief Polynomial remainder for univariate polynomials over fields
32  *
33  * Given two univariate polynomials \f$a, b \in F[x]\f$, where F is
34  * a finite field (presumably Z/p) computes the remainder @a r, which is
35  * defined as \f$a = b q + r\f$. Returns true if the remainder is zero
36  * and false otherwise.
37  */
38 bool
39 remainder_in_field(umodpoly& r, const umodpoly& a, const umodpoly& b)
40 {
41         typedef cln::cl_MI field_t;
42
43         if (degree(a) < degree(b)) {
44                 r = a;
45                 return false;
46         }
47         // The coefficient ring is a field, so any 0 degree polynomial
48         // divides any other polynomial.
49         if (degree(b) == 0) {
50                 r.clear();
51                 return true;
52         }
53
54         r = a;
55         const field_t b_lcoeff = lcoeff(b);
56         for (std::size_t k = a.size(); k-- >= b.size(); ) {
57
58                 // r -= r_k/b_n x^{k - n} b(x)
59                 if (zerop(r[k]))
60                         continue;
61
62                 field_t qk = div(r[k], b_lcoeff);
63                 bug_on(zerop(qk), "division in a field yield zero: "
64                                    << r[k] << '/' << b_lcoeff);
65
66                 // Why C++ is so off-by-one prone?
67                 for (std::size_t j = k, i = b.size(); i-- != 0; --j) {
68                         if (zerop(b[i]))
69                                 continue;
70                         r[j] = r[j] - qk*b[i];
71                 }
72                 bug_on(!zerop(r[k]), "polynomial division in field failed: " <<
73                                       "r[" << k << "] = " << r[k] << ", " <<
74                                       "r = " << r << ", b = " << b);
75
76         }
77
78         // Canonicalize the remainder: remove leading zeros. Give a hint
79         // to canonicalize(): we know degree(remainder) < degree(b)
80         // (because the coefficient ring is a field), so
81         // c_{degree(b)} \ldots c_{degree(a)} are definitely zero.
82         std::size_t from = degree(b) - 1;
83         canonicalize(r, from);
84         return r.empty();
85 }
86
87 } // namespace GiNaC