123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- #include <iostream>
- #include "EigValues.h"
- #define DEBUG_ARNOLDI
- using namespace NICE;
- using namespace std;
- void
- EVArnoldi::getEigenvalues ( const GenericMatrix & data, Vector & eigenvalues,
- Matrix & eigenvectors, uint k )
- {
-
-
-
- if ( data.rows () != data.cols () )
- {
- throw ( "EVArnoldi: matrix has to be quadratic" );
- }
-
- if ( k <= 0 )
- {
- throw ( "EVArnoldi: please use k>0." );
- }
-
-
- if ( k > data.rows() )
- {
- throw ( "EVArnoldi: specified k is larger then dimension of matrix! Aborting..." );
- }
-
-
-
-
- if ( verbose )
- cerr << "Initialize Matrices" << std::endl;
- uint n = data.cols ();
-
- if ( verbose )
- std::cerr << "EVArnoldi: n: " << n << " k: " << k << std::endl;
-
- NICE::Matrix rmatrix ( n, k, 0 );
- NICE::Matrix qmatrix ( n, k, 0 );
- eigenvalues.resize ( k );
- NICE::Vector q ( n );
- NICE::Vector r ( n );
- if ( verbose )
- cerr << "Random Initialization" << endl;
-
- for ( uint i = 0; i < k; i++ )
- for ( uint j = 0; j < n; j++ )
- #ifdef WIN32
- rmatrix (j, i) = double(rand())/RAND_MAX;
- #else
- rmatrix (j, i) = drand48 ();
- #endif
-
-
-
-
- if ( verbose )
- std::cerr << "EVArnoldi: start main computation" << std::endl;
-
-
- double delta = 1.0;
- uint iteration = 0;
- while ( delta > mindelta && iteration < maxiterations )
- {
-
-
- NICE::Matrix rold(rmatrix);
-
- if ( verbose )
- std::cerr << "EVArnoldi: start for loop over reduced dims" << std::endl;
-
-
-
- for ( uint reduceddim = 0; reduceddim < k; reduceddim++ )
- {
-
- q = rmatrix.getColumn ( reduceddim );
-
- q.normalizeL2 ();
-
- qmatrix.getColumnRef ( reduceddim ) = q;
-
-
- Vector currentCol = rmatrix.getColumnRef ( reduceddim );
-
- data.multiply ( currentCol, q );
-
- for ( uint j = 0; j < reduceddim; j++ )
- rmatrix.getColumnRef ( reduceddim ) -=
- qmatrix.getColumn ( j ) *
- ( qmatrix.getColumn ( j ).
- scalarProduct ( rmatrix.getColumn ( reduceddim ) ) );
- }
-
- if ( verbose )
- std::cerr << "EVArnoldi: ended for loop over reduced dims" << std::endl;
-
-
-
-
- NICE::Vector tmpDiff;
- double norm_tmpDiff;
- delta = 0.0;
- for ( uint j = 0; j < k; j++ )
- {
- tmpDiff = rold.getColumn(j) - rmatrix.getColumn(j);
- norm_tmpDiff = tmpDiff.normL2();
- if (norm_tmpDiff > delta)
- delta = norm_tmpDiff;
- }
- iteration++;
- if ( verbose )
- cerr << "EVArnoldi: [" << iteration << "] delta=" << delta << endl;
- }
-
- if ( verbose )
- std::cerr << "EVArnoldi: while-loop done" << std::endl;
-
- eigenvectors = rmatrix;
-
- for ( uint i = 0; i < k; i++ )
- {
- NICE::Vector tmp;
- eigenvectors.getColumnRef ( i ).normalizeL2 ();
- data.multiply ( tmp, eigenvectors.getColumn ( i ) );
- eigenvalues[i] = tmp.scalarProduct ( eigenvectors.getColumn ( i ) );
- }
-
-
-
- if ( this->b_verifyDecreasingOrder && (k > 1) )
- {
- NICE::VectorT< int > ewPermutation;
- eigenvalues.sortDescend ( ewPermutation );
-
- NICE::Vector tmpRow;
- int tmpIdx (-1);
- for ( uint i = 0; i < k; i++ )
- {
- if ( i == ewPermutation[i] )
- {
-
- }
- else
- {
- if ( tmpIdx == -1 )
- {
- tmpIdx = i;
- tmpRow = eigenvectors.getColumn ( i );
- eigenvectors.getColumnRef ( i ) = eigenvectors.getColumn ( ewPermutation[i] );
- }
- else
- {
- if ( tmpIdx != ewPermutation[i] )
- {
- eigenvectors.getColumnRef ( i ) = eigenvectors.getColumn ( ewPermutation[i] );
- }
- else
- {
- eigenvectors.getColumnRef ( i ) = tmpRow;
- tmpIdx = -1;
- }
- }
- }
- }
-
- }
- }
|