]> www.ginac.de Git - ginac.git/blob - ginac/polynomial/remainder.h
Happy New Year!
[ginac.git] / ginac / polynomial / remainder.h
1 /** @file remainder.h
2  *
3  *  Functions calculating remainders. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2019 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 bool remainder_in_field(umodpoly& r, const umodpoly& a, const umodpoly& b);
34
35 /**
36  * @brief Polynomial remainder for univariate polynomials over a ring. 
37  *
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.
42  */
43 template<typename T>
44 bool remainder_in_ring(T& r, const T& a, const T& b)
45 {
46         typedef typename T::value_type ring_t;
47         if (degree(a) < degree(b)) {
48                 r = a;
49                 return false;
50         }
51         // N.B: don't bother to optimize division by constant
52
53         r = a;
54         const ring_t b_lcoeff = lcoeff(b);
55         for (std::size_t k = a.size(); k-- >= b.size(); ) {
56
57                 // r -= r_k/b_n x^{k - n} b(x)
58                 if (zerop(r[k]))
59                         continue;
60
61                 const ring_t qk = truncate1(r[k], b_lcoeff);
62
63                 // Why C++ is so off-by-one prone?
64                 for (std::size_t j = k, i = b.size(); i-- != 0; --j) {
65                         if (zerop(b[i]))
66                                 continue;
67                         r[j] = r[j] - qk*b[i];
68                 }
69
70                 if (!zerop(r[k])) {
71                         // division failed, don't bother to continue
72                         break;
73                 }
74         }
75
76         // Canonicalize the remainder: remove leading zeros. We can't say
77         // anything about the degree of the remainder here.
78         canonicalize(r);
79         return r.empty();
80 }
81
82 } // namespace GiNaC
83
84 #endif // GINAC_UPOLY_REMAINDER_H