61 typedef void (*U_fp)(...);
63 int dsyev_(
char *jobz,
char *uplo,
long int *n,
double *a,
64 long int *lda,
double *w,
double *work,
long int *lwork,
73 static double * p_work_double = 0;
76 static int m_workSize_double = 0;
84 template <
typename VectorT >
87 pca(_points,_first,_second,_third);
90 template <
typename VectorT >
93 if ( p_work_double != 0){
94 delete [] p_work_double;
96 m_workSize_double = 0;
100 template <
typename VectorT >
108 for (uint i = 0 ; i < _points.size() ; ++i ) {
109 cog = cog + _points[i];
112 cog = cog / (
typename VectorT::value_type )_points.size();
117 template <
typename VectorT >
120 std::vector< double > & _vec_EV )
124 long int lwork, info;
129 n = gmm::mat_nrows( _mat_A );
135 dsyev_( &jobz, &uplo, &n, &_mat_A( 0, 0 ), &lda, &_vec_EV[ 0 ],
136 &size, &lwork, &info );
142 if( (
long int) size > m_workSize_double )
145 delete [] p_work_double;
147 m_workSize_double = (
long int) size;
149 p_work_double=
new double[ m_workSize_double ];
151 lwork = m_workSize_double;
154 dsyev_( &jobz, &uplo, &n, &_mat_A( 0, 0 ), &lda, &_vec_EV[ 0 ],
155 p_work_double, &lwork, &info );
161 gmm::copy( _mat_A, _mat_VR );
167 template <
typename VectorT >
172 const VectorT cog = center_of_gravity(_points);
175 Matrix points(3 , _points.size() );
177 for ( uint i = 0 ; i < _points.size() ; ++i)
179 points(0 , i ) = ( _points[i] - cog) [0];
180 points(1 , i ) = ( _points[i] - cog) [1];
181 points(2 , i ) = ( _points[i] - cog) [2];
185 gmm::mult(points,gmm::transposed(points),cov );
188 std::vector< double > ew(3);
191 if ( !SymRightEigenproblem( cov, EV ,ew ) )
192 std::cerr <<
"Error: Could not compute Eigenvectors for PCA" << std::endl;
194 int maximum,middle,minimum;
196 if ( (ew[0] > ew[1]) && (ew[0] > ew[2]) ) {
199 if ( (ew[1] > ew[0]) && (ew[1] > ew[2]) )
205 if ( (ew[0] < ew[1]) && (ew[0] < ew[2]) )
207 else if ( (ew[1] < ew[0]) && (ew[1] < ew[2]) )
212 if ( (minimum != 0 ) && ( maximum != 0 ) )
215 if ( (minimum != 1 ) && ( maximum != 1 ) )
220 _first =
VectorT( EV(0,maximum) , EV(1,maximum) , EV(2,maximum) );
221 _second =
VectorT( EV(0,middle) , EV(1,middle) , EV(2,middle ) );
223 _first = _first.normalize();
224 _second = _second.normalize();
225 _third = _first % _second;
228 lastEigenValues_.clear();
229 lastEigenValues_.push_back( ew[maximum] );
230 lastEigenValues_.push_back( ew[middle] );
231 lastEigenValues_.push_back( ew[minimum] );
235 template <
typename VectorT >
240 return lastEigenValues_;
Class for principal component Analysis.
VectorT center_of_gravity(const std::vector< VectorT > &_points)
Namespace for principal Component Analysis.
void pca(std::vector< VectorT > &_points, VectorT &_first, VectorT &_second, VectorT &_third)
Classes for doing Principal component analysis.