[mlpack-git] master: Clarify some difficult ternary operator operations (the compiler should give something that is resultantly the same code anyway). Also, use the new HRectBound::Volume() function to simplify some later calculations. Testing seems to indicate no noticeable slowdown or speedup. (34f1ef3)

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


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

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

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

commit 34f1ef3b6cce168394944cf8648c8b44bf19aa1c
Author: Ryan Curtin <ryan at ratml.org>
Date:   Mon Aug 18 19:18:50 2014 +0000

    Clarify some difficult ternary operator operations (the compiler should give
    something that is resultantly the same code anyway).  Also, use the new
    HRectBound::Volume() function to simplify some later calculations.  Testing
    seems to indicate no noticeable slowdown or speedup.


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

34f1ef3b6cce168394944cf8648c8b44bf19aa1c
 .../core/tree/rectangle_tree/r_tree_split_impl.hpp | 72 +++++++---------------
 1 file changed, 23 insertions(+), 49 deletions(-)

diff --git a/src/mlpack/core/tree/rectangle_tree/r_tree_split_impl.hpp b/src/mlpack/core/tree/rectangle_tree/r_tree_split_impl.hpp
index 7a73eff..1df0c27 100644
--- a/src/mlpack/core/tree/rectangle_tree/r_tree_split_impl.hpp
+++ b/src/mlpack/core/tree/rectangle_tree/r_tree_split_impl.hpp
@@ -226,10 +226,11 @@ void RTreeSplit<DescentType, StatisticType, MatType>::GetBoundSeeds(
       double score = 1.0;
       for (int k = 0; k < tree.Bound().Dim(); k++)
       {
-        score *= std::max(tree.Children()[i]->Bound()[k].Hi(),
-            tree.Children()[j]->Bound()[k].Hi()) -
-            std::min(tree.Children()[i]->Bound()[k].Lo(),
-            tree.Children()[j]->Bound()[k].Lo());
+        const double hiMax = std::max(tree.Children()[i]->Bound()[k].Hi(),
+                                      tree.Children()[j]->Bound()[k].Hi());
+        const double loMin = std::min(tree.Children()[i]->Bound()[k].Lo(),
+                                      tree.Children()[j]->Bound()[k].Lo());
+        score *= (hiMax - loMin);
       }
 
       if (score > worstPairScore)
@@ -307,30 +308,20 @@ void RTreeSplit<DescentType, StatisticType, MatType>::AssignPointDestNode(
     // rectangle.
 
     // First, calculate the starting volume.
-    double volOne = 1.0;
-    double volTwo = 1.0;
-    for (int i = 0; i < oldTree->Bound().Dim(); i++)
-    {
-      volOne *= treeOne->Bound()[i].Width();
-      volTwo *= treeTwo->Bound()[i].Width();
-    }
+    const double volOne = treeOne->Bound().Volume();
+    const double volTwo = treeTwo->Bound().Volume();
 
     // Find the point that, when assigned to one of the two new rectangles,
     // minimizes the increase in volume.
     for (int index = 0; index < end; index++)
     {
-      double newVolOne = 1.0;
-      double newVolTwo = 1.0;
-      for (int i = 0; i < oldTree->Bound().Dim(); i++)
-      {
-        double c = oldTree->LocalDataset().col(index)[i];
-        newVolOne *= treeOne->Bound()[i].Contains(c) ?
-            treeOne->Bound()[i].Width() : (c < treeOne->Bound()[i].Lo() ?
-            (treeOne->Bound()[i].Hi() - c) : (c - treeOne->Bound()[i].Lo()));
-        newVolTwo *= treeTwo->Bound()[i].Contains(c) ?
-            treeTwo->Bound()[i].Width() : (c < treeTwo->Bound()[i].Lo() ?
-            (treeTwo->Bound()[i].Hi() - c) : (c - treeTwo->Bound()[i].Lo()));
-      }
+      HRectBound<> newBoundOne = treeOne->Bound();
+      newBoundOne |= oldTree->LocalDataset().col(index);
+      const double newVolOne = newBoundOne.Volume();
+
+      HRectBound<> newBoundTwo = treeTwo->Bound();
+      newBoundTwo |= oldTree->LocalDataset().col(index);
+      const double newVolTwo = newBoundTwo.Volume();
 
       // Choose the rectangle that requires the lesser increase in volume.
       if ((newVolOne - volOne) < (newVolTwo - volTwo))
@@ -450,35 +441,18 @@ void RTreeSplit<DescentType, StatisticType, MatType>::AssignNodeDestNode(
 
     // Calculate the increase in volume for assigning this node to each of the
     // new rectangles.
-    double volOne = 1.0;
-    double volTwo = 1.0;
-    for (int i = 0; i < oldTree->Bound().Dim(); i++)
-    {
-      volOne *= treeOne->Bound()[i].Width();
-      volTwo *= treeTwo->Bound()[i].Width();
-    }
+    const double volOne = treeOne->Bound().Volume();
+    const double volTwo = treeTwo->Bound().Volume();
 
     for (int index = 0; index < end; index++)
     {
-      double newVolOne = 1.0;
-      double newVolTwo = 1.0;
-      for (int i = 0; i < oldTree->Bound().Dim(); i++)
-      {
-        // For each of the new rectangles, find the width in this dimension if
-        // we add the rectangle at index to the new rectangle.
-        math::Range range = oldTree->Children()[index]->Bound()[i];
-        newVolOne *= treeOne->Bound()[i].Contains(range) ?
-            treeOne->Bound()[i].Width() : (range.Contains(treeOne->Bound()[i]) ?
-            range.Width() : (range.Lo() < treeOne->Bound()[i].Lo() ?
-            (treeOne->Bound()[i].Hi() - range.Lo()) : (range.Hi() -
-            treeOne->Bound()[i].Lo())));
-
-        newVolTwo *= treeTwo->Bound()[i].Contains(range) ?
-            treeTwo->Bound()[i].Width() : (range.Contains(treeTwo->Bound()[i]) ?
-            range.Width() : (range.Lo() < treeTwo->Bound()[i].Lo() ?
-            (treeTwo->Bound()[i].Hi() - range.Lo()) : (range.Hi() -
-            treeTwo->Bound()[i].Lo())));
-      }
+      HRectBound<> newBoundOne = treeOne->Bound();
+      newBoundOne |= oldTree->Children()[index]->Bound();
+      const double newVolOne = newBoundOne.Volume();
+
+      HRectBound<> newBoundTwo = treeTwo->Bound();
+      newBoundTwo |= oldTree->Children()[index]->Bound();
+      const double newVolTwo = newBoundTwo.Volume();
 
       // Choose the rectangle that requires the lesser increase in volume.
       if ((newVolOne - volOne) < (newVolTwo - volTwo))



More information about the mlpack-git mailing list