[mlpack-svn] r14049 - mlpack/trunk/src/mlpack/core/tree

fastlab-svn at coffeetalk-1.cc.gatech.edu fastlab-svn at coffeetalk-1.cc.gatech.edu
Fri Dec 21 15:52:05 EST 2012


Author: rcurtin
Date: 2012-12-21 15:52:04 -0500 (Fri, 21 Dec 2012)
New Revision: 14049

Added:
   mlpack/trunk/src/mlpack/core/tree/mrkd_statistic.cpp
   mlpack/trunk/src/mlpack/core/tree/mrkd_statistic_impl.hpp
Modified:
   mlpack/trunk/src/mlpack/core/tree/CMakeLists.txt
   mlpack/trunk/src/mlpack/core/tree/mrkd_statistic.hpp
Log:
Refactor MRKDStatistic into three files (maybe a little overkill, I realize
now).  Add accessors and mutators after making internal variables private.


Modified: mlpack/trunk/src/mlpack/core/tree/CMakeLists.txt
===================================================================
--- mlpack/trunk/src/mlpack/core/tree/CMakeLists.txt	2012-12-21 20:51:30 UTC (rev 14048)
+++ mlpack/trunk/src/mlpack/core/tree/CMakeLists.txt	2012-12-21 20:52:04 UTC (rev 14049)
@@ -19,10 +19,12 @@
   cover_tree/dual_tree_traverser_impl.hpp
   hrectbound.hpp
   hrectbound_impl.hpp
+  mrkd_statistic.hpp
+  mrkd_statistic_impl.hpp
+  mrkd_statistic.cpp
   periodichrectbound.hpp
   periodichrectbound_impl.hpp
   statistic.hpp
-  mrkd_statistic.hpp
 )
 
 # add directory name to sources

Copied: mlpack/trunk/src/mlpack/core/tree/mrkd_statistic.cpp (from rev 14041, mlpack/trunk/src/mlpack/core/tree/mrkd_statistic.hpp)
===================================================================
--- mlpack/trunk/src/mlpack/core/tree/mrkd_statistic.cpp	                        (rev 0)
+++ mlpack/trunk/src/mlpack/core/tree/mrkd_statistic.cpp	2012-12-21 20:52:04 UTC (rev 14049)
@@ -0,0 +1,43 @@
+/**
+ * @file mrkd_statistic.cpp
+ * @author James Cline
+ *
+ * Definition of the statistic for multi-resolution kd-trees.
+ */
+#include "mrkd_statistic.hpp"
+
+using namespace mlpack;
+using namespace mlpack::tree;
+
+MRKDStatistic::MRKDStatistic() :
+    dataset(NULL),
+    begin(0),
+    count(0),
+    leftStat(NULL),
+    rightStat(NULL),
+    parentStat(NULL)
+{ }
+
+/**
+ * Returns a string representation of this object.
+ */
+std::string MRKDStatistic::ToString() const
+{
+  std::ostringstream convert;
+
+  convert << "MRKDStatistic [" << this << std::endl;
+  convert << "begin: " << begin << std::endl;
+  convert << "count: " << count << std::endl;
+  convert << "sumOfSquaredNorms: " << sumOfSquaredNorms << std::endl;
+  if (leftStat != NULL)
+  {
+    convert << "leftStat:" << std::endl;
+    convert << mlpack::util::Indent(leftStat->ToString());
+  }
+  if (rightStat != NULL)
+  {
+    convert << "rightStat:" << std::endl;
+    convert << mlpack::util::Indent(rightStat->ToString());
+  }
+  return convert.str();
+}

Modified: mlpack/trunk/src/mlpack/core/tree/mrkd_statistic.hpp
===================================================================
--- mlpack/trunk/src/mlpack/core/tree/mrkd_statistic.hpp	2012-12-21 20:51:30 UTC (rev 14048)
+++ mlpack/trunk/src/mlpack/core/tree/mrkd_statistic.hpp	2012-12-21 20:52:04 UTC (rev 14049)
@@ -1,14 +1,14 @@
 /**
  * @file mrkd_statistic.hpp
+ * @author James Cline
  *
- * Definition of the policy type for the statistic class.
- *
- * You should define your own statistic that looks like EmptyStatistic.
+ * Definition of the statistic for multi-resolution kd-trees.
  */
