Changeset 287


Ignore:
Timestamp:
Apr 21, 2005, 11:50:05 PM (16 years ago)
Author:
Peter
Message:

moved specific test code to test_pca

Location:
trunk/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/PCA.cc

    r227 r287  
    1515namespace cpptools { 
    1616
    17 PCA::PCA() : process_( false ), explained_calc_( false )
    18 {
    19 }
     17  //PCA::PCA() : process_( false ), explained_calc_( false ){}
    2018
    2119PCA::PCA( const gslapi::matrix& A ) : A_( A ),
     
    220218}
    221219
    222 
    223 bool PCA::test()
    224 {
    225   std::cout << "1. Print original matrix A = \n";
    226   gslapi::matrix A( 3, 4 );
    227   for( size_t i = 0; i < 3; ++i )
    228     for( size_t j = 0; j < 4; ++j )
    229       A(i,j)= sin( static_cast<double>(i+j+2+(i+1)*(j+1)) );
    230   A_=A;
    231 
    232 // Print only matrix that is row-centered as PCA ( process() )
    233 // will do that before calculation
    234   this->row_center( A );
    235   std::cout << A << std::endl;
    236 
    237   std::cout << "2. Testing PCA\n";
    238   this->process_transposed_problem();
    239 
    240   for( size_t i = 0; i < eigenvalues_.size(); ++i )
    241     {
    242      std::cout << "lambda(" << i << ") = " << eigenvalues_[i] << " "
    243     << "has eigenvector = " << std::endl;
    244      std::cout << eigenvectors_[i] << std::endl;
    245     }
    246  
    247   // The eigenvalues and eigenvectors are
    248   // from matlab using eig(A'A)/3
    249 //   double answer_lambda[ 3 ] = { 1.8908, 0.0327, 0 };
    250 //   double answer_eigenvec[ 3 ][ 3 ] =
    251 //     {
    252 //       { 0.4763, -0.6738, 0.5649  },
    253 //       { 0.8770, 0.3182,  -0.3599 },
    254 //       { 0.0627, 0.6669 , 0.7425  },
    255 //     };
    256 
    257 //   double ERROR_TOL = 0.0001;
    258 
    259 //   // error eigenvalues
    260 //   cout << "abs( lambda(i) - lambda_matlab(i) ) ) = \n( ";
    261 //   double max_err = 0;
    262 //   for( size_t i = 0; i < 3; ++i )
    263 //     {
    264 //      double err = fabs( eigenvalues_[ i ] - answer_lambda[ i ] );
    265 //      if( err > max_err ) max_err = err;
    266 //      cout << err << " ";
    267 //     }
    268 //   cout << ")\n";
    269 
    270 //   cerr << "max_error = " << max_err << ", to be compared with ERROR_TOL = "
    271 //        << ERROR_TOL << endl;
    272 //   if( max_err < ERROR_TOL )
    273 //     {
    274 //      cout << "ok, compared with precision in given matlab-references!\n";
    275 //     }
    276 //   else
    277 //     {
    278 //      cerr << "Not ok, error to big!\n";
    279 //      return false;
    280 //     }
    281 
    282 //   // error eigenvectors
    283 //   max_err = 0;
    284 //   for( size_t i = 0; i < 3; ++i )
    285 //     {
    286 //      cout << "err_eigvec( " << i << " ) = \n";
    287 //      for( size_t j = 0; j < 3; ++j )
    288 //        {
    289 //  double err = fabs( fabs( eigenvectors_.get( i, j ) ) - fabs( answer_eigenvec[ i ][ j ] ) );
    290 //  if( err > max_err ) max_err = err;       
    291 //  cout << err << endl;
    292 //        }
    293 //      cout << ")\n\n";
    294 //     }
    295 
    296 //   cerr << "max_error = " << max_err << ", to be compared with ERROR_TOL = "
    297 //        << ERROR_TOL << endl;
    298 //   if( max_err < ERROR_TOL )
    299 //     {
    300 //      cout << "ok, compared with precision in given matlab-references!\n";
    301 //     }
    302 //   else
    303 //     {
    304 //      cerr << "Not ok, error to big!\n";
    305 //      return false;
    306 //     }
    307    
    308   std::cout << "\nThe following projection is obtained:\n";
    309   std::cout << this->projection_transposed( A_ ).transpose() << std::endl;
    310  
    311 
    312   return true;
    313 }
    314 
    315220}} // of namespace cpptools and namespace theplu
  • trunk/src/PCA.h

    r42 r287  
    3434  public:
    3535    /**
    36        This constructor is only to be used in test-class
     36       Default constructor (not implemented)
    3737    */
    3838    PCA();
     
    6464    void process_transposed_problem();
    6565   
    66     /**
    67        Performes a simple test on performance. Not optimal!
    68        Returns true if ok otherwise false.
    69     */
    70     bool test();
    71 
    72 
    7366    /**
    7467       Returns eigenvector \a i
Note: See TracChangeset for help on using the changeset viewer.