carl  24.04
Computer ARithmetic Library
Factorization.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "Power.h"
4 
6 #include "../CoCoAAdaptor.h"
9 
10 namespace carl {
11 
12 template<typename C, typename O, typename P>
13 class MultivariatePolynomial;
14 
15 namespace helper {
16  /**
17  * Returns a factors datastructure containing only the full polynomial as single factor.
18  */
19  template<typename C, typename O, typename P>
21  return { std::make_pair(p, 1) };
22  }
23 
24 } // namespace helper
25 
26 /**
27  * Try to factorize a multivariate polynomial..
28  * Uses CoCoALib and GiNaC, if available, depending on the coefficient type of the polynomial.
29  */
30 template<typename C, typename O, typename P>
32  if (p.total_degree() == 0) {
33  if (includeConstants) {
35  } else {
36  return {};
37  }
38  } else if (p.total_degree() == 1) {
40  }
41 
42  auto s = overloaded {
43  #if defined USE_COCOA
44  [includeConstants](const MultivariatePolynomial<mpq_class,O,P>& p){ CoCoAAdaptor<MultivariatePolynomial<mpq_class,O,P>> c({p}); return c.factorize(p, includeConstants); },
45  [includeConstants](const MultivariatePolynomial<mpz_class,O,P>& p){ CoCoAAdaptor<MultivariatePolynomial<mpz_class,O,P>> c({p}); return c.factorize(p, includeConstants); }
46  #else
49  #endif
50  #if defined USE_GINAC
51  ,
52  [](const MultivariatePolynomial<cln::cl_RA,O,P>& p){ return ginacFactorization(p); },
53  [](const MultivariatePolynomial<cln::cl_I,O,P>& p){ return ginacFactorization(p); }
54  #endif
55  };
56 
57  auto factors = s(p);
58  return factors;
59 }
60 
61 template<typename C, typename O, typename P>
63  return f.size() <= 1;
64 }
65 
66 
67 /**
68  * Try to factorize a multivariate polynomial and return the irreducible factors (without multiplicities).
69  * Uses CoCoALib and GiNaC, if available, depending on the coefficient type of the polynomial.
70  */
71 template<typename C, typename O, typename P>
72 std::vector<MultivariatePolynomial<C,O,P>> irreducible_factors(const MultivariatePolynomial<C,O,P>& p, bool includeConstants = true) {
73  if (p.total_degree() == 0) {
74  if (includeConstants) {
75  return {p};
76  } else {
77  return {};
78  }
79  } else if (p.total_degree() == 1) {
80  return {p};
81  }
82 
83  auto s = overloaded {
84  #if defined USE_COCOA
85  [includeConstants](const MultivariatePolynomial<mpq_class,O,P>& p){ CoCoAAdaptor<MultivariatePolynomial<mpq_class,O,P>> c({p}); return c.irreducible_factors(p, includeConstants); },
86  [includeConstants](const MultivariatePolynomial<mpz_class,O,P>& p){ CoCoAAdaptor<MultivariatePolynomial<mpz_class,O,P>> c({p}); return c.irreducible_factors(p, includeConstants); }
87  #else
88  [includeConstants](const MultivariatePolynomial<mpq_class,O,P>& p){ return std::vector<MultivariatePolynomial<mpq_class,O,P>>({p}); },
89  [includeConstants](const MultivariatePolynomial<mpz_class,O,P>& p){ return std::vector<MultivariatePolynomial<mpz_class,O,P>>({p}); }
90  #endif
91  #if defined USE_GINAC
92  ,
93  [](const MultivariatePolynomial<cln::cl_RA,O,P>& p){ return std::vector<MultivariatePolynomial<cln::cl_RA,O,P>>({p}); },
94  [](const MultivariatePolynomial<cln::cl_I,O,P>& p){ return std::vector<MultivariatePolynomial<cln::cl_I,O,P>>({p}); }
95  #endif
96  };
97  return s(p);
98 }
99 
100 }
A small wrapper that configures logging for carl.
carl is the main namespace for the library.
auto irreducible_factors(const ContextPolynomial< Coeff, Ordering, Policies > &p, bool constants=true)
Definition: Functions.h:8
bool is_trivial(const Factors< MultivariatePolynomial< C, O, P >> &f)
Definition: Factorization.h:62
std::map< Pol, uint > Factors
Definition: Common.h:21
Factors< MultivariatePolynomial< C, O, P > > factorization(const MultivariatePolynomial< C, O, P > &p, bool includeConstants=true)
Try to factorize a multivariate polynomial.
Definition: Factorization.h:31
Factors< MultivariatePolynomial< C, O, P > > trivialFactorization(const MultivariatePolynomial< C, O, P > &p)
Returns a factors datastructure containing only the full polynomial as single factor.
Definition: Factorization.h:20
The general-purpose multivariate polynomial class.
std::size_t total_degree() const
Calculates the max.