Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/19/17 18:47:00 (7 years ago)
Author:
gkronber
Message:

#2700: made some changes while reviewing the code

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  
    4949      return dist.Get(a.X, b.X);
    5050    }
    51 
    52     public override double Get(IDataPoint<T> a, IDataPoint<T> b, double threshold) {
    53       return dist.Get(a.X, b.X, threshold);
    54     }
    5551  }
    5652}
  • branches/TSNE/HeuristicLab.Algorithms.DataAnalysis/3.4/TSNE/Distances/DistanceBase.cs

    r14512 r14767  
    3737
    3838    public abstract double Get(T a, T b);
    39     public abstract double Get(T a, T b, double threshold);
    4039
    4140    public IComparer<T> GetDistanceComparer(T item) {
  • branches/TSNE/HeuristicLab.Algorithms.DataAnalysis/3.4/TSNE/Distances/EuclidianDistance.cs

    r14512 r14767  
    4545      return Math.Sqrt(point1.Zip(point2, (a1, b1) => (a1 - b1) * (a1 - b1)).Sum());
    4646    }
    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     }
    5947    #endregion
    6048
     
    6250      return GetDistance(a.ToArray(), b.ToArray());
    6351    }
    64     public override double Get(IEnumerable<double> a, IEnumerable<double> b, double threshold) {
    65       return GetDistance(a, b, threshold);
    66     }
    6752  }
    6853}
  • branches/TSNE/HeuristicLab.Algorithms.DataAnalysis/3.4/TSNE/Distances/InnerProductDistance.cs

    r14512 r14767  
    3030
    3131  /// <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.
    3334  /// </summary>
    3435  [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<IReadOnlyList<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>> {
    3738
    3839    #region HLConstructors
     
    4849
    4950    #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;
    5661    }
    5762    #endregion
    58     public override double Get(IReadOnlyList<double> a, IReadOnlyList<double> b) {
     63    public override double Get(IEnumerable<double> a, IEnumerable<double> b) {
    5964      return GetDistance(a, b);
    60     }
    61     public override double Get(IReadOnlyList<double> a, IReadOnlyList<double> b, double threshold) {
    62       return GetDistance(a, b, threshold);
    6365    }
    6466  }
Note: See TracChangeset for help on using the changeset viewer.