forked from D-F-D-P/ASUMathLabG09
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix.cpp
More file actions
829 lines (625 loc) · 15.6 KB
/
matrix.cpp
File metadata and controls
829 lines (625 loc) · 15.6 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
#include "matrix.h"
#include <iostream>
#include <cstdlib>
#include <math.h>
using namespace std;
/*attach your libraries here*/
/*
attach your function here and make sure
you use the inputs from the class variables
in case you needed to attach a new variable
inside the class feel free to add it in "matrix.h"
*/
//example function
//void matrix::doNull();
//default constructor
matrix::matrix()
{
elements =NULL;
rows = 0;
columns =0;
name = "";
}
//constructor
matrix::matrix(int rows, int columns)
{
this->rows = rows;
this->columns = columns;
if ((rows*columns) == 0) { elements = NULL; return; }
elements = new double*[rows];
for(int i=0;i<rows;i++)
{
elements[i] = new double[columns];
}
empty_matrix();
}
//copy constructor
matrix::matrix(const matrix& p)
{
this -> rows = p.rows;
this -> columns = p.columns;
if ((rows*columns) == 0) { elements = NULL; return; }
//create the matrix
elements = new double*[rows];
for(int i=0;i<rows;i++)
{
elements[i] = new double[columns];
}
//copy the values
for (int i = 0; i < rows; i++)
{
for (int j = 0 ; j < columns ; j++)
elements[i][j] = p.elements[i][j];
}
}
//gets
int matrix::get_rows()
{
return rows;
}
int matrix::get_columns()
{
return columns;
}
string matrix::get_name()
{
return name;
}
void matrix::set_name(string name)
{
this -> name = name;
}
//resize matrix
void matrix::resize_matrix(int rows, int columns)
{
if ((rows*columns) == 0) { rows = columns = 0; elements = NULL; return; }
double** newElements = new double*[rows];
for(int i=0;i<rows;i++){
newElements[i] = new double[columns];
if((i+1)<=this->rows)
{
for(int k=0; k<this->columns; k++)
{
if((k+1)<=this->columns)
{
newElements[i][k] = this->elements[i][k];
}
}
}
}
this->destroy_matrix();
this->rows = rows;
this->columns = columns;
this->elements = newElements;
}
//destructor
matrix::~matrix()
{
destroy_matrix();
}
//destroy matrix
void matrix::destroy_matrix()
{
if(elements)
{
for (int i = 0; i < rows; i++)
{
delete[] elements [i]; //deletes an inner array of integer;
}
delete[] elements; //delete pointer holding array of pointers;
elements =NULL;
rows = 0;
columns =0;
name = "";
}
}
//fill matrix from cin
void matrix::fill_matrix_cl()
{
cout << "Please fill the matrix with its elements" << endl;
for (int i = 0; i < rows; i++)
{
cout << "values in row: " << i+1 << endl;
for (int j = 0; j < columns; j++)
{
cin >> elements[i][j];
}
cout << endl;
}
}
void matrix::fill_matrix(string inputString)
{
string newString = space_trimer(inputString); // remove beginning spaces
// getting the name of the matrix
string name = newString.substr(0,1);
// to remove brackets
int bracketFinder = newString.find("[",0);
string newString2 = newString.substr(bracketFinder+1, newString.length()-bracketFinder-3);
int rows;
int columns;
rows = number_of(newString2.length(),newString2, ";") + 1;
// substring the string into row
int beginRow = 0 ; // position of a row starting
int endRow; // position of a row ending
string row ;
for(int i = 0 ; i < rows ; i ++)
{
endRow = newString2.find(";",beginRow);
row = newString2.substr(beginRow , endRow - beginRow);
string newRow = space_trimer(row); // to remove beginning spaces
beginRow = endRow+1; // update the begin of next find
// now to get the number of columns
if(i == 0)// to create the matrix once since i now can get number of columns
{
columns = number_of(newRow.length(),newRow," ") + 1;
this -> reset_matrix(rows,columns);
}
// substring the row into elements
int beginElement = 0;
int endElement;
string elementString;
float elementFloat;
for(int j = 0 ; j < columns ; j++)
{
endElement = newRow.find(" " ,beginElement);
elementString = newRow.substr(beginElement, endElement - beginElement);
beginElement = endElement +1;
elementFloat = strtof((elementString).c_str(),0); // string to float
// save the element to the matrix
this -> elements[i][j] = elementFloat;
}
}
this -> name = name;
//To print or not
if( inputString[inputString.length()-1] != ';' )
print_matrix();
}
//empty matrix
void matrix::empty_matrix()
{
for (int i = 0; i < rows; i++)
{
for (int j = 0 ; j < columns ; j++)
elements[i][j]=0;
}
}
void matrix::copy_matrix(matrix & p)
{
destroy_matrix();
this -> rows = p.rows;
this -> columns = p.columns;
if ((rows*columns) == 0) { elements = NULL; return; }
//create the matrix
elements = new double*[rows];
for(int i=0;i<rows;i++)
{
elements[i] = new double[columns];
}
//copy the values
for (int i = 0; i < rows; i++)
{
for (int j = 0 ; j < columns ; j++)
elements[i][j] = p.elements[i][j];
}
}
void matrix::reset_matrix(int rows, int columns)
{
destroy_matrix();
this -> rows = rows;
this -> columns = columns;
if ((rows*columns) == 0) { elements = NULL; return; }
this -> elements = new double*[rows];
for(int i=0;i<rows;i++)
{
this -> elements[i] = new double[columns];
}
empty_matrix();
}
void matrix::print_matrix()
{
if(this->elements == NULL) //to prevent crash
cout << "this matrix is not created" <<endl;
else
cout << this->name << " = " << endl;
for(int i=0;i<rows;i++){
for(int j=0;j<columns;j++){
cout<<elements[i][j]<<"\t";
}
cout<<endl;
}
}
// generate a sub matrix, it won't crash
matrix matrix::new_sub_matrix(int row, int column)
{
matrix *temp = new matrix((this->rows)-1,(this->columns)-1);
for(int i=0;i<(temp->rows);i++)
{
int flagRows = 0;
if(i >= row){
flagRows = 1;
}
for(int k=0;k<(temp->columns);k++)
{
int flagColumns = 0;
if(k >= column){
flagColumns = 1;
}
temp->elements[i][k] = this->elements[i+flagRows][k+flagColumns];
}
}
return *temp;
}
// measure the determinant of the matrix, it will crash if the number of rows != num of colums
double matrix::determinant()
{
double result = 0.0;
if(this->rows == 2)
{
result = ((this->elements[0][0])*(this->elements[1][1]))
- ((this->elements[0][1])*(this->elements[1][0]));
return result;
}
for(int i=0;i<(this->rows);i++)
{
matrix temp = this->new_sub_matrix(i,0);
result += (temp.determinant())*pow(-1,i)*this->elements[i][0];
}
return result;
}
//flips the rows and columns , it won't crash
void matrix::flip_matrix()
{
matrix temp(this->columns, this->rows);
for(int i=0;i< temp.rows;i++)
{
for(int k=0;k< temp.columns;k++)
{
temp.elements[i][k] = this->elements[k][i];
}
}
std::string name = this->name;
this->copy_matrix(temp);
this->name = name;
}
// divide matrix A over B , it won't crash if the rows != columns so make sure you operate with the right data
matrix matrix::inverse()
{
matrix *temp = new matrix;
temp->copy_matrix(*this);
double det = temp->determinant();
int flag = 1;
for (int i = 0; i < temp->columns; i++)
{
flag = pow(-1,i);
for (int k = 0; k < temp->rows; k++)
{
temp->elements[k][i] = (this->new_sub_matrix(k,i)).determinant() * flag;
flag *= -1;
}
}
temp->flip_matrix();
multiply_num(*temp, (1/det), *temp);
return *temp;
}
// divide number B over matrix A , it will crash if the number of rows != num of colums
void divide_num_over_matrix(matrix &A, double B , matrix &C)
{
matrix a = A.inverse();
multiply_num(a, B , C);
}
// divide matrix A over B , it will crash if the number of rows != num of colums or if the 2 matrix don't match
void divide_matrix(matrix &A, matrix &B , matrix &C)
{
matrix b = B.inverse();
multiply_matrix(A, b , C);
}
//sum of two matrix
void sum_matrix(matrix &A, matrix &B , matrix &C)
{
if (A.rows != B.rows || A.columns != B.columns)cout << "error sizing" << endl;
else {
matrix result(A.rows,A.columns);
C = result;
for (int i = 0; i < A.rows; i++)
{
for (int j = 0; j < A.columns; j++)
C.elements[i][j] = A.elements[i][j] + B.elements[i][j];
}
}
}
// sub of two matrix
void sub_matrix(matrix &A, matrix &B , matrix &C)
{
if (A.rows != B.rows || A.columns != B.columns)cout << "error sizing" << endl;
else {
matrix result(A.rows,A.columns);
C = result;
for (int i = 0; i < A.rows; i++)
{
for (int j = 0; j < A.columns; j++)
C.elements[i][j] = A.elements[i][j] - B.elements[i][j];
}
}
}
// matrix matrix:: operator - (matrix & p) // A - B = C
//{
//return sub_matrix(*this, p);
//}
//sum of matrix and number
void sum_num(matrix &A, double B, matrix &C)
{
matrix result(A.rows, A.columns);
C = result;
for (int i = 0; i < A.rows; i++)
{
for (int j = 0; j < A.columns; j++)
C.elements[i][j] = A.elements[i][j] + B;
}
}
//multiply of matrix and number
void multiply_num(matrix &A, double B, matrix &C)
{
matrix result(A.rows, A.columns);
for (int i = 0; i < A.rows; i++)
{
for (int j = 0; j < A.columns; j++)
result.elements[i][j] = A.elements[i][j] * B;
}
C = result;
}
//sub of matrix and number
void sub_num(matrix &A, double B, matrix &C)
{
matrix result(A.rows, A.columns);
C = result;
for (int i = 0; i < A.rows; i++)
{
for (int j = 0; j < A.columns; j++)
C.elements[i][j] = A.elements[i][j] - B;
}
}
void multiply_matrix(matrix &A, matrix &B , matrix &C)
{
if (A.columns!=B.rows)
cout << "error columns of first is not equal rows of the second" << endl;
//Or whatever the doctor says
else
{
matrix result(A.rows,B.columns);
C = result;
// Multiplying and store in r
for (int i=0;i<A.rows;i++){
for (int j=0;j<B.columns;j++){
for (int k=0;k<A.columns;k++){
C.elements[i][j] += A.elements[i][k]*B.elements[k][j];
}
}
}
}
}
// ************************ Operators ************************* //
//Copy
matrix matrix :: operator = (matrix p)
{
copy_matrix(p);
return *this; // this line calls the copy constructor
}
//sum of two matrix
matrix sum_matrix(matrix &A, matrix &B)
{
matrix result(A.rows, A.columns);
if (A.rows != B.rows && A.columns != B.columns)cout << "error sizing" << endl;
else {
for (int i = 0; i < A.rows; i++)
{
for (int j = 0; j < A.columns; j++)
result.elements[i][j] = A.elements[i][j] + B.elements[i][j];
}
}
return result;
}
// sum_matrix operator
matrix matrix:: operator + (matrix & p) // A + B = C
{
return sum_matrix(*this,p);
}
// sub of two matrix
matrix sub_matrix(matrix &A, matrix &B)
{
matrix result(A.rows, A.columns);
if (A.rows != B.rows && A.columns != B.columns)cout << "error sizing" << endl;
else {
for (int i = 0; i < A.rows; i++)
{
for (int j = 0; j < A.columns; j++)
result.elements[i][j] = A.elements[i][j] - B.elements[i][j];
}
}
return result;
}
matrix matrix:: operator - (matrix & p) // A - B = C
{
return sub_matrix(*this, p);
}
//sum of matrix and number
matrix sum_num(matrix &A, int B)
{
matrix result(A.rows, A.columns);
for (int i = 0; i < A.rows; i++)
{
for (int j = 0; j < A.columns; j++)
result.elements[i][j] = A.elements[i][j] + B;
}
return result;
}
// sum_num operator
matrix matrix:: operator + (int p) // A + B = C
{
return sum_num(*this, p);
}
matrix operator + (int a , matrix &p) // A + B = C
{
return sum_num(p,a);
}
//sub of matrix and number
matrix sub_num(matrix &A, int B)
{
matrix result(A.rows, A.columns);
for (int i = 0; i < A.rows; i++)
{
for (int j = 0; j < A.columns; j++)
result.elements[i][j] = A.elements[i][j] - B;
}
return result;
}
// sub_num operator
matrix matrix:: operator - (int p) // A - number = C
{
return sub_num(*this, p);
}
matrix operator - (int a, matrix &p) // number - A = C
{
return sub_num(p, a);
}
// sum_matrix
matrix matrix:: operator + (matrix p) // A + B = C
{
matrix result(this->rows,this -> columns);
sum_matrix((*this),p , result);
return result;
}
matrix matrix:: operator - (matrix p) // A + B = C
{
matrix result(this->rows,this -> columns);
sub_matrix((*this),p , result);
return result;
}
matrix matrix :: operator + (double p)// A + number = C
{
matrix result(this->rows,this -> columns);
sum_num((*this),p , result);
return result;
}
matrix matrix :: operator - (double p)// A + number = C
{
matrix result(this->rows,this -> columns);
sub_num((*this),p , result);
return result;
}
matrix operator + (double a, matrix p)
{
matrix result(p.rows, p.columns);
sum_num(p,a , result);
return result;
}
matrix operator - (double a, matrix p)
{
matrix result(p.rows, p.columns);
p=-p;
sum_num(p,a , result);
return result;
}
matrix operator - (matrix p)
{
matrix result(p.rows,p.columns);
multiply_num(p,-1,result);
return result;
}
matrix matrix :: operator * (matrix p) //C=A*B
{
matrix result(this -> rows , p.columns);
multiply_matrix((*this), p , result);
return result;
}
matrix matrix :: operator * (double p) //C=A*B
{
matrix result(this->rows , this-> columns);
multiply_num(*this,p,result);
return result;
}
matrix operator * (double a, matrix p) // A = double * A
{
matrix result(p.rows , p.columns);
multiply_num(p,a,result);
return result;
}
//C=A/B it will crash if the number of rows != num of colums or if the 2 matrix don't match
matrix matrix :: operator / (matrix p)
{
matrix result(this -> rows , p.columns);
divide_matrix((*this), p , result);
return result;
}
matrix matrix :: operator / (double p) //C=A/p
{
matrix result(this->rows , this-> columns);
multiply_num(*this,(1/p),result);
return result;
}
//it will crash if the number of rows != num of colums
matrix operator / (double a, matrix p) // C = a / p
{
matrix result(p.rows , p.columns);
divide_num_over_matrix(p,a,result);
return result;
}
// Global Functions
int number_of(int e, string s,string c)
{
int N=0;
for (int i=0;i<=e;i++)
{
if(s.substr(i,1)==c)
N++;
}
return N;
}
string space_trimer(string text)
{
int spaceCounter = 0;
while(1)
{
if(text.substr(spaceCounter,1) == " ")
{
spaceCounter ++;
}
else break;
}
return text.substr(spaceCounter,text.length() - spaceCounter);
}
/*matrix matrix :: sum_matrix(matrix &A)
{
matrix result(this->rows , this -> columns);
if (A.rows != this ->rows || A.columns != this ->columns)cout << "error sizing" << endl;
else {
for (int i = 0; i < A.rows; i++)
{
for (int j = 0; j < A.columns; j++)
result.elements[i][j] = this ->elements[i][j] + A.elements[i][j];
}
}
return result;
}
void matrix:: operator += (matrix& A)
{
sum_matrix(A);
}
// sub_num operator
matrix matrix:: operator - (int p) // A - number = C
{
return sub_num(*this, p);
}
matrix operator - (int a, matrix &p) // number - A = C
{
return sub_num(p, a);
}
// sum_num operator
matrix matrix:: operator + (int p) // A + B = C
{
return sum_num(*this, p);
}
matrix operator + (int a , matrix &p) // A + B = C
{
return sum_num(p,a);
}
*/