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.

215 lines
6.5 KiB

  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. #include "main.h"
  10. #include <unsupported/Eigen/Polynomials>
  11. #include <iostream>
  12. #include <algorithm>
  13. using namespace std;
  14. namespace Eigen {
  15. namespace internal {
  16. template<int Size>
  17. struct increment_if_fixed_size
  18. {
  19. enum {
  20. ret = (Size == Dynamic) ? Dynamic : Size+1
  21. };
  22. };
  23. }
  24. }
  25. template<int Deg, typename POLYNOMIAL, typename SOLVER>
  26. bool aux_evalSolver( const POLYNOMIAL& pols, SOLVER& psolve )
  27. {
  28. typedef typename POLYNOMIAL::Index Index;
  29. typedef typename POLYNOMIAL::Scalar Scalar;
  30. typedef typename SOLVER::RootsType RootsType;
  31. typedef Matrix<Scalar,Deg,1> EvalRootsType;
  32. const Index deg = pols.size()-1;
  33. psolve.compute( pols );
  34. const RootsType& roots( psolve.roots() );
  35. EvalRootsType evr( deg );
  36. for( int i=0; i<roots.size(); ++i ){
  37. evr[i] = std::abs( poly_eval( pols, roots[i] ) ); }
  38. bool evalToZero = evr.isZero( test_precision<Scalar>() );
  39. if( !evalToZero )
  40. {
  41. cerr << "WRONG root: " << endl;
  42. cerr << "Polynomial: " << pols.transpose() << endl;
  43. cerr << "Roots found: " << roots.transpose() << endl;
  44. cerr << "Abs value of the polynomial at the roots: " << evr.transpose() << endl;
  45. cerr << endl;
  46. }
  47. std::vector<Scalar> rootModuli( roots.size() );
  48. Map< EvalRootsType > aux( &rootModuli[0], roots.size() );
  49. aux = roots.array().abs();
  50. std::sort( rootModuli.begin(), rootModuli.end() );
  51. bool distinctModuli=true;
  52. for( size_t i=1; i<rootModuli.size() && distinctModuli; ++i )
  53. {
  54. if( internal::isApprox( rootModuli[i], rootModuli[i-1] ) ){
  55. distinctModuli = false; }
  56. }
  57. VERIFY( evalToZero || !distinctModuli );
  58. return distinctModuli;
  59. }
  60. template<int Deg, typename POLYNOMIAL>
  61. void evalSolver( const POLYNOMIAL& pols )
  62. {
  63. typedef typename POLYNOMIAL::Scalar Scalar;
  64. typedef PolynomialSolver<Scalar, Deg > PolynomialSolverType;
  65. PolynomialSolverType psolve;
  66. aux_evalSolver<Deg, POLYNOMIAL, PolynomialSolverType>( pols, psolve );
  67. }
  68. template< int Deg, typename POLYNOMIAL, typename ROOTS, typename REAL_ROOTS >
  69. void evalSolverSugarFunction( const POLYNOMIAL& pols, const ROOTS& roots, const REAL_ROOTS& real_roots )
  70. {
  71. using std::sqrt;
  72. typedef typename POLYNOMIAL::Scalar Scalar;
  73. typedef PolynomialSolver<Scalar, Deg > PolynomialSolverType;
  74. PolynomialSolverType psolve;
  75. if( aux_evalSolver<Deg, POLYNOMIAL, PolynomialSolverType>( pols, psolve ) )
  76. {
  77. //It is supposed that
  78. // 1) the roots found are correct
  79. // 2) the roots have distinct moduli
  80. typedef typename POLYNOMIAL::Scalar Scalar;
  81. typedef typename REAL_ROOTS::Scalar Real;
  82. typedef PolynomialSolver<Scalar, Deg > PolynomialSolverType;
  83. //Test realRoots
  84. std::vector< Real > calc_realRoots;
  85. psolve.realRoots( calc_realRoots );
  86. VERIFY( calc_realRoots.size() == (size_t)real_roots.size() );
  87. const Scalar psPrec = sqrt( test_precision<Scalar>() );
  88. for( size_t i=0; i<calc_realRoots.size(); ++i )
  89. {
  90. bool found = false;
  91. for( size_t j=0; j<calc_realRoots.size()&& !found; ++j )
  92. {
  93. if( internal::isApprox( calc_realRoots[i], real_roots[j] ), psPrec ){
  94. found = true; }
  95. }
  96. VERIFY( found );
  97. }
  98. //Test greatestRoot
  99. VERIFY( internal::isApprox( roots.array().abs().maxCoeff(),
  100. abs( psolve.greatestRoot() ), psPrec ) );
  101. //Test smallestRoot
  102. VERIFY( internal::isApprox( roots.array().abs().minCoeff(),
  103. abs( psolve.smallestRoot() ), psPrec ) );
  104. bool hasRealRoot;
  105. //Test absGreatestRealRoot
  106. Real r = psolve.absGreatestRealRoot( hasRealRoot );
  107. VERIFY( hasRealRoot == (real_roots.size() > 0 ) );
  108. if( hasRealRoot ){
  109. VERIFY( internal::isApprox( real_roots.array().abs().maxCoeff(), abs(r), psPrec ) ); }
  110. //Test absSmallestRealRoot
  111. r = psolve.absSmallestRealRoot( hasRealRoot );
  112. VERIFY( hasRealRoot == (real_roots.size() > 0 ) );
  113. if( hasRealRoot ){
  114. VERIFY( internal::isApprox( real_roots.array().abs().minCoeff(), abs( r ), psPrec ) ); }
  115. //Test greatestRealRoot
  116. r = psolve.greatestRealRoot( hasRealRoot );
  117. VERIFY( hasRealRoot == (real_roots.size() > 0 ) );
  118. if( hasRealRoot ){
  119. VERIFY( internal::isApprox( real_roots.array().maxCoeff(), r, psPrec ) ); }
  120. //Test smallestRealRoot
  121. r = psolve.smallestRealRoot( hasRealRoot );
  122. VERIFY( hasRealRoot == (real_roots.size() > 0 ) );
  123. if( hasRealRoot ){
  124. VERIFY( internal::isApprox( real_roots.array().minCoeff(), r, psPrec ) ); }
  125. }
  126. }
  127. template<typename _Scalar, int _Deg>
  128. void polynomialsolver(int deg)
  129. {
  130. typedef internal::increment_if_fixed_size<_Deg> Dim;
  131. typedef Matrix<_Scalar,Dim::ret,1> PolynomialType;
  132. typedef Matrix<_Scalar,_Deg,1> EvalRootsType;
  133. cout << "Standard cases" << endl;
  134. PolynomialType pols = PolynomialType::Random(deg+1);
  135. evalSolver<_Deg,PolynomialType>( pols );
  136. cout << "Hard cases" << endl;
  137. _Scalar multipleRoot = internal::random<_Scalar>();
  138. EvalRootsType allRoots = EvalRootsType::Constant(deg,multipleRoot);
  139. roots_to_monicPolynomial( allRoots, pols );
  140. evalSolver<_Deg,PolynomialType>( pols );
  141. cout << "Test sugar" << endl;
  142. EvalRootsType realRoots = EvalRootsType::Random(deg);
  143. roots_to_monicPolynomial( realRoots, pols );
  144. evalSolverSugarFunction<_Deg>(
  145. pols,
  146. realRoots.template cast <
  147. std::complex<
  148. typename NumTraits<_Scalar>::Real
  149. >
  150. >(),
  151. realRoots );
  152. }
  153. void test_polynomialsolver()
  154. {
  155. for(int i = 0; i < g_repeat; i++)
  156. {
  157. CALL_SUBTEST_1( (polynomialsolver<float,1>(1)) );
  158. CALL_SUBTEST_2( (polynomialsolver<double,2>(2)) );
  159. CALL_SUBTEST_3( (polynomialsolver<double,3>(3)) );
  160. CALL_SUBTEST_4( (polynomialsolver<float,4>(4)) );
  161. CALL_SUBTEST_5( (polynomialsolver<double,5>(5)) );
  162. CALL_SUBTEST_6( (polynomialsolver<float,6>(6)) );
  163. CALL_SUBTEST_7( (polynomialsolver<float,7>(7)) );
  164. CALL_SUBTEST_8( (polynomialsolver<double,8>(8)) );
  165. CALL_SUBTEST_9( (polynomialsolver<float,Dynamic>(
  166. internal::random<int>(9,13)
  167. )) );
  168. CALL_SUBTEST_10((polynomialsolver<double,Dynamic>(
  169. internal::random<int>(9,13)
  170. )) );
  171. }
  172. }