Skip to content

Commit

Permalink
Implmented svd() in Matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
hosseinmoein committed Dec 14, 2024
1 parent a120a51 commit e574261
Show file tree
Hide file tree
Showing 6 changed files with 841 additions and 140 deletions.
279 changes: 205 additions & 74 deletions docs/HTML/Matrix.html

Large diffs are not rendered by default.

28 changes: 28 additions & 0 deletions include/DataFrame/DataFrame.h
Original file line number Diff line number Diff line change
Expand Up @@ -3759,6 +3759,34 @@ class DataFrame : public ThreadGranularity {
std::vector<const char *> &&col_names,
normalization_type norm_type = normalization_type::none) const;











// Principal Component Analysis (PCA)
//
template<typename T>
EigenSpace<T>
prin_comp_analysis(std::vector<const char *> &&col_names,
const PCAParams params = { }) const;












// This function returns a DataFrame indexed by std::string that provides
// a few statistics about the columns of the calling DataFrame.
// The statistics are:
Expand Down
22 changes: 22 additions & 0 deletions include/DataFrame/DataFrameTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,28 @@ struct StationaryTestParams {

// ----------------------------------------------------------------------------

enum class pca_method : unsigned char {

eigen = 1, // Eigen decomposition of the covariance matrix
svd = 2, // Singular Value Decomposition of the data matrix
};

struct PCAParams {

pca_method method { pca_method::eigen };
normalization_type norm_type { normalization_type::z_score };

// if populated, number of eigen components kept.
//
std::size_t num_comp_kept { 0 };

// if populated, percentage of eigen components kept -- 0.9 means 90%.
//
double pct_comp_kept { 0.9 };
};

// ----------------------------------------------------------------------------

template<typename T>
struct RandGenParams {

Expand Down
45 changes: 44 additions & 1 deletion include/DataFrame/Utils/Matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class Matrix {
//
Matrix
covariance(bool is_unbiased = true) const;

// Let A be an nXn matrix. The number l is an eigenvalue of A if there
// exists a non-zero vector v such that
// Av = lv
Expand Down Expand Up @@ -209,6 +209,40 @@ class Matrix {
MA2 &eigenvectors,
bool sort_values) const;

// In linear algebra, the Singular Value Decomposition (SVD) is an
// important factorization of a rectangular real or complex matrix,
// with several applications in signal processing and statistics.
// Applications which employ the SVD include computing the
// pseudoinverse, matrix approximation, and determining the rank,
// range and null space of a matrix.
//
// Suppose M is an mXn matrix whose entries come from the field K,
// which is either the field of real numbers or the field of complex
// numbers. Then there exists a factorization of the form
// M = U*Σ*~V
//
// where U is an mXm unitary matrix over K, the matrix Σ is mXn
// with nonnegative numbers on the diagonal (as defined for a
// rectangular matrix) and zeros off the diagonal, and ~V denotes the
// conjugate transpose of V (transpose of V in case of real matrices),
// an nXn unitary matrix over K. Such a factorization is called a
// Singular Value Decomposition of M.
//
// -- The matrix V thus contains a set of orthonormal "input" or
// "analysing" basis vector directions for M
// -- The matrix U contains a set of orthonormal "output" basis vector
// directions for M
// -- The matrix Σ contains the singular values, which can be thought
// of as scalar "gain controls" by which each corresponding input
// is multiplied to give a corresponding output.
//
// A common convention is to order the values Σi,i in non-increasing
// fashion. In this case, the diagonal matrix Σ is uniquely determined
// by M (though the matrices U and V are not).
//
template<typename MA1, typename MA2, typename MA3>
inline void svd(MA1 &U, MA2 &S, MA3 &V, bool full_size = true) const;

private:

static constexpr size_type NOPOS_ = static_cast<size_type>(-9);
Expand Down Expand Up @@ -1453,6 +1487,15 @@ operator * (const Matrix<T, MO1> &lhs, const Matrix<T, MO2> &rhs) {
return (result);
}

// ----------------------------------------------------------------------------

template<typename T>
struct EigenSpace {

Matrix<T, matrix_orient::row_major> eigen_vals { };
Matrix<T, matrix_orient::column_major> eigen_vecs { };
};

} // namespace hmdf

// ----------------------------------------------------------------------------
Expand Down
Loading

0 comments on commit e574261

Please sign in to comment.