GNU Octave 11.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
ov-re-sparse.cc
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 1998-2026 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 "mappers.h"
36#include "oct-locbuf.h"
37#include "oct-specfun.h"
38
39#include "mxarray.h"
40#include "errwarn.h"
41
42#include "oct-hdf5.h"
43#include "ls-hdf5.h"
44
45#include "ov-re-sparse.h"
46
47#include "ov-base-sparse.h"
48#include "ov-base-sparse.cc"
49
50#include "ov-bool-sparse.h"
51
53 "double");
54
55octave::idx_vector
56octave_sparse_matrix::index_vector (bool /* require_integers */) const
57{
58 if (matrix.numel () == matrix.nnz ())
59 return octave::idx_vector (array_value ());
60 else
61 {
62 std::string nm = '<' + type_name () + '>';
63 octave::err_invalid_index (nm.c_str ());
64 }
65}
66
67double
69{
70 if (isempty ())
71 err_invalid_conversion ("real sparse matrix", "real scalar");
72
73 if (numel () > 1)
74 warn_implicit_conversion ("Octave:array-to-scalar",
75 "real sparse matrix", "real scalar");
76
77 return matrix(0, 0);
78}
79
82{
83 // FIXME: maybe this should be a function, valid_as_scalar()
84 if (rows () == 0 || columns () == 0)
85 err_invalid_conversion ("real sparse matrix", "complex scalar");
86
87 if (numel () > 1)
88 warn_implicit_conversion ("Octave:array-to-scalar",
89 "real sparse matrix", "complex scalar");
90
91 return Complex (matrix(0, 0), 0);
92}
93
96{
97 return matrix.matrix_value ();
98}
99
102{
104
105 if (m.any_element_is_nan ())
106 octave::err_nan_to_logical_conversion ();
107 if (warn && m.any_element_not_one_or_zero ())
109
110 return boolNDArray (m);
111}
112
115{
116 charNDArray retval (dims (), 0);
119
120 for (octave_idx_type j = 0; j < nc; j++)
121 for (octave_idx_type i = matrix.cidx (j); i < matrix.cidx (j+1); i++)
122 if (octave::math::isnan (matrix.data (i)))
123 retval(matrix.ridx (i) + nr * j) = 0;
124 else
125 retval(matrix.ridx (i) + nr * j) = static_cast<char> (matrix.data (i));
126
127 return retval;
128}
129
135
141
144{
145 return NDArray (matrix.matrix_value ());
146}
147
150{
152 octave::err_nan_to_logical_conversion ();
153 if (warn && matrix.any_element_not_one_or_zero ())
155
156 return mx_el_ne (matrix, 0.0);
157}
158
161{
162 octave_value retval;
163 const dim_vector& dv = dims ();
164 octave_idx_type nel = dv.numel ();
165
166 if (nel == 0)
167 {
168 char s = '\0';
169 retval = octave_value (&s, type);
170 }
171 else
172 {
175 charNDArray chm (dv, static_cast<char> (0));
176
177 bool warned = false;
178
179 for (octave_idx_type j = 0; j < nc; j++)
180 for (octave_idx_type i = matrix.cidx (j);
181 i < matrix.cidx (j+1); i++)
182 {
183 octave_quit ();
184
185 double d = matrix.data (i);
186
187 if (octave::math::isnan (d))
188 octave::err_nan_to_character_conversion ();
189
190 int ival = octave::math::nint (d);
191
192 if (ival < 0 || ival > std::numeric_limits<unsigned char>::max ())
193 {
194 // FIXME: is there something better we could do?
195
196 ival = 0;
197
198 if (! warned)
199 {
200 ::warning ("range error for conversion to character value");
201 warned = true;
202 }
203 }
204
205 chm(matrix.ridx (i) + j * nr) = static_cast<char> (ival);
206 }
207
208 retval = octave_value (chm, type);
209 }
210
211 return retval;
212}
213
216{
217 return this->matrix;
218}
219
220bool
221octave_sparse_matrix::save_binary (std::ostream& os, bool save_as_floats)
222{
223 const dim_vector& dv = this->dims ();
224 if (dv.ndims () < 1)
225 return false;
226
227 // Ensure that additional memory is deallocated
229
230 int nr = dv(0);
231 int nc = dv(1);
232 int nz = nnz ();
233
234 int32_t itmp;
235 // Use negative value for ndims to be consistent with other formats
236 itmp = -2;
237 os.write (reinterpret_cast<char *> (&itmp), 4);
238
239 itmp = nr;
240 os.write (reinterpret_cast<char *> (&itmp), 4);
241
242 itmp = nc;
243 os.write (reinterpret_cast<char *> (&itmp), 4);
244
245 itmp = nz;
246 os.write (reinterpret_cast<char *> (&itmp), 4);
247
248 save_type st = LS_DOUBLE;
249 if (save_as_floats)
250 {
252 {
253 warning ("save: some values too large to save as floats --");
254 warning ("save: saving as doubles instead");
255 }
256 else
257 st = LS_FLOAT;
258 }
259 else if (matrix.nnz () > 8192) // FIXME: make this configurable.
260 {
261 double max_val, min_val;
262 if (matrix.all_integers (max_val, min_val))
263 st = octave::get_save_type (max_val, min_val);
264 }
265
266 // add one to the printed indices to go from
267 // zero-based to one-based arrays
268 for (int i = 0; i < nc+1; i++)
269 {
270 octave_quit ();
271 itmp = matrix.cidx (i);
272 os.write (reinterpret_cast<char *> (&itmp), 4);
273 }
274
275 for (int i = 0; i < nz; i++)
276 {
277 octave_quit ();
278 itmp = matrix.ridx (i);
279 os.write (reinterpret_cast<char *> (&itmp), 4);
280 }
281
282 write_doubles (os, matrix.data (), st, nz);
283
284 return true;
285}
286
287bool
288octave_sparse_matrix::load_binary (std::istream& is, bool swap,
289 octave::mach_info::float_format fmt)
290{
291 int32_t nz, nc, nr, tmp;
292 char ctmp;
293
294 if (! is.read (reinterpret_cast<char *> (&tmp), 4))
295 return false;
296
297 if (swap)
298 swap_bytes<4> (&tmp);
299
300 if (tmp != -2)
301 error ("load: only 2-D sparse matrices are supported");
302
303 if (! is.read (reinterpret_cast<char *> (&nr), 4))
304 return false;
305 if (! is.read (reinterpret_cast<char *> (&nc), 4))
306 return false;
307 if (! is.read (reinterpret_cast<char *> (&nz), 4))
308 return false;
309
310 if (swap)
311 {
312 swap_bytes<4> (&nr);
313 swap_bytes<4> (&nc);
314 swap_bytes<4> (&nz);
315 }
316
317 SparseMatrix m (static_cast<octave_idx_type> (nr),
318 static_cast<octave_idx_type> (nc),
319 static_cast<octave_idx_type> (nz));
320
321 for (int i = 0; i < nc+1; i++)
322 {
323 octave_quit ();
324 if (! is.read (reinterpret_cast<char *> (&tmp), 4))
325 return false;
326 if (swap)
327 swap_bytes<4> (&tmp);
328 m.xcidx (i) = tmp;
329 }
330
331 for (int i = 0; i < nz; i++)
332 {
333 octave_quit ();
334 if (! is.read (reinterpret_cast<char *> (&tmp), 4))
335 return false;
336 if (swap)
337 swap_bytes<4> (&tmp);
338 m.xridx (i) = tmp;
339 }
340
341 if (! is.read (reinterpret_cast<char *> (&ctmp), 1))
342 return false;
343
344 read_doubles (is, m.xdata (), static_cast<save_type> (ctmp), nz, swap, fmt);
345
346 if (! is)
347 return false;
348
349 if (! m.indices_ok ())
350 return false;
351
352 matrix = m;
353
354 return true;
355}
356
357bool
359 bool save_as_floats)
360{
361 bool retval = false;
362
363#if defined (HAVE_HDF5)
364
365 const dim_vector& dv = dims ();
366 int empty = save_hdf5_empty (loc_id, name, dv);
367 if (empty)
368 return (empty > 0);
369
370 // Ensure that additional memory is deallocated
372
373#if defined (HAVE_HDF5_18)
374 hid_t group_hid = H5Gcreate (loc_id, name, octave_H5P_DEFAULT,
376#else
377 hid_t group_hid = H5Gcreate (loc_id, name, 0);
378#endif
379 if (group_hid < 0)
380 return false;
381
382 hid_t space_hid, data_hid;
383 space_hid = data_hid = -1;
385 octave_idx_type tmp;
386 hsize_t hdims[2];
387
388 space_hid = H5Screate_simple (0, hdims, nullptr);
389 if (space_hid < 0)
390 {
391 H5Gclose (group_hid);
392 return false;
393 }
394#if defined (HAVE_HDF5_18)
395 data_hid = H5Dcreate (group_hid, "nr", H5T_NATIVE_IDX, space_hid,
398#else
399 data_hid = H5Dcreate (group_hid, "nr", H5T_NATIVE_IDX, space_hid,
401#endif
402 if (data_hid < 0)
403 {
404 H5Sclose (space_hid);
405 H5Gclose (group_hid);
406 return false;
407 }
408
409 tmp = m.rows ();
410 retval = H5Dwrite (data_hid, H5T_NATIVE_IDX, octave_H5S_ALL, octave_H5S_ALL,
411 octave_H5P_DEFAULT, &tmp) >= 0;
412 H5Dclose (data_hid);
413 if (! retval)
414 {
415 H5Sclose (space_hid);
416 H5Gclose (group_hid);
417 return false;
418 }
419#if defined (HAVE_HDF5_18)
420 data_hid = H5Dcreate (group_hid, "nc", H5T_NATIVE_IDX, space_hid,
423#else
424 data_hid = H5Dcreate (group_hid, "nc", H5T_NATIVE_IDX, space_hid,
426#endif
427 if (data_hid < 0)
428 {
429 H5Sclose (space_hid);
430 H5Gclose (group_hid);
431 return false;
432 }
433
434 tmp = m.cols ();
435 retval = H5Dwrite (data_hid, H5T_NATIVE_IDX, octave_H5S_ALL, octave_H5S_ALL,
436 octave_H5P_DEFAULT, &tmp) >= 0;
437 H5Dclose (data_hid);
438 if (! retval)
439 {
440 H5Sclose (space_hid);
441 H5Gclose (group_hid);
442 return false;
443 }
444
445#if defined (HAVE_HDF5_18)
446 data_hid = H5Dcreate (group_hid, "nz", H5T_NATIVE_IDX, space_hid,
449#else
450 data_hid = H5Dcreate (group_hid, "nz", H5T_NATIVE_IDX, space_hid,
452#endif
453 if (data_hid < 0)
454 {
455 H5Sclose (space_hid);
456 H5Gclose (group_hid);
457 return false;
458 }
459
460 tmp = m.nnz ();
461 retval = H5Dwrite (data_hid, H5T_NATIVE_IDX, octave_H5S_ALL, octave_H5S_ALL,
462 octave_H5P_DEFAULT, &tmp) >= 0;
463 H5Dclose (data_hid);
464 if (! retval)
465 {
466 H5Sclose (space_hid);
467 H5Gclose (group_hid);
468 return false;
469 }
470
471 H5Sclose (space_hid);
472
473 hdims[0] = m.cols () + 1;
474 hdims[1] = 1;
475
476 space_hid = H5Screate_simple (2, hdims, nullptr);
477
478 if (space_hid < 0)
479 {
480 H5Gclose (group_hid);
481 return false;
482 }
483
484#if defined (HAVE_HDF5_18)
485 data_hid = H5Dcreate (group_hid, "cidx", H5T_NATIVE_IDX, space_hid,
488#else
489 data_hid = H5Dcreate (group_hid, "cidx", H5T_NATIVE_IDX, space_hid,
491#endif
492 if (data_hid < 0)
493 {
494 H5Sclose (space_hid);
495 H5Gclose (group_hid);
496 return false;
497 }
498
499 octave_idx_type *itmp = m.xcidx ();
500 retval = H5Dwrite (data_hid, H5T_NATIVE_IDX, octave_H5S_ALL, octave_H5S_ALL,
501 octave_H5P_DEFAULT, itmp) >= 0;
502 H5Dclose (data_hid);
503 if (! retval)
504 {
505 H5Sclose (space_hid);
506 H5Gclose (group_hid);
507 return false;
508 }
509
510 H5Sclose (space_hid);
511
512 hdims[0] = m.nnz ();
513 hdims[1] = 1;
514
515 space_hid = H5Screate_simple (2, hdims, nullptr);
516
517 if (space_hid < 0)
518 {
519 H5Gclose (group_hid);
520 return false;
521 }
522#if defined (HAVE_HDF5_18)
523 data_hid = H5Dcreate (group_hid, "ridx", H5T_NATIVE_IDX, space_hid,
526#else
527 data_hid = H5Dcreate (group_hid, "ridx", H5T_NATIVE_IDX, space_hid,
529#endif
530 if (data_hid < 0)
531 {
532 H5Sclose (space_hid);
533 H5Gclose (group_hid);
534 return false;
535 }
536
537 itmp = m.xridx ();
538 retval = H5Dwrite (data_hid, H5T_NATIVE_IDX, octave_H5S_ALL, octave_H5S_ALL,
539 octave_H5P_DEFAULT, itmp) >= 0;
540 H5Dclose (data_hid);
541 if (! retval)
542 {
543 H5Sclose (space_hid);
544 H5Gclose (group_hid);
545 return false;
546 }
547
548 hid_t save_type_hid = H5T_NATIVE_DOUBLE;
549
550 if (save_as_floats)
551 {
552 if (m.too_large_for_float ())
553 {
554 warning ("save: some values too large to save as floats --");
555 warning ("save: saving as doubles instead");
556 }
557 else
558 save_type_hid = H5T_NATIVE_FLOAT;
559 }
560#if defined (HAVE_HDF5_INT2FLOAT_CONVERSIONS)
561 // hdf5 currently doesn't support float/integer conversions
562 else
563 {
564 double max_val, min_val;
565
566 if (m.all_integers (max_val, min_val))
567 save_type_hid
568 = save_type_to_hdf5 (octave::get_save_type (max_val, min_val));
569 }
570#endif
571
572#if defined (HAVE_HDF5_18)
573 data_hid = H5Dcreate (group_hid, "data", save_type_hid, space_hid,
576#else
577 data_hid = H5Dcreate (group_hid, "data", save_type_hid, space_hid,
579#endif
580 if (data_hid < 0)
581 {
582 H5Sclose (space_hid);
583 H5Gclose (group_hid);
584 return false;
585 }
586
587 double *dtmp = m.xdata ();
588 retval = H5Dwrite (data_hid, H5T_NATIVE_DOUBLE, octave_H5S_ALL,
590 H5Dclose (data_hid);
591 H5Sclose (space_hid);
592 H5Gclose (group_hid);
593
594#else
595 octave_unused_parameter (loc_id);
596 octave_unused_parameter (name);
597 octave_unused_parameter (save_as_floats);
598
599 warn_save ("hdf5");
600#endif
601
602 return retval;
603}
604
605bool
607{
608 bool retval = false;
609
610#if defined (HAVE_HDF5)
611
612 octave_idx_type nr, nc, nz;
613 hid_t group_hid, data_hid, space_hid;
614 hsize_t rank;
615
616 dim_vector dv;
617 int empty = load_hdf5_empty (loc_id, name, dv);
618 if (empty > 0)
619 matrix.resize (dv);
620 if (empty)
621 return (empty > 0);
622
623#if defined (HAVE_HDF5_18)
624 group_hid = H5Gopen (loc_id, name, octave_H5P_DEFAULT);
625#else
626 group_hid = H5Gopen (loc_id, name);
627#endif
628 if (group_hid < 0) return false;
629
630#if defined (HAVE_HDF5_18)
631 data_hid = H5Dopen (group_hid, "nr", octave_H5P_DEFAULT);
632#else
633 data_hid = H5Dopen (group_hid, "nr");
634#endif
635 space_hid = H5Dget_space (data_hid);
636 rank = H5Sget_simple_extent_ndims (space_hid);
637
638 if (rank != 0)
639 {
640 H5Dclose (data_hid);
641 H5Gclose (group_hid);
642 return false;
643 }
644
645 if (H5Dread (data_hid, H5T_NATIVE_IDX, octave_H5S_ALL, octave_H5S_ALL,
646 octave_H5P_DEFAULT, &nr) < 0)
647 {
648 H5Dclose (data_hid);
649 H5Gclose (group_hid);
650 return false;
651 }
652
653 H5Dclose (data_hid);
654
655#if defined (HAVE_HDF5_18)
656 data_hid = H5Dopen (group_hid, "nc", octave_H5P_DEFAULT);
657#else
658 data_hid = H5Dopen (group_hid, "nc");
659#endif
660 space_hid = H5Dget_space (data_hid);
661 rank = H5Sget_simple_extent_ndims (space_hid);
662
663 if (rank != 0)
664 {
665 H5Dclose (data_hid);
666 H5Gclose (group_hid);
667 return false;
668 }
669
670 if (H5Dread (data_hid, H5T_NATIVE_IDX, octave_H5S_ALL, octave_H5S_ALL,
671 octave_H5P_DEFAULT, &nc) < 0)
672 {
673 H5Dclose (data_hid);
674 H5Gclose (group_hid);
675 return false;
676 }
677
678 H5Dclose (data_hid);
679
680#if defined (HAVE_HDF5_18)
681 data_hid = H5Dopen (group_hid, "nz", octave_H5P_DEFAULT);
682#else
683 data_hid = H5Dopen (group_hid, "nz");
684#endif
685 space_hid = H5Dget_space (data_hid);
686 rank = H5Sget_simple_extent_ndims (space_hid);
687
688 if (rank != 0)
689 {
690 H5Dclose (data_hid);
691 H5Gclose (group_hid);
692 return false;
693 }
694
695 if (H5Dread (data_hid, H5T_NATIVE_IDX, octave_H5S_ALL, octave_H5S_ALL,
696 octave_H5P_DEFAULT, &nz) < 0)
697 {
698 H5Dclose (data_hid);
699 H5Gclose (group_hid);
700 return false;
701 }
702
703 H5Dclose (data_hid);
704
705 SparseMatrix m (static_cast<octave_idx_type> (nr),
706 static_cast<octave_idx_type> (nc),
707 static_cast<octave_idx_type> (nz));
708
709#if defined (HAVE_HDF5_18)
710 data_hid = H5Dopen (group_hid, "cidx", octave_H5P_DEFAULT);
711#else
712 data_hid = H5Dopen (group_hid, "cidx");
713#endif
714 space_hid = H5Dget_space (data_hid);
715 rank = H5Sget_simple_extent_ndims (space_hid);
716
717 if (rank != 2)
718 {
719 H5Sclose (space_hid);
720 H5Dclose (data_hid);
721 H5Gclose (group_hid);
722 return false;
723 }
724
725 OCTAVE_LOCAL_BUFFER (hsize_t, hdims, rank);
726 OCTAVE_LOCAL_BUFFER (hsize_t, maxdims, rank);
727
728 H5Sget_simple_extent_dims (space_hid, hdims, maxdims);
729
730 if (static_cast<int> (hdims[0]) != nc + 1
731 || static_cast<int> (hdims[1]) != 1)
732 {
733 H5Sclose (space_hid);
734 H5Dclose (data_hid);
735 H5Gclose (group_hid);
736 return false;
737 }
738
739 octave_idx_type *itmp = m.xcidx ();
740 if (H5Dread (data_hid, H5T_NATIVE_IDX, octave_H5S_ALL, octave_H5S_ALL,
741 octave_H5P_DEFAULT, itmp) < 0)
742 {
743 H5Sclose (space_hid);
744 H5Dclose (data_hid);
745 H5Gclose (group_hid);
746 return false;
747 }
748
749 H5Sclose (space_hid);
750 H5Dclose (data_hid);
751
752#if defined (HAVE_HDF5_18)
753 data_hid = H5Dopen (group_hid, "ridx", octave_H5P_DEFAULT);
754#else
755 data_hid = H5Dopen (group_hid, "ridx");
756#endif
757 space_hid = H5Dget_space (data_hid);
758 rank = H5Sget_simple_extent_ndims (space_hid);
759
760 if (rank != 2)
761 {
762 H5Sclose (space_hid);
763 H5Dclose (data_hid);
764 H5Gclose (group_hid);
765 return false;
766 }
767
768 H5Sget_simple_extent_dims (space_hid, hdims, maxdims);
769
770 if (static_cast<int> (hdims[0]) != nz || static_cast<int> (hdims[1]) != 1)
771 {
772 H5Sclose (space_hid);
773 H5Dclose (data_hid);
774 H5Gclose (group_hid);
775 return false;
776 }
777
778 itmp = m.xridx ();
779 if (H5Dread (data_hid, H5T_NATIVE_IDX, octave_H5S_ALL, octave_H5S_ALL,
780 octave_H5P_DEFAULT, itmp) < 0)
781 {
782 H5Sclose (space_hid);
783 H5Dclose (data_hid);
784 H5Gclose (group_hid);
785 return false;
786 }
787
788 H5Sclose (space_hid);
789 H5Dclose (data_hid);
790
791#if defined (HAVE_HDF5_18)
792 data_hid = H5Dopen (group_hid, "data", octave_H5P_DEFAULT);
793#else
794 data_hid = H5Dopen (group_hid, "data");
795#endif
796 space_hid = H5Dget_space (data_hid);
797 rank = H5Sget_simple_extent_ndims (space_hid);
798
799 if (rank != 2)
800 {
801 H5Sclose (space_hid);
802 H5Dclose (data_hid);
803 H5Gclose (group_hid);
804 return false;
805 }
806
807 H5Sget_simple_extent_dims (space_hid, hdims, maxdims);
808
809 if (static_cast<int> (hdims[0]) != nz || static_cast<int> (hdims[1]) != 1)
810 {
811 H5Sclose (space_hid);
812 H5Dclose (data_hid);
813 H5Gclose (group_hid);
814 return false;
815 }
816
817 double *dtmp = m.xdata ();
818
819 if (H5Dread (data_hid, H5T_NATIVE_DOUBLE, octave_H5S_ALL, octave_H5S_ALL,
820 octave_H5P_DEFAULT, dtmp) >= 0
821 && m.indices_ok ())
822 {
823 retval = true;
824 matrix = m;
825 }
826
827 H5Sclose (space_hid);
828 H5Dclose (data_hid);
829 H5Gclose (group_hid);
830
831#else
832 octave_unused_parameter (loc_id);
833 octave_unused_parameter (name);
834
835 warn_load ("hdf5");
836#endif
837
838 return retval;
839}
840
841mxArray *
842octave_sparse_matrix::as_mxArray (bool interleaved) const
843{
844 mwSize nz = nzmax ();
845 mwSize nr = rows ();
846 mwSize nc = columns ();
847
848 mxArray *retval = new mxArray (interleaved, mxDOUBLE_CLASS, nr, nc, nz,
849 mxREAL);
850
851 mxDouble *pd = static_cast<mxDouble *> (retval->get_data ());
852 mwIndex *ir = retval->get_ir ();
853
854 const double *pdata = matrix.data ();
855 const octave_idx_type *pridx = matrix.ridx ();
856
857 for (mwIndex i = 0; i < nz; i++)
858 {
859 pd[i] = pdata[i];
860
861 ir[i] = pridx[i];
862 }
863
864 mwIndex *jc = retval->get_jc ();
865
866 const octave_idx_type *pcidx = matrix.cidx ();
867
868 for (mwIndex i = 0; i < nc + 1; i++)
869 jc[i] = pcidx[i];
870
871 return retval;
872}
873
876{
877 switch (umap)
878 {
879 case umap_imag:
880 return SparseMatrix (matrix.rows (), matrix.cols (), 0.0);
881
882 case umap_real:
883 case umap_conj:
884 return matrix;
885
886 // Mappers handled specially.
887#define ARRAY_METHOD_MAPPER(UMAP, FCN) \
888 case umap_ ## UMAP: \
889 return octave_value (matrix.FCN ())
890
892
893#define ARRAY_MAPPER(UMAP, TYPE, FCN) \
894 case umap_ ## UMAP: \
895 return octave_value (matrix.map<TYPE> (FCN))
896
897 ARRAY_MAPPER (acos, Complex, octave::math::rc_acos);
898 ARRAY_MAPPER (acosh, Complex, octave::math::rc_acosh);
899 ARRAY_MAPPER (angle, double, std::arg);
900 ARRAY_MAPPER (arg, double, std::arg);
901 ARRAY_MAPPER (asin, Complex, octave::math::rc_asin);
902 ARRAY_MAPPER (asinh, double, octave::math::asinh);
903 ARRAY_MAPPER (atan, double, ::atan);
904 ARRAY_MAPPER (atanh, Complex, octave::math::rc_atanh);
905 ARRAY_MAPPER (erf, double, octave::math::erf);
906 ARRAY_MAPPER (erfinv, double, octave::math::erfinv);
907 ARRAY_MAPPER (erfcinv, double, octave::math::erfcinv);
908 ARRAY_MAPPER (erfc, double, octave::math::erfc);
909 ARRAY_MAPPER (erfcx, double, octave::math::erfcx);
910 ARRAY_MAPPER (erfi, double, octave::math::erfi);
911 ARRAY_MAPPER (dawson, double, octave::math::dawson);
912 ARRAY_MAPPER (gamma, double, octave::math::gamma);
913 ARRAY_MAPPER (lgamma, Complex, octave::math::rc_lgamma);
914 ARRAY_MAPPER (cbrt, double, octave::math::cbrt);
915 ARRAY_MAPPER (ceil, double, ::ceil);
916 ARRAY_MAPPER (cos, double, ::cos);
917 ARRAY_MAPPER (cosh, double, ::cosh);
918 ARRAY_MAPPER (exp, double, ::exp);
919 ARRAY_MAPPER (expm1, double, octave::math::expm1);
920 ARRAY_MAPPER (fix, double, octave::math::fix);
921 ARRAY_MAPPER (floor, double, ::floor);
922 ARRAY_MAPPER (log, Complex, octave::math::rc_log);
923 ARRAY_MAPPER (log2, Complex, octave::math::rc_log2);
924 ARRAY_MAPPER (log10, Complex, octave::math::rc_log10);
925 ARRAY_MAPPER (log1p, Complex, octave::math::rc_log1p);
926 ARRAY_MAPPER (round, double, octave::math::round);
927 ARRAY_MAPPER (roundb, double, octave::math::roundb);
928 ARRAY_MAPPER (signum, double, octave::math::signum);
929 ARRAY_MAPPER (sin, double, ::sin);
930 ARRAY_MAPPER (sinh, double, ::sinh);
931 ARRAY_MAPPER (sqrt, Complex, octave::math::rc_sqrt);
932 ARRAY_MAPPER (tan, double, ::tan);
933 ARRAY_MAPPER (tanh, double, ::tanh);
934 ARRAY_MAPPER (isnan, bool, octave::math::isnan);
935 ARRAY_MAPPER (isna, bool, octave::math::isna);
936 ARRAY_MAPPER (isinf, bool, octave::math::isinf);
937 ARRAY_MAPPER (isfinite, bool, octave::math::isfinite);
938
939 default: // Attempt to go via dense matrix.
941 }
942}
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
bool any_element_not_one_or_zero() const
Definition dNDArray.cc:330
bool any_element_is_nan() const
Definition dNDArray.cc:318
bool any_element_not_one_or_zero() const
Definition dSparse.cc:7429
bool all_integers(double &max_val, double &min_val) const
Definition dSparse.cc:7476
bool any_element_is_nan() const
Definition dSparse.cc:7399
bool too_large_for_float() const
Definition dSparse.cc:7504
Matrix matrix_value() const
Definition dSparse.cc:7700
octave_idx_type cols() const
Definition Sparse.h:351
octave_idx_type * cidx()
Definition Sparse.h:595
T * data()
Definition Sparse.h:573
void resize(octave_idx_type r, octave_idx_type c)
Definition Sparse.cc:1006
bool indices_ok() const
Definition Sparse.h:748
T * xdata()
Definition Sparse.h:575
octave_idx_type * ridx()
Definition Sparse.h:582
octave_idx_type numel() const
Definition Sparse.h:342
Sparse< T, Alloc > maybe_compress(bool remove_zeros=false)
Definition Sparse.h:530
octave_idx_type nnz() const
Actual number of nonzero terms.
Definition Sparse.h:338
octave_idx_type rows() const
Definition Sparse.h:350
octave_idx_type * xcidx()
Definition Sparse.h:601
octave_idx_type * xridx()
Definition Sparse.h:588
Vector representing the dimensions (size) of an Array.
Definition dim-vector.h:92
octave_idx_type numel(int n=0) const
Number of elements that a matrix with this dimensions would have.
Definition dim-vector.h:341
octave_idx_type ndims() const
Number of dimensions.
Definition dim-vector.h:263
mwIndex * get_jc() const
Definition mxarray.h:565
mwIndex * get_ir() const
Definition mxarray.h:563
void * get_data() const
Definition mxarray.h:482
octave_idx_type nzmax() const
octave_value map(octave_base_value::unary_mapper_t umap) const
octave_idx_type numel() const
octave_idx_type rows() const
Definition ov-base.h:384
octave_idx_type columns() const
Definition ov-base.h:391
void warn_load(const char *type) const
Definition ov-base.cc:1152
bool isempty() const
Definition ov-base.h:429
friend class octave_value
Definition ov-base.h:278
void warn_save(const char *type) const
Definition ov-base.cc:1161
NDArray array_value(bool=false) const
mxArray * as_mxArray(bool interleaved) const
std::string type_name() const
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
octave::idx_vector index_vector(bool require_integers=false) const
SparseMatrix sparse_matrix_value(bool=false) const
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
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:820
void write_doubles(std::ostream &os, const double *data, save_type type, octave_idx_type len)
Definition data-conv.cc:960
save_type
Definition data-conv.h:85
@ LS_DOUBLE
Definition data-conv.h:93
@ LS_FLOAT
Definition data-conv.h:92
void warning(const char *fmt,...)
Definition error.cc:1083
void error(const char *fmt,...)
Definition error.cc:1008
void err_invalid_conversion(const std::string &from, const std::string &to)
Definition errwarn.cc:71
void warn_logical_conversion()
Definition errwarn.cc:366
void warn_implicit_conversion(const char *id, const char *from, const char *to)
Definition errwarn.cc:345
function gamma(x)
Definition gamma.f:3
Complex log2(const Complex &x)
Definition mappers.cc:140
Complex asin(const Complex &x)
Definition mappers.cc:106
bool isna(double x)
Definition mappers.cc:46
Complex acos(const Complex &x)
Definition mappers.cc:84
int save_hdf5_empty(octave_hdf5_id loc_id, const char *name, const dim_vector &d)
Definition ls-hdf5.cc:1255
int load_hdf5_empty(octave_hdf5_id loc_id, const char *name, dim_vector &d)
Definition ls-hdf5.cc:1312
octave_hdf5_id save_type_to_hdf5(save_type st)
Definition ls-hdf5.cc:1356
std::complex< T > ceil(const std::complex< T > &x)
Definition mappers.h:115
Complex atan(const Complex &x)
Definition mappers.h:83
double roundb(double x)
Definition mappers.h:159
std::complex< T > floor(const std::complex< T > &x)
Definition mappers.h:142
bool isfinite(double x)
Definition mappers.h:229
bool isinf(double x)
Definition mappers.h:212
double signum(double x)
Definition mappers.h:255
double round(double x)
Definition mappers.h:148
bool isnan(bool)
Definition mappers.h:192
double fix(double x)
Definition mappers.h:130
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
double erfinv(double x)
double erfi(double x)
double dawson(double x)
Complex erf(const Complex &x)
double erfcinv(double x)
double erfcx(double x)
Complex expm1(const Complex &x)
Complex log1p(const Complex &x)
Complex erfc(const Complex &x)
double cbrt(double x)
double asinh(double x)
Definition oct-specfun.h:57
double atanh(double x)
Definition oct-specfun.h:62
double acosh(double x)
Definition oct-specfun.h:39
double lgamma(double x)
#define DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA(t, n, c)
Definition ov-base.h:246
#define ARRAY_MAPPER(UMAP, TYPE, FCN)
#define ARRAY_METHOD_MAPPER(UMAP, FCN)
template int8_t abs(int8_t)
F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d