PolynomialUtils.h revision c981c48f5bc9aefeffc0bcb0cc3934c2fae179dd
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 13namespace 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 * <i><b>Note for stability:</b></i> 24 * <dd> \f$ |x| \le 1 \f$ </dd> 25 */ 26template <typename Polynomials, typename T> 27inline 28T 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 */ 44template <typename Polynomials, typename T> 45inline 46T poly_eval( const Polynomials& poly, const T& x ) 47{ 48 typedef typename NumTraits<T>::Real Real; 49 50 if( internal::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 std::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 * <i><b>Precondition:</b></i> 71 * <dd> the leading coefficient of the input polynomial poly must be non zero </dd> 72 */ 73template <typename Polynomial> 74inline 75typename NumTraits<typename Polynomial::Scalar>::Real cauchy_max_bound( const Polynomial& poly ) 76{ 77 typedef typename Polynomial::Scalar Scalar; 78 typedef typename NumTraits<Scalar>::Real Real; 79 80 assert( Scalar(0) != poly[poly.size()-1] ); 81 const Scalar inv_leading_coeff = Scalar(1)/poly[poly.size()-1]; 82 Real cb(0); 83 84 for( DenseIndex i=0; i<poly.size()-1; ++i ){ 85 cb += internal::abs(poly[i]*inv_leading_coeff); } 86 return cb + Real(1); 87} 88 89/** \ingroup Polynomials_Module 90 * \returns a minimum bound for the absolute value of any non zero root of the polynomial. 91 * \param[in] poly : the vector of coefficients of the polynomial ordered 92 * by degrees i.e. poly[i] is the coefficient of degree i of the polynomial 93 * e.g. \f$ 1 + 3x^2 \f$ is stored as a vector \f$ [ 1, 0, 3 ] \f$. 94 */ 95template <typename Polynomial> 96inline 97typename NumTraits<typename Polynomial::Scalar>::Real cauchy_min_bound( const Polynomial& poly ) 98{ 99 typedef typename Polynomial::Scalar Scalar; 100 typedef typename NumTraits<Scalar>::Real Real; 101 102 DenseIndex i=0; 103 while( i<poly.size()-1 && Scalar(0) == poly(i) ){ ++i; } 104 if( poly.size()-1 == i ){ 105 return Real(1); } 106 107 const Scalar inv_min_coeff = Scalar(1)/poly[i]; 108 Real cb(1); 109 for( DenseIndex j=i+1; j<poly.size(); ++j ){ 110 cb += internal::abs(poly[j]*inv_min_coeff); } 111 return Real(1)/cb; 112} 113 114/** \ingroup Polynomials_Module 115 * Given the roots of a polynomial compute the coefficients in the 116 * monomial basis of the monic polynomial with same roots and minimal degree. 117 * If RootVector is a vector of complexes, Polynomial should also be a vector 118 * of complexes. 119 * \param[in] rv : a vector containing the roots of a polynomial. 120 * \param[out] poly : the vector of coefficients of the polynomial ordered 121 * by degrees i.e. poly[i] is the coefficient of degree i of the polynomial 122 * e.g. \f$ 3 + x^2 \f$ is stored as a vector \f$ [ 3, 0, 1 ] \f$. 123 */ 124template <typename RootVector, typename Polynomial> 125void roots_to_monicPolynomial( const RootVector& rv, Polynomial& poly ) 126{ 127 128 typedef typename Polynomial::Scalar Scalar; 129 130 poly.setZero( rv.size()+1 ); 131 poly[0] = -rv[0]; poly[1] = Scalar(1); 132 for( DenseIndex i=1; i< rv.size(); ++i ) 133 { 134 for( DenseIndex j=i+1; j>0; --j ){ poly[j] = poly[j-1] - rv[i]*poly[j]; } 135 poly[0] = -rv[i]*poly[0]; 136 } 137} 138 139} // end namespace Eigen 140 141#endif // EIGEN_POLYNOMIAL_UTILS_H 142