source: trunk/test/test_pca.cc @ 13

Last change on this file since 13 was 13, checked in by daniel, 20 years ago

* empty log message *

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 2.4 KB
Line 
1// $Id: test_pca.cc 13 2003-06-19 10:00:28Z daniel $
2
3//Test program for SVD class.
4
5#include <fstream>
6#include <string>
7#include "PCA.h"
8
9using namespace std;
10
11template<class T>
12void break_line( vector<T>& collector, char* p_buff, const char& delimiter );
13void try_peters_test_file( const string& );
14
15int main()
16{
17  thep_cpp_tools::PCA* mypca = new thep_cpp_tools::PCA;
18  if( mypca->test() ) { delete mypca; return 0; }
19  else { delete mypca; return -1; }
20//   try_peters_test_file( "testdatapca/nm_trn.dat" );
21
22//   return 0;
23}
24
25
26// This function run PCA on testdata from peter.
27// The last column is excluded because looks suspicious
28// (only 1s and 2s e t c ). The function use get_line
29// which will limit the lengths of a line of the file
30// to MAXLINE (defined below).
31void try_peters_test_file( const string& filename )
32{
33  const size_t MAXLINE = 100000;
34  char* buff = new char[ MAXLINE ];
35  ifstream fin( filename.c_str() );
36  vector< vector<double> > data;
37
38  while( fin.peek() != EOF )
39    {
40     fin.getline( buff, MAXLINE );
41     vector<double> row;
42     break_line( row, buff, ' ' );
43     row.pop_back();
44     data.push_back( row );
45    }
46  delete[] buff;
47
48  const size_t cols = data.size();
49  assert( data.size() > 0 );
50  const size_t rows = data[0].size();
51 
52  thep_gsl_api::matrix A( cols, rows );
53
54  // Copy data into matrix A
55  for( size_t i = 0; i < cols; ++i )
56    {
57     for( size_t j = 0; j < rows; ++j )
58       {
59  A.set( i, j, data[i][j] );
60       }
61    }
62
63  //  cout << A << endl;
64
65  thep_cpp_tools::PCA* mypca = new thep_cpp_tools::PCA( A.transpose() );
66  mypca->process();
67
68
69  // The following functions print the eigenvalues with the explained
70  // intensity.
71  cout << "Eigenvalues \t Explained intensity\n";
72  for( size_t i = 0; i < 64; ++i )
73    {
74     printf( "%6.6f \t %6.6f \n", mypca->get_eigenvalue( i ), 
75       mypca->get_explained_intensity( i ) );
76     //  cout << mypca->get_eigenvector( i ) << endl;
77    }
78  delete mypca;
79}
80
81
82
83template<class T>
84void break_line( vector<T>& row, char* p_buff, const char& delimiter )
85{
86  while( true )
87    {
88     string number = "";
89     while( *p_buff++ != '\0' )
90       {
91  if( *p_buff == delimiter )
92    {
93     if( number.size() > 0 )
94       {
95        row.push_back( atof( number.c_str() ) );
96        break;
97       }
98    }
99  else
100    {
101     number += *p_buff;
102    }
103       }
104
105     if( *p_buff == '\0' ) 
106       {
107  // Add last number
108  if( number.size() > 0 )
109    {
110     row.push_back( atof( number.c_str() ) );
111    }
112  break;
113       }
114    }
115}
Note: See TracBrowser for help on using the repository browser.