1
1
// Copyright (c) Six Labors.
2
2
// Licensed under the Six Labors Split License.
3
- using System . Numerics ;
4
3
5
4
namespace SixLabors . ImageSharp . Common . Helpers ;
6
5
@@ -9,9 +8,7 @@ namespace SixLabors.ImageSharp.Common.Helpers;
9
8
/// This class applies Gaussian Elimination to transform the matrix into row echelon form and then performs back substitution to find the solution vector.
10
9
/// This implementation is based on: https://www.algorithm-archive.org/contents/gaussian_elimination/gaussian_elimination.html
11
10
/// </summary>
12
- /// <typeparam name="TNumber">The type of numbers used in the matrix and solution vector. Must implement the <see cref="INumber{TNumber}"/> interface.</typeparam>
13
- internal static class GaussianEliminationSolver < TNumber >
14
- where TNumber : INumber < TNumber >
11
+ internal static class GaussianEliminationSolver
15
12
{
16
13
/// <summary>
17
14
/// Solves the system of linear equations represented by the given matrix and result vector using Gaussian Elimination.
@@ -23,32 +20,32 @@ internal static class GaussianEliminationSolver<TNumber>
23
20
/// The matrix passed to this method must be a square matrix.
24
21
/// If the matrix is singular (i.e., has no unique solution), an <see cref="NotSupportedException"/> will be thrown.
25
22
/// </remarks>
26
- public static void Solve ( TNumber [ ] [ ] matrix , TNumber [ ] result )
23
+ public static void Solve ( float [ ] [ ] matrix , float [ ] result )
27
24
{
28
25
TransformToRowEchelonForm ( matrix , result ) ;
29
26
ApplyBackSubstitution ( matrix , result ) ;
30
27
}
31
28
32
- private static void TransformToRowEchelonForm ( TNumber [ ] [ ] matrix , TNumber [ ] result )
29
+ private static void TransformToRowEchelonForm ( float [ ] [ ] matrix , float [ ] result )
33
30
{
34
31
int colCount = matrix . Length ;
35
32
int rowCount = matrix [ 0 ] . Length ;
36
33
int pivotRow = 0 ;
37
34
for ( int pivotCol = 0 ; pivotCol < colCount ; pivotCol ++ )
38
35
{
39
- TNumber maxValue = TNumber . Abs ( matrix [ pivotRow ] [ pivotCol ] ) ;
36
+ float maxValue = float . Abs ( matrix [ pivotRow ] [ pivotCol ] ) ;
40
37
int maxIndex = pivotRow ;
41
38
for ( int r = pivotRow + 1 ; r < rowCount ; r ++ )
42
39
{
43
- TNumber value = TNumber . Abs ( matrix [ r ] [ pivotCol ] ) ;
40
+ float value = float . Abs ( matrix [ r ] [ pivotCol ] ) ;
44
41
if ( value > maxValue )
45
42
{
46
43
maxIndex = r ;
47
44
maxValue = value ;
48
45
}
49
46
}
50
47
51
- if ( matrix [ maxIndex ] [ pivotCol ] == TNumber . Zero )
48
+ if ( matrix [ maxIndex ] [ pivotCol ] == 0 )
52
49
{
53
50
throw new NotSupportedException ( "Matrix is singular and cannot be solve" ) ;
54
51
}
@@ -58,21 +55,21 @@ private static void TransformToRowEchelonForm(TNumber[][] matrix, TNumber[] resu
58
55
59
56
for ( int r = pivotRow + 1 ; r < rowCount ; r ++ )
60
57
{
61
- TNumber fraction = matrix [ r ] [ pivotCol ] / matrix [ pivotRow ] [ pivotCol ] ;
58
+ float fraction = matrix [ r ] [ pivotCol ] / matrix [ pivotRow ] [ pivotCol ] ;
62
59
for ( int c = pivotCol + 1 ; c < colCount ; c ++ )
63
60
{
64
61
matrix [ r ] [ c ] -= matrix [ pivotRow ] [ c ] * fraction ;
65
62
}
66
63
67
64
result [ r ] -= result [ pivotRow ] * fraction ;
68
- matrix [ r ] [ pivotCol ] = TNumber . Zero ;
65
+ matrix [ r ] [ pivotCol ] = 0 ;
69
66
}
70
67
71
68
pivotRow ++ ;
72
69
}
73
70
}
74
71
75
- private static void ApplyBackSubstitution ( TNumber [ ] [ ] matrix , TNumber [ ] result )
72
+ private static void ApplyBackSubstitution ( float [ ] [ ] matrix , float [ ] result )
76
73
{
77
74
int rowCount = matrix [ 0 ] . Length ;
78
75
0 commit comments