[arrayfire] 73/408: Allow users to set precision when using print

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Mon Sep 21 19:11:19 UTC 2015


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

ghisvail-guest pushed a commit to branch debian/sid
in repository arrayfire.

commit 99a9234d8378bf22b835beaf5fcbb24ae52e7d5c
Author: Shehzan Mohammed <shehzan at arrayfire.com>
Date:   Thu Jul 2 13:34:26 2015 -0400

    Allow users to set precision when using print
---
 include/af/util.h    | 21 ++++++++++++++++++++
 src/api/c/print.cpp  | 56 +++++++++++++++++++++++++++++++++++++---------------
 src/api/cpp/util.cpp |  7 +++++++
 3 files changed, 68 insertions(+), 16 deletions(-)

diff --git a/include/af/util.h b/include/af/util.h
index 6b1a984..92ca840 100644
--- a/include/af/util.h
+++ b/include/af/util.h
@@ -30,12 +30,23 @@ namespace af
     */
     AFAPI void print(const char *exp, const array &arr);
 
+    /**
+        \param[in] exp is an expression, generally the name of the array
+        \param[in] arr is the input array
+        \param[in] precision is the precision length for display
+
+        \ingroup print_func_print
+    */
+    AFAPI void print(const char *exp, const array &arr, const int precision);
+
     // Purpose of Addition: "How to add Function" documentation
     AFAPI array exampleFunction(const array& in, const af_someenum_t param);
 }
 
 #define af_print(exp) af::print(#exp, exp);
 
+#define af_print_p(exp, p) af::print(#exp, exp, p);
+
 #endif //__cplusplus
 
 #ifdef __cplusplus
@@ -238,6 +249,16 @@ extern "C" {
     */
     AFAPI af_err af_print_array(af_array arr);
 
+    /**
+        \param[in] arr is the input array
+        \param[in] precision precision for the display
+
+        \returns error codes
+
+        \ingroup print_func_print
+    */
+    AFAPI af_err af_print_array_p(af_array arr, const int precision);
+
     // Purpose of Addition: "How to add Function" documentation
     AFAPI af_err af_example_function(af_array* out, const af_array in, const af_someenum_t param);
 
diff --git a/src/api/c/print.cpp b/src/api/c/print.cpp
index a85505d..1b9357b 100644
--- a/src/api/c/print.cpp
+++ b/src/api/c/print.cpp
@@ -28,7 +28,7 @@ using std::endl;
 using std::vector;
 
 template<typename T>
-static void printer(ostream &out, const T* ptr, const ArrayInfo &info, unsigned dim)
+static void printer(ostream &out, const T* ptr, const ArrayInfo &info, unsigned dim, const int precision)
 {
 
     dim_t stride =   info.strides()[dim];
@@ -38,14 +38,14 @@ static void printer(ostream &out, const T* ptr, const ArrayInfo &info, unsigned
     if(dim == 0) {
         for(dim_t i = 0, j = 0; i < d; i++, j+=stride) {
             out<<   std::fixed <<
-                    std::setw(10) <<
-                    std::setprecision(4) << toNum(ptr[j]) << " ";
+                    std::setw(precision + 6) <<
+                    std::setprecision(precision) << toNum(ptr[j]) << " ";
         }
         out << endl;
     }
     else {
         for(dim_t i = 0; i < d; i++) {
-            printer(out, ptr, info, dim - 1);
+            printer(out, ptr, info, dim - 1, precision);
             ptr += stride;
         }
         out << endl;
@@ -53,7 +53,7 @@ static void printer(ostream &out, const T* ptr, const ArrayInfo &info, unsigned
 }
 
 template<typename T>
-static void print(af_array arr)
+static void print(af_array arr, const int precision)
 {
     const ArrayInfo info = getInfo(arr);
     vector<T> data(info.elements());
@@ -74,7 +74,7 @@ static void print(af_array arr)
     std::cout <<"   Strides: ["<<info.strides()<<"]"<<std::endl;
 #endif
 
-    printer(std::cout, &data.front(), infoT, infoT.ndims() - 1);
+    printer(std::cout, &data.front(), infoT, infoT.ndims() - 1, precision);
 
     std::cout.flags(backup);
 }
@@ -86,16 +86,40 @@ af_err af_print_array(af_array arr)
         af_dtype type = info.getType();
         switch(type)
         {
-        case f32:   print<float>(arr);    break;
-        case c32:   print<cfloat>(arr);   break;
-        case f64:   print<double>(arr);   break;
-        case c64:   print<cdouble>(arr);  break;
-        case b8:    print<char>(arr);     break;
-        case s32:   print<int>(arr);      break;
-        case u32:   print<unsigned>(arr); break;
-        case u8:    print<uchar>(arr);    break;
-        case s64:   print<intl>(arr);     break;
-        case u64:   print<uintl>(arr);    break;
+        case f32:   print<float>   (arr, 4);   break;
+        case c32:   print<cfloat>  (arr, 4);   break;
+        case f64:   print<double>  (arr, 4);   break;
+        case c64:   print<cdouble> (arr, 4);   break;
+        case b8:    print<char>    (arr, 4);   break;
+        case s32:   print<int>     (arr, 4);   break;
+        case u32:   print<unsigned>(arr, 4);   break;
+        case u8:    print<uchar>   (arr, 4);   break;
+        case s64:   print<intl>    (arr, 4);   break;
+        case u64:   print<uintl>   (arr, 4);   break;
+        default:    TYPE_ERROR(1, type);
+        }
+    }
+    CATCHALL;
+    return AF_SUCCESS;
+}
+
+af_err af_print_array_p(af_array arr, const int precision)
+{
+    try {
+        ArrayInfo info = getInfo(arr);
+        af_dtype type = info.getType();
+        switch(type)
+        {
+        case f32:   print<float>   (arr, precision);   break;
+        case c32:   print<cfloat>  (arr, precision);   break;
+        case f64:   print<double>  (arr, precision);   break;
+        case c64:   print<cdouble> (arr, precision);   break;
+        case b8:    print<char>    (arr, precision);   break;
+        case s32:   print<int>     (arr, precision);   break;
+        case u32:   print<unsigned>(arr, precision);   break;
+        case u8:    print<uchar>   (arr, precision);   break;
+        case s64:   print<intl>    (arr, precision);   break;
+        case u64:   print<uintl>   (arr, precision);   break;
         default:    TYPE_ERROR(1, type);
         }
     }
diff --git a/src/api/cpp/util.cpp b/src/api/cpp/util.cpp
index 3705f50..f4e9ff6 100644
--- a/src/api/cpp/util.cpp
+++ b/src/api/cpp/util.cpp
@@ -22,4 +22,11 @@ namespace af
         AF_THROW(af_print_array(arr.get()));
         return;
     }
+
+    void print(const char *exp, const array &arr, const int precision)
+    {
+        printf("%s ", exp);
+        AF_THROW(af_print_array_p(arr.get(), precision));
+        return;
+    }
 }

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/arrayfire.git



More information about the debian-science-commits mailing list