carl  24.04
Computer ARithmetic Library
Functions.cpp
Go to the documentation of this file.
1 #include "Functions.h"
2 
3 #include <carl-common/config.h>
4 #ifdef USE_LIBPOLY
5 
6 #include "CoCoAAdaptorLP.h"
7 
8 namespace carl {
9 
10 inline Factors<LPPolynomial> factorization(const LPPolynomial& p, const CoCoAAdaptorLP& adaptor, bool includeConstant) {
11  #ifndef USE_COCOA
12  CARL_LOG_FATAL("carl.poly", "factorization is not supported without libcocoa");
13  #endif
14  return adaptor.factorize(p, includeConstant);
15 }
16 
17 /*
18  * @brief wrapper for the factorization of LPPolynomials
19  * @param LPPolynomial p
20  * @return factorization(p)
21  */
22 Factors<LPPolynomial> factorization(const LPPolynomial& p, bool includeConstant) {
23  CoCoAAdaptorLP adaptor = CoCoAAdaptorLP(p.context());
24  return factorization(p, adaptor, includeConstant);
25 }
26 
27 inline std::vector<LPPolynomial> irreducible_factors(const LPPolynomial& p, const CoCoAAdaptorLP& adaptor, bool includeConstants) {
28  #ifndef USE_COCOA
29  CARL_LOG_FATAL("carl.poly", "factorization is not supported without libcocoa");
30  #endif
31 
32  if (is_constant(p)) {
33  if (includeConstants) {
34  return {p};
35  } else {
36  return {};
37  }
38  } else if (p.total_degree() == 1) {
39  return {p};
40  }
41 
42  return adaptor.irreducible_factors(p, includeConstants);
43 }
44 
45 std::vector<LPPolynomial> irreducible_factors(const LPPolynomial& p, bool includeConstants) {
46  CoCoAAdaptorLP adaptor = CoCoAAdaptorLP(p.context());
47  return irreducible_factors(p, adaptor, includeConstants);
48 }
49 
50 std::vector<LPPolynomial> square_free_factors(const LPPolynomial& p) {
51  lp_polynomial_t** factors = nullptr;
52  std::size_t* multiplicities = nullptr;
53  std::size_t size = 0;
54  lp_polynomial_factor_square_free(p.get_internal(), &factors, &multiplicities, &size);
55  std::vector<LPPolynomial> res;
56  for (std::size_t i = 0; i < size; ++i) {
57  res.emplace_back(factors[i], p.context());
58  }
59  free(factors);
60  free(multiplicities);
61  return res;
62 }
63 
64 std::vector<LPPolynomial> content_free_factors(const LPPolynomial& p) {
65  lp_polynomial_t** factors = nullptr;
66  std::size_t* multiplicities = nullptr;
67  std::size_t size = 0;
68  lp_polynomial_factor_content_free(p.get_internal(), &factors, &multiplicities, &size);
69  std::vector<LPPolynomial> res;
70  for (std::size_t i = 0; i < size; ++i) {
71  res.emplace_back(factors[i], p.context());
72  }
73  free(factors);
74  free(multiplicities);
75  return res;
76 }
77 
78 std::vector<LPPolynomial> groebner_basis(const std::vector<LPPolynomial>& polys) {
79  if (polys.size() <= 1) return polys;
80  CoCoAAdaptorLP adaptor = CoCoAAdaptorLP(polys.at(0).context());
81  return adaptor.GBasis(polys);
82 }
83 
84 }
85 
86 #endif
#define CARL_LOG_FATAL(channel, msg)
Definition: carl-logging.h:39
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_constant(const ContextPolynomial< Coeff, Ordering, Policies > &p)
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
std::vector< MultivariatePolynomial< C, O, P > > groebner_basis(const std::vector< MultivariatePolynomial< C, O, P >> &polys)
Definition: Groebner.h:11