Skip to content

Commit e244efa

Browse files
committed
Adjust GaussianEliminationSolver, replace generic type with float
1 parent 5a79591 commit e244efa

File tree

3 files changed

+11
-14
lines changed

3 files changed

+11
-14
lines changed

src/ImageSharp/Common/Helpers/GaussianEliminationSolver.cs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
3-
using System.Numerics;
43

54
namespace 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

src/ImageSharp/Common/Helpers/QuadDistortionHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public static Matrix4x4 ComputeQuadDistortMatrix(Rectangle rectangle, PointF top
6060
q4.Y,
6161
];
6262

63-
GaussianEliminationSolver<float>.Solve(matrixData, b);
63+
GaussianEliminationSolver.Solve(matrixData, b);
6464

6565
#pragma warning disable SA1117
6666
Matrix4x4 projectionMatrix = new(

tests/ImageSharp.Tests/Common/GaussianEliminationSolverTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class GaussianEliminationSolverTest
1111
[MemberData(nameof(MatrixTestData))]
1212
public void CanSolve(float[][] matrix, float[] result, float[] expected)
1313
{
14-
GaussianEliminationSolver<float>.Solve(matrix, result);
14+
GaussianEliminationSolver.Solve(matrix, result);
1515

1616
for (int i = 0; i < expected.Length; i++)
1717
{

0 commit comments

Comments
 (0)