From 18a356c5e5c5ec6fb80fdd2db60d7b9fb08e1b79 Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Fri, 16 Dec 2011 06:37:10 +0000 Subject: [PATCH] Clean up BallBound code; now, expand things in batch because that's better. --- src/mlpack/core/tree/ballbound.hpp | 10 ++++++++-- src/mlpack/core/tree/ballbound_impl.hpp | 7 ++++--- src/mlpack/core/tree/binary_space_tree_impl.hpp | 7 ++----- src/mlpack/core/tree/hrectbound.hpp | 10 +++++++--- src/mlpack/core/tree/hrectbound_impl.hpp | 11 +++++++---- 5 files changed, 28 insertions(+), 17 deletions(-) diff --git a/src/mlpack/core/tree/ballbound.hpp b/src/mlpack/core/tree/ballbound.hpp index 673745583a..2e9ea19dd4 100644 --- a/src/mlpack/core/tree/ballbound.hpp +++ b/src/mlpack/core/tree/ballbound.hpp @@ -113,9 +113,15 @@ class BallBound const BallBound& operator|=(const BallBound& other); /** - * Expand the bound to include the given point. + * Expand the bound to include the given point. The centroid is recalculated + * to be the center of all of the given points. + * + * @tparam MatType Type of matrix; could be arma::mat, arma::spmat, or a + * vector. + * @tparam data Data points to add. */ - const BallBound& operator|=(const VecType& point); + template + const BallBound& operator|=(const MatType& data); }; }; // namespace bound diff --git a/src/mlpack/core/tree/ballbound_impl.hpp b/src/mlpack/core/tree/ballbound_impl.hpp index f647f3388a..fc010edc04 100644 --- a/src/mlpack/core/tree/ballbound_impl.hpp +++ b/src/mlpack/core/tree/ballbound_impl.hpp @@ -110,7 +110,7 @@ math::Range BallBound::RangeDistance( /** * Expand the bound to include the given bound. - */ + * template const BallBound& BallBound::operator|=( @@ -123,14 +123,15 @@ BallBound::operator|=( radius = dist; return *this; -} +}*/ /** * Expand the bound to include the given point. */ template +template const BallBound& -BallBound::operator|=(const VecType& point) +BallBound::operator|=(const MatType& point) { double dist = metric::EuclideanDistance::Evaluate(center, point); diff --git a/src/mlpack/core/tree/binary_space_tree_impl.hpp b/src/mlpack/core/tree/binary_space_tree_impl.hpp index 64f8fa4112..ebbcbd6c7b 100644 --- a/src/mlpack/core/tree/binary_space_tree_impl.hpp +++ b/src/mlpack/core/tree/binary_space_tree_impl.hpp @@ -389,10 +389,8 @@ inline size_t BinarySpaceTree::Count() const template void BinarySpaceTree::SplitNode(MatType& data) { - // This should be a single function for Bound. // We need to expand the bounds of this node properly. - for (size_t i = begin; i < (begin + count); i++) - bound |= data.col(i); + bound |= data.cols(begin, begin + count - 1); // Now, check if we need to split at all. if (count <= leafSize) @@ -441,8 +439,7 @@ void BinarySpaceTree::SplitNode( { // This should be a single function for Bound. // We need to expand the bounds of this node properly. - for (size_t i = begin; i < (begin + count); i++) - bound |= data.col(i); + bound |= data.cols(begin, begin + count - 1); // First, check if we need to split at all. if (count <= leafSize) diff --git a/src/mlpack/core/tree/hrectbound.hpp b/src/mlpack/core/tree/hrectbound.hpp index 9fed17a9af..70ade69188 100644 --- a/src/mlpack/core/tree/hrectbound.hpp +++ b/src/mlpack/core/tree/hrectbound.hpp @@ -106,10 +106,14 @@ class HRectBound math::Range RangeDistance(const VecType& point) const; /** - * Expands this region to include a new point. + * Expands this region to include new points. + * + * @tparam MatType Type of matrix; could be Mat, SpMat, a subview, or just a + * vector. + * @param data Data points to expand this region to include. */ - template - HRectBound& operator|=(const VecType& vector); + template + HRectBound& operator|=(const MatType& data); /** * Expands this region to encompass another bound. diff --git a/src/mlpack/core/tree/hrectbound_impl.hpp b/src/mlpack/core/tree/hrectbound_impl.hpp index b734260a47..cf0996b2d4 100644 --- a/src/mlpack/core/tree/hrectbound_impl.hpp +++ b/src/mlpack/core/tree/hrectbound_impl.hpp @@ -310,13 +310,16 @@ math::Range HRectBound::RangeDistance(const VecType& point) const * Expands this region to include a new point. */ template -template -HRectBound& HRectBound::operator|=(const VecType& vector) +template +HRectBound& HRectBound::operator|=(const MatType& data) { - Log::Assert(vector.n_elem == dim); + Log::Assert(data.n_rows == dim); + + arma::vec mins = min(data, 1); + arma::vec maxs = max(data, 1); for (size_t i = 0; i < dim; i++) - bounds[i] |= vector[i]; + bounds[i] |= math::Range(mins[i], maxs[i]); return *this; }