public double Compare(IVector v1, IVector v2) { //v1 = NormalizeOrDefault(v1); //v2 = NormalizeOrDefault(v2); Dictionary <IDimensionKey, IDimensionValue> v1Values = v1.DimensionValues.ToDictionary(dv => dv.DimensionKey); double dotProduct = 0; double squaredV1Sum = 0; double squaredV2Sum = 0; Dictionary <IDimensionKey, IDimensionValue> v1DimensionKeys = v1.DimensionValues.ToDictionary(dv => dv.DimensionKey); foreach (IDimensionValue v2DimensionValue in v2.DimensionValues) { IDimensionValue vDim1Value = v1Values.TryGetValue(v2DimensionValue.DimensionKey, out IDimensionValue v1v) ? v1v : null; v1DimensionKeys.Remove(v2DimensionValue.DimensionKey); v1Values.Remove(v2DimensionValue.DimensionKey); double v1Value = vDim1Value?.Value ?? 0.0; double v2Value = v2DimensionValue.Value; AddDimension(v1Value, v2Value, ref dotProduct, ref squaredV1Sum, ref squaredV2Sum); } foreach (double v1Value in v1DimensionKeys.Values.Select(dv => dv.Value)) { const double v2Value = 0.0; AddDimension(v1Value, v2Value, ref dotProduct, ref squaredV1Sum, ref squaredV2Sum); } double v1Length = Math.Sqrt(squaredV1Sum); double v2Length = Math.Sqrt(squaredV2Sum); double lengthProduct; lengthProduct = v1Length * v2Length; if (double.IsInfinity(lengthProduct)) { throw new OverflowException(); } if (lengthProduct == 0) { return(0); } double frac = dotProduct / lengthProduct; const double fracTollerance = 0.000000000000001; if (Math.Abs(frac - 1) < fracTollerance) { return(0); } double arcCos = Math.Acos(frac); return(arcCos); }
private IEnumerable <IDimensionValue> LastDimensions(DateTime?statsLastSuccess, DateTime?statsLastFailure) { TimeSpan fiveMinutes = TimeSpan.FromMinutes(5); TimeSpan oneMinute = TimeSpan.FromMinutes(1); var last5Dim = LastDimensions(statsLastSuccess, statsLastFailure, fiveMinutes, DimensionNames.FiveMinuteDeltaSinceLastSuccessOrFailure); var last1Dim = LastDimensions(statsLastSuccess, statsLastFailure, oneMinute, DimensionNames.OneMinuteDeltaSinceLastSuccessOrFailure); var lastDims = new IDimensionValue[] { last5Dim, last1Dim }; return(lastDims); }