From e244efa77a932b1288d5f3dbc5f2ba584a777c1a Mon Sep 17 00:00:00 2001 From: Socolin Date: Wed, 31 Jul 2024 18:37:56 -0400 Subject: [PATCH] Adjust GaussianEliminationSolver, replace generic type with float --- .../Helpers/GaussianEliminationSolver.cs | 21 ++++++++----------- .../Common/Helpers/QuadDistortionHelper.cs | 2 +- .../Common/GaussianEliminationSolverTest.cs | 2 +- 3 files changed, 11 insertions(+), 14 deletions(-) diff --git a/src/ImageSharp/Common/Helpers/GaussianEliminationSolver.cs b/src/ImageSharp/Common/Helpers/GaussianEliminationSolver.cs index 3bb9eb3956..840bc34e38 100644 --- a/src/ImageSharp/Common/Helpers/GaussianEliminationSolver.cs +++ b/src/ImageSharp/Common/Helpers/GaussianEliminationSolver.cs @@ -1,6 +1,5 @@ // Copyright (c) Six Labors. // Licensed under the Six Labors Split License. -using System.Numerics; namespace SixLabors.ImageSharp.Common.Helpers; @@ -9,9 +8,7 @@ namespace SixLabors.ImageSharp.Common.Helpers; /// This class applies Gaussian Elimination to transform the matrix into row echelon form and then performs back substitution to find the solution vector. /// This implementation is based on: https://www.algorithm-archive.org/contents/gaussian_elimination/gaussian_elimination.html /// -/// The type of numbers used in the matrix and solution vector. Must implement the interface. -internal static class GaussianEliminationSolver - where TNumber : INumber +internal static class GaussianEliminationSolver { /// /// Solves the system of linear equations represented by the given matrix and result vector using Gaussian Elimination. @@ -23,24 +20,24 @@ internal static class GaussianEliminationSolver /// The matrix passed to this method must be a square matrix. /// If the matrix is singular (i.e., has no unique solution), an will be thrown. /// - public static void Solve(TNumber[][] matrix, TNumber[] result) + public static void Solve(float[][] matrix, float[] result) { TransformToRowEchelonForm(matrix, result); ApplyBackSubstitution(matrix, result); } - private static void TransformToRowEchelonForm(TNumber[][] matrix, TNumber[] result) + private static void TransformToRowEchelonForm(float[][] matrix, float[] result) { int colCount = matrix.Length; int rowCount = matrix[0].Length; int pivotRow = 0; for (int pivotCol = 0; pivotCol < colCount; pivotCol++) { - TNumber maxValue = TNumber.Abs(matrix[pivotRow][pivotCol]); + float maxValue = float.Abs(matrix[pivotRow][pivotCol]); int maxIndex = pivotRow; for (int r = pivotRow + 1; r < rowCount; r++) { - TNumber value = TNumber.Abs(matrix[r][pivotCol]); + float value = float.Abs(matrix[r][pivotCol]); if (value > maxValue) { maxIndex = r; @@ -48,7 +45,7 @@ internal static class GaussianEliminationSolver } } - if (matrix[maxIndex][pivotCol] == TNumber.Zero) + if (matrix[maxIndex][pivotCol] == 0) { throw new NotSupportedException("Matrix is singular and cannot be solve"); } @@ -58,21 +55,21 @@ internal static class GaussianEliminationSolver for (int r = pivotRow + 1; r < rowCount; r++) { - TNumber fraction = matrix[r][pivotCol] / matrix[pivotRow][pivotCol]; + float fraction = matrix[r][pivotCol] / matrix[pivotRow][pivotCol]; for (int c = pivotCol + 1; c < colCount; c++) { matrix[r][c] -= matrix[pivotRow][c] * fraction; } result[r] -= result[pivotRow] * fraction; - matrix[r][pivotCol] = TNumber.Zero; + matrix[r][pivotCol] = 0; } pivotRow++; } } - private static void ApplyBackSubstitution(TNumber[][] matrix, TNumber[] result) + private static void ApplyBackSubstitution(float[][] matrix, float[] result) { int rowCount = matrix[0].Length; diff --git a/src/ImageSharp/Common/Helpers/QuadDistortionHelper.cs b/src/ImageSharp/Common/Helpers/QuadDistortionHelper.cs index 9fd6e84c75..1f114633d6 100644 --- a/src/ImageSharp/Common/Helpers/QuadDistortionHelper.cs +++ b/src/ImageSharp/Common/Helpers/QuadDistortionHelper.cs @@ -60,7 +60,7 @@ internal static class QuadDistortionHelper q4.Y, ]; - GaussianEliminationSolver.Solve(matrixData, b); + GaussianEliminationSolver.Solve(matrixData, b); #pragma warning disable SA1117 Matrix4x4 projectionMatrix = new( diff --git a/tests/ImageSharp.Tests/Common/GaussianEliminationSolverTest.cs b/tests/ImageSharp.Tests/Common/GaussianEliminationSolverTest.cs index 85bc68b826..3964f40517 100644 --- a/tests/ImageSharp.Tests/Common/GaussianEliminationSolverTest.cs +++ b/tests/ImageSharp.Tests/Common/GaussianEliminationSolverTest.cs @@ -11,7 +11,7 @@ public class GaussianEliminationSolverTest [MemberData(nameof(MatrixTestData))] public void CanSolve(float[][] matrix, float[] result, float[] expected) { - GaussianEliminationSolver.Solve(matrix, result); + GaussianEliminationSolver.Solve(matrix, result); for (int i = 0; i < expected.Length; i++) {