PolynomialUtils.h
Go to the documentation of this file.
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 #include "./InternalHeaderCheck.h"
14 
15 namespace Eigen {
16 
28 template <typename Polynomials, typename T>
29 inline
30 T poly_eval_horner( const Polynomials& poly, const T& x )
31 {
32  T val=poly[poly.size()-1];
33  for(DenseIndex i=poly.size()-2; i>=0; --i ){
34  val = val*x + poly[i]; }
35  return val;
36 }
37 
46 template <typename Polynomials, typename T>
47 inline
48 T poly_eval( const Polynomials& poly, const T& x )
49 {
50  typedef typename NumTraits<T>::Real Real;
51 
52  if( numext::abs2( x ) <= Real(1) ){
53  return poly_eval_horner( poly, x ); }
54  else
55  {
56  T val=poly[0];
57  T inv_x = T(1)/x;
58  for( DenseIndex i=1; i<poly.size(); ++i ){
59  val = val*inv_x + poly[i]; }
60 
61  return numext::pow(x,(T)(poly.size()-1)) * val;
62  }
63 }
64 
75 template <typename Polynomial>
76 inline
78 {
79  using std::abs;
80  typedef typename Polynomial::Scalar Scalar;
81  typedef typename NumTraits<Scalar>::Real Real;
82 
83  eigen_assert( Scalar(0) != poly[poly.size()-1] );
84  const Scalar inv_leading_coeff = Scalar(1)/poly[poly.size()-1];
85  Real cb(0);
86 
87  for( DenseIndex i=0; i<poly.size()-1; ++i ){
88  cb += abs(poly[i]*inv_leading_coeff); }
89  return cb + Real(1);
90 }
91 
98 template <typename Polynomial>
99 inline
101 {
102  using std::abs;
103  typedef typename Polynomial::Scalar Scalar;
104  typedef typename NumTraits<Scalar>::Real Real;
105 
106  DenseIndex i=0;
107  while( i<poly.size()-1 && Scalar(0) == poly(i) ){ ++i; }
108  if( poly.size()-1 == i ){
109  return Real(1); }
110 
111  const Scalar inv_min_coeff = Scalar(1)/poly[i];
112  Real cb(1);
113  for( DenseIndex j=i+1; j<poly.size(); ++j ){
114  cb += abs(poly[j]*inv_min_coeff); }
115  return Real(1)/cb;
116 }
117 
128 template <typename RootVector, typename Polynomial>
129 void roots_to_monicPolynomial( const RootVector& rv, Polynomial& poly )
130 {
131 
132  typedef typename Polynomial::Scalar Scalar;
133 
134  poly.setZero( rv.size()+1 );
135  poly[0] = -rv[0]; poly[1] = Scalar(1);
136  for( DenseIndex i=1; i< rv.size(); ++i )
137  {
138  for( DenseIndex j=i+1; j>0; --j ){ poly[j] = poly[j-1] - rv[i]*poly[j]; }
139  poly[0] = -rv[i]*poly[0];
140  }
141 }
142 
143 } // end namespace Eigen
144 
145 #endif // EIGEN_POLYNOMIAL_UTILS_H
int i
#define eigen_assert(x)
NumTraits< typename Polynomial::Scalar >::Real cauchy_max_bound(const Polynomial &poly)
T poly_eval_horner(const Polynomials &poly, const T &x)
NumTraits< typename Polynomial::Scalar >::Real cauchy_min_bound(const Polynomial &poly)
T poly_eval(const Polynomials &poly, const T &x)
void roots_to_monicPolynomial(const RootVector &rv, Polynomial &poly)
bool abs2(bool x)
internal::pow_impl< ScalarX, ScalarY >::result_type pow(const ScalarX &x, const ScalarY &y)
: TensorContractionSycl.h, provides various tensor contraction kernel for SYCL backend
EIGEN_DEFAULT_DENSE_INDEX_TYPE DenseIndex
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_abs_op< typename Derived::Scalar >, const Derived > abs(const Eigen::ArrayBase< Derived > &x)
adouble abs(const adouble &x)
Definition: AdolcForward:74
std::ptrdiff_t j