forked from mnucci32/aither
/
matrix.cpp
1008 lines (825 loc) · 26.2 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
/* An open source Navier-Stokes CFD solver.
Copyright (C) 2015 Michael Nucci (michael.nucci@gmail.com)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include <cstdlib> //exit()
#include "matrix.h"
#include <iostream> //cout
#include <cmath>
using std::cout;
using std::endl;
using std::cerr;
using std::copy;
using std::swap_ranges;
using std::fabs;
//copy constructor
squareMatrix::squareMatrix( const squareMatrix &cp){
(*this).size = cp.Size();
(*this).data = new double[cp.Size()*cp.Size()];
copy(&cp.data[0], &cp.data[0] + cp.Size()*cp.Size(), &(*this).data[0]);
}
//copy assignment operator
squareMatrix& squareMatrix::operator= (squareMatrix other){
swap(*this, other);
return *this;
}
//friend function to allow for swap functionality
void swap(squareMatrix &first, squareMatrix &second){
std::swap(first.size, second.size);
std::swap(first.data, second.data);
}
//member function to get the data from the matrix
double squareMatrix::Data( const int &r, const int &c )const{
//test to see that row and column inputs are within bounds
if ( (r >= ((*this).size)) || (c >= ((*this).size)) ){
cerr << "ERROR: The requested data, does not lie within the matrix bounds. Check row and column inputs." << endl;
exit(1);
}
return data[c + r * (*this).size];
}
//member function to set the data in the matrix
void squareMatrix::SetData( const int &r, const int &c, const double &val ){
//test to see that row and column inputs are within bounds
if ( (r >= ((*this).size)) || (c >= ((*this).size)) ){
cerr << "ERROR: Cannot assign data to given location because it does not lie within the matrix bounds. Check row and column inputs." << endl;
exit(1);
}
data[c + r * (*this).size] = val;
}
//member function to swap rows of matrix
void squareMatrix::SwapRows(const int &r1, const int &r2){
if(r1 != r2){
for( int ii = 0; ii < size; ii++ ){
int ind1 = ii + r1*size;
int ind2 = ii + r2*size;
std::swap((*this).data[ind1], (*this).data[ind2]);
}
}
}
//member function to invert matrix using Gauss-Jordan elimination
void squareMatrix::Inverse(){
squareMatrix I((*this).size);
int r = 0;
I.Identity();
int cPivot = 0;
for( cPivot = 0, r = 0; r < size; r++, cPivot++ ){
//find pivot row
int rPivot = (*this).FindMaxInCol(r,cPivot,size-1);
//swap rows
(*this).SwapRows(r,rPivot);
I.SwapRows(r,rPivot);
if(r != 0){ //if not on first row, need to get rid entries ahead of pivot
for( int ii = 0; ii < cPivot; ii++ ){
double factor = (*this).Data(r,ii) / (*this).Data(ii,ii);
(*this).LinCombRow(ii, factor, r);
I.LinCombRow(ii, factor, r);
}
}
//normalize row by pivot
if((*this).Data(r,cPivot) == 0.0){
cerr << "ERROR: Singular matrix in Gauss-Jordan elimination! Matrix (mid inversion) is" << endl << *this << endl;
exit(1);
}
double normFactor = 1.0/(*this).Data(r,cPivot);
(*this).RowMultiply(r,cPivot,normFactor); //only multiply entries from pivot and to the right
I.RowMultiply(r,0,normFactor); //multiply all entries
}
//matrix is now upper triangular, work way back up to identity matrix
cPivot = size - 2; //start with second to last row
for( cPivot = size - 2, r = size - 2; r >= 0; r--, cPivot-- ){
for( int ii = size - 1; ii > cPivot; ii-- ){
double factor = (*this).Data(r,ii);
(*this).LinCombRow(ii, factor, r);
I.LinCombRow(ii, factor, r);
}
}
//set this matrix equal to its inverse
(*this) = I;
}
//member function to add a linear combination of one row to another
void squareMatrix::LinCombRow(const int &r1, const double &factor, const int &r2){
for( int ii = 0; ii < size; ii++ ){
(*this).SetData(r2, ii, (*this).Data(r2,ii) - (*this).Data(r1,ii) * factor);
}
}
//member function to multiply a row by a given factor
void squareMatrix::RowMultiply(const int &r, const int &c, const double &factor){
for( int ii = c; ii < size; ii++ ){
(*this).SetData(r, ii, (*this).Data(r,ii) * factor);
}
}
//member function to find maximum absolute value in a given column and range within that column and return the corresponding row indice
int squareMatrix::FindMaxInCol(const int &c, const int &start, const int &end)const{
double maxVal = 0.0;
int maxRow = 0;
for( int ii = start; ii < end+1; ii++ ){
if( fabs((*this).Data(ii,c)) > maxVal ){
maxVal = fabs((*this).Data(ii,c));
maxRow = ii;
}
}
return maxRow;
}
//operator overload for addition
squareMatrix squareMatrix::operator + (const squareMatrix& s2)const{
squareMatrix s1 = *this;
//check to see that matrix dimensions are the same
if ( s1.size != s2.size ){
cerr << "ERROR: Cannot add matrices, dimensions do not agree." << endl;
}
int cc = 0;
int rr = 0;
for( cc = 0; cc < s2.Size(); cc++ ){
for( rr = 0; rr < s2.Size(); rr++ ){
s1.SetData(rr,cc, s1.Data(rr,cc) + s2.Data(rr,cc));
}
}
return s1;
}
//operator overload for addition with a scalar
squareMatrix squareMatrix::operator + (const double &scalar)const{
squareMatrix s1 = *this;
int cc = 0;
int rr = 0;
for( cc = 0; cc < s1.Size(); cc++ ){
for( rr = 0; rr < s1.Size(); rr++ ){
s1.SetData(rr,cc, s1.Data(rr,cc) + scalar);
}
}
return s1;
}
//operator overload for addition with a scalar
squareMatrix operator+ (const double &scalar, const squareMatrix &s2){
squareMatrix s1(s2.Size());
int cc = 0;
int rr = 0;
for( cc = 0; cc < s2.Size(); cc++ ){
for( rr = 0; rr < s2.Size(); rr++ ){
s1.SetData(rr,cc, s2.Data(rr,cc) + scalar);
}
}
return s1;
}
//operator overload for subtraction
squareMatrix squareMatrix::operator - (const squareMatrix& s2)const{
squareMatrix s1 = *this;
//check to see that matrix dimensions are the same
if ( s1.size != s2.size ){
cerr << "ERROR: Cannot subtract matrices, dimensions do not agree." << endl;
}
int cc = 0;
int rr = 0;
for( cc = 0; cc < s2.Size(); cc++ ){
for( rr = 0; rr < s2.Size(); rr++ ){
s1.SetData(rr,cc, s1.Data(rr,cc) - s2.Data(rr,cc));
}
}
return s1;
}
//operator overload for subtraction with a scalar
squareMatrix squareMatrix::operator - (const double &scalar)const{
squareMatrix s1 = *this;
int cc = 0;
int rr = 0;
for( cc = 0; cc < s1.Size(); cc++ ){
for( rr = 0; rr < s1.Size(); rr++ ){
s1.SetData(rr,cc, s1.Data(rr,cc) - scalar);
}
}
return s1;
}
//operator overload for subtraction with a scalar
squareMatrix operator- (const double &scalar, const squareMatrix &s2){
squareMatrix s1(s2.Size());
int cc = 0;
int rr = 0;
for( cc = 0; cc < s2.Size(); cc++ ){
for( rr = 0; rr < s2.Size(); rr++ ){
s1.SetData(rr,cc, scalar - s2.Data(rr,cc));
}
}
return s1;
}
//operator overload for multiplication
squareMatrix squareMatrix::operator * (const squareMatrix& s2)const{
squareMatrix s1 = *this;
//check to see that matrix dimensions are the same
if ( s1.size != s2.size ){
cerr << "ERROR: Cannot multiply matrices, dimensions do not agree." << endl;
}
int cc = 0;
int rr = 0;
for( cc = 0; cc < s2.Size(); cc++ ){
for( rr = 0; rr < s2.Size(); rr++ ){
double newVal = 0.0;
int ii = 0;
for( ii = 0; ii < s2.Size(); ii++ ){
newVal += ((*this).Data(rr,ii) * s2.Data(ii,cc));
}
s1.SetData(rr,cc, newVal);
}
}
return s1;
}
//operator overload for multiplication with a scalar
squareMatrix squareMatrix::operator * (const double &scalar)const{
squareMatrix s1 = *this;
int cc = 0;
int rr = 0;
for( cc = 0; cc < s1.Size(); cc++ ){
for( rr = 0; rr < s1.Size(); rr++ ){
s1.SetData(rr,cc, s1.Data(rr,cc) * scalar);
}
}
return s1;
}
//operator overload for multiplication with a scalar
squareMatrix operator* (const double &scalar, const squareMatrix &s2){
squareMatrix s1(s2.Size());
int cc = 0;
int rr = 0;
for( cc = 0; cc < s2.Size(); cc++ ){
for( rr = 0; rr < s2.Size(); rr++ ){
s1.SetData(rr,cc, s2.Data(rr,cc) * scalar);
}
}
return s1;
}
//operator overload for division with a scalar
squareMatrix squareMatrix::operator / (const double &scalar)const{
squareMatrix s1 = *this;
int cc = 0;
int rr = 0;
for( cc = 0; cc < s1.Size(); cc++ ){
for( rr = 0; rr < s1.Size(); rr++ ){
s1.SetData(rr,cc, s1.Data(rr,cc) / scalar);
}
}
return s1;
}
//operator overload for division with a scalar
squareMatrix operator/ (const double &scalar, const squareMatrix &s2){
squareMatrix s1(s2.Size());
int cc = 0;
int rr = 0;
for( cc = 0; cc < s2.Size(); cc++ ){
for( rr = 0; rr < s2.Size(); rr++ ){
s1.SetData(rr,cc, scalar / s2.Data(rr,cc));
}
}
return s1;
}
//operation overload for << - allows use of cout, cerr, etc.
ostream & operator<< (ostream &os, const squareMatrix &m){
int cc = 0;
int rr = 0;
for( rr = 0; rr < m.Size(); rr++ ){
for( cc = 0; cc < m.Size(); cc++ ){
cout << m.Data(rr,cc);
if(cc != (m.Size()-1)){
cout << ", ";
}
else{
cout << endl;
}
}
}
return os;
}
//member function to zero the matrix
void squareMatrix::Zero(){
for(int cc = 0; cc < size; cc++){
for(int rr = 0; rr < size; rr++){
(*this).SetData(rr,cc,0.0);
}
}
}
//member function to set matrix to Identity
void squareMatrix::Identity(){
for( int rr = 0; rr < (*this).Size(); rr++ ){
for (int cc = 0; cc < (*this).Size(); cc++ ){
if(rr == cc){
(*this).SetData(rr,cc,1.0);
}
else{
(*this).SetData(rr,cc,0.0);
}
}
}
}
colMatrix squareMatrix::Multiply( const colMatrix &X )const{
//Test to see that column matrix can be multiplied with square matrix
if ( (*this).Size() != X.Size() ){
cerr << "ERROR: Column matrix cannot be multiplied with square matrix. Sizes do not agree!" << endl;
exit(1);
}
colMatrix B(X.Size());
B.Zero();
for (int rr = 0; rr < X.Size(); rr++ ){
double tempData = 0.0;
for ( int cc = 0; cc < X.Size(); cc++ ){
tempData += (*this).Data(rr,cc) * X.Data(cc);
}
B.SetData(rr, tempData);
}
return B;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//function definitions for matrixDiagonal class
//copy constructor
matrixDiagonal::matrixDiagonal( const matrixDiagonal &cp){
(*this).size = cp.Size();
(*this).data = new squareMatrix[cp.Size()];
copy(&cp.data[0], &cp.data[0] + cp.Size(), &(*this).data[0]);
}
//copy assignment operator
matrixDiagonal& matrixDiagonal::operator= (matrixDiagonal other){
swap(*this, other);
return *this;
}
//friend function to allow for swap functionality
void swap(matrixDiagonal &first, matrixDiagonal &second){
std::swap(first.size, second.size);
std::swap(first.data, second.data);
}
//member function to get the data from the matrix
squareMatrix matrixDiagonal::Data( const int &ind)const{
//test to see that the index input is within bounds
if ( ind >= (*this).size ){
cerr << "ERROR: The requested data, does not lie within the matrix bounds. Check index input." << endl;
exit(1);
}
return data[ind];
}
//member function to set the data in the matrix
void matrixDiagonal::SetData( const int &ind, const squareMatrix &val ){
//test to see that the index input is within bounds
if ( ind >= (*this).size ){
cerr << "ERROR: The requested data, does not lie within the matrix bounds. Check index input." << endl;
exit(1);
}
data[ind] = val;
}
//operation overload for << - allows use of cout, cerr, etc.
ostream & operator<< (ostream &os, const matrixDiagonal &m){
int rr = 0;
for( rr = 0; rr < m.Size(); rr++ ){
cout << "In index " << rr << " matrix block is:" << endl;
cout << m.Data(rr) << endl;
}
return os;
}
//member function to zero the matrix
void matrixDiagonal::Zero(const int &s){
squareMatrix mZero(s);
mZero.Zero();
for(int cc = 0; cc < size; cc++){
(*this).SetData(cc,mZero);
}
}
//member function to delete the contents of the data structure and resize it
void matrixDiagonal::CleanResizeZero(const int &s, const int &m){
squareMatrix mZero(m);
mZero.Zero();
delete [] (*this).data;
(*this).data = new squareMatrix[s];
(*this).size = s;
for(int cc = 0; cc < size; cc++){
(*this).SetData(cc,mZero);
}
}
//member function to invert each matrix stored
void matrixDiagonal::Inverse(){
for(int ii = 0; ii < (*this).Size(); ii++ ){
squareMatrix temp = (*this).Data(ii);
temp.Inverse();
(*this).SetData(ii,temp);
}
}
//functions ------------------------------------------------------------------------------------------------
///////////////////////////////////////////////////////////////////////////////////////////////////////////
//functions for colMatrix class
//
//copy constructor
colMatrix::colMatrix( const colMatrix &cp){
(*this).size = cp.Size();
(*this).data = new double[cp.Size()];
copy(&cp.data[0], &cp.data[0] + cp.Size(), &(*this).data[0]);
}
//copy assignment operator
colMatrix& colMatrix::operator= (colMatrix other){
swap(*this, other);
return *this;
}
//friend function to allow for swap functionality
void swap(colMatrix &first, colMatrix &second){
std::swap(first.size, second.size);
std::swap(first.data, second.data);
}
//member function to get the data from the matrix
double colMatrix::Data( const int &r)const{
//test to see that row and column inputs are within bounds
if ( r >= (*this).size ){
cerr << "ERROR: The requested data, does not lie within the column matrix bounds. Check row input." << endl;
exit(1);
}
return data[r];
}
//member function to set the data in the matrix
void colMatrix::SetData( const int &r, const double &val ){
//test to see that row and column inputs are within bounds
if ( r >= (*this).size ){
cerr << "ERROR: Cannot assign data to given location because it does not lie within the column matrix bounds. Check row input." << endl;
exit(1);
}
data[r] = val;
}
//operator overload for addition
colMatrix colMatrix::operator + (const colMatrix& s2)const{
colMatrix s1 = *this;
//check to see that matrix dimensions are the same
if ( s1.size != s2.size ){
cerr << "ERROR: Cannot add column matrices, dimensions do not agree." << endl;
cerr << "Dimensions are " << s1.size << " and " << s2.size << endl;
}
for( int rr = 0; rr < s2.Size(); rr++ ){
s1.SetData(rr, s1.Data(rr) + s2.Data(rr));
}
return s1;
}
//operator overload for addition
colMatrix colMatrix::operator + (const vector<double>& v1)const{
colMatrix s1 = *this;
//check to see that matrix dimensions are the same
if ( s1.size != (int)v1.size() ){
cerr << "ERROR: Cannot add column matrix of size " << s1.size << " to vector class of size " << v1.size()<< ", dimensions do not agree." << endl;
}
for( int rr = 0; rr < s1.size; rr++ ){
s1.SetData(rr, s1.Data(rr) + v1[rr]);
}
return s1;
}
//operator overload for addition with a scalar
colMatrix colMatrix::operator + (const double &scalar)const{
colMatrix s1 = *this;
for( int rr = 0; rr < s1.Size(); rr++ ){
s1.SetData(rr, s1.Data(rr) + scalar);
}
return s1;
}
//operator overload for addition with a scalar
colMatrix operator+ (const double &scalar, const colMatrix &s2){
colMatrix s1(s2.Size());
for( int rr = 0; rr < s2.Size(); rr++ ){
s1.SetData(rr, s2.Data(rr) + scalar);
}
return s1;
}
//operator overload for subtraction
colMatrix colMatrix::operator - (const colMatrix& s2)const{
colMatrix s1 = *this;
//check to see that matrix dimensions are the same
if ( s1.size != s2.size ){
cerr << "ERROR: Cannot subtract column matrices, dimensions do not agree." << endl;
}
for( int rr = 0; rr < s2.Size(); rr++ ){
s1.SetData(rr, s1.Data(rr) - s2.Data(rr));
}
return s1;
}
//operator overload for addition
colMatrix colMatrix::operator - (const vector<double>& v1)const{
colMatrix s1 = *this;
//check to see that matrix dimensions are the same
if ( s1.size != (int)v1.size() ){
cerr << "ERROR: Cannot subtract column matrix of size " << s1.size << " to vector class of size " << v1.size()<< ", dimensions do not agree." << endl;
}
for( int rr = 0; rr < s1.size; rr++ ){
s1.SetData(rr, s1.Data(rr) - v1[rr]);
}
return s1;
}
//operator overload for subtraction with a scalar
colMatrix colMatrix::operator - (const double &scalar)const{
colMatrix s1 = *this;
for( int rr = 0; rr < s1.Size(); rr++ ){
s1.SetData(rr, s1.Data(rr) - scalar);
}
return s1;
}
//operator overload for subtraction with a scalar
colMatrix operator- (const double &scalar, const colMatrix &s2){
colMatrix s1(s2.Size());
for( int rr = 0; rr < s2.Size(); rr++ ){
s1.SetData(rr, scalar - s2.Data(rr));
}
return s1;
}
//operator overload for elementwise multiplication
colMatrix colMatrix::operator * (const colMatrix& s2)const{
colMatrix s1 = *this;
//check to see that matrix dimensions are the same
if ( s1.size != s2.size ){
cerr << "ERROR: Cannot elementwise multiply column matrices, dimensions do not agree." << endl;
}
for( int rr = 0; rr < s2.Size(); rr++ ){
s1.SetData(rr, s1.Data(rr) * s2.Data(rr));
}
return s1;
}
//operator overload for multiplication with a scalar
colMatrix colMatrix::operator * (const double &scalar)const{
colMatrix s1 = *this;
for( int rr = 0; rr < s1.Size(); rr++ ){
s1.SetData(rr, s1.Data(rr) * scalar);
}
return s1;
}
//operator overload for multiplication with a scalar
colMatrix operator* (const double &scalar, const colMatrix &s2){
colMatrix s1(s2.Size());
for( int rr = 0; rr < s2.Size(); rr++ ){
s1.SetData(rr, s2.Data(rr) * scalar);
}
return s1;
}
//operator overload for elementwise division
colMatrix colMatrix::operator / (const colMatrix& s2)const{
colMatrix s1 = *this;
//check to see that matrix dimensions are the same
if ( s1.size != s2.size ){
cerr << "ERROR: Cannot elementwise divide column matrices, dimensions do not agree." << endl;
}
for( int rr = 0; rr < s2.Size(); rr++ ){
s1.SetData(rr, s1.Data(rr) / s2.Data(rr));
}
return s1;
}
//operator overload for division with a scalar
colMatrix colMatrix::operator / (const double &scalar)const{
colMatrix s1 = *this;
for( int rr = 0; rr < s1.Size(); rr++ ){
s1.SetData(rr, s1.Data(rr) / scalar);
}
return s1;
}
//operator overload for division with a scalar
colMatrix operator/ (const double &scalar, const colMatrix &s2){
colMatrix s1(s2.Size());
for( int rr = 0; rr < s2.Size(); rr++ ){
s1.SetData(rr, scalar / s2.Data(rr));
}
return s1;
}
//operation overload for << - allows use of cout, cerr, etc.
ostream & operator<< (ostream &os, const colMatrix &m){
for( int rr = 0; rr < m.Size(); rr++ ){
cout << m.Data(rr) << endl;
}
return os;
}
//member function to zero the matrix
void colMatrix::Zero(){
for(int rr = 0; rr < size; rr++){
(*this).SetData(rr,0.0);
}
}
//member function to sum column matrix
double colMatrix::Sum(){
double sum = 0.0;
for( int ii = 0; ii < (*this).Size(); ii++ ){
sum += (*this).Data(ii);
}
return sum;
}
//member function to delete the contents of the data structure and resize it
void colMatrix::CleanResizeZero(const int &s){
delete [] (*this).data;
(*this).data = new double[s];
(*this).size = s;
for(int cc = 0; cc < size; cc++){
(*this).SetData(cc,0.0);
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////
//functions for genArray class
//
//constructors
genArray::genArray(const double &a){
for ( int ii = 0; ii < NUMVARS; ii++ ){
data[ii] = a;
}
}
genArray::genArray(){
for ( int ii = 0; ii < NUMVARS; ii++ ){
data[ii] = 0.0;
}
}
//operator overload for addition
genArray genArray::operator + (const genArray& s2)const{
genArray s1 = *this;
for( int rr = 0; rr < NUMVARS; rr++ ){
s1[rr] += s2[rr];
}
return s1;
}
//operator overload for addition
genArray genArray::operator + (const vector<double>& v1)const{
//check to see that vector is appropriate size
if ( v1.size() != NUMVARS ){
cerr << "ERROR: Cannot add vector and genArray because vector not appropriate size! Vector is of size " << v1.size() << endl;
exit(0);
}
genArray s1 = *this;
for( int rr = 0; rr < NUMVARS; rr++ ){
s1[rr] += v1[rr];
}
return s1;
}
//operator overload for addition with a scalar
genArray genArray::operator + (const double &scalar)const{
genArray s1 = *this;
for( int rr = 0; rr < NUMVARS; rr++ ){
s1[rr] += scalar;
}
return s1;
}
//operator overload for addition with a scalar
genArray operator+ (const double &scalar, const genArray &s2){
genArray s1;
for( int rr = 0; rr < NUMVARS; rr++ ){
s1[rr] = scalar + s2[rr];
}
return s1;
}
//operator overload for subtraction
genArray genArray::operator - (const genArray& s2)const{
genArray s1 = *this;
for( int rr = 0; rr < NUMVARS; rr++ ){
s1[rr] -= s2[rr];
}
return s1;
}
//operator overload for addition
genArray genArray::operator - (const vector<double>& v1)const{
//check to see that vector is appropriate size
if ( v1.size() != NUMVARS ){
cerr << "ERROR: Cannot subtract vector and genArray because vector not appropriate size! Vector is of size " << v1.size() << endl;
exit(0);
}
genArray s1 = *this;
for( int rr = 0; rr < NUMVARS; rr++ ){
s1[rr] -= v1[rr];
}
return s1;
}
//operator overload for subtraction with a scalar
genArray genArray::operator - (const double &scalar)const{
genArray s1 = *this;
for( int rr = 0; rr < NUMVARS; rr++ ){
s1[rr] -= scalar;
}
return s1;
}
//operator overload for subtraction with a scalar
genArray operator- (const double &scalar, const genArray &s2){
genArray s1;
for( int rr = 0; rr < NUMVARS; rr++ ){
s1[rr] = scalar - s2[rr];
}
return s1;
}
//operator overload for elementwise multiplication
genArray genArray::operator * (const genArray& s2)const{
genArray s1 = *this;
for( int rr = 0; rr < NUMVARS; rr++ ){
s1[rr] *= s2[rr];
}
return s1;
}
//operator overload for multiplication with a scalar
genArray genArray::operator * (const double &scalar)const{
genArray s1 = *this;
for( int rr = 0; rr < NUMVARS; rr++ ){
s1[rr] *= scalar;
}
return s1;
}
//operator overload for multiplication with a scalar
genArray operator* (const double &scalar, const genArray &s2){
genArray s1;
for( int rr = 0; rr < NUMVARS; rr++ ){
s1[rr] = s2[rr] * scalar;
}
return s1;
}
//operator overload for elementwise division
genArray genArray::operator / (const genArray& s2)const{
genArray s1 = *this;
for( int rr = 0; rr < NUMVARS; rr++ ){
s1[rr] /= s2[rr];
}
return s1;
}
//operator overload for division with a scalar
genArray genArray::operator / (const double &scalar)const{
genArray s1 = *this;
for( int rr = 0; rr < NUMVARS; rr++ ){
s1[rr] /= scalar;
}
return s1;
}
//operator overload for division with a scalar
genArray operator/ (const double &scalar, const genArray &s2){
genArray s1;
for( int rr = 0; rr < NUMVARS; rr++ ){
s1[rr] = scalar / s2[rr];
}
return s1;
}
//operation overload for << - allows use of cout, cerr, etc.
ostream & operator<< (ostream &os, const genArray &m){
for( int rr = 0; rr < NUMVARS; rr++ ){
cout << m[rr] << endl;
}
return os;
}
//member function to zero the matrix
void genArray::Zero(){
for(int rr = 0; rr < NUMVARS; rr++){
(*this)[rr] = 0.0;
}
}
//member function to sum column matrix
double genArray::Sum(){
double sum = 0.0;
for( int ii = 0; ii < NUMVARS; ii++ ){
sum += (*this)[ii];
}
return sum;
}
//member function to sum the residuals from all processors
void genArray::GlobalReduceMPI( const int &rank, const int &numEqns ){
//Get residuals from all processors
if ( rank == ROOT ){
MPI_Reduce(MPI_IN_PLACE, &(*this).data[0], numEqns, MPI_DOUBLE, MPI_SUM, ROOT, MPI_COMM_WORLD);
}
else{
MPI_Reduce(&(*this).data[0], &(*this).data[0], numEqns, MPI_DOUBLE, MPI_SUM, ROOT, MPI_COMM_WORLD);
}
}
//member function to get the addresses of a resid to create an MPI_Datatype
void resid::GetAddressesMPI(MPI_Aint (&displacement)[2])const{
//get addresses of each field
MPI_Get_address(&(*this).linf, &displacement[0]);
MPI_Get_address(&(*this).blk, &displacement[1]);
}
//member function to update class when a new maximum residual is found
void resid::UpdateMax(const double &a, const int &b, const int &c, const int &d, const int &e, const int &f){
linf = a;
blk = b;
i = c;
j = d;
k = e;
eqn = f;
}
//Member function to calculate the maximum residual from all processors
void resid::GlobalReduceMPI( const int &rank, const MPI_Datatype &MPI_DOUBLE_5INT, const MPI_Op &MPI_MAX_LINF ){
//Get residuals from all processors
if ( rank == ROOT ){
MPI_Reduce(MPI_IN_PLACE, &(*this), 1, MPI_DOUBLE_5INT, MPI_MAX_LINF, ROOT, MPI_COMM_WORLD);
}
else{
MPI_Reduce(&(*this), &(*this), 1, MPI_DOUBLE_5INT, MPI_MAX_LINF, ROOT, MPI_COMM_WORLD);
}
}
/* Function to calculate the maximum of two resid instances and allow access to all the data in the resid instance. This is used to create an operation
for MPI_Reduce.
*/
void MaxLinf( resid *in, resid *inout, int *len, MPI_Datatype *MPI_DOUBLE_5INT){
// *in -- pointer to all input residuals (from all procs)
// *inout -- pointer to input and output residuals. The answer is stored here
// *len -- pointer to array size of *in and *inout
// *MPI_DOUBLE_5INT -- pointer to MPI_Datatype of double followed by 5 Ints, which represents the resid class
resid resLinf; //intialize a resid
for ( int ii = 0; ii < *len; ii++ ){ //loop over array of resids
if ( in->linf >= inout->linf ){ //if linf from input is greater than or equal to linf from output, then new max has been found
resLinf = *in;
}
else{ //no new max
resLinf = *inout;
}