carl  24.04
Computer ARithmetic Library
Evaluation.cpp
Go to the documentation of this file.
1 #include "Evaluation.h"
2 
3 #ifdef USE_LIBPOLY
4 
5 #include "LPAssignment.h"
6 
7 
8 namespace carl {
9 
10 std::optional<LPRealAlgebraicNumber> evaluate(
11  const LPPolynomial& polynomial,
12  const std::map<Variable, LPRealAlgebraicNumber>& evalMap) {
13  lp_assignment_t& assignment = LPAssignment::getInstance().get(evalMap);
14  auto result = lp_polynomial_evaluate(polynomial.get_internal(), &assignment);
15 
16  if (result->type == LP_VALUE_NONE) {
17  CARL_LOG_DEBUG("carl.ran.libpoly", " Result: NONE");
18  return std::nullopt;
19  }
20 
21  auto ran = LPRealAlgebraicNumber(std::move(*result));
22  CARL_LOG_DEBUG("carl.ran.libpoly", " Result: " << ran);
23  return ran;
24 }
25 
26 inline auto lp_sign(carl::Relation rel) {
27  switch (rel) {
28  case Relation::EQ:
29  return LP_SGN_EQ_0;
30  case Relation::NEQ:
31  return LP_SGN_NE_0;
32  case Relation::LESS:
33  return LP_SGN_LT_0;
34  case Relation::LEQ:
35  return LP_SGN_LE_0;
36  case Relation::GREATER:
37  return LP_SGN_GT_0;
38  case Relation::GEQ:
39  return LP_SGN_GE_0;
40  default:
41  assert(false);
42  return LP_SGN_EQ_0;
43  }
44 }
45 
46 boost::tribool evaluate(const BasicConstraint<LPPolynomial>& constraint, const std::map<Variable, LPRealAlgebraicNumber>& evalMap) {
47  CARL_LOG_DEBUG("carl.ran.libpoly", " Evaluation constraint " << constraint << " for assignment " << evalMap);
48 
49  if (is_constant(constraint.lhs())) {
50  return carl::evaluate(constraint.lhs().constant_part(), constraint.relation());
51  }
52 
53  auto poly_pol = constraint.lhs().get_internal();
54  lp_assignment_t& assignment = LPAssignment::getInstance().get(evalMap);
55 
56  for (const auto& v : carl::variables(constraint)) {
57  if (evalMap.find(v) == evalMap.end()) {
58  int result = lp_polynomial_constraint_evaluate_subs(poly_pol, lp_sign(constraint.relation()), &assignment);
59  if (result == -1) return boost::indeterminate;
60  else return result;
61  }
62  }
63 
64  return lp_polynomial_constraint_evaluate(poly_pol, lp_sign(constraint.relation()), &assignment);
65 }
66 
67 }
68 
69 
70 #endif
#define CARL_LOG_DEBUG(channel, msg)
Definition: carl-logging.h:43
carl is the main namespace for the library.
bool is_constant(const ContextPolynomial< Coeff, Ordering, Policies > &p)
bool evaluate(const BasicConstraint< Poly > &c, const Assignment< Number > &m)
Definition: Evaluation.h:10
@ GREATER
Definition: SignCondition.h:15
Relation
Definition: Relation.h:20
void variables(const BasicConstraint< Pol > &c, carlVariables &vars)