-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cb4760c
commit be8a26a
Showing
3 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
Algorithms.Tests/LinearAlgebra/Distances/ChebyshevTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)] | ||
[TestCase(new double[] { 1.0, 1.0, 9.0 }, new double[] { 2.0, 2.0, -5.2 }, 14.2)] | ||
[TestCase(new double[] { 1.0, 2.0, 3.0 }, new double[] { 1.0, 2.0, 3.0 }, 0.0)] | ||
[TestCase(new double[] { 1.0, 2.0, 3.0, 4.0 }, new double[] { 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 double[] { 2.0, 3.0 }, new double[] { -1.0 })] | ||
[TestCase(new double[] { 1.0 }, new double[] { 1.0, 2.0, 3.0 })] | ||
public void DistanceThrowsArgumentExceptionOnDifferentPointDimensions(double[] point1, double[] point2) | ||
{ | ||
Action action = () => Chebyshev.Distance(point1, point2); | ||
action.Should().Throw<ArgumentException>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters