changeset 8376:c43481a19bfe

implement ASCII saving of diag & perm matrices
author Jaroslav Hajek <highegg@gmail.com>
date Fri, 05 Dec 2008 12:51:48 +0100
parents e3c9102431a9
children 25bc2d31e1bf
files src/ChangeLog src/ov-base-diag.cc src/ov-base-diag.h src/ov-perm.cc src/ov-perm.h
diffstat 5 files changed, 104 insertions(+), 39 deletions(-) [+]
line wrap: on
line diff
--- a/src/ChangeLog	Fri Dec 05 10:20:18 2008 +0100
+++ b/src/ChangeLog	Fri Dec 05 12:51:48 2008 +0100
@@ -1,3 +1,11 @@
+2008-12-05  Jaroslav Hajek  <highegg@gmail.com>
+
+	* ov-base-diag.cc (save_ascii, load_ascii): Save natively.
+	* ov-base-diag.h (load_ascii): Declare.
+	* ov-perm.cc (save_ascii, load_ascii): Save natively.
+	* ov-perm.h (load_ascii): Declare.
+
+
 2008-12-04  Thorsten Meyer  <thorsten.meyier@gmx.de>
 
         * strfns.cc (Fchar): Add test from str2mat.m
--- a/src/ov-base-diag.cc	Fri Dec 05 10:20:18 2008 +0100
+++ b/src/ov-base-diag.cc	Fri Dec 05 12:51:48 2008 +0100
@@ -36,6 +36,8 @@
 #include "oct-stream.h"
 #include "ops.h"
 
+#include "ls-oct-ascii.h"
+
 template <class DMT, class MT>
 octave_value
 octave_base_diag<DMT, MT>::subsref (const std::string& type,
@@ -355,32 +357,59 @@
 bool 
 octave_base_diag<DMT, MT>::save_ascii (std::ostream& os)
 {
-  // FIXME: this should probably save the matrix as diagonal.
-  return to_dense ().save_ascii (os);
+  os << "# rows: " << matrix.rows () << "\n"
+    << "# columns: " << matrix.columns () << "\n";
+
+  os << matrix.diag ();
+
+  return true;
 }
 
 template <class DMT, class MT>
 bool 
-octave_base_diag<DMT, MT>::save_binary (std::ostream& os, bool& save_as_floats)
+octave_base_diag<DMT, MT>::load_ascii (std::istream& is)
 {
-  return to_dense ().save_binary (os, save_as_floats);
-}
+  octave_idx_type r = 0, c = 0;
+  bool success = true;
 
-#if defined (HAVE_HDF5)
+  if (extract_keyword (is, "rows", r, true)
+      && extract_keyword (is, "columns", c, true))
+    {
+      octave_idx_type l = r < c ? r : c;
+      MT tmp (l, 1);
+      is >> tmp;
 
-template <class DMT, class MT>
-bool
-octave_base_diag<DMT, MT>::save_hdf5 (hid_t loc_id, const char *name, bool save_as_floats)
-{
-  return to_dense ().save_hdf5 (loc_id, name, save_as_floats);
+      if (!is) 
+	{
+	  error ("load: failed to load diagonal matrix constant");
+	  success = false;
+	}
+      else
+        {
+          // This is a little tricky, as we have the Matrix type, but
+          // not ColumnVector type. We need to help the compiler get
+          // through the inheritance tree.
+          typedef typename DMT::element_type el_type;
+          matrix = DMT (MDiagArray2<el_type> (MArray<el_type> (tmp)));
+          matrix.resize (r, c);
+
+          // Invalidate cache. Probably not necessary, but safe.
+          dense_cache = octave_value ();
+        }
+    }
+  else
+    {
+      error ("load: failed to extract number of rows and columns");
+      success = false;
+    }
+
+  return success;
 }
 
-#endif
-
 template <class DMT, class MT>
 void
 octave_base_diag<DMT, MT>::print_raw (std::ostream& os,
-			  bool pr_as_read_syntax) const
+                                      bool pr_as_read_syntax) const
 {
   return to_dense ().print_raw (os, pr_as_read_syntax);
 }
@@ -405,7 +434,8 @@
 void
 octave_base_diag<DMT, MT>::print (std::ostream& os, bool pr_as_read_syntax) const
 {
-  to_dense ().print (os, pr_as_read_syntax);
+  print_raw (os, pr_as_read_syntax);
+  newline (os);
 }
 template <class DMT, class MT>
 int
