[mlpack] 17/58: 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.

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 f620a4686745897629d414ab173f99dc260728ac
Author: rcurtin <rcurtin at 9d5b8971-822b-0410-80eb-d18c1038ef23>
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.
    
    
    git-svn-id: http://svn.cc.gatech.edu/fastlab/mlpack/trunk@17065 9d5b8971-822b-0410-80eb-d18c1038ef23
---
 .../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))

-- 
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