-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocalPower_serial.cpp
More file actions
39 lines (33 loc) · 960 Bytes
/
LocalPower_serial.cpp
File metadata and controls
39 lines (33 loc) · 960 Bytes
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
#include <Eigen/Dense>
#include <iostream>
MatrixXd GS_Ortho(const MatrixXd& A) {
int m = A.rows();
int n = A.cols();
MatrixXd Q = MatrixXd::Zero(m, n);
VectorXd u, q;
for (int j = 0; j < n; ++j) {
u = A.col(j);
for (int i = 0; i < j; ++i) {
q = Q.col(i);
u -= (q.dot(A.col(j)) / q.dot(q)) * q;
}
Q.col(j) = u / u.norm();
}
return Q;
}
int main () {
// Initialise matrix A
MatrixXd A;
// Number of singular values to approximate
int k = 5;
// start global timer
timer t1;
t1.start();
// Compute the SVD decomposition
JacobiSVD<MatrixXd> svd(A, ComputeThinU | ComputeThinV);
// Compute the truncated SVD matrix: A_k: O(n^2 k)
MatrixXd U_k = svd.matrixU().leftCols(k);
MatrixXd S_k = svd.singularValues().head(k).asDiagonal();
MatrixXd V_kT = svd.MatrixV().leftCols(k).transpose();
MatrixXd A_k = U_k * S_k * V_kT;
}