// // Math.NET Numerics, part of the Math.NET Project // http://numerics.mathdotnet.com // http://github.com/mathnet/mathnet-numerics // http://mathnetnumerics.codeplex.com // // Copyright (c) 2009-2013 Math.NET // // Permission is hereby granted, free of charge, to any person // obtaining a copy of this software and associated documentation // files (the "Software"), to deal in the Software without // restriction, including without limitation the rights to use, // copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following // conditions: // // The above copyright notice and this permission notice shall be // included in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR // OTHER DEALINGS IN THE SOFTWARE. // using System; using MathNet.Numerics.LinearAlgebra; using MathNet.Numerics.Properties; namespace MathNet.Numerics { public static class Distance { /// /// Sum of Absolute Difference (SAD), i.e. the L1-norm (Manhattan) of the difference. /// public static double SAD(Vector a, Vector b) { return (a - b).L1Norm(); } /// /// Sum of Absolute Difference (SAD), i.e. the L1-norm (Manhattan) of the difference. /// public static double SAD(Vector a, Vector b) { return (a - b).L1Norm(); } /// /// Sum of Absolute Difference (SAD), i.e. the L1-norm (Manhattan) of the difference. /// public static double SAD(double[] a, double[] b) { if (a.Length != b.Length) throw new ArgumentException(Resources.ArgumentVectorsSameLength); double sum = 0d; for (var i = 0; i < a.Length; i++) { sum += Math.Abs(a[i] - b[i]); } return sum; } /// /// Sum of Absolute Difference (SAD), i.e. the L1-norm (Manhattan) of the difference. /// public static float SAD(float[] a, float[] b) { if (a.Length != b.Length) throw new ArgumentException(Resources.ArgumentVectorsSameLength); float sum = 0f; for (var i = 0; i < a.Length; i++) { sum += Math.Abs(a[i] - b[i]); } return sum; } /// /// Mean-Absolute Error (MAE), i.e. the normalized L1-norm (Manhattan) of the difference. /// public static double MAE(Vector a, Vector b) { return (a - b).L1Norm()/a.Count; } /// /// Mean-Absolute Error (MAE), i.e. the normalized L1-norm (Manhattan) of the difference. /// public static double MAE(Vector a, Vector b) { return (a - b).L1Norm()/a.Count; } /// /// Mean-Absolute Error (MAE), i.e. the normalized L1-norm (Manhattan) of the difference. /// public static double MAE(double[] a, double[] b) { return SAD(a, b)/a.Length; } /// /// Mean-Absolute Error (MAE), i.e. the normalized L1-norm (Manhattan) of the difference. /// public static float MAE(float[] a, float[] b) { return SAD(a, b)/a.Length; } /// /// Sum of Squared Difference (SSD), i.e. the squared L2-norm (Euclidean) of the difference. /// public static double SSD(Vector a, Vector b) { var norm = (a - b).L2Norm(); return norm*norm; } /// /// Sum of Squared Difference (SSD), i.e. the squared L2-norm (Euclidean) of the difference. /// public static double SSD(Vector a, Vector b) { var norm = (a - b).L2Norm(); return norm*norm; } /// /// Sum of Squared Difference (SSD), i.e. the squared L2-norm (Euclidean) of the difference. /// public static double SSD(double[] a, double[] b) { var diff = new double[a.Length]; Control.LinearAlgebraProvider.SubtractArrays(a, b, diff); return Control.LinearAlgebraProvider.DotProduct(diff, diff); } /// /// Sum of Squared Difference (SSD), i.e. the squared L2-norm (Euclidean) of the difference. /// public static float SSD(float[] a, float[] b) { var diff = new float[a.Length]; Control.LinearAlgebraProvider.SubtractArrays(a, b, diff); return Control.LinearAlgebraProvider.DotProduct(diff, diff); } /// /// Mean-Squared Error (MSE), i.e. the normalized squared L2-norm (Euclidean) of the difference. /// public static double MSE(Vector a, Vector b) { var norm = (a - b).L2Norm(); return norm*norm/a.Count; } /// /// Mean-Squared Error (MSE), i.e. the normalized squared L2-norm (Euclidean) of the difference. /// public static double MSE(Vector a, Vector b) { var norm = (a - b).L2Norm(); return norm*norm/a.Count; } /// /// Mean-Squared Error (MSE), i.e. the normalized squared L2-norm (Euclidean) of the difference. /// public static double MSE(double[] a, double[] b) { return SSD(a, b)/a.Length; } /// /// Mean-Squared Error (MSE), i.e. the normalized squared L2-norm (Euclidean) of the difference. /// public static float MSE(float[] a, float[] b) { return SSD(a, b)/a.Length; } /// /// Euclidean Distance, i.e. the L2-norm of the difference. /// public static double Euclidean(Vector a, Vector b) { return (a - b).L2Norm(); } /// /// Euclidean Distance, i.e. the L2-norm of the difference. /// public static double Euclidean(Vector a, Vector b) { return (a - b).L2Norm(); } /// /// Euclidean Distance, i.e. the L2-norm of the difference. /// public static double Euclidean(double[] a, double[] b) { return Math.Sqrt(SSD(a, b)); } /// /// Euclidean Distance, i.e. the L2-norm of the difference. /// public static float Euclidean(float[] a, float[] b) { return (float) Math.Sqrt(SSD(a, b)); } /// /// Manhattan Distance, i.e. the L1-norm of the difference. /// public static double Manhattan(Vector a, Vector b) { return (a - b).L1Norm(); } /// /// Manhattan Distance, i.e. the L1-norm of the difference. /// public static double Manhattan(Vector a, Vector b) { return (a - b).L1Norm(); } /// /// Manhattan Distance, i.e. the L1-norm of the difference. /// public static double Manhattan(double[] a, double[] b) { return SAD(a, b); } /// /// Manhattan Distance, i.e. the L1-norm of the difference. /// public static float Manhattan(float[] a, float[] b) { return SAD(a, b); } /// /// Chebyshev Distance, i.e. the Infinity-norm of the difference. /// public static double Chebyshev(Vector a, Vector b) { return (a - b).InfinityNorm(); } /// /// Chebyshev Distance, i.e. the Infinity-norm of the difference. /// public static double Chebyshev(Vector a, Vector b) { return (a - b).InfinityNorm(); } /// /// Chebyshev Distance, i.e. the Infinity-norm of the difference. /// public static double Chebyshev(double[] a, double[] b) { if (a.Length != b.Length) throw new ArgumentOutOfRangeException("b"); double max = Math.Abs(a[0] - b[0]); for (int i = 1; i < a.Length; i++) { var next = Math.Abs(a[i] - b[i]); if (next > max) { max = next; } } return max; } /// /// Chebyshev Distance, i.e. the Infinity-norm of the difference. /// public static float Chebyshev(float[] a, float[] b) { if (a.Length != b.Length) throw new ArgumentOutOfRangeException("b"); float max = Math.Abs(a[0] - b[0]); for (int i = 1; i < a.Length; i++) { var next = Math.Abs(a[i] - b[i]); if (next > max) { max = next; } } return max; } /// /// Hamming Distance, i.e. the number of positions that have different values in the vectors. /// public static double Hamming(double[] a, double[] b) { if (a.Length != b.Length) throw new ArgumentOutOfRangeException("b"); int count = 0; for (int i = 1; i < a.Length; i++) { if (a[i] != b[i]) { count++; } } return count; } /// /// Hamming Distance, i.e. the number of positions that have different values in the vectors. /// public static float Hamming(float[] a, float[] b) { if (a.Length != b.Length) throw new ArgumentOutOfRangeException("b"); int count = 0; for (int i = 1; i < a.Length; i++) { if (a[i] != b[i]) { count++; } } return count; } } }