GNU Octave  4.2.1
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-2017 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 #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 
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 {
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)))
89  retval = new octave_matrix (matrix.matrix_value ());
90  }
91 
92  return retval;
93 }
94 
95 double
97 {
98  if (is_empty ())
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 {
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, 0);
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, 0);
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, 0);
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, octave::math::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);
944  ARRAY_MAPPER (isna, bool, octave::math::is_NA);
946  ARRAY_MAPPER (isfinite, bool, octave::math::finite);
947 
948  default: // Attempt to go via dense matrix.
950  }
951 }
save_type
Definition: data-conv.h:85
octave_idx_type * xridx(void)
Definition: Sparse.h:536
bool load_hdf5(octave_hdf5_id loc_id, const char *name)
#define ARRAY_MAPPER(UMAP, TYPE, FCN)
octave_idx_type cols(void) const
Definition: Sparse.h:272
Complex rc_log10(double x)
Definition: lo-mappers.cc:536
std::string type_name(void) const
Definition: ov-re-sparse.h:162
Complex complex_value(bool=false) const
T * data(void)
Definition: Sparse.h:521
SparseMatrix sparse_matrix_value(bool=false) const
Definition: ov-re-sparse.h:127
octave_idx_type rows(void) const
Definition: Sparse.h:271
bool load_binary(std::istream &is, bool swap, octave::mach_info::float_format fmt)
octave_idx_type numel(void) const
Definition: Sparse.h:257
ComplexNDArray complex_array_value(bool=false) const
float erfcx(float x)
Definition: lo-specfun.cc:271
octave_idx_type columns(void) const
Definition: ov-base.h:319
function erf(X)
Definition: erf.f:2
bool save_hdf5(octave_hdf5_id loc_id, const char *name, bool save_as_floats)
Complex rc_acosh(double x)
Definition: lo-mappers.cc:468
Complex rc_sqrt(double x)
Definition: lo-mappers.cc:551
std::complex< double > erfi(std::complex< double > z, double relerr=0)
void warn_logical_conversion(void)
Definition: errwarn.cc:353
const octave_hdf5_id octave_H5S_ALL
int save_hdf5_empty(octave_hdf5_id loc_id, const char *name, const dim_vector d)
Definition: ls-hdf5.cc:897
save_type get_save_type(double, double)
Definition: ls-utils.cc:35
bool isnan(double x)
Definition: lo-mappers.cc:347
octave_value convert_to_str_internal(bool pad, bool force, char type) const
Complex rc_lgamma(double x)
Definition: lo-specfun.cc:373
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)
double ceil(double x)
Definition: lo-mappers.h:138
function atanh(X)
Definition: atanh.f:2
octave_idx_type * xcidx(void)
Definition: Sparse.h:549
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:570
void * get_data(void) const
Definition: mxarray.h:449
void write_doubles(std::ostream &os, const double *data, save_type type, octave_idx_type len)
Definition: data-conv.cc:886
#define DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA(t, n, c)
Definition: ov-base.h:169
octave_idx_type * cidx(void)
Definition: Sparse.h:543
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:90
double expm1(double x)
Definition: lo-specfun.cc:473
double asinh(double x)
Definition: lo-specfun.cc:105
bool any_element_is_nan(void) const
Definition: dNDArray.cc:555
double fix(double x)
Definition: lo-mappers.h:158
s
Definition: file-io.cc:2682
Complex asin(const Complex &x)
Definition: lo-mappers.cc:150
bool indices_ok(void) const
Definition: Sparse.h:696
void err_nan_to_logical_conversion(void)
double cbrt(double x)
Definition: lo-specfun.cc:648
octave_idx_type nzmax(void) const
double arg(double x)
Definition: lo-mappers.h:83
bool is_empty(void) const
Definition: ov-base.h:355
octave_value arg
Definition: pr-output.cc:3440
octave_idx_type numel(int n=0) const
Number of elements that a matrix with this dimensions would have.
Definition: dim-vector.h:389
double round(double x)
Definition: lo-mappers.cc:333
bool is_NA(double x)
Definition: lo-mappers.cc:54
F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &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 F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T F77_REAL F77_REAL &F77_RET_T F77_DBLE F77_DBLE &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
octave_value map(unary_mapper_t umap) const
boolNDArray bool_array_value(bool warn=false) const
octave_idx_type nnz(void) const
Actual number of nonzero terms.
Definition: Sparse.h:253
bool swap
Definition: load-save.cc:725
Complex rc_atanh(double x)
Definition: lo-mappers.cc:493
bool any_element_not_one_or_zero(void) const
Definition: dNDArray.cc:567
float erfi(float x)
Definition: lo-specfun.cc:289
void warn_load(const char *type) const
Definition: ov-base.cc:1151
mwIndex * get_ir(void) const
Definition: mxarray.h:458
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:1688
OCTAVE_EXPORT octave_value_list any number nd example oindent prints the prompt xample Pick a any number!nd example oindent and waits for the user to enter a value The string entered by the user is evaluated as an so it may be a literal a variable name
Definition: input.cc:871
OCTAVE_EXPORT octave_value_list isdir nd deftypefn *std::string nm
Definition: utils.cc:941
double gamma(double x)
Definition: lo-specfun.cc:325
void swap_bytes< 4 >(void *ptr)
Definition: byte-swap.h:60
Complex atan(const Complex &x)
Definition: lo-mappers.cc:210
bool save_binary(std::ostream &os, bool &save_as_floats)
#define OCTINTERP_API
Definition: mexproto.h:69
bool all_integers(double &max_val, double &min_val) const
Definition: dSparse.cc:7287
Matrix matrix_value(void) const
Definition: dSparse.cc:7436
octave_idx_type numel(void) const
function asinh(X)
Definition: asinh.f:2
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:771
nd deftypefn *octave_map m
Definition: ov-struct.cc:2058
Sparse< T > maybe_compress(bool remove_zeros=false)
Definition: Sparse.h:481
void resize(octave_idx_type r, octave_idx_type c)
Definition: Sparse.cc:948
double signum(double x)
Definition: lo-mappers.h:259
float dawson(float x)
Definition: lo-specfun.cc:307
octave_value map(octave_base_value::unary_mapper_t umap) const
bool save_as_floats
Definition: load-save.cc:1581
Complex rc_log2(double x)
Definition: lo-mappers.cc:521
octave_hdf5_id save_type_to_hdf5(save_type st)
Definition: ls-hdf5.cc:997
double tmp
Definition: data.cc:6300
mxArray * as_mxArray(void) const
size_t byte_size(void) const
Definition: Sparse.h:284
octave_value retval
Definition: data.cc:6294
Complex rc_log(double x)
Definition: lo-mappers.cc:506
bool too_large_for_float(void) const
Definition: dSparse.cc:7315
bool finite(double x)
Definition: lo-mappers.cc:367
double double_value(bool=false) const
Definition: ov-re-sparse.cc:96
std::complex< double > erfcx(std::complex< double > z, double relerr=0)
int64_t octave_hdf5_id
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:7240
boolMatrix mx_el_ne(const boolMatrix &m1, const boolMatrix &m2)
Definition: boolMatrix.cc:90
idx type
Definition: ov.cc:3129
Definition: dMatrix.h:37
void warn_save(const char *type) const
Definition: ov-base.cc:1160
double erfcinv(double x)
Definition: lo-specfun.cc:3049
void mxArray
Definition: mex.h:55
bool isinf(double x)
Definition: lo-mappers.cc:387
function acosh(X)
Definition: acosh.f:2
friend class octave_value
Definition: ov-base.h:211
Complex rc_acos(double x)
Definition: lo-mappers.cc:455
void warning(const char *fmt,...)
Definition: error.cc:788
double erf(double x)
Definition: lo-specfun.cc:193
charNDArray max(char d, const charNDArray &m)
Definition: chNDArray.cc:228
void err_invalid_conversion(const std::string &from, const std::string &to)
Definition: errwarn.cc:68
octave_idx_type * ridx(void)
Definition: Sparse.h:530
Matrix matrix_value(bool=false) const
=val(i)}if ode{val(i)}occurs in table i
Definition: lookup.cc:239
double log1p(double x)
Definition: lo-specfun.cc:587
OCTAVE_EXPORT octave_value_list return the value of the option it must match the dimension of the state and the relative tolerance must also be a vector of the same length tem it must match the dimension of the state and the absolute tolerance must also be a vector of the same length The local error test applied at each integration step is xample roup abs(local error in x(i))<
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:523
void warn_implicit_conversion(const char *id, const char *from, const char *to)
Definition: errwarn.cc:332
issues an error eealso double
Definition: ov-bool-mat.cc:594
Complex rc_asin(double x)
Definition: lo-mappers.cc:480
double roundb(double x)
Definition: lo-mappers.h:189
octave_idx_type ndims(void) const
Number of dimensions.
Definition: dim-vector.h:301
Complex rc_log1p(double x)
Definition: lo-specfun.cc:2872
NDArray array_value(bool=false) const
#define H5T_NATIVE_IDX
Definition: oct-hdf5.h:39
#define OCTAVE_LOCAL_BUFFER(T, buf, size)
Definition: oct-locbuf.h:200
mwIndex * get_jc(void) const
Definition: mxarray.h:460
double erfinv(double x)
Definition: lo-specfun.cc:2960
bool any_element_is_nan(void) const
Definition: dSparse.cc:7210
bool Vsparse_auto_mutate
Definition: ov-base.cc:119
double floor(double x)
Definition: lo-mappers.cc:330
const octave_hdf5_id octave_H5P_DEFAULT
double erfc(double x)
Definition: lo-specfun.cc:232
ComplexMatrix complex_matrix_value(bool=false) const
double lgamma(double x)
Definition: lo-specfun.cc:353
Definition: mxarray.h:76
std::complex< double > Complex
Definition: oct-cmplx.h:31
write the output to stdout if nargout is
Definition: load-save.cc:1576
void err_nan_to_character_conversion(void)
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:87
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:854
dim_vector dv
Definition: sub2ind.cc:263
octave_idx_type nnz(void) const
double log2(double x)
Definition: lo-mappers.cc:233
octave_idx_type rows(void) const
Definition: ov-base.h:312
octave_base_value * try_narrowing_conversion(void)
Definition: ov-re-sparse.cc:70
charNDArray char_array_value(bool=false) const
int nint(double x)
Definition: lo-mappers.cc:433
std::complex< double > erfc(std::complex< double > z, double relerr=0)
octave_value as_double(void) const