This repository has been archived by the owner on Nov 19, 2020. It is now read-only.
/
Distance.cs
216 lines (192 loc) · 7.72 KB
/
Distance.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Accord Math Library
// The Accord.NET Framework
// http://accord-framework.net
//
// Copyright © César Souza, 2009-2017
// cesarsouza at gmail.com
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
//
namespace Accord.Math
{
using Accord.Math.Decompositions;
using Accord.Math.Distances;
using System;
using System.Collections;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Linq;
/// <summary>
/// Static class Distance. Defines a set of extension methods defining distance measures.
/// </summary>
///
public static partial class Distance
{
/// <summary>
/// Checks whether a function is a real metric distance, i.e. respects
/// the triangle inequality. Please note that a function can still pass
/// this test and not respect the triangle inequality.
/// </summary>
///
public static bool IsMetric(Func<double[], double[], double> value)
{
// Direct test
double z = value(new[] { 1.0 }, new[] { 1.0 });
if (z > 2 || z < 0)
return false;
var a = new double[1];
var b = new double[1];
for (int i = -10; i < 10; i++)
{
a[0] = i;
for (int j = -10; j < +10; j++)
{
b[0] = j;
double c = value(a, b);
if (c > Math.Abs(i) + Math.Abs(j))
return false;
}
}
return true;
}
/// <summary>
/// Checks whether a function is a real metric distance, i.e. respects
/// the triangle inequality. Please note that a function can still pass
/// this test and not respect the triangle inequality.
/// </summary>
///
public static bool IsMetric(Func<int[], int[], double> value)
{
// Direct test
double z = value(new[] { 1 }, new[] { 1 });
if (z > 2 || z < 0)
return false;
int size = 3;
int[] zero = new int[size];
foreach (var a in Combinatorics.Sequences(3, size, inPlace: true))
{
foreach (var b in Combinatorics.Sequences(3, size, inPlace: true))
{
double dza = value(zero, a);
double dzb = value(zero, b);
double dab = value(a, b);
if (dab > dza + dzb)
return false;
double daz = value(a, zero);
double dbz = value(b, zero);
double dba = value(b, a);
if (daz != dza || dbz != dzb || dab != dba)
return false;
}
}
return true;
}
/// <summary>
/// Checks whether a function is a real metric distance, i.e. respects
/// the triangle inequality. Please note that a function can still pass
/// this test and not respect the triangle inequality.
/// </summary>
///
public static bool IsMetric<T>(IDistance<T> value)
{
return value is IMetric<T>;
}
/// <summary>
/// Gets the a <see cref="IDistance{T}"/> object implementing a
/// particular method of the <see cref="Distance"/> static class.
/// </summary>
///
/// <remarks>
/// <para>
/// This method is intended to be used in scenarios where you have been using any
/// of the static methods in the <see cref="Distance"/> class, but now you would like
/// to obtain a reference to an object that implements the same distance you have been
/// using before, but in a object-oriented, polymorphic manner. Please see the example
/// below for more details.</para>
/// <para>
/// Note: This method relies on reflection and might not work
/// on all scenarios, environments, and/or platforms.</para>
/// </remarks>
///
/// <typeparam name="T">The type of the elements being compared in the distance function.</typeparam>
///
/// <param name="func">The method of <see cref="Distance"/>.</param>
///
/// <returns>
/// An object of the class that implements the given distance.
/// </returns>
///
/// <example>
/// <code source="Unit Tests\Accord.Tests.Math\DistanceTest.cs" region="doc_getdistance" />
/// </example>
///
public static IDistance<T> GetDistance<T>(Func<T, T, double> func)
{
#if NETSTANDARD1_4
var methods = typeof(Distance).GetTypeInfo().DeclaredMethods.Where(m=>m.IsPublic && m.IsStatic);
#else
var methods = typeof(Distance).GetMethods(BindingFlags.Public | BindingFlags.Static);
#endif
foreach (var method in methods)
{
#if NETSTANDARD1_4
var methodInfo = func.GetMethodInfo();
#else
var methodInfo = func.Method;
#endif
if (methodInfo == method)
{
var t = Type.GetType("Accord.Math.Distances." + method.Name);
if (t == null)
{
// TODO: Remove the following special case, as it is needed only
// for preserving compatibility for a few next releases more.
if (methodInfo.Name == "BitwiseHamming")
return new Hamming() as IDistance<T>;
}
return (IDistance<T>)Activator.CreateInstance(t, new object[] { });
}
}
return null;
}
/// <summary>
/// Gets the Bitwise Hamming distance between two points.
/// Please use the <see cref="Distance.Hamming(byte[], byte[])">Distance.Hamming</see>
/// method or the <see cref="Accord.Math.Distances.Hamming"/> class instead.
/// </summary>
///
[Obsolete("Please use Distance.Hamming instead.")]
public static double BitwiseHamming(byte[] x, byte[] y)
{
return Distance.Hamming(x, y);
}
/// <summary>
/// Gets the Levenshtein distance between two points.
/// </summary>
///
/// <param name="x"></param>
/// <param name="y"></param>
///
/// <returns>The Levenshtein distance between x and y.</returns>
///
#if NET45 || NET46 || NET462 || NETSTANDARD2_0
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
public static double Levenshtein<T>(T[] x, T[] y)
{
return new Levenshtein<T>().Distance(x, y);
}
}
}