- Timestamp:
- 03/19/17 18:47:00 (7 years ago)
- Location:
- branches/TSNE/HeuristicLab.Algorithms.DataAnalysis/3.4/TSNE/Distances
- Files:
-
- 1 deleted
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/TSNE/HeuristicLab.Algorithms.DataAnalysis/3.4/TSNE/Distances/DataPointDistance.cs
r14512 r14767 49 49 return dist.Get(a.X, b.X); 50 50 } 51 52 public override double Get(IDataPoint<T> a, IDataPoint<T> b, double threshold) {53 return dist.Get(a.X, b.X, threshold);54 }55 51 } 56 52 } -
branches/TSNE/HeuristicLab.Algorithms.DataAnalysis/3.4/TSNE/Distances/DistanceBase.cs
r14512 r14767 37 37 38 38 public abstract double Get(T a, T b); 39 public abstract double Get(T a, T b, double threshold);40 39 41 40 public IComparer<T> GetDistanceComparer(T item) { -
branches/TSNE/HeuristicLab.Algorithms.DataAnalysis/3.4/TSNE/Distances/EuclidianDistance.cs
r14512 r14767 45 45 return Math.Sqrt(point1.Zip(point2, (a1, b1) => (a1 - b1) * (a1 - b1)).Sum()); 46 46 } 47 public static double GetDistance(IEnumerable<double> a, IEnumerable<double> b, double threshold) {48 double sum = 0;49 var it1 = a.GetEnumerator();50 var it2 = b.GetEnumerator();51 while (sum > threshold * threshold && it1.MoveNext() && it2.MoveNext()) {52 var d = it1.Current - it2.Current;53 sum += d * d;54 }55 it1.Dispose();56 it2.Dispose();57 return sum;58 }59 47 #endregion 60 48 … … 62 50 return GetDistance(a.ToArray(), b.ToArray()); 63 51 } 64 public override double Get(IEnumerable<double> a, IEnumerable<double> b, double threshold) {65 return GetDistance(a, b, threshold);66 }67 52 } 68 53 } -
branches/TSNE/HeuristicLab.Algorithms.DataAnalysis/3.4/TSNE/Distances/InnerProductDistance.cs
r14512 r14767 30 30 31 31 /// <summary> 32 /// this is not a proper distance 32 /// The angluar distance as defined as a normalized distance measure dependent on the angle between two vectors. 33 /// It is designed for vectors with all positive coordinates. 33 34 /// </summary> 34 35 [StorableClass] 35 [Item("InnerProductDistance", "The Angluar Distance as defined as a normalized distance measure dependent on the angle between two vectors.\nIt is designed for vectors with all positive coordinates")]36 public class InnerProductDistance : DistanceBase<I ReadOnlyList<double>> {36 [Item("InnerProductDistance", "The angluar distance as defined as a normalized distance measure dependent on the angle between two vectors.\nIt is designed for vectors with all positive coordinates")] 37 public class InnerProductDistance : DistanceBase<IEnumerable<double>> { 37 38 38 39 #region HLConstructors … … 48 49 49 50 #region statics 50 public static double GetDistance(IReadOnlyList<double> point1, IReadOnlyList<double> point2) { 51 if (point1.Count != point2.Count) throw new ArgumentException("Inner Product distance not defined on vectors of different length"); 52 return point1.Zip(point2, (x, y) => x * y).Sum(); 53 } 54 public static double GetDistance(IEnumerable<double> a, IEnumerable<double> b, double threshold) { 55 return GetDistance(a.ToArray(), b.ToArray()); //no shortcut evaluation for Inner Product (summands may be negative => no way of telling if threshold is reached or not) 51 public static double GetDistance(IEnumerable<double> point1, IEnumerable<double> point2) { 52 var xs = point1.GetEnumerator(); 53 var ys = point2.GetEnumerator(); 54 var sum = 0.0; 55 while(xs.MoveNext() & ys.MoveNext()) { 56 if(xs.Current < 0 || ys.Current < 0) throw new ArgumentException("Inner product distance is only defined for vectors with non-negative elements"); 57 sum += xs.Current * ys.Current; 58 } 59 if(xs.MoveNext() || ys.MoveNext()) throw new ArgumentException("Enumerables contain a different number of elements"); 60 return sum; 56 61 } 57 62 #endregion 58 public override double Get(I ReadOnlyList<double> a, IReadOnlyList<double> b) {63 public override double Get(IEnumerable<double> a, IEnumerable<double> b) { 59 64 return GetDistance(a, b); 60 }61 public override double Get(IReadOnlyList<double> a, IReadOnlyList<double> b, double threshold) {62 return GetDistance(a, b, threshold);63 65 } 64 66 }
Note: See TracChangeset
for help on using the changeset viewer.