|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using System.Threading.Tasks; |
| 6 | + |
| 7 | +namespace KdTree.Math |
| 8 | +{ |
| 9 | + [Serializable] |
| 10 | + public class DoubleMath : TypeMath<double> |
| 11 | + { |
| 12 | + public override int Compare(double a, double b) |
| 13 | + { |
| 14 | + return a.CompareTo(b); |
| 15 | + } |
| 16 | + |
| 17 | + public override bool AreEqual(double a, double b) |
| 18 | + { |
| 19 | + return a == b; |
| 20 | + } |
| 21 | + |
| 22 | + public override double MinValue |
| 23 | + { |
| 24 | + get { return double.MinValue; } |
| 25 | + } |
| 26 | + |
| 27 | + public override double MaxValue |
| 28 | + { |
| 29 | + get { return double.MaxValue; } |
| 30 | + } |
| 31 | + |
| 32 | + public override double Zero |
| 33 | + { |
| 34 | + get { return 0; } |
| 35 | + } |
| 36 | + |
| 37 | + public override double NegativeInfinity { get { return double.NegativeInfinity; } } |
| 38 | + |
| 39 | + public override double PositiveInfinity { get { return double.PositiveInfinity; } } |
| 40 | + |
| 41 | + public override double Add(double a, double b) |
| 42 | + { |
| 43 | + return a + b; |
| 44 | + } |
| 45 | + |
| 46 | + public override double Subtract(double a, double b) |
| 47 | + { |
| 48 | + return a - b; |
| 49 | + } |
| 50 | + |
| 51 | + public override double Multiply(double a, double b) |
| 52 | + { |
| 53 | + return a * b; |
| 54 | + } |
| 55 | + |
| 56 | + public override double DistanceSquaredBetweenPoints(double[] a, double[] b) |
| 57 | + { |
| 58 | + double distance = Zero; |
| 59 | + int dimensions = a.Length; |
| 60 | + |
| 61 | + // Return the absolute distance bewteen 2 hyper points |
| 62 | + for (var dimension = 0; dimension < dimensions; dimension++) |
| 63 | + { |
| 64 | + double distOnThisAxis = Subtract(a[dimension], b[dimension]); |
| 65 | + double distOnThisAxisSquared = Multiply(distOnThisAxis, distOnThisAxis); |
| 66 | + |
| 67 | + distance = Add(distance, distOnThisAxisSquared); |
| 68 | + } |
| 69 | + |
| 70 | + return distance; |
| 71 | + } |
| 72 | + } |
| 73 | +} |
0 commit comments