Skip to content

Commit

Permalink
Add Chebyshev distance algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
Paulofalcao2002 committed Nov 6, 2024
1 parent cb4760c commit be8a26a
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
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 double[] { 1.0, 1.0 }, new double[] { 2.0, 2.0 }, 1.0)]

Check notice on line 10 in Algorithms.Tests/LinearAlgebra/Distances/ChebyshevTests.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Algorithms.Tests/LinearAlgebra/Distances/ChebyshevTests.cs#L10

Remove the array type; it is redundant.
[TestCase(new double[] { 1.0, 1.0, 9.0 }, new double[] { 2.0, 2.0, -5.2 }, 14.2)]

Check notice on line 11 in Algorithms.Tests/LinearAlgebra/Distances/ChebyshevTests.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Algorithms.Tests/LinearAlgebra/Distances/ChebyshevTests.cs#L11

Remove the array type; it is redundant.
[TestCase(new double[] { 1.0, 2.0, 3.0 }, new double[] { 1.0, 2.0, 3.0 }, 0.0)]

Check notice on line 12 in Algorithms.Tests/LinearAlgebra/Distances/ChebyshevTests.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Algorithms.Tests/LinearAlgebra/Distances/ChebyshevTests.cs#L12

Remove the array type; it is redundant.
[TestCase(new double[] { 1.0, 2.0, 3.0, 4.0 }, new double[] { 1.75, 2.25, -3.0, 0.5 }, 6.0)]

Check notice on line 13 in Algorithms.Tests/LinearAlgebra/Distances/ChebyshevTests.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Algorithms.Tests/LinearAlgebra/Distances/ChebyshevTests.cs#L13

Remove the array type; it is redundant.
public void DistanceTest(double[] point1, double[] point2, double expectedDistance)
{
Chebyshev.Distance(point1, point2).Should().BeApproximately(expectedDistance, 0.01);
}

[TestCase(new double[] { 2.0, 3.0 }, new double[] { -1.0 })]

Check notice on line 19 in Algorithms.Tests/LinearAlgebra/Distances/ChebyshevTests.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Algorithms.Tests/LinearAlgebra/Distances/ChebyshevTests.cs#L19

Remove the array type; it is redundant.
[TestCase(new double[] { 1.0 }, new double[] { 1.0, 2.0, 3.0 })]

Check notice on line 20 in Algorithms.Tests/LinearAlgebra/Distances/ChebyshevTests.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Algorithms.Tests/LinearAlgebra/Distances/ChebyshevTests.cs#L20

Remove the array type; it is redundant.
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

0 comments on commit be8a26a

Please sign in to comment.