carl  24.04
Computer ARithmetic Library
Evaluation.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "Degree.h"
4 
5 #include "../MultivariatePolynomial.h"
6 #include "../UnivariatePolynomial.h"
7 
8 namespace carl {
9 
10 template<typename Coefficient>
11 Coefficient evaluate(const Monomial& m, const std::map<Variable, Coefficient>& substitutions) {
12  CARL_LOG_FUNC("carl.core.monomial", m << ", " << substitutions);
13  Coefficient res = carl::constant_one<Coefficient>::get();
14  for (const auto& ve : m) {
15  auto it = substitutions.find(ve.first);
16  assert(it != substitutions.end());
17  res *= carl::pow(it->second, ve.second);
18  }
19  CARL_LOG_TRACE("carl.core.monomial", "Result: " << res);
20  return res;
21 }
22 
23 template<typename Coefficient>
24 Coefficient evaluate(const Term<Coefficient>& t, const std::map<Variable, Coefficient>& map) {
25  if (t.monomial()) {
26  return t.coeff() * evaluate(*t.monomial(), map);
27  } else {
28  return t.coeff();
29  }
30 }
31 
32 /**
33  * Like substitute, but expects substitutions for all variables.
34  * @return For a polynomial p, the function value p(x_1,...,x_n).
35  */
36 template<typename C, typename O, typename P, typename SubstitutionType>
37 SubstitutionType evaluate(const MultivariatePolynomial<C,O,P>& p, const std::map<Variable, SubstitutionType>& substitutions) {
38  if(carl::is_zero(p)) {
40  } else {
41  SubstitutionType result(evaluate(p[0], substitutions));
42  for (unsigned i = 1; i < p.nr_terms(); ++i) {
43  result += evaluate(p[i], substitutions);
44  }
45  return result;
46  };
47 }
48 
49 template<typename Coeff>
51  Coeff result(0);
52  Coeff var(1);
53  for (const Coeff& coeff : p.coefficients()) {
54  result += (coeff * var);
55  var *= value;
56  }
57  return result;
58 }
59 
60 
61 template<typename Coeff>
62 bool is_root_of(const UnivariatePolynomial<Coeff>& p, const Coeff& value) {
63  return carl::sgn(carl::evaluate(p, value)) == Sign::ZERO;
64 }
65 
66 }
Implements utility functions concerning the (total) degree of monomials, terms and polynomials.
#define CARL_LOG_FUNC(channel, args)
Definition: carl-logging.h:46
#define CARL_LOG_TRACE(channel, msg)
Definition: carl-logging.h:44
carl is the main namespace for the library.
@ ZERO
Indicates that .
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
bool is_root_of(const UnivariatePolynomial< Coeff > &p, const Coeff &value)
Definition: Evaluation.h:62
Sign sgn(const Number &n)
Obtain the sign of the given number.
Definition: Sign.h:54
typename UnderlyingNumberType< P >::type Coeff
Interval< Number > pow(const Interval< Number > &i, Integer exp)
Definition: Power.h:11
static const T & get()
Definition: constants.h:42
static const T & get()
Definition: constants.h:51
This class represents a univariate polynomial with coefficients of an arbitrary type.
const std::vector< Coefficient > & coefficients() const &
Retrieves the coefficients defining this polynomial.
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
Represents a single term, that is a numeric coefficient and a monomial.
Definition: Term.h:23
Coefficient & coeff()
Get the coefficient.
Definition: Term.h:80
Monomial::Arg & monomial()
Get the monomial.
Definition: Term.h:91