-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
134 lines (92 loc) · 3.46 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
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
//
// main.cpp
// gauss-jordan
//
// Created by mndx on 17/04/2022.
//
#include <chrono>
#include "Eigen/LU"
#include "lib_gauss.hpp"
#include "lib_mat.hpp"
#include "lib_mem.hpp"
#include "lib_testing.hpp"
#include "lib_testing_ref.hpp"
#include "user_types.hpp"
using namespace std;
using namespace std::chrono;
int main(int argc, char * argv[]) {
// Size input matrix
int n = 5;
// Allocate space for matrices
double ** mat = mat2D(n);
double ** mat_inv = mat2D(n);
double ** mat_prod = mat2D(n);
double ** mat_store = mat2D(n);
matrix mat1(n, n);
i_real_matrix mat2;
MatrixXd mat3(n,n);
// Populate matrix mat with some data
init_mat(n, mat);
// Populate reference matrix mat1 with mat data
set_mat_to_matrix(mat, n, mat1);
// Populate reference matrix mat2 with mat data
set_mat_to_vec2D(mat, n, mat2);
// Populate reference matrix mat3 with mat data
set_mat_to_matxd(mat, n, mat3);
// Store initial matrix mat
set_mat(mat, n, mat_store);
// Time custom Gauss-Jordan method
auto start = high_resolution_clock::now();
// Compute inverse using custom Gauss-Jordan method
gauss_jordan(mat, n, mat_inv);
// Get stop time custom Gauss-Jordan method
auto stop = high_resolution_clock::now();
// Get duration custom Gauss-Jordan method
auto duration = duration_cast<seconds>(stop - start);
// Print duration custom Gauss-Jordan method
cout << "duration custom Guass-Jordan: " << duration.count() << " (s)" << endl;
// Time reference method 1, Rosetta Code
start = high_resolution_clock::now();
// Compute inverse using reference method 1, Rosetta Code
auto mat1_inv = inverse(mat1);
// Get stop time reference method 1, Rosetta Code
stop = high_resolution_clock::now();
// Get duration reference method 1, Rosetta Code
duration = duration_cast<seconds>(stop - start);
// Print duration reference method 1, Rosetta Code
cout << "duration reference method 1: " << duration.count() << " (s)" << endl;
// Time reference method 2, MIT
start = high_resolution_clock::now();
// Compute inverse using reference method 2, MIT
i_real_matrix mat2_inv = inv_ref(mat2, true);
// Get stop time reference method 2, MIT
stop = high_resolution_clock::now();
// Get duration reference method 2, MIT
duration = duration_cast<seconds>(stop - start);
// Print duration of reference method 2, MIT
cout << "duration reference method 2: " << duration.count() << " (s)" << endl;
// Time reference method 3, Eigen
start = high_resolution_clock::now();
// Compute inverse using reference method 3, Eigen
MatrixXd mat3_inv = mat3.inverse();
// Get stop time reference method 3, Eigen
stop = high_resolution_clock::now();
// Get duration reference method 3, Eigen
duration = duration_cast<seconds>(stop - start);
// Print duration of reference method 3, Eigen
cout << "duration reference method 3: " << duration.count() << " (s)" << endl;
// Verify computation custom Gauss-Jordan method
mat_mult_sq(mat_store, mat_inv, n, mat_prod);
// Print results
print_mat(mat_prod, n);
print_mat(mat_inv, n);
print(cout, mat1_inv);
showMatrix(mat2_inv, "reference solution MIT", false);
print_matxd(mat3_inv, n);
// Free allocated space
free_mat2D(mat, n);
free_mat2D(mat_inv, n);
free_mat2D(mat_prod, n);
free_mat2D(mat_store, n);
return 0;
}