From aa221977da1a500a8a1b48a78f51e5a806380885 Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Tue, 17 Jun 2014 01:02:38 +0200 Subject: [PATCH] Distance: fix bug in Hamming distance that skipped the head pair #220 --- src/Numerics/Distance.cs | 4 ++-- src/UnitTests/DistanceTests.cs | 19 +++++++++++++++++++ src/UnitTests/UnitTests.csproj | 1 + 3 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 src/UnitTests/DistanceTests.cs diff --git a/src/Numerics/Distance.cs b/src/Numerics/Distance.cs index 425c6152..05ddd6ff 100644 --- a/src/Numerics/Distance.cs +++ b/src/Numerics/Distance.cs @@ -351,7 +351,7 @@ namespace MathNet.Numerics { if (a.Length != b.Length) throw new ArgumentOutOfRangeException("b"); int count = 0; - for (int i = 1; i < a.Length; i++) + for (int i = 0; i < a.Length; i++) { if (a[i] != b[i]) { @@ -368,7 +368,7 @@ namespace MathNet.Numerics { if (a.Length != b.Length) throw new ArgumentOutOfRangeException("b"); int count = 0; - for (int i = 1; i < a.Length; i++) + for (int i = 0; i < a.Length; i++) { if (a[i] != b[i]) { diff --git a/src/UnitTests/DistanceTests.cs b/src/UnitTests/DistanceTests.cs new file mode 100644 index 00000000..b18e80a6 --- /dev/null +++ b/src/UnitTests/DistanceTests.cs @@ -0,0 +1,19 @@ +using NUnit.Framework; + +namespace MathNet.Numerics.UnitTests +{ + [TestFixture] + public class DistanceTests + { + [Test] + public void Hamming() + { + Assert.That(Distance.Hamming(new[] { 0.0, 0.0 }, new[] { 0.0, 0.0 }), Is.EqualTo(0.0)); + Assert.That(Distance.Hamming(new[] { 1.0, 0.0 }, new[] { 1.0, 0.0 }), Is.EqualTo(0.0)); + Assert.That(Distance.Hamming(new[] { 0.0, 0.0 }, new[] { 1.0, 0.0 }), Is.EqualTo(1.0)); + Assert.That(Distance.Hamming(new[] { 0.0, 0.0 }, new[] { 0.0, 1.0 }), Is.EqualTo(1.0)); + Assert.That(Distance.Hamming(new[] { 0.0, 0.0 }, new[] { 1.0, 1.0 }), Is.EqualTo(2.0)); + Assert.That(Distance.Hamming(new[] { 1.0, 0.0 }, new[] { 0.0, 1.0 }), Is.EqualTo(2.0)); + } + } +} diff --git a/src/UnitTests/UnitTests.csproj b/src/UnitTests/UnitTests.csproj index cd1e2369..069ba696 100644 --- a/src/UnitTests/UnitTests.csproj +++ b/src/UnitTests/UnitTests.csproj @@ -85,6 +85,7 @@ +