[mlpack] 16/58: Add HRectBound<>::Volume() and a test for it.
Barak A. Pearlmutter
barak+git at cs.nuim.ie
Tue Sep 9 13:19:39 UTC 2014
This is an automated email from the git hooks/post-receive script.
bap pushed a commit to branch svn-trunk
in repository mlpack.
commit 90a50290834783823ed25174bd607bec66d90a9b
Author: rcurtin <rcurtin at 9d5b8971-822b-0410-80eb-d18c1038ef23>
Date: Mon Aug 18 17:50:15 2014 +0000
Add HRectBound<>::Volume() and a test for it.
git-svn-id: http://svn.cc.gatech.edu/fastlab/mlpack/trunk@17064 9d5b8971-822b-0410-80eb-d18c1038ef23
---
src/mlpack/core/tree/hrectbound.hpp | 7 +++++++
src/mlpack/core/tree/hrectbound_impl.hpp | 15 +++++++++++++++
src/mlpack/tests/tree_test.cpp | 15 +++++++++++++++
3 files changed, 37 insertions(+)
diff --git a/src/mlpack/core/tree/hrectbound.hpp b/src/mlpack/core/tree/hrectbound.hpp
index 79590f9..4d81338 100644
--- a/src/mlpack/core/tree/hrectbound.hpp
+++ b/src/mlpack/core/tree/hrectbound.hpp
@@ -79,6 +79,13 @@ class HRectBound
void Centroid(arma::vec& centroid) const;
/**
+ * Calculate the volume of the hyperrectangle.
+ *
+ * @return Volume of the hyperrectangle.
+ */
+ double Volume() const;
+
+ /**
* Calculates minimum bound-to-point distance.
*
* @param point Point to which the minimum distance is requested.
diff --git a/src/mlpack/core/tree/hrectbound_impl.hpp b/src/mlpack/core/tree/hrectbound_impl.hpp
index df59317..a1dc041 100644
--- a/src/mlpack/core/tree/hrectbound_impl.hpp
+++ b/src/mlpack/core/tree/hrectbound_impl.hpp
@@ -116,6 +116,21 @@ void HRectBound<Power, TakeRoot>::Centroid(arma::vec& centroid) const
}
/**
+ * Calculate the volume of the hyperrectangle.
+ *
+ * @return Volume of the hyperrectangle.
+ */
+template<int Power, bool TakeRoot>
+double HRectBound<Power, TakeRoot>::Volume() const
+{
+ double volume = 1.0;
+ for (size_t i = 0; i < dim; ++i)
+ volume *= (bounds[i].Hi() - bounds[i].Lo());
+
+ return volume;
+}
+
+/**
* Calculates minimum bound-to-point squared distance.
*/
template<int Power, bool TakeRoot>
diff --git a/src/mlpack/tests/tree_test.cpp b/src/mlpack/tests/tree_test.cpp
index eaa6c79..6125a14 100644
--- a/src/mlpack/tests/tree_test.cpp
+++ b/src/mlpack/tests/tree_test.cpp
@@ -143,6 +143,21 @@ BOOST_AUTO_TEST_CASE(HRectBoundCentroid)
}
/**
+ * Ensure the volume calculation is correct.
+ */
+BOOST_AUTO_TEST_CASE(HRectBoundVolume)
+{
+ // Create a simple 3-dimensional bound.
+ HRectBound<2> b(3);
+
+ b[0] = Range(0.0, 5.0);
+ b[1] = Range(-2.0, -1.0);
+ b[2] = Range(-10.0, 50.0);
+
+ BOOST_REQUIRE_CLOSE(b.Volume(), 300.0, 1e-5);
+}
+
+/**
* Ensure that we calculate the correct minimum distance between a point and a
* bound.
*/
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/mlpack.git
More information about the debian-science-commits
mailing list