-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSquareMatrixTest.cpp
512 lines (465 loc) · 14 KB
/
SquareMatrixTest.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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
/*
* SquareMatrixTest.cpp
*
* Created on: University Collage London.
* Date: 12.2018-01.2019
* Author: student number:18043309
*/
#include "Matrix.h"
#include "SquareMatrix.h"
#include <cstdlib>
#include <typeinfo>
#include <stdio.h>
using namespace std;
void onesAndeyesTesting () {
cout << "Testing the function ones and eyes" << endl;
cout << "Case 1:Creating a 4x4 matrix of ones with the ones:" << endl;
{
SquareMatrix matrix(4,4);
cout<<"matrix1 = "<<endl;
cout << matrix.Ones(4,4)<< endl;
cout << "Press any key to continue ..." << flush;
system("read");
cout << endl;
}
cout << "Case 2:Creating a 4x4 unit matrix with the eye:" << endl;
{
SquareMatrix matrix(4,4);
cout<<"matrix2 = "<<endl;
cout << matrix.eye(4)<< endl;
cout << "Press any key to continue ..." << flush;
system("read");
cout << endl;
}
}
void ToeplitzTesting () {
cout << "Testing the function toeplitz:" << endl;
cout << "Case 1: When the input is only one vector" << endl;
{
double vec1[4] = {4, 3, 2, 1};
// matrix should be stored as following
//4 3 2 1
//3 4 3 2
//2 3 4 3
//1 2 3 4
cout<<"vec1 = "<<endl;
cout<<" ";
for(int i=0;i<4;i++){
cout<<vec1[i]<<" ";
}
cout<<endl;
cout<<endl;
SquareMatrix toeplitz(4,4);
cout<<"matrix3 = "<<endl;
cout<<toeplitz.SquareMatrix::toeplitz(vec1)<<endl;
cout << "Press any key to continue ..." << flush;
system("read");
cout << endl;
}
cout << "Case 2: When the inputs are two vectors" << endl;
{
double vec1[4] = {4, 3, 2, 1};
double vec2[4] = {1, 2, 3, 4};
// matrix should be stored as following
//1 3 2 1
//2 1 3 2
//3 2 1 3
//4 3 2 1
cout<<"vec1 = "<<endl;
cout<<" ";
for(int i=0;i<4;i++){
cout<<vec1[i]<<" ";
}
cout<<endl;
cout<<"vec2 = "<<endl;
cout<<" ";
for(int i=0;i<4;i++){
cout<<vec2[i]<<" ";
}
cout<<endl;
SquareMatrix toeplitz1(4,4);
cout<<"matrix4 = "<<endl;
cout<<toeplitz1.SquareMatrix::toeplitz(vec2,vec1)<<endl;
cout << "Press any key to continue ..." << flush;
system("read");
cout << endl;
}
}
void TransposeTesting () {
cout << "Testing the Transpose functions:" << endl;
cout<<endl;
{
// the same matrix as in ToeplitzTesting
double vec1[4] = {4, 3, 2, 1};
double vec2[4] = {1, 2, 3, 4};
SquareMatrix matrix = SquareMatrix::toeplitz(vec2,vec1);
cout<<"the input matrix ="<<endl;
cout<<matrix<<endl;
Matrix transpose = Matrix::Transpose(matrix);
cout << "Matrix5= " << endl;
cout << transpose << endl;
cout << "Press any key to continue ..." << flush;
system("read");
cout << endl;
}
}
void triuTesting (){
cout << "Testing the triu functions:" << endl;
{
// the same matrix as in ToeplitzTesting
double vec1[4] = {4, 3, 2, 1};
double vec2[4] = {1, 2, 3, 4};
SquareMatrix matrix=SquareMatrix::toeplitz(vec2,vec1);
cout<<"the input matrix ="<<endl;
cout<<endl;
cout<<matrix<<endl;
cout << "Matrix6= " << endl;
cout <<matrix.SquareMatrix::triu(matrix,4)<< endl;
cout << "Press any key to continue ..." << flush;
system("read");
cout << endl;
}
}
void trilTesting (){
cout << "Testing the tril functions:" << endl;
{
// the same matrix as in ToeplitzTesting
double vec1[4] = {4, 3, 2, 1};
double vec2[4] = {1, 2, 3, 4};
SquareMatrix matrix=SquareMatrix::toeplitz(vec2,vec1);;
cout<<"the input matrix ="<<endl;
cout<<endl;
cout<<matrix<<endl;
cout << "Matrix7= " << endl;
cout <<matrix.SquareMatrix::tril(matrix,4)<< endl;
cout << "Press any key to continue ..." << flush;
system("read");
cout << endl;
}
}
void lu1Testing (){
cout << "Testing the function lu" << endl;
cout << "Algorithm 2.1"<<endl;
cout << "Case1:it is matched with matlab syntax [lmatrix1, umatrix1] = lu(matrix3) "<<endl;
cout<<endl;
{
double vec1[4] = {4, 3, 2, 1};
// matrix should be stored as following
//4 3 2 1
//3 4 3 2
//2 3 4 3
//1 2 3 4
SquareMatrix matrix=SquareMatrix::toeplitz(vec1);
cout<<"the input matrix ="<<endl;
cout<<endl;
cout<<matrix<<endl;
matrix.lu1(matrix);
cout << "Press any key to continue ..." << flush;
system("read");
cout << endl;
}
cout << "Case2:when any of the pivots is equal to zero "<<endl;
cout<<endl;
{
double vec1[4] = {0, 3, 2, 1};
// matrix should be stored as following
//4 3 2 1
//3 4 3 2
//2 3 4 3
//1 2 3 4
SquareMatrix matrix=SquareMatrix::toeplitz(vec1);
cout<<"the input matrix ="<<endl;
cout<<endl;
cout<<matrix<<endl;
matrix.lu1(matrix);
cout << "Press any key to continue ..." << flush;
system("read");
cout << endl;
}
cout << "Case3:when the input matrix is singular "<<endl;
cout<<endl;
{
static double input[9]={4,-5,7,-2,6,0,1,0,3};
SquareMatrix matrix(3,3);
matrix.SquareMatrix::TestingMatrixInput(input,3,3);
cout<<"the input matrix ="<<endl;
cout<<endl;
cout<<matrix<<endl;
matrix.lu1(matrix);
cout << "Press any key to continue ..." << flush;
system("read");
cout << endl;
}
}
void lu2Testing() {
cout << "Testing the function lu" << endl;
cout << "Algorithm 2.2"<<endl;
cout << "Case 1: When the input is matrix3" << endl;
cout << "It is matched with matlab syntax [lmatrix1, umatrix1, pmatrix1] = lu(matrix3) "<<endl;
cout<<endl;
{
int dim = 4;
double vec1[4] = {4, 3, 2, 1};
// matrix should be stored as following
//4 3 2 1
//3 4 3 2
//2 3 4 3
//1 2 3 4
SquareMatrix matrix1=SquareMatrix::toeplitz(vec1);
cout<<"the input matrix ="<<endl;
cout<<endl;
cout<<matrix1<<endl;
SquareMatrix lmatrix(dim,dim);
SquareMatrix umatrix(dim,dim);
SquareMatrix pmatrix(dim,dim);
matrix1.lu2(matrix1,umatrix,lmatrix,pmatrix);
cout<<"lmatrix1 = "<<endl;
cout<<lmatrix<<endl;
cout<<"umatrix1 = "<<endl;
cout<<umatrix<<endl;
cout<<"pmatrix1 = "<<endl;
cout<<pmatrix<<endl;
cout << "Press any key to continue ..." << flush;
system("read");
cout << endl;
}
cout << "Case 2: When the input is matrix4" << endl;
cout << "It is matched with matlab syntax [lmatrix2, umatrix2, pmatrix2] = lu(matrix4) "<<endl;
cout<<endl;
{
int dim = 4;
double vec1[4] = {4 ,3, 2, 1};
double vec2[4] = {1, 2, 3, 4};
// matrix should be stored as following
//1 3 2 1
//2 1 3 2
//3 2 1 3
//4 3 2 1
SquareMatrix matrix2=SquareMatrix::toeplitz(vec2,vec1);
cout<<"the input matrix ="<<endl;
cout<<endl;
cout<<matrix2<<endl;
SquareMatrix lmatrix(dim,dim);
SquareMatrix umatrix(dim,dim);
SquareMatrix pmatrix(dim,dim);
matrix2.lu2(matrix2,umatrix,lmatrix,pmatrix);
cout<<"lmatrix2 = "<<endl;
cout<<lmatrix<<endl;
cout<<"umatrix2 = "<<endl;
cout<<umatrix<<endl;
cout<<"pmatrix2 = "<<endl;
cout<<pmatrix<<endl;
cout << "Press any key to continue ..." << flush;
system("read");
cout << endl;
}
}
void applicationTesting1(){
cout << "Testing the application of function lu" << endl;
cout << "when Number of equations = Number of unknowns" << endl;
cout << "Case 1: When the input is matrix3" << endl;
{
int dim=4;
double b[4]={3,3,2,1};
double vec1[4] = {4, 3, 2, 1};
SquareMatrix matrix=SquareMatrix::toeplitz(vec1);
cout<<"the coefficient matrix ="<<endl;
cout<<endl;
cout<<matrix<<endl;
cout<<"the right hand side array b of equations"<<endl;
cout<<endl;
for(int i=0;i<dim;i++){
cout<<" "<<b[i]<<endl;
}
cout<<endl;
cout<<"the expected solution caculated by online matrix caculator is"<<endl;
cout<<"x1 = 0.4000"<<endl;
cout<<"x2 = 0.5000"<<endl;
cout<<"x3 = 0.0000"<<endl;
cout<<"x4 = -0.1000"<<endl;
cout<<endl;
//initialize lmatrix,umatrix,pmatrix in order to get the return values from lu function
SquareMatrix lmatrix(dim,dim);
SquareMatrix umatrix(dim,dim);
SquareMatrix pmatrix(dim,dim);
matrix.SquareMatrix::lu2(matrix,lmatrix,umatrix,pmatrix);
SquareMatrix Pb = SquareMatrix::b_permutation(pmatrix,b,dim);
//call forwardSubstitution function, return SquareMatrix y
SquareMatrix y=matrix.forwardSubstitution(lmatrix,Pb,dim);
//call backSubstitution function to get the solution of linear equations
matrix.backSubstitution(umatrix,y,dim);
cout << "Press any key to continue ..." << flush;
system("read");
cout << endl;
}
cout << "Case 2: When the input is matrix4" << endl;
{
int dim=4;
double b[4]={3,3,2,1};
double vec1[4] = {4, 3, 2, 1};
double vec2[4] = {1, 2, 3, 4};
SquareMatrix matrix=SquareMatrix::toeplitz(vec2,vec1);
cout<<"the coefficient matrix ="<<endl;
cout<<endl;
cout<<matrix<<endl;
cout<<"the right hand side array b of equations"<<endl;
cout<<endl;
for(int i=0;i<dim;i++){
cout<<" "<<b[i]<<endl;
}
cout<<endl;
cout<<"the expected solution caculated by online matrix caculator ="<<endl;
cout<<endl;
cout<<"x1 = -0.6667"<<endl;
cout<<"x2 = 0.4444"<<endl;
cout<<"x3 = 0.7778"<<endl;
cout<<"x4 = -0.7778"<<endl;
cout<<endl;
//initialize lmatrix,umatrix,pmatrix in order to get the return values from lu function
SquareMatrix lmatrix(dim,dim);
SquareMatrix umatrix(dim,dim);
SquareMatrix pmatrix(dim,dim);
matrix.SquareMatrix::lu2(matrix,lmatrix,umatrix,pmatrix);
SquareMatrix Pb = SquareMatrix::b_permutation(pmatrix,b,dim);
//call forwardSubstitution function, return SquareMatrix y
SquareMatrix y=matrix.forwardSubstitution(lmatrix,Pb,dim);
//call backSubstitution function to get the solution of linear equations
matrix.backSubstitution(umatrix,y,dim);
cout << "Press any key to continue ..." << flush;
system("read");
cout << endl;
}
}
void applicationTesting2(){
cout << "Testing the application of function lu" << endl;
cout << "when Number of equations > Number of unknowns" << endl;
cout<<endl;
cout << "Case1:when no swapping is required" << endl;
cout<<endl;
{
int m=4;
int n=3;
double b[4]={3,3,2,1};
double input[12] = {2, 1, 3, -1, 4, 2, 1, 5, -1, 2, 2, 1};
Matrix A(4,3);
A.Matrix::TestingInput(input,4,3);
double* input_ATA=A.Matrix::lss_ATA(A);
SquareMatrix AT_A(3,3);
AT_A.SquareMatrix::TestingMatrixInput(input_ATA,3,3);
cout<<"the coefficient matrix ="<<endl;
cout<<endl;
cout<< A <<endl;
cout<<"the right hand side array b of equations"<<endl;
cout<<endl;
for(int i=0;i<m;i++){
cout<<" "<<b[i]<<endl;
}
cout<<endl;
cout<<"the expected solution caculated by online matrix caculator ="<<endl;
cout<<endl;
cout<<"x1 = 0.6559"<<endl;
cout<<"x2 = 0.4002"<<endl;
cout<<"x3 = 0.1919"<<endl;
cout<<endl;
double* AT_B=A.Matrix::lss_ATB(A,b);
SquareMatrix lmatrix(n,n);
SquareMatrix umatrix(n,n);
SquareMatrix pmatrix(n,n);
AT_A.SquareMatrix::lu2(AT_A,lmatrix,umatrix,pmatrix);
SquareMatrix Pb = SquareMatrix::b_permutation(pmatrix,AT_B,n);
//call forwardSubstitution function, return SquareMatrix y
SquareMatrix y = AT_A.forwardSubstitution(lmatrix,Pb,n);
//call backSubstitution function to get the solution of linear equations
AT_A.backSubstitution(umatrix,y,n);
cout << "Press any key to continue ..." << flush;
system("read");
cout << endl;
}
cout << "Case2:when swapping is required" << endl;
cout<<endl;
{
int m=4;
int n=3;
double b[4]={3,3,2,1};
double input[12] = {2, 1, 3, -1, 4, 2, 1, 5, -1, 2, 2, 9};
Matrix A(4,3);
A.Matrix::TestingInput(input,4,3);
double* input_ATA=A.Matrix::lss_ATA(A);
SquareMatrix AT_A(3,3);
AT_A.SquareMatrix::TestingMatrixInput(input_ATA,3,3);
cout<<"the coefficient matrix ="<<endl;
cout<<endl;
cout<< A <<endl;
cout<<"the right hand side array b of equations"<<endl;
cout<<endl;
for(int i=0;i<m;i++){
cout<<" "<<b[i]<<endl;
}
cout<<endl;
cout<<"the expected solution caculated by online matrix caculator ="<<endl;
cout<<endl;
cout<<"x1 = 0.6688"<<endl;
cout<<"x2 = 0.4776"<<endl;
cout<<"x3 = -0.0493"<<endl;
cout<<endl;
double* AT_B=A.Matrix::lss_ATB(A,b);
SquareMatrix lmatrix(n,n);
SquareMatrix umatrix(n,n);
SquareMatrix pmatrix(n,n);
AT_A.SquareMatrix::lu2(AT_A,lmatrix,umatrix,pmatrix);
SquareMatrix Pb = SquareMatrix::b_permutation(pmatrix,AT_B,n);
//call forwardSubstitution function, return SquareMatrix y
SquareMatrix y = AT_A.forwardSubstitution(lmatrix,Pb,n);
//call backSubstitution function to get the solution of linear equations
AT_A.backSubstitution(umatrix,y,n);
cout << "Press any key to continue ..." << flush;
system("read");
cout << endl;
}
}
int main () {
for (;;) {
cout << "Choose to test one of the following:" << endl;
cout << " Enter \'1\' for the ones and eyes function" << endl;
cout << " Enter \'2\' for the Toeplitz function" << endl;
cout << " Enter \'3\' for the Transpose function" << endl;
cout << " Enter \'4\' for the triu funtion" << endl;
cout << " Enter \'5\' for the tril funtion" << endl;
cout << " Enter \'6\' for the lu functions with the algorithm 2.1" << endl;
cout << " Enter \'7\' for the lu functions with the algorithm 2.2" << endl;
cout << " Enter \'8\' for the apppication of lu functions when Number of equations = Number of unknowns" << endl;
cout << " Enter \'9\' for the apppication of lu functions when Number of equations > Number of unknowns" << endl;
cout << ">> ";
cin.clear();
cin.sync();
char choice;
cin >> choice;
switch (choice) {
case '1': onesAndeyesTesting();
break;
case '2': ToeplitzTesting();
break;
case '3': TransposeTesting();
break;
case '4': triuTesting();
break;
case '5': trilTesting();
break;
case '6': lu1Testing();
break;
case '7': lu2Testing();
break;
case '8': applicationTesting1();
break;
case '9': applicationTesting2();
break;
}
fflush(stdin);
cout << "Enter \'0\' to exit or \'1\' to choose another test" << endl;
cout << ">> ";
cin >> choice;
if (choice == '0') {
return 0;
}
}
}