GNU Octave  4.4.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
ov-re-sparse.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2004-2018 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
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12 
13 Octave is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License 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 <https://www.gnu.org/licenses/>.
21 
22 */
23 
24 #if defined (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 "errwarn.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 
51 
52 template class OCTINTERP_API octave_base_sparse<SparseMatrix>;
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  octave::err_invalid_index (nm.c_str ());
66  }
67 }
68 
71 {
72  octave_base_value *retval = nullptr;
73 
75  {
76  // Don't use numel, since it can overflow for very large matrices
77  // Note that for the second test, this means it becomes approximative
78  // since it involves a cast to double to avoid issues of overflow
79  if (matrix.rows () == 1 && matrix.cols () == 1)
80  {
81  // Const copy of the matrix, so the right version of () operator used
82  const SparseMatrix tmp (matrix);
83 
84  retval = new octave_scalar (tmp (0));
85  }
86  else if (matrix.cols () > 0 && matrix.rows () > 0
87  && (double (matrix.byte_size ()) > double (matrix.rows ())
88  * double (matrix.cols ()) * sizeof (double)))
90  }
91 
92  return retval;
93 }
94 
95 double
97 {
98  if (isempty ())
99  err_invalid_conversion ("real sparse matrix", "real scalar");
100 
101  if (numel () > 1)
102  warn_implicit_conversion ("Octave:array-to-scalar",
103  "real sparse matrix", "real scalar");
104 
105  return matrix(0, 0);
106 }
107 
108 Complex
110 {
111  // FIXME: maybe this should be a function, valid_as_scalar()
112  if (rows () == 0 || columns () == 0)
113  err_invalid_conversion ("real sparse matrix", "complex scalar");
114 
115  if (numel () > 1)
116  warn_implicit_conversion ("Octave:array-to-scalar",
117  "real sparse matrix", "complex scalar");
118 
119  return Complex (matrix(0, 0), 0);
120 }
121 
122 Matrix
124 {
125  return matrix.matrix_value ();
126 }
127 
130 {
131  NDArray m = matrix.matrix_value ();
132 
133  if (m.any_element_is_nan ())
135  if (warn && m.any_element_not_one_or_zero ())
137 
138  return boolNDArray (m);
139 }
140 
143 {
144  charNDArray retval (dims (), 0);
145  octave_idx_type nc = matrix.cols ();
146  octave_idx_type nr = matrix.rows ();
147 
148  for (octave_idx_type j = 0; j < nc; j++)
149  for (octave_idx_type i = matrix.cidx (j); i < matrix.cidx (j+1); i++)
150  retval(matrix.ridx (i) + nr * j) = static_cast<char>(matrix.data (i));
151 
152  return retval;
153 }
154 
157 {
158  return ComplexMatrix (matrix.matrix_value ());
159 }
160 
163 {
165 }
166 
167 NDArray
169 {
170  return NDArray (matrix.matrix_value ());
171 }
172 
175 {
176  if (matrix.any_element_is_nan ())
178  if (warn && matrix.any_element_not_one_or_zero ())
180 
181  return mx_el_ne (matrix, 0.0);
182 }
183 
186 {
188  dim_vector dv = dims ();
189  octave_idx_type nel = dv.numel ();
190 
191  if (nel == 0)
192  {
193  char s = '\0';
194  retval = octave_value (&s, type);
195  }
196  else
197  {
198  octave_idx_type nr = matrix.rows ();
199  octave_idx_type nc = matrix.cols ();
200  charNDArray chm (dv, static_cast<char> (0));
201 
202  bool warned = false;
203 
204  for (octave_idx_type j = 0; j < nc; j++)
205  for (octave_idx_type i = matrix.cidx (j);
206  i < matrix.cidx (j+1); i++)
207  {
208  octave_quit ();
209 
210  double d = matrix.data (i);
211 
212  if (octave::math::isnan (d))
214 
215  int ival = octave::math::nint (d);
216 
217  if (ival < 0 || ival > std::numeric_limits<unsigned char>::max ())
218  {
219  // FIXME: is there something better we could do?
220 
221  ival = 0;
222 
223  if (! warned)
224  {
225  ::warning ("range error for conversion to character value");
226  warned = true;
227  }
228  }
229 
230  chm(matrix.ridx (i) + j * nr) = static_cast<char> (ival);
231  }
232 
233  retval = octave_value (chm, type);
234  }
235 
236  return retval;
237 }
238 
241 {
242  return this->matrix;
243 }
244 
245 bool
247 {
248  dim_vector dv = this->dims ();
249  if (dv.ndims () < 1)
250  return false;
251 
252  // Ensure that additional memory is deallocated
254 
255  int nr = dv(0);
256  int nc = dv(1);
257  int nz = nnz ();
258 
259  int32_t itmp;
260  // Use negative value for ndims to be consistent with other formats
261  itmp = -2;
262  os.write (reinterpret_cast<char *> (&itmp), 4);
263 
264  itmp = nr;
265  os.write (reinterpret_cast<char *> (&itmp), 4);
266 
267  itmp = nc;
268  os.write (reinterpret_cast<char *> (&itmp), 4);
269 
270  itmp = nz;
271  os.write (reinterpret_cast<char *> (&itmp), 4);
272 
273  save_type st = LS_DOUBLE;
274  if (save_as_floats)
275  {
277  {
278  warning ("save: some values too large to save as floats --");
279  warning ("save: saving as doubles instead");
280  }
281  else
282  st = LS_FLOAT;
283  }
284  else if (matrix.nnz () > 8192) // FIXME: make this configurable.
285  {
286  double max_val, min_val;
287  if (matrix.all_integers (max_val, min_val))
288  st = get_save_type (max_val, min_val);
289  }
290 
291  // add one to the printed indices to go from
292  // zero-based to one-based arrays
293  for (int i = 0; i < nc+1; i++)
294  {
295  octave_quit ();
296  itmp = matrix.cidx (i);
297  os.write (reinterpret_cast<char *> (&itmp), 4);
298  }
299 
300  for (int i = 0; i < nz; i++)
301  {
302  octave_quit ();
303  itmp = matrix.ridx (i);
304  os.write (reinterpret_cast<char *> (&itmp), 4);
305  }
306 
307  write_doubles (os, matrix.data (), st, nz);
308 
309  return true;
310 }
311 
312 bool
315 {
316  int32_t nz, nc, nr, tmp;
317  char ctmp;
318 
319  if (! is.read (reinterpret_cast<char *> (&tmp), 4))
320  return false;
321 
322  if (swap)
323  swap_bytes<4> (&tmp);
324 
325  if (tmp != -2)
326  error ("load: only 2-D sparse matrices are supported");
327 
328  if (! is.read (reinterpret_cast<char *> (&nr), 4))
329  return false;
330  if (! is.read (reinterpret_cast<char *> (&nc), 4))
331  return false;
332  if (! is.read (reinterpret_cast<char *> (&nz), 4))
333  return false;
334 
335  if (swap)
336  {
337  swap_bytes<4> (&nr);
338  swap_bytes<4> (&nc);
339  swap_bytes<4> (&nz);
340  }
341 
342  SparseMatrix m (static_cast<octave_idx_type> (nr),
343  static_cast<octave_idx_type> (nc),
344  static_cast<octave_idx_type> (nz));
345 
346  for (int i = 0; i < nc+1; i++)
347  {
348  octave_quit ();
349  if (! is.read (reinterpret_cast<char *> (&tmp), 4))
350  return false;
351  if (swap)
352  swap_bytes<4> (&tmp);
353  m.xcidx (i) = tmp;
354  }
355 
356  for (int i = 0; i < nz; i++)
357  {
358  octave_quit ();
359  if (! is.read (reinterpret_cast<char *> (&tmp), 4))
360  return false;
361  if (swap)
362  swap_bytes<4> (&tmp);
363  m.xridx (i) = tmp;
364  }
365 
366  if (! is.read (reinterpret_cast<char *> (&ctmp), 1))
367  return false;
368 
369  read_doubles (is, m.xdata (), static_cast<save_type> (ctmp), nz, swap, fmt);
370 
371  if (! is)
372  return false;
373 
374  if (! m.indices_ok ())
375  return false;
376 
377  matrix = m;
378 
379  return true;
380 }
381 
382 bool
384  bool save_as_floats)
385 {
386  bool retval = false;
387 
388 #if defined (HAVE_HDF5)
389 
390  dim_vector dv = dims ();
391  int empty = save_hdf5_empty (loc_id, name, dv);
392  if (empty)
393  return (empty > 0);
394 
395  // Ensure that additional memory is deallocated
397 
398 #if defined (HAVE_HDF5_18)
399  hid_t group_hid = H5Gcreate (loc_id, name, octave_H5P_DEFAULT,
401 #else
402  hid_t group_hid = H5Gcreate (loc_id, name, 0);
403 #endif
404  if (group_hid < 0)
405  return false;
406 
407  hid_t space_hid, data_hid;
408  space_hid = data_hid = -1;
411  hsize_t hdims[2];
412 
413  space_hid = H5Screate_simple (0, hdims, nullptr);
414  if (space_hid < 0)
415  {
416  H5Gclose (group_hid);
417  return false;
418  }
419 #if defined (HAVE_HDF5_18)
420  data_hid = H5Dcreate (group_hid, "nr", H5T_NATIVE_IDX, space_hid,
422 #else
423  data_hid = H5Dcreate (group_hid, "nr", H5T_NATIVE_IDX, space_hid,
425 #endif
426  if (data_hid < 0)
427  {
428  H5Sclose (space_hid);
429  H5Gclose (group_hid);
430  return false;
431  }
432 
433  tmp = m.rows ();
434  retval = H5Dwrite (data_hid, H5T_NATIVE_IDX, octave_H5S_ALL, octave_H5S_ALL,
435  octave_H5P_DEFAULT, &tmp) >= 0;
436  H5Dclose (data_hid);
437  if (! retval)
438  {
439  H5Sclose (space_hid);
440  H5Gclose (group_hid);
441  return false;
442  }
443 #if defined (HAVE_HDF5_18)
444  data_hid = H5Dcreate (group_hid, "nc", H5T_NATIVE_IDX, space_hid,
446 #else
447  data_hid = H5Dcreate (group_hid, "nc", H5T_NATIVE_IDX, space_hid,
449 #endif
450  if (data_hid < 0)
451  {
452  H5Sclose (space_hid);
453  H5Gclose (group_hid);
454  return false;
455  }
456 
457  tmp = m.cols ();
458  retval = H5Dwrite (data_hid, H5T_NATIVE_IDX, octave_H5S_ALL, octave_H5S_ALL,
459  octave_H5P_DEFAULT, &tmp) >= 0;
460  H5Dclose (data_hid);
461  if (! retval)
462  {
463  H5Sclose (space_hid);
464  H5Gclose (group_hid);
465  return false;
466  }
467 
468 #if defined (HAVE_HDF5_18)
469  data_hid = H5Dcreate (group_hid, "nz", H5T_NATIVE_IDX, space_hid,
471 #else
472  data_hid = H5Dcreate (group_hid, "nz", H5T_NATIVE_IDX, space_hid,
474 #endif
475  if (data_hid < 0)
476  {
477  H5Sclose (space_hid);
478  H5Gclose (group_hid);
479  return false;
480  }
481 
482  tmp = m.nnz ();
483  retval = H5Dwrite (data_hid, H5T_NATIVE_IDX, octave_H5S_ALL, octave_H5S_ALL,
484  octave_H5P_DEFAULT, &tmp) >= 0;
485  H5Dclose (data_hid);
486  if (! retval)
487  {
488  H5Sclose (space_hid);
489  H5Gclose (group_hid);
490  return false;
491  }
492 
493  H5Sclose (space_hid);
494 
495  hdims[0] = m.cols () + 1;
496  hdims[1] = 1;
497 
498  space_hid = H5Screate_simple (2, hdims, nullptr);
499 
500  if (space_hid < 0)
501  {
502  H5Gclose (group_hid);
503  return false;
504  }
505 
506 #if defined (HAVE_HDF5_18)
507  data_hid = H5Dcreate (group_hid, "cidx", H5T_NATIVE_IDX, space_hid,
509 #else
510  data_hid = H5Dcreate (group_hid, "cidx", H5T_NATIVE_IDX, space_hid,
512 #endif
513  if (data_hid < 0)
514  {
515  H5Sclose (space_hid);
516  H5Gclose (group_hid);
517  return false;
518  }
519 
520  octave_idx_type *itmp = m.xcidx ();
521  retval = H5Dwrite (data_hid, H5T_NATIVE_IDX, octave_H5S_ALL, octave_H5S_ALL,
522  octave_H5P_DEFAULT, itmp) >= 0;
523  H5Dclose (data_hid);
524  if (! retval)
525  {
526  H5Sclose (space_hid);
527  H5Gclose (group_hid);
528  return false;
529  }
530 
531  H5Sclose (space_hid);
532 
533  hdims[0] = m.nnz ();
534  hdims[1] = 1;
535 
536  space_hid = H5Screate_simple (2, hdims, nullptr);
537 
538  if (space_hid < 0)
539  {
540  H5Gclose (group_hid);
541  return false;
542  }
543 #if defined (HAVE_HDF5_18)
544  data_hid = H5Dcreate (group_hid, "ridx", H5T_NATIVE_IDX, space_hid,
546 #else
547  data_hid = H5Dcreate (group_hid, "ridx", H5T_NATIVE_IDX, space_hid,
549 #endif
550  if (data_hid < 0)
551  {
552  H5Sclose (space_hid);
553  H5Gclose (group_hid);
554  return false;
555  }
556 
557  itmp = m.xridx ();
558  retval = H5Dwrite (data_hid, H5T_NATIVE_IDX, octave_H5S_ALL, octave_H5S_ALL,
559  octave_H5P_DEFAULT, itmp) >= 0;
560  H5Dclose (data_hid);
561  if (! retval)
562  {
563  H5Sclose (space_hid);
564  H5Gclose (group_hid);
565  return false;
566  }
567 
568  hid_t save_type_hid = H5T_NATIVE_DOUBLE;
569 
570  if (save_as_floats)
571  {
572  if (m.too_large_for_float ())
573  {
574  warning ("save: some values too large to save as floats --");
575  warning ("save: saving as doubles instead");
576  }
577  else
578  save_type_hid = H5T_NATIVE_FLOAT;
579  }
580 #if defined (HAVE_HDF5_INT2FLOAT_CONVERSIONS)
581  // hdf5 currently doesn't support float/integer conversions
582  else
583  {
584  double max_val, min_val;
585 
586  if (m.all_integers (max_val, min_val))
587  save_type_hid
588  = save_type_to_hdf5 (get_save_type (max_val, min_val));
589  }
590 #endif
591 
592 #if defined (HAVE_HDF5_18)
593  data_hid = H5Dcreate (group_hid, "data", save_type_hid, space_hid,
595 #else
596  data_hid = H5Dcreate (group_hid, "data", save_type_hid, space_hid,
598 #endif
599  if (data_hid < 0)
600  {
601  H5Sclose (space_hid);
602  H5Gclose (group_hid);
603  return false;
604  }
605 
606  double *dtmp = m.xdata ();
607  retval = H5Dwrite (data_hid, H5T_NATIVE_DOUBLE, octave_H5S_ALL, octave_H5S_ALL,
608  octave_H5P_DEFAULT, dtmp) >= 0;
609  H5Dclose (data_hid);
610  H5Sclose (space_hid);
611  H5Gclose (group_hid);
612 
613 #else
614  octave_unused_parameter (loc_id);
615  octave_unused_parameter (name);
616  octave_unused_parameter (save_as_floats);
617 
618  warn_save ("hdf5");
619 #endif
620 
621  return retval;
622 }
623 
624 bool
626 {
627  bool retval = false;
628 
629 #if defined (HAVE_HDF5)
630 
631  octave_idx_type nr, nc, nz;
632  hid_t group_hid, data_hid, space_hid;
633  hsize_t rank;
634 
635  dim_vector dv;
636  int empty = load_hdf5_empty (loc_id, name, dv);
637  if (empty > 0)
638  matrix.resize (dv);
639  if (empty)
640  return (empty > 0);
641 
642 #if defined (HAVE_HDF5_18)
643  group_hid = H5Gopen (loc_id, name, octave_H5P_DEFAULT);
644 #else
645  group_hid = H5Gopen (loc_id, name);
646 #endif
647  if (group_hid < 0) return false;
648 
649 #if defined (HAVE_HDF5_18)
650  data_hid = H5Dopen (group_hid, "nr", octave_H5P_DEFAULT);
651 #else
652  data_hid = H5Dopen (group_hid, "nr");
653 #endif
654  space_hid = H5Dget_space (data_hid);
655  rank = H5Sget_simple_extent_ndims (space_hid);
656 
657  if (rank != 0)
658  {
659  H5Dclose (data_hid);
660  H5Gclose (group_hid);
661  return false;
662  }
663 
664  if (H5Dread (data_hid, H5T_NATIVE_IDX, octave_H5S_ALL, octave_H5S_ALL,
665  octave_H5P_DEFAULT, &nr) < 0)
666  {
667  H5Dclose (data_hid);
668  H5Gclose (group_hid);
669  return false;
670  }
671 
672  H5Dclose (data_hid);
673 
674 #if defined (HAVE_HDF5_18)
675  data_hid = H5Dopen (group_hid, "nc", octave_H5P_DEFAULT);
676 #else
677  data_hid = H5Dopen (group_hid, "nc");
678 #endif
679  space_hid = H5Dget_space (data_hid);
680  rank = H5Sget_simple_extent_ndims (space_hid);
681 
682  if (rank != 0)
683  {
684  H5Dclose (data_hid);
685  H5Gclose (group_hid);
686  return false;
687  }
688 
689  if (H5Dread (data_hid, H5T_NATIVE_IDX, octave_H5S_ALL, octave_H5S_ALL,
690  octave_H5P_DEFAULT, &nc) < 0)
691  {
692  H5Dclose (data_hid);
693  H5Gclose (group_hid);
694  return false;
695  }
696 
697  H5Dclose (data_hid);
698 
699 #if defined (HAVE_HDF5_18)
700  data_hid = H5Dopen (group_hid, "nz", octave_H5P_DEFAULT);
701 #else
702  data_hid = H5Dopen (group_hid, "nz");
703 #endif
704  space_hid = H5Dget_space (data_hid);
705  rank = H5Sget_simple_extent_ndims (space_hid);
706 
707  if (rank != 0)
708  {
709  H5Dclose (data_hid);
710  H5Gclose (group_hid);
711  return false;
712  }
713 
714  if (H5Dread (data_hid, H5T_NATIVE_IDX, octave_H5S_ALL, octave_H5S_ALL,
715  octave_H5P_DEFAULT, &nz) < 0)
716  {
717  H5Dclose (data_hid);
718  H5Gclose (group_hid);
719  return false;
720  }
721 
722  H5Dclose (data_hid);
723 
724  SparseMatrix m (static_cast<octave_idx_type> (nr),
725  static_cast<octave_idx_type> (nc),
726  static_cast<octave_idx_type> (nz));
727 
728 #if defined (HAVE_HDF5_18)
729  data_hid = H5Dopen (group_hid, "cidx", octave_H5P_DEFAULT);
730 #else
731  data_hid = H5Dopen (group_hid, "cidx");
732 #endif
733  space_hid = H5Dget_space (data_hid);
734  rank = H5Sget_simple_extent_ndims (space_hid);
735 
736  if (rank != 2)
737  {
738  H5Sclose (space_hid);
739  H5Dclose (data_hid);
740  H5Gclose (group_hid);
741  return false;
742  }
743 
744  OCTAVE_LOCAL_BUFFER (hsize_t, hdims, rank);
745  OCTAVE_LOCAL_BUFFER (hsize_t, maxdims, rank);
746 
747  H5Sget_simple_extent_dims (space_hid, hdims, maxdims);
748 
749  if (static_cast<int> (hdims[0]) != nc + 1
750  || static_cast<int> (hdims[1]) != 1)
751  {
752  H5Sclose (space_hid);
753  H5Dclose (data_hid);
754  H5Gclose (group_hid);
755  return false;
756  }
757 
758  octave_idx_type *itmp = m.xcidx ();
759  if (H5Dread (data_hid, H5T_NATIVE_IDX, octave_H5S_ALL, octave_H5S_ALL,
760  octave_H5P_DEFAULT, itmp) < 0)
761  {
762  H5Sclose (space_hid);
763  H5Dclose (data_hid);
764  H5Gclose (group_hid);
765  return false;
766  }
767 
768  H5Sclose (space_hid);
769  H5Dclose (data_hid);
770 
771 #if defined (HAVE_HDF5_18)
772  data_hid = H5Dopen (group_hid, "ridx", octave_H5P_DEFAULT);
773 #else
774  data_hid = H5Dopen (group_hid, "ridx");
775 #endif
776  space_hid = H5Dget_space (data_hid);
777  rank = H5Sget_simple_extent_ndims (space_hid);
778 
779  if (rank != 2)
780  {
781  H5Sclose (space_hid);
782  H5Dclose (data_hid);
783  H5Gclose (group_hid);
784  return false;
785  }
786 
787  H5Sget_simple_extent_dims (space_hid, hdims, maxdims);
788 
789  if (static_cast<int> (hdims[0]) != nz || static_cast<int> (hdims[1]) != 1)
790  {
791  H5Sclose (space_hid);
792  H5Dclose (data_hid);
793  H5Gclose (group_hid);
794  return false;
795  }
796 
797  itmp = m.xridx ();
798  if (H5Dread (data_hid, H5T_NATIVE_IDX, octave_H5S_ALL, octave_H5S_ALL,
799  octave_H5P_DEFAULT, itmp) < 0)
800  {
801  H5Sclose (space_hid);
802  H5Dclose (data_hid);
803  H5Gclose (group_hid);
804  return false;
805  }
806 
807  H5Sclose (space_hid);
808  H5Dclose (data_hid);
809 
810 #if defined (HAVE_HDF5_18)
811  data_hid = H5Dopen (group_hid, "data", octave_H5P_DEFAULT);
812 #else
813  data_hid = H5Dopen (group_hid, "data");
814 #endif
815  space_hid = H5Dget_space (data_hid);
816  rank = H5Sget_simple_extent_ndims (space_hid);
817 
818  if (rank != 2)
819  {
820  H5Sclose (space_hid);
821  H5Dclose (data_hid);
822  H5Gclose (group_hid);
823  return false;
824  }
825 
826  H5Sget_simple_extent_dims (space_hid, hdims, maxdims);
827 
828  if (static_cast<int> (hdims[0]) != nz || static_cast<int> (hdims[1]) != 1)
829  {
830  H5Sclose (space_hid);
831  H5Dclose (data_hid);
832  H5Gclose (group_hid);
833  return false;
834  }
835 
836  double *dtmp = m.xdata ();
837 
838  if (H5Dread (data_hid, H5T_NATIVE_DOUBLE, octave_H5S_ALL, octave_H5S_ALL,
839  octave_H5P_DEFAULT, dtmp) >= 0
840  && m.indices_ok ())
841  {
842  retval = true;
843  matrix = m;
844  }
845 
846  H5Sclose (space_hid);
847  H5Dclose (data_hid);
848  H5Gclose (group_hid);
849 
850 #else
851  octave_unused_parameter (loc_id);
852  octave_unused_parameter (name);
853 
854  warn_load ("hdf5");
855 #endif
856 
857  return retval;
858 }
859 
860 mxArray *
862 {
863  mwSize nz = nzmax ();
864  mwSize nr = rows ();
865  mwSize nc = columns ();
866  mxArray *retval = new mxArray (mxDOUBLE_CLASS, nr, nc, nz, mxREAL);
867  double *pr = static_cast<double *> (retval->get_data ());
868  mwIndex *ir = retval->get_ir ();
869  mwIndex *jc = retval->get_jc ();
870 
871  for (mwIndex i = 0; i < nz; i++)
872  {
873  pr[i] = matrix.data (i);
874  ir[i] = matrix.ridx (i);
875  }
876 
877  for (mwIndex i = 0; i < nc + 1; i++)
878  jc[i] = matrix.cidx (i);
879 
880  return retval;
881 }
882 
885 {
886  switch (umap)
887  {
888  case umap_imag:
889  return SparseMatrix (matrix.rows (), matrix.cols (), 0.0);
890 
891  case umap_real:
892  case umap_conj:
893  return matrix;
894 
895  // Mappers handled specially.
896 #define ARRAY_METHOD_MAPPER(UMAP, FCN) \
897  case umap_ ## UMAP: \
898  return octave_value (matrix.FCN ())
899 
901 
902 #define ARRAY_MAPPER(UMAP, TYPE, FCN) \
903  case umap_ ## UMAP: \
904  return octave_value (matrix.map<TYPE> (FCN))
905 
908  ARRAY_MAPPER (angle, double, std::arg);
909  ARRAY_MAPPER (arg, double,std::arg);
912  ARRAY_MAPPER (atan, double, ::atan);
924  ARRAY_MAPPER (ceil, double, ::ceil);
925  ARRAY_MAPPER (cos, double, ::cos);
926  ARRAY_MAPPER (cosh, double, ::cosh);
927  ARRAY_MAPPER (exp, double, ::exp);
930  ARRAY_MAPPER (floor, double, ::floor);
938  ARRAY_MAPPER (sin, double, ::sin);
939  ARRAY_MAPPER (sinh, double, ::sinh);
941  ARRAY_MAPPER (tan, double, ::tan);
942  ARRAY_MAPPER (tanh, double, ::tanh);
947 
948  default: // Attempt to go via dense matrix.
950  }
951 }
save_type
Definition: data-conv.h:85
octave_idx_type write(const octave_value &data, octave_idx_type block_size, oct_data_conv::data_type output_type, octave_idx_type skip, mach_info::float_format flt_fmt)
Definition: oct-stream.cc:6704
double erfcx(double x)
Definition: lo-specfun.cc:1756
bool any_element_is_nan(void) const
Definition: dSparse.cc:7252
bool load_hdf5(octave_hdf5_id loc_id, const char *name)
octave_value convert_to_str_internal(bool pad, bool force, char type) const
#define ARRAY_MAPPER(UMAP, TYPE, FCN)
void resize(octave_idx_type r, octave_idx_type c)
Definition: Sparse.cc:950
T * data(void)
Definition: Sparse.h:486
Complex rc_log10(double x)
Definition: lo-mappers.cc:307
bool too_large_for_float(void) const
Definition: dSparse.cc:7357
bool load_binary(std::istream &is, bool swap, octave::mach_info::float_format fmt)
ComplexMatrix complex_matrix_value(bool=false) const
OCTAVE_IDX_TYPE mwSize
Definition: mxarray.in.h:93
octave_value map(unary_mapper_t umap) const
double double_value(bool=false) const
Definition: ov-re-sparse.cc:96
bool save_hdf5(octave_hdf5_id loc_id, const char *name, bool save_as_floats)
octave_idx_type nnz(void) const
Complex rc_acosh(double x)
Definition: lo-mappers.cc:241
Complex rc_sqrt(double x)
Definition: lo-mappers.cc:322
std::complex< double > erfi(std::complex< double > z, double relerr=0)
void warn_logical_conversion(void)
Definition: errwarn.cc:359
const octave_hdf5_id octave_H5S_ALL
double erfi(double x)
Definition: lo-specfun.cc:1775
save_type get_save_type(double, double)
Definition: ls-utils.cc:35
SparseBoolMatrix sparse_bool_matrix_value(bool warn=false) const
double asinh(double x)
Definition: lo-specfun.h:67
octave_idx_type * cidx(void)
Definition: Sparse.h:508
bool isna(double x)
Definition: lo-mappers.cc:45
Complex rc_lgamma(double x)
Definition: lo-specfun.cc:2211
OCTAVE_EXPORT octave_value_list or N dimensional array whose elements are all equal to the base of natural logarithms The constant ex $e satisfies the equation log(e)
bool isnan(bool)
Definition: lo-mappers.h:187
std::complex< T > ceil(const std::complex< T > &x)
Definition: lo-mappers.h:112
void err_invalid_index(const std::string &idx, octave_idx_type nd, octave_idx_type dim, const std::string &)
void error(const char *fmt,...)
Definition: error.cc:578
bool isinf(double x)
Definition: lo-mappers.h:225
std::complex< T > floor(const std::complex< T > &x)
Definition: lo-mappers.h:139
charNDArray char_array_value(bool=false) const
static T abs(T x)
Definition: pr-output.cc:1696
void write_doubles(std::ostream &os, const double *data, save_type type, octave_idx_type len)
Definition: data-conv.cc:887
#define DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA(t, n, c)
Definition: ov-base.h:180
int load_hdf5_empty(octave_hdf5_id loc_id, const char *name, dim_vector &d)
Definition: ls-hdf5.cc:953
Complex acos(const Complex &x)
Definition: lo-mappers.cc:83
Complex atan(const Complex &x)
Definition: lo-mappers.h:80
bool any_element_not_one_or_zero(void) const
Definition: dSparse.cc:7282
double fix(double x)
Definition: lo-mappers.h:127
s
Definition: file-io.cc:2729
Complex asin(const Complex &x)
Definition: lo-mappers.cc:105
void err_nan_to_logical_conversion(void)
Complex log2(const Complex &x)
Definition: lo-mappers.cc:137
Complex erfc(const Complex &x)
Definition: lo-specfun.cc:1653
std::complex< double > erf(std::complex< double > z, double relerr=0)
octave_value arg
Definition: pr-output.cc:3244
F77_RET_T const F77_REAL const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE &F77_RET_T const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
ComplexNDArray complex_array_value(bool=false) const
Matrix matrix_value(bool=false) const
void warn_load(const char *type) const
Definition: ov-base.cc:1097
bool swap
Definition: load-save.cc:738
Complex rc_atanh(double x)
Definition: lo-mappers.cc:266
octave_idx_type nnz(void) const
Actual number of nonzero terms.
Definition: Sparse.h:240
idx_vector index_vector(bool require_integers=false) const
Definition: ov-re-sparse.cc:58
double lgamma(double x)
Definition: lo-specfun.h:377
bool all_integers(double &max_val, double &min_val) const
Definition: dSparse.cc:7329
Complex complex_value(bool=false) const
octave_idx_type numel(void) const
Definition: Sparse.h:244
Complex expm1(const Complex &x)
Definition: lo-specfun.cc:1875
mxArray * as_mxArray(void) const
create a structure array and initialize its values The dimensions of each cell array of values must match Singleton cells and non cell values are repeated so that they fill the entire array If the cells are empty
Definition: ov-struct.cc:1736
nd deftypefn *std::string name
Definition: sysdep.cc:647
double acosh(double x)
Definition: lo-specfun.h:49
OCTAVE_EXPORT octave_value_list isdir nd deftypefn *std::string nm
Definition: utils.cc:975
double gamma(double x)
Definition: lo-specfun.cc:1913
void swap_bytes< 4 >(void *ptr)
Definition: byte-swap.h:60
bool save_binary(std::ostream &os, bool &save_as_floats)
void read_doubles(std::istream &is, double *data, save_type type, octave_idx_type len, bool swap, octave::mach_info::float_format fmt)
Definition: data-conv.cc:772
Complex log1p(const Complex &x)
Definition: lo-specfun.cc:1959
octave_idx_type rows(void) const
Definition: ov-base.h:316
double signum(double x)
Definition: lo-mappers.h:244
double dawson(double x)
Definition: lo-specfun.cc:1518
bool save_as_floats
Definition: load-save.cc:1617
Complex rc_log2(double x)
Definition: lo-mappers.cc:292
octave_hdf5_id save_type_to_hdf5(save_type st)
Definition: ls-hdf5.cc:997
double tmp
Definition: data.cc:6252
octave_value retval
Definition: data.cc:6246
Complex rc_log(double x)
Definition: lo-mappers.cc:279
std::complex< double > erfcx(std::complex< double > z, double relerr=0)
octave_idx_type * ridx(void)
Definition: Sparse.h:495
int64_t octave_hdf5_id
octave_idx_type * xridx(void)
Definition: Sparse.h:501
double atanh(double x)
Definition: lo-specfun.h:72
boolMatrix mx_el_ne(const boolMatrix &m1, const boolMatrix &m2)
Definition: boolMatrix.cc:89
void warn_save(const char *type) const
Definition: ov-base.cc:1106
idx type
Definition: ov.cc:3114
Definition: dMatrix.h:36
Complex erf(const Complex &x)
Definition: lo-specfun.cc:1638
double cbrt(double x)
Definition: lo-specfun.h:298
double erfcinv(double x)
Definition: lo-specfun.cc:1745
void mxArray
Definition: mex.h:55
friend class octave_value
Definition: ov-base.h:228
Complex rc_acos(double x)
Definition: lo-mappers.cc:228
void warning(const char *fmt,...)
Definition: error.cc:801
octave_idx_type cols(void) const
Definition: Sparse.h:259
charNDArray max(char d, const charNDArray &m)
Definition: chNDArray.cc:227
int save_hdf5_empty(octave_hdf5_id loc_id, const char *name, const dim_vector &d)
Definition: ls-hdf5.cc:897
void err_invalid_conversion(const std::string &from, const std::string &to)
Definition: errwarn.cc:68
octave_value map(octave_base_value::unary_mapper_t umap) const
Matrix matrix_value(void) const
Definition: dSparse.cc:7478
octave_idx_type numel(int n=0) const
Number of elements that a matrix with this dimensions would have.
Definition: dim-vector.h:362
bool any_element_not_one_or_zero(void) const
Definition: dNDArray.cc:565
bool indices_ok(void) const
Definition: Sparse.h:661
#define ARRAY_METHOD_MAPPER(UMAP, FCN)
T * xdata(void)
Definition: Sparse.h:488
octave_idx_type numel(void) const
octave_idx_type nzmax(void) const
void warn_implicit_conversion(const char *id, const char *from, const char *to)
Definition: errwarn.cc:338
Complex rc_asin(double x)
Definition: lo-mappers.cc:253
bool isfinite(double x)
Definition: lo-mappers.h:201
double roundb(double x)
Definition: lo-mappers.h:156
Complex rc_log1p(double x)
Definition: lo-specfun.cc:2248
#define H5T_NATIVE_IDX
Definition: oct-hdf5.h:39
#define OCTAVE_LOCAL_BUFFER(T, buf, size)
Definition: oct-locbuf.h:41
Sparse< T > maybe_compress(bool remove_zeros=false)
Definition: Sparse.h:438
double erfinv(double x)
Definition: lo-specfun.cc:1864
bool Vsparse_auto_mutate
Definition: ov-base.cc:101
double round(double x)
Definition: lo-mappers.h:145
NDArray array_value(bool=false) const
for i
Definition: data.cc:5264
const octave_hdf5_id octave_H5P_DEFAULT
octave_idx_type columns(void) const
Definition: ov-base.h:323
bool isempty(void) const
Definition: ov-base.h:359
octave_value as_double(void) const
octave_idx_type ndims(void) const
Number of dimensions.
Definition: dim-vector.h:295
boolNDArray bool_array_value(bool warn=false) const
std::complex< double > Complex
Definition: oct-cmplx.h:31
size_t byte_size(void) const
Definition: Sparse.h:271
octave_idx_type * xcidx(void)
Definition: Sparse.h:514
OCTAVE_IDX_TYPE mwIndex
Definition: mxarray.in.h:94
write the output to stdout if nargout is
Definition: load-save.cc:1612
void err_nan_to_character_conversion(void)
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:87
std::string type_name(void) const
Definition: ov-re-sparse.h:160
If this string is the system will ring the terminal sometimes it is useful to be able to print the original representation of the string
Definition: utils.cc:888
SparseMatrix sparse_matrix_value(bool=false) const
Definition: ov-re-sparse.h:125
dim_vector dv
Definition: sub2ind.cc:263
octave::stream os
Definition: file-io.cc:627
octave_idx_type rows(void) const
Definition: Sparse.h:258
octave_base_value * try_narrowing_conversion(void)
Definition: ov-re-sparse.cc:70
bool any_element_is_nan(void) const
Definition: dNDArray.cc:553
int nint(double x)
Definition: lo-mappers.cc:206
std::complex< double > erfc(std::complex< double > z, double relerr=0)