-
Notifications
You must be signed in to change notification settings - Fork 905
/
Copy pathManagedLinearAlgebraProvider.Complex.cs
3279 lines (2876 loc) · 120 KB
/
ManagedLinearAlgebraProvider.Complex.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
// <copyright file="ManagedLinearAlgebraProvider.Complex.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
//
// Copyright (c) 2009-2021 Math.NET
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
using System;
using MathNet.Numerics.Threading;
using Complex = System.Numerics.Complex;
using QRMethod = MathNet.Numerics.LinearAlgebra.Factorization.QRMethod;
using static System.FormattableString;
namespace MathNet.Numerics.Providers.LinearAlgebra
{
/// <summary>
/// The managed linear algebra provider.
/// </summary>
public partial class ManagedLinearAlgebraProvider
{
/// <summary>
/// Adds a scaled vector to another: <c>result = y + alpha*x</c>.
/// </summary>
/// <param name="y">The vector to update.</param>
/// <param name="alpha">The value to scale <paramref name="x"/> by.</param>
/// <param name="x">The vector to add to <paramref name="y"/>.</param>
/// <param name="result">The result of the addition.</param>
/// <remarks>This is similar to the AXPY BLAS routine.</remarks>
public void AddVectorToScaledVector(Complex[] y, Complex alpha, Complex[] x, Complex[] result)
{
if (y == null)
{
throw new ArgumentNullException(nameof(y));
}
if (x == null)
{
throw new ArgumentNullException(nameof(x));
}
if (y.Length != x.Length)
{
throw new ArgumentException("All vectors must have the same dimensionality.");
}
if (alpha.IsZero())
{
y.Copy(result);
}
else if (alpha.IsOne())
{
for (int i = 0; i < result.Length; i++)
{
result[i] = y[i] + x[i];
}
}
else
{
for (int i = 0; i < result.Length; i++)
{
result[i] = y[i] + (alpha * x[i]);
}
}
}
/// <summary>
/// Scales an array. Can be used to scale a vector and a matrix.
/// </summary>
/// <param name="alpha">The scalar.</param>
/// <param name="x">The values to scale.</param>
/// <param name="result">This result of the scaling.</param>
/// <remarks>This is similar to the SCAL BLAS routine.</remarks>
public void ScaleArray(Complex alpha, Complex[] x, Complex[] result)
{
if (x == null)
{
throw new ArgumentNullException(nameof(x));
}
if (alpha.IsZero())
{
Array.Clear(result, 0, result.Length);
}
else if (alpha.IsOne())
{
x.Copy(result);
}
else
{
for (int i = 0; i < result.Length; i++)
{
result[i] = alpha * x[i];
}
}
}
/// <summary>
/// Conjugates an array. Can be used to conjugate a vector and a matrix.
/// </summary>
/// <param name="x">The values to conjugate.</param>
/// <param name="result">This result of the conjugation.</param>
public void ConjugateArray(Complex[] x, Complex[] result)
{
if (x == null)
{
throw new ArgumentNullException(nameof(x));
}
for (int i = 0; i < result.Length; i++)
{
result[i] = x[i].Conjugate();
}
}
/// <summary>
/// Computes the dot product of x and y.
/// </summary>
/// <param name="x">The vector x.</param>
/// <param name="y">The vector y.</param>
/// <returns>The dot product of x and y.</returns>
/// <remarks>This is equivalent to the DOT BLAS routine.</remarks>
public Complex DotProduct(Complex[] x, Complex[] y)
{
if (y == null)
{
throw new ArgumentNullException(nameof(y));
}
if (x == null)
{
throw new ArgumentNullException(nameof(x));
}
if (y.Length != x.Length)
{
throw new ArgumentException("All vectors must have the same dimensionality.");
}
Complex dot = Complex.Zero;
for (var index = 0; index < y.Length; index++)
{
dot += y[index]*x[index];
}
return dot;
}
/// <summary>
/// Does a point wise add of two arrays <c>z = x + y</c>. This can be used
/// to add vectors or matrices.
/// </summary>
/// <param name="x">The array x.</param>
/// <param name="y">The array y.</param>
/// <param name="result">The result of the addition.</param>
/// <remarks>There is no equivalent BLAS routine, but many libraries
/// provide optimized (parallel and/or vectorized) versions of this
/// routine.</remarks>
public void AddArrays(Complex[] x, Complex[] y, Complex[] result)
{
if (y == null)
{
throw new ArgumentNullException(nameof(y));
}
if (x == null)
{
throw new ArgumentNullException(nameof(x));
}
if (result == null)
{
throw new ArgumentNullException(nameof(result));
}
if (y.Length != x.Length || y.Length != result.Length)
{
throw new ArgumentException("All vectors must have the same dimensionality.");
}
for (int i = 0; i < result.Length; i++)
{
result[i] = x[i] + y[i];
}
}
/// <summary>
/// Does a point wise subtraction of two arrays <c>z = x - y</c>. This can be used
/// to subtract vectors or matrices.
/// </summary>
/// <param name="x">The array x.</param>
/// <param name="y">The array y.</param>
/// <param name="result">The result of the subtraction.</param>
/// <remarks>There is no equivalent BLAS routine, but many libraries
/// provide optimized (parallel and/or vectorized) versions of this
/// routine.</remarks>
public void SubtractArrays(Complex[] x, Complex[] y, Complex[] result)
{
if (y == null)
{
throw new ArgumentNullException(nameof(y));
}
if (x == null)
{
throw new ArgumentNullException(nameof(x));
}
if (result == null)
{
throw new ArgumentNullException(nameof(result));
}
if (y.Length != x.Length || y.Length != result.Length)
{
throw new ArgumentException("All vectors must have the same dimensionality.");
}
for (int i = 0; i < result.Length; i++)
{
result[i] = x[i] - y[i];
}
}
/// <summary>
/// Does a point wise multiplication of two arrays <c>z = x * y</c>. This can be used
/// to multiple elements of vectors or matrices.
/// </summary>
/// <param name="x">The array x.</param>
/// <param name="y">The array y.</param>
/// <param name="result">The result of the point wise multiplication.</param>
/// <remarks>There is no equivalent BLAS routine, but many libraries
/// provide optimized (parallel and/or vectorized) versions of this
/// routine.</remarks>
public void PointWiseMultiplyArrays(Complex[] x, Complex[] y, Complex[] result)
{
if (y == null)
{
throw new ArgumentNullException(nameof(y));
}
if (x == null)
{
throw new ArgumentNullException(nameof(x));
}
if (result == null)
{
throw new ArgumentNullException(nameof(result));
}
if (y.Length != x.Length || y.Length != result.Length)
{
throw new ArgumentException("All vectors must have the same dimensionality.");
}
for (int i = 0; i < result.Length; i++)
{
result[i] = x[i] * y[i];
}
}
/// <summary>
/// Does a point wise division of two arrays <c>z = x / y</c>. This can be used
/// to divide elements of vectors or matrices.
/// </summary>
/// <param name="x">The array x.</param>
/// <param name="y">The array y.</param>
/// <param name="result">The result of the point wise division.</param>
/// <remarks>There is no equivalent BLAS routine, but many libraries
/// provide optimized (parallel and/or vectorized) versions of this
/// routine.</remarks>
public void PointWiseDivideArrays(Complex[] x, Complex[] y, Complex[] result)
{
if (y == null)
{
throw new ArgumentNullException(nameof(y));
}
if (x == null)
{
throw new ArgumentNullException(nameof(x));
}
if (result == null)
{
throw new ArgumentNullException(nameof(result));
}
if (y.Length != x.Length || y.Length != result.Length)
{
throw new ArgumentException("All vectors must have the same dimensionality.");
}
CommonParallel.For(0, y.Length, 4096, (a, b) =>
{
for (int i = a; i < b; i++)
{
result[i] = x[i] / y[i];
}
});
}
/// <summary>
/// Does a point wise power of two arrays <c>z = x ^ y</c>. This can be used
/// to raise elements of vectors or matrices to the powers of another vector or matrix.
/// </summary>
/// <param name="x">The array x.</param>
/// <param name="y">The array y.</param>
/// <param name="result">The result of the point wise power.</param>
/// <remarks>There is no equivalent BLAS routine, but many libraries
/// provide optimized (parallel and/or vectorized) versions of this
/// routine.</remarks>
public void PointWisePowerArrays(Complex[] x, Complex[] y, Complex[] result)
{
if (y == null)
{
throw new ArgumentNullException(nameof(y));
}
if (x == null)
{
throw new ArgumentNullException(nameof(x));
}
if (result == null)
{
throw new ArgumentNullException(nameof(result));
}
if (y.Length != x.Length || y.Length != result.Length)
{
throw new ArgumentException("All vectors must have the same dimensionality.");
}
CommonParallel.For(0, y.Length, 4096, (a, b) =>
{
for (int i = a; i < b; i++)
{
result[i] = Complex.Pow(x[i], y[i]);
}
});
}
/// <summary>
/// Computes the requested <see cref="Norm"/> of the matrix.
/// </summary>
/// <param name="norm">The type of norm to compute.</param>
/// <param name="rows">The number of rows.</param>
/// <param name="columns">The number of columns.</param>
/// <param name="matrix">The matrix to compute the norm from.</param>
/// <returns>
/// The requested <see cref="Norm"/> of the matrix.
/// </returns>
public double MatrixNorm(Norm norm, int rows, int columns, Complex[] matrix)
{
switch (norm)
{
case Norm.OneNorm:
var norm1 = 0d;
for (var j = 0; j < columns; j++)
{
var s = 0.0;
for (var i = 0; i < rows; i++)
{
s += matrix[(j*rows) + i].Magnitude;
}
norm1 = Math.Max(norm1, s);
}
return norm1;
case Norm.LargestAbsoluteValue:
var normMax = 0d;
for (var j = 0; j < columns; j++)
{
for (var i = 0; i < rows; i++)
{
normMax = Math.Max(matrix[(j * rows) + i].Magnitude, normMax);
}
}
return normMax;
case Norm.InfinityNorm:
var r = new double[rows];
for (var j = 0; j < columns; j++)
{
for (var i = 0; i < rows; i++)
{
r[i] += matrix[(j * rows) + i].Magnitude;
}
}
// TODO: reuse
var max = r[0];
for (int i = 0; i < r.Length; i++)
{
if (r[i] > max)
{
max = r[i];
}
}
return max;
case Norm.FrobeniusNorm:
var aat = new Complex[rows*rows];
MatrixMultiplyWithUpdate(Transpose.DontTranspose, Transpose.ConjugateTranspose, 1.0, matrix, rows, columns, matrix, rows, columns, 0.0, aat);
var normF = 0d;
for (var i = 0; i < rows; i++)
{
normF += aat[(i * rows) + i].Magnitude;
}
return Math.Sqrt(normF);
default:
throw new NotSupportedException();
}
}
/// <summary>
/// Multiples two matrices. <c>result = x * y</c>
/// </summary>
/// <param name="x">The x matrix.</param>
/// <param name="rowsX">The number of rows in the x matrix.</param>
/// <param name="columnsX">The number of columns in the x matrix.</param>
/// <param name="y">The y matrix.</param>
/// <param name="rowsY">The number of rows in the y matrix.</param>
/// <param name="columnsY">The number of columns in the y matrix.</param>
/// <param name="result">Where to store the result of the multiplication.</param>
/// <remarks>This is a simplified version of the BLAS GEMM routine with alpha
/// set to 1.0 and beta set to 0.0, and x and y are not transposed.</remarks>
public void MatrixMultiply(Complex[] x, int rowsX, int columnsX, Complex[] y, int rowsY, int columnsY, Complex[] result)
{
if (x == null)
{
throw new ArgumentNullException(nameof(x));
}
if (y == null)
{
throw new ArgumentNullException(nameof(y));
}
if (result == null)
{
throw new ArgumentNullException(nameof(result));
}
if (columnsX != rowsY)
{
throw new ArgumentOutOfRangeException(Invariant($"columnsA ({columnsX}) != rowsB ({rowsY})"));
}
if (rowsX * columnsX != x.Length)
{
throw new ArgumentOutOfRangeException(Invariant($"rowsA ({rowsX}) * columnsA ({columnsX}) != a.Length ({x.Length})"));
}
if (rowsY * columnsY != y.Length)
{
throw new ArgumentOutOfRangeException(Invariant($"rowsB ({rowsY}) * columnsB ({columnsY}) != b.Length ({y.Length})"));
}
if (rowsX * columnsY != result.Length)
{
throw new ArgumentOutOfRangeException(Invariant($"rowsA ({rowsX}) * columnsB ({columnsY}) != c.Length ({result.Length})"));
}
// handle degenerate cases
Array.Clear(result, 0, result.Length);
// Extract column arrays
var columnDataB = new Complex[columnsY][];
for (int i = 0; i < columnDataB.Length; i++)
{
var column = new Complex[rowsY];
GetColumn(Transpose.DontTranspose, i, rowsY, columnsY, y, column);
columnDataB[i] = column;
}
var shouldNotParallelize = rowsX + columnsY + columnsX < Control.ParallelizeOrder || Control.MaxDegreeOfParallelism < 2;
if (shouldNotParallelize)
{
var row = new Complex[columnsX];
for (int i = 0; i < rowsX; i++)
{
GetRow(Transpose.DontTranspose, i, rowsX, columnsX, x, row);
for (int j = 0; j < columnsY; j++)
{
var col = columnDataB[j];
Complex sum = Complex.Zero;
for (int ii = 0; ii < row.Length; ii++)
{
sum += row[ii] * col[ii];
}
result[j * rowsX + i] += Complex.One * sum;
}
}
}
else
{
CommonParallel.For(0, rowsX, 1, (u, v) =>
{
var row = new Complex[columnsX];
for (int i = u; i < v; i++)
{
GetRow(Transpose.DontTranspose, i, rowsX, columnsX, x, row);
for (int j = 0; j < columnsY; j++)
{
var column = columnDataB[j];
Complex sum = Complex.Zero;
for (int ii = 0; ii < row.Length; ii++)
{
sum += row[ii] * column[ii];
}
result[j * rowsX + i] += Complex.One * sum;
}
}
});
}
}
/// <summary>
/// Multiplies two matrices and updates another with the result. <c>c = alpha*op(a)*op(b) + beta*c</c>
/// </summary>
/// <param name="transposeA">How to transpose the <paramref name="a"/> matrix.</param>
/// <param name="transposeB">How to transpose the <paramref name="b"/> matrix.</param>
/// <param name="alpha">The value to scale <paramref name="a"/> matrix.</param>
/// <param name="a">The a matrix.</param>
/// <param name="rowsA">The number of rows in the <paramref name="a"/> matrix.</param>
/// <param name="columnsA">The number of columns in the <paramref name="a"/> matrix.</param>
/// <param name="b">The b matrix</param>
/// <param name="rowsB">The number of rows in the <paramref name="b"/> matrix.</param>
/// <param name="columnsB">The number of columns in the <paramref name="b"/> matrix.</param>
/// <param name="beta">The value to scale the <paramref name="c"/> matrix.</param>
/// <param name="c">The c matrix.</param>
public void MatrixMultiplyWithUpdate(Transpose transposeA, Transpose transposeB, Complex alpha, Complex[] a, int rowsA, int columnsA, Complex[] b, int rowsB, int columnsB, Complex beta, Complex[] c)
{
if (a == null)
{
throw new ArgumentNullException(nameof(a));
}
if (b == null)
{
throw new ArgumentNullException(nameof(b));
}
if (c == null)
{
throw new ArgumentNullException(nameof(c));
}
if (transposeA != Transpose.DontTranspose)
{
(rowsA, columnsA) = (columnsA, rowsA);
}
if (transposeB != Transpose.DontTranspose)
{
(rowsB, columnsB) = (columnsB, rowsB);
}
if (columnsA != rowsB)
{
throw new ArgumentOutOfRangeException(Invariant($"columnsA ({columnsA}) != rowsB ({rowsB})"));
}
if (rowsA * columnsA != a.Length)
{
throw new ArgumentOutOfRangeException(Invariant($"rowsA ({rowsA}) * columnsA ({columnsA}) != a.Length ({a.Length})"));
}
if (rowsB * columnsB != b.Length)
{
throw new ArgumentOutOfRangeException(Invariant($"rowsB ({rowsB}) * columnsB ({columnsB}) != b.Length ({b.Length})"));
}
if (rowsA * columnsB != c.Length)
{
throw new ArgumentOutOfRangeException(Invariant($"rowsA ({rowsA}) * columnsB ({columnsB}) != c.Length ({c.Length})"));
}
// handle degenerate cases
if (beta == Complex.Zero)
{
Array.Clear(c, 0, c.Length);
}
else if (beta != Complex.One)
{
ScaleArray(beta, c, c);
}
if (alpha == Complex.Zero)
{
return;
}
// Extract column arrays
var columnDataB = new Complex[columnsB][];
for (int i = 0; i < columnDataB.Length; i++)
{
var column = new Complex[rowsB];
GetColumn(transposeB, i, rowsB, columnsB, b, column);
columnDataB[i] = column;
}
var shouldNotParallelize = rowsA + columnsB + columnsA < Control.ParallelizeOrder || Control.MaxDegreeOfParallelism < 2;
if (shouldNotParallelize)
{
var row = new Complex[columnsA];
for (int i = 0; i < rowsA; i++)
{
GetRow(transposeA, i, rowsA, columnsA, a, row);
for (int j = 0; j < columnsB; j++)
{
var col = columnDataB[j];
Complex sum = Complex.Zero;
for (int ii = 0; ii < row.Length; ii++)
{
sum += row[ii] * col[ii];
}
c[j * rowsA + i] += alpha * sum;
}
}
}
else
{
CommonParallel.For(0, rowsA, 1, (u, v) =>
{
var row = new Complex[columnsA];
for (int i = u; i < v; i++)
{
GetRow(transposeA, i, rowsA, columnsA, a, row);
for (int j = 0; j < columnsB; j++)
{
var column = columnDataB[j];
Complex sum = Complex.Zero;
for (int ii = 0; ii < row.Length; ii++)
{
sum += row[ii] * column[ii];
}
c[j * rowsA + i] += alpha * sum;
}
}
});
}
}
/// <summary>
/// Computes the LUP factorization of A. P*A = L*U.
/// </summary>
/// <param name="data">An <paramref name="order"/> by <paramref name="order"/> matrix. The matrix is overwritten with the
/// the LU factorization on exit. The lower triangular factor L is stored in under the diagonal of <paramref name="data"/> (the diagonal is always 1.0
/// for the L factor). The upper triangular factor U is stored on and above the diagonal of <paramref name="data"/>.</param>
/// <param name="order">The order of the square matrix <paramref name="data"/>.</param>
/// <param name="ipiv">On exit, it contains the pivot indices. The size of the array must be <paramref name="order"/>.</param>
/// <remarks>This is equivalent to the GETRF LAPACK routine.</remarks>
public void LUFactor(Complex[] data, int order, int[] ipiv)
{
if (data == null)
{
throw new ArgumentNullException(nameof(data));
}
if (ipiv == null)
{
throw new ArgumentNullException(nameof(ipiv));
}
if (data.Length != order*order)
{
throw new ArgumentException("The array arguments must have the same length.", nameof(data));
}
if (ipiv.Length != order)
{
throw new ArgumentException("The array arguments must have the same length.", nameof(ipiv));
}
// Initialize the pivot matrix to the identity permutation.
for (var i = 0; i < order; i++)
{
ipiv[i] = i;
}
var vecLUcolj = new Complex[order];
// Outer loop.
for (var j = 0; j < order; j++)
{
var indexj = j*order;
var indexjj = indexj + j;
// Make a copy of the j-th column to localize references.
for (var i = 0; i < order; i++)
{
vecLUcolj[i] = data[indexj + i];
}
// Apply previous transformations.
for (var i = 0; i < order; i++)
{
// Most of the time is spent in the following dot product.
var kmax = Math.Min(i, j);
var s = Complex.Zero;
for (var k = 0; k < kmax; k++)
{
s += data[(k*order) + i]*vecLUcolj[k];
}
data[indexj + i] = vecLUcolj[i] -= s;
}
// Find pivot and exchange if necessary.
var p = j;
for (var i = j + 1; i < order; i++)
{
if (vecLUcolj[i].Magnitude > vecLUcolj[p].Magnitude)
{
p = i;
}
}
if (p != j)
{
for (var k = 0; k < order; k++)
{
var indexk = k*order;
var indexkp = indexk + p;
var indexkj = indexk + j;
(data[indexkp], data[indexkj]) = (data[indexkj], data[indexkp]);
}
ipiv[j] = p;
}
// Compute multipliers.
if (j < order & data[indexjj] != 0.0)
{
for (var i = j + 1; i < order; i++)
{
data[indexj + i] /= data[indexjj];
}
}
}
}
/// <summary>
/// Computes the inverse of matrix using LU factorization.
/// </summary>
/// <param name="a">The N by N matrix to invert. Contains the inverse On exit.</param>
/// <param name="order">The order of the square matrix <paramref name="a"/>.</param>
/// <remarks>This is equivalent to the GETRF and GETRI LAPACK routines.</remarks>
public void LUInverse(Complex[] a, int order)
{
if (a == null)
{
throw new ArgumentNullException(nameof(a));
}
if (a.Length != order*order)
{
throw new ArgumentException("The array arguments must have the same length.", nameof(a));
}
var ipiv = new int[order];
LUFactor(a, order, ipiv);
LUInverseFactored(a, order, ipiv);
}
/// <summary>
/// Computes the inverse of a previously factored matrix.
/// </summary>
/// <param name="a">The LU factored N by N matrix. Contains the inverse On exit.</param>
/// <param name="order">The order of the square matrix <paramref name="a"/>.</param>
/// <param name="ipiv">The pivot indices of <paramref name="a"/>.</param>
/// <remarks>This is equivalent to the GETRI LAPACK routine.</remarks>
public void LUInverseFactored(Complex[] a, int order, int[] ipiv)
{
if (a == null)
{
throw new ArgumentNullException(nameof(a));
}
if (ipiv == null)
{
throw new ArgumentNullException(nameof(ipiv));
}
if (a.Length != order*order)
{
throw new ArgumentException("The array arguments must have the same length.", nameof(a));
}
if (ipiv.Length != order)
{
throw new ArgumentException("The array arguments must have the same length.", nameof(ipiv));
}
var inverse = new Complex[a.Length];
for (var i = 0; i < order; i++)
{
inverse[i + (order*i)] = Complex.One;
}
LUSolveFactored(order, a, order, ipiv, inverse);
inverse.Copy(a);
}
/// <summary>
/// Solves A*X=B for X using LU factorization.
/// </summary>
/// <param name="columnsOfB">The number of columns of B.</param>
/// <param name="a">The square matrix A.</param>
/// <param name="order">The order of the square matrix <paramref name="a"/>.</param>
/// <param name="b">On entry the B matrix; on exit the X matrix.</param>
/// <remarks>This is equivalent to the GETRF and GETRS LAPACK routines.</remarks>
public void LUSolve(int columnsOfB, Complex[] a, int order, Complex[] b)
{
if (a == null)
{
throw new ArgumentNullException(nameof(a));
}
if (b == null)
{
throw new ArgumentNullException(nameof(b));
}
if (a.Length != order*order)
{
throw new ArgumentException("The array arguments must have the same length.", nameof(a));
}
if (b.Length != order*columnsOfB)
{
throw new ArgumentException("The array arguments must have the same length.", nameof(b));
}
if (ReferenceEquals(a, b))
{
throw new ArgumentException("Arguments must be different objects.");
}
var ipiv = new int[order];
var clone = new Complex[a.Length];
a.Copy(clone);
LUFactor(clone, order, ipiv);
LUSolveFactored(columnsOfB, clone, order, ipiv, b);
}
/// <summary>
/// Solves A*X=B for X using a previously factored A matrix.
/// </summary>
/// <param name="columnsOfB">The number of columns of B.</param>
/// <param name="a">The factored A matrix.</param>
/// <param name="order">The order of the square matrix <paramref name="a"/>.</param>
/// <param name="ipiv">The pivot indices of <paramref name="a"/>.</param>
/// <param name="b">On entry the B matrix; on exit the X matrix.</param>
/// <remarks>This is equivalent to the GETRS LAPACK routine.</remarks>
public void LUSolveFactored(int columnsOfB, Complex[] a, int order, int[] ipiv, Complex[] b)
{
if (a == null)
{
throw new ArgumentNullException(nameof(a));
}
if (ipiv == null)
{
throw new ArgumentNullException(nameof(ipiv));
}
if (b == null)
{
throw new ArgumentNullException(nameof(b));
}
if (a.Length != order*order)
{
throw new ArgumentException("The array arguments must have the same length.", nameof(a));
}
if (ipiv.Length != order)
{
throw new ArgumentException("The array arguments must have the same length.", nameof(ipiv));
}
if (b.Length != order*columnsOfB)
{
throw new ArgumentException("The array arguments must have the same length.", nameof(b));
}
if (ReferenceEquals(a, b))
{
throw new ArgumentException("Arguments must be different objects.");
}
// Compute the column vector P*B
for (var i = 0; i < ipiv.Length; i++)
{
if (ipiv[i] == i)
{
continue;
}
var p = ipiv[i];
for (var j = 0; j < columnsOfB; j++)
{
var indexk = j*order;
var indexkp = indexk + p;
var indexkj = indexk + i;
(b[indexkp], b[indexkj]) = (b[indexkj], b[indexkp]);
}
}
// Solve L*Y = P*B
for (var k = 0; k < order; k++)
{
var korder = k*order;
for (var i = k + 1; i < order; i++)
{
for (var j = 0; j < columnsOfB; j++)
{
var index = j*order;
b[i + index] -= b[k + index]*a[i + korder];
}
}
}
// Solve U*X = Y;
for (var k = order - 1; k >= 0; k--)
{
var korder = k + (k*order);
for (var j = 0; j < columnsOfB; j++)
{
b[k + (j*order)] /= a[korder];
}
korder = k*order;
for (var i = 0; i < k; i++)
{
for (var j = 0; j < columnsOfB; j++)
{
var index = j*order;
b[i + index] -= b[k + index]*a[i + korder];
}
}
}
}
/// <summary>
/// Computes the Cholesky factorization of A.
/// </summary>
/// <param name="a">On entry, a square, positive definite matrix. On exit, the matrix is overwritten with the
/// the Cholesky factorization.</param>
/// <param name="order">The number of rows or columns in the matrix.</param>
/// <remarks>This is equivalent to the POTRF LAPACK routine.</remarks>
public void CholeskyFactor(Complex[] a, int order)
{
if (a == null)
{
throw new ArgumentNullException(nameof(a));
}
var tmpColumn = new Complex[order];
// Main loop - along the diagonal
for (var ij = 0; ij < order; ij++)
{
// "Pivot" element
var tmpVal = a[(ij*order) + ij];
if (tmpVal.Real > 0.0)
{
tmpVal = tmpVal.SquareRoot();
a[(ij*order) + ij] = tmpVal;
tmpColumn[ij] = tmpVal;
// Calculate multipliers and copy to local column
// Current column, below the diagonal