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.

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