@@ -419,7 +449,7 @@
 template <class DMT, class MT>
 void
 octave_base_diag<DMT, MT>::print_info (std::ostream& os,
-				    const std::string& prefix) const
+                                       const std::string& prefix) const
 {
   matrix.print_info (os, prefix);
 }
--- a/src/ov-base-diag.h	Fri Dec 05 10:20:18 2008 +0100
+++ b/src/ov-base-diag.h	Fri Dec 05 12:51:48 2008 +0100
@@ -189,11 +189,7 @@
 
   bool save_ascii (std::ostream& os);
 
-  bool save_binary (std::ostream& os, bool& save_as_floats);
-
-#if defined (HAVE_HDF5)
-  bool save_hdf5 (hid_t loc_id, const char *name, bool save_as_floats);
-#endif
+  bool load_ascii (std::istream& is);
 
   int write (octave_stream& os, int block_size,
 	     oct_data_conv::data_type output_type, int skip,
--- a/src/ov-perm.cc	Fri Dec 05 10:20:18 2008 +0100
+++ b/src/ov-perm.cc	Fri Dec 05 12:51:48 2008 +0100
@@ -32,6 +32,8 @@
 #include "gripes.h"
 #include "ops.h"
 
+#include "ls-oct-ascii.h"
+
 octave_value
 octave_perm_matrix::subsref (const std::string& type,
                              const std::list<octave_value_list>& idx)
@@ -239,26 +241,59 @@
 bool 
 octave_perm_matrix::save_ascii (std::ostream& os)
 {
-  // FIXME: this should probably save the matrix as permutation.
-  return to_dense ().save_ascii (os);
+  typedef octave_int<octave_idx_type> idx_int_type;
+
+  os << "# size: " << matrix.rows () << "\n";
+  os << "# orient: " << (matrix.is_col_perm () ? 'c' : 'r') << '\n';
+
+  Array<octave_idx_type> pvec = matrix.pvec ();
+  octave_idx_type n = pvec.length ();
+  ColumnVector tmp (n);
+  for (octave_idx_type i = 0; i < n; i++) tmp(i) = pvec(i) + 1;
+  os << tmp;
+
+  return true;
 }
 
 bool 
-octave_perm_matrix::save_binary (std::ostream& os, bool& save_as_floats)
+octave_perm_matrix::load_ascii (std::istream& is)
 {
-  return to_dense ().save_binary (os, save_as_floats);
-}
-
-#if defined (HAVE_HDF5)
+  typedef octave_int<octave_idx_type> idx_int_type;
+  octave_idx_type n;
+  bool success = true;
+  char orient;
 
-bool
-octave_perm_matrix::save_hdf5 (hid_t loc_id, const char *name, bool save_as_floats)
-{
-  return to_dense ().save_hdf5 (loc_id, name, save_as_floats);
+  if (extract_keyword (is, "size", n, true)
+      && extract_keyword (is, "orient", orient, true))
+    {
+      bool colp = orient == 'c';
+      dim_vector dv (n);
+      ColumnVector tmp (n);
+      is >> tmp;
+      if (!is) 
+	{
+	  error ("load: failed to load permutation matrix constant");
+	  success = false;
+	}
+      else
+        {
+          Array<octave_idx_type> pvec (n);
+          for (octave_idx_type i = 0; i < n; i++) pvec(i) = tmp(i) - 1;
+          matrix = PermMatrix (pvec, colp);
+
+          // Invalidate cache. Probably not necessary, but safe.
+          dense_cache = octave_value ();
+        }
+    }
+  else
+    {
+      error ("load: failed to extract size & orientation");
+      success = false;
+    }
+
+  return success;
 }
 
-#endif
-
 void
 octave_perm_matrix::print_raw (std::ostream& os,
 			  bool pr_as_read_syntax) const
--- a/src/ov-perm.h	Fri Dec 05 10:20:18 2008 +0100
+++ b/src/ov-perm.h	Fri Dec 05 12:51:48 2008 +0100
@@ -183,11 +183,7 @@
 
   bool save_ascii (std::ostream& os);
 
-  bool save_binary (std::ostream& os, bool& save_as_floats);
-
-#if defined (HAVE_HDF5)
-  bool save_hdf5 (hid_t loc_id, const char *name, bool save_as_floats);
-#endif
+  bool load_ascii (std::istream& is);
 
   int write (octave_stream& os, int block_size,
 	     oct_data_conv::data_type output_type, int skip,