carl  24.04
Computer ARithmetic Library
Encoding.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "VariableComparison.h"
4 
6 
7 namespace carl {
8 
9 template<typename Poly>
11  ass.erase(f.var());
12  auto roots = real_roots(to_univariate_polynomial(f.poly(), f.var()), ass);
13  if (!roots.is_univariate() || roots.roots().size() < f.k()) return;
14  Variable previous;
15  for (int i = 1; i <= f.k(); i++) {
16  Variable tmp_var;
17  if (i == f.k()) tmp_var = var;
18  else tmp_var = fresh_real_variable();
19  out.emplace_back(substitute(f.poly(), f.var(), Poly(tmp_var)), Relation::EQ);
20  if (previous != Variable::NO_VARIABLE) {
21  out.emplace_back(Poly(previous) - tmp_var, Relation::LESS);
22  }
23  previous = tmp_var;
24  }
25 }
26 
27 template<typename Poly>
29  ass.erase(f.var());
30  auto roots = real_roots(to_univariate_polynomial(f.poly(), f.var()), ass);
31  if (!roots.is_univariate() || roots.roots().size() < f.k()) return;
32  auto poly = substitute(f.poly(), f.var(), Poly(var));
33  ass.emplace(var, roots.roots()[f.k()-1]);
34  while (!poly.is_constant()) {
35  auto sign = sgn(evaluate(poly, ass));
36  Relation rel = Relation::EQ;
37  if (sign == Sign::POSITIVE) rel = Relation::GREATER;
38  else if (sign == Sign::NEGATIVE) rel = Relation::LESS;
39  out.emplace_back(poly, rel);
40  poly = derivative(poly, var);
41  }
42 }
43 
44 template<typename Poly>
45 using EncodingCache = std::map<MultivariateRoot<Poly>, std::pair<std::vector<BasicConstraint<Poly>>, Variable>>;
46 
47 template<typename Poly>
48 std::pair<std::vector<BasicConstraint<Poly>>, Variable> encode_as_constraints(const MultivariateRoot<Poly>& f, Assignment<typename VariableComparison<Poly>::RAN> ass, EncodingCache<Poly> cache) {
49  if (cache.find(f) == cache.end()) {
51  std::vector<BasicConstraint<Poly>> result;
52  encode_as_constraints_simple(f, ass, var, result);
53  encode_as_constraints_thom(f, ass, var, result);
54  if (!result.empty()) {
55  cache.emplace(f, std::make_pair(result, var));
56  } else {
57  cache.emplace(f, std::make_pair(result, Variable()));
58  }
59  }
60  return cache.at(f);
61 }
62 
63 template<typename Poly>
64 std::pair<std::vector<BasicConstraint<Poly>>, BasicConstraint<Poly>> encode_as_constraints(const VariableComparison<Poly>& f, const Assignment<typename VariableComparison<Poly>::RAN>& ass, EncodingCache<Poly> cache) {
65  auto c = as_constraint(f);
66  if (c) return std::make_pair(std::vector<BasicConstraint<Poly>>(), *c);
67  assert(std::holds_alternative<typename VariableComparison<Poly>::MR>(f.value()));
68  auto mv = std::get<typename VariableComparison<Poly>::MR>(f.value());
69  auto [result, var] = encode_as_constraints(mv, ass, cache);
70  if (result.empty()) return std::make_pair(std::vector<BasicConstraint<Poly>>(), BasicConstraint<Poly>(true));
71  BasicConstraint<Poly> res_constr(Poly(f.var()) - var, f.negated() ? inverse(f.relation()) : f.relation());
72  return std::make_pair(result, res_constr);
73 }
74 
75 }
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...
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
std::map< MultivariateRoot< Poly >, std::pair< std::vector< BasicConstraint< Poly > >, Variable > > EncodingCache
Definition: Encoding.h:45
RealRootsResult< IntRepRealAlgebraicNumber< Number > > real_roots(const UnivariatePolynomial< Coeff > &polynomial, const Interval< Number > &interval=Interval< Number >::unbounded_interval())
Find all real roots of a univariate 'polynomial' with numeric coefficients within a given 'interval'.
Definition: RealRoots.h:25
@ NEGATIVE
Indicates that .
@ POSITIVE
Indicates that .
std::optional< BasicConstraint< Poly > > as_constraint(const VariableComparison< Poly > &f)
Convert this variable comparison "v < root(..)" into a simpler polynomial (in)equality against zero "...
void encode_as_constraints_thom(const MultivariateRoot< Poly > &f, Assignment< typename VariableComparison< Poly >::RAN > ass, Variable var, std::vector< BasicConstraint< Poly >> &out)
Definition: Encoding.h:28
bool evaluate(const BasicConstraint< Poly > &c, const Assignment< Number > &m)
Definition: Evaluation.h:10
void encode_as_constraints_simple(const MultivariateRoot< Poly > &f, Assignment< typename VariableComparison< Poly >::RAN > ass, Variable var, std::vector< BasicConstraint< Poly >> &out)
Definition: Encoding.h:10
Sign sgn(const Number &n)
Obtain the sign of the given number.
Definition: Sign.h:54
Coeff substitute(const Monomial &m, const std::map< Variable, Coeff > &substitutions)
Applies the given substitutions to a monomial.
Definition: Substitution.h:19
Variable fresh_real_variable() noexcept
Definition: VariablePool.h:198
Relation inverse(Relation r)
Inverts the given relation symbol.
Definition: Relation.h:40
@ GREATER
Definition: SignCondition.h:15
std::map< Variable, T > Assignment
Definition: Common.h:13
Relation
Definition: Relation.h:20
std::pair< std::vector< BasicConstraint< Poly > >, Variable > encode_as_constraints(const MultivariateRoot< Poly > &f, Assignment< typename VariableComparison< Poly >::RAN > ass, EncodingCache< Poly > cache)
Definition: Encoding.h:48
Represent a polynomial (in)equality against zero.
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
std::size_t k() const noexcept
Return k, the index of the root.
const Poly & poly() const noexcept
Variable var() const noexcept
Represent a sum type/variant of an (in)equality between a variable on the left-hand side and multivar...
const std::variant< MR, RAN > & value() const
typename MultivariateRoot< Poly >::RAN RAN