EigValues.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /**
  2. * @file EigValues.cpp
  3. * @brief EigValues Class
  4. * @author Michael Koch
  5. * @date 08/19/2008
  6. */
  7. #include <iostream>
  8. #include "EigValues.h"
  9. #define DEBUG_ARNOLDI
  10. using namespace NICE;
  11. using namespace std;
  12. void
  13. EVArnoldi::getEigenvalues ( const GenericMatrix & data, Vector & eigenvalues,
  14. Matrix & eigenvectors, uint k )
  15. {
  16. /////////////////////////////////////
  17. ///////// check input arguments /////
  18. /////////////////////////////////////
  19. if ( data.rows () != data.cols () )
  20. {
  21. throw ( "EVArnoldi: matrix has to be quadratic" );
  22. }
  23. if ( k <= 0 )
  24. {
  25. throw ( "EVArnoldi: please use k>0." );
  26. }
  27. // did we specify more eigenvalues than the matrix can actually have?
  28. if ( k > data.rows() )
  29. {
  30. throw ( "EVArnoldi: specified k is larger then dimension of matrix! Aborting..." );
  31. }
  32. //////////////////////////////////////
  33. ///////// initialize variables ///////
  34. //////////////////////////////////////
  35. if ( verbose )
  36. cerr << "Initialize Matrices" << std::endl;
  37. uint n = data.cols ();
  38. if ( verbose )
  39. std::cerr << "EVArnoldi: n: " << n << " k: " << k << std::endl;
  40. NICE::Matrix rmatrix ( n, k, 0 ); //=eigenvectors
  41. NICE::Matrix qmatrix ( n, k, 0 ); //saves data =eigenvectors
  42. eigenvalues.resize ( k );
  43. NICE::Vector q ( n );
  44. NICE::Vector r ( n );
  45. if ( verbose )
  46. cerr << "Random Initialization" << endl;
  47. //random initialisation
  48. for ( uint i = 0; i < k; i++ )
  49. for ( uint j = 0; j < n; j++ )
  50. rmatrix ( j, i ) = drand48 ();
  51. // rmatrix ( j, i ) = 0.5;
  52. //TODO the random initialization might help, but it is bad for reproducibility :(
  53. ////////////////////////////////////
  54. ///////// start computation ///////
  55. ////////////////////////////////////
  56. if ( verbose )
  57. std::cerr << "EVArnoldi: start main computation" << std::endl;
  58. //reduceddim
  59. double delta = 1.0;
  60. uint iteration = 0;
  61. while ( delta > mindelta && iteration < maxiterations )
  62. {
  63. //replace Vector rold by matrix rold to check for convergence of all eigenvectors
  64. //NICE::Vector rold ( rmatrix.getColumn ( k - 1 ) );
  65. NICE::Matrix rold(rmatrix);
  66. if ( verbose )
  67. std::cerr << "EVArnoldi: start for loop over reduced dims" << std::endl;
  68. // meta-comment: i is an index for the iteration, j is an index for the basis
  69. // element (1 <= j <= k)
  70. for ( uint reduceddim = 0; reduceddim < k; reduceddim++ )
  71. {
  72. // -> get r^i_j from R matrix
  73. q = rmatrix.getColumn ( reduceddim );
  74. // q^i_j = r^i_j / ||r^i_j||
  75. q.normalizeL2 ();
  76. // -> store in Q matrix
  77. qmatrix.getColumnRef ( reduceddim ) = q;
  78. // this line copies a vector with external memory!
  79. // changing currentcol leads to a change in the R matrix!!
  80. Vector currentCol = rmatrix.getColumnRef ( reduceddim );
  81. // r^{i+1}_j = A * q^i_j ( r^i_j is overwritten by r^{i+1}_j )
  82. data.multiply ( currentCol, q );
  83. // for all j: r^{i+1}_j -= q^i_j * < q^i_j, r^{i+1}_j >
  84. for ( uint j = 0; j < reduceddim; j++ )
  85. rmatrix.getColumnRef ( reduceddim ) -=
  86. qmatrix.getColumn ( j ) *
  87. ( qmatrix.getColumn ( j ).
  88. scalarProduct ( rmatrix.getColumn ( reduceddim ) ) );
  89. }
  90. if ( verbose )
  91. std::cerr << "EVArnoldi: ended for loop over reduced dims" << std::endl;
  92. //convergence stuff (replaced by checking all eigenvectors instead of a single one
  93. //NICE::Vector diff = rold - rmatrix.getColumn ( k - 1 );
  94. //delta = diff.normL2 ();
  95. NICE::Vector tmpDiff;
  96. double norm_tmpDiff;
  97. delta = 0.0;
  98. for ( uint j = 0; j < k; j++ )
  99. {
  100. tmpDiff = rold.getColumn(j) - rmatrix.getColumn(j);
  101. norm_tmpDiff = tmpDiff.normL2();
  102. if (norm_tmpDiff > delta)
  103. delta = norm_tmpDiff;
  104. }
  105. iteration++;
  106. if ( verbose )
  107. cerr << "EVArnoldi: [" << iteration << "] delta=" << delta << endl;
  108. }
  109. if ( verbose )
  110. std::cerr << "EVArnoldi: while-loop done" << std::endl;
  111. eigenvectors = rmatrix;
  112. for ( uint i = 0; i < k; i++ )
  113. {
  114. NICE::Vector tmp;
  115. eigenvectors.getColumnRef ( i ).normalizeL2 ();
  116. data.multiply ( tmp, eigenvectors.getColumn ( i ) );
  117. eigenvalues[i] = tmp.scalarProduct ( eigenvectors.getColumn ( i ) );
  118. }
  119. // post-processing: ensure that eigenvalues are in correct order!
  120. if ( this->b_verifyDecreasingOrder && (k > 1) )
  121. {
  122. NICE::VectorT< int > ewPermutation;
  123. eigenvalues.sortDescend ( ewPermutation );
  124. NICE::Vector tmpRow;
  125. int tmpIdx (-1);
  126. for ( uint i = 0; i < k; i++ )
  127. {
  128. if ( i == ewPermutation[i] )
  129. {
  130. //identity - nothing to do here
  131. }
  132. else
  133. {
  134. if ( tmpIdx == -1 )
  135. {
  136. tmpIdx = i;
  137. tmpRow = eigenvectors.getColumn ( i );
  138. eigenvectors.getColumnRef ( i ) = eigenvectors.getColumn ( ewPermutation[i] );
  139. }
  140. else
  141. {
  142. if ( tmpIdx != ewPermutation[i] )
  143. {
  144. eigenvectors.getColumnRef ( i ) = eigenvectors.getColumn ( ewPermutation[i] );
  145. }
  146. else // tmpIdx == ewPermutation[i]
  147. {
  148. eigenvectors.getColumnRef ( i ) = tmpRow;
  149. tmpIdx = -1;
  150. }
  151. }
  152. }
  153. }
  154. }// sorting is only useful if we compute more then 1 ew
  155. }