Changeset 14767 for branches/TSNE/HeuristicLab.Algorithms.DataAnalysis/3.4/TSNE/Distances/InnerProductDistance.cs
- Timestamp:
- 03/19/17 18:47:00 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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.