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
9namespace kmeans {
10
21template<typename Data_, typename Index_, typename Dim_ = int>
23public:
30 SimpleMatrix(Dim_ num_dimensions, Index_ num_observations, const Data_* data) :
31 my_num_dim(num_dimensions), my_num_obs(num_observations), my_data(data), my_long_num_dim(num_dimensions) {}
32
33private:
34 Dim_ my_num_dim;
35 Index_ my_num_obs;
36 const Data_* my_data;
37 size_t my_long_num_dim;
38
39public:
43 typedef Data_ data_type;
44
45 typedef Index_ index_type;
46
47 typedef Dim_ dimension_type;
48
49 struct RandomAccessWorkspace{};
50
51 struct ConsecutiveAccessWorkspace {
52 ConsecutiveAccessWorkspace(index_type start) : at(start) {}
53 size_t at;
54 };
55
56 struct IndexedAccessWorkspace {
57 IndexedAccessWorkspace(const index_type* sequence) : sequence(sequence) {}
58 const index_type* sequence;
59 size_t at = 0;
60 };
61
62public:
63 Index_ num_observations() const {
64 return my_num_obs;
65 }
66
67 dimension_type num_dimensions() const {
68 return my_num_dim;
69 }
70
71 RandomAccessWorkspace create_workspace() const {
72 return RandomAccessWorkspace();
73 }
74
75 ConsecutiveAccessWorkspace create_workspace(index_type start, index_type) const {
76 return ConsecutiveAccessWorkspace(start);
77 }
78
79 IndexedAccessWorkspace create_workspace(const index_type* sequence, index_type) const {
80 return IndexedAccessWorkspace(sequence);
81 }
82
83 const data_type* get_observation(Index_ i, [[maybe_unused]] RandomAccessWorkspace& workspace) const {
84 return my_data + static_cast<size_t>(i) * my_long_num_dim; // avoid overflow during multiplication.
85 }
86
87 const data_type* get_observation(ConsecutiveAccessWorkspace& workspace) const {
88 return my_data + (workspace.at++) * my_long_num_dim; // everything is already a size_t.
89 }
90
91 const data_type* get_observation(IndexedAccessWorkspace& workspace) const {
92 return my_data + static_cast<size_t>(workspace.sequence[workspace.at++]) * my_long_num_dim; // avoid overflow during multiplication.
93 }
97};
98
99}
100
101#endif
Implements the variance partitioning method of Su and Dy (2007).
Definition InitializeVariancePartition.hpp:164
A simple matrix of observations.
Definition SimpleMatrix.hpp:22
SimpleMatrix(Dim_ num_dimensions, Index_ num_observations, const Data_ *data)
Definition SimpleMatrix.hpp:30
Namespace for k-means clustering.
Definition compute_wcss.hpp:12