From 113271a701952df0eee5eac5ab0299e022f41dc9 Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Sat, 23 Sep 2017 16:05:20 +0200 Subject: [PATCH] Distance: more consistent argument checking --- src/Numerics/Distance.cs | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/Numerics/Distance.cs b/src/Numerics/Distance.cs index 6714ab1e..be3ca36a 100644 --- a/src/Numerics/Distance.cs +++ b/src/Numerics/Distance.cs @@ -116,6 +116,8 @@ namespace MathNet.Numerics /// public static double SSD(double[] a, double[] b) { + if (a.Length != b.Length) throw new ArgumentException(Resources.ArgumentVectorsSameLength); + var diff = new double[a.Length]; Control.LinearAlgebraProvider.SubtractArrays(a, b, diff); return Control.LinearAlgebraProvider.DotProduct(diff, diff); @@ -126,6 +128,8 @@ namespace MathNet.Numerics /// public static float SSD(float[] a, float[] b) { + if (a.Length != b.Length) throw new ArgumentException(Resources.ArgumentVectorsSameLength); + var diff = new float[a.Length]; Control.LinearAlgebraProvider.SubtractArrays(a, b, diff); return Control.LinearAlgebraProvider.DotProduct(diff, diff); @@ -217,7 +221,8 @@ namespace MathNet.Numerics /// public static double Chebyshev(double[] a, double[] b) { - if (a.Length != b.Length) throw new ArgumentOutOfRangeException("b"); + if (a.Length != b.Length) throw new ArgumentException(Resources.ArgumentVectorsSameLength); + double max = Math.Abs(a[0] - b[0]); for (int i = 1; i < a.Length; i++) { @@ -235,7 +240,8 @@ namespace MathNet.Numerics /// public static float Chebyshev(float[] a, float[] b) { - if (a.Length != b.Length) throw new ArgumentOutOfRangeException("b"); + if (a.Length != b.Length) throw new ArgumentException(Resources.ArgumentVectorsSameLength); + float max = Math.Abs(a[0] - b[0]); for (int i = 1; i < a.Length; i++) { @@ -262,6 +268,7 @@ namespace MathNet.Numerics public static double Minkowski(double p, double[] a, double[] b) { if (a.Length != b.Length) throw new ArgumentException(Resources.ArgumentVectorsSameLength); + if (p < 0d) throw new ArgumentOutOfRangeException("p"); if (p == 1d) return Manhattan(a, b); if (p == 2d) return Euclidean(a, b); @@ -281,6 +288,7 @@ namespace MathNet.Numerics public static float Minkowski(double p, float[] a, float[] b) { if (a.Length != b.Length) throw new ArgumentException(Resources.ArgumentVectorsSameLength); + if (p < 0d) throw new ArgumentOutOfRangeException("p"); if (p == 1d) return Manhattan(a, b); if (p == 2d) return Euclidean(a, b); @@ -329,6 +337,8 @@ namespace MathNet.Numerics /// public static double Cosine(double[] a, double[] b) { + if (a.Length != b.Length) throw new ArgumentException(Resources.ArgumentVectorsSameLength); + var ab = Control.LinearAlgebraProvider.DotProduct(a, b); var a2 = Control.LinearAlgebraProvider.DotProduct(a, a); var b2 = Control.LinearAlgebraProvider.DotProduct(b, b); @@ -340,6 +350,8 @@ namespace MathNet.Numerics /// public static float Cosine(float[] a, float[] b) { + if (a.Length != b.Length) throw new ArgumentException(Resources.ArgumentVectorsSameLength); + var ab = Control.LinearAlgebraProvider.DotProduct(a, b); var a2 = Control.LinearAlgebraProvider.DotProduct(a, a); var b2 = Control.LinearAlgebraProvider.DotProduct(b, b); @@ -351,7 +363,8 @@ namespace MathNet.Numerics /// public static double Hamming(double[] a, double[] b) { - if (a.Length != b.Length) throw new ArgumentOutOfRangeException("b"); + if (a.Length != b.Length) throw new ArgumentException(Resources.ArgumentVectorsSameLength); + int count = 0; for (int i = 0; i < a.Length; i++) { @@ -368,7 +381,8 @@ namespace MathNet.Numerics /// public static float Hamming(float[] a, float[] b) { - if (a.Length != b.Length) throw new ArgumentOutOfRangeException("b"); + if (a.Length != b.Length) throw new ArgumentException(Resources.ArgumentVectorsSameLength); + int count = 0; for (int i = 0; i < a.Length; i++) {