-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
83 lines (76 loc) · 1.67 KB
/
main.cpp
File metadata and controls
83 lines (76 loc) · 1.67 KB
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
#include <iostream>
#include "matrix.h"
using namespace std;
int main() {
vector<vector<double>> vec= {
{ 2, -1, 1 },
{ 1, 3, -2 },
{ 0, 1, -2 },
};
Matrix m1(vec);
cout << "original matrix" << endl;
m1.printMatrix();
cout << endl;
double det = m1.determinant();
cout << "determinant: " << det << endl << endl;
if (det != 0) {
Matrix inv_m1 = m1.inverse();
cout << "inverse:" << endl;
inv_m1.printMatrix();
cout << endl;
Matrix identity = (inv_m1*m1);
identity.trimMatrix();
cout << "identity:" << endl;
identity.printMatrix();
cout << endl;
}
pair<Matrix, Matrix> p = m1.qr_decomposition();
cout << "original matrix" << endl;
m1.printMatrix();
cout << endl;
cout << "Q" << endl;
p.first.printMatrix();
cout << endl;
cout << "R" << endl;
p.second.printMatrix();
cout << endl;
Matrix s = p.first*p.second;
s.trimMatrix();
cout << "Q*R" << endl;
s.printMatrix();
if (s == m1) {
cout << "QR passed the test." << endl;
}
else {
cout << endl << "QR error: " << max((s - m1).apply(abs)) << endl;
}
cout << endl;
array<Matrix, 3> arr = m1.singular_value_decomposition();
Matrix & U = arr[0], & E = arr[1], & VT=arr[2];
cout << "U" << endl;
U.printMatrix();
cout << endl;
cout << "E" << endl;
E.printMatrix();
cout << endl;
cout << "VT" << endl;
VT.printMatrix();
cout << endl;
cout << "MATRIX" << endl;
m1.printMatrix();
cout << endl;
U.trimMatrix();
E.trimMatrix();
VT.trimMatrix();
Matrix res = U * E * VT;
res.trimMatrix();
cout << "U*E*VT" << endl;
res.printMatrix();
if (res == m1) {
cout << "SVD passed the test." << endl;
}
else {
cout << endl << "SVD error: " << max((res - m1).apply(abs)) << endl;
}
return 0;
}