-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRMatrix.cs
1151 lines (1090 loc) · 35.8 KB
/
RMatrix.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Diffusion2D_Library
{
/// <summary>
/// Defines a matrix class that can store real-valued data
/// </summary>
public class RMatrix : ICloneable
{
// Fields
private readonly int nRows;
private readonly int nCols;
private double[,] matrix;
// Constructors
public RMatrix(int nRows, int nCols)
{
this.nRows = nRows;
this.nCols = nCols;
matrix = new double[nRows, nCols];
for (int i = 0; i < nRows; i++)
{
for (int j = 0; j < nCols; j++)
{
matrix[i, j] = 0.0;
}
}
}
public RMatrix(double[,] matrix)
{
nRows = matrix.GetLength(0);
nCols = matrix.GetLength(1);
this.matrix = matrix;
}
public RMatrix(RVector rv)
{
nRows = 1;
nCols = rv.GetRVectorSize;
matrix = new double[1, rv.GetRVectorSize];
for (int i = 0; i < rv.GetRVectorSize; i++)
{
matrix[0, i] = rv[i];
}
}
public RMatrix IdentityMatrix()
{
RMatrix m = new(nRows, nCols);
for (int i = 0; i < nRows; i++)
{
for (int j = 0; j < nCols; j++)
{
if (i == j)
{
m[i, j] = 1.0;
}
}
}
return m;
}
public RMatrix(double[] rv)
{
nRows = rv.Length;
nCols = 1;
matrix = new double[nRows, nCols];
for (int i = 0; i < nRows; i++)
{
matrix[i, 0] = rv[i];
}
}
// Accessors
public int GetnRows
{ get { return nRows; } }
public int GetnCols
{ get { return nCols; } }
// Indexers
public double this[int m, int n]
{
get
{
if (m < 0 || m > nRows)
{
throw new Exception("m-th row is out of range!");
}
if (n < 0 || n > nCols)
{
throw new Exception("n-th col is out of range!");
}
return matrix[m, n];
}
set { matrix[m, n] = value; }
}
// Override Methods
public override string ToString()
{
string strMatrix = "(";
for (int i = 0; i < nRows; i++)
{
string str = "";
for (int j = 0; j < nCols - 1; j++)
{
str += matrix[i, j].ToString() + ", ";
}
str += matrix[i, nCols - 1].ToString();
if (i != nRows - 1 && i == 0)
strMatrix += str + "\n";
else if (i != nRows - 1 && i != 0)
strMatrix += " " + str + "\n";
else
strMatrix += " " + str + ")";
}
return strMatrix;
}
public override bool Equals(object obj)
{
return (obj is RMatrix matrix1) && Equals(matrix1);
}
public bool Equals(RMatrix cm)
{
return matrix == cm.matrix;
}
public override int GetHashCode()
{
return matrix.GetHashCode();
}
public static bool operator ==(RMatrix cm1, RMatrix cm2)
{
return cm1.Equals(cm2);
}
public static bool operator !=(RMatrix cm1, RMatrix cm2)
{
return !cm1.Equals(cm2);
}
public static bool operator >=(RMatrix rm1, RMatrix rm2)
{
int nrows1 = rm1.GetnRows;
int ncols1 = rm1.GetnCols;
int nrows2 = rm2.GetnRows;
int ncols2 = rm2.GetnCols;
bool gt = true;
if (nrows1! != nrows2 || ncols1 != ncols2) { throw new Exception("Matrices do not have the same dimensions!"); }
for (int i = 0; i < nrows1; i++)
{
for (int j = 0; j < ncols1; j++)
{
if (rm1[i, j] < rm2[i, j]) { gt = false; }
}
}
return gt;
}
public static bool operator <=(RMatrix rm1, RMatrix rm2)
{
int nrows1 = rm1.GetnRows;
int ncols1 = rm1.GetnCols;
int nrows2 = rm2.GetnRows;
int ncols2 = rm2.GetnCols;
bool lt = true;
if (nrows1! != nrows2 || ncols1 != ncols2) { throw new Exception("Matrices do not have the same dimensions!"); }
for (int i = 0; i < nrows1; i++)
{
for (int j = 0; j < ncols1; j++)
{
if (rm1[i, j] > rm2[i, j]) { lt = false; }
}
}
return lt;
}
public static RMatrix operator +(RMatrix rm)
{
return rm;
}
public static RMatrix operator +(RMatrix rm1, RMatrix rm2)
{
if (!RMatrix.CompareDimension(rm1, rm2))
{
throw new Exception("The dimensions of 2 matrices must be the same!");
}
RMatrix result = new(rm1.GetnRows, rm1.GetnCols);
for (int i = 0; i < rm1.GetnRows; i++)
{
for (int j = 0; j < rm1.GetnCols; j++)
{
result[i, j] = rm1[i, j] + rm2[i, j];
}
}
return result;
}
public static RMatrix operator +(RMatrix rm, double cn)
{
RMatrix result = new(rm.GetnRows, rm.GetnCols);
for (int i = 0; i < rm.GetnRows; i++)
{
for (int j = 0; j < rm.GetnCols; j++)
{
result[i, j] = rm[i, j] + cn;
}
}
return result;
}
public static RMatrix operator +(double cn, RMatrix rm)
{
RMatrix result = new(rm.GetnRows, rm.GetnCols);
for (int i = 0; i < rm.GetnRows; i++)
{
for (int j = 0; j < rm.GetnCols; j++)
{
result[i, j] = rm[i, j] + cn;
}
}
return result;
}
public static RMatrix operator +(RVector rv, RMatrix rm)
{
if (rv.GetRVectorSize != rm.GetnRows)
{
throw new Exception("The dimensions of vector and matrix must be the same!");
}
int nvals = rv.GetRVectorSize;
RMatrix result = new(nvals, 1);
for (int i = 0; i < nvals; i++)
{
result[i, 0] = rv[i] + rm[i, 0];
}
return result;
}
public static RMatrix operator -(RMatrix rm)
{
for (int i = 0; i < rm.GetnRows; i++)
{
for (int j = 0; j < rm.GetnCols; j++)
{
rm[i, j] = -rm[i, j];
}
}
return rm;
}
public static RMatrix operator -(RMatrix rm1, RMatrix rm2)
{
if (!RMatrix.CompareDimension(rm1, rm2))
{
throw new Exception("The dimensions of two matrices must be the same!");
}
RMatrix result = new(rm1.GetnRows, rm1.GetnCols);
for (int i = 0; i < rm1.GetnRows; i++)
{
for (int j = 0; j < rm1.GetnCols; j++)
{
result[i, j] = rm1[i, j] - rm2[i, j];
}
}
return result;
}
public static RMatrix operator -(RMatrix rm, double cn)
{
RMatrix result = new(rm.GetnRows, rm.GetnCols);
for (int i = 0; i < rm.GetnRows; i++)
{
for (int j = 0; j < rm.GetnCols; j++)
{
result[i, j] = rm[i, j] - cn;
}
}
return result;
}
public static RMatrix operator -(double cn, RMatrix rm)
{
RMatrix result = new(rm.GetnRows, rm.GetnCols);
for (int i = 0; i < rm.GetnRows; i++)
{
for (int j = 0; j < rm.GetnCols; j++)
{
result[i, j] = cn - rm[i, j];
}
}
return result;
}
public static RMatrix operator *(RMatrix rm, double cn)
{
RMatrix result = new(rm.GetnRows, rm.GetnCols);
for (int i = 0; i < rm.GetnRows; i++)
{
for (int j = 0; j < rm.GetnCols; j++)
{
result[i, j] = rm[i, j] * cn;
}
}
return result;
}
public static RMatrix operator *(double cn, RMatrix rm)
{
RMatrix result = new(rm.GetnRows, rm.GetnCols);
for (int i = 0; i < rm.GetnRows; i++)
{
for (int j = 0; j < rm.GetnCols; j++)
{
result[i, j] = rm[i, j] * cn;
}
}
return result;
}
public static RMatrix operator *(RMatrix m1, RMatrix m2)
{
if (m1.GetnCols != m2.GetnRows)
{
throw new Exception("# columns of the matrix 1 must = # columns of the matrix 2");
}
double ctmp;
int nrows1 = m1.GetnRows;
int ncols2 = m2.GetnCols;
RMatrix result = new(nrows1, ncols2);
//for (int i = 0; i < cm1.GetnRows; i++)
//{
// for (int j = 0; j < cm2.GetnCols; j++)
// {
// ctmp = result[i, j];
// for (int k = 0; k < cm2.GetnRows; k++)
// {
// ctmp += cm1[i, k] * cm2[k, j];
// }
// result[i, j] = ctmp;
// }
//}
for (int i = 0; i < nrows1; i++)
{
for (int j = 0; j < ncols2; j++)
{
ctmp = 0.0;
RVector row = m1.GetRowVector(i);
RVector col = m2.GetColVector(j);
for (int k = 0; k < m1.GetnCols; k++)
{
ctmp += row[k] * col[k];
}
result[i, j] = ctmp;
}
}
return result;
}
public static RMatrix operator *(RMatrix rm, RVector rv)
{
int num_rows = rm.GetnRows;
int num_cols = rv.GetRVectorSize;
RMatrix Result = new(num_rows, num_cols);
for (int i = 0; i < num_rows; i++)
{
for (int j = 0; j < num_cols; j++)
{
Result[i, j] = rm[i, 0] * rv[j];
}
}
return Result;
}
public static RMatrix operator /(RMatrix rm, double cn)
{
RMatrix result = new(rm.GetnRows, rm.GetnCols);
for (int i = 0; i < rm.GetnRows; i++)
{
for (int j = 0; j < rm.GetnCols; j++)
{
result[i, j] = rm[i, j] / cn;
}
}
return result;
}
public static RMatrix operator /(double cn, RMatrix rm)
{
RMatrix result = new(rm.GetnRows, rm.GetnCols);
for (int i = 0; i < rm.GetnRows; i++)
{
for (int j = 0; j < rm.GetnCols; j++)
{
result[i, j] = rm[i, j] / cn;
}
}
return result;
}
// Methods
// Checks for a square matrix where #rows = #cols
public bool IsSquared()
{
if (nRows == nCols)
return true;
else
return false;
}
// Compares the dimension of two real matrices
public static bool CompareDimension(RMatrix cm1, RMatrix cm2)
{
if (cm1.GetnRows == cm2.GetnRows && cm1.GetnCols == cm2.GetnCols)
return true;
else
return false;
}
// Makes a clone copy of a real matrix
public RMatrix Clone()
{
RMatrix cm = new(matrix);
cm.matrix = (double[,])matrix.Clone();
return cm;
}
object ICloneable.Clone()
{
return Clone();
}
// Sets up a call to calculate the transpose of a real matrix
public RMatrix GetTranspose()
{
RMatrix ct = this;
if (IsSquared())
{
ct.Transpose();
return ct;
}
else
{
RMatrix nt = Transpose(ct);
return nt;
}
}
// Calculates the transpose of a real matrix
public void Transpose()
{
RMatrix cm = new(nCols, nRows);
for (int i = 0; i < nRows; i++)
{
for (int j = 0; j < nCols; j++)
{
cm[j, i] = matrix[i, j];
}
}
for (int i = 0; i < nRows; i++)
{
for (int j = 0; j < nCols; j++)
{
matrix[i, j] = cm[i, j];
}
}
}
public static RMatrix Transpose(RMatrix cm)
{
RMatrix nm = new(cm.nCols, cm.nRows);
for (int i = 0; i < cm.nRows; i++)
{
for (int j = 0; j < cm.nCols; j++)
{
nm[j, i] = cm.matrix[i, j];
}
}
return nm;
}
// Calculates the trace of a real matrix
public double GetTrace()
{
double sum_of_diag = 0.0;
for (int i = 0; i < nRows; i++)
{
for (int j = 0; j < nCols; j++)
{
if (i == j)
sum_of_diag += matrix[i, j];
}
}
return sum_of_diag;
}
// Extracts a row vector from a real matrix at specified row
public RVector GetRowVector(int m)
{
if (m < 0 || m > nRows)
{
throw new Exception("m-th row is out of range!");
}
RVector RowVector = new(nCols);
for (int i = 0; i < nCols; i++)
{
RowVector[i] = matrix[m, i];
}
return RowVector;
}
public RVector GetRowVector(int m, int ns, int ne)
{
if (m < 0 || m > nRows) { throw new Exception("m-th row is out of range!"); }
if (ns > nCols || ne > nCols) { throw new Exception("request for section is out of range!"); }
if (ne < ns)
{
int nh = ns;
ns = ne;
ne = nh;
}
int delta = ne - ns;
RVector RowVector = new(delta);
for (int i = ns; i < ne; i++)
{
RowVector[i - ns] = matrix[m, i];
}
return RowVector;
}
// Extracts a column vector from a real matrix at specified column
public RVector GetColVector(int m)
{
if (m < 0 || m > nCols)
{
throw new Exception("n-th col is out of range!");
}
RVector ColCVector = new(nRows);
for (int i = 0; i < nRows; i++)
{
ColCVector[i] = matrix[i, m];
}
return ColCVector;
}
public RVector GetColVector(int m, int ns, int ne)
{
if (m < 0 || m > nCols) { throw new Exception("m-th row is out of range!"); }
if (ns > nRows || ne > nRows) { throw new Exception("request for section is out of range!"); }
if (ne < ns)
{
int nh = ns;
ns = ne;
ne = nh;
}
int delta = ne - ns;
RVector ColCVector = new(delta);
for (int i = ns; i < ne; i++)
{
ColCVector[i - ns] = matrix[i, m];
}
return ColCVector;
}
// Swaps specificed real matrix row with another row
public RMatrix SwapMatrixRow(int m, int n)
{
double ctemp = 0.0;
for (int i = 0; i < nCols; i++)
{
ctemp = matrix[m, i];
matrix[m, i] = matrix[n, i];
matrix[n, i] = ctemp;
}
return new RMatrix(matrix);
}
// Swaps specificed real matrix column with another column
public RMatrix SwapMatrixColumn(int m, int n)
{
double ctemp = 0.0;
for (int i = 0; i < nRows; i++)
{
ctemp = matrix[i, m];
matrix[i, m] = matrix[i, n];
matrix[i, n] = ctemp;
}
return new RMatrix(matrix);
}
// Calculates the transform of a real matrix
public static RVector RTransform(RMatrix rm, RVector rv)
{
RVector result = new(rv.GetRVectorSize);
if (!rm.IsSquared())
{
throw new Exception("The matrix must be squared!");
}
if (rm.GetnCols != rv.GetRVectorSize)
{
throw new Exception("Vector size must = # rows in matrix");
}
for (int i = 0; i < rm.GetnRows; i++)
{
result[i] = 0.0;
for (int j = 0; j < rm.GetnCols; j++)
{
result[i] += rm[i, j] * rv[j];
}
}
return result;
}
public static RVector RTransform(RVector rv, RMatrix rm)
{
RVector result = new(rv.GetRVectorSize);
if (!rm.IsSquared())
{
throw new Exception("The matrix must be squared!");
}
if (rm.GetnRows != rv.GetRVectorSize)
{
throw new Exception("Vector size must = # rows in matrix");
}
for (int i = 0; i < rm.GetnRows; i++)
{
result[i] = 0.0;
for (int j = 0; j < rm.GetnCols; j++)
{
result[i] += rv[j] * rm[j, i];
}
}
return result;
}
public static RMatrix RTransform(RVector rv1, RVector rv2)
{
if (rv1.GetRVectorSize != rv2.GetRVectorSize)
{
throw new Exception("The vectors must have the same size!");
}
RMatrix result = new(rv1.GetRVectorSize, rv1.GetRVectorSize);
for (int i = 0; i < rv1.GetRVectorSize; i++)
{
for (int j = 0; j < rv1.GetRVectorSize; j++)
{
result[j, i] = rv1[i] * rv2[j];
}
}
return result;
}
// Calculates the determinant of a real matrix
public static double Determinant(RMatrix rm)
{
double result = 0.0;
if (!rm.IsSquared())
{
throw new Exception("The matrix must be squared!");
}
if (rm.GetnRows == 1)
result = rm[0, 0];
else
{
for (int i = 0; i < rm.GetnRows; i++)
{
double mDeterm = Determinant(Minor(rm, 0, i));
result += Math.Pow(-1, i) * rm[0, i] * mDeterm;
}
}
return result;
}
// Calculates the minor of a real matrix at specified row and column
public static RMatrix Minor(RMatrix rm, int row, int col)
{
RMatrix cmm = new(rm.GetnRows - 1, rm.GetnCols - 1);
int ii = 0, jj = 0;
for (int i = 0; i < rm.GetnRows; i++)
{
if (i == row)
continue;
jj = 0;
for (int j = 0; j < rm.GetnCols; j++)
{
if (j == col)
continue;
cmm[ii, jj] = rm[i, j];
jj++;
}
ii++;
}
return cmm;
}
// Calculates the adjoint of a real matrix
public static RMatrix Adjoint(RMatrix rm)
{
if (!rm.IsSquared())
{
throw new Exception("The matrix must be squared!");
}
RMatrix ma = new(rm.GetnRows, rm.GetnCols);
for (int i = 0; i < rm.GetnRows; i++)
{
for (int j = 0; j < rm.GetnCols; j++)
{
ma[i, j] = Math.Pow(-1, i + j) * Determinant(Minor(rm, i, j));
}
}
return ma.GetTranspose();
}
// Calculates the inverse of a real matrix
public static RMatrix Inverse(RMatrix rm)
{
double Dm = Determinant(rm);
if (Dm == 0)
{
throw new Exception("Cannot inverse a matrix with 0 determinant!");
}
return Adjoint(rm) / Dm;
}
public static RMatrix InverseAccurate(RMatrix rm)
{
double Dm = Determinant(rm);
if (Dm == 0)
{
throw new Exception("Cannot inverse a matrix with 0 determinant!");
}
int nrows = rm.GetnRows;
int ncols = rm.GetnCols;
RMatrix InvMat = new(nrows, ncols);
bool rowsum = false;
RMatrix D0 = new(nrows, ncols), D0B, I = new(nrows, ncols), C;
//double start_pow = 0.1;
//double delta_pow = 0.01;
int counter = 0;
while (!rowsum)
{
double themax = Max(rm);
double apow = Math.Log10(Math.Abs(themax)) + 5;
double the_pow = apow; // start_pow - counter * delta_pow;
double multiplier = Math.Pow(10, -the_pow);
D0 = multiplier * Inverse(rm);
D0B = D0 * rm;
I = rm.IdentityMatrix();
C = I - D0B;
RVector row_sum = new(nrows);
for (int i = 0; i < nrows; i++)
{
for (int j = 0; j < ncols; j++)
{
row_sum[i] += C[i, j];
}
}
int rowcounter = 0;
for (int i = 0; i < nrows; i++)
{
if (row_sum[i] < 1.0) { rowcounter++; }
}
if (rowcounter == nrows - 1) { rowsum = true; }
counter++;
}
if (rowsum == true)
{
RMatrix Dnew = new(nrows, ncols);
for (int c = 1; c < 30; c++)
{
Dnew = (2 * I - D0 * rm) * D0;
if (Dnew == D0) { break; }
else { D0 = Dnew; }
}
InvMat = Dnew;
}
return InvMat;
}
// Replaces the n-th row of a real matrix with contents of a real vector
public RMatrix ReplaceRow(RVector rv, int m)
{
if (m < 0 || m > nRows)
{
throw new Exception("m-th row is out of range!");
}
if (rv.GetRVectorSize != nCols)
{
throw new Exception("Vector size is out of range!");
}
for (int i = 0; i < nCols; i++)
{
matrix[m, i] = rv[i];
}
return new RMatrix(matrix);
}
public RMatrix ReplaceRow(RVector rv, int m, int ns, int ne)
{
if (m < 0 || m > nRows)
{
throw new Exception("m-th row is out of range!");
}
//if (rv.GetRVectorSize != nCols)
//{
// throw new Exception("Vector size is out of range!");
//}
for (int i = ns; i < ne; i++)
{
matrix[m, i] = rv[i - ns];
}
return new RMatrix(matrix);
}
// Replaces the n-th column of a real matrix with contents of a real vector
public RMatrix ReplaceCol(RVector rv, int n)
{
if (n < 0 || n > nCols)
{
throw new Exception("n-th col is out of range!");
}
if (rv.GetRVectorSize != nRows)
{
throw new Exception("Vector size is out of range!");
}
for (int i = 0; i < nRows; i++)
{
matrix[i, n] = rv[i];
}
return new RMatrix(matrix);
}
public RMatrix ReplaceCol(RVector rv, int n, int ns, int ne)
{
if (n < 0 || n > nCols)
{
throw new Exception("n-th col is out of range!");
}
//if (rv.GetRVectorSize != nRows)
//{
// throw new Exception("Vector size is out of range!");
//}
for (int i = ns; i < ne; i++)
{
matrix[i, n] = rv[i - ns];
}
return new RMatrix(matrix);
}
public static (int rval, int cval) IndexOf(RMatrix rm, double c)
{
int ifound = -1, jfound = -1;
for (int i = 0; i < rm.GetnRows; i++)
{
for (int j = 0; j < rm.GetnCols; j++)
{
if (rm[i, j] == c)
{
ifound = i;
jfound = j;
}
}
}
return (ifound, jfound);
}
public static RMatrix GetDiagonal(RMatrix rm)
{
int m = rm.GetnRows;
int n = rm.GetnCols;
RMatrix Result = new(m, n);
for (int i = 0; i <= m - 1; i++)
{
for (int j = 0; j <= n - 1; j++)
{
if (i == j)
{
Result[i, j] = rm[i, j];
}
}
}
return Result;
}
public static void Cholesky_Decomposition(RMatrix rm)
{
int n = rm.GetnRows;
RMatrix lower = new(n, n);
// Decomposing a matrix
// into Lower Triangular
for (int i = 0; i < n; i++)
{
for (int j = 0; j <= i; j++)
{
double sum = 0;
// summation for diagonals
if (j == i)
{
for (int k = 0; k < j; k++) { sum += (int)Math.Pow(lower[j, k], 2); }
lower[j, j] = Math.Sqrt(rm[j, j] - sum);
}
else
{
// Evaluating L(i, j)
// using L(j, j)
for (int k = 0; k < j; k++) { sum += lower[i, k] * lower[j, k]; }
lower[i, j] = (rm[i, j] - sum) / lower[j, j];
}
}
}
}
public static double Max(RMatrix rm)
{
int nrows = rm.GetnRows;
int ncols = rm.GetnCols;
double max = 0.0;
for (int i = 0; i < nrows; i++)
{
for (int j = 0; j < ncols; j++)
{
if (Math.Abs(rm[i, j]) > max) { max = rm[i, j]; }
}
}
return max;
}
public static double MinDiagonal(RMatrix rm)
{
int nrows = rm.GetnRows;
int ncols = rm.GetnCols;
double min = Math.Abs(rm[0, 0]);
for (int i = 0; i < nrows; i++)
{
for (int j = 0; j < ncols; j++)
{
if (i == j && Math.Abs(rm[i, j]) < min) { min = rm[i, j]; }
}
}
return min;
}
public static RVector DotProduct(RMatrix rm, RVector rv)
{
int nrows = rm.GetnRows;
int ncols1 = rm.GetnCols;
int nrows2 = rv.GetRVectorSize;
RVector result = new(nrows2);
for (int i = 0; i < nrows; i++)
{
double col_sum = 0;
for (int j = 0; j < ncols1; j++)
{
double p = rm[i, j] * rv[j];
col_sum += p;
}
result[i] = col_sum;
}
return result;
}
public static RMatrix GaussSeidel(RMatrix A, RVector x0, RMatrix b)
{
double tol = 1.0e-8;
bool stop = false;
int iter = 0;
double scale = Math.Pow(10, -6);
int N = 300;
int nrows = A.GetnRows; //2; //
int ncols = A.GetnCols; //2; //
RMatrix p = new(nrows, 1);
RVector x = new(nrows);
RVector y = new(nrows); ;
double sum = 0.0;
//A = new RMatrix(2, 2);
//A[0, 0] = 16;
//A[0, 1] = 3;
////A[0, 2] = 2;
//A[1, 0] = 7;
//A[1, 1] = -11;
////A[1, 2] = 1;
////A[2, 0] = 1;
////A[2, 1] = 1;
////A[2, 2] = 3;
//b = new RMatrix(3, 1);
//b[0, 0] = 11;
//b[1, 0] = 13;
////b[2, 0] = 3;
//x[0] = 0;
//x[1] = 0;
////x[2] = 0;
for (int m = 0; m < nrows; m++) { y[m] = 0.0; }
while (!stop && iter < N)
{
for (int i = 0; i < nrows; i++)
{
sum = b[i, 0];
for (int j = 0; j < ncols; j++)
{
if (j != i) { sum -= A[i, j] * x[j]; }
}
x[i] = sum / A[i, i];
}
int check_sum = 0;
for (int k = 0; k < nrows; k++)
{
double check_term = Math.Abs((x[k] - y[k]) / x[k]);
if (check_term < tol) { check_sum += 1; }
if (check_sum >= nrows) { stop = true; }
else { y[k] = x[k]; }
}
iter += 1;
}
//RMatrix D = GetDiagonal(A);