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<
unsigned int N>
158 return (z1 - z2).norm();
169 template<
typename Vector>
172 if(v1.size() != v2.size()) {
173 TH_MATH_ERROR(
"minkowski_distance", v1.size(), INVALID_ARGUMENT);
199 template<
typename Vector,
typename T = real>
202 if(v1.size() != v2.size()) {
203 TH_MATH_ERROR(
"hermitian_distance", v1.size(), INVALID_ARGUMENT);
209 for (
size_t i = 0; i < v1.size(); ++i) {
224 template<
unsigned int N,
typename T>
228 return hermitian_distance<vec<complex<T>, N>, T>(v1, v2);
238 template<
typename Vector>
241 if(v1.size() != v2.size()) {
242 TH_MATH_ERROR(
"manhattan_distance", v1.size(), INVALID_ARGUMENT);
256 template<
typename Vector>
259 if(v1.size() != v2.size()) {
260 TH_MATH_ERROR(
"chebyshev_distance", v1.size(), INVALID_ARGUMENT);
275 template<
typename Vector>
279 if(v1.size() != v2.size()) {
280 TH_MATH_ERROR(
"discrete_distance", v1.size(), INVALID_ARGUMENT);
286 for (
size_t i = 0; i < v1.size(); ++i) {
290 if(
abs(v1[i] - v2[i]) > tolerance) {
305 template<
typename Vector>
308 if(v1.size() != v2.size()) {
309 TH_MATH_ERROR(
"canberra_distance", v1.size(), INVALID_ARGUMENT);
315 for (
size_t i = 0; i < v1.size(); ++i)
327 template<
typename Vector>
330 if(v1.size() != v2.size()) {
331 TH_MATH_ERROR(
"cosine_distance", v1.size(), INVALID_ARGUMENT);
339 for (
size_t i = 0; i < v1.size(); ++i) {
341 sum_sqr_x +=
square(v1[i]);
342 sum_sqr_y +=
square(v2[i]);
356 template<
typename Vector>
360 if(v1.size() != v2.size()) {
361 TH_MATH_ERROR(
"hamming_distance", v1.size(), INVALID_ARGUMENT);
365 unsigned int count = 0;
368 for (
unsigned int i = 0; i < v1.size(); ++i)
369 if(
abs(v1[i] - v2[i]) > tolerance)
Complex number in algebraic form .
Definition: complex.h:26
complex conjugate() const
Compute the conjugate of the complex number.
Definition: complex.h:122
A statically allocated N-dimensional vector with elements of the given type.
Definition: vec.h:88
#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:219
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:276
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:170
real manhattan_distance(const Vector &v1, const Vector &v2)
Compute the Manhattan distance between two vectors: .
Definition: distance.h:239
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:328
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
auto hermitian_distance(const Vector &v1, const Vector &v2)
Compute the Hermitian distance between two vectors: .
Definition: distance.h:200
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:306
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:357
real distance(const vec< real, N > &v1, const vec< real, N > &v2)
Compute the Euclidean distance between two vectors: .
Definition: distance.h:123
real chebyshev_distance(const Vector &v1, const Vector &v2)
Compute the Chebyshev distance between two vectors: .
Definition: distance.h:257
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:188
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:183
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
dual2 conjugate(dual2 x)
Return the conjugate of a second order dual number.
Definition: dual2_functions.h:35
auto max(const Vector &X)
Finds the maximum value inside a dataset.
Definition: dataset.h:330
constexpr real MACH_EPSILON
Machine epsilon for the real type.
Definition: constants.h:197
real root(real x, int n)
Compute the n-th root of x.
Definition: real_analysis.h:813
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
real nan()
Return a quiet NaN number in floating point representation.
Definition: error.h:54
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.