-
 #ifndef __MLPACK_CORE_TREE_MRKD_STATISTIC_HPP
 #define __MLPACK_CORE_TREE_MRKD_STATISTIC_HPP
 
+#include <mlpack/core.hpp>
+
 namespace mlpack {
 namespace tree {
 
@@ -17,139 +17,103 @@
  */
 class MRKDStatistic
 {
-  public:
-    MRKDStatistic()
-    :
-      dataset(NULL),
-      begin(0),
-      count(0),
-      leftStat(NULL),
-      rightStat(NULL),
-      parentStat(NULL)
-    { }
+ public:
+  //! Initialize an empty statistic.
+  MRKDStatistic();
 
-    ~MRKDStatistic() {}
+  /**
+   * This constructor is called when a leaf is created.
+   *
+   * @param dataset Matrix that the tree is being built on.
+   * @param begin Starting index corresponding to this leaf.
+   * @param count Number of points held in this leaf.
+   */
+  template<typename MatType>
+  MRKDStatistic(const MatType& dataset,
+                const size_t begin,
+                const size_t count);
 
-    /**
-     * This constructor is called when a leaf is created.
-     *
-     * @param dataset Matrix that the tree is being built on.
-     * @param begin Starting index corresponding to this leaf.
-     * @param count Number of points held in this leaf.
-     */
-    template<typename MatType>
-    MRKDStatistic(const MatType& dataset,
-                   const size_t begin,
-                   const size_t count)
-    :
-      dataset(&dataset),
-      begin(begin),
-      count(count),
-      leftStat(NULL),
-      rightStat(NULL),
-      parentStat(NULL)
-    {
-      centerOfMass = dataset.col(begin);
-      for(size_t i = begin+1; i < begin+count; ++i)
-        centerOfMass += dataset.col(i);
+  /**
+   * This constructor is called when a non-leaf node is created.
+   * This lets you build fast bottom-up statistics when building trees.
+   *
+   * @param dataset Matrix that the tree is being built on.
+   * @param begin Starting index corresponding to this leaf.
+   * @param count Number of points held in this leaf.
+   * @param leftStat MRKDStatistic object of the left child node.
+   * @param rightStat MRKDStatistic object of the right child node.
+   */
+  template<typename MatType>
+  MRKDStatistic(const MatType& dataset,
+                const size_t begin,
+                const size_t count,
+                MRKDStatistic& leftStat,
+                MRKDStatistic& rightStat);
 
-      sumOfSquaredNorms = 0.0;
-      for(size_t i = begin; i < begin+count; ++i)
-        sumOfSquaredNorms += arma::norm(dataset.col(i), 2);
-    }
+  /**
+   * Returns a string representation of this object.
+   */
+  std::string ToString() const;
 
-    /**
-     * This constructor is called when a non-leaf node is created.
-     * This lets you build fast bottom-up statistics when building trees.
-     *
-     * @param dataset Matrix that the tree is being built on.
-     * @param begin Starting index corresponding to this leaf.
-     * @param count Number of points held in this leaf.
-     * @param leftStat MRKDStatistic object of the left child node.
-     * @param rightStat MRKDStatistic object of the right child node.
-     */
-    template<typename MatType>
-    MRKDStatistic(const MatType& dataset,
-                   const size_t begin,
-                   const size_t count,
-                   MRKDStatistic& leftStat,
-                   MRKDStatistic& rightStat)
-    :
-      dataset(&dataset),
-      begin(begin),
-      count(count),
-      leftStat(&leftStat),
-      rightStat(&rightStat),
-      parentStat(NULL)
-    {
-      sumOfSquaredNorms = leftStat.sumOfSquaredNorms + rightStat.sumOfSquaredNorms;
+  //! Get the index of the initial item in the dataset.
+  size_t Begin() const { return begin; }
+  //! Modify the index of the initial item in the dataset.
+  size_t& Begin() { return begin; }
 
-      /*
-      centerOfMass = ((leftStat.centerOfMass * leftStat.count) +
-                      (rightStat.centerOfMass * rightStat.count)) /
-                      (leftStat.count + rightStat.count);
-      */
-      centerOfMass = leftStat.centerOfMass + rightStat.centerOfMass;
+  //! Get the number of items in the dataset.
+  size_t Count() const { return count; }
+  //! Modify the number of items in the dataset.
+  size_t& Count() { return count; }
 
-      isWhiteListValid = false;
+  //! Get the center of mass.
+  const arma::colvec& CenterOfMass() const { return centerOfMass; }
+  //! Modify the center of mass.
+  arma::colvec& CenterOfMass() { return centerOfMass; }
 
-      leftStat.parentStat = this;
-      rightStat.parentStat = this;
-    }
+  //! Get the index of the dominating centroid.
+  size_t DominatingCentroid() const { return dominatingCentroid; }
+  //! Modify the index of the dominating centroid.
+  size_t& DominatingCentroid() { return dominatingCentroid; }
 
-    /**
-     * Returns a string representation of this object.
-     */
-    std::string ToString() const
-    {
-      std::ostringstream convert;
-      convert << "MRKDStatistic [" << this << std::endl;
-      convert << "begin: " << begin << std::endl;
-      convert << "count: " << count << std::endl;
-      convert << "sumOfSquaredNorms: " << sumOfSquaredNorms << std::endl;
-      if (leftStat != NULL)
-      {
-        convert << "leftStat:" << std::endl;
-        convert << mlpack::util::Indent(leftStat->ToString());
-      }
-      if (rightStat != NULL)
-      {
-        convert << "rightStat:" << std::endl;
-        convert << mlpack::util::Indent(rightStat->ToString());
-      }
-      return convert.str();
-    }
+  //! Access the whitelist.
+  const std::vector<size_t>& Whitelist() const { return whitelist; }
+  //! Modify the whitelist.
+  std::vector<size_t>& Whitelist() { return whitelist; }
 
-    //! The data points this object contains
-    const arma::mat* dataset;
-    //! The initial item in the dataset, so we don't have to make a copy
-    size_t begin;
-    //! The number of items in the dataset
-    size_t count;
-    //! The left child 
-    const MRKDStatistic* leftStat;
-    //! The right child 
-    const MRKDStatistic* rightStat;
-    //! A link to my parent node, null if I am the root
-    const MRKDStatistic* parentStat;
+ private:
+  //! The data points this object contains.
+  const arma::mat* dataset;
+  //! The initial item in the dataset, so we don't have to make a copy.
+  size_t begin;
+  //! The number of items in the dataset.
+  size_t count;
+  //! The left child.
+  const MRKDStatistic* leftStat;
+  //! The right child.
+  const MRKDStatistic* rightStat;
+  //! A link to the parent node; NULL if this is the root.
+  const MRKDStatistic* parentStat;
 
-    // Computed statistics
-    //! The center of mass for this dataset
-    arma::colvec centerOfMass;
-    //! The sum of the squared Euclidian norms for this dataset
-    double sumOfSquaredNorms;
-		
-		// There may be a better place to store this -- HRectBound?
-		//! The index of the dominating centroid of the associated hyperrectangle
-		size_t dominatingCentroid;
+  // Computed statistics.
+  //! The center of mass for this dataset.
+  arma::colvec centerOfMass;
+  //! The sum of the squared Euclidian norms for this dataset.
+  double sumOfSquaredNorms;
 
-    //! The list of centroids that cannot own this hyperrectangle
-    std::vector<size_t> whiteList;
-    //! Whether or not the whitelist is valid
-    bool isWhiteListValid;
+  // There may be a better place to store this -- HRectBound?
+  //! The index of the dominating centroid of the associated hyperrectangle.
+  size_t dominatingCentroid;
+
+  //! The list of centroids that cannot own this hyperrectangle.
+  std::vector<size_t> whitelist;
+  //! Whether or not the whitelist is valid.
+  bool isWhitelistValid;
 };
 
 }; // namespace tree
 }; // namespace mlpack
 
