changeset 1620:ded0dddd03f5 octave-forge

saving to the octave -binary format
author aadler
date Mon, 02 Aug 2004 16:35:41 +0000
parents 10b9a5816c31
children 8e73e59477ef
files main/sparse/buildtests.sh main/sparse/complex_sparse_ops.cc main/sparse/sparse_ops.cc
diffstat 3 files changed, 122 insertions(+), 79 deletions(-) [+]
line wrap: on
line diff
--- a/main/sparse/buildtests.sh	Mon Aug 02 16:00:07 2004 +0000
+++ b/main/sparse/buildtests.sh	Mon Aug 02 16:35:41 2004 +0000
@@ -455,13 +455,20 @@
 
 function gen_save_tests {
     cat >>$TESTS <<EOF
-%!test
+%!test # save ascii
 %! savefile= tmpnam();
 %! mark_for_deletion( savefile );
-%! save(savefile,'bf','as','af');
-%! clear as;
-%! load(savefile,'as');
-%!assert(as,af);
+%! as_save=as; save(savefile,'bf','as_save','af');
+%! clear as_save;
+%! load(savefile,'as_save');
+%! assert(as_save,af);
+%!test # save binary
+%! savefile= tmpnam();
+%! mark_for_deletion( savefile );
+%! as_save=as; save('-binary',savefile,'bf','as_save','af');
+%! clear as_save;
+%! load(savefile,'as_save');
+%! assert(as_save,af);
 EOF
 }
 
--- a/main/sparse/complex_sparse_ops.cc	Mon Aug 02 16:00:07 2004 +0000
+++ b/main/sparse/complex_sparse_ops.cc	Mon Aug 02 16:35:41 2004 +0000
@@ -571,14 +571,71 @@
 bool 
 octave_complex_sparse::save_binary (std::ostream& os, bool& save_as_floats)
 {
-  return false;
+  DEFINE_SP_POINTERS_CPLX( X )
+
+  FOUR_BYTE_INT itmp;
+  // Use negative value for ndims to be consistent with other formats
+  itmp= -2;        os.write (X_CAST (char *, &itmp), 4);
+  itmp= Xnr;       os.write (X_CAST (char *, &itmp), 4);
+  itmp= Xnc;       os.write (X_CAST (char *, &itmp), 4);
+  itmp= NCFX->nnz; os.write (X_CAST (char *, &itmp), 4);
+
+  // add one to the printed indices to go from
+  //  zero-based to one-based arrays
+   for (int j=0; j< Xnc; j++)  {
+      OCTAVE_QUIT;
+      for (int i= cidxX[j]; i< cidxX[j+1]; i++) {
+         itmp= ridxX[i]+1; os.write (X_CAST (char *, &itmp), 4);
+         itmp= j+1;        os.write (X_CAST (char *, &itmp), 4);
+
+	 // TODO: how to manage save_as_floats?
+         os.write (X_CAST (char *, &coefX[i]), 16);
+      }
+   }
+  return true;
 }
 
 bool 
 octave_complex_sparse::load_binary (std::istream& is, bool swap,
 				 oct_mach_info::float_format fmt)
 {
-  return false;
+  FOUR_BYTE_INT nnz, cols, rows, tmp;
+  if (! is.read (X_CAST (char *, &tmp), 4))
+    return false;
+
+  if (tmp != -2) {
+    error("load: only 2D sparse matrices are supported");
+    return false;
+  }
+
+  if (! is.read (X_CAST (char *, &rows), 4))
+    return false;
+  if (! is.read (X_CAST (char *, &cols), 4))
+    return false;
+  if (! is.read (X_CAST (char *, &nnz), 4))
+    return false;
+
+  ColumnVector        ridxA( nnz ), cidxA( nnz );
+  ComplexColumnVector coefA( nnz );
+  for( int i=0; i<nnz; i++) {
+     FOUR_BYTE_INT cidx, ridx;
+     if (! is.read (X_CAST (char *, &ridx), 4))
+       return false;
+     ridxA(i)= ridx;
+
+     if (! is.read (X_CAST (char *, &cidx), 4))
+       return false;
+     cidxA(i)= cidx;
+
+     Complex coef;
+     if (! is.read (X_CAST (char *, &coef), 16))
+       return false;
+     coefA(i)= coef;
+  }
+
+  X= assemble_sparse( cols, rows, coefA, ridxA, cidxA, 0);
+
+  return true;
 }
 #endif
 
