diff --git a/src/Numerics/Distance.cs b/src/Numerics/Distance.cs index 065d1de3..61eeb5c6 100644 --- a/src/Numerics/Distance.cs +++ b/src/Numerics/Distance.cs @@ -185,5 +185,155 @@ namespace MathNet.Numerics { 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 float 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 float 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 float 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"); + var 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"); + var 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; + } } }