SMT-RAT  24.02
Toolbox for Strategic and Parallel Satisfiability-Modulo-Theories Solving
utils.h
Go to the documentation of this file.
1 #pragma once
2 
3 
4 #include <carl-arith/poly/umvpoly/functions/Substitution.h>
5 #include <carl-formula/model/evaluation/ModelEvaluation.h>
6 #include <smtrat-common/model.h>
8 
9 namespace smtrat::lve {
10 
11 Rational evaluate(carl::Variable v, const Poly& p, const Rational& r) {
12  assert(carl::substitute(p, v, Poly(r)).is_constant());
13  return carl::substitute(p, v, Poly(r)).constant_part();
14 }
15 carl::Sign sgn(carl::Variable v, const Poly& p, const RAN& r) {
16  Model m;
17  m.assign(v, r);
18  auto res = carl::evaluate(p, m);
19  if (res.isRational()) {
20  return carl::sgn(res.asRational());
21  } else if (res.isRAN()) {
22  return carl::sgn(res.asRAN());
23  }
24  assert(false);
25  return carl::Sign::ZERO;
26 }
27 
28 std::optional<ModelValue> get_root(carl::Variable v, const Poly& p) {
29  auto res = carl::real_roots(carl::to_univariate_polynomial(p, v));
30  if (!res.is_univariate() || res.roots().empty()) {
31  return std::nullopt;
32  } else {
33  return res.roots().front();
34  }
35 }
36 
37 ModelValue get_non_root(carl::Variable v, const Poly& p) {
38  Rational r = 0;
39  while (carl::is_zero(evaluate(v, p, r))) {
40  r += Rational(1);
41  }
42  return r;
43 }
44 
45 std::optional<ModelValue> get_value_for_sgn(carl::Variable v, const Poly& p, carl::Sign sign) {
46  RAN test;
47  if (sgn(v, p, test) == sign) {
48  return ModelValue(test);
49  }
50  auto res = carl::real_roots(carl::to_univariate_polynomial(p, v));
51  if (!res.is_univariate() || res.roots().empty()) {
52  return std::nullopt;
53  }
54  test = sample_below(res.roots().front());
55  if (sgn(v, p, test) == sign) {
56  return ModelValue(test);
57  }
58  test = sample_above(res.roots().back());
59  if (sgn(v, p, test) == sign) {
60  return ModelValue(test);
61  }
62  for (std::size_t i = 0; i < res.roots().size() - 1; ++i) {
63  test = sample_between(res.roots()[i], res.roots()[i+1]);
64  if (sgn(v, p, test) == sign) {
65  return ModelValue(test);
66  }
67  }
68  return std::nullopt;
69 }
70 
71 carl::Sign sgn_of_invariant_poly(carl::Variable v, const Poly& p) {
72  return carl::sgn(evaluate(v, p, 0));
73 }
74 
75 }
bool sign(Lit p)
Definition: SolverTypes.h:63
Polynomial::RootType RAN
Definition: common.h:24
Rational evaluate(carl::Variable v, const Poly &p, const Rational &r)
Definition: utils.h:11
ModelValue get_non_root(carl::Variable v, const Poly &p)
Definition: utils.h:37
carl::Sign sgn_of_invariant_poly(carl::Variable v, const Poly &p)
Definition: utils.h:71
std::optional< ModelValue > get_value_for_sgn(carl::Variable v, const Poly &p, carl::Sign sign)
Definition: utils.h:45
std::optional< ModelValue > get_root(carl::Variable v, const Poly &p)
Definition: utils.h:28
carl::Sign sgn(carl::Variable v, const Poly &p, const RAN &r)
Definition: utils.h:15
bool is_constant(const T &t)
Checks whether the constraint is constant, i.e.
bool substitute(const FormulaT &constr, const carl::Variable var, const carl::vs::Term< Poly > &term, FormulaT &result)
Definition: VSHelper.h:138
carl::ModelValue< Rational, Poly > ModelValue
Definition: model.h:12
carl::Model< Rational, Poly > Model
Definition: model.h:13
carl::MultivariatePolynomial< Rational > Poly
Definition: types.h:25
mpq_class Rational
Definition: types.h:19