This repository was archived by the owner on May 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathMatrix.cs
1041 lines (988 loc) · 36.9 KB
/
Matrix.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 RCNet.Extensions;
using RCNet.MathTools.VectorMath;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace RCNet.MathTools.MatrixMath
{
/// <summary>
/// Implements the real matrix.
/// </summary>
/// <remarks>
/// It does not support the sparse matrix format.
/// </remarks>
[Serializable]
public class Matrix
{
//Constants
//Attributes
/// <summary>
/// Matrix data stored in array of arrays of double.
/// </summary>
protected double[][] _data;
//Constructors
/// <summary>
/// Creates an initialized instance.
/// </summary>
/// <param name="numOfRows">The number of rows.</param>
/// <param name="numOfCols">The number of columns.</param>
/// <param name="flatData">The data to be copied into the matrix (optional). If used, data must be in a flat format.</param>
public Matrix(int numOfRows, int numOfCols, double[] flatData = null)
{
_data = new double[numOfRows][];
Parallel.For(0, numOfRows, row =>
{
_data[row] = new double[numOfCols];
_data[row].Populate(0);
});
if (flatData != null)
{
Set(flatData);
}
return;
}
/// <summary>
/// Creates an initialized instance.
/// </summary>
/// <param name="data">The matrix data.</param>
/// <param name="copy">Specifies whether to use a copy of the data or whether to use the data directly.</param>
public Matrix(double[][] data, bool copy = true)
{
_data = copy ? data.Clone2D() : data;
return;
}
/// <summary>
/// The deep copy constructor.
/// </summary>
/// <param name="sourceMatrix">The source matrix.</param>
public Matrix(Matrix sourceMatrix)
: this(sourceMatrix._data, true)
{
return;
}
//Properties
/// <summary>
/// The matrix data.
/// </summary>
public double[][] Data { get { return _data; } }
/// <summary>
/// The number of matrix rows.
/// </summary>
public int NumOfRows { get { return _data.Length; } }
/// <summary>
/// The number of matrix columns.
/// </summary>
public int NumOfCols { get { return _data[0].Length; } }
/// <summary>
/// The size of the matrix = (NumOfRows * NumOfCols)
/// </summary>
public int Size { get { return NumOfRows * NumOfCols; } }
/// <summary>
/// Indicates whether the matrix is a vector.
/// </summary>
public bool IsVector { get { return (NumOfRows == 1 || NumOfCols == 1); } }
/// <summary>
/// Indicates whether the matrix is the singular matrix.
/// </summary>
public bool IsSingular
{
get
{
for (int i = 0; i < NumOfRows; i++)
{
for (int j = 0; j < NumOfCols; j++)
{
if (_data[i][j] != 0)
{
return false;
}
}
}
return true;
}
}
/// <summary>
/// Indicates the matrix is the square matrix.
/// </summary>
public bool IsSquareMatrix
{
get
{
return (NumOfCols == NumOfRows);
}
}
//Methods
//Static methods
/// <summary>
/// Calculates the hypotenuse.
/// </summary>
/// <remarks>
/// https://en.wikipedia.org/wiki/Hypot
/// </remarks>
/// <param name="x">The x value.</param>
/// <param name="y">The y value.</param>
public static double Hypotenuse(double x, double y)
{
double hypot = 0d;
if (Math.Abs(x) > Math.Abs(y))
{
hypot = Math.Abs(x) * Math.Sqrt(1d + (y / x).Power(2));
}
else if (y != 0)
{
hypot = Math.Abs(y) * Math.Sqrt(1d + (x / y).Power(2));
}
return hypot;
}
/// <summary>
/// Creates the matrix having the single column and multiple rows.
/// </summary>
/// <param name="data">The values.</param>
public static Matrix CreateSingleColumnMatrix(double[] data)
{
Matrix result = new Matrix(data.Length, 1);
result.SetCol(0, data);
return result;
}
/// <summary>
/// Creates the matrix having the single row and multiple columns.
/// </summary>
/// <param name="data">The values.</param>
public static Matrix CreateSingleRowMatrix(double[] data)
{
Matrix result = new Matrix(1, data.Length);
result.SetRowValues(0, data);
return result;
}
//Instance methods
/// <summary>
/// Fills the whole matrix with specified value.
/// </summary>
/// <param name="value">The value to be filled in.</param>
public void Set(double value = 0)
{
_data.Populate(value);
return;
}
/// <summary>
/// Fills the whole matrix with data stored in the 1D array (the flat format is required).
/// </summary>
/// <param name="flatData">The data in a flat format.</param>
public void Set(double[] flatData)
{
int dataIndex = 0;
for (int i = 0; i < NumOfRows; i++)
{
for (int j = 0; j < NumOfCols; j++)
{
_data[i][j] = flatData[dataIndex++];
}
}
return;
}
/// <summary>
/// Copies all data from the source matrix.
/// </summary>
/// <param name="source">The source matrix.</param>
public void Set(Matrix source)
{
Parallel.For(0, NumOfRows, i =>
{
for (int j = 0; j < NumOfCols; j++)
{
_data[i][j] = source._data[i][j];
}
});
return;
}
/// <summary>
/// Fills the specified row with the constant value.
/// </summary>
/// <param name="row">The row index.</param>
/// <param name="value">The value to be set.</param>
public void SetRowValues(int row, double value = 0)
{
_data[row].Populate(value);
return;
}
/// <summary>
/// Copies the values from an array into the specified matrix row.
/// </summary>
/// <param name="row">The row index.</param>
/// <param name="data">The array.</param>
public void SetRowValues(int row, double[] data)
{
data.CopyTo(_data[row], 0);
return;
}
/// <summary>
/// Fills the specified column with the constant value.
/// </summary>
/// <param name="col">The column index.</param>
/// <param name="value">The value to be set.</param>
public void SetCol(int col, double value = 0)
{
for (int i = 0; i < _data.Length; i++)
{
_data[i][col] = value;
}
return;
}
/// <summary>
/// Copies the values from an array into the specified matrix column.
/// </summary>
/// <param name="col">The column index.</param>
/// <param name="data">The array.</param>
public void SetCol(int col, double[] data)
{
for (int i = 0; i < data.Length; i++)
{
_data[i][col] = data[i];
}
return;
}
/// <summary>
/// Copies all data from the source matrix starting at the specified upper left corner row and col position.
/// </summary>
/// <param name="fromRow">The upper left row index.</param>
/// <param name="fromCol">The upper left column index.</param>
/// <param name="source">The source matrix.</param>
public void SetSubMatrix(int fromRow, int fromCol, Matrix source)
{
for (int i = 0; i <= source.NumOfRows; i++)
{
for (int j = 0; j <= source.NumOfCols; j++)
{
_data[fromRow + i][fromCol + j] = source._data[i][j];
}
}
return;
}
/// <summary>
/// Copies all data into the 1D array in a flat format.
/// </summary>
/// <param name="flatData">The array to be data copied in.</param>
public void CopyFlatData(double[] flatData)
{
int dataIndex = 0;
for (int i = 0; i < NumOfRows; i++)
{
for (int j = 0; j < NumOfCols; j++)
{
flatData[dataIndex++] = _data[i][j];
}
}
return;
}
/// <summary>
/// Gets the data in a flat format.
/// </summary>
public double[] GetFlatData()
{
double[] flatData = new double[Size];
CopyFlatData(flatData);
return flatData;
}
/// <summary>
/// Creates a submatrix.
/// </summary>
/// <param name="fromRow">The start row index.</param>
/// <param name="toRow">The end row index.</param>
/// <param name="fromCol">The start column index.</param>
/// <param name="toCol">The end column index.</param>
public Matrix CreateSubMatrix(int fromRow, int toRow, int fromCol, int toCol)
{
Matrix resultMatrix = new Matrix(toRow - fromRow + 1, toCol - fromCol + 1);
for (int i = fromRow; i <= toRow; i++)
{
for (int j = fromCol; j <= toCol; j++)
{
resultMatrix._data[i - fromRow][j - fromCol] = _data[i][j];
}
}
return resultMatrix;
}
/// <summary>
/// Gets a clone of the inner data.
/// </summary>
public double[][] GetDataClone()
{
return _data.Clone2D();
}
/// <summary>
/// Creates the deep copy.
/// </summary>
public Matrix DeepClone()
{
return new Matrix(this);
}
/// <inheritdoc/>
public override int GetHashCode()
{
return base.GetHashCode();
}
/// <inheritdoc/>
public override bool Equals(object obj)
{
if (obj == null) return false;
Matrix cmpMatrix = obj as Matrix;
if (NumOfCols != cmpMatrix.NumOfCols || NumOfRows != cmpMatrix.NumOfRows)
{
return false;
}
for (int i = 0; i < NumOfRows; i++)
{
for (int j = 0; j < NumOfCols; j++)
{
if (_data[i][j] != cmpMatrix._data[i][j])
{
return false;
}
}
}
return true;
}
/// <summary>
/// Computes A + B.
/// </summary>
/// <param name="A">The matrix A.</param>
/// <param name="B">The matrix B.</param>
/// <returns>The resulting matrix.</returns>
public static Matrix Add(Matrix A, Matrix B)
{
int rowsA = A.NumOfRows;
int colsA = A.NumOfCols;
int rowsB = B.NumOfRows;
int colsB = B.NumOfCols;
if (colsA != colsB || rowsA != rowsB)
{
throw new InvalidOperationException($"Dimensions of A must equal to dimensions of B.");
}
double[][] resultData = new double[rowsA][];
var rangePartitioner = Partitioner.Create(0, rowsA);
Parallel.ForEach(rangePartitioner, range =>
{
double[] rowDataResult, rowDataA, rowDataB;
for (int i = range.Item1; i < range.Item2; i++)
{
rowDataResult = new double[colsA];
rowDataA = A._data[i];
rowDataB = B._data[i];
for (int j = 0; j < colsA; j++)
{
rowDataResult[j] = rowDataA[j] + rowDataB[j];
}
resultData[i] = rowDataResult;
}
});
return new Matrix(resultData, false);
}
/// <inheritdoc cref="Add(Matrix, Matrix)"/>
public static Matrix operator +(Matrix A, Matrix B)
{
return Add(A, B);
}
/// <summary>
/// Adds the matrix B.
/// </summary>
/// <param name="B">The matrix B.</param>
public void Add(Matrix B)
{
int rowsB = B.NumOfRows;
int colsB = B.NumOfCols;
if (NumOfCols != colsB || NumOfRows != rowsB)
{
throw new InvalidOperationException($"Dimensions of B must equal to dimensions of this matrix.");
}
var rangePartitioner = Partitioner.Create(0, rowsB);
Parallel.ForEach(rangePartitioner, range =>
{
double[] rowData, rowDataB;
for (int i = range.Item1; i < range.Item2; i++)
{
rowData = _data[i];
rowDataB = B._data[i];
for (int j = 0; j < colsB; j++)
{
rowData[j] += rowDataB[j];
}
}
});
return;
}
/// <summary>
/// Adds a scalar to main diagonal of the square matrix A.
/// </summary>
/// <param name="A">The matrix.</param>
/// <param name="s">The scalar.</param>
/// <returns>The resulting matrix.</returns>
public static Matrix AddScalarToDiagonal(Matrix A, double s)
{
int rowsA = A.NumOfRows;
int colsA = A.NumOfCols;
if (rowsA != colsA)
{
throw new InvalidOperationException($"Matrix A must be a square matrix (rows dimension = columns dimension).");
}
double[][] resultData = new double[rowsA][];
var rangePartitioner = Partitioner.Create(0, rowsA);
Parallel.ForEach(rangePartitioner, range =>
{
for (int i = range.Item1; i < range.Item2; i++)
{
resultData[i] = (double[])A._data[i].Clone();
resultData[i][i] += s;
}
});
return new Matrix(resultData, false);
}
/// <summary>
/// Adds a scalar to main diagonal.
/// </summary>
/// <param name="s">The scalar.</param>
public void AddScalarToDiagonal(double s)
{
if (!IsSquareMatrix)
{
throw new InvalidOperationException($"Matrix must be a square matrix (rows dimension = columns dimension)");
}
var rangePartitioner = Partitioner.Create(0, NumOfRows);
Parallel.ForEach(rangePartitioner, range =>
{
for (int i = range.Item1; i < range.Item2; i++)
{
_data[i][i] += s;
}
});
return;
}
/// <summary>
/// Computes A - B.
/// </summary>
/// <param name="A">The matrix A.</param>
/// <param name="B">The matrix B.</param>
/// <returns>The resulting matrix.</returns>
public static Matrix Substract(Matrix A, Matrix B)
{
int rowsA = A.NumOfRows;
int colsA = A.NumOfCols;
int rowsB = B.NumOfRows;
int colsB = B.NumOfCols;
if (colsA != colsB || rowsA != rowsB)
{
throw new InvalidOperationException($"Dimensions of A must equal to dimensions of B");
}
double[][] resultData = new double[rowsA][];
var rangePartitioner = Partitioner.Create(0, rowsA);
Parallel.ForEach(rangePartitioner, range =>
{
double[] rowDataResult, rowDataA, rowDataB;
for (int i = range.Item1; i < range.Item2; i++)
{
rowDataResult = new double[colsA];
rowDataA = A._data[i];
rowDataB = B._data[i];
for (int j = 0; j < colsA; j++)
{
rowDataResult[j] = rowDataA[j] - rowDataB[j];
}
resultData[i] = rowDataResult;
}
});
return new Matrix(resultData, false);
}
/// <inheritdoc cref="Substract(Matrix, Matrix)"/>
public static Matrix operator -(Matrix A, Matrix B)
{
return Substract(A, B);
}
/// <summary>
/// Substracts the matrix B.
/// </summary>
/// <param name="B">The matrix B.</param>
public void Substract(Matrix B)
{
int rowsB = B.NumOfRows;
int colsB = B.NumOfCols;
if (NumOfCols != colsB || NumOfRows != rowsB)
{
throw new InvalidOperationException($"Dimensions of B must equal to dimensions of this matrix");
}
var rangePartitioner = Partitioner.Create(0, rowsB);
Parallel.ForEach(rangePartitioner, range =>
{
double[] rowData, rowDataB;
for (int i = range.Item1; i < range.Item2; i++)
{
rowData = _data[i];
rowDataB = B._data[i];
for (int j = 0; j < colsB; j++)
{
rowData[j] -= rowDataB[j];
}
}
});
return;
}
/// <summary>
/// Computes A * B.
/// </summary>
/// <param name="A">The matrix A.</param>
/// <param name="B">The matrix B.</param>
/// <returns>The resulting matrix.</returns>
public static Matrix Multiply(Matrix A, Matrix B)
{
int rowsA = A.NumOfRows;
int colsA = A.NumOfCols;
int rowsB = B.NumOfRows;
int colsB = B.NumOfCols;
if (colsA != rowsB)
{
throw new InvalidOperationException($"Number of columns of A must be equal to number of rows of B.");
}
var rangePartitioner = Partitioner.Create(0, rowsA);
double[][] resultData = new double[rowsA][];
Parallel.ForEach(rangePartitioner, range =>
{
double[] rowDataA, rowDataB, rowDataResult;
for (int i = range.Item1; i < range.Item2; i++)
{
rowDataA = A._data[i];
rowDataResult = new double[colsB];
rowDataResult.Populate(0);
for (int j = 0; j < rowsB; j++)
{
rowDataB = B._data[j];
double valA = rowDataA[j];
for (int k = 0; k < colsB; k++)
{
rowDataResult[k] += valA * rowDataB[k];
}
}
resultData[i] = rowDataResult;
};
});
return new Matrix(resultData, false);
}
/// <inheritdoc cref="Multiply(Matrix, Matrix)"/>
public static Matrix operator *(Matrix A, Matrix B)
{
return Multiply(A, B);
}
/// <summary>
/// Multiplies a matrix by the vector.
/// </summary>
/// <param name="A">The matrix.</param>
/// <param name="v">The vector.</param>
/// <returns>The resulting vector.</returns>
public static Vector Multiply(Matrix A, Vector v)
{
int rowsA = A.NumOfRows;
int colsA = A.NumOfCols;
if (colsA != v.Length)
{
throw new InvalidOperationException($"Number of columns of A must be equal to length of vector v.");
}
double[] resultData = new double[rowsA];
double[] vData = v.Data;
int vDataLength = vData.Length;
var rangePartitioner = Partitioner.Create(0, rowsA);
Parallel.ForEach(rangePartitioner, range =>
{
for (int i = range.Item1; i < range.Item2; i++)
{
double[] dataRowA = A._data[i];
double sum = 0;
for (int j = 0; j < vDataLength; j++)
{
sum += dataRowA[j] * vData[j];
}
resultData[i] = sum;
}
});
return new Vector(resultData, false);
}
/// <inheritdoc cref="Multiply(Matrix, Vector)"/>
public static Vector operator *(Matrix A, Vector v)
{
return Multiply(A, v);
}
/// <summary>
/// Multiplies by the vector.
/// </summary>
/// <param name="v">The vector.</param>
/// <returns>The resulting vector.</returns>
public Vector Multiply(Vector v)
{
if (NumOfCols != v.Length)
{
throw new InvalidOperationException($"Number of columns of the matrix must be equal to length of the vector.");
}
int rows = NumOfRows;
double[] resultData = new double[rows];
double[] vData = v.Data;
var rangePartitioner = Partitioner.Create(0, NumOfRows);
Parallel.ForEach(rangePartitioner, range =>
{
for (int i = range.Item1; i < range.Item2; i++)
{
double[] dataRow = _data[i];
double sum = 0;
for (int j = 0; j < rows; j++)
{
sum += dataRow[j] * vData[j];
}
resultData[i] = sum;
}
});
return new Vector(resultData, false);
}
/// <summary>
/// Multiplies a matrix by the scalar.
/// </summary>
/// <param name="A">The matrix.</param>
/// <param name="s">The scalar.</param>
/// <returns>The resulting matrix.</returns>
public static Matrix Multiply(Matrix A, double s)
{
int rowsA = A.NumOfRows;
int colsA = A.NumOfCols;
double[][] dataR = new double[rowsA][];
var rangePartitioner = Partitioner.Create(0, rowsA);
Parallel.ForEach(rangePartitioner, range =>
{
for (int i = range.Item1; i < range.Item2; i++)
{
double[] rowDataA = A._data[i];
double[] rowDataR = new double[colsA];
for (int j = 0; j < colsA; j++)
{
rowDataR[j] = rowDataA[j] * s;
}
dataR[i] = rowDataR;
}
});
return new Matrix(dataR, false);
}
/// <inheritdoc cref="Multiply(Matrix, double)"/>
public static Matrix operator *(Matrix A, double s)
{
return Multiply(A, s);
}
/// <summary>
/// Multiplies by the scalar.
/// </summary>
/// <param name="s">The scalar.</param>
public void Multiply(double s)
{
int cols = NumOfCols;
var rangePartitioner = Partitioner.Create(0, NumOfRows);
Parallel.ForEach(rangePartitioner, range =>
{
for (int i = range.Item1; i < range.Item2; i++)
{
double[] rowData = _data[i];
for (int j = 0; j < cols; j++)
{
rowData[j] *= s;
}
}
});
return;
}
/// <summary>
/// Transposes a matrix.
/// </summary>
/// <param name="A">The matrix to be transposed.</param>
public static Matrix Transpose(Matrix A)
{
int rowsA = A.NumOfRows;
int colsA = A.NumOfCols;
int rowsR = colsA;
int colsR = rowsA;
double[][] dataR = new double[rowsR][];
var rangePartitioner = Partitioner.Create(0, rowsR);
Parallel.ForEach(rangePartitioner, range =>
{
for (int i = range.Item1; i < range.Item2; i++)
{
double[] rowData = new double[colsR];
for (int j = 0; j < colsR; j++)
{
rowData[j] = A._data[j][i];
}
dataR[i] = rowData;
}
});
return new Matrix(dataR, false);
}
/// <summary>
/// Transposes this matrix.
/// </summary>
/// <returns>The resulting matrix.</returns>
public Matrix Transpose()
{
int rowsR = NumOfCols;
int colsR = NumOfRows;
double[][] dataR = new double[rowsR][];
var rangePartitioner = Partitioner.Create(0, rowsR);
Parallel.ForEach(rangePartitioner, range =>
{
for (int i = range.Item1; i < range.Item2; i++)
{
double[] rowData = new double[colsR];
for (int j = 0; j < colsR; j++)
{
rowData[j] = _data[j][i];
}
dataR[i] = rowData;
}
});
return new Matrix(dataR, false);
}
/// <summary>
/// Inverses this matrix.
/// </summary>
/// <remarks>
/// <para>
/// Function implements the algorithm originally proposed by Ahmad FAROOQ and Khan HAMID in the publication:
/// https://www.researchgate.net/publication/220337321_An_Efficient_and_Generic_Algorithm_for_Matrix_Inversion (An Efficient and Generic Algorithm for Matrix Inversion).
/// </para>
/// <para>
/// Additionaly was implemented flexible off-diagonal pivot selection using dictionary
/// approach to build final inverted matrix proposed by Hafsa Athar Jafree, Muhammad Imtiaz, Syed Inayatullah,
/// Fozia Hanif Khan and Tajuddin Nizami in the publication:
/// https://arxiv.org/ftp/arxiv/papers/1304/1304.6893.pdf (A space efficient flexible pivot selection approach to evaluate determinant and inverse of a matrix).
/// </para>
/// <para>
/// Additionaly was implemented parallel processing to improve the performance.
/// </para>
/// </remarks>
/// <param name="preferAccuracy">Specifies whether to favorite the accuracy (selects pivots having max abs values) over the execution speed (selects diagonal pivots).</param>
/// <returns>The resulting matrix.</returns>
public Matrix Inverse(bool preferAccuracy = true)
{
if (!IsSquareMatrix)
{
throw new InvalidOperationException($"Matrix must be square.");
}
//Dimension to be used within the function
int size = NumOfRows;
//Prepare ranges for parallel processing
var rangePartitioner = Partitioner.Create(0, size);
//Dictionary
//Computational dictionary matrix - a copy of this matrix in the beginning
Matrix dictMatrix = new Matrix(this);
//Rows
int[] dictRows = new int[size];
dictRows.Indices();
//Available pivot rows
List<int> availableDictPivotRows = new List<int>(dictRows);
//Cols
int[] dictCols = new int[size];
dictCols.Indices();
//Available pivot columns
List<int> availableDictPivotCols = new List<int>(dictCols);
double minPivotValue = 1e-20;
//Indicates that some changes were made in dictionary
bool dictChanged = false;
//Main loop
for (int n = 0; n < size; n++)
{
//Pivot
int pivotRow = -1;
int pivotCol = -1;
//Select pivot element
if (!preferAccuracy)
{
//Simply use diagonal element
pivotRow = n;
pivotCol = n;
//Validate pivot value
if (Math.Abs(dictMatrix._data[pivotRow][pivotCol]) < minPivotValue)
{
//Failed
throw new InvalidOperationException($"Absolute value of the diagonal Pivot at row {pivotRow} is too small. Pivot = {dictMatrix._data[pivotRow][pivotCol]}.");
}
}
else
{
//Find new Pivot element having highest absolute value
double selectedPivotValue = 0;
int selectedPivotRowListIdx = -1, selectedPivotColListIdx = -1;
for (int pivotRowListIdx = 0; pivotRowListIdx < availableDictPivotRows.Count; pivotRowListIdx++)
{
int row = availableDictPivotRows[pivotRowListIdx];
for (int pivotColListIdx = 0; pivotColListIdx < availableDictPivotCols.Count; pivotColListIdx++)
{
int col = availableDictPivotCols[pivotColListIdx];
double elemAbsValue = Math.Abs(dictMatrix._data[row][col]);
if (elemAbsValue >= minPivotValue && (pivotColListIdx == 0 || elemAbsValue > Math.Abs(selectedPivotValue)))
{
selectedPivotValue = dictMatrix._data[row][col];
selectedPivotRowListIdx = pivotRowListIdx;
pivotRow = row;
selectedPivotColListIdx = pivotColListIdx;
pivotCol = col;
}
}
}
//Test success of pivot selection
if (selectedPivotValue == 0)
{
//Pivot was not selected
throw new InvalidOperationException($"Can't select Pivot in step {n + 1}. No matrix element has enaugh absolute value >= {minPivotValue}.");
}
//Remove row from available pivot rows
availableDictPivotRows.RemoveAt(selectedPivotRowListIdx);
//Remove col from available pivot cols
availableDictPivotCols.RemoveAt(selectedPivotColListIdx);
}
//Pick up Pivot value
double pivot = dictMatrix._data[pivotRow][pivotCol];
//Update dictionary
if (pivotRow != n || pivotCol != n)
{
dictRows[pivotRow] = pivotCol;
dictCols[pivotCol] = pivotRow;
dictChanged = true;
}
//Pivot processing
//Pivot column elements
Parallel.ForEach(rangePartitioner, range =>
{
for (int i = range.Item1; i < range.Item2; i++)
{
if (i != pivotRow)
{
dictMatrix._data[i][pivotCol] /= -pivot;
}
}
});
//Whole matrix except pivot row and column elements
Parallel.ForEach(rangePartitioner, range =>
{
for (int i = range.Item1; i < range.Item2; i++)
{
if (i != pivotRow)
{
for (int j = 0; j < size; j++)
{
if (j != pivotCol)
{
dictMatrix._data[i][j] += dictMatrix._data[pivotRow][j] * dictMatrix._data[i][pivotCol];
}
}
}
}
});
;
//Pivot row elements
Parallel.ForEach(rangePartitioner, range =>
{
for (int i = range.Item1; i < range.Item2; i++)
{
if (i != pivotCol)
{
dictMatrix._data[pivotRow][i] /= pivot;
}
}
});
;
//Pivot element
dictMatrix._data[pivotRow][pivotCol] = 1d / pivot;
}
//Result finalization
if (!dictChanged)
{
//No transpositionings so use directly IM as the result
return dictMatrix;
}
else
{
//Use dictionary and build resulting matrix
Matrix resultingMatrix = new Matrix(size, size);
Parallel.ForEach(rangePartitioner, range =>
{
for (int i = range.Item1; i < range.Item2; i++)
{
for (int j = 0; j < size; j++)
{
resultingMatrix._data[i][j] = dictMatrix._data[dictCols[j]][dictRows[i]];
}
}
});
return resultingMatrix;
}
}
/// <summary>
/// Estimates the largest eigenvalue (in magnitude).
/// </summary>
/// <remarks>
/// Implements the Power Iteration Method.
/// </remarks>
/// <param name="resultEigenVector">The returned corresponding eigenvector.</param>
/// <param name="maxNumOfIterations">The maximum number of the iterations.</param>
/// <param name="stopDelta">The stopping corvengence delta of the previous iteration and current iteration.</param>
/// <returns>The estimated largest eigen value (in magnitude).</returns>
public double EstimateLargestEigenvalue(out double[] resultEigenVector, int maxNumOfIterations = 1000, double stopDelta = 1e-6)
{
//Check square matrix
if (!IsSquareMatrix)
{
throw new InvalidOperationException($"Matrix must be square.");
}
//Local variables
//Iteration initialization
int iteration = 0;
double iterationDelta = 0;
int n = NumOfRows;
double[] tmpVector = new double[n];
double eigenValue = 0;
double[] eigenVector = new double[n];
eigenVector.Populate(1);
//Results
double minDelta = double.MaxValue;
double resultEigenValue = 0;
resultEigenVector = new double[n];
//Convergence loop
do
{
Parallel.For(0, n, i =>
{
tmpVector[i] = 0;