PolynomialUtils.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. #ifndef EIGEN_POLYNOMIAL_UTILS_H
  10. #define EIGEN_POLYNOMIAL_UTILS_H
  11. namespace Eigen {
  12. /** \ingroup Polynomials_Module
  13. * \returns the evaluation of the polynomial at x using Horner algorithm.
  14. *
  15. * \param[in] poly : the vector of coefficients of the polynomial ordered
  16. * by degrees i.e. poly[i] is the coefficient of degree i of the polynomial
  17. * e.g. \f$ 1 + 3x^2 \f$ is stored as a vector \f$ [ 1, 0, 3 ] \f$.
  18. * \param[in] x : the value to evaluate the polynomial at.
  19. *
  20. * \note for stability:
  21. * \f$ |x| \le 1 \f$
  22. */
  23. template <typename Polynomials, typename T>
  24. inline
  25. T poly_eval_horner( const Polynomials& poly, const T& x )
  26. {
  27. T val=poly[poly.size()-1];
  28. for(DenseIndex i=poly.size()-2; i>=0; --i ){
  29. val = val*x + poly[i]; }
  30. return val;
  31. }
  32. /** \ingroup Polynomials_Module
  33. * \returns the evaluation of the polynomial at x using stabilized Horner algorithm.
  34. *
  35. * \param[in] poly : the vector of coefficients of the polynomial ordered
  36. * by degrees i.e. poly[i] is the coefficient of degree i of the polynomial
  37. * e.g. \f$ 1 + 3x^2 \f$ is stored as a vector \f$ [ 1, 0, 3 ] \f$.
  38. * \param[in] x : the value to evaluate the polynomial at.
  39. */
  40. template <typename Polynomials, typename T>
  41. inline
  42. T poly_eval( const Polynomials& poly, const T& x )
  43. {
  44. typedef typename NumTraits<T>::Real Real;
  45. if( numext::abs2( x ) <= Real(1) ){
  46. return poly_eval_horner( poly, x ); }
  47. else
  48. {
  49. T val=poly[0];
  50. T inv_x = T(1)/x;
  51. for( DenseIndex i=1; i<poly.size(); ++i ){
  52. val = val*inv_x + poly[i]; }
  53. return numext::pow(x,(T)(poly.size()-1)) * val;
  54. }
  55. }
  56. /** \ingroup Polynomials_Module
  57. * \returns a maximum bound for the absolute value of any root of the polynomial.
  58. *
  59. * \param[in] poly : the vector of coefficients of the polynomial ordered
  60. * by degrees i.e. poly[i] is the coefficient of degree i of the polynomial
  61. * e.g. \f$ 1 + 3x^2 \f$ is stored as a vector \f$ [ 1, 0, 3 ] \f$.
  62. *
  63. * \pre
  64. * the leading coefficient of the input polynomial poly must be non zero
  65. */
  66. template <typename Polynomial>
  67. inline
  68. typename NumTraits<typename Polynomial::Scalar>::Real cauchy_max_bound( const Polynomial& poly )
  69. {
  70. using std::abs;
  71. typedef typename Polynomial::Scalar Scalar;
  72. typedef typename NumTraits<Scalar>::Real Real;
  73. eigen_assert( Scalar(0) != poly[poly.size()-1] );
  74. const Scalar inv_leading_coeff = Scalar(1)/poly[poly.size()-1];
  75. Real cb(0);
  76. for( DenseIndex i=0; i<poly.size()-1; ++i ){
  77. cb += abs(poly[i]*inv_leading_coeff); }
  78. return cb + Real(1);
  79. }
  80. /** \ingroup Polynomials_Module
  81. * \returns a minimum bound for the absolute value of any non zero root of the polynomial.
  82. * \param[in] poly : the vector of coefficients of the polynomial ordered
  83. * by degrees i.e. poly[i] is the coefficient of degree i of the polynomial
  84. * e.g. \f$ 1 + 3x^2 \f$ is stored as a vector \f$ [ 1, 0, 3 ] \f$.
  85. */
  86. template <typename Polynomial>
  87. inline
  88. typename NumTraits<typename Polynomial::Scalar>::Real cauchy_min_bound( const Polynomial& poly )
  89. {
  90. using std::abs;
  91. typedef typename Polynomial::Scalar Scalar;
  92. typedef typename NumTraits<Scalar>::Real Real;
  93. DenseIndex i=0;
  94. while( i<poly.size()-1 && Scalar(0) == poly(i) ){ ++i; }
  95. if( poly.size()-1 == i ){
  96. return Real(1); }
  97. const Scalar inv_min_coeff = Scalar(1)/poly[i];
  98. Real cb(1);
  99. for( DenseIndex j=i+1; j<poly.size(); ++j ){
  100. cb += abs(poly[j]*inv_min_coeff); }
  101. return Real(1)/cb;
  102. }
  103. /** \ingroup Polynomials_Module
  104. * Given the roots of a polynomial compute the coefficients in the
  105. * monomial basis of the monic polynomial with same roots and minimal degree.
  106. * If RootVector is a vector of complexes, Polynomial should also be a vector
  107. * of complexes.
  108. * \param[in] rv : a vector containing the roots of a polynomial.
  109. * \param[out] poly : the vector of coefficients of the polynomial ordered
  110. * by degrees i.e. poly[i] is the coefficient of degree i of the polynomial
  111. * e.g. \f$ 3 + x^2 \f$ is stored as a vector \f$ [ 3, 0, 1 ] \f$.
  112. */
  113. template <typename RootVector, typename Polynomial>
  114. void roots_to_monicPolynomial( const RootVector& rv, Polynomial& poly )
  115. {
  116. typedef typename Polynomial::Scalar Scalar;
  117. poly.setZero( rv.size()+1 );
  118. poly[0] = -rv[0]; poly[1] = Scalar(1);
  119. for( DenseIndex i=1; i< rv.size(); ++i )
  120. {
  121. for( DenseIndex j=i+1; j>0; --j ){ poly[j] = poly[j-1] - rv[i]*poly[j]; }
  122. poly[0] = -rv[i]*poly[0];
  123. }
  124. }
  125. } // end namespace Eigen
  126. #endif // EIGEN_POLYNOMIAL_UTILS_H