Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/20/18 13:52:40 (6 years ago)
Author:
pfleck
Message:

#2845 reverted the last merge (r16307) because some revisions were missing

Location:
branches/2845_EnhancedProgress
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/2845_EnhancedProgress

  • branches/2845_EnhancedProgress/HeuristicLab.Algorithms.DataAnalysis

  • branches/2845_EnhancedProgress/HeuristicLab.Algorithms.DataAnalysis/3.4

    • Property svn:mergeinfo deleted
  • branches/2845_EnhancedProgress/HeuristicLab.Algorithms.DataAnalysis/3.4/TSNE/Distances/CosineDistance.cs

    r16307 r16308  
    11#region License Information
    22/* HeuristicLab
    3  * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
    44 *
    55 * This file is part of HeuristicLab.
     
    2222using System;
    2323using System.Collections.Generic;
     24using System.Linq;
    2425using HeuristicLab.Common;
    2526using HeuristicLab.Core;
     
    2728
    2829namespace HeuristicLab.Algorithms.DataAnalysis {
     30
    2931  /// <summary>
    3032  /// The angular distance as defined as a normalized distance measure dependent on the angle between two vectors.
     
    3335  [Item("CosineDistance", "The angular distance as defined as a normalized distance measure dependent on the angle between two vectors.")]
    3436  public class CosineDistance : DistanceBase<IEnumerable<double>> {
     37
    3538    #region HLConstructors & Cloning
    3639    [StorableConstructor]
     
    4548
    4649    #region statics
    47     public static double GetDistance(IEnumerable<double> point1, IEnumerable<double> point2) {
    48       using (IEnumerator<double> p1Enum = point1.GetEnumerator(), p2Enum = point2.GetEnumerator()) {
    49         var innerprod = 0.0;
    50         var length1 = 0.0;
    51         var length2 = 0.0;
    52         while (p1Enum.MoveNext() & p2Enum.MoveNext()) {
    53           double d1 = p1Enum.Current, d2 = p2Enum.Current;
    54           innerprod += d1 * d2;
    55           length1 += d1 * d1;
    56           length2 += d2 * d2;
    57         }
    58         var divisor = Math.Sqrt(length1 * length2);
    59         if (divisor.IsAlmost(0)) throw new ArgumentException("Cosine distance is not defined on vectors of length 0");
    60         if (p1Enum.MoveNext() || p2Enum.MoveNext()) throw new ArgumentException("Cosine distance not defined on vectors of different length");
    61         return 1 - innerprod / divisor;
     50    public static double GetDistance(IReadOnlyList<double> point1, IReadOnlyList<double> point2) {
     51      if (point1.Count != point2.Count) throw new ArgumentException("Cosine distance not defined on vectors of different length");
     52      var innerprod = 0.0;
     53      var length1 = 0.0;
     54      var length2 = 0.0;
     55
     56      for (var i = 0; i < point1.Count; i++) {
     57        double d1 = point1[i], d2 = point2[i];
     58        innerprod += d1 * d2;
     59        length1 += d1 * d1;
     60        length2 += d2 * d2;
    6261      }
     62      var l = Math.Sqrt(length1 * length2);
     63      if (l.IsAlmost(0)) throw new ArgumentException("Cosine distance is not defined on vectors of length 0");
     64      return 1 - innerprod / l;
    6365    }
    6466    #endregion
    6567    public override double Get(IEnumerable<double> a, IEnumerable<double> b) {
    66       return GetDistance(a, b);
     68      return GetDistance(a.ToArray(), b.ToArray());
    6769    }
    6870  }
Note: See TracChangeset for help on using the changeset viewer.