• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2010 Manuel Yguel <manuel.yguel@gmail.com>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #ifndef EIGEN_POLYNOMIAL_UTILS_H
11 #define EIGEN_POLYNOMIAL_UTILS_H
12 
13 namespace Eigen {
14 
15 /** \ingroup Polynomials_Module
16  * \returns the evaluation of the polynomial at x using Horner algorithm.
17  *
18  * \param[in] poly : the vector of coefficients of the polynomial ordered
19  *  by degrees i.e. poly[i] is the coefficient of degree i of the polynomial
20  *  e.g. \f$ 1 + 3x^2 \f$ is stored as a vector \f$ [ 1, 0, 3 ] \f$.
21  * \param[in] x : the value to evaluate the polynomial at.
22  *
23  * \note for stability:
24  *   \f$ |x| \le 1 \f$
25  */
26 template <typename Polynomials, typename T>
27 inline
poly_eval_horner(const Polynomials & poly,const T & x)28 T poly_eval_horner( const Polynomials& poly, const T& x )
29 {
30   T val=poly[poly.size()-1];
31   for(DenseIndex i=poly.size()-2; i>=0; --i ){
32     val = val*x + poly[i]; }
33   return val;
34 }
35 
36 /** \ingroup Polynomials_Module
37  * \returns the evaluation of the polynomial at x using stabilized Horner algorithm.
38  *
39  * \param[in] poly : the vector of coefficients of the polynomial ordered
40  *  by degrees i.e. poly[i] is the coefficient of degree i of the polynomial
41  *  e.g. \f$ 1 + 3x^2 \f$ is stored as a vector \f$ [ 1, 0, 3 ] \f$.
42  * \param[in] x : the value to evaluate the polynomial at.
43  */
44 template <typename Polynomials, typename T>
45 inline
poly_eval(const Polynomials & poly,const T & x)46 T poly_eval( const Polynomials& poly, const T& x )
47 {
48   typedef typename NumTraits<T>::Real Real;
49 
50   if( numext::abs2( x ) <= Real(1) ){
51     return poly_eval_horner( poly, x ); }
52   else
53   {
54     T val=poly[0];
55     T inv_x = T(1)/x;
56     for( DenseIndex i=1; i<poly.size(); ++i ){
57       val = val*inv_x + poly[i]; }
58 
59     return numext::pow(x,(T)(poly.size()-1)) * val;
60   }
61 }
62 
63 /** \ingroup Polynomials_Module
64  * \returns a maximum bound for the absolute value of any root of the polynomial.
65  *
66  * \param[in] poly : the vector of coefficients of the polynomial ordered
67  *  by degrees i.e. poly[i] is the coefficient of degree i of the polynomial
68  *  e.g. \f$ 1 + 3x^2 \f$ is stored as a vector \f$ [ 1, 0, 3 ] \f$.
69  *
70  *  \pre
71  *   the leading coefficient of the input polynomial poly must be non zero
72  */
73 template <typename Polynomial>
74 inline
cauchy_max_bound(const Polynomial & poly)75 typename NumTraits<typename Polynomial::Scalar>::Real cauchy_max_bound( const Polynomial& poly )
76 {
77   using std::abs;
78   typedef typename Polynomial::Scalar Scalar;
79   typedef typename NumTraits<Scalar>::Real Real;
80 
81   eigen_assert( Scalar(0) != poly[poly.size()-1] );
82   const Scalar inv_leading_coeff = Scalar(1)/poly[poly.size()-1];
83   Real cb(0);
84 
85   for( DenseIndex i=0; i<poly.size()-1; ++i ){
86     cb += abs(poly[i]*inv_leading_coeff); }
87   return cb + Real(1);
88 }
89 
90 /** \ingroup Polynomials_Module
91  * \returns a minimum bound for the absolute value of any non zero root of the polynomial.
92  * \param[in] poly : the vector of coefficients of the polynomial ordered
93  *  by degrees i.e. poly[i] is the coefficient of degree i of the polynomial
94  *  e.g. \f$ 1 + 3x^2 \f$ is stored as a vector \f$ [ 1, 0, 3 ] \f$.
95  */
96 template <typename Polynomial>
97 inline
cauchy_min_bound(const Polynomial & poly)98 typename NumTraits<typename Polynomial::Scalar>::Real cauchy_min_bound( const Polynomial& poly )
99 {
100   using std::abs;
101   typedef typename Polynomial::Scalar Scalar;
102   typedef typename NumTraits<Scalar>::Real Real;
103 
104   DenseIndex i=0;
105   while( i<poly.size()-1 && Scalar(0) == poly(i) ){ ++i; }
106   if( poly.size()-1 == i ){
107     return Real(1); }
108 
109   const Scalar inv_min_coeff = Scalar(1)/poly[i];
110   Real cb(1);
111   for( DenseIndex j=i+1; j<poly.size(); ++j ){
112     cb += abs(poly[j]*inv_min_coeff); }
113   return Real(1)/cb;
114 }
115 
116 /** \ingroup Polynomials_Module
117  * Given the roots of a polynomial compute the coefficients in the
118  * monomial basis of the monic polynomial with same roots and minimal degree.
119  * If RootVector is a vector of complexes, Polynomial should also be a vector
120  * of complexes.
121  * \param[in] rv : a vector containing the roots of a polynomial.
122  * \param[out] poly : the vector of coefficients of the polynomial ordered
123  *  by degrees i.e. poly[i] is the coefficient of degree i of the polynomial
124  *  e.g. \f$ 3 + x^2 \f$ is stored as a vector \f$ [ 3, 0, 1 ] \f$.
125  */
126 template <typename RootVector, typename Polynomial>
roots_to_monicPolynomial(const RootVector & rv,Polynomial & poly)127 void roots_to_monicPolynomial( const RootVector& rv, Polynomial& poly )
128 {
129 
130   typedef typename Polynomial::Scalar Scalar;
131 
132   poly.setZero( rv.size()+1 );
133   poly[0] = -rv[0]; poly[1] = Scalar(1);
134   for( DenseIndex i=1; i< rv.size(); ++i )
135   {
136     for( DenseIndex j=i+1; j>0; --j ){ poly[j] = poly[j-1] - rv[i]*poly[j]; }
137     poly[0] = -rv[i]*poly[0];
138   }
139 }
140 
141 } // end namespace Eigen
142 
143 #endif // EIGEN_POLYNOMIAL_UTILS_H
144