Beispiel #1
0
        public static double CalculateDistance(VectorD vectorA, VectorD vectorB, DistanceMode mode)
        {
            if (vectorA.Count != vectorB.Count)
            {
                throw new Exception("Both vectors should have the same number of elemetns in order to calculate a distance between them!");
            }

            switch (mode)
            {
            case DistanceMode.Euclidean:
                var sum = vectorA.Select((t, i) => Math.Pow(t - vectorB[i], 2.0)).Sum();
                return(Math.Sqrt(sum));

            case DistanceMode.Manhattan:
                var sum2 = vectorA.Select((t, i) => Math.Abs(t - vectorB[i])).Sum();
                return(sum2);


            case DistanceMode.Max:
                var max = vectorA.Select((t, i) => Math.Abs(t - vectorB[i])).Max();
                return(max);


            case DistanceMode.Min:
                var min = vectorA.Select((t, i) => Math.Abs(t - vectorB[i])).Min();
                return(min);

            case DistanceMode.Levenshtein:
                var l = LevenshteinDistance(vectorA, vectorB);
                return(l);

            default:
                throw new Exception("Unknown distance mode!");
            }
        }