-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathMatrix.cpp
More file actions
1168 lines (1054 loc) · 24.8 KB
/
Copy pathMatrix.cpp
File metadata and controls
1168 lines (1054 loc) · 24.8 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
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include<iostream>
#include"Matrix.h"
//#include"stdlib.h"
#include <stdio.h>
#include"string.h"
#include"map"
#include<string>
#include<math.h>
//#include<fstream>
#include <algorithm>
//#include<sstream>
//#include <cctype>
//#include <locale>
using namespace std;
class Exception
{
public:
const char* msg;
Exception(const char* arg)
: msg(arg)
{
}
};
int nRow;
int nCol;
double **pData;
char name;
//default constructor
Matrix:: Matrix()
{
this->nRow=0; this->nCol=0;
this->pData=new double*[0];
for(int i=0;i<0;i++){
this->pData[i]=new double[0];
}
}
// constructor
Matrix::Matrix(int row,int col){
nRow=row; nCol=col;
pData=new double*[row];
for(int i=0;i<col;i++){
pData[i]=new double[col];
}
}
Matrix::Matrix(string s)
{
char str[s.size()+1];
strcpy(str,s.c_str());
char*token;
int nR=0,nC=0;
token=strtok(str,"[,;,]");
while(token)
{
nR++;
token=strtok(NULL,"[,;,]");
}
int start=s.find("[")+1;
int End=s.find(";");
string Col=s.substr(start,End-start);
char colstring[Col.size()+1];
strcpy(colstring,Col.c_str());
token=strtok(colstring," ");
while (token)
{
nC++;
token=strtok(NULL," ");
}
nRow=nR;
nCol=nC;
pData=new double*[nRow];
for(int i=0;i<nRow;i++)
pData[i]=new double[nCol];
strcpy(str,s.c_str());
double values[nR*nC];
int index=0;
token=strtok(str," ,[,],;");
while (token)
{
values[index]=atof(token);
index++;
token=strtok(NULL," ,[,],;");
}
int k=0;
for (int i=0;i<nRow;i++)
{
for(int j=0;j<nCol;j++)
{
pData[i][j]=values[k];
k++;
}
}
}
void Matrix::horzcat(Matrix& A,Matrix& B)
{
if (pData)
{
for (int i=0;i<nRow;i++)
delete[] pData[i];
delete[] pData;
}
nRow=0;
nCol=0;
pData=NULL;
double temp,temp2;
if(A.nRow != B.nRow)
throw Exception("InValid Matrix dimension");
nRow=A.nRow;
nCol=A.nCol+B.nCol;
pData=new double*[nRow];
for(int i=0;i<nRow;i++)
pData[i]=new double [nCol];
for(int i=0;i<nRow;i++)
{
int k=0;
for(int j=0;j<nCol;j++)
{
if(j<A.nCol)
{
pData[i][j]=A.pData[i][j];
}
else
{
pData[i][j]=B.pData[i][k];
k++;
}
}
}
}
string Matrix::toString(void)
{
string mat="";
for(int i=0;i<nRow;i++)
{
for(int j=0;j<nCol;j++)
{
mat+=to_string(pData[i][j]);
if(j!=nCol-1)
mat+=" ";
}
if(i!=nRow-1)
mat+=";";
}
mat+="";
return mat;
}
/* ~Matrix()
{
for (int i=0;i<nCol;i++)
{
delete pData[i];
}
delete pData;
} */
void Matrix:: setValue(double data[]){
//set values
int i=0;
for(int r=0;r<this->nRow;r++){
for(int c=0;c<this->nCol;c++){
this->pData[r][c]=data[i];
i++;
}
}
}
void Matrix:: reset()
{
if (pData)
{
for (int i=0;i<nRow;i++)
delete[] pData[i];
delete[] pData;
}
nRow=0;
nCol=0;
pData=NULL;
}
Matrix Matrix::zeros(int nR, int nC)
{
Matrix t(nR, nC);
if ((nR*nC) == 0)
{
t.pData = NULL;
return t;
}
t.pData = new double*[nR];
for (int iR = 0; iR < nR; iR++)
{
t.pData[iR] = new double[nC];
for (int iC = 0; iC < nC; iC++)
{
t.pData[iR][iC] = 0;
}
}
return t;
}
Matrix Matrix::ones(int nR, int nC)
{
Matrix t(nR, nC);
if ((nR*nC) == 0)
{
t.pData = NULL;
return t;
}
t.pData = new double*[nR];
for (int iR = 0; iR < nR; iR++)
{
t.pData[iR] = new double[nC];
for (int iC = 0; iC < nC; iC++)
{
t.pData[iR][iC] = 1;
}
}
return t;
}
Matrix Matrix::eye(int nR, int nC)
{
Matrix t(nR, nC);
if ((nR*nC) == 0)
{
t.pData = NULL;
return t;
}
t.pData = new double*[nR];
if (nR == nC)
{
for (int iR = 0; iR < nR; iR++)
{
t.pData[iR] = new double[nC];
for (int iC = 0; iC < nC; iC++)
{
t.pData[iR][iC] = (iR == iC) ? 1 : 0;
}
}
}
else
{
cout << "error" << endl;
Matrix n;
return n;
}
return t;
}
Matrix Matrix::randM(int nR, int nC)
{
Matrix t(nR, nC);
if ((nR*nC) == 0)
{
t.pData = NULL;
return t;
}
t.pData = new double*[nR];
for (int iR = 0; iR < nR; iR++)
{
t.pData[iR] = new double[nC];
for (int iC = 0; iC < nC; iC++)
{
t.pData[iR][iC] = (rand() % 100) + 1;
}
}
return t;
}
Matrix Matrix::sin(Matrix m){
Matrix temp(m.nRow,m.nCol);
for(int i=0; i<m.nRow; i++){
for(int j=0; j<m.nCol; j++){
temp.pData[i][j]=std::sin(m.pData[i][j]);
}
}
return temp;
}
Matrix Matrix::cos(Matrix m){
Matrix temp(m.nRow,m.nCol);
for(int i=0; i<m.nRow; i++){
for(int j=0; j<m.nCol; j++){
temp.pData[i][j]=std::cos(m.pData[i][j]);
}
}
return temp;
}
Matrix Matrix::tan(Matrix m){
Matrix temp(m.nRow,m.nCol);
for(int i=0; i<m.nRow; i++){
for(int j=0; j<m.nCol; j++){
temp.pData[i][j]=std::tan(m.pData[i][j]);
}
}
return temp;
}
//Getters
// index operator.
double& Matrix::operator()(const int r, const int c)
{
if (pData != NULL && r > 0 && r <= nRow && c > 0 && c <= nCol)
{
return this->pData[r-1][c-1];
}
else
{
throw Exception("Subscript out of range");
}
}
int Matrix::getRow(){
return this->nRow;
}
int Matrix:: getCol(){
return this->nCol;
}
double Matrix::get(int r,int c){
return this->pData[r][c];
}
void Matrix::printMatrix(){
for(int r=0;r<nRow;r++){
for(int c=0;c<nCol;c++){
cout<<pData[r][c]<<" ";
}
cout<<endl;
}
}
// add a double value
Matrix Matrix::add(Matrix a, double v)
{
int row=a.getRow(); int col=a.getCol(); int i=0;
double *values=new double[row*col];
for(int r=0;r<row;r++){
for(int c=0;c<col;c++){
values[i]=a.get(r,c)+v;
i++;
}
}
Matrix c(row,col);
c.setValue(values);
return c;
}
//add matrix with matrix
Matrix Matrix:: add(Matrix a, Matrix b){
int row=a.getRow(); int col=a.getCol(); int i=0;
double *values=new double[row*col];
//check that the two matrices has the same dimension
if(a.getRow()==b.getRow() && a.getCol()==b.getCol()){
for(int r=0;r<row;r++){
for(int c=0;c<col;c++){
values[i]=a.get(r,c)+b.get(r,c);
i++;
}
}
}
else
{
// give an error
throw Exception("Dimensions does not match");
}
Matrix c(row,col);
c.setValue(values);
return c;
}
// subtract a double value (elements wise)
Matrix Matrix::subtract(Matrix a, double v)
{
return add(a,-v);
}
Matrix Matrix::subtract(Matrix a, Matrix b){
int row=a.getRow(); int col=a.getCol(); int i=0;
double *values=new double[row*col];
//check that the two matrices has the same dimension
if(a.getRow()==b.getRow() && a.getCol()==b.getCol()){
for(int r=0;r<row;r++){
for(int c=0;c<col;c++){
values[i]=a.get(r,c)-b.get(r,c);
i++;
}
}
}
else{cout<<"nonconformant arguments"<<endl;}
Matrix c(row,col);
c.setValue(values);
return c;
}
// multiply by double value
Matrix Matrix::Multiply(Matrix a,double v)
{
for (int r = 0; r < a.nRow; r++)
{
for (int c = 0; c < a.nCol; c++)
{
a.pData[r][c] *= v;
}
}
return *this;
}
// multiply matrix by matrix
Matrix Matrix::Multiply(Matrix a ,Matrix b){
double values[a.nRow*b.nCol]; int i=0;
// check if the dimensions match
// cout<<a.nCol<<"--"<<b.nRow<<endl;
if (a.nCol == b.nRow)
{
Matrix res(a.nRow, b.nCol);
for (int r = 0; r < a.nRow; r++)
{
for (int c_res = 0; c_res < b.nCol; c_res++)
{
for (int c = 0; c < a.nCol; c++)
{
res.pData[r][c_res] += a.pData[r][c] * b.pData[c][c_res];
// values[i]+= a.pData[r][c] * b.pData[c][c_res];
}
}
i++;
}
// res.setValue(values);
return res;
}
else
{
// give an error
throw Exception("Dimensions does not match");
}
}
// divide by double value
Matrix Matrix:: Divide( double v)
{
return Multiply(*this,1/v);
}
Matrix Matrix::transpose(Matrix A)
{
Matrix c(A.nRow,A.nCol);
if (A.nRow == A.nCol)
{
for (int i=0;i<nRow;i++)
{
for (int j=0;j<nCol;j++)
{
c.pData[i][j]=A.pData[j][i];
}
}
}
else
{ cout<< "The matrix must be a square matrix";}
return c;
}
// swap two values
void Matrix::Swap(double& a, double& b)
{
double temp = a;
a = b;
b = temp;
}
Matrix Matrix:: operator /(float x)
{
Matrix A(nRow,nCol);
for(int i=0;i<nRow;i++)
{
for(int j=0;j<nCol;j++)
{
A.pData[i][j]=pData[i][j]/x;
}
}
return A;
}
/**
* returns a diagonal matrix with size n x n with ones at the diagonal
* @param v a vector
* @return a diagonal matrix with ones on the diagonal
*/
Matrix Matrix::Diag(const int n)
{
Matrix res = Matrix(n, n);
for (int i = 1; i <= n; i++)
{
res(i, i) = 1;
}
return res;
}
/**
* returns a diagonal matrix
* @param v a vector
* @return a diagonal matrix with the given vector v on the diagonal
*/
Matrix Matrix::Diag( Matrix& v)
{
Matrix res;
if (v.getCol() == 1)
{
// the given matrix is a vector n x 1
int rows = v.getRow();
res = Matrix(rows, rows);
// copy the values of the vector to the matrix
for (int r=1; r <= rows; r++)
{
res(r, r) = v.get(r, 1);
}
}
else if (v.getRow() == 1)
{
// the given matrix is a vector 1 x n
int cols = v.getCol();
res = Matrix(cols, cols);
// copy the values of the vector to the matrix
for (int c=1; c <= cols; c++)
{
res(c, c) = v.get(1, c);
}
}
else
{
throw Exception("Parameter for diag must be a vector");
}
return res;
}
/**
* returns the minor from the given matrix where
* the selected row and column are removed
*/
Matrix Matrix:: Minor(const int row, const int col)
{
Matrix res;
if (row > 0 && row <= nRow && col > 0 && col <= nCol)
{
res = Matrix(nRow - 1, nCol - 1);
// copy the content of the matrix to the minor, except the selected
for (int r = 1; r <= (nRow - (row >= nRow)); r++)
{
for (int c = 1; c <= (nCol - (col >= nCol)); c++)
{
res(r - (r > row), c - (c > col)) = pData[r-1][c-1];
}
}
}
else
{
throw Exception("Index for minor out of range");
}
return res;
}
/*
* returns the determinant of Matrix a
*/
double Matrix::Det( Matrix& a)
{
double d = 0; // value of the determinant
int rows = a.getRow();
int cols = a.getRow();
if (rows == cols)
{
// this is a square matrix
if (rows == 1)
{
// this is a 1 x 1 matrix
d = a.get(1, 1);
}
else if (rows == 2)
{
// this is a 2 x 2 matrix
// the determinant of [a11,a12;a21,a22] is det = a11*a22-a21*a12
d = a.get(1, 1) * a.get(2, 2) - a.get(2, 1) * a.get(1, 2);
}
else
{
// this is a matrix of 3 x 3 or larger
for (int c = 1; c <= cols; c++)
{
Matrix M = a.Minor(1, c);
//d += pow(-1, 1+c) * a(1, c) * Det(M);
d += (c%2 + c%2 - 1) * a.get(1, c) * Det(M); // faster than with pow()
}
}
}
else
{
throw Exception("Matrix must be square");
}
return d;
}
/*
// swap two values
void Swap(double& a, double& b)
{
double temp = a;
a = b;
b = temp;
}
*/
/*
* returns the inverse of Matrix a
*/
Matrix Matrix::Inv( Matrix& a)
{
Matrix res;
double d = 0; // value of the determinant
int rows = a.getRow();
int cols = a.getRow();
d = Det(a);
if (rows == cols && d != 0)
{
// this is a square matrix
if (rows == 1)
{
// this is a 1 x 1 matrix
res = Matrix(rows, cols);
res(1, 1) = 1 / a.get(1, 1);
}
else if (rows == 2)
{
// this is a 2 x 2 matrix
res = Matrix(rows, cols);
res(1, 1) = a.get(2, 2);
res(1, 2) = -a.get(1, 2);
res(2, 1) = -a.get(2, 1);
res(2, 2) = a.get(1, 1);
//res = (1/d) * res;
res=Multiply(res,(1/d));
}
else
{
// this is a matrix of 3 x 3 or larger
// calculate inverse using gauss-jordan elimination
// http://mathworld.wolfram.com/MatrixInverse.html
// http://math.uww.edu/~mcfarlat/inverse.htm
res = Diag(rows); // a diagonal matrix with ones at the diagonal
Matrix ai = a; // make a copy of Matrix a
for (int c = 1; c <= cols; c++)
{
// element (c, c) should be non zero. if not, swap content
// of lower rows
int r;
for (r = c; r <= rows && ai(r, c) == 0; r++)
{
}
if (r != c)
{
// swap rows
for (int s = 1; s <= cols; s++)
{
Swap(ai(c, s), ai(r, s));
Swap(res(c, s), res(r, s));
}
}
// eliminate non-zero values on the other rows at column c
for (int r = 1; r <= rows; r++)
{
if(r != c)
{
// eleminate value at column c and row r
if (ai(r, c) != 0)
{
double f = - ai(r, c) / ai(c, c);
// add (f * row c) to row r to eleminate the value
// at column c
for (int s = 1; s <= cols; s++)
{
ai(r, s) += f * ai(c, s);
res(r, s) += f * res(c, s);
}
}
}
else
{
// make value at (c, c) one,
// divide each value on row r with the value at ai(c,c)
double f = ai(c, c);
for (int s = 1; s <= cols; s++)
{
ai(r, s) /= f;
res(r, s) /= f;
}
}
}
}
}
}
else
{
if (rows == cols)
{
throw Exception("Matrix must be square");
}
else
{
throw Exception("Determinant of matrix is zero");
}
}
return res;
}
// division of Matrix with Matrix
Matrix Matrix::Divide ( Matrix a, Matrix b)
{
// check if the dimensions match: must be square and equal sizes
if (a.nRow == a.nCol && a.nRow == a.nCol && b.nRow == b.nCol)
{
Matrix res(a.nRow, a.nCol);
// res = a * Inv(b);
res=Multiply(a,Inv(b));
return res;
}
else
{
// give an error
throw Exception("Dimensions does not match");
}
// return an empty matrix (this never happens but just for safety)
return Matrix();
}
//*******************************************************************************************
Matrix Matrix::getCofactor(int row,int column) //get the cofactor of matrix NXN
{
if((nRow<1 &&nCol <1))
throw "Invalid Matrix Dimension";
Matrix subMat (nRow-1,nCol-1);
for (int iR=0;iR<subMat.nRow;iR++)
{
for (int iC=0;iC<subMat.nCol;iC++)
{
int sR=(iR<row)?iR:iR+1;
int sC=(iC<column)?iC:iC+1;
subMat.pData[iR][iC]=pData[sR][sC];
}
}
return subMat;
}
double Matrix::matDeterminant ()
{
if (nRow!=nCol)
throw "The matrix must be a square matrix";
if (nRow ==1 && nCol==1)
return pData[0][0];
else if (nRow ==2 && nCol == 2)
return pData[0][0]*pData[1][1]-pData[1][0]*pData[0][1];
else
{
double det=1,temp,temp1;
int r=0;
while (r<nRow)
{
temp=pData[r][r];
for (int ir=r+1;ir<nRow;ir++)
{
for (int ic=0;ic<nCol;ic++)
{
pData[ir][ic]=pData[ir][ic]-(pData[ir][r]/temp)*pData[r][ic];
}
}
r++;
}
for (int i=0;i<nRow;i++)
det*=pData[i][i];
return det;
}
}
void Matrix::matProduct(double Const) //Multiply each number in the matrix with Constant
{
for (int i=0;i<nRow;i++)
{
for (int j=0;j<nCol;j++)
{
pData[i][j]*=Const;
}
}
}
void Matrix::minorMat (Matrix &A) // get matrix of minors
{
double sign =1;
if (nRow==A.nRow && nCol==A.nCol)
{
for (int i=0;i<nRow;i++)
{
for (int j=0;j<nRow;j++)
{
pData[i][j]=A.getCofactor(i,j).matDeterminant();
pData[i][j]*=sign;
sign*=-1;
}
sign*=-1;
}
}
}
void Matrix::matTranspose(Matrix &A) //get the transpose of matrix
{
if (A.nRow == A.nCol)
{
for (int i=0;i<nRow;i++)
{
for (int j=0;j<nCol;j++)
{
pData[i][j]=A.pData[j][i];
}
}
}
else
"The matrix must be a square matrix";
}
bool Matrix :: isZeros()
{
int temp=0;
for (int ir=0;ir<nRow;ir++)
{
for (int ic=0;ic<nCol;ic++)
{
if (pData[ir][ic] == 0)
{
temp++;
}
}
}
if (temp == nRow * nCol)
return true;
else
return false;
}
void Matrix :: eye ()
{
for (int i=0;i<nRow;i++)
{
for (int j=0;j<nCol;j++)
{
if (i==j)
pData[i][j]=1;
else
pData[i][j]=0;
}
}
}
void Matrix :: inverseMat (Matrix A)
{
eye();
double temp,temp1,temp2,temp3,temp4;
int r=0;
while (r<nRow)
{
temp=A.pData[r][r];
if (temp == 0 )
{
for (int ir=r;ir<nRow;ir++)
{
if (r == nRow-1)
break;
temp1=A.pData[r+1][r];
for (int ic=0;ic<nCol;ic++)
{
if (temp1 == 0) break;
temp3=A.pData[r][ic];
temp4=pData[r][ic];
A.pData[ir][ic]=A.pData[ir+1][ic]/temp1;
pData[ir][ic]=pData[ir+1][ic]/temp1;
A.pData[ir+1][ic]=temp3;
pData[ir+1][ic]=temp4;
temp2=1;
}
if (temp2 ==1 ) break;
}
}
else
{
for (int c=0;c<nCol;c++)
{
A.pData[r][c]/=temp;
pData[r][c]/=temp;
temp2=A.pData[r][c];
temp3=pData[r][c];
}
}
if (r==0)
{
for (int ir=r+1;ir<nRow;ir++)
{
temp1=A.pData[ir][r];
for (int ic=0;ic<nCol;ic++)
{
A.pData[ir][ic]-=temp1*A.pData[r][ic];
pData[ir][ic]-=temp1*pData[r][ic];
temp2=A.pData[ir][ic];
temp3=pData[ir][ic];
}
}
}
else if (r==nRow-1)
{
for (int ir=r-1;ir>=0;ir--)
{
temp1=A.pData[ir][r];
for (int ic=0;ic<nCol;ic++)
{
A.pData[ir][ic]-=temp1*A.pData[r][ic] ;
pData[ir][ic]-=temp1*pData[r][ic] ;
temp2=A.pData[ir][ic];
temp3=pData[ir][ic];
}
}
}
else
{
for (int ir=r+1;ir<nRow;ir++)
{
temp1=A.pData[ir][r];
for (int ic=0;ic<nCol;ic++)
{
A.pData[ir][ic]-=temp1*A.pData[r][ic] ;
pData[ir][ic]-=temp1*pData[r][ic];
temp2=A.pData[ir][ic];
temp3=pData[ir][ic];
}
}
for (int ir=r-1;ir>=0;ir--)
{
temp1=A.pData[ir][r];
for (int ic=0;ic<nCol;ic++)
{
A.pData[ir][ic]-=temp1*A.pData[r][ic];
pData[ir][ic]-=temp1*pData[r][ic] ;
temp2=A.pData[ir][ic];
temp3=pData[ir][ic];
}
}
}
r++;
}
}
void Matrix::multiply (Matrix &A) // Multiply Two matrices
{