changeset 22480:9263b2889003

Change mxGetScalar to return the first non-zero element of sparse array. * mex.cc (mxArray_octave_value::get_scalar): Check is_sparse_type() and if found, get a pointer to the actual data and cast to the correct type.
author Rik <rik@octave.org>
date Wed, 14 Sep 2016 10:40:17 -0700
parents eb0146564805
children ea8d53084b06 ca03f89e9f2a
files libinterp/corefcn/mex.cc
diffstat 1 files changed, 19 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/mex.cc	Tue Sep 13 19:45:44 2016 +0200
+++ b/libinterp/corefcn/mex.cc	Wed Sep 14 10:40:17 2016 -0700
@@ -378,8 +378,25 @@
   // Not allowed.
   void set_cell (mwIndex /*idx*/, mxArray * /*val*/) { request_mutation (); }
 
-  // FIXME: For sparse arrays, this should return the first non-zero value.
-  double get_scalar (void) const { return val.scalar_value (true); }
+  double get_scalar (void) const
+  {
+    if (val.is_sparse_type ())
+      {
+        // For sparse arrays, return the first non-zero value. 
+        void * data = val.mex_get_data (); 
+        if (data == NULL)
+          return 0.0;
+
+        if (val.is_bool_type ())
+          return *static_cast<bool *> (data);
+        else if (val.is_real_type ())
+          return *static_cast<double *> (data);
+        else  // Complex type, only return real part
+          return *static_cast<double *> (data);
+      }
+    else
+      return val.scalar_value (true);
+  }
 
   void *get_data (void) const
   {
@@ -2834,8 +2851,6 @@
   return static_cast<double *> (ptr->get_imag_data ());
 }
 
-// FIXME: For sparse arrays, mxGetScalar should return the first non-zero
-// element, rather than just the first element.
 double
 mxGetScalar (const mxArray *ptr)
 {