-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix.cpp
2040 lines (1901 loc) · 61.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
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 "matrix.h"
matrix::matrix()
{
for (int i = 0; i < Size_r; i++)
mat[i] = new complex[Size_c]; //each pointer points to an array of complexes
//all complex values are intialized with zeros and there can not be an empty matrix
rows = 1;
columns = 1;
}
matrix::matrix(int row_count, int column_count) //we better not make (new complex[column_count]) cause if we try to equalize this matrix with a bigger one latter some values will be outside this one
{
for (int i = 0; i < Size_r; i++)
mat[i] = new complex[Size_c]; //each pointer points to an array of complexes
//all complex values are intialized with zeros and there can not be an empty matrix
rows = row_count;
columns = column_count;
}
void matrix::set_dimentions(int row, int column)
{
rows = row;
columns = column;
}
void matrix::set_element(int row, int column, complex value)
{
mat[row][column] = value;
}
void matrix::set_a_row(int position, matrix row) //position starts from 0
{
if (row.columns != 1)
return;
for (int n = 0; n < columns; n++) //if the row is shorter than the matrix it leaves the rest as it is
{
if (n >= row.columns)
break;
mat[position][n] = row.mat[0][n];
}
}
void matrix::set_a_column(int position, matrix column) //position starts from 0
{
if (column.columns != 1)
return;
for (int n = 0; n < rows; n++) //if the column is shorter than the matrix it leaves the rest as it is
{
if (n >= column.rows)
break;
mat[n][position] = column.mat[n][0];
}
}
void matrix::set_a_row(int position, vector_m row) //position starts from 0
{
for (int n = 0; n < columns; n++) //if the row is shorter than the matrix it leaves the rest as it is
{
if (n >= row.get_count())
break;
mat[position][n] = row.get_element(n);
}
}
void matrix::set_a_column(int position, vector_m column) //position starts from 0
{
for (int n = 0; n < rows; n++) //if the column is shorter than the matrix it leaves the rest as it is
{
if (n >= column.get_count())
break;
mat[n][position] = column.get_element(n);
}
}
void matrix::get_a_row(int position, matrix full) //position starts from 0
{
for (int n = 0; n < full.columns; n++)
mat[0][n] = full.mat[position][n];
columns = full.columns;
rows = 1;
}
void matrix::get_a_column(int position, matrix full) //position starts from 0
{
for (int n = 0; n < full.rows; n++)
mat[n][0] = full.mat[n][position];
columns = 1;
rows = full.rows;
}
vector_m matrix::get_a_row(int position)const //position starts from 0
{
vector_m row(columns);
for (int n = 0; n < columns; n++)
row.set_element(n, mat[position][n]);
return row;
}
vector_m matrix::get_a_column(int position)const //position starts from 0
{
vector_m column(rows);
for (int n = 0; n < rows; n++)
column.set_element(n, mat[n][position]);
return column;
}
void matrix::swap_rows(int from, int to)
{
vector_m first, second;
first = (*this).get_a_row(from);
second = (*this).get_a_row(to);
(*this).set_a_row(to, first);
(*this).set_a_row(from, second);
}
void matrix::swap_columns(int from, int to)
{
vector_m first, second;
first = (*this).get_a_column(from);
second = (*this).get_a_column(to);
(*this).set_a_column(to, first);
(*this).set_a_column(from, second);
}
void matrix::set_rows(int row)
{
rows = row;
}
void matrix::set_columns(int column)
{
columns = column;
}
int matrix::get_rows(void)const
{
return rows;
}
int matrix::get_columns(void)const
{
return columns;
}
complex matrix::get_element(int row, int column)const
{
return mat[row][column];
}
void matrix::print(void)const
{
cout << '[';
for (int m = 0; m < rows; m++)
{
for (int n = 0; n < columns; n++)
{
mat[m][n].print();
if (m != (rows - 1))
{
if (n == (columns - 1))
cout << ";";
else
cout << " ";
}
else
{
if (n != (columns - 1))
cout << " ";
}
}
}
cout << ']';
}
void matrix::intialize(void)
{
for (int row = 0; row < rows; row++) //first input rows
for (int column = 0; column < columns; column++) //second input columns
mat[row][column] = complex(0, 0);
}
void matrix::operator=(matrix var)
{
for (int m = 0; m < var.rows; m++)
{
for (int n = 0; n < var.columns; n++)
{
mat[m][n] = var.mat[m][n];
}
}
rows = var.rows;
columns = var.columns;
}
void matrix::operator+=(matrix var)
{
(*this) = (*this) + var;
}
void matrix::operator-=(matrix var)
{
(*this) = (*this) - var;
}
void matrix::operator*=(matrix var)
{
(*this) = (*this) * var;
}
void matrix::operator/=(matrix var)
{
(*this) = (*this) / var;
}
bool matrix::operator==(matrix var)const
{
bool state = true;
for (int m = 0; m < var.rows; m++)
for (int n = 0; n < var.columns; n++)
if (mat[m][n] != var.mat[m][n])
state = false;
if (rows != var.rows || columns != var.columns)
state = false;
return state;
}
bool matrix::operator!=(matrix var)const
{
bool state = false;
for (int m = 0; m < var.rows; m++)
for (int n = 0; n < var.columns; n++)
if (mat[m][n] != var.mat[m][n])
state = true;
if (rows != var.rows || columns != var.columns)
state = true;
return state;
}
matrix matrix::operator+(matrix var)const
{
if ((rows != var.rows) || (columns != var.columns))
{
var.set_element(0, 0, complex(nanf(0), nanf(0)));
// the two matrixes don't have the same dimentions
return var;
}
else
{
matrix temp(rows, columns);
for (int row = 0; row < rows; row++)
for (int column = 0; column < columns; column++)
temp.mat[row][column] = mat[row][column] + var.mat[row][column];
return temp;
}
}
matrix matrix::operator-(matrix var)const
{
if ((rows != var.rows) || (columns != var.columns))
{
var.set_element(0, 0, complex(nanf(0), nanf(0)));
// the two matrixes don't have the same dimentions
return var;
}
else
{
matrix temp(rows, columns);
for (int row = 0; row < rows; row++)
for (int column = 0; column < columns; column++)
temp.mat[row][column] = mat[row][column] - var.mat[row][column];
return temp;
}
}
matrix matrix::operator*(matrix var)const
{
if (get_rows() == 1 && get_columns() == 1) //if it's a scalar
{
for (int row = 0; row < var.rows; row++) //first input rows
for (int column = 0; column < var.columns; column++) //second input columns
var.mat[row][column] = mat[0][0] * var.mat[row][column];
return var;
}
else if (var.get_rows() == 1 && var.get_columns() == 1) //if it's a scalar
{
complex factor = var.mat[0][0]; //this method to keep the function const
for (int row = 0; row < rows; row++) //first input rows
for (int column = 0; column < columns; column++) //second input columns
var.mat[row][column] = mat[row][column] * factor;
var.set_dimentions(rows, columns);
return var;
}
else if (columns != var.rows) //m*n,k*j ---> n=k , out [m,j]
{
var.set_element(0, 0, complex(nanf(0), nanf(0)));
// the two matrixes don't have the same dimentions
return var;
}
else
{ //no need for intialization with zeros
matrix temp(rows, var.columns);
for (short row = 0; row < rows; row++) //first input rows
for (short column = 0; column < var.columns; column++) //second input columns
for (short x = 0; x < columns; x++)
temp.mat[row][column] += (mat[row][x] * var.mat[x][column]);
return temp;
}
}
matrix matrix::operator/(matrix var)const
{
if (var.get_rows() == 1 && var.get_columns() == 1) //if it's a scalar
{
complex factor = var.mat[0][0];
for (int row = 0; row < rows; row++) //first input rows
for (int column = 0; column < columns; column++) //second input columns
var.mat[row][column] = mat[row][column] / factor;
var.set_dimentions(rows, columns);
return var;
}
//we'll give the imaginaries to this function just to use in the multiply function as matrixes full of zeros instead of defining and intializing 3 new matrixes
else if (columns != var.rows || var.columns != var.rows) //m*n,k*j ---> n=k , out [m,j]
{
for (int m = 0; m < var.rows; m++)
for (int n = 0; n < var.columns; n++)
var.mat[m][n] = complex(nan(""), nan(""));
return var;
}
else
return (*this) * var.m_inverse();
}
matrix matrix::operator^(unsigned int power)const
{
if (columns != rows) //m*n,k*j ---> n=k , out [m,j]
{
matrix var(rows, columns);
for (int m = 0; m < var.rows; m++)
for (int n = 0; n < var.columns; n++)
var.mat[m][n] = complex(nan(""), nan(""));
return var;
}
else
{
matrix var(rows, columns);
if (power == 0) //identity Matrix
{
for (int m = 0; m < var.rows; m++)
{
for (int n = 0; n < var.columns; n++)
{
var.mat[m][n] = complex(0, 0);
}
var.mat[m][m] = complex(1, 0);
}
return var;
}
else if (power == 1)
return (*this);
else
{
matrix var(rows, columns);
//matrix TempCopy;
var = (*this);
for (int Power = 0; Power < (power - 1); Power++) //Power Loop
var = (*this)* var; //using *= causes stack overflow
return var;
}
}
}
void matrix::deletem(void)
{
for (int i = 0; i < Size_r; i++)
delete[]mat[i];
delete[]mat;
}
matrix matrix::m_inverse()const //shorened version (less variables for use with division) //equivalent for the operator of inverse
{
matrix bigMat(rows, columns * 2);
if (rows != columns) //not square
{
bigMat.set_dimentions(rows, columns);
for (int m = 0; m < rows; m++)
for (int n = 0; n < columns; n++)
bigMat.mat[m][n] = complex(nan(""), nan(""));
return bigMat;
}
else
{
//first putting the matrix next to I
for (int m = 0; m < rows; m++)
{
for (int n = 0; n < columns * 2; n++)
{
if (n >= columns)
{
bigMat.mat[m][n] = complex(0, 0);
}
else
bigMat.mat[m][n] = mat[m][n];
}
bigMat.mat[m][m + columns] = complex(1, 0);
}
//getting zeros under the diagonal
vector_m temp1;
complex one(1, 0), minus_one(-1, 0), diag_element;
for (int diag = 0; (diag < rows - 1) && (diag < columns); diag++) //chooses the element of the diagonal that we're eliminating under
{
for (int u_diag = diag + 1; u_diag < rows; u_diag++) //under each diagonal element we'll eliminate
{
if (bigMat.mat[u_diag][diag] == complex(0, 0))
continue;
temp1 = bigMat.get_a_row(u_diag);
if (bigMat.mat[diag][diag] == one)
bigMat.set_a_row(u_diag, temp1 + (bigMat.get_a_row(diag)*(bigMat.mat[u_diag][diag] * minus_one)));
else if (bigMat.mat[diag][diag] == minus_one)
bigMat.set_a_row(u_diag, temp1 + (bigMat.get_a_row(diag)*bigMat.mat[u_diag][diag]));
else if (bigMat.mat[diag][diag] == complex(0, 0))
{
for (int trans = u_diag; trans < bigMat.rows; trans++)
{
if (bigMat.mat[trans][diag] != complex(0, 0))
{
bigMat.swap_rows(diag, trans);
bigMat.set_a_row(trans, bigMat.get_a_row(trans)*minus_one);
u_diag--;
break;
}
}
}
else
bigMat.set_a_row(u_diag, temp1 + (bigMat.get_a_row(diag)*(one / bigMat.mat[diag][diag])*(bigMat.mat[u_diag][diag] * minus_one)));
}
}
//check for zero rows
for (int m = 0; m < rows; m++) //not invertible
{
if (bigMat.get_a_row(m).is_zero_vec_inverseSpecial(columns))
{
bigMat.set_dimentions(rows, columns);
for (int m = 0; m < rows; m++)
for (int n = 0; n < columns; n++)
bigMat.mat[m][n] = complex(nan(""), nan(""));
return bigMat;
}
}
//making the diagonal of our matrix = ones
for (int m = 0; m < bigMat.rows; m++)
bigMat.set_a_row(m, bigMat.get_a_row(m)*(one / bigMat.mat[m][m]));
//eliminating above the diagoanl
for (int diag = rows - 1; diag >= 0; diag--) //chooses the element of the diagonal that we're eliminating under
{
for (int u_diag = diag - 1; u_diag >= 0; u_diag--) //under each diagonal element we'll eliminate
{
if (bigMat.mat[u_diag][diag] == complex(0, 0))
continue;
temp1 = bigMat.get_a_row(u_diag);
bigMat.set_a_row(u_diag, temp1 + (bigMat.get_a_row(diag)*(bigMat.mat[u_diag][diag] * minus_one)));
}
}
//returing the inverse
for (int m = 0; m < rows; m++)
for (int n = 0; n < columns; n++)
bigMat.mat[m][n] = bigMat.mat[m][n + columns];
bigMat.set_dimentions(rows, columns); //use bigMat again to be returned (to avoid declaring new matrix)
}
return bigMat;
}
matrix matrix::m_transpose()const
{
matrix temp(columns, rows);
for (int m = 0; m < rows; m++)
for (int n = 0; n < columns; n++)
temp.mat[n][m] = mat[m][n];
return temp;
}
matrix matrix::m_eliminate()const
{
vector_m temp1;
complex one(1, 0), minus_one(-1, 0), diag_element;
matrix var = (*this);
for (int diag = 0; (diag < var.rows - 1) && (diag < var.columns); diag++) //chooses the element of the diagonal that we're eliminating under
{
for (int u_diag = diag + 1; u_diag < var.rows; u_diag++) //under each diagonal element we'll eliminate
{
if (var.mat[u_diag][diag] == complex(0, 0))
continue;
temp1 = var.get_a_row(u_diag);
if (var.mat[diag][diag] == complex(1, 0))
var.set_a_row(u_diag, temp1 + (var.get_a_row(diag)*(var.mat[u_diag][diag] * minus_one)));
else if (var.mat[diag][diag] == complex(-1, 0))
var.set_a_row(u_diag, temp1 + (var.get_a_row(diag)*var.mat[u_diag][diag]));
else if (var.mat[diag][diag] == complex(0, 0))
{
for (int trans = u_diag; trans < var.rows; trans++)
{
if (var.mat[trans][diag] != complex(0, 0))
{
var.swap_rows(diag, trans);
var.set_a_row(trans, var.get_a_row(trans)*minus_one);
u_diag--;
break;
}
}
}
else
var.set_a_row(u_diag, temp1 + (var.get_a_row(diag)*(one / (var.mat[diag][diag])*(var.mat[u_diag][diag] * minus_one))));
}
}
return var;
}
int matrix::add(matrix first, matrix second)
{
if ((first.rows != second.rows) || (first.columns != second.columns))
{
cout << "Error!\nImproper Dimentions";// the two matrixes don't have the same dimentions
return 1;
}
else
{
for (int row = 0; row < first.rows; row++)
for (int column = 0; column < first.columns; column++)
mat[row][column] = first.mat[row][column] + second.mat[row][column];
}
rows = first.rows; //this
columns = first.columns;
return 0;
}
int matrix::subtract(matrix first, matrix second)
{
if ((first.rows != second.rows) || (first.columns != second.columns))
{
cout << "Error!\nImproper Dimentions";// the two matrixes don't have the same dimentions
return 1;
}
else
for (int row = 0; row < first.rows; row++)
for (int column = 0; column < first.columns; column++)
mat[row][column] = first.mat[row][column] - second.mat[row][column];
rows = first.rows;
columns = first.columns;
return 0;
}
int matrix::multiply(matrix first, matrix second)
{
if (first.get_rows() == 1 && first.get_columns() == 1) //if it's a scalar
{
for (int row = 0; row < second.rows; row++) //first input rows
for (int column = 0; column < second.columns; column++) //second input columns
second.mat[row][column] = first.mat[0][0] * second.mat[row][column];
(*this) = second;
return 0;
}
else if (second.get_rows() == 1 && second.get_columns() == 1) //if it's a scalar
{
for (int row = 0; row < first.rows; row++) //first input rows
for (int column = 0; column < first.columns; column++) //second input columns
first.mat[row][column] = first.mat[row][column] * second.mat[0][0];
(*this) = first;
return 0;
}
else if (first.columns != second.rows) //m*n,k*j ---> n=k , out [m,j]
{
cout << "Error!\nImproper Dimentions";
return 1;// the two matrixes don't have proper dimentions
}
else
{ // intialization with zeros
for (int row = 0; row < first.rows; row++) //first input rows
for (int column = 0; column < second.columns; column++) //second input columns
mat[row][column] = complex(0, 0);
for (int row = 0; row < first.rows; row++) //first input rows
for (int column = 0; column < second.columns; column++) //second input columns
for (int x = 0; x < first.columns; x++)
mat[row][column] += (first.mat[row][x] * second.mat[x][column]);
}
rows = first.rows;
columns = second.columns;
return 0;
}
int matrix::transpose(matrix var)
{
for (int m = 0; m < var.rows; m++)
for (int n = 0; n < var.columns; n++)
mat[n][m] = var.mat[m][n];
rows = var.columns;
columns = var.rows;
return 0;
}
int matrix::power(matrix var, string pow1)
{
for (int i = 0; i < pow1.size(); i++)
{
switch (pow1[i])
{
case'0':
case'1':
case'2':
case'3':
case'4':
case'5':
case'6':
case'7':
case'8':
case'9':
break;
default:
cout << "Error!\nThe Power Is Not a Positive Intger"; //power is not an integer Or is not +ve
return 1;
}
}
float pow = atof(pow1.c_str());
if (var.columns != var.rows) //m*n,k*j ---> n=k , out [m,j]
{
cout << "Error!\nImproper Dimentions";
return 1;// not a square matrix
}
else
{
if (pow == 0) //identity Matrix
{
for (int m = 0; m < var.rows; m++)
{
for (int n = 0; n < var.columns; n++)
{
mat[m][n] = complex(0, 0);
}
mat[m][m] = complex(1, 0);
}
rows = var.rows;
columns = var.columns;
}
else if (pow == 1)
for (int m = 0; m < var.rows; m++)
for (int n = 0; n < var.columns; n++)
(*this) = var;
else
{
//matrix TempCopy;
(*this) = var;
for (int Power = 0; Power < (pow - 1); Power++) //Power Loop
(*this) = (*this)* var; //using *= causes stack overflow
}
}
rows = var.rows;
columns = var.columns;
return 0;
}
complex matrix::determinant(bool &state)const //still there's a bug when trying to s swap
{
state = 0;
if (columns != rows)
{
cout << "Error!\nNot a Square matrix!"; // Not a square matrix
state = 1;
return mat[0][0]; //null value won't be used
}
else
{
matrix temp = (*this);
vector_m temp1;
complex one(1, 0), minus_one(-1, 0), diag_element;
for (int diag = 0; diag < temp.rows - 1; diag++) //chooses the element of the diagonal that we're eliminating under
{
for (int u_diag = diag + 1; u_diag < temp.rows; u_diag++) //under each diagonal element we'll eliminate
{
if (temp.mat[u_diag][diag] == complex(0, 0))
continue;
temp1 = temp.get_a_row(u_diag);
if (temp.mat[diag][diag] == one)
temp.set_a_row(u_diag, temp1 + (temp.get_a_row(diag)*(temp.mat[u_diag][diag] * minus_one)));
else if (temp.mat[diag][diag] == minus_one)
temp.set_a_row(u_diag, temp1 + (temp.get_a_row(diag)*temp.mat[u_diag][diag]));
else if (temp.mat[diag][diag] == complex(0, 0))
{
for (int trans = u_diag; trans < temp.rows; trans++)
{
if (temp.mat[trans][diag] != complex(0, 0))
{
temp.swap_rows(diag, trans);
temp.set_a_row(trans, temp.get_a_row(trans)*minus_one);
u_diag--;
break;
}
else
{
if (trans == temp.rows - 1)
{
diag_element = complex(0, 0);
return diag_element;
}
}
}
}
else
temp.set_a_row(u_diag, temp1 + (temp.get_a_row(diag)*(one / temp.mat[diag][diag])*(temp.mat[u_diag][diag] * (minus_one))));
}
}
return temp.multiply_diagonal_elements();
}
}
complex matrix::determinant()const //still there's a bug when trying to s swap //no cout but returns nan (can't substitute the private one)
{
if (columns != rows)
// Not a square matrix
return complex(nanf(""), nanf("")); //null value won't be used
else
{
matrix temp = (*this);
vector_m temp1;
complex one(1, 0), minus_one(-1, 0), diag_element;
for (int diag = 0; diag < temp.rows - 1; diag++) //chooses the element of the diagonal that we're eliminating under
{
for (int u_diag = diag + 1; u_diag < temp.rows; u_diag++) //under each diagonal element we'll eliminate
{
if (temp.mat[u_diag][diag] == complex(0, 0))
continue;
temp1 = temp.get_a_row(u_diag);
if (temp.mat[diag][diag] == one)
temp.set_a_row(u_diag, temp1 + (temp.get_a_row(diag)*(temp.mat[u_diag][diag] * minus_one)));
else if (temp.mat[diag][diag] == minus_one)
temp.set_a_row(u_diag, temp1 + (temp.get_a_row(diag)*temp.mat[u_diag][diag]));
else if (temp.mat[diag][diag] == complex(0, 0))
{
for (int trans = u_diag; trans < temp.rows; trans++)
{
if (temp.mat[trans][diag] != complex(0, 0))
{
temp.swap_rows(diag, trans);
temp.set_a_row(trans, temp.get_a_row(trans)*minus_one);
u_diag--;
break;
}
else
{
if (trans == temp.rows - 1)
{
diag_element = complex(0, 0);
return diag_element;
}
}
}
}
else
temp.set_a_row(u_diag, temp1 + (temp.get_a_row(diag)*(one / temp.mat[diag][diag])*(temp.mat[u_diag][diag] * (minus_one))));
}
}
return temp.multiply_diagonal_elements();
}
}
vector_m matrix::cramer(matrix ans_column, bool &state)
{
state = false;
if (ans_column.rows != rows)
{
state = true;
cout << "Error!\nAnswer-Column Does Not Meet no. Of Equations";
return ans_column.get_a_column(0);
}
else if (rows < columns)
{
state = true;
cout << "Error!\nNot Enough Equations";
return ans_column.get_a_column(0);
}
else if (rows > columns)
{
state = true;
cout << "Error!\nRemove " << rows - columns << " Of The Equations";
return ans_column.get_a_column(0);
}
else
{
complex delta, delta_i;
delta = determinant();
if (delta == complex(0, 0))
{
state = true;
cout << "Error!\nCoefficient Matrix Equals Zero";
return ans_column.get_a_column(0);
}
vector_m unknowns(columns), temp_column;
for (int i = 0; i < rows; i++)
{
temp_column = get_a_column(i);
set_a_column(i, ans_column);
delta_i = determinant();
unknowns.set_element(i, delta_i / delta);
set_a_column(i, temp_column);
}
return unknowns;
}
}
vector_m matrix::cramer(matrix ans_column)
{
vector_m ans;
ans = ans_column.get_a_column(0);
if (ans_column.rows != rows || rows != columns)
{
ans.set_element(0, complex(nanf(""), nanf("")));
return ans;
}
else
{
complex delta, delta_i;
delta = determinant();
if (delta == complex(0, 0))
{
ans.set_element(0, complex(nanf(""), nanf("")));
return ans;
}
vector_m unknowns(rows), temp_column;
for (int i = 0; i < rows; i++)
{
temp_column = get_a_column(i);
set_a_column(i, ans_column);
delta_i = determinant();
unknowns.set_element(i, delta_i / delta);
set_a_column(i, temp_column);
}
return unknowns;
}
}
int matrix::eliminate(matrix var)
{ //change elimination to be the method of inversing [1 0 0;0 1 0;0 0 1]
vector_m temp1;
complex one(1, 0), minus_one(-1, 0), diag_element;
for (int diag = 0; (diag < var.rows - 1) && (diag < var.columns); diag++) //chooses the element of the diagonal that we're eliminating under
{
for (int u_diag = diag + 1; u_diag < var.rows; u_diag++) //under each diagonal element we'll eliminate
{
if (var.mat[u_diag][diag] == complex(0, 0))
continue;
temp1 = var.get_a_row(u_diag);
if (var.mat[diag][diag] == complex(1, 0))
var.set_a_row(u_diag, temp1 + (var.get_a_row(diag)*(var.mat[u_diag][diag] * minus_one)));
else if (var.mat[diag][diag] == complex(-1, 0))
var.set_a_row(u_diag, temp1 + (var.get_a_row(diag)*var.mat[u_diag][diag]));
else if (var.mat[diag][diag] == complex(0, 0))
{
for (int trans = u_diag; trans < var.rows; trans++)
{
if (var.mat[trans][diag] != complex(0, 0))
{
var.swap_rows(diag, trans);
var.set_a_row(trans, var.get_a_row(trans)*minus_one);
u_diag--;
break;
}
}
}
else
var.set_a_row(u_diag, temp1 + (var.get_a_row(diag)*(one / (var.mat[diag][diag])*(var.mat[u_diag][diag] * minus_one))));
}
}
(*this) = var;
return 0;
}
int matrix::eliminate_d(matrix var)
{
cout << endl;
vector_m temp1;
complex one(1, 0), minus_one(-1, 0), diag_element, factor0;
for (int diag = 0, step = 1; (diag < var.rows - 1) && (diag < var.columns); diag++) //chooses the element of the diagonal that we're eliminating under
{
for (int u_diag = diag + 1; u_diag < var.rows; u_diag++, step++) //under each diagonal element we'll eliminate
{
if (var.mat[u_diag][diag] == complex(0, 0))
{
step--;
continue;
}
cout << "Step " << step << ":" << endl;
temp1 = var.get_a_row(u_diag);
if (!(var.mat[diag][diag] == complex(0, 0)))
factor0 = (one / var.mat[diag][diag])*(var.mat[u_diag][diag] * minus_one);
if (var.mat[diag][diag] == one)
var.set_a_row(u_diag, temp1 + (var.get_a_row(diag)*(var.mat[u_diag][diag] * minus_one)));
else if (var.mat[diag][diag] == minus_one)
var.set_a_row(u_diag, temp1 + (var.get_a_row(diag)*var.mat[u_diag][diag]));
else if (var.mat[diag][diag] == complex(0, 0))
{
for (int trans = u_diag; trans < var.rows; trans++)
{
if (var.mat[trans][diag] != complex(0, 0))
{
var.swap_rows(diag, trans);
var.set_a_row(trans, var.get_a_row(trans)*minus_one);
cout << "Swap Row " << diag + 1 << " With Row " << trans + 1 << " , Then Row " << trans + 1 << " * -1" << endl;
var.print();
cout << endl << endl;
u_diag--;
break;
}
}
continue;
}
else
var.set_a_row(u_diag, temp1 + (var.get_a_row(diag)*(one / var.mat[diag][diag])*(var.mat[u_diag][diag] * minus_one)));
cout << "Row " << diag + 1 << " * (";
factor0.print();
cout << ") + Row " << u_diag + 1 << endl;
var.print();
cout << endl << endl;
}
}
((*this) = var);
return 0;
}
int matrix::rank(void)
{
vector_m temp1;
complex one(1, 0), minus_one(-1, 0), diag_element;
int zeroVectors;
for (int diag = 0; (diag < rows - 1) && (diag < columns); diag++) //chooses the element of the diagonal that we're eliminating under
{
for (int u_diag = diag + 1; u_diag < rows; u_diag++) //under each diagonal element we'll eliminate
{
if (mat[u_diag][diag] == complex(0, 0))
continue;
temp1 = get_a_row(u_diag);
if (mat[diag][diag] == one)
set_a_row(u_diag, temp1 + (get_a_row(diag)*mat[u_diag][diag] * minus_one));
else if (mat[diag][diag] == minus_one)
set_a_row(u_diag, temp1 + (get_a_row(diag)*mat[u_diag][diag]));
else if (mat[diag][diag] == complex(0, 0))
{
for (int trans = u_diag; trans < rows; trans++)
{
if (mat[trans][diag] != complex(0, 0)) //swap with the first row that does not have zero in the same place
{
swap_rows(diag, trans);
set_a_row(trans, get_a_row(trans)*minus_one);
u_diag--;
break;
}
}
}
else
set_a_row(u_diag, temp1 + (get_a_row(diag)*(one / mat[diag][diag])*(mat[u_diag][diag] * minus_one)));
}
}
return rows - (*this).no_of_zero_vectors();
}
int matrix::rank_d(void)
{
cout << endl;
vector_m temp1;
complex one(1, 0), minus_one(-1, 0), diag_element, factor0;
for (int diag = 0, step = 1; (diag < rows - 1) && (diag < columns); diag++) //chooses the element of the diagonal that we're eliminating under
{
for (int u_diag = diag + 1; u_diag < rows; u_diag++, step++) //under each diagonal element we'll eliminate
{
if (mat[u_diag][diag] == complex(0, 0))
{
step--;
continue;
}
cout << "Step " << step << ":" << endl;
temp1 = get_a_row(u_diag);
if (mat[diag][diag] != complex(0, 0))
factor0 = (one / mat[diag][diag])*(mat[u_diag][diag] * minus_one);
if (mat[diag][diag] == one)
set_a_row(u_diag, temp1 + (get_a_row(diag)*(mat[u_diag][diag] * minus_one)));
else if (mat[diag][diag] == minus_one)
set_a_row(u_diag, temp1 + (get_a_row(diag)*mat[u_diag][diag]));
else if (mat[diag][diag] == complex(0, 0))
{