Free cookie consent management tool by TermsFeed Policy Generator

source: branches/TSNE/HeuristicLab.Algorithms.DataAnalysis/3.4/Distances/InnerProductDistance.cs @ 14512

Last change on this file since 14512 was 14512, checked in by bwerth, 7 years ago

#2700 worked in several comments from mkommend, extended analysis during algorithm run, added more Distances, made algorithm stoppable

File size: 2.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Problems.SurrogateProblem {
30
31  /// <summary>
32  /// this is not a proper distance
33  /// </summary>
34  [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>> {
37
38    #region HLConstructors
39    [StorableConstructor]
40    protected InnerProductDistance(bool deserializing) : base(deserializing) { }
41    protected InnerProductDistance(InnerProductDistance original, Cloner cloner)
42      : base(original, cloner) { }
43    public InnerProductDistance() { }
44    public override IDeepCloneable Clone(Cloner cloner) {
45      return new InnerProductDistance(this, cloner);
46    }
47    #endregion
48
49    #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)
56    }
57    #endregion
58    public override double Get(IReadOnlyList<double> a, IReadOnlyList<double> b) {
59      return GetDistance(a, b);
60    }
61    public override double Get(IReadOnlyList<double> a, IReadOnlyList<double> b, double threshold) {
62      return GetDistance(a, b, threshold);
63    }
64  }
65}
Note: See TracBrowser for help on using the repository browser.