-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSquareMatrix.cpp
379 lines (315 loc) · 10.1 KB
/
SquareMatrix.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
/*
* SquareMatrix.cpp
*
* Created on: University Collage London.
* Date: 12.2018-01.2019
* Author: student number:18043309
*/
#include "SquareMatrix.h"
#include <stdio.h>
#include <iomanip>
#include <stdlib.h>
#include <math.h>
//standard constructor
SquareMatrix::SquareMatrix(const int noOfRow, const int noOfCols):Matrix (noOfRow, noOfCols){
}
//copy constructor
SquareMatrix::SquareMatrix(const SquareMatrix& input):Matrix(input){
}
//creat identity matrix
SquareMatrix SquareMatrix::eye(const int dim){
SquareMatrix matrix(dim,dim);
//set the diagonal values to 1.0
for(int i=0;i<dim*dim;++i){
matrix.data[GetIndex(i,i)]=1.0;
}
return matrix;
}
//create toeplitz matrix using one vector as input
SquareMatrix SquareMatrix::toeplitz(const double* vec1){
SquareMatrix toeplitz(4,4);
for(int i=0;i<4;++i){
for(int j=0;j<4;++j){
//interating through the index n of the input array
for(int n=0;n<4;++n){
if(i-j==n||j-i==n){
//use the values of the vec1 array to assign for toeplitz matrix
toeplitz.data[toeplitz.GetIndex(i,j)]=vec1[n];
}
}
}
}
return toeplitz;
}
//create toeplitz matrix using two vectors as input
SquareMatrix SquareMatrix::toeplitz(const double* vec1,const double* vec2){
SquareMatrix toeplitz(4,4);
for(int i=0;i<4;++i){
for(int j=0;j<4;++j){
//interating through the index n of the input array
for(int n=0;n<4;++n){
if(i-j==n){
//use the values of the vec1 array to assign for toeplitz matrix
toeplitz.data[toeplitz.GetIndex(i,j)]=vec1[n];
}
else if(j-i==n){
//use the values of the vec2 array to assign for toeplitz matrix
toeplitz.data[toeplitz.GetIndex(i,j)]=vec2[n];
}
}
}
}
return toeplitz;
}
//create triu function
SquareMatrix SquareMatrix::triu(const SquareMatrix& matrix,const int dim){
SquareMatrix triu(dim,dim);
for(int i=0;i<dim;++i){
for(int j=0;j<dim;++j){
//assign entries for upper triangular part
if(i<=j){
triu.data[GetIndex(i,j)]=matrix.data[matrix.GetIndex(i,j)];
}
//set 0 to lower triangular part
else{
triu.data[GetIndex(i,j)]=0.0;
}
}
}
return triu;
}
//create tril function
SquareMatrix SquareMatrix::tril(const SquareMatrix& matrix,const int dim){
SquareMatrix tril(dim,dim);
for(int i=0;i<dim;++i){
for(int j=0;j<dim;++j){
//set 0 to upper triangular part
if(i<j){
tril.data[GetIndex(i,j)]=0.0;
}
//assign entries for lowver triangular part
else{
tril.data[GetIndex(i,j)]=matrix.data[matrix.GetIndex(i,j)];
}
}
}
return tril;
}
//lu function with Algorithm 2.1
void SquareMatrix::lu1(const SquareMatrix& matrix){
//set precision
cout.precision(4);
//make cout print floats with a fixed number of decimals
cout.setf(ios::fixed);
int det=0, p, h, k, i, j, temp[4][4];
//initialise umatrix1 with the input matrix
SquareMatrix umatrix1(matrix.noOfRows,matrix.noOfColumns);
for(int i=0;i<matrix.noOfRows;++i){
for(int j=0;j<noOfColumns;++j){
umatrix1.data[GetIndex(i,j)]=matrix.data[matrix.GetIndex(i,j)];
}
}
//initialise lmatrix1 with identity matrix
SquareMatrix lmatrix1(matrix.noOfRows,matrix.noOfColumns);
for(int i=0;i<matrix.noOfRows;++i){
for(int j=0;j<matrix.noOfColumns;++j){
lmatrix1.data[GetIndex(i,i)]=1.0;
}
}
//initialize the value of m as the row of the input matrix
int m=matrix.noOfRows;
for (int i = 0; i < m; i++)
//determine whether this algorithm can be used
if (umatrix1.data[GetIndex(i,i)]==0)
{
cout << "can't use this meathod,because some of the pivots are equal to zero" << endl;
cout<<endl;
return;
}
//loop to perform the gaussian elimination
for(int k=0;k<m-1;++k){
for (int j=k+1;j<m;++j){
//get the values of lower matrix
lmatrix1.data[lmatrix1.GetIndex(j,k)]=(umatrix1.data[umatrix1.GetIndex(j,k)]) / (umatrix1.data[umatrix1.GetIndex(k,k)]);
for(int n=k;n<m;++n){
//get the values of upper matrix
umatrix1.data[umatrix1.GetIndex(j,n)]=umatrix1.data[umatrix1.GetIndex(j,n)]-lmatrix1.data[lmatrix1.GetIndex(j,k)]*umatrix1.data[umatrix1.GetIndex(k,n)];
}
}
}
//check if input matrix is singular matrix
for(int i=0;i<matrix.noOfRows;i++){
for(int j=i;j<matrix.noOfColumns;j++){
if (i>=j){
//check if any entry of upper triangular matrix is equal to 0 or NAN
if(umatrix1.data[umatrix1.GetIndex(i,j)]==0||umatrix1.data[umatrix1.GetIndex(i,j)]==NAN){
cout<<"please enter non-singular matrix"<<endl;
cout<<endl;
return;
}
else{
break;
}
}
else{
break;
}
}
}
for(int i=0;i<matrix.noOfRows;i++){
for(int j=0;j<=i;j++){
//check if any entry of lower triangular matrix is equal to 0 or NAN
if(lmatrix1.data[lmatrix1.GetIndex(i,j)]==0||lmatrix1.data[lmatrix1.GetIndex(i,j)]==NAN){
cout<<"please enter non-singular matrix"<<endl;
cout<<endl;
return;
}
else{
break;
}
}
}
//print the lmatrix1 and umatrix1
cout<<"lmatrix1 = "<<endl;
cout<<lmatrix1<<endl;
cout<<"umatrix1 = "<<endl;
cout<<umatrix1<<endl;
}
//lu function with Algorithm 2.2
void SquareMatrix::lu2(const SquareMatrix& matrix,SquareMatrix& lmatrix, SquareMatrix& umatrix, SquareMatrix& pmatrix){
//set precision
cout.precision(4);
//make cout print floats with a fixed number of decimals
cout.setf(ios::fixed);
int m=matrix.noOfRows;
int i,j,k,n;
//initialise lmatrix1 with the input matrix
SquareMatrix umatrix1(matrix.noOfRows,matrix.noOfColumns);
for(i=0;i<matrix.noOfRows;++i){
for(j=0;j<noOfColumns;++j){
umatrix1.data[GetIndex(i,j)]=matrix.data[matrix.GetIndex(i,j)];
}
}
//initialise lmatrix1 with identity matrix
SquareMatrix lmatrix1(matrix.noOfRows,matrix.noOfColumns);
for(i=0;i<matrix.noOfRows;++i){
for(j=0;j<matrix.noOfColumns;++j){
lmatrix1.data[GetIndex(i,i)]=1.0;
}
}
//initialise pmatrix1 with identity matrix
SquareMatrix pmatrix1(matrix.noOfRows,matrix.noOfColumns);
for(i=0;i<matrix.noOfRows;++i){
for(j=0;j<matrix.noOfColumns;++j){
pmatrix1.data[GetIndex(i,i)]=1.0;
}
}
for(k=0;k<m-1;++k){
for(j=k+1;j<m;j++){
//determines the entry with the largest absolute value
if(abs(umatrix1.data[umatrix1.GetIndex(k,k)])<abs(umatrix1.data[umatrix1.GetIndex(j,k)])){
//swap the row containing the diagonals of the column with the row containing the new pivot.
for(n=k;n<m;n++){
double tmp1= umatrix1.data[umatrix1.GetIndex(k,n)];
umatrix1.data[umatrix1.GetIndex(k,n)]=umatrix1.data[umatrix1.GetIndex(j,n)];
umatrix1.data[umatrix1.GetIndex(j,n)]=tmp1;
}
for(n=0;n<k;n++){
double tmp2= lmatrix1.data[lmatrix1.GetIndex(k,n)];
lmatrix1.data[lmatrix1.GetIndex(k,n)]=lmatrix1.data[lmatrix1.GetIndex(j,n)];
lmatrix1.data[lmatrix1.GetIndex(j,n)]=tmp2;
}
//permutation matrix records this swapping of rows
for(n=0;n<m;n++){
double tmp3= pmatrix1.data[umatrix1.GetIndex(k,n)];
pmatrix1.data[pmatrix1.GetIndex(k,n)]=pmatrix1.data[pmatrix1.GetIndex(j,n)];
pmatrix1.data[pmatrix1.GetIndex(j,n)]=tmp3;
}
}
}
//loop to implement the gaussian elimination
for (int j=k+1;j<m;++j){
//get the values of lower matrix
lmatrix1.data[lmatrix1.GetIndex(j,k)]=(umatrix1.data[umatrix1.GetIndex(j,k)]) / (umatrix1.data[umatrix1.GetIndex(k,k)]);
for(int n=k;n<m;++n){
//get the values of upper matrix
umatrix1.data[umatrix1.GetIndex(j,n)]=umatrix1.data[umatrix1.GetIndex(j,n)]-lmatrix1.data[lmatrix1.GetIndex(j,k)]*umatrix1.data[umatrix1.GetIndex(k,n)];
}
}
}
//assign matrices in order to use in the test file
lmatrix=lmatrix1;
umatrix=umatrix1;
pmatrix=pmatrix1;
}
//create testing matrix
SquareMatrix& SquareMatrix::TestingMatrixInput(const double* input, const int noOfRows,const int noOfColumns){
//initialise matrix as the type of SquareMatrix
SquareMatrix matrix (noOfRows,noOfColumns);
//assign entries of matrix by input array
for(int i =0;i<noOfRows*noOfColumns;i++){
matrix.data[i]=*(input+i);
}
//assign the value of the object by matrix
*this = matrix;
return *this;
}
//Solve for y for equation Ly=Pb as algorithm 3.1:Forward substitution
SquareMatrix SquareMatrix::forwardSubstitution(const SquareMatrix& lmatrix,const SquareMatrix& b,const int m){
//initialise y as the type of SquareMatrix
SquareMatrix y(m,1);
y.data[y.GetIndex(0,0)]=b.data[y.GetIndex(0,0)]/lmatrix.data[0];
for(int k=1;k<m;k++){
//initialise sum to store the sum of partial iteration
double sum=0;
for(int j=0;j<=k-1;j++){
sum += lmatrix.data[lmatrix.GetIndex(k,j)]*y.data[y.GetIndex(j,0)];
y.data[y.GetIndex(k,0)]=(b.data[y.GetIndex(k,0)]-sum) / lmatrix.data[lmatrix.GetIndex(k,k)];
}
}
return y;
}
//solve for x in equation Ux=y as algorithm 3.2:Back substitution
void SquareMatrix::backSubstitution(const SquareMatrix& umatrix,const SquareMatrix& b,const int m){
//set precision
cout.precision(4);
//make cout print floats with a fixed number of decimals
cout.setf(ios::fixed);
//define the dynamic array to store the solutions x
double* x=new double[m];
x[m-1]=b.data[b.GetIndex(m-1,0)]/umatrix.data[umatrix.GetIndex(m-1,m-1)];
for(int k=m-2;k>=0;k--){
//initialise sum to store the sum of partial iteration
double sum=0;
for(int j=k+1;j<m;j++){
sum += umatrix.data[umatrix.GetIndex(k,j)]*x[j];
x[k]=(b.data[b.GetIndex(k,0)]-sum) / umatrix.data[umatrix.GetIndex(k,k)];
}
}
//print the solution
cout<<"the solution of this program for linear equtions = "<<endl;
cout<<endl;
for(int i=0;i<m;i++){
if(x[i]>=0){
cout<<"x"<<i+1<<" = "<<' '<<x[i]<<endl;
}
else{
cout<<"x"<<i+1<<" = "<<x[i]<<endl;
}
}
delete[] x;
}
//get the Pb for the right hand side of equation LUx=Pb
SquareMatrix SquareMatrix::b_permutation(const SquareMatrix& matrix,const double* b,const int dim){
SquareMatrix P(matrix.noOfRows,matrix.noOfColumns);
//initialise matrix_b as the type of SquareMatrix
SquareMatrix matrix_b(dim,1);
//assign entries of matrix_b by input array b
for(int i=0;i<dim;i++){
matrix_b.data[(matrix_b.GetIndex(i,0))]=b[i];
}
P = matrix;
//assign the value of P as the product of P*b
P *= matrix_b;
return P;
}