blob: 2bb8bc84aaf063a7ee7d5093b9fd46d2015fea49 [file] [log] [blame]
Narayan Kamathc981c482012-11-02 10:59:05 +00001// 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
Carlos Hernandez7faaa9f2014-08-05 17:53:32 -070050 if( numext::abs2( x ) <= Real(1) ){
Narayan Kamathc981c482012-11-02 10:59:05 +000051 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{
Carlos Hernandez7faaa9f2014-08-05 17:53:32 -070077 using std::abs;
Narayan Kamathc981c482012-11-02 10:59:05 +000078 typedef typename Polynomial::Scalar Scalar;
79 typedef typename NumTraits<Scalar>::Real Real;
80
Carlos Hernandez7faaa9f2014-08-05 17:53:32 -070081 eigen_assert( Scalar(0) != poly[poly.size()-1] );
Narayan Kamathc981c482012-11-02 10:59:05 +000082 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 ){
Carlos Hernandez7faaa9f2014-08-05 17:53:32 -070086 cb += abs(poly[i]*inv_leading_coeff); }
Narayan Kamathc981c482012-11-02 10:59:05 +000087 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 */
96template <typename Polynomial>
97inline
98typename NumTraits<typename Polynomial::Scalar>::Real cauchy_min_bound( const Polynomial& poly )
99{
Carlos Hernandez7faaa9f2014-08-05 17:53:32 -0700100 using std::abs;
Narayan Kamathc981c482012-11-02 10:59:05 +0000101 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 ){
Carlos Hernandez7faaa9f2014-08-05 17:53:32 -0700112 cb += abs(poly[j]*inv_min_coeff); }
Narayan Kamathc981c482012-11-02 10:59:05 +0000113 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 */
126template <typename RootVector, typename Polynomial>
127void 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