-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrix.cpp
426 lines (351 loc) · 11.1 KB
/
Matrix.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
/*
* Matrix.cpp
*
* Created on: University Collage London.
* Date: 12.2018-01.2019
* Author: student number:18043309
*/
#include "Matrix.h"
#include <stdio.h>
//determine row-column indexing mapping to 1-D indexing
int Matrix::GetIndex (const int rowIdx, const int columnIdx) const{
//assuming matrix is mapped onto a 1-D array column-wise
return columnIdx*noOfRows+rowIdx;
}
//standard constructor
Matrix::Matrix (const int noOfRow, const int noOfCols){
//assign for member variables in standard constructor
noOfRows=noOfRow;
noOfColumns=noOfCols;
//allocate memory
data=new double[noOfRows*noOfColumns*2];
//initialise each array entry to 0.0
for(int i=0;i<noOfRows*noOfColumns;++i){
data[i]=0.0;
}
}
//copy constructor
Matrix::Matrix (const Matrix& input){
//assign for member variables in copy constructor
noOfColumns=input.noOfColumns;
noOfRows=input.noOfRows;
//allocate memory
data=new double[noOfRows*noOfColumns];
//array entry-wise copy
for(int i=0;i<noOfRows*noOfColumns;++i){
data[i]=input.data[i];
}
}
//destructor
Matrix::~Matrix(){
delete[] data;
}
//static member funtion for assigning values to 0.0 to object matrix
Matrix Matrix::Zeros(const int noOfRows, const int noOfColumns){
Matrix matrix(noOfRows,noOfColumns);
//set every entry to 0.0
for(int i=0;i<noOfRows*noOfColumns;++i){
matrix.data[i]=0.0;
}
return matrix;
}
//static member funtion for assigning values to 1.0 to object matrix
Matrix Matrix::Ones(const int noOfRows, const int noOfColumns){
Matrix matrix(noOfRows,noOfColumns);
//set every entry to 1.0
for(int i=0;i<noOfRows*noOfColumns;++i){
matrix.data[i]=1.0;
}
return matrix;
}
//assignment operator
Matrix& Matrix::operator= (const Matrix& rhs){
//prevent the same matrix assignment
if(this==&rhs){
return *this;
}
else{
noOfRows=rhs.noOfRows;
noOfColumns=rhs.noOfColumns;
//assign the every value of lhs by the values of rhs
for(int i=0;i<noOfRows*noOfColumns;i++){
data[i]=rhs.data[i];
}
}
}
//operator overloading to output the matrix
ostream& operator<< (ostream& out, const Matrix& rhs){
//interating through the rows
for(int i=0;i<rhs.noOfRows;++i){
//interating through the columns
for(int j=0;j<rhs.noOfColumns;++j){
//adjust the space to align the matrix
if(rhs.data[rhs.GetIndex(i,j)]<0){
out<<rhs.data[rhs.GetIndex(i,j)]<<' ';
}
else{
out<<' '<<rhs.data[rhs.GetIndex(i,j)]<<' ';
}
}
out<<endl;
}
return out;
}
//create Toeplitz matrix
Matrix Matrix::Toeplitz(const double* column, const int noOfRows,const double* row,const int noOfColumns){
Matrix toeplitz(noOfRows,noOfColumns);
for(int i=0;i<noOfRows;++i){
for(int j=0;j<noOfColumns;++j){
//when the number of column is equal or larger than the number of row
if(i>=j){
//interating through the index n of the input array
for(int n=0;n<noOfRows;++n){
//use the values of the column array to assign for toeplitz matrix
if(i-j==n){
toeplitz.data[toeplitz.GetIndex(i,j)]=column[n];
}
//use the values of the row array to assign for toeplitz matrix
else if(j-i==n){
toeplitz.data[toeplitz.GetIndex(i,j)]=row[n];
}
}
}
//when the number of column is smaller than the number of row
else{
for(int n=0;n<noOfColumns;++n){
//use the values of the row array to assign for toeplitz matrix
if(j-i==n){
toeplitz.data[toeplitz.GetIndex(i,j)]=row[n];
}
//use the values of the column array to assign for toeplitz matrix
else if(i-j==n){
toeplitz.data[toeplitz.GetIndex(i,j)]=column[n];
}
}
}
}
}
return toeplitz;
}
//print function for ToeplitzTestingHelper in test file
void Matrix::Print(const double* array, const int noOfRows,const int noOfColumns){
//print for the first column
if(noOfColumns==1){
for(int i=0;i<noOfRows;++i){
//adjust the space to align the matrix
if(array[i]<0){
cout<<array[i]<<endl;
}
else{
cout<<' '<<array[i]<<endl;
}
}
}
//print for the first row
else if(noOfRows==1){
for(int i=0;i<noOfColumns;++i){
cout<<' '<<array[i]<<' '<<' '<<' ';
}
cout<<endl;
}
//print for the expected matrix
else{
for(int i=0;i<noOfRows;++i){
for(int j=0;j<noOfColumns;++j){
//adjust the space to align the matrix
if(array[i+noOfRows*j]<0){
cout<<array[i+noOfRows*j]<<' ';
}
else{
cout<<' '<<array[i+noOfRows*j]<<' ';
}
}
cout<<endl;
}
}
}
//static transpose function
Matrix Matrix::Transpose(const Matrix& matrix){
//define a matrix as the retult of transpose matrix of the original matrix
Matrix trans(matrix.noOfColumns,matrix.noOfRows);
for(int i=0;i<matrix.noOfColumns;++i){
for(int j=0;j<matrix.noOfRows;++j){
//assign the trans matrix by the original matrix with the index i and j switched
trans.data[trans.GetIndex(i,j)]=matrix.data[matrix.GetIndex(j,i)];
}
}
return trans;
}
//non-static transpose function
Matrix& Matrix::Transpose(){
//define a matrix, using the noOfColumns of original matrix as the number of rows
//and the noOfRows of original matrix asthe number of columns
Matrix trans((*this).noOfColumns,(*this).noOfRows);
for(int i=0;i<(*this).noOfColumns;++i){
for(int j=0;j<(*this).noOfRows;++j){
//assign the trans matrix by the original matrix with the index i and j switched
trans.data[trans.GetIndex(i,j)]=data[GetIndex(j,i)];
}
}
//assign the original matrix as the new matrix
*this=trans;
return *this;
}
// a non-member function but a friend for "*" operator overloading
Matrix operator * (const Matrix& lhs,const Matrix rhs){
//create a matrix as the product of lhs and rhs
Matrix result(lhs.noOfRows,rhs.noOfColumns);
for(int i=0;i<result.noOfRows;i++){
for(int j=0;j<result.noOfColumns;j++){
for(int k=0;k<lhs.noOfColumns;k++){
//use the regulation of matrix multiplication
result.data[result.GetIndex(i,j)]+=lhs.data[lhs.GetIndex(i,k)]*rhs.data[rhs.GetIndex(k,j)];
}
}
}
return result;
}
//a member function of multiplication assignment operator
Matrix& Matrix::operator*= (const Matrix& rhs){
//create a matrix as the product of lhs (*this point the matrix which call the function in test file ) and rhs
Matrix result=(*this)*rhs;
//assgin the lhs matrix as the product result.
(*this)=result;
return *this;
}
//exchange the whole rows
Matrix Matrix::ExchangeRows(int row1,int row2){
//create a matrix which is equal to the original matrix
Matrix exchangeRow(noOfRows,noOfColumns);
exchangeRow=(*this);
//loop through all the values of the matrix to exchange two rows
for(int i=0;i<noOfRows;i++){
for(int j=0;j<noOfColumns;j++){
//exchange two rows
exchangeRow.data[exchangeRow.GetIndex(row1,j)]=(*this).data[(*this).GetIndex(row2,j)];
exchangeRow.data[exchangeRow.GetIndex(row2,j)]=(*this).data[(*this).GetIndex(row1,j)];
}
}
return exchangeRow;
}
//exchange partial values of the rows
Matrix Matrix::ExchangeRows(int row1,int row2,int column1,int column2){
//create a matrix, which is equal to the matrix that has already exchanged 2nd and 4th rows
Matrix exchangeRow(noOfRows,noOfColumns);
exchangeRow=(*this).ExchangeRows(1, 3);
for(int i=0;i<noOfRows;++i){
for(int j=0;j<noOfColumns;++j){
//exchange rows but only for the specified columns
for(int n=column1;n<=column2;++n){
exchangeRow.data[exchangeRow.GetIndex(row2,n)]=((*this).ExchangeRows(1, 3)).data[((*this).ExchangeRows(1, 3)).GetIndex(row1,n)];
exchangeRow.data[exchangeRow.GetIndex(row1,n)]=((*this).ExchangeRows(1, 3)).data[((*this).ExchangeRows(1, 3)).GetIndex(row2,n)];
}
}
}
return exchangeRow;
}
//exchange the whole columns
Matrix Matrix::ExchangeColumns(int column1,int column2){
//create a matrix which is equal to the original matrix
Matrix exchangeColumn(noOfRows,noOfColumns);
exchangeColumn=(*this);
//loop through all the values of the matrix to exchange two columns
for(int i=0;i<noOfRows;i++){
for(int j=0;j<noOfColumns;j++){
//exchange two columns
exchangeColumn.data[exchangeColumn.GetIndex(i,column1)]=(*this).data[(*this).GetIndex(i,column2)];
exchangeColumn.data[exchangeColumn.GetIndex(i,column2)]=(*this).data[(*this).GetIndex(i,column1)];
}
}
return exchangeColumn;
}
//exchange partial values of the columns
Matrix Matrix::ExchangeColumns(int column1,int column2,int row1,int row2){
//create a matrix, which is equal to the matrix that has already exchanged 1st and 2nd column
Matrix exchangeColumn(noOfRows,noOfColumns);
exchangeColumn=(*this).ExchangeColumns(0, 1);
for(int i=0;i<noOfRows;++i){
for(int j=0;j<noOfColumns;++j){
//exchange columns but only for the specified rows
for(int n=row1;n<=row2;++n){
exchangeColumn.data[exchangeColumn.GetIndex(n,column2)]=((*this).ExchangeColumns(0, 1)).data[((*this).ExchangeColumns(0, 1)).GetIndex(n,column1)];
exchangeColumn.data[exchangeColumn.GetIndex(n,column1)]=((*this).ExchangeColumns(0, 1)).data[((*this).ExchangeColumns(0, 1)).GetIndex(n,column2)];
}
}
}
return exchangeColumn;
}
//The function that sets every entry to zero
Matrix& Matrix::Zeros(){
for(int i=0;i<(*this).noOfRows;++i){
for(int j=0;j<(*this).noOfColumns;++j){
data[GetIndex(i,j)]=0.0;
}
}
return *this;
}
//The function that sets every entry to one
Matrix& Matrix::Ones(){
for(int i=0;i<(*this).noOfRows;++i){
for(int j=0;j<(*this).noOfColumns;++j){
data[GetIndex(i,j)]=1.0;
}
}
return *this;
}
//The function for getting the number of rows
int Matrix::GetNoOfRows()const{
return this->noOfRows;
}
//The function for getting the number of columns
int Matrix::GetNoOfColumns()const{
return this->noOfColumns;
}
//The function for getting the entries
int Matrix::GetEntry(int row,int column)const{
return data[GetIndex(row,column)];
}
//get the product of AT*A for least square solution
double* Matrix::lss_ATA(const Matrix& A){
//get the transpose matrix of A
Matrix AT=Transpose(A);
//intialise the product of A and the transpose matrix of A
Matrix At_A(A.noOfColumns,A.noOfColumns);
//get the product of A and the transpose matrix of A
At_A = AT*A;
static double AT_A[9];
for(int i=0;i<A.noOfRows*A.noOfColumns;i++){
AT_A[i]=At_A.data[i];
}
return AT_A;
}
//get the product of AT*b for least square solution
double* Matrix::lss_ATB(const Matrix& A,const double* b){
//get the transpose matrix of A
Matrix AT=Transpose(A);
//initialise B as Matrix class in order to multiply with AT
Matrix B(noOfRows,1);
for(int i=0;i<noOfRows;i++){
B.data[i]=b[i];
}
//intialise the product of B and the transpose matrix of A
Matrix At_B(A.noOfColumns,1);
//get the product of A and the transpose matrix of A
At_B = AT*B;
static double AT_B[3];
for(int i=0;i<A.noOfColumns;i++){
AT_B[i]=At_B.data[i];
}
return AT_B;
}
Matrix& Matrix::TestingInput(const double* input, const int noOfRows,const int noOfColumns){
//initialise matrix as the type of SquareMatrix
Matrix 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;
}