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.

113 lines
3.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/StormEigen/Polynomials>
  11. #include <iostream>
  12. using namespace std;
  13. namespace StormEigen {
  14. namespace internal {
  15. template<int Size>
  16. struct increment_if_fixed_size
  17. {
  18. enum {
  19. ret = (Size == Dynamic) ? Dynamic : Size+1
  20. };
  21. };
  22. }
  23. }
  24. template<typename _Scalar, int _Deg>
  25. void realRoots_to_monicPolynomial_test(int deg)
  26. {
  27. typedef internal::increment_if_fixed_size<_Deg> Dim;
  28. typedef Matrix<_Scalar,Dim::ret,1> PolynomialType;
  29. typedef Matrix<_Scalar,_Deg,1> EvalRootsType;
  30. PolynomialType pols(deg+1);
  31. EvalRootsType roots = EvalRootsType::Random(deg);
  32. roots_to_monicPolynomial( roots, pols );
  33. EvalRootsType evr( deg );
  34. for( int i=0; i<roots.size(); ++i ){
  35. evr[i] = std::abs( poly_eval( pols, roots[i] ) ); }
  36. bool evalToZero = evr.isZero( test_precision<_Scalar>() );
  37. if( !evalToZero ){
  38. cerr << evr.transpose() << endl; }
  39. VERIFY( evalToZero );
  40. }
  41. template<typename _Scalar> void realRoots_to_monicPolynomial_scalar()
  42. {
  43. CALL_SUBTEST_2( (realRoots_to_monicPolynomial_test<_Scalar,2>(2)) );
  44. CALL_SUBTEST_3( (realRoots_to_monicPolynomial_test<_Scalar,3>(3)) );
  45. CALL_SUBTEST_4( (realRoots_to_monicPolynomial_test<_Scalar,4>(4)) );
  46. CALL_SUBTEST_5( (realRoots_to_monicPolynomial_test<_Scalar,5>(5)) );
  47. CALL_SUBTEST_6( (realRoots_to_monicPolynomial_test<_Scalar,6>(6)) );
  48. CALL_SUBTEST_7( (realRoots_to_monicPolynomial_test<_Scalar,7>(7)) );
  49. CALL_SUBTEST_8( (realRoots_to_monicPolynomial_test<_Scalar,17>(17)) );
  50. CALL_SUBTEST_9( (realRoots_to_monicPolynomial_test<_Scalar,Dynamic>(
  51. internal::random<int>(18,26) )) );
  52. }
  53. template<typename _Scalar, int _Deg>
  54. void CauchyBounds(int deg)
  55. {
  56. typedef internal::increment_if_fixed_size<_Deg> Dim;
  57. typedef Matrix<_Scalar,Dim::ret,1> PolynomialType;
  58. typedef Matrix<_Scalar,_Deg,1> EvalRootsType;
  59. PolynomialType pols(deg+1);
  60. EvalRootsType roots = EvalRootsType::Random(deg);
  61. roots_to_monicPolynomial( roots, pols );
  62. _Scalar M = cauchy_max_bound( pols );
  63. _Scalar m = cauchy_min_bound( pols );
  64. _Scalar Max = roots.array().abs().maxCoeff();
  65. _Scalar min = roots.array().abs().minCoeff();
  66. bool eval = (M >= Max) && (m <= min);
  67. if( !eval )
  68. {
  69. cerr << "Roots: " << roots << endl;
  70. cerr << "Bounds: (" << m << ", " << M << ")" << endl;
  71. cerr << "Min,Max: (" << min << ", " << Max << ")" << endl;
  72. }
  73. VERIFY( eval );
  74. }
  75. template<typename _Scalar> void CauchyBounds_scalar()
  76. {
  77. CALL_SUBTEST_2( (CauchyBounds<_Scalar,2>(2)) );
  78. CALL_SUBTEST_3( (CauchyBounds<_Scalar,3>(3)) );
  79. CALL_SUBTEST_4( (CauchyBounds<_Scalar,4>(4)) );
  80. CALL_SUBTEST_5( (CauchyBounds<_Scalar,5>(5)) );
  81. CALL_SUBTEST_6( (CauchyBounds<_Scalar,6>(6)) );
  82. CALL_SUBTEST_7( (CauchyBounds<_Scalar,7>(7)) );
  83. CALL_SUBTEST_8( (CauchyBounds<_Scalar,17>(17)) );
  84. CALL_SUBTEST_9( (CauchyBounds<_Scalar,Dynamic>(
  85. internal::random<int>(18,26) )) );
  86. }
  87. void test_polynomialutils()
  88. {
  89. for(int i = 0; i < g_repeat; i++)
  90. {
  91. realRoots_to_monicPolynomial_scalar<double>();
  92. realRoots_to_monicPolynomial_scalar<float>();
  93. CauchyBounds_scalar<double>();
  94. CauchyBounds_scalar<float>();
  95. }
  96. }