Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/20/17 15:29:53 (6 years ago)
Author:
bwerth
Message:

#2850 worked on weighted tSNE

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/Weighted TSNE/3.4/TSNE/Distances/EuclideanDistance.cs

    r15207 r15479  
    3131  [Item("EuclideanDistance", "A norm function that uses Euclidean distance")]
    3232  public class EuclideanDistance : DistanceBase<IEnumerable<double>> {
    33 
    3433    #region HLConstructors & Cloning
    3534    [StorableConstructor]
    3635    protected EuclideanDistance(bool deserializing) : base(deserializing) { }
    3736    protected EuclideanDistance(EuclideanDistance original, Cloner cloner) : base(original, cloner) { }
    38     public override IDeepCloneable Clone(Cloner cloner) { return new EuclideanDistance(this, cloner); }
     37    public override IDeepCloneable Clone(Cloner cloner) {
     38      return new EuclideanDistance(this, cloner);
     39    }
    3940    public EuclideanDistance() { }
    4041    #endregion
    4142
    42     public static double GetDistance(IReadOnlyList<double> point1, IReadOnlyList<double> point2) {
    43       if (point1.Count != point2.Count) throw new ArgumentException("Euclidean distance not defined on vectors of different length");
    44       var sum = 0.0;
    45       for (var i = 0; i < point1.Count; i++) {
    46         var d = point1[i] - point2[i];
    47         sum += d * d;
     43    public static double GetDistance(IEnumerable<double> point1, IEnumerable<double> point2) {
     44      using (IEnumerator<double> p1Enum = point1.GetEnumerator(), p2Enum = point2.GetEnumerator()) {
     45        var sum = 0.0;
     46        var p1Next = p1Enum.MoveNext();
     47        var p2Next = p2Enum.MoveNext();
     48        while (p1Next && p2Next) {
     49          var d = p1Enum.Current - p2Enum.Current;
     50          sum += d * d;
     51          p1Next = p1Enum.MoveNext();
     52          p2Next = p1Enum.MoveNext();
     53        }
     54        if (p2Next || p1Next) throw new ArgumentException("Euclidean distance not defined on vectors of different length");
     55        return Math.Sqrt(sum);
    4856      }
    49 
    50       return Math.Sqrt(sum);
    5157    }
    5258
    5359    public override double Get(IEnumerable<double> a, IEnumerable<double> b) {
    54       return GetDistance(a.ToArray(), b.ToArray());
     60      return GetDistance(a, b);
    5561    }
    5662  }
Note: See TracChangeset for help on using the changeset viewer.