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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.