carl  24.04
Computer ARithmetic Library
ModelEvaluation_Polynomial.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "../Model.h"
4 
8 
9 namespace carl {
10 
11  /**
12  * Substitutes all variables from a model within a polynomial.
13  * May fail to substitute some variables, for example if the values are RANs or SqrtEx.
14  */
15  template<typename Rational, typename Poly, typename ModelPoly>
17  for (auto var: carl::variables(p)) {
18  auto it = m.find(var);
19  if (it == m.end()) continue;
20  const ModelValue<Rational,ModelPoly>& value = m.evaluated(var);
21  if (value.isRational()) {
22  substitute_inplace(p, var, value.asRational());
23  } else if (value.isRAN()) {
24  if (value.asRAN().is_numeric()) {
25  substitute_inplace(p, var, value.asRAN().value());
26  }
27  } else if (value.isSubstitution()) {
28  if constexpr(std::is_same<Poly,ModelPoly>::value) {
29  const auto& subs = value.asSubstitution();
30  auto polysub = dynamic_cast<const ModelPolynomialSubstitution<Rational,Poly>*>(subs.get());
31  if (polysub != nullptr) {
32  substitute_inplace(p, var, polysub->getPoly());
33  }
34  }
35  }
36  }
37  }
38 
39  /**
40  * Evaluates a polynomial to a ModelValue over a Model.
41  * If evaluation can not be done for some variables, the result may actually be a ModelPolynomialSubstitution.
42  */
43  template<typename Rational, typename Poly>
45  substitute_inplace(p, m);
46 
47  auto map = model::collectRANIR(carl::variables(p).as_set(), m);
48  if (map.size() == carl::variables(p).size()) {
49  res = *evaluate(p, map);
50  return;
51  }
52  res = createSubstitution<Rational,Poly,ModelPolynomialSubstitution<Rational,Poly>>(p);
53  }
54 
55 }
carl is the main namespace for the library.
bool evaluate(const BasicConstraint< Poly > &c, const Assignment< Number > &m)
Definition: Evaluation.h:10
void evaluate_inplace(ModelValue< Rational, Poly > &res, BVTerm &bvt, const Model< Rational, Poly > &m)
Evaluates a bitvector term to a ModelValue over a Model.
void variables(const BasicConstraint< Pol > &c, carlVariables &vars)
void substitute_inplace(MultivariateRoot< Poly > &mr, Variable var, const Poly &poly)
Create a copy of the underlying polynomial with the given variable replaced by the given polynomial.
Assignment< typename Poly::RootType > collectRANIR(const std::set< Variable > &vars, const Model< Rational, Poly > &model)
Represent a collection of assignments/mappings from variables to values.
Definition: Model.h:19
auto find(const typename Map::key_type &key) const
Definition: Model.h:102
auto end() const
Definition: Model.h:46
const ModelValue< Rational, Poly > & evaluated(const typename Map::key_type &key) const
Return the ModelValue for the given key, evaluated if it's a ModelSubstitution and evaluatable,...
Definition: Model.h:146
Represent a sum type/variant over the different kinds of values that can be assigned to the different...
Definition: ModelValue.h:56
const Poly::RootType & asRAN() const
Definition: ModelValue.h:233
const ModelSubstitutionPtr< Rational, Poly > & asSubstitution() const
Definition: ModelValue.h:273
const Rational & asRational() const
Definition: ModelValue.h:217
bool isSubstitution() const
Definition: ModelValue.h:202
bool isRational() const
Definition: ModelValue.h:150
bool isRAN() const
Definition: ModelValue.h:164