-
Notifications
You must be signed in to change notification settings - Fork 0
/
redsvd.hpp
185 lines (144 loc) · 4.26 KB
/
redsvd.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
* Copyright (c) 2010 Daisuke Okanohara
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above Copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above Copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the authors nor the names of its contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*/
#ifndef REDSVD_HPP__
#define REDSVD_HPP__
#define EIGEN_YES_I_KNOW_SPARSE_MODULE_IS_NOT_STABLE_YET
#include <vector>
#include <eigen3/Eigen/Sparse>
#include <eigen3/Eigen/Dense>
#include <eigen3/Eigen/Eigenvalues>
#include "util.hpp"
namespace REDSVD {
class RedSVD {
public:
RedSVD(){}
template <class Mat>
RedSVD(Mat& A){
int r = (A.rows() < A.cols()) ? A.rows() : A.cols();
run(A, r);
}
template <class Mat>
RedSVD(Mat& A, const int rank){
run(A, rank);
}
template <class Mat>
void run(Mat& A, const int rank){
if (A.cols() == 0 || A.rows() == 0) return;
int r = (rank < A.cols()) ? rank : A.cols();
r = (r < A.rows()) ? r : A.rows();
// Gaussian Random Matrix for A^T
Eigen::MatrixXd O(A.rows(), r);
Util::sampleGaussianMat(O);
// Compute Sample Matrix of A^T
Eigen::MatrixXd Y = A.transpose() * O;
// Orthonormalize Y
Util::processGramSchmidt(Y);
// Range(B) = Range(A^T)
Eigen::MatrixXd B = A * Y;
// Gaussian Random Matrix
Eigen::MatrixXd P(B.cols(), r);
Util::sampleGaussianMat(P);
// Compute Sample Matrix of B
Eigen::MatrixXd Z = B * P;
// Orthonormalize Z
Util::processGramSchmidt(Z);
// Range(C) = Range(B)
Eigen::MatrixXd C = Z.transpose() * B;
Eigen::JacobiSVD<Eigen::MatrixXd> svdOfC(C, Eigen::ComputeThinU | Eigen::ComputeThinV);
// C = USV^T
// A = Z * U * S * V^T * Y^T()
matU_ = Z * svdOfC.matrixU();
matS_ = svdOfC.singularValues();
matV_ = Y * svdOfC.matrixV();
}
const Eigen::MatrixXd& matrixU() const {
return matU_;
}
const Eigen::VectorXd& singularValues() const {
return matS_;
}
const Eigen::MatrixXd& matrixV() const {
return matV_;
}
private:
Eigen::MatrixXd matU_;
Eigen::VectorXd matS_;
Eigen::MatrixXd matV_;
};
class RedSymEigen {
public:
RedSymEigen(){}
template <class Mat>
RedSymEigen(Mat& A, const int rank){
run(A, rank);
}
template <class Mat>
void run(Mat& A, const int rank){
if (A.cols() == 0 || A.rows() == 0) return;
int r = (rank < A.cols()) ? rank : A.cols();
r = (r < A.rows()) ? r : A.rows();
// Gaussian Random Matrix
Eigen::MatrixXd O(A.rows(), r);
Util::sampleGaussianMat(O);
// Compute Sample Matrix of A
Eigen::MatrixXd Y = A.transpose() * O;
// Orthonormalize Y
Util::processGramSchmidt(Y);
Eigen::MatrixXd B = Y.transpose() * A * Y;
Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd> eigenOfB(B);
eigenValues_ = eigenOfB.eigenvalues();
eigenVectors_ = Y * eigenOfB.eigenvectors();
}
const Eigen::MatrixXd& eigenVectors() const {
return eigenVectors_;
}
const Eigen::VectorXd& eigenValues() const {
return eigenValues_;
}
private:
Eigen::VectorXd eigenValues_;
Eigen::MatrixXd eigenVectors_;
};
class RedPCA {
public:
RedPCA(){}
template <class Mat>
RedPCA(const Mat& A, const int rank) {
run(A, rank);
}
template <class Mat>
void run(const Mat& A, const int rank) {
RedSVD redsvd;
redsvd.run(A, rank);
const Eigen::VectorXd& S = redsvd.singularValues();
principalComponents_ = redsvd.matrixV();
scores_ = redsvd.matrixU() * S.asDiagonal();
}
const Eigen::MatrixXd& principalComponents() const {
return principalComponents_;
}
const Eigen::MatrixXd& scores() const {
return scores_;
}
private:
Eigen::MatrixXd principalComponents_;
Eigen::MatrixXd scores_;
};
}
#endif // REDSVD_HPP__