Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: spatial.cKDTree: do not slide the median #20606

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
32 changes: 16 additions & 16 deletions scipy/spatial/ckdtree/src/build.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -121,24 +121,24 @@ build(ckdtree *self, ckdtree_intp_t start_idx, intptr_t end_idx,
split = (maxval + minval) / 2;

p = partition_pivot(indices + start_idx, indices + end_idx, split);
}

/* slide midpoint if necessary */
if (p == start_idx) {
/* no points less than split */
auto min_idx = *std::min_element(
indices + start_idx, indices + end_idx, index_compare);
split = std::nextafter(data[min_idx * m + d], HUGE_VAL);
p = partition_pivot(indices + start_idx, indices + end_idx, split);
}
else if (p == end_idx) {
/* no points greater than split */
auto max_idx = *std::max_element(
indices + start_idx, indices + end_idx, index_compare);
split = data[max_idx * m + d];
p = partition_pivot(indices + start_idx, indices + end_idx, split);
/* slide midpoint if necessary */
if (p == start_idx) {
/* no points less than split */
auto min_idx = *std::min_element(
indices + start_idx, indices + end_idx, index_compare);
split = std::nextafter(data[min_idx * m + d], HUGE_VAL);
p = partition_pivot(indices + start_idx, indices + end_idx, split);
}
else if (p == end_idx) {
/* no points greater than split */
auto max_idx = *std::max_element(
indices + start_idx, indices + end_idx, index_compare);
split = data[max_idx * m + d];
p = partition_pivot(indices + start_idx, indices + end_idx, split);
}
}

if (CKDTREE_UNLIKELY(p == start_idx || p == end_idx)) {
// All children are equal in this dimension, try again with new bounds
assert(!_compact);
Expand Down