testEigenvalues.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /**
  2. * @file testEigenvalues.cpp
  3. * @brief test eigenvalues
  4. * @author Erik Rodner
  5. * @date 05/21/2008
  6. */
  7. #include "core/vector/VectorT.h"
  8. #include "core/vector/MatrixT.h"
  9. #include "core/image/ImageT.h"
  10. #include "core/imagedisplay/ImageDisplay.h"
  11. #ifdef NICE_USELIB_ICE
  12. #include <image_nonvis.h>
  13. #include <core/iceconversion/convertice.h>
  14. #include <core/vector/Distance.h>
  15. #include "vislearning/math/algebra/EigValues.h"
  16. #ifdef NICE_USELIB_TRLAN
  17. #include "vislearning/math/algebra_trlan/EigValuesTRLAN.h"
  18. #endif
  19. #include "core/basics/Config.h"
  20. using namespace OBJREC;
  21. using namespace NICE;
  22. using namespace std;
  23. int main (int argc, char **argv)
  24. {
  25. std::set_terminate(__gnu_cxx::__verbose_terminate_handler);
  26. Config conf ( argc, argv );
  27. uint rows = conf.gI("main", "n", 2);
  28. uint cols = rows;
  29. uint k = conf.gI("main", "k", rows);
  30. uint maxiterations = conf.gI("main", "maxiterations", 100);
  31. double mindelta = conf.gD("main", "mindelta", 1e-4 );
  32. double sparse_prob = conf.gD("main", "sparseprob", 0.0 );
  33. bool trlan = conf.gB("main", "trlan", false);
  34. int trlan_magnitude = conf.gI("main", "trlan_magnitude", 1);
  35. bool init_random = conf.gB("main", "init_random", true );
  36. // refactor-nice.pl: check this substitution
  37. // old: Matrix T (rows, cols);
  38. NICE::Matrix T (rows, cols);
  39. T.set(0.0);
  40. if ( init_random )
  41. srand48(time(NULL));
  42. // generate random symmetric matrix
  43. for ( uint i = 0 ; i < rows ; i++ )
  44. for ( uint j = i ; j < cols ; j++ )
  45. {
  46. if ( sparse_prob != 0.0 )
  47. if ( drand48() < sparse_prob )
  48. continue;
  49. T(i, j) = drand48();
  50. T(j, i) = T(i, j);
  51. }
  52. // refactor-nice.pl: check this substitution
  53. // old: Vector ice_eigvalues;
  54. ice::Vector ice_eigvalues;
  55. // refactor-nice.pl: check this substitution
  56. // old: Matrix ice_eigvect;
  57. ice::Matrix ice_eigvect;
  58. Eigenvalue ( NICE::makeICEMatrix(T), ice_eigvalues, ice_eigvect );
  59. EigValues *eig;
  60. if ( ! trlan )
  61. {
  62. fprintf (stderr, "Eigenvalue method: Arnoldi iteration\n");
  63. eig = new EVArnoldi( maxiterations, mindelta );
  64. } else {
  65. #ifdef NICE_USELIB_TRLAN
  66. fprintf (stderr, "Eigenvalue method: Lanczos (TRLAN) magn=%d\n", trlan_magnitude);
  67. eig = new EigValuesTRLAN(trlan_magnitude);
  68. #else
  69. fprintf (stderr, "Eigenvalue method: Lanczos (TRLAN) not supported !\n");
  70. exit(-1);
  71. #endif
  72. }
  73. NICE::Vector eigvalues_dense;
  74. NICE::Matrix eigvect_dense;
  75. NICE::Vector eigvalues_sparse;
  76. NICE::Matrix eigvect_sparse;
  77. GMSlowICE Tg (T);
  78. double start_dense = ice::TimeD();
  79. eig->getEigenvalues ( Tg, eigvalues_dense, eigvect_dense, k );
  80. double time_dense = ice::TimeD() - start_dense;
  81. GMSparse Ts (T);
  82. double start_sparse = ice::TimeD();
  83. eig->getEigenvalues ( Ts, eigvalues_sparse, eigvect_sparse, k );
  84. double time_sparse = ice::TimeD() - start_sparse;
  85. fprintf (stderr, "time: sparse %fs, dense %fs\n", time_sparse, time_dense );
  86. // test property
  87. NICE::EuclidianDistance<double> eucliddist;
  88. for ( uint i = 0 ; i < k ; i++ )
  89. {
  90. NICE::Vector v_dense = eigvect_dense.getColumn(i);
  91. double lambda_dense = eigvalues_dense[i];
  92. NICE::Vector Tv_dense;
  93. Tv_dense.multiply ( T, v_dense );
  94. NICE::Vector lv_dense = v_dense;
  95. lv_dense *= lambda_dense;
  96. double err_dense = eucliddist(Tv_dense, lv_dense);
  97. NICE::Vector v_sparse = eigvect_sparse.getColumn(i);
  98. double lambda_sparse = eigvalues_sparse[i];
  99. NICE::Vector Tv_sparse;
  100. Tv_sparse.multiply ( T, v_sparse );
  101. NICE::Vector lv_sparse = v_sparse;
  102. lv_sparse *= lambda_sparse;
  103. double err_sparse = eucliddist(Tv_sparse, lv_sparse);
  104. ice::Matrix ice_T = makeICEMatrix(T);
  105. cerr << T << endl;
  106. cerr << ice_T << endl;
  107. double err_ice = (ice_T*(!ice_eigvect)[i] - ice_eigvalues[i]*(!ice_eigvect)[i]).Length();
  108. cerr << "eigenvalue " << i << endl;
  109. cerr << "lambda (dense) = " << lambda_dense << endl;
  110. cerr << "lambda (sparse) = " << lambda_sparse << endl;
  111. cerr << "lambda (ICE) = " << ice_eigvalues[i] << endl;
  112. cerr << "||Av - lambda v|| (dense) = " << err_dense << endl;
  113. cerr << "||Av - lambda v|| (sparse) = " << err_sparse << endl;
  114. cerr << "||Av - lambda v|| (ICE) = " << err_ice << endl;
  115. }
  116. // test covariance part
  117. cerr << "------- Covariance Test -------" << endl;
  118. int n = 2*rows; // number of data elements
  119. NICE::Matrix data(rows, n);
  120. ice::Statistics stat (rows);
  121. for (int i = 0 ; i < n ; i++ )
  122. {
  123. for ( int j = 0 ; j < (int)rows ; j++ )
  124. data(j, i) = drand48() + 100.0;
  125. Put ( stat, NICE::makeIceVectorT( data.getColumn(i) ) );
  126. }
  127. cerr << "Mean: " << Mean(stat) << endl;
  128. NICE::Matrix cov = NICE::makeDoubleMatrix ( Covariance(stat) );
  129. GMSlowICE cov_slow ( cov );
  130. GMCovariance cov_cov ( &data );
  131. NICE::Vector x1t_slow;
  132. cov_slow.multiply ( x1t_slow, data.getColumn(0) );
  133. cerr << x1t_slow << endl;
  134. NICE::Vector x1t_cov;
  135. cov_cov.multiply ( x1t_cov, data.getColumn(0) );
  136. cerr << x1t_cov << endl;
  137. cerr << "Difference (simple vector multiplication): " << eucliddist( x1t_slow, x1t_cov ) << endl;
  138. double start_std = ice::TimeD();
  139. eig->getEigenvalues ( cov_slow, eigvalues_dense, eigvect_dense, k );
  140. double time_std = ice::TimeD() - start_std;
  141. NICE::Vector eigvalues_cov;
  142. NICE::Matrix eigvect_cov;
  143. double start_cov = ice::TimeD();
  144. eig->getEigenvalues ( cov_cov, eigvalues_cov, eigvect_cov, k );
  145. double time_cov = ice::TimeD() - start_cov;
  146. cerr << "Eigenvalues (std) = " << eigvalues_dense << endl;
  147. cerr << "Eigenvalues (covariance) = " << eigvalues_cov << endl;
  148. fprintf (stderr, "time: cov %fs, std %fs\n", time_std, time_cov );
  149. return 0;
  150. }
  151. #else
  152. int main (int argc, char **argv)
  153. {
  154. throw("no ice installed\n");
  155. return 0;
  156. }
  157. #endif