carl  24.04
Computer ARithmetic Library
RootCounting.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "Evaluation.h"
4 #include "SturmSequence.h"
5 #include <carl-arith/core/Sign.h>
6 #include "../UnivariatePolynomial.h"
7 
8 #include <cassert>
9 
10 namespace carl {
11 
12 /**
13  * Calculate the number of real roots of a polynomial within a given interval based on a sturm sequence of this polynomial.
14  * @param seq Sturm sequence.
15  * @param i Interval.
16  * @return Number of real roots in the interval.
17  */
18 template<typename Coefficient>
20  int l = static_cast<int>(carl::sign_variations(seq.begin(), seq.end(),
21  [&i](const auto& p){ return carl::sgn(carl::evaluate(p, i.lower())); }
22  ));
23  int r = static_cast<int>(carl::sign_variations(seq.begin(), seq.end(),
24  [&i](const auto& p){ return carl::sgn(carl::evaluate(p, i.upper())); }
25  ));
26  return l - r;
27 }
28 
29 /**
30  * Count the number of real roots of p within the given interval using Sturm sequences.
31  * @param p The polynomial.
32  * @param i Count roots within this interval.
33  * @return Number of real roots within the interval.
34  */
35 template<typename Coefficient>
37  assert(!is_zero(p));
38  assert(!carl::is_root_of(p, i.lower()));
39  assert(!carl::is_root_of(p, i.upper()));
40  return count_real_roots(sturm_sequence(p), i);
41 }
42 
43 }
carl is the main namespace for the library.
std::vector< UnivariatePolynomial< Coeff > > sturm_sequence(const UnivariatePolynomial< Coeff > &p, const UnivariatePolynomial< Coeff > &q)
Computes the sturm sequence of two polynomials.
Definition: SturmSequence.h:16
int count_real_roots(const std::vector< UnivariatePolynomial< Coefficient >> &seq, const Interval< Coefficient > &i)
Calculate the number of real roots of a polynomial within a given interval based on a sturm sequence ...
Definition: RootCounting.h:19
std::size_t sign_variations(InputIterator begin, InputIterator end)
Counts the number of sign variations in the given object range.
Definition: Sign.h:69
bool is_zero(const Interval< Number > &i)
Check if this interval is a point-interval containing 0.
Definition: Interval.h:1453
bool is_root_of(const UnivariatePolynomial< Coeff > &p, const Coeff &value)
Definition: Evaluation.h:62
The class which contains the interval arithmetic including trigonometric functions.
Definition: Interval.h:134
const Number & upper() const
The getter for the upper boundary of the interval.
Definition: Interval.h:849
const Number & lower() const
The getter for the lower boundary of the interval.
Definition: Interval.h:840
This class represents a univariate polynomial with coefficients of an arbitrary type.