1 // (C) Copyright Jeremy William Murphy 2016.
3 // Use, modification and distribution are subject to the
4 // Boost Software License, Version 1.0. (See accompanying file
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 #ifndef BOOST_MATH_TOOLS_POLYNOMIAL_GCD_HPP
8 #define BOOST_MATH_TOOLS_POLYNOMIAL_GCD_HPP
14 #include <boost/math/tools/polynomial.hpp>
15 #include <boost/math/common_factor_rt.hpp>
16 #include <boost/type_traits/is_pod.hpp>
23 namespace gcd_detail {
29 struct gcd_traits<boost::math::tools::polynomial<T> >
31 inline static const boost::math::tools::polynomial<T>& abs(const boost::math::tools::polynomial<T>& val) { return val; }
33 static const method_type method = method_euclid;
41 namespace math{ namespace tools{
45 * We may write any nonzero polynomial u(x) from R[x] where R is a UFD as
47 * u(x) = cont(u) . pp(u(x))
49 * where cont(u), the content of u, is an element of S, and pp(u(x)), the primitive
50 * part of u(x), is a primitive polynomial over S.
51 * When u(x) = 0, it is convenient to define cont(u) = pp(u(x)) = O.
55 T content(polynomial<T> const &x)
57 return x ? gcd_range(x.data().begin(), x.data().end()).first : T(0);
62 polynomial<T> primitive_part(polynomial<T> const &x, T const &cont)
64 return x ? x / cont : polynomial<T>();
69 polynomial<T> primitive_part(polynomial<T> const &x)
71 return primitive_part(x, content(x));
75 // Trivial but useful convenience function referred to simply as l() in Knuth.
77 T leading_coefficient(polynomial<T> const &x)
79 return x ? x.data().back() : T(0);
85 /* Reduce u and v to their primitive parts and return the gcd of their
86 * contents. Used in a couple of gcd algorithms.
89 T reduce_to_primitive(polynomial<T> &u, polynomial<T> &v)
91 using boost::math::gcd;
92 T const u_cont = content(u), v_cont = content(v);
95 return gcd(u_cont, v_cont);
101 * Knuth, The Art of Computer Programming: Volume 2, Third edition, 1998
102 * Algorithm 4.6.1C: Greatest common divisor over a unique factorization domain.
104 * The subresultant algorithm by George E. Collins [JACM 14 (1967), 128-142],
105 * later improved by W. S. Brown and J. F. Traub [JACM 18 (1971), 505-514].
107 * Although step C3 keeps the coefficients to a "reasonable" size, they are
108 * still potentially several binary orders of magnitude larger than the inputs.
109 * Thus, this algorithm should only be used where T is a multi-precision type.
111 * @tparam T Polynomial coefficient type.
112 * @param u First polynomial.
113 * @param v Second polynomial.
114 * @return Greatest common divisor of polynomials u and v.
117 typename enable_if_c< std::numeric_limits<T>::is_integer, polynomial<T> >::type
118 subresultant_gcd(polynomial<T> u, polynomial<T> v)
121 BOOST_ASSERT(u || v);
128 typedef typename polynomial<T>::size_type N;
130 if (u.degree() < v.degree())
133 T const d = detail::reduce_to_primitive(u, v);
138 BOOST_ASSERT(u.degree() >= v.degree());
142 return d * primitive_part(v); // Attach the content.
144 return d * polynomial<T>(T(1)); // The content is the result.
145 N const delta = u.degree() - v.degree();
148 v = r / (g * detail::integer_power(h, delta));
149 g = leading_coefficient(u);
150 T const tmp = detail::integer_power(g, delta);
152 h = tmp * detail::integer_power(h, N(1) - delta);
154 h = tmp / detail::integer_power(h, delta - N(1));
160 * @brief GCD for polynomials with unbounded multi-precision integral coefficients.
162 * The multi-precision constraint is enforced via numeric_limits.
164 * Note that intermediate terms in the evaluation can grow arbitrarily large, hence the need for
165 * unbounded integers, otherwise numeric loverflow would break the algorithm.
167 * @tparam T A multi-precision integral type.
169 template <typename T>
170 typename enable_if_c<std::numeric_limits<T>::is_integer && !std::numeric_limits<T>::is_bounded, polynomial<T> >::type
171 gcd(polynomial<T> const &u, polynomial<T> const &v)
173 return subresultant_gcd(u, v);
175 // GCD over bounded integers is not currently allowed:
176 template <typename T>
177 typename enable_if_c<std::numeric_limits<T>::is_integer && std::numeric_limits<T>::is_bounded, polynomial<T> >::type
178 gcd(polynomial<T> const &u, polynomial<T> const &v)
180 BOOST_STATIC_ASSERT_MSG(sizeof(v) == 0, "GCD on polynomials of bounded integers is disallowed due to the excessive growth in the size of intermediate terms.");
181 return subresultant_gcd(u, v);
183 // GCD over polynomials of floats can go via the Euclid algorithm:
184 template <typename T>
185 typename enable_if_c<!std::numeric_limits<T>::is_integer && (std::numeric_limits<T>::min_exponent != std::numeric_limits<T>::max_exponent) && !std::numeric_limits<T>::is_exact, polynomial<T> >::type
186 gcd(polynomial<T> const &u, polynomial<T> const &v)
188 return boost::integer::gcd_detail::Euclid_gcd(u, v);
193 // Using declaration so we overload the default implementation in this namespace:
195 using boost::math::tools::gcd;
202 // Using declaration so we overload the default implementation in this namespace:
204 using boost::math::tools::gcd;
207 } // namespace boost::math::tools