carl  24.04
Computer ARithmetic Library
IntervalEvaluation.h
Go to the documentation of this file.
1 #pragma once
4 
5 #include "../Monomial.h"
6 #include "../Term.h"
7 #include "../MultivariatePolynomial.h"
8 
9 namespace carl {
10 
11 template<typename Numeric>
12 inline Interval<Numeric> evaluate(const Monomial& m, const std::map<Variable, Interval<Numeric>>& map)
13 {
14  Interval<Numeric> result(1);
15  CARL_LOG_TRACE("carl.core.intervalevaluation", "Iterating over " << m);
16  for(unsigned i = 0; i < m.num_variables(); ++i)
17  {
18  CARL_LOG_TRACE("carl.core.intervalevaluation", "Iterating: " << m[i].first);
19  // We expect every variable to be in the map.
20  CARL_LOG_ASSERT("carl.core.intervalevaluation", map.count(m[i].first) > (size_t)0, "Every variable is expected to be in the map.");
21  result *= carl::pow(map.at(m[i].first), m[i].second);
22  if( result.is_zero() )
23  return result;
24  }
25  return result;
26 }
27 
28 template<typename Coeff, typename Numeric, EnableIf<std::is_same<Numeric, Coeff>> = dummy>
29 inline Interval<Numeric> evaluate(const Term<Coeff>& t, const std::map<Variable, Interval<Numeric>>& map)
30 {
31  Interval<Numeric> result(t.coeff());
32  if (t.monomial())
33  result *= evaluate( *t.monomial(), map );
34  return result;
35 }
36 
37 template<typename Coeff, typename Numeric, DisableIf<std::is_same<Numeric, Coeff>> = dummy>
38 inline Interval<Numeric> evaluate(const Term<Coeff>& t, const std::map<Variable, Interval<Numeric>>& map)
39 {
40  Interval<Numeric> result(t.coeff());
41  if (t.monomial())
42  result *= evaluate( *t.monomial(), map );
43  return result;
44 }
45 
46 template<typename Coeff, typename Policy, typename Ordering, typename Numeric>
48 {
49  CARL_LOG_FUNC("carl.core.intervalevaluation", p << ", " << map);
50  if(is_zero(p)) {
51  return Interval<Numeric>(0);
52  } else {
53  Interval<Numeric> result(evaluate(p[0], map));
54  for (unsigned i = 1; i < p.nr_terms(); ++i) {
55  if( result.is_infinite() )
56  return result;
57  result += evaluate(p[i], map);
58  }
59  return result;
60  }
61 }
62 
63 template<typename Numeric, typename Coeff, EnableIf<std::is_same<Numeric, Coeff>> = dummy>
65  CARL_LOG_FUNC("carl.core.intervalevaluation", p << ", " << map);
66  assert(map.count(p.main_var()) > 0);
68  const Interval<Numeric> varValue = map.at(p.main_var());
70  for (unsigned i = 0; i < p.degree(); i++) {
71  res += p.coefficients()[i] * exp;
72  if( res.is_infinite() )
73  return res;
74  exp = varValue.pow(i+1);
75  }
76  return res;
77 }
78 
79 template<typename Numeric, typename Coeff, DisableIf<std::is_same<Numeric, Coeff>> = dummy>
80 inline Interval<Numeric> evaluate(const UnivariatePolynomial<Coeff>& p, const std::map<Variable, Interval<Numeric>>& map) {
81  CARL_LOG_FUNC("carl.core.intervalevaluation", p << ", " << map);
82  assert(map.count(p.main_var()) > 0);
83  Interval<Numeric> res = Interval<Numeric>(carl::constant_zero<Numeric>().get());
84  const Interval<Numeric>& varValue = map.at(p.main_var());
85  Interval<Numeric> exp(1);
86  for (uint i = 0; i <= p.degree(); i++) {
87  res += evaluate(p.coefficients()[i], map) * exp;
88  if( res.is_infinite() )
89  return res;
90  exp = carl::pow(varValue, i+1);
91  }
92  return res;
93 }
94 
95 } //Namespace carl
#define CARL_LOG_FUNC(channel, args)
Definition: carl-logging.h:46
#define CARL_LOG_TRACE(channel, msg)
Definition: carl-logging.h:44
#define CARL_LOG_ASSERT(channel, condition, msg)
Definition: carl-logging.h:47
carl is the main namespace for the library.
std::uint64_t uint
Definition: numbers.h:16
Interval< Number > exp(const Interval< Number > &i)
Definition: Exponential.h:10
bool is_zero(const Interval< Number > &i)
Check if this interval is a point-interval containing 0.
Definition: Interval.h:1453
bool evaluate(const BasicConstraint< Poly > &c, const Assignment< Number > &m)
Definition: Evaluation.h:10
Interval< Number > pow(const Interval< Number > &i, Integer exp)
Definition: Power.h:11
auto & get(const std::string &name)
A Variable represents an algebraic variable that can be used throughout carl.
Definition: Variable.h:85
The class which contains the interval arithmetic including trigonometric functions.
Definition: Interval.h:134
static Interval< Number > empty_interval()
Method which returns the empty interval rooted at 0.
Definition: Interval.h:813
bool is_infinite() const
Function which determines, if the interval is (-oo,oo).
Definition: Interval.h:1026
bool is_zero() const
Function which determines, if the interval is the zero interval.
Definition: Interval.h:1101
This class represents a univariate polynomial with coefficients of an arbitrary type.
const std::vector< Coefficient > & coefficients() const &
Retrieves the coefficients defining this polynomial.
Variable main_var() const
Retrieves the main variable of this polynomial.
uint degree() const
Get the maximal exponent of the main variable.
The general-purpose multivariate polynomial class.
std::size_t nr_terms() const
Calculate the number of terms.
The general-purpose monomials.
Definition: Monomial.h:59
std::size_t num_variables() const
Returns the number of variables that occur in the monomial.
Definition: Monomial.h:245
Coefficient & coeff()
Get the coefficient.
Definition: Term.h:80
Monomial::Arg & monomial()
Get the monomial.
Definition: Term.h:91