[mlpack-git] master: Add HRectBound<>::Volume() and a test for it. (f557c18)

gitdub at big.cc.gt.atl.ga.us gitdub at big.cc.gt.atl.ga.us
Thu Mar 5 21:58:35 EST 2015


Repository : https://github.com/mlpack/mlpack

On branch  : master
Link       : https://github.com/mlpack/mlpack/compare/904762495c039e345beba14c1142fd719b3bd50e...f94823c800ad6f7266995c700b1b630d5ffdcf40

>---------------------------------------------------------------

commit f557c18e9f72c4d44a0c72de44eff672829f2ab2
Author: Ryan Curtin <ryan at ratml.org>
Date:   Mon Aug 18 17:50:15 2014 +0000

    Add HRectBound<>::Volume() and a test for it.


>---------------------------------------------------------------

f557c18e9f72c4d44a0c72de44eff672829f2ab2
 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.
  */



More information about the mlpack-git mailing list