kmeans
A C++ library for k-means
Loading...
Searching...
No Matches
SimpleMatrix.hpp
Go to the documentation of this file.
1#ifndef KMEANS_SIMPLE_MATRIX_HPP
2#define KMEANS_SIMPLE_MATRIX_HPP
3
4#include "Matrix.hpp"
5
11namespace kmeans {
12
16template<typename Index_, typename Data_>
17class SimpleMatrix;
18
19template<typename Index_, typename Data_>
20class SimpleMatrixRandomAccessExtractor final : public RandomAccessExtractor<Index_, Data_> {
21public:
22 SimpleMatrixRandomAccessExtractor(const SimpleMatrix<Index_, Data_>& parent) : my_parent(parent) {}
23
24private:
25 const SimpleMatrix<Index_, Data_>& my_parent;
26
27public:
28 const Data_* get_observation(Index_ i) {
29 return my_parent.my_data + static_cast<size_t>(i) * my_parent.my_num_dim; // cast to size_t to avoid overflow during multiplication.
30 }
31};
32
33template<typename Index_, typename Data_>
34class SimpleMatrixConsecutiveAccessExtractor final : public ConsecutiveAccessExtractor<Index_, Data_> {
35public:
36 SimpleMatrixConsecutiveAccessExtractor(const SimpleMatrix<Index_, Data_>& parent, size_t start) : my_parent(parent), my_position(start) {}
37
38private:
39 const SimpleMatrix<Index_, Data_>& my_parent;
40 size_t my_position;
41
42public:
43 const Data_* get_observation() {
44 return my_parent.my_data + (my_position++) * my_parent.my_num_dim; // already size_t's, no casting required.
45 }
46};
47
48template<typename Index_, typename Data_>
49class SimpleMatrixIndexedAccessExtractor final : public IndexedAccessExtractor<Index_, Data_> {
50public:
51 SimpleMatrixIndexedAccessExtractor(const SimpleMatrix<Index_, Data_>& parent, const Index_* sequence) : my_parent(parent), my_sequence(sequence) {}
52
53private:
54 const SimpleMatrix<Index_, Data_>& my_parent;
55 const Index_* my_sequence;
56 size_t my_position = 0;
57
58public:
59 const Data_* get_observation() {
60 return my_parent.my_data + static_cast<size_t>(my_sequence[my_position++]) * my_parent.my_num_dim; // cast to size_t to avoid overflow during multiplication.
61 }
62};
75template<typename Index_, typename Data_>
76class SimpleMatrix final : public Matrix<Index_, Data_> {
77public:
84 SimpleMatrix(size_t num_dimensions, Index_ num_observations, const Data_* data) :
85 my_num_dim(num_dimensions), my_num_obs(num_observations), my_data(data) {}
86
87private:
88 size_t my_num_dim;
89 Index_ my_num_obs;
90 const Data_* my_data;
91 friend class SimpleMatrixRandomAccessExtractor<Index_, Data_>;
92 friend class SimpleMatrixConsecutiveAccessExtractor<Index_, Data_>;
93 friend class SimpleMatrixIndexedAccessExtractor<Index_, Data_>;
94
95public:
99 Index_ num_observations() const {
100 return my_num_obs;
101 }
102
103 size_t num_dimensions() const {
104 return my_num_dim;
105 }
106
107public:
108 std::unique_ptr<RandomAccessExtractor<Index_, Data_> > new_extractor() const {
109 return std::make_unique<SimpleMatrixRandomAccessExtractor<Index_, Data_> >(*this);
110 }
111
112 std::unique_ptr<ConsecutiveAccessExtractor<Index_, Data_> > new_extractor(Index_ start, Index_) const {
113 return std::make_unique<SimpleMatrixConsecutiveAccessExtractor<Index_, Data_> >(*this, start);
114 }
115
116 std::unique_ptr<IndexedAccessExtractor<Index_, Data_> > new_extractor(const Index_* sequence, size_t) const {
117 return std::make_unique<SimpleMatrixIndexedAccessExtractor<Index_, Data_> >(*this, sequence);
118 }
122};
123
124}
125
126#endif
Interface for matrix inputs.
Interface for matrix data.
Definition Matrix.hpp:130
virtual std::unique_ptr< RandomAccessExtractor< Index_, Data_ > > new_extractor() const =0
virtual size_t num_dimensions() const =0
virtual Index_ num_observations() const =0
A simple matrix of observations.
Definition SimpleMatrix.hpp:76
SimpleMatrix(size_t num_dimensions, Index_ num_observations, const Data_ *data)
Definition SimpleMatrix.hpp:84
Namespace for k-means clustering.
Definition compute_wcss.hpp:12