GNU Octave  9.1.0
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) 1998-2024 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if defined (HAVE_CONFIG_H)
27 # include "config.h"
28 #endif
29 
30 #include <istream>
31 #include <limits>
32 #include <ostream>
33 #include <vector>
34 
35 #include "lo-specfun.h"
36 #include "lo-mappers.h"
37 #include "oct-locbuf.h"
38 
39 #include "mxarray.h"
40 #include "ov-base.h"
41 #include "ov-scalar.h"
42 #include "errwarn.h"
43 
44 #include "oct-hdf5.h"
45 #include "ls-hdf5.h"
46 
47 #include "ov-re-sparse.h"
48 
49 #include "ov-base-sparse.h"
50 #include "ov-base-sparse.cc"
51 
52 #include "ov-bool-sparse.h"
53 
54 
56 
58  "double");
59 
61 octave_sparse_matrix::index_vector (bool /* require_integers */) const
62 {
63  if (matrix.numel () == matrix.nnz ())
64  return octave::idx_vector (array_value ());
65  else
66  {
67  std::string nm = '<' + type_name () + '>';
68  octave::err_invalid_index (nm.c_str ());
69  }
70 }
71 
72 double
74 {
75  if (isempty ())
76  err_invalid_conversion ("real sparse matrix", "real scalar");
77 
78  if (numel () > 1)
79  warn_implicit_conversion ("Octave:array-to-scalar",
80  "real sparse matrix", "real scalar");
81 
82  return matrix(0, 0);
83 }
84 
85 Complex
87 {
88  // FIXME: maybe this should be a function, valid_as_scalar()
89  if (rows () == 0 || columns () == 0)
90  err_invalid_conversion ("real sparse matrix", "complex scalar");
91 
92  if (numel () > 1)
93  warn_implicit_conversion ("Octave:array-to-scalar",
94  "real sparse matrix", "complex scalar");
95 
96  return Complex (matrix(0, 0), 0);
97 }
98 
99 Matrix
101 {
102  return matrix.matrix_value ();
103 }
104 
107 {
109 
110  if (m.any_element_is_nan ())
112  if (warn && m.any_element_not_one_or_zero ())
114 
115  return boolNDArray (m);
116 }
117 
120 {
121  charNDArray retval (dims (), 0);
122  octave_idx_type nc = matrix.cols ();
123  octave_idx_type nr = matrix.rows ();
124 
125  for (octave_idx_type j = 0; j < nc; j++)
126  for (octave_idx_type i = matrix.cidx (j); i < matrix.cidx (j+1); i++)
127  retval(matrix.ridx (i) + nr * j) = static_cast<char> (matrix.data (i));
128 
129  return retval;
130 }
131 
134 {
135  return ComplexMatrix (matrix.matrix_value ());
136 }
137 
140 {
142 }
143 
144 NDArray
146 {
147  return NDArray (matrix.matrix_value ());
148 }
149 
152 {
153  if (matrix.any_element_is_nan ())
155  if (warn && matrix.any_element_not_one_or_zero ())
157 
158  return mx_el_ne (matrix, 0.0);
159 }
160 
163 {
164  octave_value retval;
165  dim_vector dv = dims ();
166  octave_idx_type nel = dv.numel ();
167 
168  if (nel == 0)
169  {
170  char s = '\0';
171  retval = octave_value (&s, type);
172  }
173  else
174  {
175  octave_idx_type nr = matrix.rows ();
176  octave_idx_type nc = matrix.cols ();
177  charNDArray chm (dv, static_cast<char> (0));
178 
179  bool warned = false;
180 
181  for (octave_idx_type j = 0; j < nc; j++)
182  for (octave_idx_type i = matrix.cidx (j);
183  i < matrix.cidx (j+1); i++)
184  {
185  octave_quit ();
186 
187  double d = matrix.data (i);
188 
189  if (octave::math::isnan (d))
191 
192  int ival = octave::math::nint (d);
193 
194  if (ival < 0 || ival > std::numeric_limits<unsigned char>::max ())
195  {
196  // FIXME: is there something better we could do?
197 
198  ival = 0;
199 
200  if (! warned)
201  {
202  ::warning ("range error for conversion to character value");
203  warned = true;
204  }
205  }
206 
207  chm(matrix.ridx (i) + j * nr) = static_cast<char> (ival);
208  }
209 
210  retval = octave_value (chm, type);
211  }
212 
213  return retval;
214 }
215 
218 {
219  return this->matrix;
220 }
221 
222 bool
223 octave_sparse_matrix::save_binary (std::ostream& os, bool save_as_floats)
224 {
225  dim_vector dv = this->dims ();
226  if (dv.ndims () < 1)
227  return false;
228 
229  // Ensure that additional memory is deallocated
231 
232  int nr = dv(0);
233  int nc = dv(1);
234  int nz = nnz ();
235 
236  int32_t itmp;
237  // Use negative value for ndims to be consistent with other formats
238  itmp = -2;
239  os.write (reinterpret_cast<char *> (&itmp), 4);
240 
241  itmp = nr;
242  os.write (reinterpret_cast<char *> (&itmp), 4);
243 
244  itmp = nc;
245  os.write (reinterpret_cast<char *> (&itmp), 4);
246 
247  itmp = nz;
248  os.write (reinterpret_cast<char *> (&itmp), 4);
249 
250  save_type st = LS_DOUBLE;
251  if (save_as_floats)
252  {
254  {
255  warning ("save: some values too large to save as floats --");
256  warning ("save: saving as doubles instead");
257  }
258  else
259  st = LS_FLOAT;
260  }
261  else if (matrix.nnz () > 8192) // FIXME: make this configurable.
262  {
263  double max_val, min_val;
264  if (matrix.all_integers (max_val, min_val))
265  st = octave::get_save_type (max_val, min_val);
266  }
267 
268  // add one to the printed indices to go from
269  // zero-based to one-based arrays
270  for (int i = 0; i < nc+1; i++)
271  {
272  octave_quit ();
273  itmp = matrix.cidx (i);
274  os.write (reinterpret_cast<char *> (&itmp), 4);
275  }
276 
277  for (int i = 0; i < nz; i++)
278  {
279  octave_quit ();
280  itmp = matrix.ridx (i);
281  os.write (reinterpret_cast<char *> (&itmp), 4);
282  }
283 
284  write_doubles (os, matrix.data (), st, nz);
285 
286  return true;
287 }
288 
289 bool
290 octave_sparse_matrix::load_binary (std::istream& is, bool swap,
292 {
293  int32_t nz, nc, nr, tmp;
294  char ctmp;
295 
296  if (! is.read (reinterpret_cast<char *> (&tmp), 4))
297  return false;
298 
299  if (swap)
300  swap_bytes<4> (&tmp);
301 
302  if (tmp != -2)
303  error ("load: only 2-D sparse matrices are supported");
304 
305  if (! is.read (reinterpret_cast<char *> (&nr), 4))
306  return false;
307  if (! is.read (reinterpret_cast<char *> (&nc), 4))
308  return false;
309  if (! is.read (reinterpret_cast<char *> (&nz), 4))
310  return false;
311 
312  if (swap)
313  {
314  swap_bytes<4> (&nr);
315  swap_bytes<4> (&nc);
316  swap_bytes<4> (&nz);
317  }
318 
319  SparseMatrix m (static_cast<octave_idx_type> (nr),
320  static_cast<octave_idx_type> (nc),
321  static_cast<octave_idx_type> (nz));
322 
323  for (int i = 0; i < nc+1; i++)
324  {
325  octave_quit ();
326  if (! is.read (reinterpret_cast<char *> (&tmp), 4))
327  return false;
328  if (swap)
329  swap_bytes<4> (&tmp);
330  m.xcidx (i) = tmp;
331  }
332 
333  for (int i = 0; i < nz; i++)
334  {
335  octave_quit ();
336  if (! is.read (reinterpret_cast<char *> (&tmp), 4))
337  return false;
338  if (swap)
339  swap_bytes<4> (&tmp);
340  m.xridx (i) = tmp;
341  }
342 
343  if (! is.read (reinterpret_cast<char *> (&ctmp), 1))
344  return false;
345 
346  read_doubles (is, m.xdata (), static_cast<save_type> (ctmp), nz, swap, fmt);
347 
348  if (! is)
349  return false;
350 
351  if (! m.indices_ok ())
352  return false;
353 
354  matrix = m;
355 
356  return true;
357 }
358 
359 bool
361  bool save_as_floats)
362 {
363  bool retval = false;
364 
365 #if defined (HAVE_HDF5)
366 
367  dim_vector dv = dims ();
368  int empty = save_hdf5_empty (loc_id, name, dv);
369  if (empty)
370  return (empty > 0);
371 
372  // Ensure that additional memory is deallocated
374 
375 #if defined (HAVE_HDF5_18)
376  hid_t group_hid = H5Gcreate (loc_id, name, octave_H5P_DEFAULT,
378 #else
379  hid_t group_hid = H5Gcreate (loc_id, name, 0);
380 #endif
381  if (group_hid < 0)
382  return false;
383 
384  hid_t space_hid, data_hid;
385  space_hid = data_hid = -1;
387  octave_idx_type tmp;
388  hsize_t hdims[2];
389 
390  space_hid = H5Screate_simple (0, hdims, nullptr);
391  if (space_hid < 0)
392  {
393  H5Gclose (group_hid);
394  return false;
395  }
396 #if defined (HAVE_HDF5_18)
397  data_hid = H5Dcreate (group_hid, "nr", H5T_NATIVE_IDX, space_hid,
400 #else
401  data_hid = H5Dcreate (group_hid, "nr", H5T_NATIVE_IDX, space_hid,
403 #endif
404  if (data_hid < 0)
405  {
406  H5Sclose (space_hid);
407  H5Gclose (group_hid);
408  return false;
409  }
410 
411  tmp = m.rows ();
412  retval = H5Dwrite (data_hid, H5T_NATIVE_IDX, octave_H5S_ALL, octave_H5S_ALL,
413  octave_H5P_DEFAULT, &tmp) >= 0;
414  H5Dclose (data_hid);
415  if (! retval)
416  {
417  H5Sclose (space_hid);
418  H5Gclose (group_hid);
419  return false;
420  }
421 #if defined (HAVE_HDF5_18)
422  data_hid = H5Dcreate (group_hid, "nc", H5T_NATIVE_IDX, space_hid,
425 #else
426  data_hid = H5Dcreate (group_hid, "nc", H5T_NATIVE_IDX, space_hid,
428 #endif
429  if (data_hid < 0)
430  {
431  H5Sclose (space_hid);
432  H5Gclose (group_hid);
433  return false;
434  }
435 
436  tmp = m.cols ();
437  retval = H5Dwrite (data_hid, H5T_NATIVE_IDX, octave_H5S_ALL, octave_H5S_ALL,
438  octave_H5P_DEFAULT, &tmp) >= 0;
439  H5Dclose (data_hid);
440  if (! retval)
441  {
442  H5Sclose (space_hid);
443  H5Gclose (group_hid);
444  return false;
445  }
446 
447 #if defined (HAVE_HDF5_18)
448  data_hid = H5Dcreate (group_hid, "nz", H5T_NATIVE_IDX, space_hid,
451 #else
452  data_hid = H5Dcreate (group_hid, "nz", H5T_NATIVE_IDX, space_hid,
454 #endif
455  if (data_hid < 0)
456  {
457  H5Sclose (space_hid);
458  H5Gclose (group_hid);
459  return false;
460  }
461 
462  tmp = m.nnz ();
463  retval = H5Dwrite (data_hid, H5T_NATIVE_IDX, octave_H5S_ALL, octave_H5S_ALL,
464  octave_H5P_DEFAULT, &tmp) >= 0;
465  H5Dclose (data_hid);
466  if (! retval)
467  {
468  H5Sclose (space_hid);
469  H5Gclose (group_hid);
470  return false;
471  }
472 
473  H5Sclose (space_hid);
474 
475  hdims[0] = m.cols () + 1;
476  hdims[1] = 1;
477 
478  space_hid = H5Screate_simple (2, hdims, nullptr);
479 
480  if (space_hid < 0)
481  {
482  H5Gclose (group_hid);
483  return false;
484  }
485 
486 #if defined (HAVE_HDF5_18)
487  data_hid = H5Dcreate (group_hid, "cidx", H5T_NATIVE_IDX, space_hid,
490 #else
491  data_hid = H5Dcreate (group_hid, "cidx", H5T_NATIVE_IDX, space_hid,
493 #endif
494  if (data_hid < 0)
495  {
496  H5Sclose (space_hid);
497  H5Gclose (group_hid);
498  return false;
499  }
500 
501  octave_idx_type *itmp = m.xcidx ();
502  retval = H5Dwrite (data_hid, H5T_NATIVE_IDX, octave_H5S_ALL, octave_H5S_ALL,
503  octave_H5P_DEFAULT, itmp) >= 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.nnz ();
515  hdims[1] = 1;
516 
517  space_hid = H5Screate_simple (2, hdims, nullptr);
518 
519  if (space_hid < 0)
520  {
521  H5Gclose (group_hid);
522  return false;
523  }
524 #if defined (HAVE_HDF5_18)
525  data_hid = H5Dcreate (group_hid, "ridx", H5T_NATIVE_IDX, space_hid,
528 #else
529  data_hid = H5Dcreate (group_hid, "ridx", H5T_NATIVE_IDX, space_hid,
531 #endif
532  if (data_hid < 0)
533  {
534  H5Sclose (space_hid);
535  H5Gclose (group_hid);
536  return false;
537  }
538 
539  itmp = m.xridx ();
540  retval = H5Dwrite (data_hid, H5T_NATIVE_IDX, octave_H5S_ALL, octave_H5S_ALL,
541  octave_H5P_DEFAULT, itmp) >= 0;
542  H5Dclose (data_hid);
543  if (! retval)
544  {
545  H5Sclose (space_hid);
546  H5Gclose (group_hid);
547  return false;
548  }
549 
550  hid_t save_type_hid = H5T_NATIVE_DOUBLE;
551 
552  if (save_as_floats)
553  {
554  if (m.too_large_for_float ())
555  {
556  warning ("save: some values too large to save as floats --");
557  warning ("save: saving as doubles instead");
558  }
559  else
560  save_type_hid = H5T_NATIVE_FLOAT;
561  }
562 #if defined (HAVE_HDF5_INT2FLOAT_CONVERSIONS)
563  // hdf5 currently doesn't support float/integer conversions
564  else
565  {
566  double max_val, min_val;
567 
568  if (m.all_integers (max_val, min_val))
569  save_type_hid
570  = save_type_to_hdf5 (octave::get_save_type (max_val, min_val));
571  }
572 #endif
573 
574 #if defined (HAVE_HDF5_18)
575  data_hid = H5Dcreate (group_hid, "data", save_type_hid, space_hid,
578 #else
579  data_hid = H5Dcreate (group_hid, "data", save_type_hid, space_hid,
581 #endif
582  if (data_hid < 0)
583  {
584  H5Sclose (space_hid);
585  H5Gclose (group_hid);
586  return false;
587  }
588 
589  double *dtmp = m.xdata ();
590  retval = H5Dwrite (data_hid, H5T_NATIVE_DOUBLE, octave_H5S_ALL,
591  octave_H5S_ALL, octave_H5P_DEFAULT, dtmp) >= 0;
592  H5Dclose (data_hid);
593  H5Sclose (space_hid);
594  H5Gclose (group_hid);
595 
596 #else
597  octave_unused_parameter (loc_id);
598  octave_unused_parameter (name);
599  octave_unused_parameter (save_as_floats);
600 
601  warn_save ("hdf5");
602 #endif
603 
604  return retval;
605 }
606 
607 bool
609 {
610  bool retval = false;
611 
612 #if defined (HAVE_HDF5)
613 
614  octave_idx_type nr, nc, nz;
615  hid_t group_hid, data_hid, space_hid;
616  hsize_t rank;
617 
618  dim_vector dv;
619  int empty = load_hdf5_empty (loc_id, name, dv);
620  if (empty > 0)
621  matrix.resize (dv);
622  if (empty)
623  return (empty > 0);
624 
625 #if defined (HAVE_HDF5_18)
626  group_hid = H5Gopen (loc_id, name, octave_H5P_DEFAULT);
627 #else
628  group_hid = H5Gopen (loc_id, name);
629 #endif
630  if (group_hid < 0) return false;
631 
632 #if defined (HAVE_HDF5_18)
633  data_hid = H5Dopen (group_hid, "nr", octave_H5P_DEFAULT);
634 #else
635  data_hid = H5Dopen (group_hid, "nr");
636 #endif
637  space_hid = H5Dget_space (data_hid);
638  rank = H5Sget_simple_extent_ndims (space_hid);
639 
640  if (rank != 0)
641  {
642  H5Dclose (data_hid);
643  H5Gclose (group_hid);
644  return false;
645  }
646 
647  if (H5Dread (data_hid, H5T_NATIVE_IDX, octave_H5S_ALL, octave_H5S_ALL,
648  octave_H5P_DEFAULT, &nr) < 0)
649  {
650  H5Dclose (data_hid);
651  H5Gclose (group_hid);
652  return false;
653  }
654 
655  H5Dclose (data_hid);
656 
657 #if defined (HAVE_HDF5_18)
658  data_hid = H5Dopen (group_hid, "nc", octave_H5P_DEFAULT);
659 #else
660  data_hid = H5Dopen (group_hid, "nc");
661 #endif
662  space_hid = H5Dget_space (data_hid);
663  rank = H5Sget_simple_extent_ndims (space_hid);
664 
665  if (rank != 0)
666  {
667  H5Dclose (data_hid);
668  H5Gclose (group_hid);
669  return false;
670  }
671 
672  if (H5Dread (data_hid, H5T_NATIVE_IDX, octave_H5S_ALL, octave_H5S_ALL,
673  octave_H5P_DEFAULT, &nc) < 0)
674  {
675  H5Dclose (data_hid);
676  H5Gclose (group_hid);
677  return false;
678  }
679 
680  H5Dclose (data_hid);
681 
682 #if defined (HAVE_HDF5_18)
683  data_hid = H5Dopen (group_hid, "nz", octave_H5P_DEFAULT);
684 #else
685  data_hid = H5Dopen (group_hid, "nz");
686 #endif
687  space_hid = H5Dget_space (data_hid);
688  rank = H5Sget_simple_extent_ndims (space_hid);
689 
690  if (rank != 0)
691  {
692  H5Dclose (data_hid);
693  H5Gclose (group_hid);
694  return false;
695  }
696 
697  if (H5Dread (data_hid, H5T_NATIVE_IDX, octave_H5S_ALL, octave_H5S_ALL,
698  octave_H5P_DEFAULT, &nz) < 0)
699  {
700  H5Dclose (data_hid);
701  H5Gclose (group_hid);
702  return false;
703  }
704 
705  H5Dclose (data_hid);
706 
707  SparseMatrix m (static_cast<octave_idx_type> (nr),
708  static_cast<octave_idx_type> (nc),
709  static_cast<octave_idx_type> (nz));
710 
711 #if defined (HAVE_HDF5_18)
712  data_hid = H5Dopen (group_hid, "cidx", octave_H5P_DEFAULT);
713 #else
714  data_hid = H5Dopen (group_hid, "cidx");
715 #endif
716  space_hid = H5Dget_space (data_hid);
717  rank = H5Sget_simple_extent_ndims (space_hid);
718 
719  if (rank != 2)
720  {
721  H5Sclose (space_hid);
722  H5Dclose (data_hid);
723  H5Gclose (group_hid);
724  return false;
725  }
726 
727  OCTAVE_LOCAL_BUFFER (hsize_t, hdims, rank);
728  OCTAVE_LOCAL_BUFFER (hsize_t, maxdims, rank);
729 
730  H5Sget_simple_extent_dims (space_hid, hdims, maxdims);
731 
732  if (static_cast<int> (hdims[0]) != nc + 1
733  || static_cast<int> (hdims[1]) != 1)
734  {
735  H5Sclose (space_hid);
736  H5Dclose (data_hid);
737  H5Gclose (group_hid);
738  return false;
739  }
740 
741  octave_idx_type *itmp = m.xcidx ();
742  if (H5Dread (data_hid, H5T_NATIVE_IDX, octave_H5S_ALL, octave_H5S_ALL,
743  octave_H5P_DEFAULT, itmp) < 0)
744  {
745  H5Sclose (space_hid);
746  H5Dclose (data_hid);
747  H5Gclose (group_hid);
748  return false;
749  }
750 
751  H5Sclose (space_hid);
752  H5Dclose (data_hid);
753 
754 #if defined (HAVE_HDF5_18)
755  data_hid = H5Dopen (group_hid, "ridx", octave_H5P_DEFAULT);
756 #else
757  data_hid = H5Dopen (group_hid, "ridx");
758 #endif
759  space_hid = H5Dget_space (data_hid);
760  rank = H5Sget_simple_extent_ndims (space_hid);
761 
762  if (rank != 2)
763  {
764  H5Sclose (space_hid);
765  H5Dclose (data_hid);
766  H5Gclose (group_hid);
767  return false;
768  }
769 
770  H5Sget_simple_extent_dims (space_hid, hdims, maxdims);
771 
772  if (static_cast<int> (hdims[0]) != nz || static_cast<int> (hdims[1]) != 1)
773  {
774  H5Sclose (space_hid);
775  H5Dclose (data_hid);
776  H5Gclose (group_hid);
777  return false;
778  }
779 
780  itmp = m.xridx ();
781  if (H5Dread (data_hid, H5T_NATIVE_IDX, octave_H5S_ALL, octave_H5S_ALL,
782  octave_H5P_DEFAULT, itmp) < 0)
783  {
784  H5Sclose (space_hid);
785  H5Dclose (data_hid);
786  H5Gclose (group_hid);
787  return false;
788  }
789 
790  H5Sclose (space_hid);
791  H5Dclose (data_hid);
792 
793 #if defined (HAVE_HDF5_18)
794  data_hid = H5Dopen (group_hid, "data", octave_H5P_DEFAULT);
795 #else
796  data_hid = H5Dopen (group_hid, "data");
797 #endif
798  space_hid = H5Dget_space (data_hid);
799  rank = H5Sget_simple_extent_ndims (space_hid);
800 
801  if (rank != 2)
802  {
803  H5Sclose (space_hid);
804  H5Dclose (data_hid);
805  H5Gclose (group_hid);
806  return false;
807  }
808 
809  H5Sget_simple_extent_dims (space_hid, hdims, maxdims);
810 
811  if (static_cast<int> (hdims[0]) != nz || static_cast<int> (hdims[1]) != 1)
812  {
813  H5Sclose (space_hid);
814  H5Dclose (data_hid);
815  H5Gclose (group_hid);
816  return false;
817  }
818 
819  double *dtmp = m.xdata ();
820 
821  if (H5Dread (data_hid, H5T_NATIVE_DOUBLE, octave_H5S_ALL, octave_H5S_ALL,
822  octave_H5P_DEFAULT, dtmp) >= 0
823  && m.indices_ok ())
824  {
825  retval = true;
826  matrix = m;
827  }
828 
829  H5Sclose (space_hid);
830  H5Dclose (data_hid);
831  H5Gclose (group_hid);
832 
833 #else
834  octave_unused_parameter (loc_id);
835  octave_unused_parameter (name);
836 
837  warn_load ("hdf5");
838 #endif
839 
840  return retval;
841 }
842 
843 mxArray *
844 octave_sparse_matrix::as_mxArray (bool interleaved) const
845 {
846  mwSize nz = nzmax ();
847  mwSize nr = rows ();
848  mwSize nc = columns ();
849 
850  mxArray *retval = new mxArray (interleaved, mxDOUBLE_CLASS, nr, nc, nz,
851  mxREAL);
852 
853  mxDouble *pd = static_cast<mxDouble *> (retval->get_data ());
854  mwIndex *ir = retval->get_ir ();
855 
856  const double *pdata = matrix.data ();
857  const octave_idx_type *pridx = matrix.ridx ();
858 
859  for (mwIndex i = 0; i < nz; i++)
860  {
861  pd[i] = pdata[i];
862 
863  ir[i] = pridx[i];
864  }
865 
866  mwIndex *jc = retval->get_jc ();
867 
868  const octave_idx_type *pcidx = matrix.cidx ();
869 
870  for (mwIndex i = 0; i < nc + 1; i++)
871  jc[i] = pcidx[i];
872 
873  return retval;
874 }
875 
878 {
879  switch (umap)
880  {
881  case umap_imag:
882  return SparseMatrix (matrix.rows (), matrix.cols (), 0.0);
883 
884  case umap_real:
885  case umap_conj:
886  return matrix;
887 
888  // Mappers handled specially.
889 #define ARRAY_METHOD_MAPPER(UMAP, FCN) \
890  case umap_ ## UMAP: \
891  return octave_value (matrix.FCN ())
892 
894 
895 #define ARRAY_MAPPER(UMAP, TYPE, FCN) \
896  case umap_ ## UMAP: \
897  return octave_value (matrix.map<TYPE> (FCN))
898 
901  ARRAY_MAPPER (angle, double, std::arg);
902  ARRAY_MAPPER (arg, double, std::arg);
905  ARRAY_MAPPER (atan, double, ::atan);
917  ARRAY_MAPPER (ceil, double, ::ceil);
918  ARRAY_MAPPER (cos, double, ::cos);
919  ARRAY_MAPPER (cosh, double, ::cosh);
920  ARRAY_MAPPER (exp, double, ::exp);
923  ARRAY_MAPPER (floor, double, ::floor);
931  ARRAY_MAPPER (sin, double, ::sin);
932  ARRAY_MAPPER (sinh, double, ::sinh);
934  ARRAY_MAPPER (tan, double, ::tan);
935  ARRAY_MAPPER (tanh, double, ::tanh);
940 
941  default: // Attempt to go via dense matrix.
943  }
944 }
boolMatrix mx_el_ne(const boolMatrix &m1, const boolMatrix &m2)
Definition: boolMatrix.cc:91
void swap_bytes< 4 >(void *ptr)
Definition: byte-swap.h:63
charNDArray max(char d, const charNDArray &m)
Definition: chNDArray.cc:230
Definition: dMatrix.h:42
bool any_element_not_one_or_zero() const
Definition: dSparse.cc:7254
bool all_integers(double &max_val, double &min_val) const
Definition: dSparse.cc:7301
bool any_element_is_nan() const
Definition: dSparse.cc:7224
bool too_large_for_float() const
Definition: dSparse.cc:7329
Matrix matrix_value() const
Definition: dSparse.cc:7450
octave_idx_type cols() const
Definition: Sparse.h:352
octave_idx_type * cidx()
Definition: Sparse.h:596
void resize(octave_idx_type r, octave_idx_type c)
Definition: Sparse.cc:993
T * data()
Definition: Sparse.h:574
octave_idx_type * ridx()
Definition: Sparse.h:583
Sparse< T, Alloc > maybe_compress(bool remove_zeros=false)
Definition: Sparse.h:531
octave_idx_type numel() const
Definition: Sparse.h:343
octave_idx_type nnz() const
Actual number of nonzero terms.
Definition: Sparse.h:339
octave_idx_type rows() const
Definition: Sparse.h:351
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:94
octave_idx_type numel(int n=0) const
Number of elements that a matrix with this dimensions would have.
Definition: dim-vector.h:335
octave_idx_type ndims() const
Number of dimensions.
Definition: dim-vector.h:257
mwIndex * get_ir() const
Definition: mxarray.h:554
mwIndex * get_jc() const
Definition: mxarray.h:556
void * get_data() const
Definition: mxarray.h:473
octave_idx_type nzmax() const
octave_value map(octave_base_value::unary_mapper_t umap) const
octave_idx_type numel() const
octave_idx_type nnz() const
octave_idx_type rows() const
Definition: ov-base.h:374
octave_idx_type columns() const
Definition: ov-base.h:381
void warn_load(const char *type) const
Definition: ov-base.cc:1157
bool isempty() const
Definition: ov-base.h:417
friend class octave_value
Definition: ov-base.h:269
void warn_save(const char *type) const
Definition: ov-base.cc:1166
NDArray array_value(bool=false) const
mxArray * as_mxArray(bool interleaved) const
std::string type_name() const
Definition: ov-re-sparse.h:153
ComplexNDArray complex_array_value(bool=false) const
ComplexMatrix complex_matrix_value(bool=false) const
bool save_binary(std::ostream &os, bool save_as_floats)
bool save_hdf5(octave_hdf5_id loc_id, const char *name, bool save_as_floats)
Complex complex_value(bool=false) const
Definition: ov-re-sparse.cc:86
octave::idx_vector index_vector(bool require_integers=false) const
Definition: ov-re-sparse.cc:61
SparseMatrix sparse_matrix_value(bool=false) const
Definition: ov-re-sparse.h:125
octave_value map(unary_mapper_t umap) const
octave_value convert_to_str_internal(bool pad, bool force, char type) const
double double_value(bool=false) const
Definition: ov-re-sparse.cc:73
charNDArray char_array_value(bool=false) const
SparseBoolMatrix sparse_bool_matrix_value(bool warn=false) const
Matrix matrix_value(bool=false) const
bool load_hdf5(octave_hdf5_id loc_id, const char *name)
boolNDArray bool_array_value(bool warn=false) const
octave_value as_double() const
bool load_binary(std::istream &is, bool swap, octave::mach_info::float_format fmt)
const octave_hdf5_id octave_H5P_DEFAULT
const octave_hdf5_id octave_H5S_ALL
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:776
void write_doubles(std::ostream &os, const double *data, save_type type, octave_idx_type len)
Definition: data-conv.cc:892
save_type
Definition: data-conv.h:87
@ LS_DOUBLE
Definition: data-conv.h:95
@ LS_FLOAT
Definition: data-conv.h:94
void warning(const char *fmt,...)
Definition: error.cc:1063
void() error(const char *fmt,...)
Definition: error.cc:988
void err_invalid_conversion(const std::string &from, const std::string &to)
Definition: errwarn.cc:71
void warn_logical_conversion()
Definition: errwarn.cc:365
void warn_implicit_conversion(const char *id, const char *from, const char *to)
Definition: errwarn.cc:344
function gamma(X)
Definition: gamma.f:3
octave::idx_vector idx_vector
Definition: idx-vector.h:1022
void err_nan_to_logical_conversion()
void err_invalid_index(const std::string &idx, octave_idx_type nd, octave_idx_type dim, const std::string &)
void err_nan_to_character_conversion()
Complex log2(const Complex &x)
Definition: lo-mappers.cc:141
Complex rc_acosh(double x)
Definition: lo-mappers.cc:253
Complex rc_atanh(double x)
Definition: lo-mappers.cc:278
Complex rc_log(double x)
Definition: lo-mappers.cc:291
Complex asin(const Complex &x)
Definition: lo-mappers.cc:107
int nint(double x)
Definition: lo-mappers.cc:216
Complex rc_asin(double x)
Definition: lo-mappers.cc:265
bool isna(double x)
Definition: lo-mappers.cc:47
Complex rc_acos(double x)
Definition: lo-mappers.cc:240
Complex rc_sqrt(double x)
Definition: lo-mappers.cc:334
Complex rc_log10(double x)
Definition: lo-mappers.cc:319
Complex acos(const Complex &x)
Definition: lo-mappers.cc:85
Complex rc_log2(double x)
Definition: lo-mappers.cc:304
Complex atan(const Complex &x)
Definition: lo-mappers.h:71
double roundb(double x)
Definition: lo-mappers.h:147
bool isfinite(double x)
Definition: lo-mappers.h:192
bool isinf(double x)
Definition: lo-mappers.h:203
double signum(double x)
Definition: lo-mappers.h:229
double round(double x)
Definition: lo-mappers.h:136
bool isnan(bool)
Definition: lo-mappers.h:178
std::complex< T > floor(const std::complex< T > &x)
Definition: lo-mappers.h:130
std::complex< T > ceil(const std::complex< T > &x)
Definition: lo-mappers.h:103
double fix(double x)
Definition: lo-mappers.h:118
F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
double erfinv(double x)
Definition: lo-specfun.cc:1823
double dawson(double x)
Definition: lo-specfun.cc:1467
double erfcinv(double x)
Definition: lo-specfun.cc:1697
Complex rc_log1p(double x)
Definition: lo-specfun.cc:2215
Complex rc_lgamma(double x)
Definition: lo-specfun.cc:2177
Complex expm1(const Complex &x)
Definition: lo-specfun.cc:1835
Complex log1p(const Complex &x)
Definition: lo-specfun.cc:1919
double cbrt(double x)
Definition: lo-specfun.h:289
double asinh(double x)
Definition: lo-specfun.h:58
double atanh(double x)
Definition: lo-specfun.h:63
double acosh(double x)
Definition: lo-specfun.h:40
double lgamma(double x)
Definition: lo-specfun.h:336
int save_hdf5_empty(octave_hdf5_id loc_id, const char *name, const dim_vector &d)
Definition: ls-hdf5.cc:1249
int load_hdf5_empty(octave_hdf5_id loc_id, const char *name, dim_vector &d)
Definition: ls-hdf5.cc:1306
octave_hdf5_id save_type_to_hdf5(save_type st)
Definition: ls-hdf5.cc:1350
save_type get_save_type(double, double)
Definition: ls-utils.cc:40
float_format
Definition: mach-info.h:38
void mxArray
Definition: mex.h:58
T octave_idx_type m
Definition: mx-inlines.cc:781
@ mxDOUBLE_CLASS
Definition: mxtypes.h:64
int64_t mwIndex
Definition: mxtypes.h:125
double mxDouble
Definition: mxtypes.h:91
@ mxREAL
Definition: mxtypes.h:80
int64_t mwSize
Definition: mxtypes.h:124
std::complex< double > erfc(std::complex< double > z, double relerr=0)
std::complex< double > erfcx(std::complex< double > z, double relerr=0)
std::complex< double > erfi(std::complex< double > z, double relerr=0)
std::complex< double > erf(std::complex< double > z, double relerr=0)
std::complex< double > Complex
Definition: oct-cmplx.h:33
int64_t octave_hdf5_id
#define H5T_NATIVE_IDX
Definition: oct-hdf5.h:42
#define OCTAVE_LOCAL_BUFFER(T, buf, size)
Definition: oct-locbuf.h:44
#define DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA(t, n, c)
Definition: ov-base.h:235
#define ARRAY_MAPPER(UMAP, TYPE, FCN)
#define ARRAY_METHOD_MAPPER(UMAP, FCN)
template int8_t abs(int8_t)