[mlpack] 107/207: Unwrap arma::Op objects for printing in prefixedoutstream

Barak A. Pearlmutter barak+git at pearlmutter.net
Thu Mar 23 17:53:45 UTC 2017


This is an automated email from the git hooks/post-receive script.

bap pushed a commit to branch master
in repository mlpack.

commit d8f88918c15c1cfc9ab8448d84ea744baf8528c4
Author: shikhar <shikharbhardwaj68 at gmail.com>
Date:   Thu Mar 2 20:54:18 2017 +0530

    Unwrap arma::Op objects for printing in prefixedoutstream
---
 src/mlpack/core/util/prefixedoutstream_impl.hpp | 13 +++++----
 src/mlpack/tests/prefixedoutstream_test.cpp     | 37 +++++++++++++++----------
 2 files changed, 31 insertions(+), 19 deletions(-)

diff --git a/src/mlpack/core/util/prefixedoutstream_impl.hpp b/src/mlpack/core/util/prefixedoutstream_impl.hpp
index f9d3ddd..9ccf396 100644
--- a/src/mlpack/core/util/prefixedoutstream_impl.hpp
+++ b/src/mlpack/core/util/prefixedoutstream_impl.hpp
@@ -140,6 +140,9 @@ template<typename T>
 typename std::enable_if<arma::is_arma_type<T>::value, void>::type
 PrefixedOutStream::BaseLogic(const T& val)
 {
+
+  // Extract printable object from the input
+  auto printVal = arma::unwrap<T>(val).M;
   // We will use this to track whether or not we need to terminate at the end of
   // this call (only for streams which terminate after a newline).
   bool newlined = false;
@@ -154,7 +157,7 @@ PrefixedOutStream::BaseLogic(const T& val)
   if(destination.flags() == convert.flags() && 
      destination.precision() == convert.precision())
   {
-    convert << val;
+    arma::arma_ostream::print(convert, printVal, true);
   }
   else
   {
@@ -163,16 +166,16 @@ PrefixedOutStream::BaseLogic(const T& val)
     convert.precision(destination.precision());
 
     // Set width of the convert stream
-    double maxVal = arma::abs(val).max();
-    if(maxVal == 0.f) {
+    double maxVal = arma::abs(printVal).max();
+    if(maxVal == 0.f)
+    {
         maxVal = 1;
     }
     int maxLog = log10(maxVal);
     maxLog = (maxLog > 0) ? floor(maxLog) + 1 : 1;
-
     const int padding = 4;
     convert.width(convert.precision() + maxLog + padding);
-    arma::arma_ostream::print(convert, val, false);
+    arma::arma_ostream::print(convert, printVal, false);
   }
 
   if (convert.fail())
diff --git a/src/mlpack/tests/prefixedoutstream_test.cpp b/src/mlpack/tests/prefixedoutstream_test.cpp
index ae2ed9d..0527859 100644
--- a/src/mlpack/tests/prefixedoutstream_test.cpp
+++ b/src/mlpack/tests/prefixedoutstream_test.cpp
@@ -81,12 +81,12 @@ BOOST_AUTO_TEST_CASE(TestArmadilloPrefixedOutStream)
       BASH_GREEN "[INFO ] " BASH_CLEAR "   3.5000\n"
       BASH_GREEN "[INFO ] " BASH_CLEAR "   4.0000\n");
 
-  // TODO: Make Op types printable on logs?
-  //ss.str("");
-  //pss << trans(test);
-  //// This should result in there being stuff on the line.
-  //BOOST_REQUIRE_EQUAL(ss.str(), BASH_GREEN "[INFO ] " BASH_CLEAR
-      //"   1.0000   1.5000   2.0000   2.5000   3.0000   3.5000   4.0000\n");
+  ss.str("");
+  pss << trans(test);
+
+  // This should result in there being stuff on the line.
+  BOOST_REQUIRE_EQUAL(ss.str(), BASH_GREEN "[INFO ] " BASH_CLEAR
+      "   1.0000   1.5000   2.0000   2.5000   3.0000   3.5000   4.0000\n");
 
   arma::mat test2("1.0 1.5 2.0; 2.5 3.0 3.5; 4.0 4.5 4.99999");
   ss.str("");
@@ -113,7 +113,7 @@ BOOST_AUTO_TEST_CASE(TestArmadilloPrefixedOutStream)
 
   pss << test;
 
-  BOOST_REQUIRE_EQUAL(ss.str(), 
+  BOOST_REQUIRE_EQUAL(ss.str(),
       BASH_GREEN "[INFO ] " BASH_CLEAR "   1.000000\n"
       BASH_GREEN "[INFO ] " BASH_CLEAR "   1.500000\n"
       BASH_GREEN "[INFO ] " BASH_CLEAR "   2.000000\n"
@@ -122,6 +122,15 @@ BOOST_AUTO_TEST_CASE(TestArmadilloPrefixedOutStream)
       BASH_GREEN "[INFO ] " BASH_CLEAR "   3.500000\n"
       BASH_GREEN "[INFO ] " BASH_CLEAR "   4.000000\n");
 
+  ss.str("");
+
+  pss << trans(test);
+
+  BOOST_REQUIRE_EQUAL(ss.str(),
+      BASH_GREEN "[INFO ] " BASH_CLEAR
+      "   1.000000   1.500000   2.000000   2.500000"
+      "   3.000000   3.500000   4.000000\n");
+
   // Try printing a matrix, with higher precision
   ss << std::setprecision(8);
   ss.str("");
@@ -129,25 +138,25 @@ BOOST_AUTO_TEST_CASE(TestArmadilloPrefixedOutStream)
   pss << test2;
 
   BOOST_REQUIRE_EQUAL(ss.str(),
-      BASH_GREEN "[INFO ] " BASH_CLEAR 
+      BASH_GREEN "[INFO ] " BASH_CLEAR
       "   1.00000000   1.50000000   2.00000000\n"
-      BASH_GREEN "[INFO ] " BASH_CLEAR 
+      BASH_GREEN "[INFO ] " BASH_CLEAR
       "   2.50000000   3.00000000   3.50000000\n"
-      BASH_GREEN "[INFO ] " BASH_CLEAR 
+      BASH_GREEN "[INFO ] " BASH_CLEAR
       "   4.00000000   4.50000000   4.99999000\n");
 
   // Try alignment with larger values
   test2.at(2) = 40;
   ss.str("");
-  pss << test2;
+  pss << trans(test2);
 
   BOOST_REQUIRE_EQUAL(ss.str(),
       BASH_GREEN "[INFO ] " BASH_CLEAR 
-      "    1.00000000    1.50000000    2.00000000\n"
+      "    1.00000000    2.50000000   40.00000000\n"
       BASH_GREEN "[INFO ] " BASH_CLEAR 
-      "    2.50000000    3.00000000    3.50000000\n"
+      "    1.50000000    3.00000000    4.50000000\n"
       BASH_GREEN "[INFO ] " BASH_CLEAR 
-      "   40.00000000    4.50000000    4.99999000\n");
+      "    2.00000000    3.50000000    4.99999000\n");
 
   // Test stream after reset
   test2.at(2) = 4;

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