PolynomialUtils.h
Go to the documentation of this file.
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2010 Manuel Yguel <manuel.yguel@gmail.com>
00005 //
00006 // Eigen is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 3 of the License, or (at your option) any later version.
00010 //
00011 // Alternatively, you can redistribute it and/or
00012 // modify it under the terms of the GNU General Public License as
00013 // published by the Free Software Foundation; either version 2 of
00014 // the License, or (at your option) any later version.
00015 //
00016 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
00017 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00018 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
00019 // GNU General Public License for more details.
00020 //
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License and a copy of the GNU General Public License along with
00023 // Eigen. If not, see <http://www.gnu.org/licenses/>.
00024 
00025 #ifndef EIGEN_POLYNOMIAL_UTILS_H
00026 #define EIGEN_POLYNOMIAL_UTILS_H
00027 
00039 template <typename Polynomials, typename T>
00040 inline
00041 T poly_eval_horner( const Polynomials& poly, const T& x )
00042 {
00043   T val=poly[poly.size()-1];
00044   for(DenseIndex i=poly.size()-2; i>=0; --i ){
00045     val = val*x + poly[i]; }
00046   return val;
00047 }
00048 
00057 template <typename Polynomials, typename T>
00058 inline
00059 T poly_eval( const Polynomials& poly, const T& x )
00060 {
00061   typedef typename NumTraits<T>::Real Real;
00062 
00063   if( internal::abs2( x ) <= Real(1) ){
00064     return poly_eval_horner( poly, x ); }
00065   else
00066   {
00067     T val=poly[0];
00068     T inv_x = T(1)/x;
00069     for( DenseIndex i=1; i<poly.size(); ++i ){
00070       val = val*inv_x + poly[i]; }
00071 
00072     return std::pow(x,(T)(poly.size()-1)) * val;
00073   }
00074 }
00075 
00086 template <typename Polynomial>
00087 inline
00088 typename NumTraits<typename Polynomial::Scalar>::Real cauchy_max_bound( const Polynomial& poly )
00089 {
00090   typedef typename Polynomial::Scalar Scalar;
00091   typedef typename NumTraits<Scalar>::Real Real;
00092 
00093   assert( Scalar(0) != poly[poly.size()-1] );
00094   const Scalar inv_leading_coeff = Scalar(1)/poly[poly.size()-1];
00095   Real cb(0);
00096 
00097   for( DenseIndex i=0; i<poly.size()-1; ++i ){
00098     cb += internal::abs(poly[i]*inv_leading_coeff); }
00099   return cb + Real(1);
00100 }
00101 
00108 template <typename Polynomial>
00109 inline
00110 typename NumTraits<typename Polynomial::Scalar>::Real cauchy_min_bound( const Polynomial& poly )
00111 {
00112   typedef typename Polynomial::Scalar Scalar;
00113   typedef typename NumTraits<Scalar>::Real Real;
00114 
00115   DenseIndex i=0;
00116   while( i<poly.size()-1 && Scalar(0) == poly(i) ){ ++i; }
00117   if( poly.size()-1 == i ){
00118     return Real(1); }
00119 
00120   const Scalar inv_min_coeff = Scalar(1)/poly[i];
00121   Real cb(1);
00122   for( DenseIndex j=i+1; j<poly.size(); ++j ){
00123     cb += internal::abs(poly[j]*inv_min_coeff); }
00124   return Real(1)/cb;
00125 }
00126 
00137 template <typename RootVector, typename Polynomial>
00138 void roots_to_monicPolynomial( const RootVector& rv, Polynomial& poly )
00139 {
00140 
00141   typedef typename Polynomial::Scalar Scalar;
00142 
00143   poly.setZero( rv.size()+1 );
00144   poly[0] = -rv[0]; poly[1] = Scalar(1);
00145   for( DenseIndex i=1; i< rv.size(); ++i )
00146   {
00147     for( DenseIndex j=i+1; j>0; --j ){ poly[j] = poly[j-1] - rv[i]*poly[j]; }
00148     poly[0] = -rv[i]*poly[0];
00149   }
00150 }
00151 
00152 
00153 #endif // EIGEN_POLYNOMIAL_UTILS_H


re_vision
Author(s): Dorian Galvez-Lopez
autogenerated on Sun Jan 5 2014 11:32:09