Theoretica
Mathematical Library
Loading...
Searching...
No Matches
distance.h
Go to the documentation of this file.
1
7
8#ifndef THEORETICA_DISTANCE_H
9#define THEORETICA_DISTANCE_H
10
11#include "./vec.h"
12#include "./algebra.h"
13#include "../core/constants.h"
14#include "../core/core_traits.h"
15#include "../core/real_analysis.h"
16#include "../complex/complex_analysis.h"
17
18
19namespace theoretica {
20
21 namespace algebra {
22
23
25
26
33 template<typename Vector>
34 inline real lp_norm(const Vector& v, unsigned int p) {
35
36 real sum = 0;
37
38 for (unsigned int i = 0; i < v.size(); ++i)
39 sum += pow(abs(v[i]), p);
40
41 return root(sum, p);
42 }
43
44
50 template<typename Vector>
51 inline real l1_norm(const Vector& v) {
52
53 real sum = 0;
54
55 for (unsigned int i = 0; i < v.size(); ++i)
56 sum += abs(v[i]);
57
58 return sum;
59 }
60
66 template<typename Vector>
67 inline real l2_norm(const Vector& v) {
68
69 auto sum = pair_inner_product(v[0], v[0]);
70
71 for (unsigned int i = 1; i < v.size(); ++i)
72 sum += pair_inner_product(v[i], v[i]);
73
74 return real(sqrt(sum));
75 }
76
77
83 template<typename Vector>
84 inline real linf_norm(const Vector& v) {
85
86 real res = abs(v[0]);
87
88 for (unsigned int i = 1; i < v.size(); ++i)
89 res = max(res, abs(v[i]));
90
91 return res;
92 }
93
94
96
97
104 template<typename Vector>
105 inline real euclidean_distance(const Vector& v1, const Vector& v2) {
106
107 if(v1.size() != v2.size()) {
108 TH_MATH_ERROR("euclidean_distance", v1.size(), INVALID_ARGUMENT);
109 return nan();
110 }
111
112 return l2_norm(v1 - v2);
113 }
114
115
122 template<typename Vector>
123 inline real distance(const Vector& v1, const Vector& v2) {
124 return euclidean_distance(v1, v2);
125 }
126
127
135 return abs(a - b);
136 }
137
138
145 template<typename T>
146 inline real euclidean_distance(complex<T> z1, complex<T> z2) {
147 return abs(z1 - z2);
148 }
149
150
157 inline real distance(real a, real b) {
158 return euclidean_distance(a, b);
159 }
160
161
168 template<typename T>
169 inline real distance(complex<T> z1, complex<T> z2) {
170 return euclidean_distance(z1, z2);
171 }
172
173
181 template<typename Vector>
182 inline real minkowski_distance(const Vector& v1, const Vector& v2, unsigned int p) {
183
184 if(v1.size() != v2.size()) {
185 TH_MATH_ERROR("minkowski_distance", v1.size(), INVALID_ARGUMENT);
186 return nan();
187 }
188
189 return lp_norm(v1 - v2, p);
190 }
191
192
200 inline real minkowski_distance(real a, real b, unsigned int p) {
201 return root(pow(abs(b - a), p), p);
202 }
203
204
212 template<typename T>
213 inline real minkowski_distance(complex<T> z1, complex<T> z2, unsigned int p) {
214 return root(pow(abs(z2 - z1), p), p);
215 }
216
217
224 template<typename Vector>
225 inline real manhattan_distance(const Vector& v1, const Vector& v2) {
226
227 if(v1.size() != v2.size()) {
228 TH_MATH_ERROR("manhattan_distance", v1.size(), INVALID_ARGUMENT);
229 return nan();
230 }
231
232 return l1_norm(v1 - v2);
233 }
234
235
242 template<typename Vector>
243 inline real chebyshev_distance(const Vector& v1, const Vector& v2) {
244
245 if(v1.size() != v2.size()) {
246 TH_MATH_ERROR("chebyshev_distance", v1.size(), INVALID_ARGUMENT);
247 return nan();
248 }
249
250 return linf_norm(v1 - v2);
251 }
252
253
261 template<typename Vector>
263 const Vector& v1, const Vector& v2, real tolerance = MACH_EPSILON) {
264
265 if(v1.size() != v2.size()) {
266 TH_MATH_ERROR("discrete_distance", v1.size(), INVALID_ARGUMENT);
267 return nan();
268 }
269
270 bool diff = false;
271
272 for (size_t i = 0; i < v1.size(); ++i) {
273
274 // The vectors differ if a pair of elements differs
275 // more than the given tolerance
276 if(abs(v1[i] - v2[i]) > tolerance) {
277 diff = true;
278 break;
279 }
280 }
281
282 return diff ? 1 : 0;
283 }
284
285
291 template<typename Vector>
292 inline real canberra_distance(const Vector& v1, const Vector& v2) {
293
294 if(v1.size() != v2.size()) {
295 TH_MATH_ERROR("canberra_distance", v1.size(), INVALID_ARGUMENT);
296 return nan();
297 }
298
299 real sum = 0;
300
301 for (size_t i = 0; i < v1.size(); ++i)
302 sum += abs(v1[i] - v2[i]) / (abs(v1[i]) + abs(v2[i]));
303
304 return sum;
305 }
306
307
313 template<typename Vector>
314 inline real cosine_distance(const Vector& v1, const Vector& v2) {
315
316 if(v1.size() != v2.size()) {
317 TH_MATH_ERROR("cosine_distance", v1.size(), INVALID_ARGUMENT);
318 return nan();
319 }
320
321 real product = 0;
322 real sum_sqr_x = 0;
323 real sum_sqr_y = 0;
324
325 for (size_t i = 0; i < v1.size(); ++i) {
326 product += v1[i] * v2[i];
327 sum_sqr_x += square(v1[i]);
328 sum_sqr_y += square(v2[i]);
329 }
330
331 return product / sqrt(sum_sqr_x * sum_sqr_y);
332 }
333
334
342 template<typename Vector>
344 const Vector& v1, const Vector& v2, real tolerance = MACH_EPSILON) {
345
346 if(v1.size() != v2.size()) {
347 TH_MATH_ERROR("hamming_distance", v1.size(), INVALID_ARGUMENT);
348 return nan();
349 }
350
351 unsigned int count = 0;
352
353 // Count how many elements differ to some tolerance
354 for (unsigned int i = 0; i < v1.size(); ++i)
355 if(abs(v1[i] - v2[i]) > tolerance)
356 count++;
357
358 return count;
359 }
360 }
361}
362
363#endif
Linear algebra routines.
#define TH_MATH_ERROR(F_NAME, VALUE, EXCEPTION)
TH_MATH_ERROR is a macro which throws exceptions or modifies errno (depending on which compiling opti...
Definition error.h:225
real l2_norm(const Vector &v)
Compute the l2 norm of a vector: .
Definition distance.h:67
real discrete_distance(const Vector &v1, const Vector &v2, real tolerance=MACH_EPSILON)
Compute the discrete distance between two vectors.
Definition distance.h:262
real l1_norm(const Vector &v)
Compute the l1 norm of a vector: .
Definition distance.h:51
real minkowski_distance(const Vector &v1, const Vector &v2, unsigned int p)
Compute the Minkowski distance between two vectors: .
Definition distance.h:182
real manhattan_distance(const Vector &v1, const Vector &v2)
Compute the Manhattan distance between two vectors: .
Definition distance.h:225
real euclidean_distance(const Vector &v1, const Vector &v2)
Distances.
Definition distance.h:105
real cosine_distance(const Vector &v1, const Vector &v2)
Compute the cosine distance between two vectors.
Definition distance.h:314
auto pair_inner_product(const Type &v_i, const Type &w_i)
Compute the contribution of the inner product between a pair of elements of two vectors,...
Definition algebra.h:252
real linf_norm(const Vector &v)
Compute the linf norm of a vector: .
Definition distance.h:84
real canberra_distance(const Vector &v1, const Vector &v2)
Compute the Canberra distance between two vectors.
Definition distance.h:292
real distance(const Vector &v1, const Vector &v2)
Compute the Euclidean distance between two vectors: .
Definition distance.h:123
real lp_norm(const Vector &v, unsigned int p)
Norms.
Definition distance.h:34
real hamming_distance(const Vector &v1, const Vector &v2, real tolerance=MACH_EPSILON)
Compute the Hamming distance between two vectors.
Definition distance.h:343
real chebyshev_distance(const Vector &v1, const Vector &v2)
Compute the Chebyshev distance between two vectors: .
Definition distance.h:243
Main namespace of the library which contains all functions and objects.
Definition algebra.h:27
double real
A real number, defined as a floating point type.
Definition constants.h:198
dual2 sqrt(dual2 x)
Compute the square root of a second order dual number.
Definition dual2_functions.h:54
dual2 abs(dual2 x)
Compute the absolute value of a second order dual number.
Definition dual2_functions.h:198
auto sum(const Vector &X)
Compute the sum of a vector of real values using pairwise summation to reduce round-off error.
Definition dataset.h:219
auto max(const Vector &X)
Finds the maximum value inside a dataset.
Definition dataset.h:330
TH_CONSTEXPR real nan()
Return a quiet NaN number in floating point representation.
Definition error.h:54
constexpr real MACH_EPSILON
Machine epsilon for the real type.
Definition constants.h:207
real root(real x, int n)
Compute the n-th root of x.
Definition real_analysis.h:812
auto product(const Vector &X)
Compute the product of a set of values.
Definition dataset.h:29
dual2 square(dual2 x)
Return the square of a second order dual number.
Definition dual2_functions.h:23
dual2 pow(dual2 x, int n)
Compute the n-th power of a second order dual number.
Definition dual2_functions.h:41
Vector class and operations.