-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix_algebra.cpp
More file actions
2894 lines (2774 loc) · 98.7 KB
/
matrix_algebra.cpp
File metadata and controls
2894 lines (2774 loc) · 98.7 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 "matrix_algebra.h"
//this assigns the matrix_type variable and sets the at_ptr ,at_ptr_c
template<typename DataType>
void matrix<DataType>::set_feature(int matrix_t){
if(matrix_t>=general&&matrix_t<=orthonormal){
matrix_type= matrix_t;
set_at_ptr(matrix_t) ;
set_start_end_ptr(matrix_t);
}
}
//helper function for copying
template<typename DataType>
void copy_vec(DataType*& dest, const DataType* src, int size) {
if(src) {
if(!dest) {
dest = get_vec<DataType>(size, 1);
}
else {
delete[] dest;
dest = get_vec<DataType>(size, 1);
}
for(int i = 0; i < size; i++) {
dest[i] = src[i];
}
}
}
//helper function for filling vectors like pindex and row_start
template<typename DataType>
void fill_vec(DataType*vec,int size, DataType val){
if(vec){
for(int i = 0 ; i<size;i++){
vec[i]=val;
}
}
}
template<typename DataType>
DataType*get_vec(int r ,int c){
//check for passed parameters
if(r>0&&c>0){
//memory allocation
DataType*ret_vec =new DataType[r*c];
return ret_vec ;
}
return NULL ;
}
template<typename DataType>
matrix<DataType>::matrix(){
rows = 0;
cols = 0 ;
actual_size =0;
vec= NULL ;
row_start= NULL ;
pindex=NULL ;
empty_vec[0]=0;
is_compressed = false;
compression_type = general_compress ;
set_feature(general);
}
//initialize a matrix with one value compressed or not
//by default its not compressed
template<typename DataType>
matrix<DataType>::matrix(int r,int c,DataType value,bool compressed){
rows= r;
cols= c;
pindex=NULL ;
row_start=NULL ;
empty_vec[0] = 0;
if(compressed){
//first dimensions
vec = get_vec<DataType>(1,1) ;
actual_size= 1;
if(value!=DataType(no_init)){
vec[0] =value;
}
compression_type = constant_compress;
is_compressed=true;
set_feature(constant) ;
}
else{
is_compressed =false;
compression_type = general_compress;
set_feature(general) ;
vec=get_vec<DataType>(r,c) ;
actual_size = r*c;
if(value!=DataType(no_init)){
for(int i =0 ;i<rows; i++){
for(int j= 0 ;j<cols ;j++){
at(i,j) = value;
}
}
}
}
}
template <typename DataType>
matrix<DataType>::~matrix(){
if(vec){
delete[]vec ;
vec = NULL;
}
if(row_start){
delete[]row_start ;
row_start=NULL;
}
if(pindex){
delete[]pindex;
pindex=NULL;
}
}
template <typename DataType>
matrix<DataType>::matrix(const matrix&mat){
//setting matrix features
rows= mat.get_rows();
cols= mat.get_cols();
row_start=NULL ;
pindex=NULL ;
vec=NULL;
is_compressed = mat.is_compressed ;
compression_type = mat.compression_type ;
set_feature(mat.matrix_type);
actual_size= mat.actual_size ;
empty_vec[0] =mat.empty_vec[0];
if(mat.matrix_type ==utri||mat.matrix_type==ltri){
copy_vec(row_start, mat.row_start, rows);
copy_vec(pindex, mat.pindex, rows);
}
//memory allocation and copy
vec = get_vec<DataType>(mat.actual_size,1) ;
for(int i =0 ; i <rows; i++){
for(int j = 0; j<cols; j++){
at(i,j) = mat.at(i,j);
}
}
}
template <typename DataType>
matrix<DataType>::matrix(int r,int c , DataType*arr,int size){
if(r>0&&c>0&&size<=r*c){
rows= r;
cols= c;
vec=get_vec<DataType>(r,c) ;
row_start=NULL ;
pindex=NULL ;
actual_size= r*c;
is_compressed=false;
compression_type = general_compress;
set_feature(general) ;
empty_vec[0] = 0;
for(int i =0 ;i<rows; i++){
for(int j= 0 ;j<cols ;j++){
if((i*c+j)<size){
at(i,j) = arr[i*c+j];
}
//fill rest of elements with zeroes
else{
at(i,j) = 0;
}
}
}
}
}
template <typename DataType>
int matrix<DataType>::get_rows()const{
return rows ;
}
template <typename DataType>
int matrix<DataType>::get_cols()const{
return cols ;
}
template <typename DataType>
int matrix<DataType>::get_size()const{
return actual_size ;
}
template <typename DataType>
int matrix<DataType>::get_type()const{
return matrix_type ;
}
//compression type of matrix must match matrix_t
//if not then the matrix is decompressed and set to be general
template<typename DataType>
void matrix<DataType>:: set_at_ptr(int matrix_t){
if(compression_type ==matrix_t){
switch(matrix_t){
case symmetric :{
at_ptr_c = at_symmetric_c;
at_ptr= at_symmetric ;
}break ;
case diagonal :{
at_ptr_c = at_diagonal_c;
at_ptr= at_diagonal ;
empty_vec[0] =0;
}break ;
case utri :{
at_ptr_c= at_utri_c;
at_ptr= at_utri ;
empty_vec[0] =0;
}break;
case ltri :{
at_ptr_c= at_ltri_c;
at_ptr= at_ltri ;
empty_vec[0] =0;
}break;
case general :{
at_ptr_c = at_general_c;
at_ptr= at_general ;
}break ;
case orthonormal :{
at_ptr_c = at_general_c;
at_ptr= at_general ;
}break ;
case anti_symmetric :{
at_ptr_c = at_anti_symmetric_c;
at_ptr= at_anti_symmetric ;
}break ;
case iden :{
at_ptr_c= at_identity_c;
at_ptr= at_identity ;
empty_vec[0] =0;
}break;
case constant:{
at_ptr_c= at_const_c;
at_ptr= at_const ;
}break ;
default:{
at_ptr_c = at_general_c;
at_ptr= at_general ;
}break;
}
}
else{
decompress();
compression_type=general_compress;
at_ptr=at_general;
at_ptr_c = at_general_c;
is_compressed=false;
}
}
template<typename DataType>
void matrix<DataType>:: set_start_end_ptr(int matrix_t){
if(compression_type!=matrix_t){
start_ptr = start_general;
end_ptr = end_general;
}
else{
switch(matrix_t){
case symmetric :{
start_ptr = start_symmetric;
end_ptr= end_symmetric ;
}break ;
case diagonal :{
start_ptr = start_diagonal;
end_ptr= end_diagonal ;
}break ;
case utri :{
start_ptr= start_utri;
end_ptr= end_utri ;
}break;
case ltri :{
start_ptr= start_ltri;
end_ptr= end_ltri ;
}break;
case general :{
start_ptr= start_general;
end_ptr= end_general ;
}break ;
case orthonormal :{
start_ptr = start_general;
end_ptr= end_general ;
}break ;
case anti_symmetric :{
start_ptr = start_symmetric;
end_ptr= end_symmetric ;
}break ;
case iden :{
start_ptr= start_identity;
end_ptr= end_identity ;
}break;
case constant:{
start_ptr= start_constant;
end_ptr= end_constant ;
}break ;
default:{
start_ptr = start_general;
end_ptr= end_general ;
}break;
}
}
}
template <typename DataType>
bool matrix<DataType>::is_valid_index(int row_i,int col_i)const{
return (row_i>=0&&row_i<rows&&col_i>=0&&col_i< cols);
}
//declarations of at for each matrix type
//for general matrices
template<typename DataType>
const DataType& matrix<DataType>::at_general_c(int row_i,int col_i) const {
return vec[row_i*cols+col_i] ;
}
template<typename DataType>
DataType& matrix<DataType>::at_general(int row_i,int col_i){
return vec[row_i*cols+col_i] ;
}
//for upper triangular matrices
template<typename DataType>
DataType& matrix<DataType>::at_utri(int row_i,int col_i) {
if(pindex[row_i]!=-1&&(col_i>=pindex[row_i])){
return vec[row_start[row_i]+(col_i-pindex[row_i])] ;
}
else{
empty_vec[0]=0;
return empty_vec[0];
}
}
//for lower triangular matrices
template<typename DataType>
DataType& matrix<DataType>::at_ltri(int row_i,int col_i) {
//if this row has a pivot and the col index is before or is the pivot
//then we return data else its a zero
if(pindex[row_i]!=-1&&col_i<=pindex[row_i]){
return vec[row_start[row_i]+pindex[row_i]-col_i] ;
}
else{
empty_vec[0]=0;
return empty_vec[0];
}
}
//for diagonal matrices
template<typename DataType>
DataType& matrix<DataType>:: at_diagonal(int row_i,int col_i) {
if(row_i==col_i){
return vec[row_i];
}
else{
empty_vec[0]=0;
return empty_vec[0] ;
}
}
//for identity matrices
template<typename DataType>
DataType& matrix<DataType>::at_identity(int row_i,int col_i) {
if(row_i==col_i){
//vec will only contain one element which is one
//over engineered
return vec[0];
}
else{
empty_vec[0] = 0 ;
return empty_vec[0] ;
}
}
template<typename DataType>
DataType& matrix<DataType>::at_const(int row_i,int col_i) {
return vec[0];
}
template<typename DataType>
DataType& matrix<DataType>::at_symmetric(int row_i, int col_i) {
if(col_i >=row_i) {
return vec[(row_i * (2*rows - row_i + 1))/2 + col_i - row_i];
}
else {
return vec[(col_i * (2*rows - col_i + 1))/2 + row_i - col_i];
}
}
template<typename DataType>
const DataType& matrix<DataType>::at_symmetric_c(int row_i, int col_i)const{
if(col_i >=row_i) {
return vec[(row_i * (2*rows - row_i + 1))/2 + col_i - row_i];
}
else {
return vec[(col_i * (2*rows - col_i + 1))/2 + row_i - col_i];
}
}
template<typename DataType>
const DataType& matrix<DataType>::at_anti_symmetric_c(int row_i, int col_i)const{
if(col_i >=row_i) {
return vec[(row_i * (2*rows - row_i + 1))/2 + col_i - row_i];
}
else {
empty_vec[0]= vec[(col_i * (2*rows - col_i + 1))/2 + row_i - col_i]*DataType(-1);
return empty_vec[0] ;
}
}
template<typename DataType>
DataType& matrix<DataType>::at_anti_symmetric(int row_i, int col_i) {
if(col_i >=row_i) {
return vec[(row_i * (2*rows - row_i + 1))/2 + col_i - row_i];
}
else {
empty_vec[0]= vec[(col_i * (2*rows - col_i + 1))/2 + row_i - col_i]*DataType(-1);
return empty_vec[0] ;
}
}
//for upper triangular matrices
template<typename DataType>
const DataType& matrix<DataType>:: at_utri_c(int row_i,int col_i) const{
if(pindex[row_i]!=-1&&(col_i>=pindex[row_i])){
return vec[row_start[row_i]+(col_i-pindex[row_i])] ;
}
else{
empty_vec[0]=0;
return empty_vec[0];
}
}
//for lower triangular matrices
template<typename DataType>
const DataType& matrix<DataType>:: at_ltri_c(int row_i,int col_i) const {
//if this row has a pivot and the col index is before or is the pivot
//then we return data else its a zero
if(pindex[row_i]!=-1&&col_i<=pindex[row_i]){
return vec[row_start[row_i]+pindex[row_i]-col_i] ;
}
else{
empty_vec[0]=0;
return empty_vec[0]; }
}
//for diagonal matrices
template<typename DataType>
const DataType& matrix<DataType>:: at_diagonal_c(int row_i,int col_i) const {
if(row_i==col_i){
return vec[row_i];
}
else{
empty_vec[0] = 0;
return empty_vec[0] ;
}
}
//for identity matrices
template<typename DataType>
const DataType& matrix<DataType>::at_identity_c(int row_i,int col_i) const {
if(row_i==col_i){
//vec will only contain one element which is one
//over engineered
return vec[0];
}
else{
empty_vec[0] = 0;
return empty_vec[0] ;
}
}
template<typename DataType>
const DataType& matrix<DataType>:: at_const_c(int row_i,int col_i) const{
return vec[0] ;
}
template<typename DataType>
const DataType& matrix<DataType>:: at(int row_i,int col_i)const{
return (this->*at_ptr_c)(row_i, col_i);
}
template<typename DataType>
DataType& matrix<DataType>:: at(int row_i,int col_i){
return (this->*at_ptr)(row_i, col_i);
}
//append cols of 2 matrices and return the new matrix
template <typename DataType>
matrix<DataType> matrix<DataType>::append_cols(const matrix&src)const {
matrix<DataType>ret_mat ;
if(src.rows==rows){
ret_mat = matrix<DataType>(rows,cols+src.cols);
for(int row_c = 0;row_c<rows;row_c++){
for(int j= 0 ; j<cols;j++){
ret_mat.at(row_c,j) =at(row_c,j) ;
}
for(int j= 0 ; j<src.cols;j++){
ret_mat.at(row_c,j+cols)=src.at(row_c,j) ;
}
}
}
else{
cout<<"can't append 2 matrices with different number of rows default garbage value is -1" ;
ret_mat = matrix(1,1,-1) ;
}
return ret_mat ;
}
template <typename DataType>
matrix<DataType> matrix<DataType>::append_rows(const matrix&src)const {
matrix<DataType>ret_mat ;
if(src.cols==cols){
ret_mat = matrix(rows+src.rows,cols);
for(int row_c = 0;row_c<rows+src.rows;row_c++){
if(row_c<rows){
for(int j= 0 ; j<cols;j++){
ret_mat.at(row_c,j) =at(row_c,j) ;
}
}
else{
for(int j= 0 ; j<cols;j++){
ret_mat.at(row_c,j) =src.at(row_c-rows,j) ;
}
}
}
}
else{
cout<<"can't append 2 matrices with different number of rows default garbage value is -1" ;
ret_mat = matrix(1,1,-1) ;
}
return ret_mat ;
}
//append rows of 2 matrices and return the new matrix
template <typename DataType>
DataType matrix<DataType>::dot(const matrix&mat)const{
if(same_shape(mat)){
DataType res = 0;
for(int i = 0 ;i <rows;i++){
for(int j = 0 ; j<cols ;j++){
res+=mat.at(i,j)*conjugate(at(i,j));
}
}
return res ;
}
cout<<shape_error;
return -1 ;
}
//performs a*x+y
template <typename DataType>
DataType matrix<DataType>::axpy(DataType alpha,const matrix&y)const{
if(y.vec&&same_shape(y)){
DataType res = 0;
for(int i = 0 ;i <rows;i++){
for(int j = max(start(i),y.start(i)); j<min(end(i),y.end(i));j++){
res+=alpha*at(i,j)+y.at(i,j) ;
}
}
return res ;
}
cout<<shape_error ;
return -1 ;
}
template <typename DataType>
bool matrix<DataType>::same_shape(const matrix&mat)const{
return ((mat.get_rows()==get_rows())&&(mat.get_cols()==get_cols())) ;
}
template <typename DataType>
void matrix<DataType>:: set_identity(){
if(is_square()){
for(int i = 0 ;i <rows;i++){
for(int j = 0 ; j<cols ;j++){
if(i==j){
at(i,j) =1;
}
else{
at(i,j) =0;
}
}
}
matrix_type = iden;
}
else{
cout<<square_error;
}
}
template <typename DataType>
void matrix<DataType>::fill(DataType value){
if(vec){
for(int i= 0 ; i <rows;i++){
for(int j =0 ; j<cols;j++){
at(i,j) = value;
}
}
matrix_type = constant;
}
}
//shows the matrix<DataType>:)
template <typename DataType>
void matrix<DataType>:: show(void)const {
if(vec){
for(int row_counter= 0 ; row_counter<rows; row_counter++){
cout<<'\n' ;
cout<<"[";
for(int col_counter = 0 ; col_counter<cols-1;col_counter++){
cout<<at(row_counter,col_counter)<<" , ";
}
cout<<at(row_counter,cols-1);
cout<<"]";
}
}
}
//turns the whole matrix<DataType>int o a string ease print ing
template <typename DataType>
string matrix<DataType>::mat_to_string(void)const{
string ret_str="" ;
for(int i = 0 ; i<rows;i++){
ret_str+='[' ;
for(int j= 0 ; j<cols ;j++){
ret_str+=to_string(at(i,j));
if(j!=cols-1){
ret_str+= " , " ;
}
}
ret_str+="]\n" ;
}
return ret_str ;
}
template <typename DataType>
matrix<DataType> matrix<DataType>:: operator+(const matrix&mat)const{
if(vec&&same_shape(mat)){
matrix<DataType>ret_mat(rows,cols) ;
for(int i = 0 ;i<rows; i++){
for(int j = 0 ; j<cols ;j++){
ret_mat.at(i,j) = at(i,j)+mat.at(i,j);
}
}
return ret_mat ;
}
cout<<shape_error ;
matrix<DataType>error_mat(1,1,-1);
return error_mat ;
}
// Subtract a matrix<DataType>from caller
template <typename DataType>
matrix<DataType> matrix<DataType>:: operator-(const matrix&mat)const{
if(vec&&same_shape(mat)){
matrix<DataType>ret_mat(rows,cols) ;
for(int i = 0 ;i<rows; i++){
for(int j = 0 ; j<cols ;j++){
ret_mat.at(i,j) = at(i,j)-mat.at(i,j);
}
}
return ret_mat ;
}
cout<<shape_error ;
matrix<DataType>error_mat(1,1,-1) ;
return error_mat ;
}
//returns a scaled up matrix
template <typename DataType>
matrix<DataType> matrix<DataType>::operator*(DataType scalar)const{
if(vec){
matrix<DataType>ret_mat(rows,cols) ;
for(int i = 0 ;i<rows; i++){
for(int j = start(i) ; j<end(i) ;j++){
ret_mat.at(i,j)=at(i,j)*scalar ;
}
}
return ret_mat ;
}
cout<<uninit_error ;
matrix<DataType>error_mat(1,1,-1) ;
return error_mat ;
}
template <typename DataType>
matrix<DataType> matrix<DataType>:: operator * (const matrix&mat)const{
if(cols ==mat.rows){
matrix<DataType>ret_mat(rows,mat.cols,0) ;
for(int row_counter= 0 ; row_counter<rows; row_counter++){
for(int col_counter = 0 ; col_counter<mat.cols;col_counter++){
for(int ele_counter = start(row_counter) ; ele_counter <end(row_counter);ele_counter++){
ret_mat.at(row_counter,col_counter)+= at(row_counter,ele_counter)*mat.at(ele_counter,col_counter);
}
}
}
return ret_mat;
}
cout<<shape_error ;
matrix<DataType>error_mat(1,1,-1) ;
return error_mat ;
}
template <typename DataType>
matrix<DataType> matrix<DataType>::transpose(void)const{
if(vec){
matrix<DataType>ret_mat(cols,rows);
for(int i = 0 ; i <rows;i++){
for(int j = 0 ; j<cols;j++){
//don't worry conjugate is overloaded
//so that if its any data type other than complex
//it does nothing but if its a complex number
//it produces the conjugate
ret_mat.at(j,i)=conjugate(at(i,j));
}
}
return ret_mat ;
}
cout<<square_error ;
matrix<DataType> error_mat(1,1,-1) ;
return error_mat ;
}
template <typename DataType>
DataType matrix<DataType>::trace(void)const{
if(vec&&is_square()){
int res = 0;
for(int i = 0 ; i <rows;i++){
res+=at(i,i);
}
return res ;
}
cout<<square_error ;
return -1 ;
}
template <typename DataType>
bool matrix<DataType>::is_square(void)const{
return rows==cols;
}
template <typename DataType>
bool matrix<DataType>::is_symmetric(void)const {
if(vec&&is_square()){
for(int i = 0 ; i <rows;i++){
for(int j=i+1;j<cols;j++){
if(abs(at(i,j)-conjugate(at(j,i)))>check_tolerance){
return false ;
}
}
}
matrix_type = symmetric;
return true ;
}
return false ;
}
template <typename DataType>
bool matrix<DataType>::is_diagonal(void) const {
bool logic= is_upper_tri()&&is_lower_tri();
if(logic){
matrix_type = diagonal;
}
return logic ;
}
template <typename DataType>
DataType matrix<DataType>::norm2(void) const {
if(vec){
return sqrt(abs(dot(*this))) ;
}
cout<<uninit_error ;
return -1 ;
}
//same functionality as norm2 just different wrapper
template <typename DataType>
DataType matrix<DataType>::length(void) const {
return norm2() ;
}
template <typename DataType>
DataType matrix<DataType>::theta(matrix&mat) const {
//a.b =|a||b|costheta
//theta = acos(a.b/ab)
if(same_shape(mat)){
DataType len_a = length() ;
DataType len_b = mat.length() ;
DataType adotb = dot(mat) ;
if(len_a>=tolerance&&len_b>=tolerance){
DataType val =acos(abs(adotb/(len_a*len_b))) ;
return (val>=tolerance)?val*(180/M_PI):0 ;
}
}
cout<<shape_error ;
return -1 ;
}
// Check if this matrix<DataType>is orthogonal
template <typename DataType>
bool matrix<DataType>::is_orthogonal(void)const {
matrix<DataType>trans_mat = transpose() ;
trans_mat = *this *trans_mat ;
bool logic =trans_mat.is_identity() ;
if(logic){
matrix_type = orthonormal;
}
return logic;
}
//check if this matrix<DataType>is orthogonal with matrix<DataType>mat
template <typename DataType>
bool matrix<DataType>::is_orthogonal(const matrix&mat) const {
if(rows==mat.rows){
matrix<DataType>trans= transpose() ;
trans = trans*mat ;
return trans.is_zero() ;
}
return false ;
}
template <typename DataType>
bool matrix<DataType>:: is_parallel(const matrix&mat)const {
return abs(theta(mat))<=tolerance||abs(theta(mat)-180)<=check_tolerance ;
}
template <typename DataType>
bool matrix<DataType>::operator == (const matrix&mat)const{
if(same_shape(mat)){
for (int i = 0; i < rows; i++){
for (int j = 0; j <cols; j++) {
if(abs(at(i,j)-mat.at(i,j))>check_tolerance){
return false ;
}
}
}
return true ;
}
return false ;
}
//helper function
template <typename DataType>
void matrix<DataType>::row_axpy(DataType scalar,int upper_row,int lower_row){
for(int col_counter =start(lower_row) ; col_counter<end(lower_row);col_counter++){
at(lower_row,col_counter)+= at(upper_row,col_counter)*scalar;
}
}
//performs downward gaussian elimination producing an upper triangular matrix
//optional if you want to know the indices of the pivots for each row
//pass in a matrix<DataType>aka pivots_indices
template <typename DataType>
matrix<DataType> matrix<DataType>::gauss_down( matrix<int>*pivots_indices,int pivots_locations) const {
if(matrix_type!=utri){
matrix<DataType>ret_mat = *this;
//make a separate vector for compression of already
//compressed matrix
if(pivots_indices){
if(pivots_locations==new_locations){
*pivots_indices = matrix<int>(rows,1,-1) ;
}
else{
//here it will be permutaions matrix
//to record each row exchange happening
*pivots_indices = matrix<int>(rows,1);
for(int i= 0 ; i<pivots_indices->get_rows();i++){
pivots_indices->at(i,0) = i;
}
}
}
//when getting pivot indices we have to keep track of old pivot since
//the new pivot won't exist in the same column so we go to next column each iteration
int old_pivot = -1 ;
for(int up_r = 0;up_r<rows; up_r++){
//check for pivot in the next column
//at first iteration we check for sure for first col hence 1-1 = 0
int pivot_index =old_pivot+1 ;
//if not a pivot then we find next pivot by increasing the pivot index
//aka find it in the next column
int pivot_condition = ret_mat.is_pivot(up_r,pivot_index) ;
while(pivot_index<cols&&pivot_condition==-1){
pivot_index++ ;
pivot_condition = ret_mat.is_pivot(up_r,pivot_index);
}
//make sure you aren't out of bounds
if(pivot_index<cols){
if(pivots_indices){
//if user wants new locations after switching rows
if(pivots_locations==new_locations){
pivots_indices->at(up_r,0) = pivot_index ;
}
//else user wants the locations of pivots lying in original rows
else if(pivots_locations==old_locations){
pivots_indices->switch_rows(up_r,pivot_condition) ;
}
}
for(int low_r = up_r+1; low_r<rows; low_r++){
//do gaussian elimination downward
//check if lower element is not zero to save processing power
if(abs(ret_mat.at(low_r,pivot_index))>tolerance){
DataType c = (ret_mat.at(low_r,pivot_index)/ret_mat.at(up_r,pivot_index))*DataType(-1);
for(int i =pivot_index ; i<cols;i++){
ret_mat.at(low_r,i)+=c*ret_mat.at(up_r,i) ;
}
}
}
//record that pivot to search for next pivot in the next column not in same column
//as mentioned above
old_pivot = pivot_index ;
}
}
ret_mat.set_feature(utri);
return ret_mat;
}
if(pivots_indices!=NULL){
*pivots_indices=matrix<int>(rows,1,pindex,rows);
}
return *this;
}
//performs upward gaussian elimination producing a lower triangular matrix
//optional if you want to know the indices of the pivots for each row
//pass in a matrix<DataType>aka pivots_indices
template <typename DataType>
matrix<DataType> matrix<DataType>::gauss_up( matrix<int>*pivots_indices)const {
if(matrix_type!=ltri){
matrix<DataType>ret_mat = *this ;
if(pivots_indices){
*pivots_indices=matrix<int>(rows,1,-1) ;
}
//same idea as gauss_down but instead of -1 its now rows since we look
//for pivots from last row till first row
int old_pivot =cols;
for(int low_r = rows-1 ; low_r>=0;low_r--){
//pivot_index in first iteration will be rows-1 which is the first location
//to look for a pivot
int pivot_index =old_pivot-1 ;
//if not a pivot then we find next pivot by decreasing the pivot index
//aka find it in the prev column
int pivot_condition = ret_mat.is_pivot_up(low_r,pivot_index) ;
while(pivot_index>=0&&pivot_condition==-1){
pivot_index-- ;
pivot_condition = ret_mat.is_pivot(low_r,pivot_index);
}
if(pivot_index>=0){
if(pivots_indices){
pivots_indices->at(pivot_condition,0) = pivot_index ;
}
for(int up_r = pivot_condition-1;up_r>=0;up_r--){
if(abs(ret_mat.at(up_r,pivot_index))>tolerance){
DataType c =(ret_mat.at(up_r,pivot_index)/ret_mat.at(pivot_condition,pivot_index))*DataType(-1);
for(int col_c = pivot_index ; col_c>=0; col_c--){
ret_mat.at(up_r,col_c)+= c*ret_mat.at(pivot_condition,col_c);
}
}
}
//keep track of pivot same as gauss_down
old_pivot = pivot_index ;
}
}
ret_mat.set_feature(ltri);
return ret_mat;
}
if(pivots_indices!=NULL){
*pivots_indices=matrix<int>(rows,1,pindex,rows);
}
return *this ;
}
//this function switches 2 rows and returns state of switching meaning the rows are valid
template <typename DataType>
bool matrix<DataType>::switch_rows(int r1 ,int r2 ){
if(r1>=0&&r1<get_rows()&&r2>=0&&r2<get_rows()&&r1!=r2){
for(int i = 0 ; i <get_cols();i++){
swap(at(r1,i),at(r2,i));
}
return true ;
}
return false ;
}
//performs back substitution on lower triangular invertible matrix
template <typename DataType>
DataType matrix<DataType>:: back_sub(int row_index,const matrix&sol_mat)const {
DataType sum = at(row_index,get_cols()-1);
for(int col_counter = row_index+1 ;col_counter<get_cols()-1; col_counter++){
sum-=at(row_index,col_counter)*sol_mat.at(col_counter,0);
}
return sum/at(row_index,row_index) ;
}
//performs forward substitution on lower triangular invertible matrix
template <typename DataType>
DataType matrix<DataType>:: fwd_sub(int row_index,const matrix&sol_mat)const {
DataType sum = at(row_index,get_cols()-1);
for(int col_counter = 0 ;col_counter<row_index;col_counter++){
sum-=at(row_index,col_counter)*sol_mat.at(col_counter,0);
}
return sum/at(row_index,row_index) ;
}
//pass an appended matrix
template <typename DataType>
matrix<DataType> matrix<DataType>:: solve(void) const {
matrix<int >pivots_indices;
//turns the system int o uppertriangular system
matrix<DataType>mat_cpy=gauss_down(&pivots_indices,new_locations);
//check for number of pivots first
for(int i = 0 ; i<rows;i++){
if(pivots_indices.at(i,0)==-1){
cout<<"Number of pivots is insufficient default garbage value is -1" ;