Skip to content

Commit c51d84d

Browse files
Use double precision for accuracy
1 parent 4544742 commit c51d84d

File tree

3 files changed

+23
-23
lines changed

3 files changed

+23
-23
lines changed

src/ImageSharp/Common/Helpers/GaussianEliminationSolver.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,24 @@ internal static class GaussianEliminationSolver
2020
/// The matrix passed to this method must be a square matrix.
2121
/// If the matrix is singular (i.e., has no unique solution), an <see cref="NotSupportedException"/> will be thrown.
2222
/// </remarks>
23-
public static void Solve(float[][] matrix, float[] result)
23+
public static void Solve(double[][] matrix, double[] result)
2424
{
2525
TransformToRowEchelonForm(matrix, result);
2626
ApplyBackSubstitution(matrix, result);
2727
}
2828

29-
private static void TransformToRowEchelonForm(float[][] matrix, float[] result)
29+
private static void TransformToRowEchelonForm(double[][] matrix, double[] result)
3030
{
3131
int colCount = matrix.Length;
3232
int rowCount = matrix[0].Length;
3333
int pivotRow = 0;
3434
for (int pivotCol = 0; pivotCol < colCount; pivotCol++)
3535
{
36-
float maxValue = float.Abs(matrix[pivotRow][pivotCol]);
36+
double maxValue = double.Abs(matrix[pivotRow][pivotCol]);
3737
int maxIndex = pivotRow;
3838
for (int r = pivotRow + 1; r < rowCount; r++)
3939
{
40-
float value = float.Abs(matrix[r][pivotCol]);
40+
double value = double.Abs(matrix[r][pivotCol]);
4141
if (value > maxValue)
4242
{
4343
maxIndex = r;
@@ -55,7 +55,7 @@ private static void TransformToRowEchelonForm(float[][] matrix, float[] result)
5555

5656
for (int r = pivotRow + 1; r < rowCount; r++)
5757
{
58-
float fraction = matrix[r][pivotCol] / matrix[pivotRow][pivotCol];
58+
double fraction = matrix[r][pivotCol] / matrix[pivotRow][pivotCol];
5959
for (int c = pivotCol + 1; c < colCount; c++)
6060
{
6161
matrix[r][c] -= matrix[pivotRow][c] * fraction;
@@ -69,7 +69,7 @@ private static void TransformToRowEchelonForm(float[][] matrix, float[] result)
6969
}
7070
}
7171

72-
private static void ApplyBackSubstitution(float[][] matrix, float[] result)
72+
private static void ApplyBackSubstitution(double[][] matrix, double[] result)
7373
{
7474
int rowCount = matrix[0].Length;
7575

src/ImageSharp/Common/Helpers/QuadDistortionHelper.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public static Matrix4x4 ComputeQuadDistortMatrix(
4040
PointF q3 = bottomRight;
4141
PointF q4 = bottomLeft;
4242

43-
float[][] matrixData =
43+
double[][] matrixData =
4444
[
4545
[p1.X, p1.Y, 1, 0, 0, 0, -p1.X * q1.X, -p1.Y * q1.X],
4646
[0, 0, 0, p1.X, p1.Y, 1, -p1.X * q1.Y, -p1.Y * q1.Y],
@@ -52,7 +52,7 @@ public static Matrix4x4 ComputeQuadDistortMatrix(
5252
[0, 0, 0, p4.X, p4.Y, 1, -p4.X * q4.Y, -p4.Y * q4.Y],
5353
];
5454

55-
float[] b =
55+
double[] b =
5656
[
5757
q1.X,
5858
q1.Y,
@@ -68,10 +68,10 @@ public static Matrix4x4 ComputeQuadDistortMatrix(
6868

6969
#pragma warning disable SA1117
7070
Matrix4x4 projectionMatrix = new(
71-
b[0], b[3], 0, b[6],
72-
b[1], b[4], 0, b[7],
71+
(float)b[0], (float)b[3], 0, (float)b[6],
72+
(float)b[1], (float)b[4], 0, (float)b[7],
7373
0, 0, 1, 0,
74-
b[2], b[5], 0, 1);
74+
(float)b[2], (float)b[5], 0, 1);
7575
#pragma warning restore SA1117
7676

7777
return projectionMatrix;

tests/ImageSharp.Tests/Common/GaussianEliminationSolverTest.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class GaussianEliminationSolverTest
99
{
1010
[Theory]
1111
[MemberData(nameof(MatrixTestData))]
12-
public void CanSolve(float[][] matrix, float[] result, float[] expected)
12+
public void CanSolve(double[][] matrix, double[] result, double[] expected)
1313
{
1414
GaussianEliminationSolver.Solve(matrix, result);
1515

@@ -19,44 +19,44 @@ public void CanSolve(float[][] matrix, float[] result, float[] expected)
1919
}
2020
}
2121

22-
public static TheoryData<float[][], float[], float[]> MatrixTestData
22+
public static TheoryData<double[][], double[], double[]> MatrixTestData
2323
{
2424
get
2525
{
26-
TheoryData<float[][], float[], float[]> data = [];
26+
TheoryData<double[][], double[], double[]> data = [];
2727
{
28-
float[][] matrix =
28+
double[][] matrix =
2929
[
3030
[2, 3, 4],
3131
[1, 2, 3],
3232
[3, -4, 0],
3333
];
34-
float[] result = [6, 4, 10];
35-
float[] expected = [18 / 11f, -14 / 11f, 18 / 11f];
34+
double[] result = [6, 4, 10];
35+
double[] expected = [18 / 11f, -14 / 11f, 18 / 11f];
3636
data.Add(matrix, result, expected);
3737
}
3838

3939
{
40-
float[][] matrix =
40+
double[][] matrix =
4141
[
4242
[1, 4, -1],
4343
[2, 5, 8],
4444
[1, 3, -3],
4545
];
46-
float[] result = [4, 15, 1];
47-
float[] expected = [1, 1, 1];
46+
double[] result = [4, 15, 1];
47+
double[] expected = [1, 1, 1];
4848
data.Add(matrix, result, expected);
4949
}
5050

5151
{
52-
float[][] matrix =
52+
double[][] matrix =
5353
[
5454
[-1, 0, 0],
5555
[0, 1, 0],
5656
[0, 0, 1],
5757
];
58-
float[] result = [1, 2, 3];
59-
float[] expected = [-1, 2, 3];
58+
double[] result = [1, 2, 3];
59+
double[] expected = [-1, 2, 3];
6060
data.Add(matrix, result, expected);
6161
}
6262

0 commit comments

Comments
 (0)