# HG changeset patch # User Jaroslav Hajek # Date 1228477908 -3600 # Node ID c43481a19bfe496accc54fd2f57c7dd0993acbe6 # Parent e3c9102431a9c50c04622e87deb34d8af98600ba implement ASCII saving of diag & perm matrices diff --git a/src/ChangeLog b/src/ChangeLog --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,11 @@ +2008-12-05 Jaroslav Hajek + + * 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 * strfns.cc (Fchar): Add test from str2mat.m diff --git a/src/ov-base-diag.cc b/src/ov-base-diag.cc --- a/src/ov-base-diag.cc +++ b/src/ov-base-diag.cc @@ -36,6 +36,8 @@ #include "oct-stream.h" #include "ops.h" +#include "ls-oct-ascii.h" + template octave_value octave_base_diag::subsref (const std::string& type, @@ -355,32 +357,59 @@ bool octave_base_diag::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 bool -octave_base_diag::save_binary (std::ostream& os, bool& save_as_floats) +octave_base_diag::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 -bool -octave_base_diag::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 (MArray (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 void octave_base_diag::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::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 int @@ -419,7 +449,7 @@ template void octave_base_diag::print_info (std::ostream& os, - const std::string& prefix) const + const std::string& prefix) const { matrix.print_info (os, prefix); } diff --git a/src/ov-base-diag.h b/src/ov-base-diag.h --- a/src/ov-base-diag.h +++ b/src/ov-base-diag.h @@ -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, diff --git a/src/ov-perm.cc b/src/ov-perm.cc --- a/src/ov-perm.cc +++ b/src/ov-perm.cc @@ -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& 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 idx_int_type; + + os << "# size: " << matrix.rows () << "\n"; + os << "# orient: " << (matrix.is_col_perm () ? 'c' : 'r') << '\n'; + + Array 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 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 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 diff --git a/src/ov-perm.h b/src/ov-perm.h --- a/src/ov-perm.h +++ b/src/ov-perm.h @@ -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,