@@ -1677,6 +1734,9 @@
 
 /*
  * $Log$
+ * Revision 1.29  2004/08/02 16:35:40  aadler
+ * saving to the octave -binary format
+ *
  * Revision 1.28  2004/08/02 15:46:33  aadler
  * tests for sparse saving
  *
--- a/main/sparse/sparse_ops.cc	Mon Aug 02 16:00:07 2004 +0000
+++ b/main/sparse/sparse_ops.cc	Mon Aug 02 16:35:41 2004 +0000
@@ -533,97 +533,70 @@
 bool 
 octave_sparse::save_binary (std::ostream& os, bool& save_as_floats)
 {
-  return true;
-#if 0
-  char tmp = m ();
-  os.write (X_CAST (char *, &tmp), 1);
-  FOUR_BYTE_INT itmp = primpoly ();
-  os.write (X_CAST (char *, &itmp), 4);
+  DEFINE_SP_POINTERS_REAL( X )
 
-  dim_vector d = dims ();
-
-  // Don't handle N-D arrays yet
-  if (d.length() != 2)
-    return false;
-
+  FOUR_BYTE_INT itmp;
   // Use negative value for ndims to be consistent with other formats
-  itmp = - d.length();
-  os.write (X_CAST (char *, &itmp), 4);
-  for (int i=0; i < d.length (); i++)
-    {
-      itmp = d(i);
-      os.write (X_CAST (char *, &itmp), 4);
-    }
+  itmp= -2;        os.write (X_CAST (char *, &itmp), 4);
+  itmp= Xnr;       os.write (X_CAST (char *, &itmp), 4);
+  itmp= Xnc;       os.write (X_CAST (char *, &itmp), 4);
+  itmp= NCFX->nnz; os.write (X_CAST (char *, &itmp), 4);
 
-  Matrix m = matrix_value ();
-  save_type st;
-  if (tmp < 8)
-    st = LS_U_CHAR;
-  else if (tmp < 16)
-    st = LS_U_SHORT;
-  else
-    st = LS_U_INT;
-  const double *mtmp = m.data ();
-  write_doubles (os, mtmp, st, d.numel ());
+  // add one to the printed indices to go from
+  //  zero-based to one-based arrays
+   for (int j=0; j< Xnc; j++)  {
+      OCTAVE_QUIT;
+      for (int i= cidxX[j]; i< cidxX[j+1]; i++) {
+         itmp= ridxX[i]+1; os.write (X_CAST (char *, &itmp), 4);
+         itmp= j+1;        os.write (X_CAST (char *, &itmp), 4);
 
+	 // TODO: how to manage save_as_floats?
+         os.write (X_CAST (char *, &coefX[i]), 8);
+      }
+   }
   return true;
-#endif
 }
 
 bool 
 octave_sparse::load_binary (std::istream& is, bool swap,
 				 oct_mach_info::float_format fmt)
 {
-  return true;
-#if 0
-  char mord;
-  FOUR_BYTE_INT prim, mdims;
+  FOUR_BYTE_INT nnz, cols, rows, tmp;
+  if (! is.read (X_CAST (char *, &tmp), 4))
+    return false;
 
-  if (! is.read (X_CAST (char *, &mord), 1))
+  if (tmp != -2) {
+    error("load: only 2D sparse matrices are supported");
+    return false;
+  }
+
+  if (! is.read (X_CAST (char *, &rows), 4))
+    return false;
+  if (! is.read (X_CAST (char *, &cols), 4))
+    return false;
+  if (! is.read (X_CAST (char *, &nnz), 4))
     return false;
 
-  if (! is.read (X_CAST (char *, &prim), 4))
-    return false;
-  if (swap)
-    swap_4_bytes (X_CAST (char *, &prim));
-
-  if (! is.read (X_CAST (char *, &mdims), 4))
-    return false;
-  if (swap)
-    swap_4_bytes (X_CAST (char *, &mdims));
-
-  // Don't treat N-D arrays yet
-  if (mdims == -2)
-    {
-      mdims = - mdims;
-      FOUR_BYTE_INT di;
-      dim_vector dv;
-      dv.resize (mdims);
+  ColumnVector ridxA( nnz ), cidxA( nnz ), coefA( nnz );
+  for( int i=0; i<nnz; i++) {
+     FOUR_BYTE_INT cidx, ridx;
+     if (! is.read (X_CAST (char *, &ridx), 4))
+       return false;
+     ridxA(i)= ridx;
 
-      for (int i = 0; i < mdims; i++)
-	{
-	  if (! is.read (X_CAST (char *, &di), 4))
-	    return false;
-	  if (swap)
-	    swap_4_bytes (X_CAST (char *, &di));
-	  dv(i) = di;
-	}
+     if (! is.read (X_CAST (char *, &cidx), 4))
+       return false;
+     cidxA(i)= cidx;
 
-      char tmp;
-      if (! is.read (X_CAST (char *, &tmp), 1))
-	return false;
+     double coef;
+     if (! is.read (X_CAST (char *, &coef), 8))
+       return false;
+     coefA(i)= coef;
+  }
 
-      Matrix m (dv(0), dv(1));
-      double *re = m.fortran_vec ();
-      read_doubles (is, re, X_CAST (save_type, tmp), dv.numel (), swap, fmt);
-      if (error_state || ! is)
-	return false;
-
-      gval = galois (m, mord, prim);
-    }
+  X= assemble_sparse( cols, rows, coefA, ridxA, cidxA, 0);
 
   return true;
-#endif
 }
 #endif
 
@@ -1482,6 +1455,9 @@
 
 /*
  * $Log$
+ * Revision 1.22  2004/08/02 16:35:41  aadler
+ * saving to the octave -binary format
+ *
  * Revision 1.21  2004/08/02 15:46:33  aadler
  * tests for sparse saving
  *