carl  24.04
Computer ARithmetic Library
GCD_multivariate.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <carl-common/config.h>
4 #include "PrimitiveEuclidean.h"
7 
8 #include "../CoCoAAdaptor.h"
10 
11 namespace carl {
12 
13 namespace gcd_detail {
14  template<typename Polynomial>
15  Variable select_variable(const Polynomial& p1, const Polynomial& p2) {
16  auto v1 = carl::variables(p1).as_vector();
17  auto v2 = carl::variables(p2).as_vector();
18  std::vector<Variable> common;
19 
20  std::set_intersection(v1.begin(),v1.end(),v2.begin(),v2.end(), std::inserter(common,common.begin()));
21  if (common.empty()) {
22  return Variable::NO_VARIABLE;
23  } else {
24  return *common.begin();
25  }
26  }
27 
28  template<typename Polynomial>
29  Polynomial gcd_calculate(const Polynomial& a, const Polynomial& b) {
30  Variable x = select_variable(a, b);
31  if (x == Variable::NO_VARIABLE) {
32  return Polynomial(1);
33  }
36  auto B = carl::to_univariate_polynomial(b,x);
37  Polynomial result(primitive_euclidean(A.normalized(), B.normalized()));
38  if (carl::is_negative(result.lcoeff()) && !(carl::is_negative(a.lcoeff()) && carl::is_negative(b.lcoeff()))) {
39  return -result;
40  }
41  return result;
42  }
43 }
44 
45 template<typename C, typename O, typename P>
47  CARL_LOG_DEBUG("carl.core.gcd", "gcd(" << a << ", " << b << ")");
48  assert(!is_zero(a));
49  assert(!is_zero(b));
50 
51  if (is_one(a) || is_one(b)) {
53  }
54  if (a.is_constant() && b.is_constant()) {
55  CARL_LOG_DEBUG("carl.core.gcd", "gcd(" << a << ", " << b << ") = " << carl::gcd(a.constant_part(), b.constant_part()));
57  }
58  if (a.is_constant() || b.is_constant()) {
60  }
61 
62  auto s = overloaded {
63  #if defined USE_GINAC
64  [](const MultivariatePolynomial<cln::cl_RA,O,P>& n1, const MultivariatePolynomial<cln::cl_RA,O,P>& n2){ return ginacGcd<MultivariatePolynomial<cln::cl_RA,O,P>>( n1, n2 ); },
65  [](const MultivariatePolynomial<cln::cl_I,O,P>& n1, const MultivariatePolynomial<cln::cl_I,O,P>& n2){ return ginacGcd<MultivariatePolynomial<cln::cl_I,O,P>>( n1, n2 ); },
66  #endif
67  #if defined USE_COCOA
68  [](const MultivariatePolynomial<mpq_class,O,P>& n1, const MultivariatePolynomial<mpq_class,O,P>& n2){ CoCoAAdaptor<MultivariatePolynomial<mpq_class,O,P>> c({n1, n2}); return c.gcd(n1,n2); },
69  [](const MultivariatePolynomial<mpz_class,O,P>& n1, const MultivariatePolynomial<mpz_class,O,P>& n2){ CoCoAAdaptor<MultivariatePolynomial<mpz_class,O,P>> c({n1, n2}); return c.gcd(n1,n2); }
70  #else
73  #endif
74  };
75  CARL_LOG_DEBUG("carl.core.gcd", "gcd(" << a << ", " << b << ")");
76  auto res = s(a, b);
77  CARL_LOG_DEBUG("carl.core.gcd", "gcd(" << a << ", " << b << ") = " << res);
78  return res;
79 }
80 }
#define CARL_LOG_INEFFICIENT()
Definition: carl-logging.h:49
#define CARL_LOG_DEBUG(channel, msg)
Definition: carl-logging.h:43
carl is the main namespace for the library.
UnivariatePolynomial< C > to_univariate_polynomial(const MultivariatePolynomial< C, O, P > &p)
Convert a univariate polynomial that is currently (mis)represented by a 'MultivariatePolynomial' into...
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
Interval< Number > set_intersection(const Interval< Number > &lhs, const Interval< Number > &rhs)
Intersects two intervals in a set-theoretic manner.
Definition: SetTheory.h:99
bool is_negative(const cln::cl_I &n)
Definition: operations.h:47
UnivariatePolynomial< Coeff > primitive_euclidean(const UnivariatePolynomial< Coeff > &a, const UnivariatePolynomial< Coeff > &b)
Computes the GCD of two univariate polynomial with coefficients from a unique factorization domain us...
void variables(const BasicConstraint< Pol > &c, carlVariables &vars)
bool is_one(const Interval< Number > &i)
Check if this interval is a point-interval containing 1.
Definition: Interval.h:1462
Polynomial gcd_calculate(const Polynomial &a, const Polynomial &b)
Variable select_variable(const Polynomial &p1, const Polynomial &p2)
A Variable represents an algebraic variable that can be used throughout carl.
Definition: Variable.h:85
static const Variable NO_VARIABLE
Instance of an invalid variable.
Definition: Variable.h:203
The general-purpose multivariate polynomial class.
bool is_constant() const
Check if the polynomial is constant.
const Coeff & constant_part() const
Retrieve the constant term of this polynomial or zero, if there is no constant term.