25 #include <MathAlgo/PCA.hh>
26 #include <MathAlgo/SVD.hh>
27 #include <Base/Common/BIASpragma.hh>
37 for (
unsigned int i=0; i<vec.size(); i++) {
38 counter += vec[i].size();
40 vector<Vector<PCAType> > newvec;
41 newvec.reserve(counter);
42 for (
unsigned int i=0; i<vec.size(); i++) {
43 const vector<Vector<PCAType> > &partvec = vec[i];
44 for (
unsigned int j=0; j<partvec.size(); j++) {
45 newvec.push_back(partvec[j]);
48 ComputeReductionMatrix(newvec, matrix, reductionSize);
55 if (reductionSize==-1) reductionSize = reductionSize_;
56 BIASASSERT(reductionSize>0);
58 const int dimensions=vec[0].size();
65 ComputeMean(vec, mean_);
66 ComputeScatter(vec, mean_, cov);
67 ComputeReductionMatrix(cov, matrix,
false, reductionSize);
76 if (reductionSize==-1) reductionSize = reductionSize_;
77 BIASASSERT(reductionSize>0);
81 for (
int i=0; i<cov.
num_cols(); i++){
82 for (
int j=0; j<=i; j++){
84 covd[i][j] = covd[j][i] = double( cov[j][i] ) + double( cov[i][j] ) ;
99 for (
int i=0;i<matrix.
num_cols();i++){
100 for (
int j=0;j<matrix.
num_rows();j++){
101 matrix[j][i]= PCAType( U[i][j] );
111 for (
int k=0; k<(int)vec.size(); k++){
113 for (
int i=0; i<veck.
size(); i++){
114 const PCAType &vecki = veck[i];
115 for (
int j=0; j<=i; j++){
116 cov[i][j] += (vecki-mean[i]) * (veck[j]-mean[j]);
120 for (
int i=0; i<cov.
num_rows(); i++){
121 for (
int j=i+1; j<cov.
num_cols(); j++){
122 cov[i][j] = cov[j][i];
129 BIASASSERT(!vec.empty());
133 for (
int k=0; k<(int)vec.size(); k++){
135 for (
int i=0; i<veck.
size(); i++){
140 const PCAType vecsize = 1.0f / PCAType(vec.size());
141 for (
int i=0; i<mean.
size(); i++){
147 reductionSize_ = size;
void ComputeScatter(const std::vector< BIAS::Vector< PCAType > > &vec, const BIAS::Vector< PCAType > &mean, BIAS::Matrix< PCAType > &cov)
compute unnomalized covariance
computes and holds the singular value decomposition of a rectangular (not necessarily quadratic) Matr...
Subscript num_cols() const
void SetReductionSize(int size)
Matrix< T > & newsize(Subscript M, Subscript N)
void SetZero()
equivalent to matrix call
Vector< T > & newsize(Subscript N)
void GetMean(BIAS::Vector< PCAType > &mean)
get mean of a vector
const Matrix< double > & GetVT() const
return VT (=transposed(V))
const Vector< double > & GetS() const
return S which is a vector of the singular values of A in descending order.
matrix class with arbitrary size, indexing is row major.
void ComputeReductionMatrix(const std::vector< BIAS::Vector< PCAType > > &vec, BIAS::Matrix< PCAType > &matrix, int reductionSize=-1)
Computes a reduction-matrix.
Subscript num_rows() const
void ComputeMean(const std::vector< BIAS::Vector< PCAType > > &vec, BIAS::Vector< PCAType > &mean)
computes mean of a set of vectors
void NormalizeRows()
Normalizes each row to L2 norm one.