Skip to content

Commit

Permalink
From double to float
Browse files Browse the repository at this point in the history
  • Loading branch information
Kees Schollaart committed Dec 29, 2024
1 parent 07e5f13 commit 7cbab28
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 57 deletions.
18 changes: 9 additions & 9 deletions src/SortCS/Kalman/KalmanBoxTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace SortCS.Kalman;
internal class KalmanBoxTracker
{
private static readonly Matrix _stateTransitioningMatrix = new(
new double[,]
new float[,]
{
{ 1, 0, 0, 0, 1, 0, 0 },
{ 0, 1, 0, 0, 0, 1, 0 },
Expand All @@ -18,7 +18,7 @@ internal class KalmanBoxTracker
});

private static readonly Matrix _measurementFunction = new(
new double[,]
new float[,]
{
{ 1, 0, 0, 0, 0, 0, 0 },
{ 0, 1, 0, 0, 0, 0, 0 },
Expand All @@ -27,7 +27,7 @@ internal class KalmanBoxTracker
});

private static readonly Matrix _uncertaintyCovariances = new(
new double[,]
new float[,]
{
{ 10, 0, 0, 0, 0, 0, 0 },
{ 0, 10, 0, 0, 0, 0, 0 },
Expand All @@ -38,7 +38,7 @@ internal class KalmanBoxTracker
{ 0, 0, 0, 0, 0, 0, 10000 }
});

private static readonly Matrix _measurementUncertainty = new(new double[,]
private static readonly Matrix _measurementUncertainty = new(new float[,]
{
{ 1, 0, 0, 0 },
{ 0, 1, 0, 0 },
Expand All @@ -47,15 +47,15 @@ internal class KalmanBoxTracker
});

private static readonly Matrix _processUncertainty = new(
new[,]
new float[,]
{
{ 1, 0, 0, 0, 0, 0, 0 },
{ 0, 1, 0, 0, 0, 0, 0 },
{ 0, 0, 1, 0, 0, 0, 0 },
{ 0, 0, 0, 1, 0, 0, 0 },
{ 0, 0, 0, 0, .01, 0, 0 },
{ 0, 0, 0, 0, 0, .01, 0 },
{ 0, 0, 0, 0, 0, 0, .0001 }
{ 0, 0, 0, 0, .01f, 0, 0 },
{ 0, 0, 0, 0, 0, .01f, 0 },
{ 0, 0, 0, 0, 0, 0, .0001f }
});

private readonly KalmanFilter _filter;
Expand Down Expand Up @@ -95,7 +95,7 @@ public RectangleF Predict()
private static Vector ToMeasurement(RectangleF box)
{
var center = new PointF(box.Left + (box.Width / 2f), box.Top + (box.Height / 2f));
return new Vector(center.X, center.Y, box.Width * (double)box.Height, box.Width / (double)box.Height);
return new Vector(center.X, center.Y, box.Width * box.Height, box.Width / box.Height);
}

private static RectangleF ToBoundingBox(Vector currentState)
Expand Down
6 changes: 3 additions & 3 deletions src/SortCS/Kalman/KalmanFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ internal class KalmanFilter
private readonly int _stateSize;
private readonly int _measurementSize;
private readonly Matrix _identity;
private readonly double _alphaSq;
private readonly float _alphaSq;

private Vector _currentState;
private Matrix _uncertaintyCovariances;
Expand All @@ -26,7 +26,7 @@ public KalmanFilter(int stateSize, int measurementSize)
_stateSize = stateSize;
_measurementSize = measurementSize;
_identity = Matrix.Identity(stateSize);
_alphaSq = 1.0d;
_alphaSq = 1.0f;

StateTransitionMatrix = _identity; // F
MeasurementFunction = new Matrix(_measurementSize, _stateSize); // H
Expand Down Expand Up @@ -99,7 +99,7 @@ public Matrix MeasurementFunction
: throw new ArgumentException($"Matrix must be of size {_measurementSize}x{_stateSize}.", nameof(value));
}

public void SetState(int index, double values)
public void SetState(int index, float values)
{
_currentState[index] = values;
}
Expand Down
62 changes: 31 additions & 31 deletions src/SortCS/Kalman/Matrix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ namespace SortCS.Kalman;
[DebuggerTypeProxy(typeof(MatrixDisplay))]
internal class Matrix
{
private readonly double[,] _values;
private readonly float[,] _values;

public Matrix(double[,] values)
public Matrix(float[,] values)
{
_values = values;
Rows = _values.GetLength(0);
Expand All @@ -26,13 +26,13 @@ public Matrix(int[,] values)
{
for (var col = 0; col < Columns; col++)
{
_values[row, col] = (double)values[row, col];
_values[row, col] = (float)values[row, col];
}
}
}

public Matrix(int rows, int columns)
: this(new double[rows, columns])
: this(new float[rows, columns])
{
}

Expand All @@ -49,7 +49,7 @@ public Matrix Transposed
return field;
}

var result = new double[Columns, Rows];
var result = new float[Columns, Rows];

for (var row = 0; row < Rows; row++)
{
Expand All @@ -72,13 +72,13 @@ public Matrix Inverted
Debug.Assert(Rows == Columns);

var (lu, indices) = GetDecomposition();
var result = new double[Rows, Columns];
var result = new float[Rows, Columns];

for (var col = 0; col < Columns; col++)
{
var column = new double[Columns];
var column = new float[Columns];

column[col] = 1.0d;
column[col] = 1.0f;

var x = BackSubstitution(lu, indices, column);

Expand All @@ -98,7 +98,7 @@ public Matrix Inverted
{
Debug.Assert(first.Rows == second.Rows && first.Columns == second.Columns);

var result = new double[first.Rows, first.Columns];
var result = new float[first.Rows, first.Columns];

for (var row = 0; row < first.Rows; row++)
{
Expand All @@ -115,7 +115,7 @@ public Matrix Inverted
{
Debug.Assert(first.Rows == second.Rows && first.Columns == second.Columns);

var result = new double[first.Rows, first.Columns];
var result = new float[first.Rows, first.Columns];

for (var row = 0; row < first.Rows; row++)
{
Expand All @@ -128,9 +128,9 @@ public Matrix Inverted
return new Matrix(result);
}

public static Matrix operator *(double scalar, Matrix matrix)
public static Matrix operator *(float scalar, Matrix matrix)
{
var result = new double[matrix.Rows, matrix.Columns];
var result = new float[matrix.Rows, matrix.Columns];

for (var row = 0; row < matrix.Rows; row++)
{
Expand All @@ -143,26 +143,26 @@ public Matrix Inverted
return new Matrix(result);
}

public static Matrix operator *(Matrix matrix, double scalar)
public static Matrix operator *(Matrix matrix, float scalar)
{
return scalar * matrix;
}

public static Matrix operator *(Matrix first, Matrix second)
{
var result = new double[first.Rows, second.Columns];
var result = new float[first.Rows, second.Columns];
var rows = result.GetLength(0);
var cols = result.GetLength(1);

for (var row = 0; row < rows; row++)
{
for (var col = 0; col < cols; col++)
{
var bufFirst = ArrayPool<double>.Shared.Rent(first.Columns);
var bufSecond = ArrayPool<double>.Shared.Rent(first.Rows);
var bufFirst = ArrayPool<float>.Shared.Rent(first.Columns);
var bufSecond = ArrayPool<float>.Shared.Rent(first.Rows);
result[row, col] = first.Row(row, bufFirst).Dot(second.Column(col, bufSecond));
ArrayPool<double>.Shared.Return(bufFirst, true);
ArrayPool<double>.Shared.Return(bufSecond, true);
ArrayPool<float>.Shared.Return(bufFirst, true);
ArrayPool<float>.Shared.Return(bufSecond, true);
}
}

Expand All @@ -171,13 +171,13 @@ public Matrix Inverted

public static Matrix Identity(int size)
{
var identity = new double[size, size];
var identity = new float[size, size];

for (var row = 0; row < size; row++)
{
for (var col = 0; col < size; col++)
{
identity[row, col] = row == col ? 1.0d : 0d;
identity[row, col] = row == col ? 1.0f : 0f;
}
}

Expand All @@ -193,24 +193,24 @@ public Vector Dot(Vector vector)
{
Debug.Assert(Columns == vector.Size);

var result = new double[Rows];
var result = new float[Rows];
for (var i = 0; i < Rows; i++)
{
var buf = ArrayPool<double>.Shared.Rent(Columns);
var buf = ArrayPool<float>.Shared.Rent(Columns);
var row = Row(i, buf);
result[i] = row.Dot(vector);
ArrayPool<double>.Shared.Return(buf);
ArrayPool<float>.Shared.Return(buf);
}

return new Vector(result);
}

public Vector Row(int index)
{
return Row(index, new double[Columns]);
return Row(index, new float[Columns]);
}

public Vector Row(int index, double[] buffer)
public Vector Row(int index, float[] buffer)
{
Debug.Assert(index <= Rows);
for (var col = 0; col < Columns; col++)
Expand All @@ -221,7 +221,7 @@ public Vector Row(int index, double[] buffer)
return new Vector(buffer, Columns);
}

public Vector Column(int index, double[] buf)
public Vector Column(int index, float[] buf)
{
Debug.Assert(index <= Columns);
for (var row = 0; row < Rows; row++)
Expand All @@ -232,9 +232,9 @@ public Vector Column(int index, double[] buf)
return new Vector(buf, Rows);
}

private double[] BackSubstitution(double[,] lu, int[] indices, double[] b)
private float[] BackSubstitution(float[,] lu, int[] indices, float[] b)
{
var x = (double[])b.Clone();
var x = (float[])b.Clone();
var ii = 0;
for (var row = 0; row < Rows; row++)
{
Expand Down Expand Up @@ -272,12 +272,12 @@ private double[] BackSubstitution(double[,] lu, int[] indices, double[] b)
return x;
}

private (double[,] Result, int[] Indices) GetDecomposition()
private (float[,] Result, int[] Indices) GetDecomposition()
{
var maxRow = 0;
var vv = Enumerable.Range(0, Rows)
.Select(row => 1.0d / Enumerable.Range(0, Columns).Select(col => Math.Abs(_values[row, col])).Max()).ToArray();
var result = (double[,])_values.Clone();
var result = (float[,])_values.Clone();
var index = new int[Rows];
var d = 1.0d;

Expand Down Expand Up @@ -329,7 +329,7 @@ private double[] BackSubstitution(double[,] lu, int[] indices, double[] b)

if (col != Rows - 1)
{
var tmp = 1.0d / result[col, col];
var tmp = 1.0f / result[col, col];
for (var row = col + 1; row < Rows; row++)
{
result[row, col] *= tmp;
Expand Down
20 changes: 10 additions & 10 deletions src/SortCS/Kalman/Vector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ namespace SortCS.Kalman;

internal struct Vector
{
private readonly double[] _values;
private readonly float[] _values;

public Vector(params double[] values)
public Vector(params float[] values)
{
_values = values;
Size = values.Length;
}

public Vector(double[] values, int size)
public Vector(float[] values, int size)
{
if (size > values.Length)
{
Expand All @@ -28,13 +28,13 @@ public Vector(double[] values, int size)

public Vector(int size)
{
_values = new double[size];
_values = new float[size];
Size = size;
}

public int Size { get; }

public double this[int index]
public float this[int index]
{
get => index <= Size ? _values[index] : throw new ArgumentOutOfRangeException(nameof(index));
set
Expand All @@ -51,7 +51,7 @@ public double this[int index]
public static Vector operator -(Vector first, Vector second)
{
Debug.Assert(first.Size == second.Size);
var resultArray = new double[first.Size];
var resultArray = new float[first.Size];
for (var i = 0; i < first.Size; i++)
{
resultArray[i] = first[i] - second[i];
Expand All @@ -63,7 +63,7 @@ public double this[int index]
public static Vector operator +(Vector first, Vector second)
{
Debug.Assert(first.Size == second.Size);
var resultArray = new double[first.Size];
var resultArray = new float[first.Size];
for (var i = 0; i < first.Size; i++)
{
resultArray[i] = first[i] + second[i];
Expand All @@ -72,11 +72,11 @@ public double this[int index]
return new Vector(resultArray);
}

public double Dot(Vector other)
public float Dot(Vector other)
{
Debug.Assert(Size == other.Size, $"Vectors should be of equal length {Size} != {other.Size}.");
Debug.Assert(Size > 0);
double sum = 0;
float sum = 0;
for (var i = 0; i < Size; i++)
{
sum += _values[i] * other[i];
Expand All @@ -90,7 +90,7 @@ public override string ToString()
return string.Join(", ", _values.Select(v => v.ToString("###0.00", CultureInfo.InvariantCulture)));
}

internal Vector Append(params double[] extraElements)
internal Vector Append(params float[] extraElements)
{
return new Vector(_values.Take(Size).Concat(extraElements).ToArray());
}
Expand Down
8 changes: 4 additions & 4 deletions src/SortCS/SortTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,16 +158,16 @@ private void Log(IEnumerable<Track> tracks)
return (matchedBoxes, unmatchedBoxes);
}

private static double IoU(RectangleF a, RectangleF b)
private static float IoU(RectangleF a, RectangleF b)
{
var intersection = RectangleF.Intersect(a, b);
if (intersection.IsEmpty)
{
return 0;
}

var intersectArea = (1.0 + intersection.Width) * (1.0 + intersection.Height);
var unionArea = ((1.0 + a.Width) * (1.0 + a.Height)) + ((1.0 + b.Width) * (1.0 + b.Height)) - intersectArea;
return intersectArea / (unionArea + 1e-5);
var intersectArea = (1.0f + intersection.Width) * (1.0f + intersection.Height);
var unionArea = ((1.0f + a.Width) * (1.0f + a.Height)) + ((1.0f + b.Width) * (1.0f + b.Height)) - intersectArea;
return intersectArea / (unionArea + 1e-5f);
}
}

0 comments on commit 7cbab28

Please sign in to comment.