This repository has been archived by the owner on Jun 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
70 lines (61 loc) · 6.39 KB
/
main.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
#include "includes/NLAMethods.h"
auto A = Matrix(
std::vector<std::vector<lld>>{
{1.0000000000, 4.9176000000, 1.0000000000, 3.4720000000, 0.9980000000, 1.0000000000, 7.0000000000, 4.0000000000, 42.0000000000, 3.0000000000, 1.0000000000, 0.0000000000},
{1.0000000000, 5.0208000000, 1.0000000000, 3.5310000000, 1.5000000000, 2.0000000000, 7.0000000000, 4.0000000000, 62.0000000000, 1.0000000000, 1.0000000000, 0.0000000000},
{1.0000000000, 4.5429000000, 1.0000000000, 2.2750000000, 1.1750000000, 1.0000000000, 6.0000000000, 3.0000000000, 40.0000000000, 2.0000000000, 1.0000000000, 0.0000000000},
{1.0000000000, 4.5573000000, 1.0000000000, 4.0500000000, 1.2320000000, 1.0000000000, 6.0000000000, 3.0000000000, 54.0000000000, 4.0000000000, 1.0000000000, 0.0000000000},
{1.0000000000, 5.0597000000, 1.0000000000, 4.4550000000, 1.1210000000, 1.0000000000, 6.0000000000, 3.0000000000, 42.0000000000, 3.0000000000, 1.0000000000, 0.0000000000},
{1.0000000000, 3.8910000000, 1.0000000000, 4.4550000000, 0.9880000000, 1.0000000000, 6.0000000000, 3.0000000000, 56.0000000000, 2.0000000000, 1.0000000000, 0.0000000000},
{1.0000000000, 5.8980000000, 1.0000000000, 5.8500000000, 1.2400000000, 1.0000000000, 7.0000000000, 3.0000000000, 51.0000000000, 2.0000000000, 1.0000000000, 1.0000000000},
{1.0000000000, 5.6039000000, 1.0000000000, 9.5200000000, 1.5010000000, 0.0000000000, 6.0000000000, 3.0000000000, 32.0000000000, 1.0000000000, 1.0000000000, 0.0000000000},
{1.0000000000, 15.4202000000, 2.5000000000, 9.8000000000, 3.4200000000, 2.0000000000, 10.0000000000, 5.0000000000, 42.0000000000, 2.0000000000, 1.0000000000, 1.0000000000},
{1.0000000000, 14.4598000000, 2.5000000000, 12.8000000000, 3.0000000000, 2.0000000000, 9.0000000000, 5.0000000000, 14.0000000000, 4.0000000000, 1.0000000000, 1.0000000000},
{1.0000000000, 5.8282000000, 1.0000000000, 6.4350000000, 1.2250000000, 2.0000000000, 6.0000000000, 3.0000000000, 32.0000000000, 1.0000000000, 1.0000000000, 0.0000000000},
{1.0000000000, 5.3003000000, 1.0000000000, 4.9883000000, 1.5520000000, 1.0000000000, 6.0000000000, 3.0000000000, 30.0000000000, 1.0000000000, 2.0000000000, 0.0000000000},
{1.0000000000, 6.2712000000, 1.0000000000, 5.5200000000, 0.9750000000, 1.0000000000, 5.0000000000, 2.0000000000, 30.0000000000, 1.0000000000, 2.0000000000, 0.0000000000},
{1.0000000000, 5.9592000000, 1.0000000000, 6.6660000000, 1.1210000000, 2.0000000000, 6.0000000000, 3.0000000000, 32.0000000000, 2.0000000000, 1.0000000000, 0.0000000000},
{1.0000000000, 5.0500000000, 1.0000000000, 5.0000000000, 1.0200000000, 0.0000000000, 5.0000000000, 2.0000000000, 46.0000000000, 4.0000000000, 1.0000000000, 1.0000000000},
{1.0000000000, 5.6039000000, 1.0000000000, 9.5200000000, 1.5010000000, 0.0000000000, 6.0000000000, 3.0000000000, 32.0000000000, 1.0000000000, 1.0000000000, 0.0000000000},
{1.0000000000, 8.2464000000, 1.5000000000, 5.1500000000, 1.6640000000, 2.0000000000, 8.0000000000, 4.0000000000, 50.0000000000, 4.0000000000, 1.0000000000, 0.0000000000},
{1.0000000000, 6.6969000000, 1.5000000000, 6.0920000000, 1.4880000000, 1.5000000000, 7.0000000000, 3.0000000000, 22.0000000000, 1.0000000000, 1.0000000000, 1.0000000000},
{1.0000000000, 7.7841000000, 1.5000000000, 7.1020000000, 1.3760000000, 1.0000000000, 6.0000000000, 3.0000000000, 17.0000000000, 2.0000000000, 1.0000000000, 0.0000000000},
{1.0000000000, 9.0384000000, 1.0000000000, 7.8000000000, 1.5000000000, 1.5000000000, 7.0000000000, 3.0000000000, 23.0000000000, 3.0000000000, 3.0000000000, 0.0000000000},
{1.0000000000, 5.9894000000, 1.0000000000, 5.5200000000, 1.2560000000, 2.0000000000, 6.0000000000, 3.0000000000, 40.0000000000, 4.0000000000, 1.0000000000, 1.0000000000},
{1.0000000000, 7.5422000000, 1.5000000000, 4.0000000000, 1.6900000000, 1.0000000000, 6.0000000000, 3.0000000000, 22.0000000000, 1.0000000000, 1.0000000000, 0.0000000000},
{1.0000000000, 8.7951000000, 1.5000000000, 9.8900000000, 1.8200000000, 2.0000000000, 8.0000000000, 4.0000000000, 50.0000000000, 1.0000000000, 1.0000000000, 1.0000000000},
{1.0000000000, 6.0931000000, 1.5000000000, 6.7265000000, 1.6520000000, 1.0000000000, 6.0000000000, 3.0000000000, 44.0000000000, 4.0000000000, 1.0000000000, 0.0000000000},
{1.0000000000, 8.3607000000, 1.5000000000, 9.1500000000, 1.7770000000, 2.0000000000, 8.0000000000, 4.0000000000, 48.0000000000, 1.0000000000, 1.0000000000, 1.0000000000},
{1.0000000000, 8.1400000000, 1.0000000000, 8.0000000000, 1.5040000000, 2.0000000000, 7.0000000000, 3.0000000000, 3.0000000000, 1.0000000000, 3.0000000000, 0.0000000000},
{1.0000000000, 9.1416000000, 1.5000000000, 7.3262000000, 1.8310000000, 1.5000000000, 8.0000000000, 4.0000000000, 31.0000000000, 4.0000000000, 1.0000000000, 0.0000000000},
{1.0000000000, 12.0000000000, 1.5000000000, 5.0000000000, 1.2000000000, 2.0000000000, 6.0000000000, 3.0000000000, 30.0000000000, 3.0000000000, 1.0000000000, 1.0000000000}
}
);
// homework 8 (SVD):
int main() {
auto m = A.rows;
auto n = A.cols;
auto [r, count, timing] = SVDMethod(A);
std::cout << "iterations: " << count << std::endl;
std::cout << "time spent: " << timing.count() << "μs" << std::endl;
auto eigenvalues = Vector(r.B.cols);
// take diagonal elements of B as eigenvalues:
for (ull i = 0; i < r.B.cols; i++) {
eigenvalues.array[i] = r.B.matrix[i][i];
}
// sort eigenvalues in descending order:
std::sort(eigenvalues.array.begin(), eigenvalues.array.end(), std::less<>());
// print eigenvalues:
std::cout << "eigenvalues = ";
eigenvalues.print();
std::cout << "ep = " << MatrixNorm_Infinity(r.U.transpose() * r.U - Matrix::identity(m)) << std::endl
<< "eq = " << MatrixNorm_Infinity(r.V.transpose() * r.V - Matrix::identity(n)) << std::endl
<< "et = " << MatrixNorm_Infinity(r.U * A * r.V - r.B) << std::endl;
std::cout << "T=" << std::endl;
r.B.print();
std::cout << "P=" << std::endl;
r.U.print();
std::cout << "Q=" << std::endl;
r.V.print();
return 0;
}