Mercurial > hg > octave-lyh
view src/octave-value/ov-cx-sparse.cc @ 15057:46b19589b593
maint: Partition src/ directory with more code in subdirs.
Create new octave-value dir for ov* code.
Create new parse-tre dir for pt* code.
Move OPERATORS and TEMPLATE-INST directories to lowercase names
* octave-value/module.mk: Hook code in octave-value dir into build system.
* octave-value/ov-base-diag.cc,
octave-value/ov-base-diag.h, octave-value/ov-base-int.cc,
octave-value/ov-base-int.h, octave-value/ov-base-mat.cc,
octave-value/ov-base-mat.h, octave-value/ov-base-scalar.cc,
octave-value/ov-base-scalar.h, octave-value/ov-base-sparse.cc,
octave-value/ov-base-sparse.h, octave-value/ov-base.cc, octave-value/ov-base.h,
octave-value/ov-bool-mat.cc, octave-value/ov-bool-mat.h,
octave-value/ov-bool-sparse.cc, octave-value/ov-bool-sparse.h,
octave-value/ov-bool.cc, octave-value/ov-bool.h, octave-value/ov-builtin.cc,
octave-value/ov-builtin.h, octave-value/ov-cell.cc, octave-value/ov-cell.h,
octave-value/ov-ch-mat.cc, octave-value/ov-ch-mat.h, octave-value/ov-class.cc,
octave-value/ov-class.h, octave-value/ov-colon.cc, octave-value/ov-colon.h,
octave-value/ov-complex.cc, octave-value/ov-complex.h,
octave-value/ov-cs-list.cc, octave-value/ov-cs-list.h,
octave-value/ov-cx-diag.cc, octave-value/ov-cx-diag.h,
octave-value/ov-cx-mat.cc, octave-value/ov-cx-mat.h,
octave-value/ov-cx-sparse.cc, octave-value/ov-cx-sparse.h,
octave-value/ov-dld-fcn.cc, octave-value/ov-dld-fcn.h,
octave-value/ov-fcn-handle.cc, octave-value/ov-fcn-handle.h,
octave-value/ov-fcn-inline.cc, octave-value/ov-fcn-inline.h,
octave-value/ov-fcn.cc, octave-value/ov-fcn.h, octave-value/ov-float.cc,
octave-value/ov-float.h, octave-value/ov-flt-complex.cc,
octave-value/ov-flt-complex.h, octave-value/ov-flt-cx-diag.cc,
octave-value/ov-flt-cx-diag.h, octave-value/ov-flt-cx-mat.cc,
octave-value/ov-flt-cx-mat.h, octave-value/ov-flt-re-diag.cc,
octave-value/ov-flt-re-diag.h, octave-value/ov-flt-re-mat.cc,
octave-value/ov-flt-re-mat.h, octave-value/ov-int-traits.h,
octave-value/ov-int16.cc, octave-value/ov-int16.h, octave-value/ov-int32.cc,
octave-value/ov-int32.h, octave-value/ov-int64.cc, octave-value/ov-int64.h,
octave-value/ov-int8.cc, octave-value/ov-int8.h, octave-value/ov-intx.h,
octave-value/ov-lazy-idx.cc, octave-value/ov-lazy-idx.h,
octave-value/ov-mex-fcn.cc, octave-value/ov-mex-fcn.h,
octave-value/ov-null-mat.cc, octave-value/ov-null-mat.h,
octave-value/ov-oncleanup.cc, octave-value/ov-oncleanup.h,
octave-value/ov-perm.cc, octave-value/ov-perm.h, octave-value/ov-range.cc,
octave-value/ov-range.h, octave-value/ov-re-diag.cc, octave-value/ov-re-diag.h,
octave-value/ov-re-mat.cc, octave-value/ov-re-mat.h,
octave-value/ov-re-sparse.cc, octave-value/ov-re-sparse.h,
octave-value/ov-scalar.cc, octave-value/ov-scalar.h,
octave-value/ov-str-mat.cc, octave-value/ov-str-mat.h,
octave-value/ov-struct.cc, octave-value/ov-struct.h,
octave-value/ov-type-conv.h, octave-value/ov-typeinfo.cc,
octave-value/ov-typeinfo.h, octave-value/ov-uint16.cc,
octave-value/ov-uint16.h, octave-value/ov-uint32.cc, octave-value/ov-uint32.h,
octave-value/ov-uint64.cc, octave-value/ov-uint64.h, octave-value/ov-uint8.cc,
octave-value/ov-uint8.h, octave-value/ov-usr-fcn.cc, octave-value/ov-usr-fcn.h,
octave-value/ov.cc, octave-value/ov.h: Moved from src/ dir to octave-value dir.
* operators/module.mk, operators/op-b-b.cc, operators/op-b-bm.cc,
operators/op-b-sbm.cc, operators/op-bm-b.cc, operators/op-bm-bm.cc,
operators/op-bm-sbm.cc, operators/op-cdm-cdm.cc, operators/op-cdm-cm.cc,
operators/op-cdm-cs.cc, operators/op-cdm-dm.cc, operators/op-cdm-m.cc,
operators/op-cdm-s.cc, operators/op-cell.cc, operators/op-chm.cc,
operators/op-class.cc, operators/op-cm-cdm.cc, operators/op-cm-cm.cc,
operators/op-cm-cs.cc, operators/op-cm-dm.cc, operators/op-cm-m.cc,
operators/op-cm-pm.cc, operators/op-cm-s.cc, operators/op-cm-scm.cc,
operators/op-cm-sm.cc, operators/op-cs-cm.cc, operators/op-cs-cs.cc,
operators/op-cs-m.cc, operators/op-cs-s.cc, operators/op-cs-scm.cc,
operators/op-cs-sm.cc, operators/op-dm-cdm.cc, operators/op-dm-cm.cc,
operators/op-dm-cs.cc, operators/op-dm-dm.cc, operators/op-dm-m.cc,
operators/op-dm-s.cc, operators/op-dm-scm.cc, operators/op-dm-sm.cc,
operators/op-dm-template.cc, operators/op-dms-template.cc,
operators/op-double-conv.cc, operators/op-fcdm-fcdm.cc,
operators/op-fcdm-fcm.cc, operators/op-fcdm-fcs.cc, operators/op-fcdm-fdm.cc,
operators/op-fcdm-fm.cc, operators/op-fcdm-fs.cc, operators/op-fcm-fcdm.cc,
operators/op-fcm-fcm.cc, operators/op-fcm-fcs.cc, operators/op-fcm-fdm.cc,
operators/op-fcm-fm.cc, operators/op-fcm-fs.cc, operators/op-fcm-pm.cc,
operators/op-fcn.cc, operators/op-fcs-fcm.cc, operators/op-fcs-fcs.cc,
operators/op-fcs-fm.cc, operators/op-fcs-fs.cc, operators/op-fdm-fcdm.cc,
operators/op-fdm-fcm.cc, operators/op-fdm-fcs.cc, operators/op-fdm-fdm.cc,
operators/op-fdm-fm.cc, operators/op-fdm-fs.cc, operators/op-float-conv.cc,
operators/op-fm-fcdm.cc, operators/op-fm-fcm.cc, operators/op-fm-fcs.cc,
operators/op-fm-fdm.cc, operators/op-fm-fm.cc, operators/op-fm-fs.cc,
operators/op-fm-pm.cc, operators/op-fs-fcm.cc, operators/op-fs-fcs.cc,
operators/op-fs-fm.cc, operators/op-fs-fs.cc, operators/op-i16-i16.cc,
operators/op-i32-i32.cc, operators/op-i64-i64.cc, operators/op-i8-i8.cc,
operators/op-int-concat.cc, operators/op-int-conv.cc, operators/op-int.h,
operators/op-m-cdm.cc, operators/op-m-cm.cc, operators/op-m-cs.cc,
operators/op-m-dm.cc, operators/op-m-m.cc, operators/op-m-pm.cc,
operators/op-m-s.cc, operators/op-m-scm.cc, operators/op-m-sm.cc,
operators/op-pm-cm.cc, operators/op-pm-fcm.cc, operators/op-pm-fm.cc,
operators/op-pm-m.cc, operators/op-pm-pm.cc, operators/op-pm-scm.cc,
operators/op-pm-sm.cc, operators/op-pm-template.cc, operators/op-range.cc,
operators/op-s-cm.cc, operators/op-s-cs.cc, operators/op-s-m.cc,
operators/op-s-s.cc, operators/op-s-scm.cc, operators/op-s-sm.cc,
operators/op-sbm-b.cc, operators/op-sbm-bm.cc, operators/op-sbm-sbm.cc,
operators/op-scm-cm.cc, operators/op-scm-cs.cc, operators/op-scm-m.cc,
operators/op-scm-s.cc, operators/op-scm-scm.cc, operators/op-scm-sm.cc,
operators/op-sm-cm.cc, operators/op-sm-cs.cc, operators/op-sm-m.cc,
operators/op-sm-s.cc, operators/op-sm-scm.cc, operators/op-sm-sm.cc,
operators/op-str-m.cc, operators/op-str-s.cc, operators/op-str-str.cc,
operators/op-struct.cc, operators/op-ui16-ui16.cc, operators/op-ui32-ui32.cc,
operators/op-ui64-ui64.cc, operators/op-ui8-ui8.cc: Moved from OPERATORS/
dir to operators/ directory.
* mkops: Correctly print comment in generated file ops.cc that it is made by
mkops. Change sed expression for OPERATORS/ to operators/.
* parse-tree/module.mk: Hook code in parse-tree dir into build system.
* parse-tree/pt-all.h, parse-tree/pt-arg-list.cc, parse-tree/pt-arg-list.h,
parse-tree/pt-assign.cc, parse-tree/pt-assign.h, parse-tree/pt-binop.cc,
parse-tree/pt-binop.h, parse-tree/pt-bp.cc, parse-tree/pt-bp.h,
parse-tree/pt-cbinop.cc, parse-tree/pt-cbinop.h, parse-tree/pt-cell.cc,
parse-tree/pt-cell.h, parse-tree/pt-check.cc, parse-tree/pt-check.h,
parse-tree/pt-cmd.cc, parse-tree/pt-cmd.h, parse-tree/pt-colon.cc,
parse-tree/pt-colon.h, parse-tree/pt-const.cc, parse-tree/pt-const.h,
parse-tree/pt-decl.cc, parse-tree/pt-decl.h, parse-tree/pt-eval.cc,
parse-tree/pt-eval.h, parse-tree/pt-except.cc, parse-tree/pt-except.h,
parse-tree/pt-exp.cc, parse-tree/pt-exp.h, parse-tree/pt-fcn-handle.cc,
parse-tree/pt-fcn-handle.h, parse-tree/pt-id.cc, parse-tree/pt-id.h,
parse-tree/pt-idx.cc, parse-tree/pt-idx.h, parse-tree/pt-jump.cc,
parse-tree/pt-jump.h, parse-tree/pt-loop.cc, parse-tree/pt-loop.h,
parse-tree/pt-mat.cc, parse-tree/pt-mat.h, parse-tree/pt-misc.cc,
parse-tree/pt-misc.h, parse-tree/pt-pr-code.cc, parse-tree/pt-pr-code.h,
parse-tree/pt-select.cc, parse-tree/pt-select.h, parse-tree/pt-stmt.cc,
parse-tree/pt-stmt.h, parse-tree/pt-unop.cc, parse-tree/pt-unop.h,
parse-tree/pt-walk.h, parse-tree/pt.cc, parse-tree/pt.h:
Moved from src/ dir to parse-tree dir.
* template-inst/Array-jit.cc, template-inst/Array-os.cc,
template-inst/Array-sym.cc, template-inst/Array-tc.cc, template-inst/module.mk:
Moved from TEMPLATE-INST dir to template-inst/ directory.
* src/Makefile.am: Add new directories to build system.
* corefcn/module.mk: Use COREFCN_SRC with all capitals to indicate it is not an
Automake special target.
author | Rik <rik@octave.org> |
---|---|
date | Mon, 30 Jul 2012 15:29:19 -0700 |
parents | src/ov-cx-sparse.cc@f7afecdd87ef |
children | 62a35ae7d6a2 |
line wrap: on
line source
/* Copyright (C) 2004-2012 David Bateman Copyright (C) 1998-2004 Andy Adler This file is part of Octave. Octave is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. Octave is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with Octave; see the file COPYING. If not, see <http://www.gnu.org/licenses/>. */ #ifdef HAVE_CONFIG_H #include <config.h> #endif #include <climits> #include <iostream> #include <vector> #include "lo-specfun.h" #include "lo-mappers.h" #include "oct-locbuf.h" #include "ov-base.h" #include "ov-scalar.h" #include "ov-complex.h" #include "gripes.h" #include "ov-re-sparse.h" #include "ov-cx-sparse.h" #include "ov-base-sparse.h" #include "ov-base-sparse.cc" #include "ov-bool-sparse.h" template class OCTINTERP_API octave_base_sparse<SparseComplexMatrix>; DEFINE_OCTAVE_ALLOCATOR (octave_sparse_complex_matrix); DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_sparse_complex_matrix, "sparse complex matrix", "double"); octave_base_value * octave_sparse_complex_matrix::try_narrowing_conversion (void) { octave_base_value *retval = 0; if (Vsparse_auto_mutate) { int nr = matrix.rows (); int nc = matrix.cols (); // Don't use numel, since it can overflow for very large matrices // Note that for the tests on matrix size, they become approximative // since they involves a cast to double to avoid issues of overflow if (matrix.rows () == 1 && matrix.cols () == 1) { // Const copy of the matrix, so the right version of () operator used const SparseComplexMatrix tmp (matrix); Complex c = tmp (0, 0); if (std::imag (c) == 0.0) retval = new octave_scalar (std::real (c)); else retval = new octave_complex (c); } else if (nr == 0 || nc == 0) retval = new octave_matrix (Matrix (nr, nc)); else if (matrix.all_elements_are_real ()) if (matrix.cols () > 0 && matrix.rows () > 0 && (double (matrix.byte_size ()) > double (matrix.rows ()) * double (matrix.cols ()) * sizeof (double))) retval = new octave_matrix (::real (matrix.matrix_value ())); else retval = new octave_sparse_matrix (::real (matrix)); else if (matrix.cols () > 0 && matrix.rows () > 0 && (double (matrix.byte_size ()) > double (matrix.rows ()) * double (matrix.cols ()) * sizeof (Complex))) retval = new octave_complex_matrix (matrix.matrix_value ()); } else { if (matrix.all_elements_are_real ()) retval = new octave_sparse_matrix (::real (matrix)); } return retval; } double octave_sparse_complex_matrix::double_value (bool force_conversion) const { double retval = lo_ieee_nan_value (); if (! force_conversion) gripe_implicit_conversion ("Octave:imag-to-real", "complex sparse matrix", "real scalar"); // FIXME -- maybe this should be a function, valid_as_scalar() if (numel () > 0) { if (numel () > 1) gripe_implicit_conversion ("Octave:array-to-scalar", "complex sparse matrix", "real scalar"); retval = std::real (matrix (0, 0)); } else gripe_invalid_conversion ("complex sparse matrix", "real scalar"); return retval; } Matrix octave_sparse_complex_matrix::matrix_value (bool force_conversion) const { Matrix retval; if (! force_conversion) gripe_implicit_conversion ("Octave:imag-to-real", "complex sparse matrix", "real matrix"); retval = ::real (matrix.matrix_value ()); return retval; } Complex octave_sparse_complex_matrix::complex_value (bool) const { double tmp = lo_ieee_nan_value (); Complex retval (tmp, tmp); // FIXME -- maybe this should be a function, valid_as_scalar() if (numel () > 0) { if (numel () > 1) gripe_implicit_conversion ("Octave:array-to-scalar", "complex sparse matrix", "real scalar"); retval = matrix (0, 0); } else gripe_invalid_conversion ("complex sparse matrix", "real scalar"); return retval; } ComplexMatrix octave_sparse_complex_matrix::complex_matrix_value (bool) const { return matrix.matrix_value (); } ComplexNDArray octave_sparse_complex_matrix::complex_array_value (bool) const { return ComplexNDArray (matrix.matrix_value ()); } charNDArray octave_sparse_complex_matrix::char_array_value (bool frc_str_conv) const { charNDArray retval; if (! frc_str_conv) gripe_implicit_conversion ("Octave:num-to-str", "sparse complex matrix", "string"); else { retval = charNDArray (dims (), 0); octave_idx_type nc = matrix.cols (); octave_idx_type nr = matrix.rows (); for (octave_idx_type j = 0; j < nc; j++) for (octave_idx_type i = matrix.cidx (j); i < matrix.cidx (j+1); i++) retval(matrix.ridx (i) + nr * j) = static_cast<char>(std::real (matrix.data (i))); } return retval; } SparseMatrix octave_sparse_complex_matrix::sparse_matrix_value (bool force_conversion) const { SparseMatrix retval; if (! force_conversion) gripe_implicit_conversion ("Octave:imag-to-real", "complex sparse matrix", "real sparse matrix"); retval = ::real (matrix); return retval; } SparseBoolMatrix octave_sparse_complex_matrix::sparse_bool_matrix_value (bool warn) const { if (matrix.any_element_is_nan ()) gripe_nan_to_logical_conversion (); else if (warn && (! matrix.all_elements_are_real () || real (matrix).any_element_not_one_or_zero ())) gripe_logical_conversion (); return mx_el_ne (matrix, Complex (0.0)); } bool octave_sparse_complex_matrix::save_binary (std::ostream& os, bool&save_as_floats) { dim_vector d = this->dims (); if (d.length () < 1) return false; // Ensure that additional memory is deallocated matrix.maybe_compress (); int nr = d(0); int nc = d(1); int nz = nnz (); int32_t itmp; // Use negative value for ndims to be consistent with other formats itmp= -2; os.write (reinterpret_cast<char *> (&itmp), 4); itmp= nr; os.write (reinterpret_cast<char *> (&itmp), 4); itmp= nc; os.write (reinterpret_cast<char *> (&itmp), 4); itmp= nz; os.write (reinterpret_cast<char *> (&itmp), 4); save_type st = LS_DOUBLE; if (save_as_floats) { if (matrix.too_large_for_float ()) { warning ("save: some values too large to save as floats --"); warning ("save: saving as doubles instead"); } else st = LS_FLOAT; } else if (matrix.nnz () > 8192) // FIXME -- make this configurable. { double max_val, min_val; if (matrix.all_integers (max_val, min_val)) st = get_save_type (max_val, min_val); } // add one to the printed indices to go from // zero-based to one-based arrays for (int i = 0; i < nc+1; i++) { octave_quit (); itmp = matrix.cidx (i); os.write (reinterpret_cast<char *> (&itmp), 4); } for (int i = 0; i < nz; i++) { octave_quit (); itmp = matrix.ridx (i); os.write (reinterpret_cast<char *> (&itmp), 4); } write_doubles (os, reinterpret_cast<const double *> (matrix.data ()), st, 2 * nz); return true; } bool octave_sparse_complex_matrix::load_binary (std::istream& is, bool swap, oct_mach_info::float_format fmt) { int32_t nz, nc, nr, tmp; char ctmp; if (! is.read (reinterpret_cast<char *> (&tmp), 4)) return false; if (swap) swap_bytes<4> (&tmp); if (tmp != -2) { error ("load: only 2D sparse matrices are supported"); return false; } if (! is.read (reinterpret_cast<char *> (&nr), 4)) return false; if (! is.read (reinterpret_cast<char *> (&nc), 4)) return false; if (! is.read (reinterpret_cast<char *> (&nz), 4)) return false; if (swap) { swap_bytes<4> (&nr); swap_bytes<4> (&nc); swap_bytes<4> (&nz); } SparseComplexMatrix m (static_cast<octave_idx_type> (nr), static_cast<octave_idx_type> (nc), static_cast<octave_idx_type> (nz)); for (int i = 0; i < nc+1; i++) { octave_quit (); if (! is.read (reinterpret_cast<char *> (&tmp), 4)) return false; if (swap) swap_bytes<4> (&tmp); m.cidx (i) = tmp; } for (int i = 0; i < nz; i++) { octave_quit (); if (! is.read (reinterpret_cast<char *> (&tmp), 4)) return false; if (swap) swap_bytes<4> (&tmp); m.ridx (i) = tmp; } if (! is.read (reinterpret_cast<char *> (&ctmp), 1)) return false; read_doubles (is, reinterpret_cast<double *> (m.data ()), static_cast<save_type> (ctmp), 2 * nz, swap, fmt); if (error_state || ! is) return false; if (! m.indices_ok ()) return false; matrix = m; return true; } #if defined (HAVE_HDF5) bool octave_sparse_complex_matrix::save_hdf5 (hid_t loc_id, const char *name, bool save_as_floats) { dim_vector dv = dims (); int empty = save_hdf5_empty (loc_id, name, dv); if (empty) return (empty > 0); // Ensure that additional memory is deallocated matrix.maybe_compress (); #if HAVE_HDF5_18 hid_t group_hid = H5Gcreate (loc_id, name, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); #else hid_t group_hid = H5Gcreate (loc_id, name, 0); #endif if (group_hid < 0) return false; hid_t space_hid = -1, data_hid = -1; bool retval = true; SparseComplexMatrix m = sparse_complex_matrix_value (); octave_idx_type tmp; hsize_t hdims[2]; space_hid = H5Screate_simple (0, hdims, 0); if (space_hid < 0) { H5Gclose (group_hid); return false; } #if HAVE_HDF5_18 data_hid = H5Dcreate (group_hid, "nr", H5T_NATIVE_IDX, space_hid, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); #else data_hid = H5Dcreate (group_hid, "nr", H5T_NATIVE_IDX, space_hid, H5P_DEFAULT); #endif if (data_hid < 0) { H5Sclose (space_hid); H5Gclose (group_hid); return false; } tmp = m.rows (); retval = H5Dwrite (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL, H5P_DEFAULT, &tmp) >= 0; H5Dclose (data_hid); if (!retval) { H5Sclose (space_hid); H5Gclose (group_hid); return false; } #if HAVE_HDF5_18 data_hid = H5Dcreate (group_hid, "nc", H5T_NATIVE_IDX, space_hid, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); #else data_hid = H5Dcreate (group_hid, "nc", H5T_NATIVE_IDX, space_hid, H5P_DEFAULT); #endif if (data_hid < 0) { H5Sclose (space_hid); H5Gclose (group_hid); return false; } tmp = m.cols (); retval = H5Dwrite (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL, H5P_DEFAULT, &tmp) >= 0; H5Dclose (data_hid); if (!retval) { H5Sclose (space_hid); H5Gclose (group_hid); return false; } #if HAVE_HDF5_18 data_hid = H5Dcreate (group_hid, "nz", H5T_NATIVE_IDX, space_hid, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); #else data_hid = H5Dcreate (group_hid, "nz", H5T_NATIVE_IDX, space_hid, H5P_DEFAULT); #endif if (data_hid < 0) { H5Sclose (space_hid); H5Gclose (group_hid); return false; } tmp = m.nnz (); retval = H5Dwrite (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL, H5P_DEFAULT, &tmp) >= 0; H5Dclose (data_hid); if (!retval) { H5Sclose (space_hid); H5Gclose (group_hid); return false; } H5Sclose (space_hid); hdims[0] = m.cols () + 1; hdims[1] = 1; space_hid = H5Screate_simple (2, hdims, 0); if (space_hid < 0) { H5Gclose (group_hid); return false; } #if HAVE_HDF5_18 data_hid = H5Dcreate (group_hid, "cidx", H5T_NATIVE_IDX, space_hid, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); #else data_hid = H5Dcreate (group_hid, "cidx", H5T_NATIVE_IDX, space_hid, H5P_DEFAULT); #endif if (data_hid < 0) { H5Sclose (space_hid); H5Gclose (group_hid); return false; } octave_idx_type * itmp = m.xcidx (); retval = H5Dwrite (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL, H5P_DEFAULT, itmp) >= 0; H5Dclose (data_hid); if (!retval) { H5Sclose (space_hid); H5Gclose (group_hid); return false; } H5Sclose (space_hid); hdims[0] = m.nnz (); hdims[1] = 1; space_hid = H5Screate_simple (2, hdims, 0); if (space_hid < 0) { H5Gclose (group_hid); return false; } #if HAVE_HDF5_18 data_hid = H5Dcreate (group_hid, "ridx", H5T_NATIVE_IDX, space_hid, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); #else data_hid = H5Dcreate (group_hid, "ridx", H5T_NATIVE_IDX, space_hid, H5P_DEFAULT); #endif if (data_hid < 0) { H5Sclose (space_hid); H5Gclose (group_hid); return false; } itmp = m.xridx (); retval = H5Dwrite (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL, H5P_DEFAULT, itmp) >= 0; H5Dclose (data_hid); if (!retval) { H5Sclose (space_hid); H5Gclose (group_hid); return false; } hid_t save_type_hid = H5T_NATIVE_DOUBLE; if (save_as_floats) { if (m.too_large_for_float ()) { warning ("save: some values too large to save as floats --"); warning ("save: saving as doubles instead"); } else save_type_hid = H5T_NATIVE_FLOAT; } #if HAVE_HDF5_INT2FLOAT_CONVERSIONS // hdf5 currently doesn't support float/integer conversions else { double max_val, min_val; if (m.all_integers (max_val, min_val)) save_type_hid = save_type_to_hdf5 (get_save_type (max_val, min_val)); } #endif /* HAVE_HDF5_INT2FLOAT_CONVERSIONS */ hid_t type_hid = hdf5_make_complex_type (save_type_hid); if (type_hid < 0) { H5Sclose (space_hid); H5Gclose (group_hid); return false; } #if HAVE_HDF5_18 data_hid = H5Dcreate (group_hid, "data", type_hid, space_hid, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); #else data_hid = H5Dcreate (group_hid, "data", type_hid, space_hid, H5P_DEFAULT); #endif if (data_hid < 0) { H5Sclose (space_hid); H5Tclose (type_hid); H5Gclose (group_hid); return false; } hid_t complex_type_hid = hdf5_make_complex_type (H5T_NATIVE_DOUBLE); retval = false; if (complex_type_hid >= 0) { Complex * ctmp = m.xdata (); retval = H5Dwrite (data_hid, complex_type_hid, H5S_ALL, H5S_ALL, H5P_DEFAULT, ctmp) >= 0; } H5Dclose (data_hid); H5Sclose (space_hid); H5Tclose (type_hid); H5Gclose (group_hid); return retval; } bool octave_sparse_complex_matrix::load_hdf5 (hid_t loc_id, const char *name) { octave_idx_type nr, nc, nz; hid_t group_hid, data_hid, space_hid; hsize_t rank; dim_vector dv; int empty = load_hdf5_empty (loc_id, name, dv); if (empty > 0) matrix.resize (dv); if (empty) return (empty > 0); #if HAVE_HDF5_18 group_hid = H5Gopen (loc_id, name, H5P_DEFAULT); #else group_hid = H5Gopen (loc_id, name); #endif if (group_hid < 0 ) return false; #if HAVE_HDF5_18 data_hid = H5Dopen (group_hid, "nr", H5P_DEFAULT); #else data_hid = H5Dopen (group_hid, "nr"); #endif space_hid = H5Dget_space (data_hid); rank = H5Sget_simple_extent_ndims (space_hid); if (rank != 0) { H5Dclose (data_hid); H5Gclose (group_hid); return false; } if (H5Dread (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL, H5P_DEFAULT, &nr) < 0) { H5Dclose (data_hid); H5Gclose (group_hid); return false; } H5Dclose (data_hid); #if HAVE_HDF5_18 data_hid = H5Dopen (group_hid, "nc", H5P_DEFAULT); #else data_hid = H5Dopen (group_hid, "nc"); #endif space_hid = H5Dget_space (data_hid); rank = H5Sget_simple_extent_ndims (space_hid); if (rank != 0) { H5Dclose (data_hid); H5Gclose (group_hid); return false; } if (H5Dread (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL, H5P_DEFAULT, &nc) < 0) { H5Dclose (data_hid); H5Gclose (group_hid); return false; } H5Dclose (data_hid); #if HAVE_HDF5_18 data_hid = H5Dopen (group_hid, "nz", H5P_DEFAULT); #else data_hid = H5Dopen (group_hid, "nz"); #endif space_hid = H5Dget_space (data_hid); rank = H5Sget_simple_extent_ndims (space_hid); if (rank != 0) { H5Dclose (data_hid); H5Gclose (group_hid); return false; } if (H5Dread (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL, H5P_DEFAULT, &nz) < 0) { H5Dclose (data_hid); H5Gclose (group_hid); return false; } H5Dclose (data_hid); SparseComplexMatrix m (static_cast<octave_idx_type> (nr), static_cast<octave_idx_type> (nc), static_cast<octave_idx_type> (nz)); #if HAVE_HDF5_18 data_hid = H5Dopen (group_hid, "cidx", H5P_DEFAULT); #else data_hid = H5Dopen (group_hid, "cidx"); #endif space_hid = H5Dget_space (data_hid); rank = H5Sget_simple_extent_ndims (space_hid); if (rank != 2) { H5Sclose (space_hid); H5Dclose (data_hid); H5Gclose (group_hid); return false; } OCTAVE_LOCAL_BUFFER (hsize_t, hdims, rank); OCTAVE_LOCAL_BUFFER (hsize_t, maxdims, rank); H5Sget_simple_extent_dims (space_hid, hdims, maxdims); if (static_cast<int> (hdims[0]) != nc + 1 || static_cast<int> (hdims[1]) != 1) { H5Sclose (space_hid); H5Dclose (data_hid); H5Gclose (group_hid); return false; } octave_idx_type *itmp = m.xcidx (); if (H5Dread (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL, H5P_DEFAULT, itmp) < 0) { H5Sclose (space_hid); H5Dclose (data_hid); H5Gclose (group_hid); return false; } H5Sclose (space_hid); H5Dclose (data_hid); #if HAVE_HDF5_18 data_hid = H5Dopen (group_hid, "ridx", H5P_DEFAULT); #else data_hid = H5Dopen (group_hid, "ridx"); #endif space_hid = H5Dget_space (data_hid); rank = H5Sget_simple_extent_ndims (space_hid); if (rank != 2) { H5Sclose (space_hid); H5Dclose (data_hid); H5Gclose (group_hid); return false; } H5Sget_simple_extent_dims (space_hid, hdims, maxdims); if (static_cast<int> (hdims[0]) != nz || static_cast<int> (hdims[1]) != 1) { H5Sclose (space_hid); H5Dclose (data_hid); H5Gclose (group_hid); return false; } itmp = m.xridx (); if (H5Dread (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL, H5P_DEFAULT, itmp) < 0) { H5Sclose (space_hid); H5Dclose (data_hid); H5Gclose (group_hid); return false; } H5Sclose (space_hid); H5Dclose (data_hid); #if HAVE_HDF5_18 data_hid = H5Dopen (group_hid, "data", H5P_DEFAULT); #else data_hid = H5Dopen (group_hid, "data"); #endif hid_t type_hid = H5Dget_type (data_hid); hid_t complex_type = hdf5_make_complex_type (H5T_NATIVE_DOUBLE); if (! hdf5_types_compatible (type_hid, complex_type)) { H5Tclose (complex_type); H5Dclose (data_hid); H5Gclose (group_hid); return false; } space_hid = H5Dget_space (data_hid); rank = H5Sget_simple_extent_ndims (space_hid); if (rank != 2) { H5Sclose (space_hid); H5Dclose (data_hid); H5Gclose (group_hid); return false; } H5Sget_simple_extent_dims (space_hid, hdims, maxdims); if (static_cast<int> (hdims[0]) != nz || static_cast<int> (hdims[1]) != 1) { H5Sclose (space_hid); H5Dclose (data_hid); H5Gclose (group_hid); return false; } Complex *ctmp = m.xdata (); bool retval = false; if (H5Dread (data_hid, complex_type, H5S_ALL, H5S_ALL, H5P_DEFAULT, ctmp) >= 0 && m.indices_ok ()) { retval = true; matrix = m; } H5Tclose (complex_type); H5Sclose (space_hid); H5Dclose (data_hid); H5Gclose (group_hid); return retval; } #endif mxArray * octave_sparse_complex_matrix::as_mxArray (void) const { mwSize nz = nzmax (); mxArray *retval = new mxArray (mxDOUBLE_CLASS, rows (), columns (), nz, mxCOMPLEX); double *pr = static_cast<double *> (retval->get_data ()); double *pi = static_cast<double *> (retval->get_imag_data ()); mwIndex *ir = retval->get_ir (); mwIndex *jc = retval->get_jc (); for (mwIndex i = 0; i < nz; i++) { Complex val = matrix.data (i); pr[i] = std::real (val); pi[i] = std::imag (val); ir[i] = matrix.ridx (i); } for (mwIndex i = 0; i < columns () + 1; i++) jc[i] = matrix.cidx (i); return retval; } octave_value octave_sparse_complex_matrix::map (unary_mapper_t umap) const { switch (umap) { // Mappers handled specially. case umap_real: return ::real (matrix); case umap_imag: return ::imag (matrix); #define ARRAY_METHOD_MAPPER(UMAP, FCN) \ case umap_ ## UMAP: \ return octave_value (matrix.FCN ()) ARRAY_METHOD_MAPPER (abs, abs); #define ARRAY_MAPPER(UMAP, TYPE, FCN) \ case umap_ ## UMAP: \ return octave_value (matrix.map<TYPE> (FCN)) ARRAY_MAPPER (acos, Complex, ::acos); ARRAY_MAPPER (acosh, Complex, ::acosh); ARRAY_MAPPER (angle, double, std::arg); ARRAY_MAPPER (arg, double, std::arg); ARRAY_MAPPER (asin, Complex, ::asin); ARRAY_MAPPER (asinh, Complex, ::asinh); ARRAY_MAPPER (atan, Complex, ::atan); ARRAY_MAPPER (atanh, Complex, ::atanh); ARRAY_MAPPER (ceil, Complex, ::ceil); ARRAY_MAPPER (conj, Complex, std::conj<double>); ARRAY_MAPPER (cos, Complex, std::cos); ARRAY_MAPPER (cosh, Complex, std::cosh); ARRAY_MAPPER (exp, Complex, std::exp); ARRAY_MAPPER (expm1, Complex, ::expm1); ARRAY_MAPPER (fix, Complex, ::fix); ARRAY_MAPPER (floor, Complex, ::floor); ARRAY_MAPPER (log, Complex, std::log); ARRAY_MAPPER (log2, Complex, xlog2); ARRAY_MAPPER (log10, Complex, std::log10); ARRAY_MAPPER (log1p, Complex, ::log1p); ARRAY_MAPPER (round, Complex, xround); ARRAY_MAPPER (roundb, Complex, xroundb); ARRAY_MAPPER (signum, Complex, ::signum); ARRAY_MAPPER (sin, Complex, std::sin); ARRAY_MAPPER (sinh, Complex, std::sinh); ARRAY_MAPPER (sqrt, Complex, std::sqrt); ARRAY_MAPPER (tan, Complex, std::tan); ARRAY_MAPPER (tanh, Complex, std::tanh); ARRAY_MAPPER (isnan, bool, xisnan); ARRAY_MAPPER (isna, bool, octave_is_NA); ARRAY_MAPPER (isinf, bool, xisinf); ARRAY_MAPPER (finite, bool, xfinite); default: // Attempt to go via dense matrix. return octave_base_sparse<SparseComplexMatrix>::map (umap); } }