10 RealRootsResult<LPRealAlgebraicNumber>
real_roots(
11 const LPPolynomial& polynomial,
12 const Interval<LPRealAlgebraicNumber::NumberType>& interval) {
13 CARL_LOG_DEBUG(
"carl.ran.libpoly",
" Real roots of " << polynomial <<
" within " << interval);
15 assert(carl::is_univariate(polynomial));
21 CARL_LOG_TRACE(
"carl.ran.libpoly",
"poly is constant but not zero -> no root");
25 lp_upolynomial_t* upoly = lp_polynomial_to_univariate(polynomial.get_internal());
27 lp_algebraic_number_t* roots =
new lp_algebraic_number_t[lp_upolynomial_degree(upoly)];
28 std::size_t roots_size;
29 lp_upolynomial_roots_isolate(upoly, roots, &roots_size);
31 if (roots_size == 0) {
33 lp_upolynomial_delete(upoly);
38 lp_interval_t* inter_poly = to_libpoly_interval(interval);
39 std::vector<LPRealAlgebraicNumber> res;
40 for (std::size_t i = 0; i < roots_size; ++i) {
42 lp_value_construct(&tmp, LP_VALUE_ALGEBRAIC, &roots[i]);
43 if (lp_interval_contains(inter_poly, &tmp)) {
44 res.emplace_back(std::move(tmp));
46 lp_value_destruct(&tmp);
48 lp_algebraic_number_destruct(&roots[i]);
51 lp_interval_destruct(inter_poly);
55 lp_upolynomial_delete(upoly);
57 std::sort(res.begin(), res.end());
61 RealRootsResult<LPRealAlgebraicNumber>
real_roots(
62 const LPPolynomial& polynomial,
63 const std::map<Variable, LPRealAlgebraicNumber>& m,
64 const Interval<LPRealAlgebraicNumber::NumberType>& interval) {
65 CARL_LOG_DEBUG(
"carl.ran.libpoly", polynomial <<
" " << m <<
" " << interval);
67 if (carl::is_univariate(polynomial)) {
76 CARL_LOG_TRACE(
"carl.ran.libpoly",
"poly is constant but not zero -> no root");
86 Variable mainVar = polynomial.main_var();
88 evalMap.erase(mainVar);
89 lp_assignment_t& assignment = LPAssignment::getInstance().get(evalMap);
92 lp_value_t* roots =
new lp_value_t[lp_polynomial_degree(polynomial.get_internal())];
93 std::size_t roots_size;
94 lp_polynomial_roots_isolate(polynomial.get_internal(), &assignment, roots, &roots_size);
97 if (roots_size == 0) {
99 CARL_LOG_DEBUG(
"carl.ran.libpoly",
" Checking for nullification -> Evaluation at " << mainVar <<
"= 1");
102 lp_value_construct_int(&val,
long(1));
103 lp_assignment_set_value(&assignment, LPVariables::getInstance().lp_variable(mainVar), &val);
104 lp_value_destruct(&val);
105 auto eval_val = lp_polynomial_evaluate(polynomial.get_internal(), &assignment);
106 lp_assignment_set_value(&assignment, LPVariables::getInstance().lp_variable(mainVar), 0);
109 lp_value_t zero_value;
110 lp_value_construct_zero(&zero_value);
111 if (lp_value_cmp(eval_val, &zero_value) == 0) {
112 lp_value_destruct(&zero_value);
113 CARL_LOG_DEBUG(
"carl.ran.libpoly",
"poly is 0 after substituting rational assignments -> nullified");
114 lp_value_delete(eval_val);
117 lp_value_destruct(&zero_value);
119 lp_value_delete(eval_val);
124 lp_interval_t* inter_poly = to_libpoly_interval(interval);
125 std::vector<LPRealAlgebraicNumber> res;
126 for (std::size_t i = 0; i < roots_size; ++i) {
127 if (lp_interval_contains(inter_poly, &roots[i])) {
128 res.emplace_back(std::move(roots[i]));
131 lp_value_destruct(&roots[i]);
134 lp_interval_destruct(inter_poly);
138 std::sort(res.begin(), res.end());
#define CARL_LOG_TRACE(channel, msg)
#define CARL_LOG_DEBUG(channel, msg)
carl is the main namespace for the library.
bool is_constant(const ContextPolynomial< Coeff, Ordering, Policies > &p)
RealRootsResult< IntRepRealAlgebraicNumber< Number > > real_roots(const UnivariatePolynomial< Coeff > &polynomial, const Interval< Number > &interval=Interval< Number >::unbounded_interval())
Find all real roots of a univariate 'polynomial' with numeric coefficients within a given 'interval'.
bool is_zero(const Interval< Number > &i)
Check if this interval is a point-interval containing 0.
void variables(const BasicConstraint< Pol > &c, carlVariables &vars)
static RealRootsResult nullified_response()
static RealRootsResult non_univariate_response()
static RealRootsResult roots_response(roots_t &&real_roots)
static RealRootsResult no_roots_response()