CASM
AClustersApproachtoStatisticalMechanics
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
PCA.hh
Go to the documentation of this file.
1 #ifndef CASM_PCA
2 #define CASM_PCA
3 
4 #include "casm/external/Eigen/Dense"
5 
6 namespace CASM {
7 
9  class PCA {
10 
11  public:
12 
14 
15 
17  template<typename Derived>
18  PCA(const Eigen::MatrixBase<Derived> &M, double singular_value_tol = 1e-14) {
19 
20  // set the mean to zero
21  Eigen::MatrixXd mat_mean_zero = M;
22  mat_mean_zero.colwise() -= (M.rowwise().mean());
23 
24  // principle component analysis to rotate to input space that to its range
25  Eigen::JacobiSVD<Eigen::MatrixXd> svd(mat_mean_zero, Eigen::ComputeFullU);
26  int rank = 0;
27  for(Index i = 0; i < svd.singularValues().size(); i++) {
28  if(std::abs(svd.singularValues()[i]) <= singular_value_tol) {
29  break;
30  }
31  else {
32  rank++;
33  }
34  }
35 
36  m_reduce = svd.matrixU().block(0, 0, M.rows(), rank).transpose();
37 
38  }
39 
41  size_type dim() const {
42  return m_reduce.cols();
43  }
44 
46  size_type rank() const {
47  return m_reduce.rows();
48  }
49 
59  return m_reduce;
60  }
61 
71  return m_reduce.transpose();
72  }
73 
74 
75  private:
76 
77  // transform full dimension [input...] column vector onto subspace [range(input...)]
79 
80  };
81 
88  inline Eigen::MatrixXd pad(const Eigen::MatrixXd &M, int n) {
89  Eigen::MatrixXd result = Eigen::MatrixXd::Zero(M.rows() + n, M.cols() + n);
90  result << M, Eigen::MatrixXd::Zero(M.rows(), n),
91  Eigen::MatrixXd::Zero(n, M.cols()), Eigen::MatrixXd::Identity(n, n);
92  return result;
93  }
94 
95 }
96 
97 
98 #endif
99 
Eigen::MatrixXd MatrixXd
Eigen::MatrixXd m_reduce
Definition: PCA.hh:78
Eigen::MatrixXd pad(const Eigen::MatrixXd &M, int n)
Construct a matrix consisting of blocks M and Identity(n,n)
Definition: PCA.hh:88
Eigen::MatrixXd::Index size_type
Definition: PCA.hh:13
size_type rank() const
Rank of the input, equivalent to pca.reduce().rows()
Definition: PCA.hh:46
PCA(const Eigen::MatrixBase< Derived > &M, double singular_value_tol=1e-14)
Constructor accepting a column vector matrix M containing points [input...].
Definition: PCA.hh:18
Main CASM namespace.
Definition: complete.cpp:8
Principle component analysis.
Definition: PCA.hh:9
EigenIndex Index
For long integer indexing:
Eigen::MatrixXd reduce() const
Orthogonal transformation matrix from a point in full [input...] space to dimension-reduced [range(in...
Definition: PCA.hh:58
size_type dim() const
Initial dimension, equivalent to pca.reduce().cols()
Definition: PCA.hh:41
Eigen::MatrixXd expand() const
Orthogonal transformation matrix from a point in dimension-reduced [range(input...)] space to full [input...] space.
Definition: PCA.hh:70