3 * Functions to normalize polynomials in a field. */
6 * GiNaC Copyright (C) 1999-2011 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_NORMALIZE_H
24 #define GINAC_UPOLY_NORMALIZE_H
27 #include "ring_traits.h"
32 /// Make the univariate polynomial @a a \in F[x] unit normal.
33 /// F should be a field.
34 /// Returns true if the polynomial @x is already unit normal, and false
36 static bool normalize_in_field(umodpoly& a, cln::cl_MI* content_ = 0)
40 if (lcoeff(a) == the_one(a[0])) {
42 *content_ = the_one(a[0]);
46 const cln::cl_MI lc_1 = recip(lcoeff(a));
47 for (std::size_t k = a.size(); k-- != 0; )
54 /// Make the univariate polynomial @a x unit normal. This version is used
55 /// for rings which are not fields.
56 /// Returns true if the polynomial @x is already unit normal, and false
58 template<typename T> bool
59 normalize_in_ring(T& x, typename T::value_type* content_ = 0, int* unit_ = 0)
61 typedef typename T::value_type ring_t;
62 static const ring_t one(1);
66 bool something_changed = false;
67 if (minusp(lcoeff(x))) {
68 something_changed = true;
71 for (std::size_t i = x.size(); i-- != 0; )
79 return something_changed;
81 return false; // initial polynomial was unit normal
84 // Compute the gcd of coefficients
85 ring_t content = lcoeff(x);
86 // We want this function to be fast when applied to unit normal
87 // polynomials. Hence we start from the leading coefficient.
88 for (std::size_t i = x.size() - 1; i-- != 0; ) {
92 return something_changed;
94 content = gcd(x[i], content);
100 return something_changed;
103 for (std::size_t i = x.size(); i-- != 0; )
104 x[i] = exquo(x[i], content);
108 return false; // initial polynomial was not unit normal
113 #endif // GINAC_UPOLY_NORMALIZE_H