8#ifndef THEORETICA_DISTANCE_H
9#define THEORETICA_DISTANCE_H
13#include "../core/constants.h"
14#include "../core/core_traits.h"
15#include "../core/real_analysis.h"
16#include "../complex/complex_analysis.h"
33 template<
typename Vector>
38 for (
unsigned int i = 0; i < v.size(); ++i)
50 template<
typename Vector>
55 for (
unsigned int i = 0; i < v.size(); ++i)
66 template<
typename Vector>
71 for (
unsigned int i = 1; i < v.size(); ++i)
83 template<
typename Vector>
88 for (
unsigned int i = 1; i < v.size(); ++i)
104 template<
typename Vector>
107 if(v1.size() != v2.size()) {
108 TH_MATH_ERROR(
"euclidean_distance", v1.size(), INVALID_ARGUMENT);
122 template<
typename Vector>
181 template<
typename Vector>
184 if(v1.size() != v2.size()) {
185 TH_MATH_ERROR(
"minkowski_distance", v1.size(), INVALID_ARGUMENT);
224 template<
typename Vector>
227 if(v1.size() != v2.size()) {
228 TH_MATH_ERROR(
"manhattan_distance", v1.size(), INVALID_ARGUMENT);
242 template<
typename Vector>
245 if(v1.size() != v2.size()) {
246 TH_MATH_ERROR(
"chebyshev_distance", v1.size(), INVALID_ARGUMENT);
261 template<
typename Vector>
265 if(v1.size() != v2.size()) {
266 TH_MATH_ERROR(
"discrete_distance", v1.size(), INVALID_ARGUMENT);
272 for (
size_t i = 0; i < v1.size(); ++i) {
276 if(
abs(v1[i] - v2[i]) > tolerance) {
291 template<
typename Vector>
294 if(v1.size() != v2.size()) {
295 TH_MATH_ERROR(
"canberra_distance", v1.size(), INVALID_ARGUMENT);
301 for (
size_t i = 0; i < v1.size(); ++i)
313 template<
typename Vector>
316 if(v1.size() != v2.size()) {
317 TH_MATH_ERROR(
"cosine_distance", v1.size(), INVALID_ARGUMENT);
325 for (
size_t i = 0; i < v1.size(); ++i) {
327 sum_sqr_x +=
square(v1[i]);
328 sum_sqr_y +=
square(v2[i]);
342 template<
typename Vector>
346 if(v1.size() != v2.size()) {
347 TH_MATH_ERROR(
"hamming_distance", v1.size(), INVALID_ARGUMENT);
351 unsigned int count = 0;
354 for (
unsigned int i = 0; i < v1.size(); ++i)
355 if(
abs(v1[i] - v2[i]) > tolerance)
#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.