CASM  1.1.0
A Clusters Approach to Statistical Mechanics
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  public:
12 
15  template <typename Derived>
16  PCA(const Eigen::MatrixBase<Derived> &M, double singular_value_tol = 1e-14) {
17  // set the mean to zero
18  Eigen::MatrixXd mat_mean_zero = M;
19  mat_mean_zero.colwise() -= (M.rowwise().mean());
20 
21  // principle component analysis to rotate to input space that to its range
22  Eigen::JacobiSVD<Eigen::MatrixXd> svd(mat_mean_zero, Eigen::ComputeFullU);
23  int rank = 0;
24  for (Index i = 0; i < svd.singularValues().size(); i++) {
25  if (std::abs(svd.singularValues()[i]) <= singular_value_tol) {
26  break;
27  } else {
28  rank++;
29  }
30  }
31 
32  m_reduce = svd.matrixU().block(0, 0, M.rows(), rank).transpose();
33  }
34 
36  size_type dim() const { return m_reduce.cols(); }
37 
39  size_type rank() const { return m_reduce.rows(); }
40 
49  Eigen::MatrixXd reduce() const { return m_reduce; }
50 
59  Eigen::MatrixXd expand() const { return m_reduce.transpose(); }
60 
61  private:
62  // transform full dimension [input...] column vector onto subspace
63  // [range(input...)]
65 };
66 
73 inline Eigen::MatrixXd pad(const Eigen::MatrixXd &M, int n) {
74  Eigen::MatrixXd result = Eigen::MatrixXd::Zero(M.rows() + n, M.cols() + n);
75  result << M, Eigen::MatrixXd::Zero(M.rows(), n),
76  Eigen::MatrixXd::Zero(n, M.cols()), Eigen::MatrixXd::Identity(n, n);
77  return result;
78 }
79 
80 } // namespace CASM
81 
82 #endif
Principle component analysis.
Definition: PCA.hh:9
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:16
Eigen::MatrixXd m_reduce
Definition: PCA.hh:64
Eigen::MatrixXd expand() const
Orthogonal transformation matrix from a point in dimension-reduced [range(input......
Definition: PCA.hh:59
size_type dim() const
Initial dimension, equivalent to pca.reduce().cols()
Definition: PCA.hh:36
size_type rank() const
Rank of the input, equivalent to pca.reduce().rows()
Definition: PCA.hh:39
Eigen::MatrixXd reduce() const
Orthogonal transformation matrix from a point in full [input...] space to dimension-reduced [range(in...
Definition: PCA.hh:49
Eigen::MatrixXd::Index size_type
Definition: PCA.hh:11
IdentitySymRepBuilder Identity()
Main CASM namespace.
Definition: APICommand.hh:8
Eigen::MatrixXd MatrixXd
Eigen::MatrixXd pad(const Eigen::MatrixXd &M, int n)
Construct a matrix consisting of blocks M and Identity(n,n)
Definition: PCA.hh:73
INDEX_TYPE Index
For long integer indexing:
Definition: definitions.hh:39