carl  24.04
Computer ARithmetic Library
ModelEvaluation_MVRoot.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "../Model.h"
5 
6 namespace carl {
7 
8  /**
9  * Substitutes all variables from a model within a MultivariateRoot.
10  * May fail to substitute some variables, for example if the values are RANs or SqrtEx.
11  */
12  template<typename Rational, typename Poly>
14  for (auto var: carl::variables(mvr)) {
15  auto it = m.find(var);
16  if (it == m.end()) continue;
17  const ModelValue<Rational,Poly>& value = m.evaluated(var);
18  if (value.isRational()) {
19  substitute_inplace(mvr, var, Poly(value.asRational()));
20  } else if (value.isRAN()) {
21  if (value.asRAN().is_numeric()) substitute_inplace(mvr, var, Poly(value.asRAN().value()));
22  }
23  }
24  }
25 
26  /**
27  * Evaluates a MultivariateRoot to a ModelValue over a Model.
28  * If evaluation can not be done for some variables, the result may actually be a ModelMVRootSubstitution.
29  */
30  template<typename Rational, typename Poly>
32  CARL_LOG_DEBUG("carl.model.evaluation", "Substituting " << m << " into " << mvr);
33  substitute_inplace(mvr, m);
34 
35  auto map = model::collectRANIR(carl::variables(mvr).as_set(), m);
36  if (map.size() == carl::variables(mvr).size()) {
37  CARL_LOG_DEBUG("carl.model.evaluation", "Fully evaluating " << mvr << " over " << map);
38  auto r = evaluate(mvr,map);
39  if (r) {
40  CARL_LOG_DEBUG("carl.model.evaluation", "Got result " << *r);
41  res = *r;
42  return;
43  } else {
44  CARL_LOG_DEBUG("carl.model.evaluation", "MVRoot does not exist.");
45  res = false;
46  return;
47  }
48  }
49  CARL_LOG_DEBUG("carl.model.evaluation", "Could not evaluate, returning " << mvr);
50  res = createSubstitution<Rational,Poly,ModelMVRootSubstitution<Rational,Poly>>(mvr);
51  }
52 
53 }
Represent a dynamic root, also known as a "root expression".
#define CARL_LOG_DEBUG(channel, msg)
Definition: carl-logging.h:43
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 Rational & asRational() const
Definition: ModelValue.h:217
bool isRational() const
Definition: ModelValue.h:150
bool isRAN() const
Definition: ModelValue.h:164