testEigenvalues.cpp 5.6 KB

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