-
Notifications
You must be signed in to change notification settings - Fork 0
/
svd.cpp
448 lines (327 loc) · 12 KB
/
svd.cpp
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#include "svd.h"
svd::svd() {
track = true;
}
void svd::Factorization(std::vector<std::vector<double>> A, std::vector<std::vector<double>>& s,
std::vector<std::vector<double>>& u, std::vector<std::vector<double>>& v)
{
std::vector<std::vector<double>> A_t, A_t_x_A, u_1, v_1, s_inverse, av_matrix;
std::vector<double> eigenvalues;
print_m(A, "getting matrix");
transpose_matr(A, A_t);
prod_matrix(A_t, A, A_t_x_A);
GetEigenvalsEigenvecs(A_t_x_A, eigenvalues, v_1, 0);
transpose_matr(v_1, v);
s.resize(A.size());
const std::size_t e_size = eigenvalues.size();
for (std::size_t index = 0; index < e_size; index++)
{
s[index].resize(e_size);
s[index][index] = eigenvalues[index];
}
InverseDiagMatrix(s, s_inverse);
prod_matrix(A, v, av_matrix);
prod_matrix(av_matrix, s_inverse, u);
if (track) {
std::cout << "\nS = \n"; print_m(s, "diag matrix");
std::cout << "\nU = \n"; print_m(u, "left factor");
std::cout << "\nV = \n"; print_m(v, "right factor");
}
}
// Inverse Diagonal Matrix that is not necessary non-degenerate
void svd::InverseDiagMatrix(std::vector<std::vector<double>> A,
std::vector<std::vector<double>>& inv_A)
{
const std::size_t m_size = A.size();
inv_A.resize(m_size);
for (std::size_t index = 0; index < m_size; index++)
inv_A[index].resize(A[index].size());
for (std::size_t index = 0; index < m_size; index++) {
if (A[index][index] != 0.) {
inv_A[index][index] = 1.0/A[index][index];
}
else {
inv_A[index][index] = 0.;
}
}
}
void svd::prod_matrix(std::vector<std::vector<double>> A,
std::vector<std::vector<double>>& B, std::vector<std::vector<double>>& prod){
prod.resize(A.size());
for (std::size_t row = 0; row < A.size(); row++){
prod[row].resize(A[row].size());
std::fill(prod[row].begin(), prod[row].end(), 0);
}
std::size_t m_size = A.size();
for (std::size_t row = 0; row < m_size; row++)
for (std::size_t col = 0; col < m_size; col++){
for (std::size_t k = 0; k < m_size; k++)
prod[row][col] += A[row][k] * B[k][col];
}
}
void svd::transpose_matr(std::vector<std::vector<double>> A,
std::vector<std::vector<double>>& A_t){
std::size_t m_size = A.size();
A_t.resize(m_size);
for (auto& m : A)
m.resize(m_size);
for (std::size_t row = 0; row < m_size; row++)
A_t[row].resize(A[row].size());
for (std::size_t row = 0; row < m_size; row++)
for (std::size_t col = 0; col < m_size; col++)
A_t[row][col] = A[col][row];
// print_m(A_t, "transpose");
}
int RandomNumber() { return (std::rand() % 10000); }
// Recursive computation of eigenvalues and eigen-vectors
// Compute eigenvalue[i] and eigen_vecs[i], where i = eig_count
void svd::GetEigenvalsEigenvecs(std::vector<std::vector<double>> matrix,
std::vector<double>& eigenvalues,
std::vector<std::vector<double>>& eigen_vecs,
std::size_t eig_count){
const std::size_t m_size = matrix.size();
std::vector<double> vec; vec.resize(m_size);
std::generate(vec.begin(), vec.end(), RandomNumber);
if (eigenvalues.size() == 0 && eigen_vecs.size() == 0)
{
eigenvalues.resize(m_size);
eigen_vecs.resize(eigenvalues.size());
matrix_i = matrix;
}
std::vector<double> m; m.resize(m_size);
std::vector<double> m_temp; m_temp.resize(m_size);
double lambda_old = 0;
// Power Iteration algoritm for finding eigenvalues of the symmetric matrix A^t*A
// The solution is obtained as (A^t*A)^\infinity * arbitrary vector
std::size_t index = 0; bool is_eval = false;
while (is_eval == false)
{
// m - will be eigenvector
for (std::size_t row = 0; row < m_size; row++)
m[row] = 0.f;
for (std::size_t row = 0; row < m_size; row++)
{
for (std::size_t col = 0; col < m_size; col++)
m[row] += matrix[row][col] * vec[col];
}
for (std::size_t col = 0; col < m_size; col++)
vec[col] = m[col];
if (index > 0)
{
// finish compute eigenvalue if lambda almost const
double lambda = (index > 0) ? (m[0] / m_temp[0]) : m[0];
is_eval = (fabs(lambda - lambda_old) <= 10e-10) ? true : false;
eigenvalues[eig_count] = lambda;
lambda_old = lambda;
}
for (std::size_t row = 0; row < m_size; row++)
m_temp[row] = m[row];
index++;
}
if (track)
print_eigenv(eigenvalues, "is_eval == true");
std::vector<std::vector<double>> matrix_new;
if (m_size > 1)
{
std::vector<std::vector<double>> M_target;
M_target.resize(m_size);
for (std::size_t row = 0; row < m_size; row++)
M_target[row].resize(m_size);
// M_target is (A^t * A - eigval*I)
for (std::size_t row = 0; row < m_size; row++)
for (std::size_t col = 0; col < m_size; col++)
M_target[row][col] = (row == col) ? \
(matrix[row][col] - eigenvalues[eig_count]) : matrix[row][col];
// Get eigen_vecs[i]
std::vector<double> eigen_vec;
GaussJordanElimination(M_target, eigen_vec);
// Matrix H - Conjugate for matrix A^t*A
std::vector<std::vector<double>> H;
ConjugateFor_M_target(eigen_vec, H);
std::vector<std::vector<double>> H_matrix_prod;
prod_matrix(H, matrix, H_matrix_prod);
// inverse matrix for H: inv_H
std::vector<std::vector<double>> inv_H;
InverseConjugateFor_M_target(eigen_vec, inv_H);
std::vector<std::vector<double>> inv_H_matrix_prod;
// Here, we get inv_H_matrix_prod = H * M * H^-1
prod_matrix(H_matrix_prod, inv_H, inv_H_matrix_prod);
// matrix_new = H * M * H^-1 is equalent to A_t_x_A without first row and first col
ReduceMatrix(inv_H_matrix_prod, matrix_new, m_size - 1);
}
if (m_size <= 1)
{
for (std::size_t index = 0; index < eigenvalues.size(); index++)
{
double lambda = eigenvalues[index];
// M_target is (A^t * A - eigval*I)
std::vector<std::vector<double>> M_target;
M_target.resize(matrix_i.size());
for (std::size_t row = 0; row < matrix_i.size(); row++)
M_target[row].resize(matrix_i.size());
std::size_t mi_size = matrix_i.size();
for (std::size_t row = 0; row < mi_size; row++)
for (std::size_t col = 0; col < mi_size; col++)
M_target[row][col] = (row == col) ? \
(matrix_i[row][col] - lambda) : matrix_i[row][col];
eigen_vecs.resize(matrix_i.size());
GaussJordanElimination(M_target, eigen_vecs[index]);
// Normalize eigen vectors
double eigsum_sq = 0;
for (std::size_t v = 0; v < eigen_vecs[index].size(); v++)
eigsum_sq += std::pow(eigen_vecs[index][v], 2.0);
for (std::size_t v = 0; v < eigen_vecs[index].size(); v++)
eigen_vecs[index][v] /= std::sqrt(eigsum_sq);
// Essentially eigenvalues[index] should be positive for symmetric matrix,
// However, negative values or very small values
// may occur due to the accuracy of the calculations.
if (eigenvalues[index] < 10e-5)
eigenvalues[index] = 0;
eigenvalues[index] = std::sqrt(eigenvalues[index]);
}
return;
}
GetEigenvalsEigenvecs(matrix_new, eigenvalues, eigen_vecs, eig_count + 1);
return;
}
// Discard first row and first column
void svd::ReduceMatrix(std::vector<std::vector<double>> matrix,
std::vector<std::vector<double>>& reduced_matr, std::size_t new_size){
if (new_size > 1)
{
reduced_matr.resize(new_size);
std::size_t index_d = matrix.size() - new_size;
std::size_t row = index_d, row_n = 0;
while (row < matrix.size())
{
reduced_matr[row_n].resize(new_size);
std::size_t col = index_d, col_n = 0;
while (col < matrix.size())
reduced_matr[row_n][col_n++] = matrix[row][col++];
row++; row_n++;
}
}
else if (new_size == 1)
{
reduced_matr.resize(new_size);
reduced_matr[0].resize(new_size);
reduced_matr[0][0] = matrix[1][1];
}
}
void svd::ConjugateFor_M_target(std::vector<double> eigen_vec,
std::vector<std::vector<double>>& h_matrix){
h_matrix.resize(eigen_vec.size());
for (std::size_t row = 0; row < eigen_vec.size(); row++)
h_matrix[row].resize(eigen_vec.size());
h_matrix[0][0] = 1.0 / eigen_vec[0];
for (std::size_t row = 1; row < eigen_vec.size(); row++)
h_matrix[row][0] = -eigen_vec[row] / eigen_vec[0];
for (std::size_t row = 1; row < eigen_vec.size(); row++)
h_matrix[row][row] = 1;
}
void svd::InverseConjugateFor_M_target(std::vector<double> eigen_vec, std::vector<std::vector<double>>& ih_matrix){
ih_matrix.resize(eigen_vec.size());
for (std::size_t row = 0; row < eigen_vec.size(); row++)
ih_matrix[row].resize(eigen_vec.size());
ih_matrix[0][0] = eigen_vec[0];
for (std::size_t row = 1; row < eigen_vec.size(); row++)
ih_matrix[row][0] = -eigen_vec[row];
for (std::size_t row = 1; row < eigen_vec.size(); row++)
ih_matrix[row][row] = 1;
}
// Gauss Jordan elimination algorithm --> reduce the matrix to row echelon form.
// 1 0 0 v1
// 0 1 0 v2
// 0 0 1 v3
// 0 0 0 0
// The solution of Av 0 is (-v1, -v2, -v3, 1)^t
// The eigenvector v is optaibed up to factor, so the last element may be set to 1
// Get eigen_vector for given M = A^t*A*v - \lambda*v
void svd::GaussJordanElimination(std::vector<std::vector<double>> M, std::vector<double>& eigen_vec) {
for (std::size_t s = 0; s < M.size() - 1; s++)
{
const double diag_elem = M[s][s];
if (diag_elem != 0 && diag_elem != 1)
{
for (std::size_t col = s; col < M[s].size(); col++)
M[s][col] /= diag_elem;
}
// Move the column with element M[s][s] to the end of matrix
if (diag_elem == 0)
{
for (std::size_t col = s; col < M[s].size(); col++)
std::swap(M[s][col], M[s + 1][col]);
}
// GaussJordan elimination process
for (std::size_t row = 0; row < M.size(); row++)
{
const double element = M[row][s];
if (row != s)
{
// Eliminate element M[row][s]. Subtract the line 's' from line 'row != s'
for (std::size_t col = s; col < M[row].size(); col++)
M[row][col] = M[row][col] - M[s][col] * element;
}
}
}
std::size_t row = 0;
while (row < M.size())
eigen_vec.push_back(-M[row++][M.size() - 1]);
eigen_vec[eigen_vec.size() - 1] = 1;
}
void svd::VerifyFactorisition(std::vector<std::vector<double>>& u, std::vector<std::vector<double>>& s,
std::vector<std::vector<double>>& v) {
std::vector<std::vector<double>> prod_US, prod_USVt, v_transp;
prod_matrix(u, s, prod_US);
transpose_matr(v, v_transp);
prod_matrix(prod_US, v_transp, prod_USVt);
if (track)
print_m(prod_USVt, "VerifyFactorisition: Product usv^t should be equal to initial matrix");
}
void svd::VerifyPseudoinverse(std::vector<std::vector<double>>& A, std::vector<std::vector<double>>& A_pesudoinv) {
std::vector<std::vector<double>> prod;
// Product should be Identity matrix or with several zeros on the diagaonal
prod_matrix(A, A_pesudoinv, prod);
if (track)
print_m(prod, "VerifyPseudoinverse: Product A*A_pinv should be initial matrix");
}
int svd::GetRank(std::vector<std::vector<double>>& s) {
int rank = 0;
for (std::size_t row = 0; row < s.size(); row++)
if (s[row][row] != 0) {
rank++;
}
return rank;
}
void svd::GetPseudoinverse(std::vector<std::vector<double>>& s,
std::vector<std::vector<double>>& u, std::vector<std::vector<double>>& v,
std::vector<std::vector<double>>& pseudoinv){
std::vector<std::vector<double>> s_inv, prod_VS_inv, u_transp;
InverseDiagMatrix(s, s_inv);
prod_matrix(v, s_inv, prod_VS_inv);
transpose_matr(u, u_transp);
prod_matrix(prod_VS_inv, u_transp, pseudoinv);
int rank = GetRank(s);
if(rank < (int)s.size())
print_m(pseudoinv, "Pseudoinverse Matrix");
else
print_m(pseudoinv, "Inverse Matrix");
}
void svd::print_m(std::vector<std::vector<double>> M, std::string operation){
std::cout << "print_m: " << operation.c_str() << ": " << std::endl;
for (std::size_t row = 0; row < M.size(); row++)
{
for (std::size_t col = 0; col < M[row].size(); col++)
std::cout << std::fixed << M[row][col] << " ";
std::cout << "\n";
}
std::cout << "\n";
}
void svd::print_eigenv(std::vector<double> eigenv, std::string operation) {
std::cout << "print_eigenv: " << operation.c_str() << ": " << std::endl;
for (std::size_t row = 0; row < eigenv.size(); row++)
{
std::cout << std::fixed << eigenv[row] << " ";
}
std::cout << "\n";
}