KMPoint KMData::sample_center(double offset) { KMPoint *sampled_p = (*points_)[internal::random_int(points_->size())]; if (offset == 0.) { return *sampled_p; } KMPoint p; for (int i = 0; i < dim_; i++) { p.push_back((*sampled_p)[i] + internal::random_uniform(-1., 1) * offset); } return p; }
void print_point(const KMPoint &p, std::ostream &out) { out << "[ "; for (unsigned int i = 0; i < p.size(); i++) { out << std::setw(8) << p[i] << " "; } out << " ]"; }
IMPSTATISTICS_BEGIN_INTERNAL_NAMESPACE double km_distance2(const KMPoint &p, const KMPoint &q) { double dist = 0; // IMP_INTERNAL_CHECK(); - TODO L add check for high checks level for (unsigned int i = 0; i < p.size(); i++) { dist += (p[i] - q[i]) * (p[i] - q[i]); } return dist; }
void KMData::sample_centers(KMPointArray *sample, int k, double offset, bool allow_duplicate) { clear_points(sample); IMP_LOG_VERBOSE("KMData::sample_centers size: " << sample->size() << std::endl); if (!allow_duplicate) { IMP_INTERNAL_CHECK(((unsigned int)k) <= points_->size(), "not enough points to sample from"); } Ints sampled_ind; for (int i = 0; i < k; i++) { int ri = internal::random_int(points_->size()); if (!allow_duplicate) { bool dup_found; do { dup_found = false; // search for duplicates for (int j = 0; j < i; j++) { if (sampled_ind[j] == ri) { dup_found = true; ri = internal::random_int(points_->size()); break; } } } while (dup_found); } sampled_ind.push_back(ri); KMPoint *p = new KMPoint(); KMPoint *copied_p = (*points_)[ri]; for (int j = 0; j < dim_; j++) { p->push_back((*copied_p)[j] + internal::random_uniform(-1., 1) * offset); } sample->push_back(p); } IMP_LOG_VERBOSE("KMData::sampled centers : " << std::endl); for (int i = 0; i < k; i++) { IMP_LOG_WRITE(VERBOSE, print_point(*((*sample)[i]))); } IMP_LOG_VERBOSE("\nKMData::sample_centers end size : " << sample->size() << std::endl); }
KMPoint KMRectangle::sample() { KMPoint p; for (unsigned int i = 0; i < lo_.size(); i++) p.push_back(internal::random_uniform(lo_[i], hi_[i])); return p; }
bool KMRectangle::is_inside(const KMPoint &p) { for (unsigned int i = 0; i < p.size(); i++) { if ((p[i] < lo_[i]) || (p[i] > hi_[i])) return false; } return true; }
bool km_is_equal(const KMPoint &p, const KMPoint &q) { for (unsigned int i = 0; i < p.size(); i++) { if (p[i] != q[i]) return false; } return true; }