carl  24.04
Computer ARithmetic Library
Functions.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <carl-common/config.h>
4 #ifdef USE_LIBPOLY
5 
6 #include "LPPolynomial.h"
7 
8 namespace carl {
9 
10 /*
11  * @brief wrapper for the gcd calculation using libpoly. Asserts that the polynomials have the same context
12  * @param LPPolynomial p,q
13  * @return gcd(p,q)
14  */
15 inline LPPolynomial gcd(const LPPolynomial& p, const LPPolynomial& q) {
16  lp_polynomial_t* res = lp_polynomial_new(p.context().lp_context());
17  lp_polynomial_gcd(res, p.get_internal(), q.get_internal());
18  return LPPolynomial(res, p.context());
19 }
20 
21 /*
22  * @brief wrapper for the lcm calculation using libpoly. Asserts that the polynomials have the same context
23  * @param LPPolynomial p,q
24  * @return lcm(p,q)
25  */
26 inline LPPolynomial lcm(const LPPolynomial& p, const LPPolynomial& q) {
27  lp_polynomial_t* res = lp_polynomial_new(p.context().lp_context());
28  lp_polynomial_lcm(res, p.get_internal(), q.get_internal());
29  return LPPolynomial(res, p.context());
30 }
31 
32 /*
33  * @brief wrapper for the polynomial content calculation using libpoly.
34  * @param LPPolynomial p
35  * @return content(p)
36  */
37 inline LPPolynomial content(const LPPolynomial& p) {
38  lp_polynomial_t* res = lp_polynomial_new(p.context().lp_context());
39  lp_polynomial_cont(res, p.get_internal());
40  return LPPolynomial(res, p.context());
41 }
42 
43 inline LPPolynomial derivative(const LPPolynomial& p) {
44  lp_polynomial_t* res = lp_polynomial_new(p.context().lp_context());
45  lp_polynomial_derivative(res, p.get_internal());
46  return LPPolynomial(res, p.context());
47 }
48 
49 /*
50  * @brief wrapper for the polynomial primitive_part calculation using libpoly.
51  * @param LPPolynomial p
52  * @return primitive_part(p)
53  */
54 inline LPPolynomial primitive_part(const LPPolynomial& p) {
55  lp_polynomial_t* res = lp_polynomial_new(p.context().lp_context());
56  lp_polynomial_pp(res, p.get_internal());
57  return LPPolynomial(res, p.context());
58 }
59 
60 /*
61  * @brief wrapper for the resultant calculation using libpoly. Asserts that the polynomials have the same context
62  * @param LPPolynomial p,q
63  * @return resultant(p,q)
64  */
65 inline LPPolynomial resultant(const LPPolynomial& p, const LPPolynomial& q) {
66  lp_polynomial_t* res = lp_polynomial_new(p.context().lp_context());
67  lp_polynomial_resultant(res, p.get_internal(), q.get_internal());
68  return LPPolynomial(res, p.context());
69 }
70 
71 /*
72  * @brief wrapper for the discriminant calculation using libpoly.
73  * @param LPPolynomial p
74  * @return discriminant(p)
75  */
76 inline LPPolynomial discriminant(const LPPolynomial& p) {
77  assert(p.context().lp_context() == lp_polynomial_get_context(p.get_internal()));
78  if (lp_polynomial_degree(p.get_internal()) == 1) { // workaround for bug in the libpoly c++ wrapper
79  return LPPolynomial(p.context(), 1);
80  }
81  // div(resultant(p, derivative(p)), leading_coefficient(p));
82  lp_polynomial_t* ldcf = lp_polynomial_new(p.context().lp_context());
83  lp_polynomial_get_coefficient(ldcf, p.get_internal(), lp_polynomial_degree(p.get_internal()));
84  lp_polynomial_t* res = lp_polynomial_new(p.context().lp_context());
85  lp_polynomial_derivative(res, p.get_internal());
86  lp_polynomial_resultant(res, p.get_internal(), res);
87  lp_polynomial_div(res, res, ldcf);
88  lp_polynomial_delete(ldcf);
89  return LPPolynomial(res, p.context());
90 }
91 
92 /*
93  * @brief wrapper for the factorization of LPPolynomials
94  * @param LPPolynomial p
95  * @return factorization(p)
96  */
97 Factors<LPPolynomial> factorization(const LPPolynomial& p, bool includeConstant = true);
98 
99 std::vector<LPPolynomial> irreducible_factors(const LPPolynomial& p, bool includeConstants = true);
100 
101 /*
102  * @brief wrapper for the square_free_factorization of LPPolynomials
103  * @param LPPolynomial p
104  * @return square_free_factorization(p)
105  */
106 std::vector<LPPolynomial> square_free_factors(const LPPolynomial& p);
107 
108 /*
109  * @brief wrapper for the content_free_factors of LPPolynomials
110  * @param LPPolynomial p
111  * @return content_free_factors(p)
112  */
113 std::vector<LPPolynomial> content_free_factors(const LPPolynomial& p);
114 
115 std::vector<LPPolynomial> groebner_basis(const std::vector<LPPolynomial>& polys);
116 
117 } // namespace carl
118 
119 #endif
carl is the main namespace for the library.
Coeff content(const UnivariatePolynomial< Coeff > &p)
The content of a polynomial is the gcd of the coefficients of the normal part of a polynomial.
Definition: Content.h:22
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
auto irreducible_factors(const ContextPolynomial< Coeff, Ordering, Policies > &p, bool constants=true)
Definition: Functions.h:8
auto discriminant(const ContextPolynomial< Coeff, Ordering, Policies > &p)
Definition: Functions.h:17
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
cln::cl_I lcm(const cln::cl_I &a, const cln::cl_I &b)
Calculate the least common multiple of two integers.
Definition: operations.h:362
UnivariatePolynomial< Coeff > primitive_part(const UnivariatePolynomial< Coeff > &p)
The primitive part of p is the normal part of p divided by the content of p.
Definition: PrimitivePart.h:19
auto resultant(const ContextPolynomial< Coeff, Ordering, Policies > &p, const ContextPolynomial< Coeff, Ordering, Policies > &q)
Definition: Functions.h:22
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