 Eigen-unsupported  3.3.7 Polynomials module

## Detailed Description

This module provides a QR based polynomial solver.

#include <unsupported/Eigen/Polynomials>

at the start of your source file.

## Classes

class  Eigen::PolynomialSolver< _Scalar, _Deg >
A polynomial solver. More...

class  Eigen::PolynomialSolverBase< _Scalar, _Deg >
Defined to be inherited by polynomial solvers: it provides convenient methods such as. More...

## Functions

template<typename Polynomial >
NumTraits< typename Polynomial::Scalar >::Real Eigen::cauchy_max_bound (const Polynomial &poly)

template<typename Polynomial >
NumTraits< typename Polynomial::Scalar >::Real Eigen::cauchy_min_bound (const Polynomial &poly)

template<typename Polynomials , typename T >
Eigen::poly_eval (const Polynomials &poly, const T &x)

template<typename Polynomials , typename T >
Eigen::poly_eval_horner (const Polynomials &poly, const T &x)

template<typename RootVector , typename Polynomial >
void Eigen::roots_to_monicPolynomial (const RootVector &rv, Polynomial &poly)

### Polynomials defines functions for dealing with polynomials

and a QR based polynomial solver.
  The remainder of the page documents first the functions for evaluating, computing
polynomials, computing estimates about polynomials and next the QR based polynomial
solver.


# convenient functions to deal with polynomials

## roots_to_monicPolynomial

The function

void roots_to_monicPolynomial( const RootVector& rv, Polynomial& poly )

computes the coefficients of where \form#44 is known through its roots i.e. \form#45.


## poly_eval

The function

T poly_eval( const Polynomials& poly, const T& x )

evaluates a polynomial at a given point using stabilized Hörner method.

The following code: first computes the coefficients in the monomial basis of the monic polynomial that has the provided roots; then, it evaluates the computed polynomial, using a stabilized Hörner method.

#include <unsupported/Eigen/Polynomials>
#include <iostream>
using namespace Eigen;
using namespace std;
int main()
{
cout << "Roots: " << roots.transpose() << endl;
roots_to_monicPolynomial( roots, polynomial );
cout << "Polynomial: ";
for( int i=0; i<4; ++i ){ cout << polynomial[i] << ".x^" << i << "+ "; }
cout << polynomial << ".x^4" << endl;
Vector4d evaluation;
for( int i=0; i<4; ++i ){
evaluation[i] = poly_eval( polynomial, roots[i] ); }
cout << "Evaluation of the polynomial at the roots: " << evaluation.transpose();
}

Output:

Roots:  0.680375 -0.211234  0.566198   0.59688
Polynomial: -0.04857.x^0+ 0.00860842.x^1+ 0.739882.x^2+ -1.63222.x^3+ 1.x^4
Evaluation of the polynomial at the roots: -2.08167e-17            0            0  2.08167e-17

## bounds

The function

Real cauchy_max_bound( const Polynomial& poly )

provides a maximum bound (the Cauchy one: ) for the absolute value of a root of the given polynomial i.e. root of , The leading coefficient : should be non zero .

  The function

Real cauchy_min_bound( const Polynomial& poly )

provides a minimum bound (the Cauchy one: ) for the absolute value of a non zero root of the given polynomial i.e. root of , # polynomial solver class

Computes the complex roots of a polynomial by computing the eigenvalues of the associated companion matrix with the QR algorithm.

The roots of are the eigenvalues of However, the QR algorithm is not guaranteed to converge when there are several eigenvalues with same modulus.

Therefore the current polynomial solver is guaranteed to provide a correct result only when the complex roots \form#56 have distinct moduli i.e. .

 With 32bit (float) floating types this problem shows up frequently.


However, almost always, correct accuracy is reached even in these cases for 64bit (double) floating types and small polynomial degree (<20).

  \include PolynomialSolver1.cpp

In the above example:

