diff --git a/Algorithms.Tests/LinearAlgebra/Distances/ChebyshevTests.cs b/Algorithms.Tests/LinearAlgebra/Distances/ChebyshevTests.cs new file mode 100644 index 00000000..2a5ca6e9 --- /dev/null +++ b/Algorithms.Tests/LinearAlgebra/Distances/ChebyshevTests.cs @@ -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(); + } +} \ No newline at end of file diff --git a/Algorithms/LinearAlgebra/Distances/Chebyshev.cs b/Algorithms/LinearAlgebra/Distances/Chebyshev.cs new file mode 100644 index 00000000..b710fb1d --- /dev/null +++ b/Algorithms/LinearAlgebra/Distances/Chebyshev.cs @@ -0,0 +1,31 @@ +using System; +using System.Linq; + +namespace Algorithms.LinearAlgebra.Distances; + +/// +/// 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. +/// +public static class Chebyshev +{ + /// + /// Calculate Chebyshev distance for two N-Dimensional points. + /// + /// First N-Dimensional point. + /// Second N-Dimensional point. + /// Calculated Chebyshev distance. + 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(); + } +} diff --git a/README.md b/README.md index bfd6df40..6d4e1e93 100644 --- a/README.md +++ b/README.md @@ -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)