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.

217 lines
6.7 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. typedef typename POLYNOMIAL::Scalar Scalar;
  72. typedef PolynomialSolver<Scalar, Deg > PolynomialSolverType;
  73. PolynomialSolverType psolve;
  74. if( aux_evalSolver<Deg, POLYNOMIAL, PolynomialSolverType>( pols, psolve ) )
  75. {
  76. //It is supposed that
  77. // 1) the roots found are correct
  78. // 2) the roots have distinct moduli
  79. typedef typename POLYNOMIAL::Scalar Scalar;
  80. typedef typename REAL_ROOTS::Scalar Real;
  81. typedef PolynomialSolver<Scalar, Deg > PolynomialSolverType;
  82. typedef typename PolynomialSolverType::RootsType RootsType;
  83. typedef Matrix<Scalar,Deg,1> EvalRootsType;
  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 = internal::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. internal::abs( psolve.greatestRoot() ), psPrec ) );
  102. //Test smallestRoot
  103. VERIFY( internal::isApprox( roots.array().abs().minCoeff(),
  104. internal::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(), internal::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(), internal::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. }
  173. }