- Timestamp:
- 11/20/17 15:29:53 (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/Weighted TSNE/3.4/TSNE/Distances/EuclideanDistance.cs
r15207 r15479 31 31 [Item("EuclideanDistance", "A norm function that uses Euclidean distance")] 32 32 public class EuclideanDistance : DistanceBase<IEnumerable<double>> { 33 34 33 #region HLConstructors & Cloning 35 34 [StorableConstructor] 36 35 protected EuclideanDistance(bool deserializing) : base(deserializing) { } 37 36 protected EuclideanDistance(EuclideanDistance original, Cloner cloner) : base(original, cloner) { } 38 public override IDeepCloneable Clone(Cloner cloner) { return new EuclideanDistance(this, cloner); } 37 public override IDeepCloneable Clone(Cloner cloner) { 38 return new EuclideanDistance(this, cloner); 39 } 39 40 public EuclideanDistance() { } 40 41 #endregion 41 42 42 public static double GetDistance(IReadOnlyList<double> point1, IReadOnlyList<double> point2) { 43 if (point1.Count != point2.Count) throw new ArgumentException("Euclidean distance not defined on vectors of different length"); 44 var sum = 0.0; 45 for (var i = 0; i < point1.Count; i++) { 46 var d = point1[i] - point2[i]; 47 sum += d * d; 43 public static double GetDistance(IEnumerable<double> point1, IEnumerable<double> point2) { 44 using (IEnumerator<double> p1Enum = point1.GetEnumerator(), p2Enum = point2.GetEnumerator()) { 45 var sum = 0.0; 46 var p1Next = p1Enum.MoveNext(); 47 var p2Next = p2Enum.MoveNext(); 48 while (p1Next && p2Next) { 49 var d = p1Enum.Current - p2Enum.Current; 50 sum += d * d; 51 p1Next = p1Enum.MoveNext(); 52 p2Next = p1Enum.MoveNext(); 53 } 54 if (p2Next || p1Next) throw new ArgumentException("Euclidean distance not defined on vectors of different length"); 55 return Math.Sqrt(sum); 48 56 } 49 50 return Math.Sqrt(sum);51 57 } 52 58 53 59 public override double Get(IEnumerable<double> a, IEnumerable<double> b) { 54 return GetDistance(a .ToArray(), b.ToArray());60 return GetDistance(a, b); 55 61 } 56 62 }
Note: See TracChangeset
for help on using the changeset viewer.