diff --git a/src/Numerics/Distance.cs b/src/Numerics/Distance.cs index da1b5082..425c6152 100644 --- a/src/Numerics/Distance.cs +++ b/src/Numerics/Distance.cs @@ -322,6 +322,28 @@ namespace MathNet.Numerics return sum; } + /// + /// Cosine Distance, representing the angular distance while ignoring the scale. + /// + public static double Cosine(double[] a, double[] b) + { + var ab = Control.LinearAlgebraProvider.DotProduct(a, b); + var a2 = Control.LinearAlgebraProvider.DotProduct(a, a); + var b2 = Control.LinearAlgebraProvider.DotProduct(b, b); + return 1d - ab/(Math.Sqrt(a2*b2)); + } + + /// + /// Cosine Distance, representing the angular distance while ignoring the scale. + /// + public static float Cosine(float[] a, float[] b) + { + var ab = Control.LinearAlgebraProvider.DotProduct(a, b); + var a2 = Control.LinearAlgebraProvider.DotProduct(a, a); + var b2 = Control.LinearAlgebraProvider.DotProduct(b, b); + return (float)(1d - ab/(Math.Sqrt(a2*b2))); + } + /// /// Hamming Distance, i.e. the number of positions that have different values in the vectors. ///