carl  24.04
Computer ARithmetic Library
SquareFreePart.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "Derivative.h"
4 #include "Division.h"
5 #include "GCD.h"
7 
8 #include "../CoCoAAdaptor.h"
10 #include "../MultivariatePolynomial.h"
11 #include "../UnivariatePolynomial.h"
12 
13 namespace carl {
14 
15 template<typename C, typename O, typename P>
17  CARL_LOG_DEBUG("carl.core.sqfree", "SquareFreePart of " << polynomial);
18  if (polynomial.is_constant() || polynomial.is_linear()) return polynomial;
19 
20  auto s = overloaded {
21  #if defined USE_COCOA
22  [](const MultivariatePolynomial<mpq_class,O,P>& p){ CoCoAAdaptor<MultivariatePolynomial<mpq_class,O,P>> c({p}); return c.squareFreePart(p); },
23  [](const MultivariatePolynomial<mpz_class,O,P>& p){ CoCoAAdaptor<MultivariatePolynomial<mpz_class,O,P>> c({p}); return c.squareFreePart(p); }
24  #else
25  [](const MultivariatePolynomial<mpq_class,O,P>& p){ return p; },
26  [](const MultivariatePolynomial<mpz_class,O,P>& p){ return p; }
27  #endif
28  #if defined USE_GINAC
29  ,
30  [](const MultivariatePolynomial<cln::cl_RA,O,P>& p){ return p; },
31  [](const MultivariatePolynomial<cln::cl_I,O,P>& p){ return p; }
32  #endif
33  };
34  return s(polynomial);
35 }
36 
37 template<typename Coeff, EnableIf<is_subset_of_rationals_type<Coeff>> = dummy>
39  CARL_LOG_DEBUG("carl.core.sqfree", "SquareFreePart of " << p);
40  if (is_zero(p)) return p;
41  if (p.is_linear_in_main_var()) return p;
42  UnivariatePolynomial<Coeff> normalized = p.coprime_coefficients().template convert<Coeff>();
43  return carl::divide(normalized, carl::gcd(normalized, derivative(normalized))).quotient;
44 }
45 
46 template<typename Coeff, DisableIf<is_subset_of_rationals_type<Coeff>> = dummy>
47 UnivariatePolynomial<Coeff> squareFreePart(const UnivariatePolynomial<Coeff>& p) {
48  CARL_LOG_DEBUG("carl.core.sqfree", "SquareFreePart of " << p);
49  return carl::to_univariate_polynomial(carl::squareFreePart(MultivariatePolynomial<typename carl::UnderlyingNumberType<Coeff>::type>(p)), p.main_var());
50 }
51 
52 }
A small wrapper that configures logging for carl.
#define CARL_LOG_DEBUG(channel, msg)
Definition: carl-logging.h:43
carl is the main namespace for the library.
MultivariatePolynomial< C, O, P > squareFreePart(const MultivariatePolynomial< C, O, P > &polynomial)
UnivariatePolynomial< C > to_univariate_polynomial(const MultivariatePolynomial< C, O, P > &p)
Convert a univariate polynomial that is currently (mis)represented by a 'MultivariatePolynomial' into...
const T & derivative(const T &t, Variable, std::size_t n=1)
Computes the n'th derivative of a number, which is either the number itself (for n = 0) or zero.
Definition: Derivative.h:23
cln::cl_I gcd(const cln::cl_I &a, const cln::cl_I &b)
Calculate the greatest common divisor of two integers.
Definition: operations.h:310
bool is_zero(const Interval< Number > &i)
Check if this interval is a point-interval containing 0.
Definition: Interval.h:1453
void divide(const cln::cl_I &dividend, const cln::cl_I &divisor, cln::cl_I &quotient, cln::cl_I &remainder)
Definition: operations.h:326
T type
A type associated with the type.
Definition: typetraits.h:77
This class represents a univariate polynomial with coefficients of an arbitrary type.
UnivariatePolynomial< typename IntegralType< Coefficient >::type > coprime_coefficients() const
Constructs a new polynomial that is scaled such that the coefficients are coprime.
The general-purpose multivariate polynomial class.
bool is_constant() const
Check if the polynomial is constant.
bool is_linear() const
Check if the polynomial is linear.