knncolle
Collection of KNN methods in C++
Loading...
Searching...
No Matches
distances.hpp
Go to the documentation of this file.
1#ifndef KNNCOLLE_DISTANCES_HPP
2#define KNNCOLLE_DISTANCES_HPP
3
4#include <cmath>
5
12namespace knncolle {
13
34 template<typename Output_, typename DataX_, typename DataY_, typename Dim_>
35 static Output_ raw_distance(const DataX_* x, const DataY_* y, Dim_ num_dimensions) {
36 Output_ output = 0;
37 for (Dim_ d = 0; d < num_dimensions; ++d, ++x, ++y) {
38 auto delta = static_cast<Output_>(*x) - *y; // see below for comments.
39 output += delta * delta;
40 }
41 return output;
42 }
43
49 template<typename Output_>
50 static Output_ normalize(Output_ raw) {
51 return raw;
52 }
53
59 template<typename Output_>
60 static Output_ denormalize(Output_ norm) {
61 return norm;
62 }
63};
64
82 template<typename Output_, typename DataX_, typename DataY_, typename Dim_>
83 static Output_ raw_distance(const DataX_* x, const DataY_* y, Dim_ num_dimensions) {
84 Output_ output = 0;
85 for (Dim_ d = 0; d < num_dimensions; ++d, ++x, ++y) {
86 auto delta = static_cast<Output_>(*x) - static_cast<Output_>(*y); // casting to ensure consistent precision regardless of DataX_, DataY_.
87 output += delta * delta;
88 }
89 return output;
90 }
91
97 template<typename Output_>
98 static Output_ normalize(Output_ raw) {
99 return std::sqrt(raw);
100 }
101
107 template<typename Output_>
108 static Output_ denormalize(Output_ norm) {
109 return norm * norm;
110 }
111};
112
130 template<typename Output_, typename DataX_, typename DataY_, typename Dim_>
131 static Output_ raw_distance(const DataX_* x, const DataY_* y, Dim_ num_dimensions) {
132 Output_ output = 0;
133 for (Dim_ d = 0; d < num_dimensions; ++d, ++x, ++y) {
134 output += std::abs(static_cast<Output_>(*x) - static_cast<Output_>(*y)); // casting to ensure consistent precision regardless of DataX_, DataY_.
135 }
136 return output;
137 }
138
144 template<typename Output_>
145 static Output_ normalize(Output_ raw) {
146 return raw;
147 }
148
154 template<typename Output_>
155 static Output_ denormalize(Output_ norm) {
156 return norm;
157 }
158};
159
160}
161
162#endif
Collection of KNN algorithms.
Definition Bruteforce.hpp:22
Compute Euclidean distances between two input vectors.
Definition distances.hpp:68
static Output_ raw_distance(const DataX_ *x, const DataY_ *y, Dim_ num_dimensions)
Definition distances.hpp:83
static Output_ denormalize(Output_ norm)
Definition distances.hpp:108
static Output_ normalize(Output_ raw)
Definition distances.hpp:98
Compute Manhattan distances between two input vectors.
Definition distances.hpp:116
static Output_ denormalize(Output_ norm)
Definition distances.hpp:155
static Output_ normalize(Output_ raw)
Definition distances.hpp:145
static Output_ raw_distance(const DataX_ *x, const DataY_ *y, Dim_ num_dimensions)
Definition distances.hpp:131
Expectations for a distance calculation class.
Definition distances.hpp:17
static Output_ raw_distance(const DataX_ *x, const DataY_ *y, Dim_ num_dimensions)
Definition distances.hpp:35
static Output_ normalize(Output_ raw)
Definition distances.hpp:50
static Output_ denormalize(Output_ norm)
Definition distances.hpp:60