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
23 const Type diff = b - a;
24 const prec_t result = prec_t(diff < Type(0.0) ? -diff : diff);
25
26 // Avoid zero values with negative sign
27 return result != prec_t(0.0) ? result : prec_t(0.0);
28 }
29
30
33 template<typename Vector>
34 inline prec_t euclidean(const Vector& v1, const Vector& v2) {
35
36 if (v1.size() != v2.size() || !v1.size())
37 return get_nan();
38
39 auto sum = (v1[0] - v2[0]) * (v1[0] - v2[0]);
40 for (size_t i = 1; i < v1.size(); i++) {
41 sum += (v1[i] - v2[i]) * (v1[i] - v2[i]);
42 }
43
44 return std::sqrt(sum);
45 }
46
47
50 inline unsigned int hamming(const std::string& a, const std::string& b) {
51
52 unsigned int sum = 0;
53 for (size_t i = 0; i < std::min(a.size(), b.size()); i++)
54 if (a[i] != b[i])
55 sum++;
56
57 if (a.size() != b.size())
58 sum += std::abs(int(a.size()) - int(b.size()));
59
60 return sum;
61 }
62
63 }
64
65}}
66
67#endif
long double prec_t
Floating-point type of higher precision, used in computations, such as error estimation.
Definition common.h:42
Distance functions for use in prec_context::equals().
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:50
prec_t euclidean(const Vector &v1, const Vector &v2)
Euclidean distance between vectors (any type with a size() method and [] operator).
Definition distance.h:34
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