Changeset 15487 for branches/Weighted TSNE/3.4/TSNE/Distances
- Timestamp:
- 11/27/17 10:22:08 (7 years ago)
- Location:
- branches/Weighted TSNE/3.4/TSNE/Distances
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/Weighted TSNE/3.4/TSNE/Distances/CosineDistance.cs
r15479 r15487 50 50 var length1 = 0.0; 51 51 var length2 = 0.0; 52 var p1Next = p1Enum.MoveNext(); 53 var p2Next = p2Enum.MoveNext(); 54 while (p1Next && p2Next) { 52 while (p1Enum.MoveNext() & p2Enum.MoveNext()) { 55 53 double d1 = p1Enum.Current, d2 = p2Enum.Current; 56 54 innerprod += d1 * d2; 57 55 length1 += d1 * d1; 58 56 length2 += d2 * d2; 59 p1Next = p1Enum.MoveNext();60 p2Next = p1Enum.MoveNext();61 57 } 62 58 var divisor = Math.Sqrt(length1 * length2); 63 59 if (divisor.IsAlmost(0)) throw new ArgumentException("Cosine distance is not defined on vectors of length 0"); 64 if (p 2Next || p1Next) throw new ArgumentException("Cosine distance not defined on vectors of different length");60 if (p1Enum.MoveNext() || p2Enum.MoveNext()) throw new ArgumentException("Cosine distance not defined on vectors of different length"); 65 61 return 1 - innerprod / divisor; 66 62 } -
branches/Weighted TSNE/3.4/TSNE/Distances/DistanceBase.cs
r15479 r15487 28 28 namespace HeuristicLab.Algorithms.DataAnalysis { 29 29 [StorableClass] 30 public abstract class DistanceBase<T> : ParameterizedNamedItem, IDistance<T> { 31 30 public abstract class DistanceBase<T> : Item, IDistance<T> { 32 31 #region HLConstructors & Cloning 33 32 [StorableConstructor] … … 44 43 45 44 public double Get(object x, object y) { 46 return Get((T) x, (T)y);45 return Get((T) x, (T) y); 47 46 } 48 47 49 48 public IComparer GetDistanceComparer(object item) { 50 return new DistanceComparer((T) item, this);49 return new DistanceComparer((T) item, this); 51 50 } 52 51 … … 65 64 66 65 public int Compare(object x, object y) { 67 return Compare((T) x, (T)y);66 return Compare((T) x, (T) y); 68 67 } 69 68 } -
branches/Weighted TSNE/3.4/TSNE/Distances/EuclideanDistance.cs
r15479 r15487 44 44 using (IEnumerator<double> p1Enum = point1.GetEnumerator(), p2Enum = point2.GetEnumerator()) { 45 45 var sum = 0.0; 46 var p1Next = p1Enum.MoveNext(); 47 var p2Next = p2Enum.MoveNext(); 48 while (p1Next && p2Next) { 46 while (p1Enum.MoveNext() & p2Enum.MoveNext()) { 49 47 var d = p1Enum.Current - p2Enum.Current; 50 48 sum += d * d; 51 p1Next = p1Enum.MoveNext();52 p2Next = p1Enum.MoveNext();53 49 } 54 if (p 2Next || p1Next) throw new ArgumentException("Euclidean distance not defined on vectors of different length");50 if (p1Enum.MoveNext() || p2Enum.MoveNext()) throw new ArgumentException("Euclidean distance not defined on vectors of different length"); 55 51 return Math.Sqrt(sum); 56 52 } -
branches/Weighted TSNE/3.4/TSNE/Distances/ManhattanDistance.cs
r15479 r15487 47 47 using (IEnumerator<double> p1Enum = point1.GetEnumerator(), p2Enum = point2.GetEnumerator()) { 48 48 var sum = 0.0; 49 var p1Next = p1Enum.MoveNext(); 50 var p2Next = p2Enum.MoveNext(); 51 while (p1Next && p2Next) { 49 while (p1Enum.MoveNext() & p2Enum.MoveNext()) 52 50 sum += Math.Abs(p1Enum.Current - p2Enum.Current); 53 p1Next = p1Enum.MoveNext(); 54 p2Next = p1Enum.MoveNext(); 55 } 56 if (p2Next || p1Next) throw new ArgumentException("Manhattan distance not defined on vectors of different length"); 51 if (p1Enum.MoveNext() || p2Enum.MoveNext()) throw new ArgumentException("Manhattan distance not defined on vectors of different length"); 57 52 return sum; 58 53 } -
branches/Weighted TSNE/3.4/TSNE/Distances/WeightedEuclideanDistance.cs
r15479 r15487 65 65 using (IEnumerator<double> p1Enum = point1.GetEnumerator(), p2Enum = point2.GetEnumerator(), weEnum = weights.GetEnumerator()) { 66 66 var sum = 0.0; 67 var p1Next = p1Enum.MoveNext(); 68 var p2Next = p2Enum.MoveNext(); 69 var weNext = weEnum.MoveNext(); 70 while (p1Next && p2Next && weNext) { 67 while (p1Enum.MoveNext() & p2Enum.MoveNext() & weEnum.MoveNext()) { 71 68 var d = p1Enum.Current - p2Enum.Current; 72 69 var w = weEnum.Current; 73 70 sum += d * d * w * w; 74 p1Next = p1Enum.MoveNext();75 p2Next = p2Enum.MoveNext();76 weNext = weEnum.MoveNext();77 71 } 78 if (weNext) throw new ArgumentException("Weighted Euclidean distance requires a non-null weight vector of length equal to the number of allowed input double variables the compared points"); 79 if (p1Next || p2Next) throw new ArgumentException("Weighted Euclidean distance not defined on vectors of different length"); 72 if (weEnum.MoveNext() || p1Enum.MoveNext() || p2Enum.MoveNext()) throw new ArgumentException("Weighted Euclidean distance not defined on vectors of different length"); 80 73 return Math.Sqrt(sum); 81 74 }
Note: See TracChangeset
for help on using the changeset viewer.