Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions Algorithms.Tests/LinearAlgebra/Distances/ChebyshevTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using NUnit.Framework;
using Algorithms.LinearAlgebra.Distances;
using FluentAssertions;
using System;

namespace Algorithms.Tests.LinearAlgebra.Distances;

public class ChebyshevTests
{
[TestCase(new[] { 1.0, 1.0 }, new[] { 2.0, 2.0 }, 1.0)]
[TestCase(new[] { 1.0, 1.0, 9.0 }, new[] { 2.0, 2.0, -5.2 }, 14.2)]
[TestCase(new[] { 1.0, 2.0, 3.0 }, new[] { 1.0, 2.0, 3.0 }, 0.0)]
[TestCase(new[] { 1.0, 2.0, 3.0, 4.0 }, new[] { 1.75, 2.25, -3.0, 0.5 }, 6.0)]
public void DistanceTest(double[] point1, double[] point2, double expectedDistance)
{
Chebyshev.Distance(point1, point2).Should().BeApproximately(expectedDistance, 0.01);
}

[TestCase(new[] { 2.0, 3.0 }, new[] { -1.0 })]
[TestCase(new[] { 1.0 }, new[] { 1.0, 2.0, 3.0 })]
public void DistanceThrowsArgumentExceptionOnDifferentPointDimensions(double[] point1, double[] point2)
{
Action action = () => Chebyshev.Distance(point1, point2);
action.Should().Throw<ArgumentException>();
}
}
31 changes: 31 additions & 0 deletions Algorithms/LinearAlgebra/Distances/Chebyshev.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Linq;

namespace Algorithms.LinearAlgebra.Distances;

/// <summary>
/// Implementation of Chebyshev distance.
/// It is the maximum absolute difference between the measures in all dimensions of two points.
/// In other words, it is the maximum distance one has to travel along any coordinate axis to get from one point to another.
///
/// It is commonly used in various fields such as chess, warehouse logistics, and more.
/// </summary>
public static class Chebyshev
{
/// <summary>
/// Calculate Chebyshev distance for two N-Dimensional points.
/// </summary>
/// <param name="point1">First N-Dimensional point.</param>
/// <param name="point2">Second N-Dimensional point.</param>
/// <returns>Calculated Chebyshev distance.</returns>
public static double Distance(double[] point1, double[] point2)
{
if (point1.Length != point2.Length)
{
throw new ArgumentException("Both points should have the same dimensionality");
}

// distance = max(|x1-y1|, |x2-y2|, ..., |xn-yn|)
return point1.Zip(point2, (x1, x2) => Math.Abs(x1 - x2)).Max();
}
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ find more than one implementation for the same objective but using different alg
* [IHeuristicKnapsackSolver](./Algorithms/Knapsack/IHeuristicKnapsackSolver.cs)
* [Linear Algebra](./Algorithms/LinearAlgebra)
* [Distances](./Algorithms/LinearAlgebra/Distances)
* [Chebyshev](./Algorithms/LinearAlgebra/Distances/Chebyshev.cs)
* [Euclidean](./Algorithms/LinearAlgebra/Distances/Euclidean.cs)
* [Manhattan](./Algorithms/LinearAlgebra/Distances/Manhattan.cs)
* [Eigenvalue](./Algorithms/LinearAlgebra/Eigenvalue)
Expand Down
Loading