9 #ifndef mrpt_math_kmeans_H
10 #define mrpt_math_kmeans_H
22 template <
typename SCALAR>
24 const bool use_kmeansplusplus_method,
const size_t nPoints,
const size_t k,
25 const size_t dims,
const SCALAR*
points,
const size_t attempts,
26 SCALAR* out_center,
int* out_assignments);
30 template <
class LIST_OF_VECTORS1,
class LIST_OF_VECTORS2>
32 const bool use_kmeansplusplus_method,
const size_t k,
33 const LIST_OF_VECTORS1&
points, std::vector<int>& assignments,
34 LIST_OF_VECTORS2* out_centers,
const size_t attempts)
39 const size_t N =
points.size();
40 assignments.resize(N);
41 if (out_centers) out_centers->clear();
47 using TInnerVector =
typename LIST_OF_VECTORS1::value_type;
48 using TInnerVectorCenters =
typename LIST_OF_VECTORS2::value_type;
49 std::vector<typename TInnerVector::value_type> raw_vals;
50 typename TInnerVector::value_type* trg_ptr =
nullptr;
57 ASSERTMSG_(dims > 0,
"Dimensionality of points can't be zero.");
58 raw_vals.resize(N * dims);
59 trg_ptr = &raw_vals[0];
64 size_t(dims) ==
size_t(it->size()),
65 "All points must have the same dimensionality.");
70 dims *
sizeof(
typename TInnerVector::value_type));
74 std::vector<typename TInnerVectorCenters::value_type> centers(dims * k);
76 false, N, k,
points.begin()->size(), &raw_vals[0], attempts,
77 ¢ers[0], &assignments[0]);
81 const typename TInnerVectorCenters::value_type* center_ptr =
83 for (
size_t i = 0; i < k; i++)
85 TInnerVectorCenters
c;
87 for (
size_t j = 0; j < dims; j++)
c[j] = *center_ptr++;
88 out_centers->push_back(
c);
123 template <
class LIST_OF_VECTORS1,
class LIST_OF_VECTORS2>
125 const size_t k,
const LIST_OF_VECTORS1&
points,
126 std::vector<int>& assignments, LIST_OF_VECTORS2* out_centers =
nullptr,
127 const size_t attempts = 3)
130 false , k,
points, assignments, out_centers,
158 template <
class LIST_OF_VECTORS1,
class LIST_OF_VECTORS2>
160 const size_t k,
const LIST_OF_VECTORS1&
points,
161 std::vector<int>& assignments, LIST_OF_VECTORS2* out_centers =
nullptr,
162 const size_t attempts = 3)
165 true , k,
points, assignments, out_centers,