-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuse_vectors.cpp
More file actions
212 lines (175 loc) · 4.38 KB
/
use_vectors.cpp
File metadata and controls
212 lines (175 loc) · 4.38 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
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
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <cassert>
#include "Exception.hpp"
#include "Matrix.hpp"
#include "Vector.hpp"
void testfunc(){
Vector b(3);
b(1) = 2;
b(2) = 5;
b(3) = 12;
Matrix m(3,3);
m(1,1) = 1;
m(1,2) = 2;
m(1,3) = -1;
m(2,1) = 1;
m(2,2) = -1;
m(2,3) = 2;
m(3,1) = 2;
m(3,2) = 2;
m(3,3) = 2;
Matrix m2(3,3);
m2(1,1) = 1;
m2(1,2) = 2;
m2(1,3) = -1;
m2(2,1) = 1;
m2(2,2) = -1;
m2(2,3) = 2;
m2(3,1) = 2;
m2(3,2) = 2;
m2(3,3) = 2;
Matrix c(3,3);
c(1,1) = 2;
c(1,2) = 4;
c(1,3) = -2;
c(2,1) = 2;
c(2,2) = -2;
c(2,3) = 4;
c(3,1) = 4;
c(3,2) = 4;
c(3,3) = 4;
Matrix c2(3,3);
c2(1,1) = 2;
c2(1,2) = -4;
c2(1,3) = 2;
c2(2,1) = 8;
c2(2,2) = 14;
c2(2,3) = 2;
c2(3,1) = 16;
c2(3,2) = 12;
c2(3,3) = 12;
Matrix d(3,3);
if (!compare(m,m2)){
throw Exception("Test not passed","Comparison operator failed.");
}
Matrix m3(m);
if (!compare(m3,m)){
throw Exception("Test not passed","Copy constructor failed.");
}
m3 = c;
if (!compare(m3,c)){
throw Exception("Test not passed","Assignment operator failed.");
}
m3 = m + m2;
if (!compare(m3,c)){
throw Exception("Test not passed","Addition operator failed.");
}
m3 = m - m2;
if (!compare(m3,d)){
throw Exception("Test not passed","Subtraction operator failed.");
}
m3 = m*c;
if (!compare(m3,c2)){
throw Exception("Test not passed","Matrix-matrix multiplication operator failed.");
}
Vector x(3);
x(1) = 1;
x(2) = 2;
x(3) = 3;
Vector v = m*x;
if (!compare(v,b)){
throw Exception("Test not passed","Matrix-vector multiplication operator failed.");
}
m3 = m*2;
if (!compare(m3,c)){
throw Exception("Test not passed","Matrix-vector multiplication operator failed.");
}
Vector s(2);
s(1) = 3;
s(2) = 3;
if (!compare(s,size(m))){
throw Exception("Test not passed","Matrix-vector multiplication operator failed.");
}
if (!(-6==determinant(m,3))){
throw Exception("Test not passed","Determinant function failed.");
}
int l = 10;
Matrix A(l,l);
for (int i=1;i<=l;i++){
for (int j=1;j<=l;j++){
A(i,j) = std::rand()%10;
//A(i,j)=i+j;
}
}
double tol = 0.000001;
Vector X(l);
for (int i=1;i<=l;i++){
X(i)=4*i;
}
Vector B=A*X;
//backslash
std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
v=B/A;
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
//std::cout << "Time difference backslash = " << std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count() << "[µs]" << std::endl;
for (int i=1;i<=l;i++){
if (abs(X(i)-v(i))>tol){
throw Exception("Test not passed","Backslash operator failed.");
}
}
//Gaussian Elimination
begin = std::chrono::steady_clock::now();
v = gausElim(A,B);
end = std::chrono::steady_clock::now();
//std::cout << "Time difference GMRES restarted = " << std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count() << "[µs]" << std::endl;
for (int i=1;i<=l;i++){
if (abs(X(i)-v(i))>tol){
throw Exception("Test not passed","Gaussian elimination failed.");
}
}
//restarted GMRES
begin = std::chrono::steady_clock::now();
v = GMRES(A,B,5,tol,100);
end = std::chrono::steady_clock::now();
//std::cout << "Time difference GMRES restarted = " << std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count() << "[µs]" << std::endl;
for (int i=1;i<=l;i++){
if (abs(X(i)-v(i))>tol){
throw Exception("Test not passed","GMRES restarted failed.");
}
}
//GMRES
begin = std::chrono::steady_clock::now();
v = GMRES(A,B);
end = std::chrono::steady_clock::now();
//std::cout << "Time difference GMRES = " << std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count() << "[µs]" << std::endl;
for (int i=1;i<=l;i++){
if (abs(X(i)-v(i))>tol){
throw Exception("Test not passed","GMRES linsolve failed.");
}
}
//CG
A = A.transpose() * A; //A^TA is always symmetric positive definite
B=A*X;
begin = std::chrono::steady_clock::now();
v = CG(A,B);
end = std::chrono::steady_clock::now();
//std::cout << "Time difference CG = " << std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count() << "[µs]" << std::endl;
for (int i=1;i<=l;i++){
if (abs(X(i)-v(i))>tol){
throw Exception("Test not passed","CG failed.");
}
}
std::cout << "All tests passed";
}
int main(int argc, char* argv[])
{
try{
testfunc();
}
catch (Exception &ex)
{
ex.DebugPrint();
}
}