forked from DanWBR/dwsim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Matrix.cs
738 lines (637 loc) · 21.7 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
namespace Mapack
{
using System;
using System.IO;
using System.Globalization;
/// <summary>Matrix provides the fundamental operations of numerical linear algebra.</summary>
public class Matrix
{
private double[][] data;
private int rows;
private int columns;
private static Random random = new Random();
/// <summary>Constructs an empty matrix of the given size.</summary>
/// <param name="rows">Number of rows.</param>
/// <param name="columns">Number of columns.</param>
public Matrix(int rows, int columns)
{
this.rows = rows;
this.columns = columns;
this.data = new double[rows][];
for (int i = 0; i < rows; i++)
{
this.data[i] = new double[columns];
}
}
/// <summary>Constructs a matrix of the given size and assigns a given value to all diagonal elements.</summary>
/// <param name="rows">Number of rows.</param>
/// <param name="columns">Number of columns.</param>
/// <param name="value">Value to assign to the diagnoal elements.</param>
public Matrix(int rows, int columns, double value)
{
this.rows = rows;
this.columns = columns;
this.data = new double[rows][];
for (int i = 0; i < rows; i++)
{
data[i] = new double[columns];
}
for (int i = 0; i < rows; i++)
{
data[i][i] = value;
}
}
/// <summary>Constructs a matrix from the given array.</summary>
/// <param name="value">The array the matrix gets constructed from.</param>
[CLSCompliant(false)]
public Matrix(double[][] value)
{
this.rows = value.Length;
this.columns = value[0].Length;
for (int i = 0; i < rows; i++)
{
if (value[i].Length != columns)
{
throw new ArgumentException("Argument out of range.");
}
}
this.data = value;
}
/// <summary>Determines weather two instances are equal.</summary>
public override bool Equals(object obj)
{
return Equals(this, (Matrix) obj);
}
/// <summary>Determines weather two instances are equal.</summary>
public static bool Equals(Matrix left, Matrix right)
{
if (((object) left) == ((object) right))
{
return true;
}
if ((((object) left) == null) || (((object) right) == null))
{
return false;
}
if ((left.Rows != right.Rows) || (left.Columns != right.Columns))
{
return false;
}
for (int i = 0; i < left.Rows; i++)
{
for (int j = 0; j < left.Columns; j++)
{
if (left[i, j] != right[i, j])
{
return false;
}
}
}
return true;
}
/// <summary>Serves as a hash function for a particular type, suitable for use in hashing algorithms and data structures like a hash table.</summary>
public override int GetHashCode()
{
return (this.Rows + this.Columns);
}
internal double[][] Array
{
get
{
return this.data;
}
}
/// <summary>Returns the number of columns.</summary>
public int Rows
{
get
{
return this.rows;
}
}
/// <summary>Returns the number of columns.</summary>
public int Columns
{
get
{
return this.columns;
}
}
/// <summary>Return <see langword="true"/> if the matrix is a square matrix.</summary>
public bool Square
{
get
{
return (rows == columns);
}
}
/// <summary>Returns <see langword="true"/> if the matrix is symmetric.</summary>
public bool Symmetric
{
get
{
if (this.Square)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j <= i; j++)
{
if (data[i][j] != data[j][i])
{
return false;
}
}
}
return true;
}
return false;
}
}
/// <summary>Access the value at the given location.</summary>
public double this[int row, int column]
{
set
{
this.data[row][column] = value;
}
get
{
return this.data[row][column];
}
}
/// <summary>Returns a sub matrix extracted from the current matrix.</summary>
/// <param name="startRow">Start row index</param>
/// <param name="endRow">End row index</param>
/// <param name="startColumn">Start column index</param>
/// <param name="endColumn">End column index</param>
public Matrix Submatrix(int startRow, int endRow, int startColumn, int endColumn)
{
if ((startRow > endRow) || (startColumn > endColumn) || (startRow < 0) || (startRow >= this.rows) || (endRow < 0) || (endRow >= this.rows) || (startColumn < 0) || (startColumn >= this.columns) || (endColumn < 0) || (endColumn >= this.columns))
{
throw new ArgumentException("Argument out of range.");
}
Matrix X = new Matrix(endRow - startRow + 1, endColumn - startColumn + 1);
double[][] x = X.Array;
for (int i = startRow; i <= endRow; i++)
{
for (int j = startColumn; j <= endColumn; j++)
{
x[i - startRow][j - startColumn] = data[i][j];
}
}
return X;
}
/// <summary>Returns a sub matrix extracted from the current matrix.</summary>
/// <param name="rowIndexes">Array of row indices</param>
/// <param name="columnIndexes">Array of column indices</param>
public Matrix Submatrix(int[] rowIndexes, int[] columnIndexes)
{
Matrix X = new Matrix(rowIndexes.Length, columnIndexes.Length);
double[][] x = X.Array;
for (int i = 0; i < rowIndexes.Length; i++)
{
for (int j = 0; j < columnIndexes.Length; j++)
{
if ((rowIndexes[i] < 0) || (rowIndexes[i] >= rows) || (columnIndexes[j] < 0) || (columnIndexes[j] >= columns))
{
throw new ArgumentException("Argument out of range.");
}
x[i][j] = data[rowIndexes[i]][columnIndexes[j]];
}
}
return X;
}
/// <summary>Returns a sub matrix extracted from the current matrix.</summary>
/// <param name="i0">Starttial row index</param>
/// <param name="i1">End row index</param>
/// <param name="c">Array of row indices</param>
public Matrix Submatrix(int i0, int i1, int[] c)
{
if ((i0 > i1) || (i0 < 0) || (i0 >= this.rows) || (i1 < 0) || (i1 >= this.rows))
{
throw new ArgumentException("Argument out of range.");
}
Matrix X = new Matrix(i1 - i0 + 1, c.Length);
double[][] x = X.Array;
for (int i = i0; i <= i1; i++)
{
for (int j = 0; j < c.Length; j++)
{
if ((c[j] < 0) || (c[j] >= columns))
{
throw new ArgumentException("Argument out of range.");
}
x[i - i0][j] = data[i][c[j]];
}
}
return X;
}
/// <summary>Returns a sub matrix extracted from the current matrix.</summary>
/// <param name="r">Array of row indices</param>
/// <param name="j0">Start column index</param>
/// <param name="j1">End column index</param>
public Matrix Submatrix(int[] r, int j0, int j1)
{
if ((j0 > j1) || (j0 < 0) || (j0 >= columns) || (j1 < 0) || (j1 >= columns))
{
throw new ArgumentException("Argument out of range.");
}
Matrix X = new Matrix(r.Length, j1-j0+1);
double[][] x = X.Array;
for (int i = 0; i < r.Length; i++)
{
for (int j = j0; j <= j1; j++)
{
if ((r[i] < 0) || (r[i] >= this.rows))
{
throw new ArgumentException("Argument out of range.");
}
x[i][j - j0] = data[r[i]][j];
}
}
return X;
}
/// <summary>Creates a copy of the matrix.</summary>
public Matrix Clone()
{
Matrix X = new Matrix(rows, columns);
double[][] x = X.Array;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
x[i][j] = data[i][j];
}
}
return X;
}
/// <summary>Returns the transposed matrix.</summary>
public Matrix Transpose()
{
Matrix X = new Matrix(columns, rows);
double[][] x = X.Array;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
x[j][i] = data[i][j];
}
}
return X;
}
/// <summary>Returns the One Norm for the matrix.</summary>
/// <value>The maximum column sum.</value>
public double Norm1
{
get
{
double f = 0;
for (int j = 0; j < columns; j++)
{
double s = 0;
for (int i = 0; i < rows; i++)
{
s += Math.Abs(data[i][j]);
}
f = Math.Max(f, s);
}
return f;
}
}
/// <summary>Returns the Infinity Norm for the matrix.</summary>
/// <value>The maximum row sum.</value>
public double InfinityNorm
{
get
{
double f = 0;
for (int i = 0; i < rows; i++)
{
double s = 0;
for (int j = 0; j < columns; j++)
s += Math.Abs(data[i][j]);
f = Math.Max(f, s);
}
return f;
}
}
/// <summary>Returns the Frobenius Norm for the matrix.</summary>
/// <value>The square root of sum of squares of all elements.</value>
public double FrobeniusNorm
{
get
{
double f = 0;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
f = Hypotenuse(f, data[i][j]);
}
}
return f;
}
}
/// <summary>Unary minus.</summary>
public static Matrix Negate(Matrix value)
{
if (value == null)
{
throw new ArgumentNullException("value");
}
int rows = value.Rows;
int columns = value.Columns;
double[][] data = value.Array;
Matrix X = new Matrix(rows, columns);
double[][] x = X.Array;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
x[i][j] = -data[i][j];
}
}
return X;
}
/// <summary>Unary minus.</summary>
public static Matrix operator-(Matrix value)
{
if (value == null)
{
throw new ArgumentNullException("value");
}
return Negate(value);
}
/// <summary>Matrix equality.</summary>
public static bool operator==(Matrix left, Matrix right)
{
return Equals(left, right);
}
/// <summary>Matrix inequality.</summary>
public static bool operator!=(Matrix left, Matrix right)
{
return !Equals(left, right);
}
/// <summary>Matrix addition.</summary>
public static Matrix Add(Matrix left, Matrix right)
{
if (left == null)
{
throw new ArgumentNullException("left");
}
if (right == null)
{
throw new ArgumentNullException("right");
}
int rows = left.Rows;
int columns = left.Columns;
double[][] data = left.Array;
if ((rows != right.Rows) || (columns != right.Columns))
{
throw new ArgumentException("Matrix dimension do not match.");
}
Matrix X = new Matrix(rows, columns);
double[][] x = X.Array;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
x[i][j] = data[i][j] + right[i,j];
}
}
return X;
}
/// <summary>Matrix addition.</summary>
public static Matrix operator+(Matrix left, Matrix right)
{
if (left == null)
{
throw new ArgumentNullException("left");
}
if (right == null)
{
throw new ArgumentNullException("right");
}
return Add(left, right);
}
/// <summary>Matrix subtraction.</summary>
public static Matrix Subtract(Matrix left, Matrix right)
{
if (left == null)
{
throw new ArgumentNullException("left");
}
if (right == null)
{
throw new ArgumentNullException("right");
}
int rows = left.Rows;
int columns = left.Columns;
double[][] data = left.Array;
if ((rows != right.Rows) || (columns != right.Columns))
{
throw new ArgumentException("Matrix dimension do not match.");
}
Matrix X = new Matrix(rows, columns);
double[][] x = X.Array;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
x[i][j] = data[i][j] - right[i,j];
}
}
return X;
}
/// <summary>Matrix subtraction.</summary>
public static Matrix operator-(Matrix left, Matrix right)
{
if (left == null)
{
throw new ArgumentNullException("left");
}
if (right == null)
{
throw new ArgumentNullException("right");
}
return Subtract(left, right);
}
/// <summary>Matrix-scalar multiplication.</summary>
public static Matrix Multiply(Matrix left, double right)
{
if (left == null)
{
throw new ArgumentNullException("left");
}
int rows = left.Rows;
int columns = left.Columns;
double[][] data = left.Array;
Matrix X = new Matrix(rows, columns);
double[][] x = X.Array;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
x[i][j] = data[i][j] * right;
}
}
return X;
}
/// <summary>Matrix-scalar multiplication.</summary>
public static Matrix operator*(Matrix left, double right)
{
if (left == null)
{
throw new ArgumentNullException("left");
}
return Multiply(left, right);
}
/// <summary>Matrix-matrix multiplication.</summary>
public static Matrix Multiply(Matrix left, Matrix right)
{
if (left == null)
{
throw new ArgumentNullException("left");
}
if (right == null)
{
throw new ArgumentNullException("right");
}
int rows = left.Rows;
double[][] data = left.Array;
if (right.Rows != left.columns)
{
throw new ArgumentException("Matrix dimensions are not valid.");
}
int columns = right.Columns;
Matrix X = new Matrix(rows, columns);
double[][] x = X.Array;
int size = left.columns;
double[] column = new double[size];
for (int j = 0; j < columns; j++)
{
for (int k = 0; k < size; k++)
{
column[k] = right[k,j];
}
for (int i = 0; i < rows; i++)
{
double[] row = data[i];
double s = 0;
for (int k = 0; k < size; k++)
{
s += row[k] * column[k];
}
x[i][j] = s;
}
}
return X;
}
/// <summary>Matrix-matrix multiplication.</summary>
public static Matrix operator*(Matrix left, Matrix right)
{
if (left == null)
{
throw new ArgumentNullException("left");
}
if (right == null)
{
throw new ArgumentNullException("right");
}
return Multiply(left, right);
}
/// <summary>Returns the LHS solution vetor if the matrix is square or the least squares solution otherwise.</summary>
public Matrix Solve(Matrix rightHandSide)
{
return (rows == columns) ? new LuDecomposition(this).Solve(rightHandSide) : new QrDecomposition(this).Solve(rightHandSide);
}
/// <summary>Inverse of the matrix if matrix is square, pseudoinverse otherwise.</summary>
public Matrix Inverse
{
get
{
return this.Solve(Diagonal(rows, rows, 1.0));
}
}
/// <summary>Determinant if matrix is square.</summary>
public double Determinant
{
get
{
return new LuDecomposition(this).Determinant;
}
}
/// <summary>Returns the trace of the matrix.</summary>
/// <returns>Sum of the diagonal elements.</returns>
public double Trace
{
get
{
double trace = 0;
for (int i = 0; i < Math.Min(rows, columns); i++)
{
trace += data[i][i];
}
return trace;
}
}
/// <summary>Returns a matrix filled with random values.</summary>
public static Matrix Random(int rows, int columns)
{
Matrix X = new Matrix(rows, columns);
double[][] x = X.Array;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
x[i][j] = random.NextDouble();
}
}
return X;
}
/// <summary>Returns a diagonal matrix of the given size.</summary>
public static Matrix Diagonal(int rows, int columns, double value)
{
Matrix X = new Matrix(rows, columns);
double[][] x = X.Array;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
x[i][j] = ((i == j) ? value : 0.0);
}
}
return X;
}
/// <summary>Returns the matrix in a textual form.</summary>
public override string ToString()
{
using (StringWriter writer = new StringWriter(CultureInfo.InvariantCulture))
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
writer.Write(this.data[i][j] + " ");
}
writer.WriteLine();
}
return writer.ToString();
}
}
private static double Hypotenuse(double a, double b)
{
if (Math.Abs(a) > Math.Abs(b))
{
double r = b / a;
return Math.Abs(a) * Math.Sqrt(1 + r * r);
}
if (b != 0)
{
double r = a / b;
return Math.Abs(b) * Math.Sqrt(1 + r * r);
}
return 0.0;
}
}
}