[mlpack-git] master: backported ind2sub and sub2ind (c3582e2)

gitdub at mlpack.org gitdub at mlpack.org
Mon Jun 6 16:45:26 EDT 2016


Repository : https://github.com/mlpack/mlpack
On branch  : master
Link       : https://github.com/mlpack/mlpack/compare/630efbe8c55e9072cc2be03a8033199ed2836177...b29bad9f77af89306ba48fb918c7772b72f19eac

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

commit c3582e2477adccc338d3ce328fd4d39b97b1b435
Author: nilayjain <nilayjain13 at gmail.com>
Date:   Mon Jun 6 20:45:26 2016 +0000

    backported ind2sub and sub2ind


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

c3582e2477adccc338d3ce328fd4d39b97b1b435
 src/mlpack/core/arma_extend/CMakeLists.txt  |  1 +
 src/mlpack/core/arma_extend/arma_extend.hpp |  2 +
 src/mlpack/core/arma_extend/fn_ind2sub.hpp  | 69 +++++++++++++++++++++++++++++
 src/mlpack/methods/CMakeLists.txt           |  2 +-
 src/mlpack/tests/CMakeLists.txt             |  5 ++-
 src/mlpack/tests/ind2sub_test.cpp           | 19 ++++++++
 6 files changed, 95 insertions(+), 3 deletions(-)

diff --git a/src/mlpack/core/arma_extend/CMakeLists.txt b/src/mlpack/core/arma_extend/CMakeLists.txt
index 4307b26..db0c221 100644
--- a/src/mlpack/core/arma_extend/CMakeLists.txt
+++ b/src/mlpack/core/arma_extend/CMakeLists.txt
@@ -3,6 +3,7 @@
 set(SOURCES
   arma_extend.hpp
   fn_ccov.hpp
+  fn_ind2sub.hpp
   glue_ccov_meat.hpp
   glue_ccov_proto.hpp
   hdf5_misc.hpp
diff --git a/src/mlpack/core/arma_extend/arma_extend.hpp b/src/mlpack/core/arma_extend/arma_extend.hpp
index b8346e5..12765c7 100644
--- a/src/mlpack/core/arma_extend/arma_extend.hpp
+++ b/src/mlpack/core/arma_extend/arma_extend.hpp
@@ -66,6 +66,8 @@ namespace arma {
   #include "glue_ccov_meat.hpp"
   #include "fn_ccov.hpp"
 
+  // index to subscript and vice versa
+  #include "fn_ind2sub.hpp"
   // inplace_reshape()
   #include "fn_inplace_reshape.hpp"
 
diff --git a/src/mlpack/core/arma_extend/fn_ind2sub.hpp b/src/mlpack/core/arma_extend/fn_ind2sub.hpp
new file mode 100644
index 0000000..b4bbfe7
--- /dev/null
+++ b/src/mlpack/core/arma_extend/fn_ind2sub.hpp
@@ -0,0 +1,69 @@
+  
+  #if (ARMA_VERSION_MAJOR < 6 && ARMA_VERSION_MINOR < 399)
+  inline
+  uvec
+  ind2sub(const SizeMat& s, const uword i)
+    {
+    arma_extra_debug_sigprint();
+    
+    arma_debug_check( (i >= (s.n_rows * s.n_cols) ), "ind2sub(): index out of range" );
+    
+    uvec out(2);
+    
+    out[0] = i % s.n_rows;
+    out[1] = i / s.n_rows;
+    
+    return out;
+    }
+
+
+  inline
+  uvec
+  ind2sub(const SizeCube& s, const uword i)
+    {
+    arma_extra_debug_sigprint();
+    
+    arma_debug_check( (i >= (s.n_rows * s.n_cols * s.n_slices) ), "ind2sub(): index out of range" );
+    
+    const uword n_elem_slice = s.n_rows * s.n_cols;
+    
+    const uword slice  = i / n_elem_slice;
+    const uword j      = i - (slice * n_elem_slice);
+    const uword row    = j % s.n_rows;
+    const uword col    = j / s.n_rows;
+    
+    uvec out(3);
+    
+    out[0] = row;
+    out[1] = col;
+    out[2] = slice;
+    
+    return out;
+    }
+
+
+  arma_inline
+  uword
+  sub2ind(const SizeMat& s, const uword row, const uword col)
+    {
+    arma_extra_debug_sigprint();
+    
+    arma_debug_check( ((row >= s.n_rows) || (col >= s.n_cols)), "sub2ind(): subscript out of range" );
+    
+    return uword(row + col*s.n_rows);
+    }
+
+
+  arma_inline
+  uword
+  sub2ind(const SizeCube& s, const uword row, const uword col, const uword slice)
+    {
+    arma_extra_debug_sigprint();
+    
+    arma_debug_check( ((row >= s.n_rows) || (col >= s.n_cols) || (slice >= s.n_slices)), "sub2ind(): subscript out of range" );
+    
+    return uword( (slice * s.n_rows * s.n_cols) + (col * s.n_rows) + row );
+    }
+#endif
+ 
+
diff --git a/src/mlpack/methods/CMakeLists.txt b/src/mlpack/methods/CMakeLists.txt
index 00a67bd..5c58bf9 100644
--- a/src/mlpack/methods/CMakeLists.txt
+++ b/src/mlpack/methods/CMakeLists.txt
@@ -23,7 +23,7 @@ set(DIRS
   det
   emst
   edge_boxes
-  fastmks
+#  fastmks
   gmm
   hmm
   hoeffding_trees
diff --git a/src/mlpack/tests/CMakeLists.txt b/src/mlpack/tests/CMakeLists.txt
index e1f255a..fab4151 100644
--- a/src/mlpack/tests/CMakeLists.txt
+++ b/src/mlpack/tests/CMakeLists.txt
@@ -17,11 +17,12 @@ add_executable(mlpack_test
   det_test.cpp
   distribution_test.cpp
   emst_test.cpp
-  fastmks_test.cpp
+#  fastmks_test.cpp
   feedforward_network_test.cpp
   gmm_test.cpp
   hmm_test.cpp
   hoeffding_tree_test.cpp
+  ind2sub_test.cpp
   init_rules_test.cpp
   kernel_test.cpp
   kernel_pca_test.cpp
@@ -62,7 +63,7 @@ add_executable(mlpack_test
   sgd_test.cpp
   serialization.hpp
   serialization.cpp
-  serialization_test.cpp
+ # serialization_test.cpp
   softmax_regression_test.cpp
   sort_policy_test.cpp
   sparse_autoencoder_test.cpp
diff --git a/src/mlpack/tests/ind2sub_test.cpp b/src/mlpack/tests/ind2sub_test.cpp
new file mode 100644
index 0000000..14baeba
--- /dev/null
+++ b/src/mlpack/tests/ind2sub_test.cpp
@@ -0,0 +1,19 @@
+#include <mlpack/core.hpp>
+//#include <mlpack/methods/edge_boxes/structured_tree.hpp>
+
+#include <boost/test/unit_test.hpp>
+#include "old_boost_test_definitions.hpp"
+BOOST_AUTO_TEST_SUITE(ind2sub_test);
+
+/**
+ * This tests handles the case wherein only one class exists in the input
+ * labels.  It checks whether the only class supplied was the only class
+ * predicted.
+ */
+BOOST_AUTO_TEST_CASE(ind2sub_test)
+{
+  arma::mat A = arma::randu(5,5);
+  arma::uvec u = arma::ind2sub(arma::size(A), 3);
+  u.print();
+}
+BOOST_AUTO_TEST_SUITE_END();




More information about the mlpack-git mailing list