TestEigenValue.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. //
  2. // C++ Implementation: TestEigenValue
  3. //
  4. // Description:
  5. //
  6. //
  7. // Author: Michael Koch <Koch.Michael@uni-jena.de>, (C) 2009
  8. //
  9. // Copyright: See COPYING file that comes with this distribution
  10. //
  11. /**
  12. * @file TestEigenValue.cpp
  13. * @brief TestEigenValue
  14. * @author Michael Koch
  15. * @date Di Aug 4 2009
  16. */
  17. #include "TestEigenValue.h"
  18. #include <string>
  19. #include "core/basics/cppunitex.h"
  20. #include "core/basics/numerictools.h"
  21. #include "core/vector/Distance.h"
  22. #include "vislearning/math/algebra/EigValues.h"
  23. #include "vislearning/math/algebra/AlgebraTools.h"
  24. #include "vislearning/math/algebra/GenericMatrix.h"
  25. #include "vislearning/nice_nonvis.h"
  26. using namespace std;
  27. using namespace NICE;
  28. using namespace OBJREC;
  29. CPPUNIT_TEST_SUITE_REGISTRATION(TestEigenValue);
  30. void TestEigenValue::setUp()
  31. {
  32. }
  33. void TestEigenValue::tearDown()
  34. {
  35. }
  36. void TestEigenValue::TestEigenValueComputation()
  37. {
  38. uint rows = 3;
  39. uint cols = rows;
  40. uint k = rows;
  41. uint maxiterations = 200;
  42. double mindelta = 1e-8;
  43. double sparse_prob = 0.0;
  44. int trlan_magnitude = 1;
  45. bool init_random = true ;
  46. // refactor-nice.pl: check this substitution
  47. // old: Matrix T (rows, cols);
  48. NICE::Matrix T(rows, cols);
  49. T.set(0.0);
  50. if (init_random)
  51. srand48(time(NULL));
  52. // generate random symmetric matrix
  53. for (uint i = 0 ; i < rows ; i++)
  54. for (uint j = i ; j < cols ; j++)
  55. {
  56. if (sparse_prob != 0.0)
  57. if (drand48() < sparse_prob)
  58. continue;
  59. T(i, j) = drand48();
  60. T(j, i) = T(i, j);
  61. }
  62. // refactor-nice.pl: check this substitution
  63. // old: Vector ice_eigvalues;
  64. EigValues *eig;
  65. for (int trlan = 0;trlan <= 1;trlan++) //this is creepy but funny
  66. {
  67. if (trlan) //this is creepy but saves lot of code
  68. {
  69. #ifdef NICE_USELIB_TRLAN
  70. eig = new EigValuesTRLAN(trlan_magnitude);
  71. #else
  72. break;
  73. #endif
  74. }
  75. else
  76. {
  77. eig = new EVArnoldi(maxiterations, mindelta);
  78. }
  79. NICE::Vector eigvalues_dense;
  80. NICE::Matrix eigvect_dense;
  81. NICE::Vector eigvalues_sparse;
  82. NICE::Matrix eigvect_sparse;
  83. GMSlowICE Tg(T);
  84. eig->getEigenvalues(Tg, eigvalues_dense, eigvect_dense, k);
  85. GMSparse Ts(T);
  86. eig->getEigenvalues(Ts, eigvalues_sparse, eigvect_sparse, k);
  87. // test property
  88. NICE::EuclidianDistance<double> eucliddist;
  89. for (uint i = 0 ; i < k ; i++)
  90. {
  91. NICE::Vector v_dense = eigvect_dense.getColumn(i);
  92. double lambda_dense = eigvalues_dense[i];
  93. NICE::Vector Tv_dense;
  94. Tv_dense.multiply(T, v_dense);
  95. NICE::Vector lv_dense = v_dense;
  96. lv_dense *= lambda_dense;
  97. double err_dense = eucliddist(Tv_dense, lv_dense);
  98. NICE::Vector v_sparse = eigvect_sparse.getColumn(i);
  99. double lambda_sparse = eigvalues_sparse[i];
  100. NICE::Vector Tv_sparse;
  101. Tv_sparse.multiply(T, v_sparse);
  102. NICE::Vector lv_sparse = v_sparse;
  103. lv_sparse *= lambda_sparse;
  104. double err_sparse = eucliddist(Tv_sparse, lv_sparse);
  105. // cerr << "eigenvalue " << i << endl;
  106. // cerr << "lambda (dense) = " << lambda_dense << endl;
  107. // cerr << "lambda (sparse) = " << lambda_sparse << endl;
  108. // cerr << "||Av - lambda v|| (dense) = " << err_dense << endl;
  109. // cerr << "||Av - lambda v|| (sparse) = " << err_sparse << endl;
  110. CPPUNIT_ASSERT_DOUBLES_EQUAL_NOT_NAN(0.0,err_dense,1e-4);
  111. CPPUNIT_ASSERT_DOUBLES_EQUAL_NOT_NAN(0.0,err_sparse,1e-4);
  112. }
  113. }
  114. }