26#ifndef UTILS_POLY_dot_HH
27#define UTILS_POLY_dot_HH
29#ifndef DOXYGEN_SHOULD_SKIP_THIS
106 template <
typename Real>
107 class Poly :
public Eigen::Matrix<Real,Eigen::Dynamic,1> {
110 using dvec_t = Eigen::Matrix<Real,Eigen::Dynamic,1>;
115 dvec_t & to_eigen() {
return *
static_cast<dvec_t*
>(
this); }
141 : m_order( c.m_order )
159 this->resize( c.size() );
162 this->dvec_t::operator = (c);
179 {
return *
static_cast<dvec_t const *
>(
this); }
206 this->resize(
order );
235 this->resize( m_order );
491 void eval( Real x, Real & p, Real & Dp )
const;
703 this->to_eigen() /= this->coeff(m_order-1);
704 this->coeffRef(m_order-1) = 1;
904 template <
typename Real>
909 using dvec_t = Eigen::Matrix<Real,Eigen::Dynamic,1>;
920 #ifndef DOXYGEN_SHOULD_SKIP_THIS
924 void setup(
Poly<Real> const * Pin ) { P = Pin; }
925 Real eval( Real x )
const override {
return P->
eval(x); }
931 AlgoBracket<Real> m_solver;
934 vector<Poly_t> m_sturm;
935 vector<Interval> m_intervals;
1128 Real
a()
const {
return m_a; }
1138 Real
b()
const {
return m_b; }
1196 #ifndef DOXYGEN_SHOULD_SKIP_THIS
1201 template <
typename Real>
1202 Poly<Real> operator + ( Poly<Real>
const & a, Poly<Real>
const & b );
1207 template <
typename Real>
1208 Poly<Real> operator + ( Poly<Real>
const & a, Real b );
1213 template <
typename Real>
1214 Poly<Real> operator + ( Real a, Poly<Real>
const & b );
1219 template <
typename Real>
1220 Poly<Real> operator - ( Poly<Real>
const & a, Poly<Real>
const & b );
1225 template <
typename Real>
1226 Poly<Real> operator - ( Poly<Real>
const & a, Real b );
1231 template <
typename Real>
1232 Poly<Real> operator - ( Real a, Poly<Real>
const & b );
1237 template <
typename Real>
1238 Poly<Real>
operator * ( Poly<Real>
const & a, Poly<Real>
const & b );
1243 template <
typename Real>
1244 Poly<Real>
operator * ( Real a, Poly<Real>
const & b );
1249 template <
typename Real>
1250 Poly<Real>
operator * ( Poly<Real>
const & a, Real b );
1256 template <
typename Real>
1258 Poly<Real>
const & p,
1259 Poly<Real>
const & q,
1267 template <
typename Real>
1270 Poly<Real>
const & p,
1271 Poly<Real>
const & q,
1281 template <
typename Real>
1286 if ( this->
order() <= 0 )
return "EMPTY!";
1287 if ( this->
order() == 1 )
return fmt::format(
"{}", this->coeff(0) );
1288 if ( (*this).cwiseAbs().maxCoeff() == 0 )
return "0";
1297 if ( this->coeff(0) != 0 ) {
1298 res = fmt::format(
"{}", this->coeff(0) );
1304 if ( this->coeff(i) < 0 ) {
1312 c = -this->coeff(i);
1316 }
else if ( this->coeff(i) > 0 ) {
1332 if ( i == 1 ) e =
"x";
1333 else e = fmt::format(
"x^{}", i );
1336 if ( c == 1 ) { res += s; res += e; }
1337 else res += fmt::format(
"{}{} {}", s, c, e );
1345 template <
typename Real,
typename Char>
1347 std::basic_ostream<Char> &
1356 template <
typename Real,
typename Char>
1358 std::basic_ostream<Char> &
1359 operator << ( std::basic_ostream<Char> & output, Sturm<Real>
const & S ) {
1361 output <<
"Sturm sequence\n";
1362 for ( Integer i = 0; i < S.length(); ++i )
1363 output <<
"P_" << i <<
"(x) = " << S.get(i) <<
'\n';
1365 Integer n = S.n_roots();
1367 fmt::print( output,
"roots separation for interval [{},{}]\n", S.a(), S.b() );
1368 for ( Integer i = 0; i < n; ++i ) {
1370 fmt::print( output,
"I = [{}, {}], V = [{}, {}]\n", I.a, I.b, I.va, I.vb );
1380 #ifndef UTILS_OS_WINDOWS
1389#ifndef DOXYGEN_SHOULD_SKIP_THIS
1392 template <
typename Real>
struct formatter<Utils::Poly<Real>> : ostream_formatter {};
1393 template <
typename Real>
struct formatter<
Utils::Sturm<Real>> : ostream_formatter {};
Abstract base class for defining mathematical functions used in the Bracket search algorithm.
Definition Utils_AlgoBracket.hh:83
Specializes the Eigen::Matrix class to represent and manipulate polynomials.
Definition Utils_Poly.hh:107
Poly(Poly_t const &c)
Definition Utils_Poly.hh:140
dvec_t const & to_eigen() const
Definition Utils_Poly.hh:178
void integral(Poly_t &result) const
Poly_t & operator-=(Poly_t const &q)
void set_degree(Integer degree)
Definition Utils_Poly.hh:233
Integer sign_variations() const
Real eval_D(Real x) const
Poly_t & operator*=(Poly_t const &q)
Poly_t & operator+=(Poly_t const &q)
Poly()
Definition Utils_Poly.hh:119
Poly(dvec_t const &c)
Definition Utils_Poly.hh:158
Poly_t & set_scalar(Real a)
void make_monic()
Definition Utils_Poly.hh:702
Poly< Real > Poly_t
Definition Utils_Poly.hh:111
void set_order(Integer order)
Definition Utils_Poly.hh:204
Real leading_coeff() const
Definition Utils_Poly.hh:513
Integer degree() const
Definition Utils_Poly.hh:325
Poly_t & set_monomial(Real a)
Poly_t & operator=(Poly_t const &c)
int Integer
Definition Utils_Poly.hh:109
Integer order() const
Definition Utils_Poly.hh:345
Poly(int order)
Definition Utils_Poly.hh:129
void derivative(Poly_t &result) const
string to_string() const
Definition Utils_Poly.hh:1284
dvec_t coeffs() const
Definition Utils_Poly.hh:306
Poly_t operator-()
Definition Utils_Poly.hh:746
Eigen::Matrix< Real, Eigen::Dynamic, 1 > dvec_t
Definition Utils_Poly.hh:110
void eval(Real x, Real &p, Real &Dp) const
Class for managing and computing the Sturm sequence associated with a polynomial.
Definition Utils_Poly.hh:905
struct Interval { Real a; Real b; Integer va; Integer vb; bool a_on_root; bool b_on_root; } Interval
Definition Utils_Poly.hh:911
Integer separate_roots(Real a, Real b)
Computes subintervals containing single roots within a given interval .
Integer sign_variations(Real x, bool &on_root) const
Computes the sign variations of the stored Sturm sequence at a given point .
Poly_t const & get(Integer i) const
Retrieves the i-th polynomial of the stored Sturm sequence.
Definition Utils_Poly.hh:1007
Poly< Real > Poly_t
Definition Utils_Poly.hh:908
Interval const & get_interval(Integer i) const
Returns the i-th interval containing a single root.
Definition Utils_Poly.hh:1150
Integer separate_roots()
Compute an interval that contains all real roots and separate them into subintervals.
Integer n_roots() const
Returns the number of roots found by the Sturm sequence.
Definition Utils_Poly.hh:1118
Integer length() const
Retrieves the length of the stored Sturm sequence.
Definition Utils_Poly.hh:974
dvec_t const & roots() const
Returns a vector containing the computed roots after refinement.
Definition Utils_Poly.hh:1192
void build(Poly_t const &p)
Constructs the Sturm sequence for a given polynomial.
Real a() const
Returns the left boundary of the interval containing all real roots.
Definition Utils_Poly.hh:1128
Eigen::Matrix< Real, Eigen::Dynamic, 1 > dvec_t
Definition Utils_Poly.hh:909
Real b() const
Returns the right boundary of the interval containing all real roots.
Definition Utils_Poly.hh:1138
Sturm()
Definition Utils_Poly.hh:942
void refine_roots()
Refine the roots of the polynomial after the intervals have been separated.
int Integer
Definition Utils_Poly.hh:907
Definition SystemUtils.cc:39
Quaternion< T > operator*(Quaternion< T > const &a, Quaternion< T > const &b)
Definition Quaternion.hxx:298
ostream_type & operator<<(ostream_type &os, Quaternion< T > const &Q)
Definition Quaternion.hxx:314