+// Include implementation.
+#include "mrkd_statistic_impl.hpp"
+
 #endif // __MLPACK_CORE_TREE_MRKD_STATISTIC_HPP

Copied: mlpack/trunk/src/mlpack/core/tree/mrkd_statistic_impl.hpp (from rev 14041, mlpack/trunk/src/mlpack/core/tree/mrkd_statistic.hpp)
===================================================================
--- mlpack/trunk/src/mlpack/core/tree/mrkd_statistic_impl.hpp	                        (rev 0)
+++ mlpack/trunk/src/mlpack/core/tree/mrkd_statistic_impl.hpp	2012-12-21 20:52:04 UTC (rev 14049)
@@ -0,0 +1,84 @@
+/**
+ * @file mrkd_statistic_impl.hpp
+ * @author James Cline
+ *
+ * Definition of the statistic for multi-resolution kd-trees.
+ */
+#ifndef __MLPACK_CORE_TREE_MRKD_STATISTIC_IMPL_HPP
+#define __MLPACK_CORE_TREE_MRKD_STATISTIC_IMPL_HPP
+
+// In case it hasn't already been included.
+#include "mrkd_statistic.hpp"
+
+namespace mlpack {
+namespace tree {
+
+/**
+ * This constructor is called when a leaf is created.
+ *
+ * @param dataset Matrix that the tree is being built on.
+ * @param begin Starting index corresponding to this leaf.
+ * @param count Number of points held in this leaf.
+ */
+template<typename MatType>
+MRKDStatistic::MRKDStatistic(const MatType& dataset,
+                             const size_t begin,
+                             const size_t count) :
+    dataset(&dataset),
+    begin(begin),
+    count(count),
+    leftStat(NULL),
+    rightStat(NULL),
+    parentStat(NULL)
+{
+  centerOfMass = dataset.col(begin);
+  for (size_t i = begin+1; i < begin+count; ++i)
+    centerOfMass += dataset.col(i);
+
+  sumOfSquaredNorms = 0.0;
+  for (size_t i = begin; i < begin+count; ++i)
+    sumOfSquaredNorms += arma::norm(dataset.col(i), 2);
+}
+
+/**
+ * This constructor is called when a non-leaf node is created.
+ * This lets you build fast bottom-up statistics when building trees.
+ *
+ * @param dataset Matrix that the tree is being built on.
+ * @param begin Starting index corresponding to this leaf.
+ * @param count Number of points held in this leaf.
+ * @param leftStat MRKDStatistic object of the left child node.
+ * @param rightStat MRKDStatistic object of the right child node.
+ */
+template<typename MatType>
+MRKDStatistic::MRKDStatistic(const MatType& dataset,
+                             const size_t begin,
+                             const size_t count,
+                             MRKDStatistic& leftStat,
+                             MRKDStatistic& rightStat) :
+    dataset(&dataset),
+    begin(begin),
+    count(count),
+    leftStat(&leftStat),
+    rightStat(&rightStat),
+    parentStat(NULL)
+{
+  sumOfSquaredNorms = leftStat.sumOfSquaredNorms + rightStat.sumOfSquaredNorms;
+
+  /*
+  centerOfMass = ((leftStat.centerOfMass * leftStat.count) +
+                  (rightStat.centerOfMass * rightStat.count)) /
+                  (leftStat.count + rightStat.count);
+  */
+  centerOfMass = leftStat.centerOfMass + rightStat.centerOfMass;
+
+  isWhitelistValid = false;
+
+  leftStat.parentStat = this;
+  rightStat.parentStat = this;
+}
+
+}; // namespace tree
+}; // namespace mlpack
+
+#endif // __MLPACK_CORE_TREE_MRKD_STATISTIC_IMPL_HPP




More information about the mlpack-svn mailing list