diff src/ov-perm.cc @ 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 445d27d79f4e
children ad896677a2e2
line wrap: on
line diff
--- 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