Chebyshev
Unit testing for scientific software
Loading...
Searching...
No Matches
distance.h
Go to the documentation of this file.
1
5
6#ifndef CHEBYSHEV_DISTANCE_H
7#define CHEBYSHEV_DISTANCE_H
8#include <cmath>
9
10
11namespace chebyshev {
12namespace prec {
13
15 namespace distance {
16
17
20 template<typename Type = real_t>
21 inline prec_t absolute(Type a, Type b) {
22 const Type diff = b - a;
23 return prec_t(diff > Type(0.0) ? diff : -diff);
24 }
25
26
29 template<typename Vector>
30 inline prec_t euclidean(const Vector& v1, const Vector& v2) {
31
32 if (v1.size() != v2.size() || !v1.size())
33 return get_nan();
34
35 auto sum = (v1[0] - v2[0]) * (v1[0] - v2[0]);
36 for (size_t i = 1; i < v1.size(); i++) {
37 sum += (v1[i] - v2[i]) * (v1[i] - v2[i]);
38 }
39
40 return std::sqrt(sum);
41 }
42
43
46 inline unsigned int hamming(const std::string& a, const std::string& b) {
47
48 unsigned int sum = 0;
49 for (size_t i = 0; i < std::min(a.size(), b.size()); i++)
50 if (a[i] != b[i])
51 sum++;
52
53 if (a.size() != b.size())
54 sum += std::abs(int(a.size()) - int(b.size()));
55
56 return sum;
57 }
58
59 }
60
61}}
62
63#endif
long double prec_t
Floating-point type of higher precision, used in computations, such as error estimation.
Definition common.h:42
unsigned int hamming(const std::string &a, const std::string &b)
Hamming distance between two strings, defined as the number of positions at which the corresponding c...
Definition distance.h:46
prec_t euclidean(const Vector &v1, const Vector &v2)
Euclidean distance between vectors (any type with a size() method and [] operator).
Definition distance.h:30
prec_t absolute(Type a, Type b)
Absolute distance between two values which have an ordering with respect to zero.
Definition distance.h:21
General namespace of the framework.
Definition benchmark.h:22
constexpr FloatType get_nan()
Get a quiet NaN of the specified floating point type.
Definition common.h:65