EigValues.cpp.refactor 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /**
  2. * @file EigValues.cpp
  3. * @brief EigValues Class
  4. * @author Michael Koch
  5. * @date 08/19/2008
  6. */
  7. #ifdef NOVISUAL
  8. #include <nice_nonvis.h>
  9. #else
  10. #include <nice.h>
  11. #endif
  12. #include <core/iceconversion/convertice.h>
  13. #include <image_nonvis.h>
  14. #include <iostream>
  15. #include "EigValues.h"
  16. #include <time.h>
  17. #define DEBUG_ARNOLDI
  18. using namespace std;
  19. // refactor-nice.pl: check this substitution
  20. // old: using namespace ice;
  21. using namespace NICE;
  22. void EVArnoldi::getEigenvalues(const GenericMatrix &data,Vector &eigenvalues,Matrix &eigenvectors,uint k)
  23. {
  24. if(data.rows()!=data.cols())
  25. {
  26. throw("EVArnoldi: matrix has to be quadratic");
  27. }
  28. if(k<=0)
  29. {
  30. throw("EVArnoldi: please use k>0.");
  31. }
  32. uint n=data.cols();
  33. ice::Matrix rmatrix (k,n,0);
  34. ice::Matrix qmatrix (k,n,0);//saves data =eigenvectors
  35. ice::Vector q (n);
  36. ice::Vector r (n);
  37. eigenvalues.resize(n);
  38. srand48(time(NULL));
  39. //random initialisation
  40. for(uint i=0;i<rmatrix.rows();i++)
  41. for(uint j=0;j<rmatrix.cols();j++)
  42. rmatrix[i][j]=drand48();
  43. //reduceddim
  44. double delta=1.0;
  45. uint iteration=0;
  46. while (delta>mindelta &&iteration<maxiterations)
  47. {
  48. ice::Vector rold (rmatrix[k-1]);
  49. for(uint reduceddim=0;reduceddim<k;reduceddim++)
  50. {
  51. //get Vector r_k of Matrix
  52. q=rmatrix[reduceddim];
  53. q.Normalize();
  54. // q=r_k/||r_k||
  55. qmatrix[reduceddim]=q;
  56. NICE::Vector tmp;
  57. data.multiply( tmp, NICE::makeEVector(q)); //r_{k+1}=A*q
  58. rmatrix[reduceddim] = NICE::makeIceVectorT(tmp);
  59. for(uint j=0;j<reduceddim;j++)
  60. rmatrix[reduceddim]=rmatrix[reduceddim]-qmatrix[j]*(qmatrix[j]*rmatrix[reduceddim]);
  61. }
  62. //convergence stuff
  63. ice::Vector diff=rold-rmatrix[k-1];
  64. delta=diff.Length();
  65. iteration++;
  66. #ifdef DEBUG_ARNOLDI
  67. fprintf (stderr, "EVArnoldi: [%d] delta=%f\n", iteration, delta );
  68. #endif
  69. }
  70. eigenvectors = NICE::makeDoubleMatrix ( rmatrix );
  71. eigenvectors.transposeInplace();
  72. for(uint i=0;i<k;i++)
  73. {
  74. // refactor-nice.pl: check this substitution
  75. // old: Vector tmp;
  76. NICE::Vector tmp;
  77. eigenvectors.getColumnRef(i).normalizeL2();
  78. data.multiply(tmp,eigenvectors.getColumn(i));
  79. eigenvalues[i] = tmp.scalarProduct (eigenvectors.getColumn(i));
  80. }
  81. }
  82. #ifdef USE_BROKEN_LANCZOS
  83. void EVLanczos::getEigenvalues(const GenericMatrix &data,Vector &eigenvalues,Matrix &eigenvectors,uint k)
  84. {
  85. if (data.rows()!=data.cols())
  86. throw("Input matrix is not quadratic.");
  87. uint n=data.cols();
  88. uint m=data.rows();
  89. if(k<=0 ) k=n;
  90. uint kint=k;
  91. NICE::Matrix wmatrix(kint+1,n,0);
  92. NICE::Matrix vmatrix(kint+1,n,0);//saves data =eigenvectors
  93. NICE::Matrix t(kint,kint,0);
  94. eigenvalues=Vector(k);
  95. NICE::Matrix eigenvectorsapprox(k,n,0);
  96. NICE::Vector w=Vector(n);
  97. // refactor-nice.pl: check this substitution
  98. // old: Vector alpha=Vector(kint+1);
  99. NICE::Vector alpha=Vector(kint+1);
  100. // refactor-nice.pl: check this substitution
  101. // old: Vector beta=Vector(kint+1);
  102. NICE::Vector beta=Vector(kint+1);
  103. //random initialisation ?
  104. vmatrix.Set(0.0);
  105. if(n<=0) return;
  106. for (uint i=0;i<vmatrix[1].size();i++)
  107. // refactor-nice.pl: check this substitution
  108. // old: vmatrix[1][i]=RandomD();
  109. vmatrix(1, i)=RandomD();
  110. beta[0]=0;
  111. //iteration
  112. double delta=1.0;
  113. uint iteration=0;
  114. while(delta>mindelta && iteration<maxiterations)
  115. {
  116. // refactor-nice.pl: check this substitution
  117. // old: Vector check=vmatrix[kint];
  118. NICE::Vector check=vmatrix[kint];
  119. for(uint i=1;i<kint;i++)
  120. {
  121. //iteration step
  122. NICE::Vector tmp;
  123. data.multiply(tmp,vmatrix[i]);
  124. wmatrix[i]=tmp-(beta[i-1]*vmatrix[i-1]);//w_i=A * v[i] - beta_i * v_(i-1)
  125. alpha[i-1]=wmatrix[i]*vmatrix[i];
  126. wmatrix[i]=wmatrix[i]-(alpha[i-1]*vmatrix[i]);
  127. beta[i]=wmatrix[i].Length();
  128. vmatrix[i+1]=1.0/beta[i]*wmatrix[i];
  129. t(i-1, i-1)=alpha[i];
  130. if(i>1)
  131. {
  132. t(i-1, i-2)=beta[i-1];
  133. t(i-2, i-1)=beta[i-1];
  134. }
  135. }
  136. // refactor-nice.pl: check this substitution
  137. // old: Vector diff=check-vmatrix[kint];
  138. NICE::Vector diff=check-vmatrix[kint];
  139. delta=diff.Length();
  140. iteration++;
  141. }
  142. cout<<"Lanczos-Iterations:"<<iteration<<"/"<<maxiterations<<"relative Error:"<<delta<<endl;
  143. // refactor-nice.pl: check this substitution
  144. // old: Vector ice_eigval;
  145. NICE::Vector ice_eigval;
  146. // refactor-nice.pl: check this substitution
  147. // old: Matrix ice_eigvect;
  148. NICE::Matrix ice_eigvect;
  149. Eigenvalue ( t, ice_eigval, ice_eigvect );
  150. cerr << ice_eigval << endl;
  151. for (uint l=0;l<k;l++)
  152. eigenvectorsapprox[l]=vmatrix[l+1];
  153. eigenvectors=eigenvectorsapprox;
  154. for (uint i=0;i<(uint)eigenvectors.rows();i++)
  155. {
  156. // refactor-nice.pl: check this substitution
  157. // old: Vector tmp;
  158. NICE::Vector tmp;
  159. if ( eigenvectors[i].Length() < 1e-12 )
  160. {
  161. cerr << "Lanczos: numerical instability" << endl;
  162. } else {
  163. eigenvectors[i].Normalize();
  164. }
  165. data.multiply(tmp,eigenvectors[i]);
  166. eigenvalues[i]=tmp * eigenvectors[i];
  167. }
  168. }
  169. #endif