86 void kcenters(
const std::vector<_T>& data,
unsigned int k, std::vector<unsigned int>& centers,
Matrix& dists)
90 std::vector<double> min_dist(data.size(), std::numeric_limits<double>::infinity());
94 if (
static_cast<long unsigned int>(dists.rows()) < data.size() ||
static_cast<long unsigned int>(dists.cols()) < k)
95 dists.resize(std::max(2 *
static_cast<long unsigned int>(dists.rows()) + 1, data.size()), k);
97 centers.push_back(std::uniform_int_distribution<size_t>{ 0, data.size() - 1 }(rsl::rng()));
98 for (
unsigned i = 1; i < k; ++i)
101 const _T& center = data[centers[i - 1]];
102 double max_dist = -std::numeric_limits<double>::infinity();
103 for (
unsigned j = 0; j < data.size(); ++j)
105 if ((dists(j, i - 1) =
distFun_(data[j], center)) < min_dist[j])
106 min_dist[j] = dists(j, i - 1);
108 if (min_dist[j] > max_dist)
111 max_dist = min_dist[j];
115 if (max_dist < std::numeric_limits<double>::epsilon())
117 centers.push_back(ind);
120 const _T& center = data[centers.back()];
121 unsigned i = centers.size() - 1;
122 for (
unsigned j = 0; j < data.size(); ++j)
123 dists(j, i) =
distFun_(data[j], center);