-# a simple use of the polynomial solver is shown;
-# the accuracy problem with the QR algorithm is presented: a polynomial with almost conjugate roots is provided to the solver.
Those roots have almost same module therefore the QR algorithm failed to converge: the accuracy
of the last root is bad;
-# a simple way to circumvent the problem is shown: use doubles instead of floats.


Output:

Roots:  0.680375 -0.211234  0.566198   0.59688  0.823295
Complex roots: (-0.211234,0)  (0.566198,0)   (0.59688,0)  (0.680375,0)  (0.823295,0)
Real roots: -0.211234  0.566198   0.59688  0.680375  0.823295

Illustration of the convergence problem with the QR algorithm:
---------------------------------------------------------------
Hard case polynomial defined by floats:   -0.957   0.9219   0.3516   0.9453  -0.4023  -0.5508 -0.03125
Complex roots:           (1.19707,0)           (0.70514,0)           (-1.9834,0)  (-0.396563,0.966801) (-0.396563,-0.966801)          (-16.7513,0)
Norms of the evaluations of the polynomial at the roots: 3.08019e-06 2.98023e-07 2.10915e-05 5.35758e-07 5.35758e-07           0

Using double's almost always solves the problem for small degrees:
-------------------------------------------------------------------
Complex roots:           (1.19707,0)           (0.70514,0)           (-1.9834,0)  (-0.396564,0.966801) (-0.396564,-0.966801)          (-16.7513,0)
Norms of the evaluations of the polynomial at the roots: 3.78175e-07           0  2.0411e-06 2.48518e-07 2.48518e-07           0

The last root in float then in double: (-16.75128174,0)	(-16.75128099,0)
Norm of the difference: 0


## ◆ cauchy_max_bound()

template<typename Polynomial >
 NumTraits::Real Eigen::cauchy_max_bound ( const Polynomial & poly )
inline
Returns
a maximum bound for the absolute value of any root of the polynomial.
Parameters
 [in] poly : the vector of coefficients of the polynomial ordered by degrees i.e. poly[i] is the coefficient of degree i of the polynomial e.g. is stored as a vector .
Precondition
the leading coefficient of the input polynomial poly must be non zero

## ◆ cauchy_min_bound()

template<typename Polynomial >
 NumTraits::Real Eigen::cauchy_min_bound ( const Polynomial & poly )
inline
Returns
a minimum bound for the absolute value of any non zero root of the polynomial.
Parameters
 [in] poly : the vector of coefficients of the polynomial ordered by degrees i.e. poly[i] is the coefficient of degree i of the polynomial e.g. is stored as a vector .

## ◆ poly_eval()

template<typename Polynomials , typename T >
 T Eigen::poly_eval ( const Polynomials & poly, const T & x )
inline
Returns
the evaluation of the polynomial at x using stabilized Horner algorithm.
Parameters
 [in] poly : the vector of coefficients of the polynomial ordered by degrees i.e. poly[i] is the coefficient of degree i of the polynomial e.g. is stored as a vector . [in] x : the value to evaluate the polynomial at.

## ◆ poly_eval_horner()

template<typename Polynomials , typename T >
 T Eigen::poly_eval_horner ( const Polynomials & poly, const T & x )
inline
Returns
the evaluation of the polynomial at x using Horner algorithm.
Parameters
 [in] poly : the vector of coefficients of the polynomial ordered by degrees i.e. poly[i] is the coefficient of degree i of the polynomial e.g. is stored as a vector . [in] x : the value to evaluate the polynomial at.
Note
for stability: ## ◆ roots_to_monicPolynomial()

template<typename RootVector , typename Polynomial >
 void Eigen::roots_to_monicPolynomial ( const RootVector & rv, Polynomial & poly )

Given the roots of a polynomial compute the coefficients in the monomial basis of the monic polynomial with same roots and minimal degree. If RootVector is a vector of complexes, Polynomial should also be a vector of complexes.

Parameters
 [in] rv : a vector containing the roots of a polynomial. [out] poly : the vector of coefficients of the polynomial ordered by degrees i.e. poly[i] is the coefficient of degree i of the polynomial e.g. is stored as a vector .