You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
2.3 KiB

  1. #include <unsupported/Eigen/Polynomials>
  2. #include <vector>
  3. #include <iostream>
  4. using namespace Eigen;
  5. using namespace std;
  6. int main()
  7. {
  8. typedef Matrix<double,5,1> Vector5d;
  9. Vector5d roots = Vector5d::Random();
  10. cout << "Roots: " << roots.transpose() << endl;
  11. Eigen::Matrix<double,6,1> polynomial;
  12. roots_to_monicPolynomial( roots, polynomial );
  13. PolynomialSolver<double,5> psolve( polynomial );
  14. cout << "Complex roots: " << psolve.roots().transpose() << endl;
  15. std::vector<double> realRoots;
  16. psolve.realRoots( realRoots );
  17. Map<Vector5d> mapRR( &realRoots[0] );
  18. cout << "Real roots: " << mapRR.transpose() << endl;
  19. cout << endl;
  20. cout << "Illustration of the convergence problem with the QR algorithm: " << endl;
  21. cout << "---------------------------------------------------------------" << endl;
  22. Eigen::Matrix<float,7,1> hardCase_polynomial;
  23. hardCase_polynomial <<
  24. -0.957, 0.9219, 0.3516, 0.9453, -0.4023, -0.5508, -0.03125;
  25. cout << "Hard case polynomial defined by floats: " << hardCase_polynomial.transpose() << endl;
  26. PolynomialSolver<float,6> psolvef( hardCase_polynomial );
  27. cout << "Complex roots: " << psolvef.roots().transpose() << endl;
  28. Eigen::Matrix<float,6,1> evals;
  29. for( int i=0; i<6; ++i ){ evals[i] = std::abs( poly_eval( hardCase_polynomial, psolvef.roots()[i] ) ); }
  30. cout << "Norms of the evaluations of the polynomial at the roots: " << evals.transpose() << endl << endl;
  31. cout << "Using double's almost always solves the problem for small degrees: " << endl;
  32. cout << "-------------------------------------------------------------------" << endl;
  33. PolynomialSolver<double,6> psolve6d( hardCase_polynomial.cast<double>() );
  34. cout << "Complex roots: " << psolve6d.roots().transpose() << endl;
  35. for( int i=0; i<6; ++i )
  36. {
  37. std::complex<float> castedRoot( psolve6d.roots()[i].real(), psolve6d.roots()[i].imag() );
  38. evals[i] = std::abs( poly_eval( hardCase_polynomial, castedRoot ) );
  39. }
  40. cout << "Norms of the evaluations of the polynomial at the roots: " << evals.transpose() << endl << endl;
  41. cout.precision(10);
  42. cout << "The last root in float then in double: " << psolvef.roots()[5] << "\t" << psolve6d.roots()[5] << endl;
  43. std::complex<float> castedRoot( psolve6d.roots()[5].real(), psolve6d.roots()[5].imag() );
  44. cout << "Norm of the difference: " << internal::abs( psolvef.roots()[5] - castedRoot ) << endl;
  45. }