1#ifndef KNNCOLLE_L2_NORMALIZED_HP
2#define KNNCOLLE_L2_NORMALIZED_HP
25template<
typename Float_>
26const Float_* l2norm(
const Float_* ptr,
size_t ndim, Float_* buffer) {
28 for (
size_t d = 0; d < ndim; ++d) {
38 for (
size_t d = 0; d < ndim; ++d) {
39 buffer[d] = ptr[d] / l2;
59template<
typename Index_,
typename Float_>
68 buffer(num_dimensions)
72 std::unique_ptr<Searcher<Index_, Float_> > my_searcher;
73 std::vector<Float_> buffer;
84 auto normalized = internal::l2norm(
ptr, buffer.size(), buffer.data());
90 return my_searcher->can_search_all();
98 auto normalized = internal::l2norm(
ptr, buffer.size(), buffer.data());
116template<
typename Dim_,
typename Index_,
typename Float_>
125 std::unique_ptr<Prebuilt<Dim_, Index_, Float_> > my_prebuilt;
132 return my_prebuilt->num_observations();
136 return my_prebuilt->num_dimensions();
145 std::unique_ptr<Searcher<Index_, Float_> >
initialize()
const {
146 return std::make_unique<L2NormalizedSearcher<Index_, Float_> >(my_prebuilt->initialize(), my_prebuilt->num_dimensions());
159template<
class Matrix_ = SimpleMatrix<
int,
int,
double> >
168 const Matrix_& my_matrix;
171 typedef typename Matrix_::data_type data_type;
172 typedef typename Matrix_::index_type index_type;
173 typedef typename Matrix_::dimension_type dimension_type;
175 dimension_type num_dimensions()
const {
176 return my_matrix.num_dimensions();
179 index_type num_observations()
const {
180 return my_matrix.num_observations();
184 Workspace(
size_t n) : normalized(n) {}
185 typename Matrix_::Workspace inner;
186 std::vector<data_type> normalized;
189 Workspace create_workspace()
const {
190 return Workspace(my_matrix.num_dimensions());
193 const data_type* get_observation(Workspace& workspace)
const {
194 auto ptr = my_matrix.get_observation(workspace.inner);
195 size_t ndim = workspace.normalized.size();
196 return internal::l2norm(ptr, ndim, workspace.normalized.data());
211template<
class Matrix_ = SimpleMatrix<
int,
int,
double>,
typename Float_ =
double>
227 std::unique_ptr<Builder<L2NormalizedMatrix<Matrix_>, Float_> > my_builder;
Interface to build nearest-neighbor indices.
Interface for prebuilt nearest-neighbor indices.
Interface for searching nearest-neighbor indices.
Interface to build nearest-neighbor search indices.
Definition Builder.hpp:22
Wrapper around a builder with L2 normalization.
Definition L2Normalized.hpp:212
L2NormalizedBuilder(Builder< L2NormalizedMatrix< Matrix_ >, Float_ > *builder)
Definition L2Normalized.hpp:224
L2NormalizedBuilder(std::unique_ptr< Builder< L2NormalizedMatrix< Matrix_ >, Float_ > > builder)
Definition L2Normalized.hpp:218
Prebuilt< typename Matrix_::dimension_type, typename Matrix_::index_type, Float_ > * build_raw(const Matrix_ &data) const
Definition L2Normalized.hpp:233
Wrapper around a matrix with L2 normalization.
Definition L2Normalized.hpp:160
Wrapper around a prebuilt index with L2 normalization.
Definition L2Normalized.hpp:117
std::unique_ptr< Searcher< Index_, Float_ > > initialize() const
Definition L2Normalized.hpp:145
L2NormalizedPrebuilt(std::unique_ptr< Prebuilt< Dim_, Index_, Float_ > > prebuilt)
Definition L2Normalized.hpp:122
Wrapper around a search interface with L2 normalization.
Definition L2Normalized.hpp:60
L2NormalizedSearcher(std::unique_ptr< Searcher< Index_, Float_ > > searcher, size_t num_dimensions)
Definition L2Normalized.hpp:66
Interface for prebuilt nearest-neighbor search indices.
Definition Prebuilt.hpp:28
virtual Index_ num_observations() const =0
virtual Dim_ num_dimensions() const =0
Interface for searching nearest-neighbor search indices.
Definition Searcher.hpp:28
virtual void search(Index_ i, Index_ k, std::vector< Index_ > *output_indices, std::vector< Float_ > *output_distances)=0
virtual bool can_search_all() const
Definition Searcher.hpp:80
virtual Index_ search_all(Index_ i, Float_ distance, std::vector< Index_ > *output_indices, std::vector< Float_ > *output_distances)
Definition Searcher.hpp:101
Collection of KNN algorithms.
Definition Bruteforce.hpp:22