PolynomialSolver1.cpp File Reference
#include <unsupported/Eigen/Polynomials>
#include <vector>
#include <iostream>

Functions

int main ()
 

Function Documentation

◆ main()

int main ( )
8  {
9  typedef Matrix<double, 5, 1> Vector5d;
10 
11  Vector5d roots = Vector5d::Random();
12  cout << "Roots: " << roots.transpose() << endl;
13  Eigen::Matrix<double, 6, 1> polynomial;
14  roots_to_monicPolynomial(roots, polynomial);
15 
16  PolynomialSolver<double, 5> psolve(polynomial);
17  cout << "Complex roots: " << psolve.roots().transpose() << endl;
18 
19  std::vector<double> realRoots;
20  psolve.realRoots(realRoots);
21  Map<Vector5d> mapRR(&realRoots[0]);
22  cout << "Real roots: " << mapRR.transpose() << endl;
23 
24  cout << endl;
25  cout << "Illustration of the convergence problem with the QR algorithm: " << endl;
26  cout << "---------------------------------------------------------------" << endl;
27  Eigen::Matrix<float, 7, 1> hardCase_polynomial;
28  hardCase_polynomial << -0.957, 0.9219, 0.3516, 0.9453, -0.4023, -0.5508, -0.03125;
29  cout << "Hard case polynomial defined by floats: " << hardCase_polynomial.transpose() << endl;
30  PolynomialSolver<float, 6> psolvef(hardCase_polynomial);
31  cout << "Complex roots: " << psolvef.roots().transpose() << endl;
33  for (int i = 0; i < 6; ++i) {
34  evals[i] = std::abs(poly_eval(hardCase_polynomial, psolvef.roots()[i]));
35  }
36  cout << "Norms of the evaluations of the polynomial at the roots: " << evals.transpose() << endl << endl;
37 
38  cout << "Using double's almost always solves the problem for small degrees: " << endl;
39  cout << "-------------------------------------------------------------------" << endl;
40  PolynomialSolver<double, 6> psolve6d(hardCase_polynomial.cast<double>());
41  cout << "Complex roots: " << psolve6d.roots().transpose() << endl;
42  for (int i = 0; i < 6; ++i) {
43  std::complex<float> castedRoot(psolve6d.roots()[i].real(), psolve6d.roots()[i].imag());
44  evals[i] = std::abs(poly_eval(hardCase_polynomial, castedRoot));
45  }
46  cout << "Norms of the evaluations of the polynomial at the roots: " << evals.transpose() << endl << endl;
47 
48  cout.precision(10);
49  cout << "The last root in float then in double: " << psolvef.roots()[5] << "\t" << psolve6d.roots()[5] << endl;
50  std::complex<float> castedRoot(psolve6d.roots()[5].real(), psolve6d.roots()[5].imag());
51  cout << "Norm of the difference: " << std::abs(psolvef.roots()[5] - castedRoot) << endl;
52 }
AnnoyingScalar abs(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:135
int i
Definition: BiCGSTAB_step_by_step.cpp:9
A matrix or vector expression mapping an existing array of data.
Definition: Map.h:96
The matrix class, also used for vectors and row-vectors.
Definition: Eigen/Eigen/src/Core/Matrix.h:186
A polynomial solver.
Definition: PolynomialSolver.h:300
T poly_eval(const Polynomials &poly, const T &x)
Definition: PolynomialUtils.h:47
void roots_to_monicPolynomial(const RootVector &rv, Polynomial &poly)
Definition: PolynomialUtils.h:128

References abs(), i, Eigen::poly_eval(), Eigen::PolynomialSolverBase< Scalar_, Deg_ >::realRoots(), Eigen::PolynomialSolverBase< Scalar_, Deg_ >::roots(), and Eigen::roots_to_monicPolynomial().