11// Copyright (c) Six Labors.
22// Licensed under the Six Labors Split License.
3- using System . Numerics ;
43
54namespace SixLabors . ImageSharp . Common . Helpers ;
65
@@ -9,9 +8,7 @@ namespace SixLabors.ImageSharp.Common.Helpers;
98/// This class applies Gaussian Elimination to transform the matrix into row echelon form and then performs back substitution to find the solution vector.
109/// This implementation is based on: https://www.algorithm-archive.org/contents/gaussian_elimination/gaussian_elimination.html
1110/// </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
1512{
1613 /// <summary>
1714 /// 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>
2320 /// The matrix passed to this method must be a square matrix.
2421 /// If the matrix is singular (i.e., has no unique solution), an <see cref="NotSupportedException"/> will be thrown.
2522 /// </remarks>
26- public static void Solve ( TNumber [ ] [ ] matrix , TNumber [ ] result )
23+ public static void Solve ( float [ ] [ ] matrix , float [ ] result )
2724 {
2825 TransformToRowEchelonForm ( matrix , result ) ;
2926 ApplyBackSubstitution ( matrix , result ) ;
3027 }
3128
32- private static void TransformToRowEchelonForm ( TNumber [ ] [ ] matrix , TNumber [ ] result )
29+ private static void TransformToRowEchelonForm ( float [ ] [ ] matrix , float [ ] result )
3330 {
3431 int colCount = matrix . Length ;
3532 int rowCount = matrix [ 0 ] . Length ;
3633 int pivotRow = 0 ;
3734 for ( int pivotCol = 0 ; pivotCol < colCount ; pivotCol ++ )
3835 {
39- TNumber maxValue = TNumber . Abs ( matrix [ pivotRow ] [ pivotCol ] ) ;
36+ float maxValue = float . Abs ( matrix [ pivotRow ] [ pivotCol ] ) ;
4037 int maxIndex = pivotRow ;
4138 for ( int r = pivotRow + 1 ; r < rowCount ; r ++ )
4239 {
43- TNumber value = TNumber . Abs ( matrix [ r ] [ pivotCol ] ) ;
40+ float value = float . Abs ( matrix [ r ] [ pivotCol ] ) ;
4441 if ( value > maxValue )
4542 {
4643 maxIndex = r ;
4744 maxValue = value ;
4845 }
4946 }
5047
51- if ( matrix [ maxIndex ] [ pivotCol ] == TNumber . Zero )
48+ if ( matrix [ maxIndex ] [ pivotCol ] == 0 )
5249 {
5350 throw new NotSupportedException ( "Matrix is singular and cannot be solve" ) ;
5451 }
@@ -58,21 +55,21 @@ private static void TransformToRowEchelonForm(TNumber[][] matrix, TNumber[] resu
5855
5956 for ( int r = pivotRow + 1 ; r < rowCount ; r ++ )
6057 {
61- TNumber fraction = matrix [ r ] [ pivotCol ] / matrix [ pivotRow ] [ pivotCol ] ;
58+ float fraction = matrix [ r ] [ pivotCol ] / matrix [ pivotRow ] [ pivotCol ] ;
6259 for ( int c = pivotCol + 1 ; c < colCount ; c ++ )
6360 {
6461 matrix [ r ] [ c ] -= matrix [ pivotRow ] [ c ] * fraction ;
6562 }
6663
6764 result [ r ] -= result [ pivotRow ] * fraction ;
68- matrix [ r ] [ pivotCol ] = TNumber . Zero ;
65+ matrix [ r ] [ pivotCol ] = 0 ;
6966 }
7067
7168 pivotRow ++ ;
7269 }
7370 }
7471
75- private static void ApplyBackSubstitution ( TNumber [ ] [ ] matrix , TNumber [ ] result )
72+ private static void ApplyBackSubstitution ( float [ ] [ ] matrix , float [ ] result )
7673 {
7774 int rowCount = matrix [ 0 ] . Length ;
7875
0 commit comments