SMT-RAT  24.02
Toolbox for Strategic and Parallel Satisfiability-Modulo-Theories Solving
utils.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <carl-arith/poly/umvpoly/functions/Factorization.h>
4 #include <carl-arith/poly/umvpoly/functions/PrimitivePart.h>
5 #include <carl-arith/poly/umvpoly/functions/Resultant.h>
6 #include <carl-arith/poly/umvpoly/functions/SquareFreePart.h>
7 #include <carl-arith/poly/umvpoly/functions/to_univariate_polynomial.h>
8 
9 namespace smtrat {
10 namespace cad {
11 namespace projection {
12 
13 /**
14  * Tries to determine whether the given Poly vanishes for some assignment.
15  * Returns true if the Poly is guaranteed not to vanish.
16  * Returns false otherwise.
17  */
18 template<typename Poly>
19 bool doesNotVanish(const Poly& p) {
20  if (is_zero(p)) return false;
21  if (p.is_constant()) return true;
22  auto def = carl::definiteness(p);
23  if (def == carl::Definiteness::POSITIVE) return true;
24  if (def == carl::Definiteness::NEGATIVE) return true;
25  return false;
26 }
27 
28 /**
29  * Normalizes the given Poly by removing constant and duplicate factors.
30  */
31 template<typename Poly>
32 Poly normalize(const Poly& p) {
33  auto res = carl::pseudo_primitive_part(carl::squareFreePart(p)).normalized();
34  SMTRAT_LOG_TRACE("smtrat.cad.projection", "Normalizing " << p << " to " << res);
35  return res;
36 }
37 
38 /**
39  * Computes the resultant of two polynomials.
40  * The polynomials are assumed to be univariate polynomials and thus know their respective main variables.
41  * The given variable instead indicates the main variable of the resulting polynomial.
42  * @param variable Main variable of the resulting polynomial.
43  * @param p First input polynomial.
44  * @param q Second input polynomial.
45  * @return Resultant of p and q in main variable variable.
46  */
47 template<typename Poly>
48 Poly resultant(carl::Variable variable, const Poly& p, const Poly& q) {
49  auto res = normalize(carl::switch_main_variable(carl::resultant(p,q), variable));
50  SMTRAT_LOG_TRACE("smtrat.cad.projection", "resultant(" << p << ", " << q << ") = " << res);
51  return res;
52 }
53 
54 /**
55  * Computes the discriminant of a polynomial.
56  * The polynomial is assumed to be univariate and thus knows is main variable.
57  * The given variable instead indicates the main variable of the resulting polynomial.
58  * @param variable Main variable of the resulting polynomial.
59  * @param p Input polynomial.
60  * @return Discriminant of p in main variable variable.
61  */
62 template<typename Poly>
63 Poly discriminant(carl::Variable variable, const Poly& p) {
64  auto dis = normalize(carl::switch_main_variable(carl::discriminant(p), variable));
65  SMTRAT_LOG_TRACE("smtrat.cad.projection", "discriminant(" << p << ") = " << dis);
66  return dis;
67 }
68 
69 /**
70  * Construct the set of reducta of the given polynomial.
71  * This only adds a custom constructor to a std::
72  */
73 template<typename Poly>
74 struct Reducta : std::vector<Poly> {
75  Reducta(const Poly& p) {
76  this->emplace_back(p);
77  while (!is_zero(this->back())) {
78  this->emplace_back(this->back());
79  this->back().truncate();
80  }
81  this->pop_back();
82  SMTRAT_LOG_TRACE("smtrat.cad.projection", "Reducta of " << p << " = " << *this);
83  }
84 };
85 
86 /**
87  * Computes the Principal Subresultant Coefficients of two polynomials.
88  */
89 template<typename Poly>
90 std::vector<Poly> PSC(const Poly& p, const Poly& q) {
91  return carl::principalSubresultantsCoefficients(p, q);
92 }
93 
94 template<typename Poly, typename Callback>
95 void returnPoly(const Poly& p, Callback&& cb) {
96  if (true) {
97  for (const auto& fact: carl::factorization(carl::MultivariatePolynomial<Rational>(p), false)) {
98  auto uf = carl::to_univariate_polynomial(fact.first, p.main_var());
99  SMTRAT_LOG_DEBUG("smtrat.cad.projection", "-> " << uf);
100  cb(uf);
101  }
102  } else {
103  SMTRAT_LOG_DEBUG("smtrat.cad.projection", "-> " << p);
104  cb(p);
105  }
106 }
107 
108 } // namespace projection
109 } // namespace cad
110 } // namespace smtrat
Poly resultant(carl::Variable variable, const Poly &p, const Poly &q)
Computes the resultant of two polynomials.
Definition: utils.h:48
void returnPoly(const Poly &p, Callback &&cb)
Definition: utils.h:95
Poly discriminant(carl::Variable variable, const Poly &p)
Computes the discriminant of a polynomial.
Definition: utils.h:63
std::vector< Poly > PSC(const Poly &p, const Poly &q)
Computes the Principal Subresultant Coefficients of two polynomials.
Definition: utils.h:90
Poly normalize(const Poly &p)
Normalizes the given Poly by removing constant and duplicate factors.
Definition: utils.h:32
bool doesNotVanish(const Poly &p)
Tries to determine whether the given Poly vanishes for some assignment.
Definition: utils.h:19
Poly discriminant(const carl::Variable &mainVariable, const Poly &p)
Projection related utilities for onecellcad.
Definition: utils.h:398
Poly resultant(const carl::Variable &mainVariable, const Poly &p1, const Poly &p2)
Definition: utils.h:404
Class to create the formulas for axioms.
carl::MultivariatePolynomial< Rational > Poly
Definition: types.h:25
#define SMTRAT_LOG_TRACE(channel, msg)
Definition: logging.h:36
#define SMTRAT_LOG_DEBUG(channel, msg)
Definition: logging.h:35
Construct the set of reducta of the given polynomial.
Definition: utils.h:74
Reducta(const Poly &p)
Definition: utils.h:75