GNU Octave  4.0.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ov-re-sparse.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2004-2015 David Bateman
4 Copyright (C) 1998-2004 Andy Adler
5 
6 This file is part of Octave.
7 
8 Octave is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
12 
13 Octave is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with Octave; see the file COPYING. If not, see
20 <http://www.gnu.org/licenses/>.
21 
22 */
23 
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27 
28 #include <iostream>
29 #include <limits>
30 #include <vector>
31 
32 #include "lo-specfun.h"
33 #include "lo-mappers.h"
34 #include "oct-locbuf.h"
35 
36 #include "mxarray.h"
37 #include "ov-base.h"
38 #include "ov-scalar.h"
39 #include "gripes.h"
40 
41 #include "oct-hdf5.h"
42 #include "ls-hdf5.h"
43 
44 #include "ov-re-sparse.h"
45 
46 #include "ov-base-sparse.h"
47 #include "ov-base-sparse.cc"
48 
49 #include "ov-bool-sparse.h"
50 
52 
53 
55  "double");
56 
58 octave_sparse_matrix::index_vector (bool /* require_integers */) const
59 {
60  if (matrix.numel () == matrix.nnz ())
61  return idx_vector (array_value ());
62  else
63  {
64  std::string nm = type_name ();
65  error ("%s type invalid as index value", nm.c_str ());
66  return idx_vector ();
67  }
68 }
69 
72 {
73  octave_base_value *retval = 0;
74 
76  {
77  // Don't use numel, since it can overflow for very large matrices
78  // Note that for the second test, this means it becomes approximative
79  // since it involves a cast to double to avoid issues of overflow
80  if (matrix.rows () == 1 && matrix.cols () == 1)
81  {
82  // Const copy of the matrix, so the right version of () operator used
83  const SparseMatrix tmp (matrix);
84 
85  retval = new octave_scalar (tmp (0));
86  }
87  else if (matrix.cols () > 0 && matrix.rows () > 0
88  && (double (matrix.byte_size ()) > double (matrix.rows ())
89  * double (matrix.cols ()) * sizeof (double)))
90  retval = new octave_matrix (matrix.matrix_value ());
91  }
92 
93  return retval;
94 }
95 
96 double
98 {
99  double retval = lo_ieee_nan_value ();
100 
101  if (numel () > 0)
102  {
103  if (numel () > 1)
104  gripe_implicit_conversion ("Octave:array-to-scalar",
105  "real sparse matrix", "real scalar");
106 
107  retval = matrix (0, 0);
108  }
109  else
110  gripe_invalid_conversion ("real sparse matrix", "real scalar");
111 
112  return retval;
113 }
114 
115 Complex
117 {
118  double tmp = lo_ieee_nan_value ();
119 
120  Complex retval (tmp, tmp);
121 
122  // FIXME: maybe this should be a function, valid_as_scalar()
123  if (rows () > 0 && columns () > 0)
124  {
125  if (numel () > 1)
126  gripe_implicit_conversion ("Octave:array-to-scalar",
127  "real sparse matrix", "complex scalar");
128 
129  retval = matrix (0, 0);
130  }
131  else
132  gripe_invalid_conversion ("real sparse matrix", "complex scalar");
133 
134  return retval;
135 }
136 
137 Matrix
139 {
140  return matrix.matrix_value ();
141 }
142 
145 {
146  NDArray m = matrix.matrix_value ();
147 
148  if (m.any_element_is_nan ())
150  else if (warn && m.any_element_not_one_or_zero ())
152 
153  return boolNDArray (m);
154 }
155 
158 {
159  charNDArray retval (dims (), 0);
160  octave_idx_type nc = matrix.cols ();
161  octave_idx_type nr = matrix.rows ();
162 
163  for (octave_idx_type j = 0; j < nc; j++)
164  for (octave_idx_type i = matrix.cidx (j); i < matrix.cidx (j+1); i++)
165  retval(matrix.ridx (i) + nr * j) = static_cast<char>(matrix.data (i));
166 
167  return retval;
168 }
169 
172 {
173  return ComplexMatrix (matrix.matrix_value ());
174 }
175 
178 {
180 }
181 
182 NDArray
184 {
185  return NDArray (matrix.matrix_value ());
186 }
187 
190 {
191  if (matrix.any_element_is_nan ())
193  else if (warn && matrix.any_element_not_one_or_zero ())
195 
196  return mx_el_ne (matrix, 0.0);
197 }
198 
201 {
202  octave_value retval;
203  dim_vector dv = dims ();
204  octave_idx_type nel = dv.numel ();
205 
206  if (nel == 0)
207  {
208  char s = '\0';
209  retval = octave_value (&s, type);
210  }
211  else
212  {
213  octave_idx_type nr = matrix.rows ();
214  octave_idx_type nc = matrix.cols ();
215  charNDArray chm (dv, static_cast<char> (0));
216 
217  bool warned = false;
218 
219  for (octave_idx_type j = 0; j < nc; j++)
220  for (octave_idx_type i = matrix.cidx (j);
221  i < matrix.cidx (j+1); i++)
222  {
223  octave_quit ();
224 
225  double d = matrix.data (i);
226 
227  if (xisnan (d))
228  {
230  return retval;
231  }
232  else
233  {
234  int ival = NINT (d);
235 
236  if (ival < 0
238  {
239  // FIXME: is there something better we could do?
240 
241  ival = 0;
242 
243  if (! warned)
244  {
245  ::warning ("range error for conversion to character value");
246  warned = true;
247  }
248  }
249 
250  chm (matrix.ridx (i) + j * nr) =
251  static_cast<char> (ival);
252  }
253  }
254 
255  retval = octave_value (chm, type);
256  }
257 
258  return retval;
259 }
260 
261 bool
262 octave_sparse_matrix::save_binary (std::ostream& os, bool&save_as_floats)
263 {
264  dim_vector d = this->dims ();
265  if (d.length () < 1)
266  return false;
267 
268  // Ensure that additional memory is deallocated
270 
271  int nr = d(0);
272  int nc = d(1);
273  int nz = nnz ();
274 
275  int32_t itmp;
276  // Use negative value for ndims to be consistent with other formats
277  itmp = -2;
278  os.write (reinterpret_cast<char *> (&itmp), 4);
279 
280  itmp = nr;
281  os.write (reinterpret_cast<char *> (&itmp), 4);
282 
283  itmp = nc;
284  os.write (reinterpret_cast<char *> (&itmp), 4);
285 
286  itmp = nz;
287  os.write (reinterpret_cast<char *> (&itmp), 4);
288 
289  save_type st = LS_DOUBLE;
290  if (save_as_floats)
291  {
293  {
294  warning ("save: some values too large to save as floats --");
295  warning ("save: saving as doubles instead");
296  }
297  else
298  st = LS_FLOAT;
299  }
300  else if (matrix.nnz () > 8192) // FIXME: make this configurable.
301  {
302  double max_val, min_val;
303  if (matrix.all_integers (max_val, min_val))
304  st = get_save_type (max_val, min_val);
305  }
306 
307  // add one to the printed indices to go from
308  // zero-based to one-based arrays
309  for (int i = 0; i < nc+1; i++)
310  {
311  octave_quit ();
312  itmp = matrix.cidx (i);
313  os.write (reinterpret_cast<char *> (&itmp), 4);
314  }
315 
316  for (int i = 0; i < nz; i++)
317  {
318  octave_quit ();
319  itmp = matrix.ridx (i);
320  os.write (reinterpret_cast<char *> (&itmp), 4);
321  }
322 
323  write_doubles (os, matrix.data (), st, nz);
324 
325  return true;
326 }
327 
328 bool
329 octave_sparse_matrix::load_binary (std::istream& is, bool swap,
331 {
332  int32_t nz, nc, nr, tmp;
333  char ctmp;
334 
335  if (! is.read (reinterpret_cast<char *> (&tmp), 4))
336  return false;
337 
338  if (swap)
339  swap_bytes<4> (&tmp);
340 
341  if (tmp != -2)
342  {
343  error ("load: only 2-D sparse matrices are supported");
344  return false;
345  }
346 
347  if (! is.read (reinterpret_cast<char *> (&nr), 4))
348  return false;
349  if (! is.read (reinterpret_cast<char *> (&nc), 4))
350  return false;
351  if (! is.read (reinterpret_cast<char *> (&nz), 4))
352  return false;
353 
354  if (swap)
355  {
356  swap_bytes<4> (&nr);
357  swap_bytes<4> (&nc);
358  swap_bytes<4> (&nz);
359  }
360 
361  SparseMatrix m (static_cast<octave_idx_type> (nr),
362  static_cast<octave_idx_type> (nc),
363  static_cast<octave_idx_type> (nz));
364 
365  for (int i = 0; i < nc+1; i++)
366  {
367  octave_quit ();
368  if (! is.read (reinterpret_cast<char *> (&tmp), 4))
369  return false;
370  if (swap)
371  swap_bytes<4> (&tmp);
372  m.xcidx (i) = tmp;
373  }
374 
375  for (int i = 0; i < nz; i++)
376  {
377  octave_quit ();
378  if (! is.read (reinterpret_cast<char *> (&tmp), 4))
379  return false;
380  if (swap)
381  swap_bytes<4> (&tmp);
382  m.xridx (i) = tmp;
383  }
384 
385  if (! is.read (reinterpret_cast<char *> (&ctmp), 1))
386  return false;
387 
388  read_doubles (is, m.xdata (), static_cast<save_type> (ctmp), nz, swap, fmt);
389 
390  if (error_state || ! is)
391  return false;
392 
393  if (! m.indices_ok ())
394  return false;
395 
396  matrix = m;
397 
398  return true;
399 }
400 
401 bool
403  bool save_as_floats)
404 {
405  bool retval = false;
406 
407 #if defined (HAVE_HDF5)
408 
409  dim_vector dv = dims ();
410  int empty = save_hdf5_empty (loc_id, name, dv);
411  if (empty)
412  return (empty > 0);
413 
414  // Ensure that additional memory is deallocated
416 
417 #if HAVE_HDF5_18
418  hid_t group_hid = H5Gcreate (loc_id, name, H5P_DEFAULT, H5P_DEFAULT,
419  H5P_DEFAULT);
420 #else
421  hid_t group_hid = H5Gcreate (loc_id, name, 0);
422 #endif
423  if (group_hid < 0)
424  return false;
425 
426  hid_t space_hid, data_hid;
427  space_hid = data_hid = -1;
429  octave_idx_type tmp;
430  hsize_t hdims[2];
431 
432  space_hid = H5Screate_simple (0, hdims, 0);
433  if (space_hid < 0)
434  {
435  H5Gclose (group_hid);
436  return false;
437  }
438 #if HAVE_HDF5_18
439  data_hid = H5Dcreate (group_hid, "nr", H5T_NATIVE_IDX, space_hid,
440  H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
441 #else
442  data_hid = H5Dcreate (group_hid, "nr", H5T_NATIVE_IDX, space_hid,
443  H5P_DEFAULT);
444 #endif
445  if (data_hid < 0)
446  {
447  H5Sclose (space_hid);
448  H5Gclose (group_hid);
449  return false;
450  }
451 
452  tmp = m.rows ();
453  retval = H5Dwrite (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL, H5P_DEFAULT,
454  &tmp) >= 0;
455  H5Dclose (data_hid);
456  if (!retval)
457  {
458  H5Sclose (space_hid);
459  H5Gclose (group_hid);
460  return false;
461  }
462 #if HAVE_HDF5_18
463  data_hid = H5Dcreate (group_hid, "nc", H5T_NATIVE_IDX, space_hid,
464  H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
465 #else
466  data_hid = H5Dcreate (group_hid, "nc", H5T_NATIVE_IDX, space_hid,
467  H5P_DEFAULT);
468 #endif
469  if (data_hid < 0)
470  {
471  H5Sclose (space_hid);
472  H5Gclose (group_hid);
473  return false;
474  }
475 
476  tmp = m.cols ();
477  retval = H5Dwrite (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL, H5P_DEFAULT,
478  &tmp) >= 0;
479  H5Dclose (data_hid);
480  if (!retval)
481  {
482  H5Sclose (space_hid);
483  H5Gclose (group_hid);
484  return false;
485  }
486 
487 #if HAVE_HDF5_18
488  data_hid = H5Dcreate (group_hid, "nz", H5T_NATIVE_IDX, space_hid,
489  H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
490 #else
491  data_hid = H5Dcreate (group_hid, "nz", H5T_NATIVE_IDX, space_hid,
492  H5P_DEFAULT);
493 #endif
494  if (data_hid < 0)
495  {
496  H5Sclose (space_hid);
497  H5Gclose (group_hid);
498  return false;
499  }
500 
501  tmp = m.nnz ();
502  retval = H5Dwrite (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL, H5P_DEFAULT,
503  &tmp) >= 0;
504  H5Dclose (data_hid);
505  if (!retval)
506  {
507  H5Sclose (space_hid);
508  H5Gclose (group_hid);
509  return false;
510  }
511 
512  H5Sclose (space_hid);
513 
514  hdims[0] = m.cols () + 1;
515  hdims[1] = 1;
516 
517  space_hid = H5Screate_simple (2, hdims, 0);
518 
519  if (space_hid < 0)
520  {
521  H5Gclose (group_hid);
522  return false;
523  }
524 
525 #if HAVE_HDF5_18
526  data_hid = H5Dcreate (group_hid, "cidx", H5T_NATIVE_IDX, space_hid,
527  H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
528 #else
529  data_hid = H5Dcreate (group_hid, "cidx", H5T_NATIVE_IDX, space_hid,
530  H5P_DEFAULT);
531 #endif
532  if (data_hid < 0)
533  {
534  H5Sclose (space_hid);
535  H5Gclose (group_hid);
536  return false;
537  }
538 
539  octave_idx_type * itmp = m.xcidx ();
540  retval = H5Dwrite (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL, H5P_DEFAULT,
541  itmp) >= 0;
542  H5Dclose (data_hid);
543  if (!retval)
544  {
545  H5Sclose (space_hid);
546  H5Gclose (group_hid);
547  return false;
548  }
549 
550  H5Sclose (space_hid);
551 
552  hdims[0] = m.nnz ();
553  hdims[1] = 1;
554 
555  space_hid = H5Screate_simple (2, hdims, 0);
556 
557  if (space_hid < 0)
558  {
559  H5Gclose (group_hid);
560  return false;
561  }
562 #if HAVE_HDF5_18
563  data_hid = H5Dcreate (group_hid, "ridx", H5T_NATIVE_IDX, space_hid,
564  H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
565 #else
566  data_hid = H5Dcreate (group_hid, "ridx", H5T_NATIVE_IDX, space_hid,
567  H5P_DEFAULT);
568 #endif
569  if (data_hid < 0)
570  {
571  H5Sclose (space_hid);
572  H5Gclose (group_hid);
573  return false;
574  }
575 
576  itmp = m.xridx ();
577  retval = H5Dwrite (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL, H5P_DEFAULT,
578  itmp) >= 0;
579  H5Dclose (data_hid);
580  if (!retval)
581  {
582  H5Sclose (space_hid);
583  H5Gclose (group_hid);
584  return false;
585  }
586 
587  hid_t save_type_hid = H5T_NATIVE_DOUBLE;
588 
589  if (save_as_floats)
590  {
591  if (m.too_large_for_float ())
592  {
593  warning ("save: some values too large to save as floats --");
594  warning ("save: saving as doubles instead");
595  }
596  else
597  save_type_hid = H5T_NATIVE_FLOAT;
598  }
599 #if HAVE_HDF5_INT2FLOAT_CONVERSIONS
600  // hdf5 currently doesn't support float/integer conversions
601  else
602  {
603  double max_val, min_val;
604 
605  if (m.all_integers (max_val, min_val))
606  save_type_hid
607  = save_type_to_hdf5 (get_save_type (max_val, min_val));
608  }
609 #endif /* HAVE_HDF5_INT2FLOAT_CONVERSIONS */
610 
611 #if HAVE_HDF5_18
612  data_hid = H5Dcreate (group_hid, "data", save_type_hid, space_hid,
613  H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
614 #else
615  data_hid = H5Dcreate (group_hid, "data", save_type_hid, space_hid,
616  H5P_DEFAULT);
617 #endif
618  if (data_hid < 0)
619  {
620  H5Sclose (space_hid);
621  H5Gclose (group_hid);
622  return false;
623  }
624 
625  double * dtmp = m.xdata ();
626  retval = H5Dwrite (data_hid, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL,
627  H5P_DEFAULT, dtmp) >= 0;
628  H5Dclose (data_hid);
629  H5Sclose (space_hid);
630  H5Gclose (group_hid);
631 
632 #else
633  gripe_save ("hdf5");
634 #endif
635 
636  return retval;
637 }
638 
639 bool
641 {
642  bool retval = false;
643 
644 #if defined (HAVE_HDF5)
645 
646  octave_idx_type nr, nc, nz;
647  hid_t group_hid, data_hid, space_hid;
648  hsize_t rank;
649 
650  dim_vector dv;
651  int empty = load_hdf5_empty (loc_id, name, dv);
652  if (empty > 0)
653  matrix.resize (dv);
654  if (empty)
655  return (empty > 0);
656 
657 #if HAVE_HDF5_18
658  group_hid = H5Gopen (loc_id, name, H5P_DEFAULT);
659 #else
660  group_hid = H5Gopen (loc_id, name);
661 #endif
662  if (group_hid < 0) return false;
663 
664 #if HAVE_HDF5_18
665  data_hid = H5Dopen (group_hid, "nr", H5P_DEFAULT);
666 #else
667  data_hid = H5Dopen (group_hid, "nr");
668 #endif
669  space_hid = H5Dget_space (data_hid);
670  rank = H5Sget_simple_extent_ndims (space_hid);
671 
672  if (rank != 0)
673  {
674  H5Dclose (data_hid);
675  H5Gclose (group_hid);
676  return false;
677  }
678 
679  if (H5Dread (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL,
680  H5P_DEFAULT, &nr) < 0)
681  {
682  H5Dclose (data_hid);
683  H5Gclose (group_hid);
684  return false;
685  }
686 
687  H5Dclose (data_hid);
688 
689 #if HAVE_HDF5_18
690  data_hid = H5Dopen (group_hid, "nc", H5P_DEFAULT);
691 #else
692  data_hid = H5Dopen (group_hid, "nc");
693 #endif
694  space_hid = H5Dget_space (data_hid);
695  rank = H5Sget_simple_extent_ndims (space_hid);
696 
697  if (rank != 0)
698  {
699  H5Dclose (data_hid);
700  H5Gclose (group_hid);
701  return false;
702  }
703 
704  if (H5Dread (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL,
705  H5P_DEFAULT, &nc) < 0)
706  {
707  H5Dclose (data_hid);
708  H5Gclose (group_hid);
709  return false;
710  }
711 
712  H5Dclose (data_hid);
713 
714 #if HAVE_HDF5_18
715  data_hid = H5Dopen (group_hid, "nz", H5P_DEFAULT);
716 #else
717  data_hid = H5Dopen (group_hid, "nz");
718 #endif
719  space_hid = H5Dget_space (data_hid);
720  rank = H5Sget_simple_extent_ndims (space_hid);
721 
722  if (rank != 0)
723  {
724  H5Dclose (data_hid);
725  H5Gclose (group_hid);
726  return false;
727  }
728 
729  if (H5Dread (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL,
730  H5P_DEFAULT, &nz) < 0)
731  {
732  H5Dclose (data_hid);
733  H5Gclose (group_hid);
734  return false;
735  }
736 
737  H5Dclose (data_hid);
738 
739  SparseMatrix m (static_cast<octave_idx_type> (nr),
740  static_cast<octave_idx_type> (nc),
741  static_cast<octave_idx_type> (nz));
742 
743 #if HAVE_HDF5_18
744  data_hid = H5Dopen (group_hid, "cidx", H5P_DEFAULT);
745 #else
746  data_hid = H5Dopen (group_hid, "cidx");
747 #endif
748  space_hid = H5Dget_space (data_hid);
749  rank = H5Sget_simple_extent_ndims (space_hid);
750 
751  if (rank != 2)
752  {
753  H5Sclose (space_hid);
754  H5Dclose (data_hid);
755  H5Gclose (group_hid);
756  return false;
757  }
758 
759  OCTAVE_LOCAL_BUFFER (hsize_t, hdims, rank);
760  OCTAVE_LOCAL_BUFFER (hsize_t, maxdims, rank);
761 
762  H5Sget_simple_extent_dims (space_hid, hdims, maxdims);
763 
764  if (static_cast<int> (hdims[0]) != nc + 1
765  || static_cast<int> (hdims[1]) != 1)
766  {
767  H5Sclose (space_hid);
768  H5Dclose (data_hid);
769  H5Gclose (group_hid);
770  return false;
771  }
772 
773  octave_idx_type *itmp = m.xcidx ();
774  if (H5Dread (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL,
775  H5P_DEFAULT, itmp) < 0)
776  {
777  H5Sclose (space_hid);
778  H5Dclose (data_hid);
779  H5Gclose (group_hid);
780  return false;
781  }
782 
783  H5Sclose (space_hid);
784  H5Dclose (data_hid);
785 
786 #if HAVE_HDF5_18
787  data_hid = H5Dopen (group_hid, "ridx", H5P_DEFAULT);
788 #else
789  data_hid = H5Dopen (group_hid, "ridx");
790 #endif
791  space_hid = H5Dget_space (data_hid);
792  rank = H5Sget_simple_extent_ndims (space_hid);
793 
794  if (rank != 2)
795  {
796  H5Sclose (space_hid);
797  H5Dclose (data_hid);
798  H5Gclose (group_hid);
799  return false;
800  }
801 
802  H5Sget_simple_extent_dims (space_hid, hdims, maxdims);
803 
804  if (static_cast<int> (hdims[0]) != nz || static_cast<int> (hdims[1]) != 1)
805  {
806  H5Sclose (space_hid);
807  H5Dclose (data_hid);
808  H5Gclose (group_hid);
809  return false;
810  }
811 
812  itmp = m.xridx ();
813  if (H5Dread (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL,
814  H5P_DEFAULT, itmp) < 0)
815  {
816  H5Sclose (space_hid);
817  H5Dclose (data_hid);
818  H5Gclose (group_hid);
819  return false;
820  }
821 
822  H5Sclose (space_hid);
823  H5Dclose (data_hid);
824 
825 #if HAVE_HDF5_18
826  data_hid = H5Dopen (group_hid, "data", H5P_DEFAULT);
827 #else
828  data_hid = H5Dopen (group_hid, "data");
829 #endif
830  space_hid = H5Dget_space (data_hid);
831  rank = H5Sget_simple_extent_ndims (space_hid);
832 
833  if (rank != 2)
834  {
835  H5Sclose (space_hid);
836  H5Dclose (data_hid);
837  H5Gclose (group_hid);
838  return false;
839  }
840 
841  H5Sget_simple_extent_dims (space_hid, hdims, maxdims);
842 
843  if (static_cast<int> (hdims[0]) != nz || static_cast<int> (hdims[1]) != 1)
844  {
845  H5Sclose (space_hid);
846  H5Dclose (data_hid);
847  H5Gclose (group_hid);
848  return false;
849  }
850 
851  double *dtmp = m.xdata ();
852 
853  if (H5Dread (data_hid, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL,
854  H5P_DEFAULT, dtmp) >= 0
855  && m.indices_ok ())
856  {
857  retval = true;
858  matrix = m;
859  }
860 
861  H5Sclose (space_hid);
862  H5Dclose (data_hid);
863  H5Gclose (group_hid);
864 
865 #else
866  gripe_load ("hdf5");
867 #endif
868 
869  return retval;
870 }
871 
872 mxArray *
874 {
875  mwSize nz = nzmax ();
876  mwSize nr = rows ();
877  mwSize nc = columns ();
878  mxArray *retval = new mxArray (mxDOUBLE_CLASS, nr, nc, nz, mxREAL);
879  double *pr = static_cast<double *> (retval->get_data ());
880  mwIndex *ir = retval->get_ir ();
881  mwIndex *jc = retval->get_jc ();
882 
883  for (mwIndex i = 0; i < nz; i++)
884  {
885  pr[i] = matrix.data (i);
886  ir[i] = matrix.ridx (i);
887  }
888 
889  for (mwIndex i = 0; i < nc + 1; i++)
890  jc[i] = matrix.cidx (i);
891 
892  return retval;
893 }
894 
897 {
898  switch (umap)
899  {
900  case umap_imag:
901  return SparseMatrix (matrix.rows (), matrix.cols (), 0.0);
902 
903  case umap_real:
904  case umap_conj:
905  return matrix;
906 
907  // Mappers handled specially.
908 #define ARRAY_METHOD_MAPPER(UMAP, FCN) \
909  case umap_ ## UMAP: \
910  return octave_value (matrix.FCN ())
911 
913 
914 #define ARRAY_MAPPER(UMAP, TYPE, FCN) \
915  case umap_ ## UMAP: \
916  return octave_value (matrix.map<TYPE> (FCN))
917 
920  ARRAY_MAPPER (angle, double, ::arg);
921  ARRAY_MAPPER (arg, double, ::arg);
923  ARRAY_MAPPER (asinh, double, ::asinh);
924  ARRAY_MAPPER (atan, double, ::atan);
926  ARRAY_MAPPER (erf, double, ::erf);
927  ARRAY_MAPPER (erfinv, double, ::erfinv);
928  ARRAY_MAPPER (erfcinv, double, ::erfcinv);
929  ARRAY_MAPPER (erfc, double, ::erfc);
930  ARRAY_MAPPER (erfcx, double, ::erfcx);
931  ARRAY_MAPPER (erfi, double, ::erfi);
932  ARRAY_MAPPER (dawson, double, ::dawson);
933  ARRAY_MAPPER (gamma, double, xgamma);
934  ARRAY_MAPPER (lgamma, Complex, rc_lgamma);
935  ARRAY_MAPPER (cbrt, double, ::cbrt);
936  ARRAY_MAPPER (ceil, double, ::ceil);
937  ARRAY_MAPPER (cos, double, ::cos);
938  ARRAY_MAPPER (cosh, double, ::cosh);
939  ARRAY_MAPPER (exp, double, ::exp);
940  ARRAY_MAPPER (expm1, double, ::expm1);
941  ARRAY_MAPPER (fix, double, ::fix);
942  ARRAY_MAPPER (floor, double, ::floor);
943  ARRAY_MAPPER (log, Complex, rc_log);
944  ARRAY_MAPPER (log2, Complex, rc_log2);
945  ARRAY_MAPPER (log10, Complex, rc_log10);
947  ARRAY_MAPPER (round, double, xround);
948  ARRAY_MAPPER (roundb, double, xroundb);
949  ARRAY_MAPPER (signum, double, ::signum);
950  ARRAY_MAPPER (sin, double, ::sin);
951  ARRAY_MAPPER (sinh, double, ::sinh);
952  ARRAY_MAPPER (sqrt, Complex, rc_sqrt);
953  ARRAY_MAPPER (tan, double, ::tan);
954  ARRAY_MAPPER (tanh, double, ::tanh);
955  ARRAY_MAPPER (isnan, bool, xisnan);
956  ARRAY_MAPPER (isna, bool, octave_is_NA);
957  ARRAY_MAPPER (isinf, bool, xisinf);
958  ARRAY_MAPPER (finite, bool, xfinite);
959 
960  default: // Attempt to go via dense matrix.
962  }
963 }
save_type
Definition: data-conv.h:83
void gripe_implicit_conversion(const char *id, const char *from, const char *to)
Definition: gripes.cc:180
octave_idx_type * xridx(void)
Definition: Sparse.h:524
bool load_hdf5(octave_hdf5_id loc_id, const char *name)
Complex rc_asin(double x)
Definition: lo-mappers.cc:536
#define ARRAY_MAPPER(UMAP, TYPE, FCN)
octave_idx_type cols(void) const
Definition: Sparse.h:264
Complex complex_value(bool=false) const
T * data(void)
Definition: Sparse.h:509
SparseMatrix sparse_matrix_value(bool=false) const
Definition: ov-re-sparse.h:125
octave_idx_type rows(void) const
Definition: Sparse.h:263
double xround(double x)
Definition: lo-mappers.cc:63
octave_idx_type numel(void) const
Definition: Sparse.h:252
ComplexNDArray complex_array_value(bool=false) const
bool xisnan(double x)
Definition: lo-mappers.cc:144
octave_idx_type columns(void) const
Definition: ov-base.h:301
double cbrt(double x)
Definition: lo-specfun.cc:662
double log1p(double x)
Definition: lo-specfun.cc:620
double erfcinv(double x)
Definition: lo-specfun.cc:3151
bool save_hdf5(octave_hdf5_id loc_id, const char *name, bool save_as_floats)
std::complex< double > erfi(std::complex< double > z, double relerr=0)
void gripe_load(const char *type) const
Definition: ov-base.cc:1258
save_type get_save_type(double, double)
Definition: ls-utils.cc:35
Complex rc_acos(double x)
Definition: lo-mappers.cc:512
octave_value convert_to_str_internal(bool pad, bool force, char type) const
function atanh(X)
Definition: atanh.f:2
octave_idx_type * xcidx(void)
Definition: Sparse.h:537
void error(const char *fmt,...)
Definition: error.cc:476
void * get_data(void) const
Definition: mxarray.h:433
bool xisinf(double x)
Definition: lo-mappers.cc:160
void write_doubles(std::ostream &os, const double *data, save_type type, octave_idx_type len)
Definition: data-conv.cc:893
#define DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA(t, n, c)
Definition: ov-base.h:164
void gripe_save(const char *type) const
Definition: ov-base.cc:1267
octave_idx_type * cidx(void)
Definition: Sparse.h:531
double lo_ieee_nan_value(void)
Definition: lo-ieee.cc:126
double expm1(double x)
Definition: lo-specfun.cc:510
bool any_element_is_nan(void) const
Definition: dNDArray.cc:564
bool load_binary(std::istream &is, bool swap, oct_mach_info::float_format fmt)
bool indices_ok(void) const
Definition: Sparse.h:684
std::complex< double > erf(std::complex< double > z, double relerr=0)
octave_idx_type nzmax(void) const
octave_idx_type numel(int n=0) const
Number of elements that a matrix with this dimensions would have.
Definition: dim-vector.h:361
F77_RET_T const double const double double * d
int load_hdf5_empty(hid_t loc_id, const char *name, dim_vector &d)
Definition: ls-hdf5.cc:790
octave_value map(unary_mapper_t umap) const
boolNDArray bool_array_value(bool warn=false) const
octave_idx_type nnz(void) const
Definition: Sparse.h:248
subroutine xgamma(x, result)
Definition: xgamma.f:1
bool any_element_not_one_or_zero(void) const
Definition: dNDArray.cc:576
double signum(double x)
Definition: lo-mappers.cc:80
Complex rc_log1p(double x)
Definition: lo-specfun.cc:2977
mwIndex * get_ir(void) const
Definition: mxarray.h:442
void swap_bytes< 4 >(void *ptr)
Definition: byte-swap.h:59
Complex rc_lgamma(double x)
Definition: lo-specfun.cc:411
bool save_binary(std::ostream &os, bool &save_as_floats)
#define OCTINTERP_API
Definition: mexproto.h:66
bool all_integers(double &max_val, double &min_val) const
Definition: dSparse.cc:7351
Matrix matrix_value(void) const
Definition: dSparse.cc:7500
octave_idx_type numel(void) const
function asinh(X)
Definition: asinh.f:2
Sparse< T > maybe_compress(bool remove_zeros=false)
Definition: Sparse.h:469
void gripe_nan_to_logical_conversion(void)
void resize(octave_idx_type r, octave_idx_type c)
Definition: Sparse.cc:944
std::complex< T > ceil(const std::complex< T > &x)
Definition: lo-mappers.h:275
octave_value map(octave_base_value::unary_mapper_t umap) const
int error_state
Definition: error.cc:101
Complex rc_sqrt(double x)
Definition: lo-mappers.cc:606
mxArray * as_mxArray(void) const
size_t byte_size(void) const
Definition: Sparse.h:276
void gripe_nan_to_character_conversion(void)
bool too_large_for_float(void) const
Definition: dSparse.cc:7379
Complex rc_log(double x)
Definition: lo-mappers.cc:561
double double_value(bool=false) const
Definition: ov-re-sparse.cc:97
Complex rc_log2(double x)
Definition: lo-mappers.cc:577
std::complex< double > erfcx(std::complex< double > z, double relerr=0)
double xroundb(double x)
Definition: lo-mappers.cc:69
idx_vector index_vector(bool require_integers=false) const
Definition: ov-re-sparse.cc:58
bool any_element_not_one_or_zero(void) const
Definition: dSparse.cc:7304
boolMatrix mx_el_ne(const boolMatrix &m1, const boolMatrix &m2)
Definition: boolMatrix.cc:90
Complex rc_atanh(double x)
Definition: lo-mappers.cc:548
Definition: dMatrix.h:35
void read_doubles(std::istream &is, double *data, save_type type, octave_idx_type len, bool swap, oct_mach_info::float_format fmt)
Definition: data-conv.cc:778
int NINT(double x)
Definition: lo-mappers.cc:657
double erfinv(double x)
Definition: lo-specfun.cc:3063
Complex rc_log10(double x)
Definition: lo-mappers.cc:591
void mxArray
Definition: mex.h:53
function acosh(X)
Definition: acosh.f:2
double arg(double x)
Definition: lo-mappers.h:37
friend class octave_value
Definition: ov-base.h:206
void warning(const char *fmt,...)
Definition: error.cc:681
charNDArray max(char d, const charNDArray &m)
Definition: chNDArray.cc:233
#define H5T_NATIVE_IDX
Definition: ls-hdf5.h:209
octave_idx_type * ridx(void)
Definition: Sparse.h:518
void gripe_logical_conversion(void)
Definition: gripes.cc:201
bool xfinite(double x)
Definition: lo-mappers.cc:152
Complex rc_acosh(double x)
Definition: lo-mappers.cc:524
void gripe_invalid_conversion(const std::string &from, const std::string &to)
Definition: gripes.cc:99
Matrix matrix_value(bool=false) const
SparseBoolMatrix sparse_bool_matrix_value(bool warn=false) const
function gamma(X)
Definition: gamma.f:2
#define ARRAY_METHOD_MAPPER(UMAP, FCN)
T * xdata(void)
Definition: Sparse.h:511
NDArray array_value(bool=false) const
int save_hdf5_empty(hid_t loc_id, const char *name, const dim_vector d)
Definition: ls-hdf5.cc:740
#define OCTAVE_LOCAL_BUFFER(T, buf, size)
Definition: oct-locbuf.h:197
double fix(double x)
Definition: lo-mappers.h:39
mwIndex * get_jc(void) const
Definition: mxarray.h:444
bool any_element_is_nan(void) const
Definition: dSparse.cc:7274
bool Vsparse_auto_mutate
Definition: ov-base.cc:118
float dawson(float x)
Definition: lo-specfun.cc:349
Complex asin(const Complex &x)
Definition: lo-mappers.cc:204
ComplexMatrix complex_matrix_value(bool=false) const
std::complex< T > floor(const std::complex< T > &x)
Definition: lo-mappers.h:282
Definition: mxarray.h:52
std::complex< double > Complex
Definition: oct-cmplx.h:29
OCTAVE_EMPTY_CPP_ARG std::string type_name(void) const
Definition: ov-re-sparse.h:159
Complex acos(const Complex &x)
Definition: lo-mappers.cc:177
T abs(T x)
Definition: pr-output.cc:3062
int length(void) const
Definition: dim-vector.h:281
Complex atan(const Complex &x)
Definition: lo-mappers.cc:231
octave_idx_type nnz(void) const
octave_idx_type rows(void) const
Definition: ov-base.h:294
octave_base_value * try_narrowing_conversion(void)
Definition: ov-re-sparse.cc:71
charNDArray char_array_value(bool=false) const
bool octave_is_NA(double x)
Definition: lo-mappers.cc:167
std::complex< double > erfc(std::complex< double > z, double relerr=0)