GNU Octave 7.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
ov-cx-sparse.cc
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 1998-2022 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 <ostream>
32#include <vector>
33
34#include "lo-specfun.h"
35#include "lo-mappers.h"
36#include "oct-locbuf.h"
37
38#include "mxarray.h"
39#include "ov-base.h"
40#include "ov-scalar.h"
41#include "ov-complex.h"
42#include "errwarn.h"
43
44#include "oct-hdf5.h"
45
46#include "ov-re-sparse.h"
47#include "ov-cx-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 "sparse complex matrix", "double");
59
62{
63 octave_base_value *retval = nullptr;
64
66 {
67 int nr = matrix.rows ();
68 int nc = matrix.cols ();
69
70 // Don't use numel, since it can overflow for very large matrices
71 // Note that for the tests on matrix size, they become approximative
72 // since they involves a cast to double to avoid issues of overflow
73 if (matrix.rows () == 1 && matrix.cols () == 1)
74 {
75 // Const copy of the matrix, so the right version of () operator used
76 const SparseComplexMatrix tmp (matrix);
77
78 Complex c = tmp (0, 0);
79
80 if (c.imag () == 0.0)
81 retval = new octave_scalar (c.real ());
82 else
83 retval = new octave_complex (c);
84 }
85 else if (nr == 0 || nc == 0)
86 retval = new octave_matrix (Matrix (nr, nc));
87 else if (matrix.all_elements_are_real ())
88 if (matrix.cols () > 0 && matrix.rows () > 0
89 && (double (matrix.byte_size ()) > double (matrix.rows ())
90 * double (matrix.cols ()) * sizeof (double)))
91 retval = new octave_matrix (::real (matrix.matrix_value ()));
92 else
93 retval = new octave_sparse_matrix (::real (matrix));
94 else if (matrix.cols () > 0 && matrix.rows () > 0
95 && (double (matrix.byte_size ()) > double (matrix.rows ())
96 * double (matrix.cols ()) * sizeof (Complex)))
98 }
99 else
100 {
102 retval = new octave_sparse_matrix (::real (matrix));
103 }
104
105 return retval;
106}
107
108double
110{
111 if (! force_conversion)
112 warn_implicit_conversion ("Octave:imag-to-real",
113 "complex sparse matrix", "real scalar");
114
115 // FIXME: maybe this should be a function, valid_as_scalar()
116 if (isempty ())
117 err_invalid_conversion ("complex sparse matrix", "real scalar");
118
119 if (numel () > 1)
120 warn_implicit_conversion ("Octave:array-to-scalar",
121 "complex sparse matrix", "real scalar");
122
123 return std::real (matrix(0, 0));
124}
125
126Matrix
128{
129 Matrix retval;
130
131 if (! force_conversion)
132 warn_implicit_conversion ("Octave:imag-to-real",
133 "complex sparse matrix", "real matrix");
134
135 retval = ::real (matrix.matrix_value ());
136
137 return retval;
138}
139
142{
143 // FIXME: maybe this should be a function, valid_as_scalar()
144 if (isempty ())
145 err_invalid_conversion ("complex sparse matrix", "real scalar");
146
147 if (numel () > 1)
148 warn_implicit_conversion ("Octave:array-to-scalar",
149 "complex sparse matrix", "real scalar");
150
151 return matrix(0, 0);
152}
153
156{
157 return matrix.matrix_value ();
158}
159
162{
164}
165
168{
169 charNDArray retval;
170
171 if (! frc_str_conv)
172 warn_implicit_conversion ("Octave:num-to-str",
173 "sparse complex matrix", "string");
174 else
175 {
176 retval = charNDArray (dims (), 0);
179
180 for (octave_idx_type j = 0; j < nc; j++)
181 for (octave_idx_type i = matrix.cidx (j); i < matrix.cidx (j+1); i++)
182 retval(matrix.ridx (i) + nr * j)
183 = static_cast<char> (std::real (matrix.data (i)));
184 }
185
186 return retval;
187}
188
191{
192 SparseMatrix retval;
193
194 if (! force_conversion)
195 warn_implicit_conversion ("Octave:imag-to-real",
196 "complex sparse matrix",
197 "real sparse matrix");
198
199 retval = ::real (matrix);
200
201 return retval;
202}
203
206{
209 if (warn && (! matrix.all_elements_are_real ()
210 || real (matrix).any_element_not_one_or_zero ()))
212
213 return mx_el_ne (matrix, Complex (0.0));
214}
215
218{
219 return this->matrix;
220}
221
222bool
224 bool save_as_floats)
225{
226 dim_vector dv = this->dims ();
227 if (dv.ndims () < 1)
228 return false;
229
230 // Ensure that additional memory is deallocated
232
233 int nr = dv(0);
234 int nc = dv(1);
235 int nz = nnz ();
236
237 int32_t itmp;
238 // Use negative value for ndims to be consistent with other formats
239 itmp = -2;
240 os.write (reinterpret_cast<char *> (&itmp), 4);
241
242 itmp = nr;
243 os.write (reinterpret_cast<char *> (&itmp), 4);
244
245 itmp = nc;
246 os.write (reinterpret_cast<char *> (&itmp), 4);
247
248 itmp = nz;
249 os.write (reinterpret_cast<char *> (&itmp), 4);
250
251 save_type st = LS_DOUBLE;
252 if (save_as_floats)
253 {
255 {
256 warning ("save: some values too large to save as floats --");
257 warning ("save: saving as doubles instead");
258 }
259 else
260 st = LS_FLOAT;
261 }
262 else if (matrix.nnz () > 8192) // FIXME: make this configurable.
263 {
264 double max_val, min_val;
265 if (matrix.all_integers (max_val, min_val))
266 st = octave::get_save_type (max_val, min_val);
267 }
268
269 // add one to the printed indices to go from
270 // zero-based to one-based arrays
271 for (int i = 0; i < nc+1; i++)
272 {
273 octave_quit ();
274 itmp = matrix.cidx (i);
275 os.write (reinterpret_cast<char *> (&itmp), 4);
276 }
277
278 for (int i = 0; i < nz; i++)
279 {
280 octave_quit ();
281 itmp = matrix.ridx (i);
282 os.write (reinterpret_cast<char *> (&itmp), 4);
283 }
284
285 write_doubles (os, reinterpret_cast<const double *> (matrix.data ()), st,
286 2 * nz);
287
288 return true;
289}
290
291bool
292octave_sparse_complex_matrix::load_binary (std::istream& is, bool swap,
294{
295 int32_t nz, nc, nr, tmp;
296 char ctmp;
297
298 if (! is.read (reinterpret_cast<char *> (&tmp), 4))
299 return false;
300
301 if (swap)
302 swap_bytes<4> (&tmp);
303
304 if (tmp != -2)
305 error ("load: only 2-D sparse matrices are supported");
306
307 if (! is.read (reinterpret_cast<char *> (&nr), 4))
308 return false;
309 if (! is.read (reinterpret_cast<char *> (&nc), 4))
310 return false;
311 if (! is.read (reinterpret_cast<char *> (&nz), 4))
312 return false;
313
314 if (swap)
315 {
316 swap_bytes<4> (&nr);
317 swap_bytes<4> (&nc);
318 swap_bytes<4> (&nz);
319 }
320
321 SparseComplexMatrix m (static_cast<octave_idx_type> (nr),
322 static_cast<octave_idx_type> (nc),
323 static_cast<octave_idx_type> (nz));
324
325 for (int i = 0; i < nc+1; i++)
326 {
327 octave_quit ();
328 if (! is.read (reinterpret_cast<char *> (&tmp), 4))
329 return false;
330 if (swap)
331 swap_bytes<4> (&tmp);
332 m.cidx (i) = tmp;
333 }
334
335 for (int i = 0; i < nz; i++)
336 {
337 octave_quit ();
338 if (! is.read (reinterpret_cast<char *> (&tmp), 4))
339 return false;
340 if (swap)
341 swap_bytes<4> (&tmp);
342 m.ridx (i) = tmp;
343 }
344
345 if (! is.read (reinterpret_cast<char *> (&ctmp), 1))
346 return false;
347
348 read_doubles (is, reinterpret_cast<double *> (m.data ()),
349 static_cast<save_type> (ctmp), 2 * nz, swap, fmt);
350
351 if (! is)
352 return false;
353
354 if (! m.indices_ok ())
355 return false;
356
357 matrix = m;
358
359 return true;
360}
361
362bool
364 const char *name,
365 bool save_as_floats)
366{
367 bool retval = false;
368
369#if defined (HAVE_HDF5)
370
371 dim_vector dv = dims ();
372 int empty = save_hdf5_empty (loc_id, name, dv);
373 if (empty)
374 return (empty > 0);
375
376 // Ensure that additional memory is deallocated
378
379#if defined (HAVE_HDF5_18)
380 hid_t group_hid = H5Gcreate (loc_id, name, octave_H5P_DEFAULT,
382#else
383 hid_t group_hid = H5Gcreate (loc_id, name, 0);
384#endif
385 if (group_hid < 0)
386 return false;
387
388 hid_t space_hid, data_hid;
389 space_hid = data_hid = -1;
391 octave_idx_type tmp;
392 hsize_t hdims[2];
393
394 space_hid = H5Screate_simple (0, hdims, nullptr);
395 if (space_hid < 0)
396 {
397 H5Gclose (group_hid);
398 return false;
399 }
400
401#if defined (HAVE_HDF5_18)
402 data_hid = H5Dcreate (group_hid, "nr", H5T_NATIVE_IDX, space_hid,
404#else
405 data_hid = H5Dcreate (group_hid, "nr", H5T_NATIVE_IDX, space_hid,
407#endif
408 if (data_hid < 0)
409 {
410 H5Sclose (space_hid);
411 H5Gclose (group_hid);
412 return false;
413 }
414
415 tmp = m.rows ();
416 retval = H5Dwrite (data_hid, H5T_NATIVE_IDX, octave_H5S_ALL, octave_H5S_ALL,
417 octave_H5P_DEFAULT, &tmp) >= 0;
418 H5Dclose (data_hid);
419 if (! retval)
420 {
421 H5Sclose (space_hid);
422 H5Gclose (group_hid);
423 return false;
424 }
425
426#if defined (HAVE_HDF5_18)
427 data_hid = H5Dcreate (group_hid, "nc", H5T_NATIVE_IDX, space_hid,
429#else
430 data_hid = H5Dcreate (group_hid, "nc", H5T_NATIVE_IDX, space_hid,
432#endif
433 if (data_hid < 0)
434 {
435 H5Sclose (space_hid);
436 H5Gclose (group_hid);
437 return false;
438 }
439
440 tmp = m.cols ();
441 retval = H5Dwrite (data_hid, H5T_NATIVE_IDX, octave_H5S_ALL, octave_H5S_ALL,
442 octave_H5P_DEFAULT, &tmp) >= 0;
443 H5Dclose (data_hid);
444 if (! retval)
445 {
446 H5Sclose (space_hid);
447 H5Gclose (group_hid);
448 return false;
449 }
450
451#if defined (HAVE_HDF5_18)
452 data_hid = H5Dcreate (group_hid, "nz", H5T_NATIVE_IDX, space_hid,
454#else
455 data_hid = H5Dcreate (group_hid, "nz", H5T_NATIVE_IDX, space_hid,
457#endif
458 if (data_hid < 0)
459 {
460 H5Sclose (space_hid);
461 H5Gclose (group_hid);
462 return false;
463 }
464
465 tmp = m.nnz ();
466 retval = H5Dwrite (data_hid, H5T_NATIVE_IDX, octave_H5S_ALL, octave_H5S_ALL,
467 octave_H5P_DEFAULT, &tmp) >= 0;
468 H5Dclose (data_hid);
469 if (! retval)
470 {
471 H5Sclose (space_hid);
472 H5Gclose (group_hid);
473 return false;
474 }
475
476 H5Sclose (space_hid);
477
478 hdims[0] = m.cols () + 1;
479 hdims[1] = 1;
480
481 space_hid = H5Screate_simple (2, hdims, nullptr);
482
483 if (space_hid < 0)
484 {
485 H5Gclose (group_hid);
486 return false;
487 }
488
489#if defined (HAVE_HDF5_18)
490 data_hid = H5Dcreate (group_hid, "cidx", H5T_NATIVE_IDX, space_hid,
492#else
493 data_hid = H5Dcreate (group_hid, "cidx", H5T_NATIVE_IDX, space_hid,
495#endif
496 if (data_hid < 0)
497 {
498 H5Sclose (space_hid);
499 H5Gclose (group_hid);
500 return false;
501 }
502
503 octave_idx_type *itmp = m.xcidx ();
504 retval = H5Dwrite (data_hid, H5T_NATIVE_IDX, octave_H5S_ALL, octave_H5S_ALL,
505 octave_H5P_DEFAULT, itmp) >= 0;
506 H5Dclose (data_hid);
507 if (! retval)
508 {
509 H5Sclose (space_hid);
510 H5Gclose (group_hid);
511 return false;
512 }
513
514 H5Sclose (space_hid);
515
516 hdims[0] = m.nnz ();
517 hdims[1] = 1;
518
519 space_hid = H5Screate_simple (2, hdims, nullptr);
520
521 if (space_hid < 0)
522 {
523 H5Gclose (group_hid);
524 return false;
525 }
526
527#if defined (HAVE_HDF5_18)
528 data_hid = H5Dcreate (group_hid, "ridx", H5T_NATIVE_IDX, space_hid,
530#else
531 data_hid = H5Dcreate (group_hid, "ridx", H5T_NATIVE_IDX, space_hid,
533#endif
534 if (data_hid < 0)
535 {
536 H5Sclose (space_hid);
537 H5Gclose (group_hid);
538 return false;
539 }
540
541 itmp = m.xridx ();
542 retval = H5Dwrite (data_hid, H5T_NATIVE_IDX, octave_H5S_ALL, octave_H5S_ALL,
543 octave_H5P_DEFAULT, itmp) >= 0;
544 H5Dclose (data_hid);
545 if (! retval)
546 {
547 H5Sclose (space_hid);
548 H5Gclose (group_hid);
549 return false;
550 }
551
552 hid_t save_type_hid = H5T_NATIVE_DOUBLE;
553
554 if (save_as_floats)
555 {
556 if (m.too_large_for_float ())
557 {
558 warning ("save: some values too large to save as floats --");
559 warning ("save: saving as doubles instead");
560 }
561 else
562 save_type_hid = H5T_NATIVE_FLOAT;
563 }
564#if defined (HAVE_HDF5_INT2FLOAT_CONVERSIONS)
565 // hdf5 currently doesn't support float/integer conversions
566 else
567 {
568 double max_val, min_val;
569
570 if (m.all_integers (max_val, min_val))
571 save_type_hid
572 = save_type_to_hdf5 (octave::get_save_type (max_val, min_val));
573 }
574#endif
575
576 hid_t type_hid = hdf5_make_complex_type (save_type_hid);
577 if (type_hid < 0)
578 {
579 H5Sclose (space_hid);
580 H5Gclose (group_hid);
581 return false;
582 }
583#if defined (HAVE_HDF5_18)
584 data_hid = H5Dcreate (group_hid, "data", type_hid, space_hid,
586#else
587 data_hid = H5Dcreate (group_hid, "data", type_hid, space_hid,
589#endif
590 if (data_hid < 0)
591 {
592 H5Sclose (space_hid);
593 H5Tclose (type_hid);
594 H5Gclose (group_hid);
595 return false;
596 }
597
598 hid_t complex_type_hid = hdf5_make_complex_type (H5T_NATIVE_DOUBLE);
599 retval = false;
600 if (complex_type_hid >= 0)
601 {
602 Complex *ctmp = m.xdata ();
603
604 retval = H5Dwrite (data_hid, complex_type_hid, octave_H5S_ALL, octave_H5S_ALL,
605 octave_H5P_DEFAULT, ctmp) >= 0;
606 }
607
608 H5Dclose (data_hid);
609 H5Sclose (space_hid);
610 H5Tclose (type_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
624bool
626 const char *name)
627{
628 bool retval = false;
629
630#if defined (HAVE_HDF5)
631
632 octave_idx_type nr, nc, nz;
633 hid_t group_hid, data_hid, space_hid;
634 hsize_t rank;
635
636 dim_vector dv;
637 int empty = load_hdf5_empty (loc_id, name, dv);
638 if (empty > 0)
639 matrix.resize (dv);
640 if (empty)
641 return (empty > 0);
642
643#if defined (HAVE_HDF5_18)
644 group_hid = H5Gopen (loc_id, name, octave_H5P_DEFAULT);
645#else
646 group_hid = H5Gopen (loc_id, name);
647#endif
648 if (group_hid < 0) return false;
649
650#if defined (HAVE_HDF5_18)
651 data_hid = H5Dopen (group_hid, "nr", octave_H5P_DEFAULT);
652#else
653 data_hid = H5Dopen (group_hid, "nr");
654#endif
655 space_hid = H5Dget_space (data_hid);
656 rank = H5Sget_simple_extent_ndims (space_hid);
657
658 if (rank != 0)
659 {
660 H5Dclose (data_hid);
661 H5Gclose (group_hid);
662 return false;
663 }
664
665 if (H5Dread (data_hid, H5T_NATIVE_IDX, octave_H5S_ALL,
667 {
668 H5Dclose (data_hid);
669 H5Gclose (group_hid);
670 return false;
671 }
672
673 H5Dclose (data_hid);
674
675#if defined (HAVE_HDF5_18)
676 data_hid = H5Dopen (group_hid, "nc", octave_H5P_DEFAULT);
677#else
678 data_hid = H5Dopen (group_hid, "nc");
679#endif
680 space_hid = H5Dget_space (data_hid);
681 rank = H5Sget_simple_extent_ndims (space_hid);
682
683 if (rank != 0)
684 {
685 H5Dclose (data_hid);
686 H5Gclose (group_hid);
687 return false;
688 }
689
690 if (H5Dread (data_hid, H5T_NATIVE_IDX, octave_H5S_ALL,
692 {
693 H5Dclose (data_hid);
694 H5Gclose (group_hid);
695 return false;
696 }
697
698 H5Dclose (data_hid);
699
700#if defined (HAVE_HDF5_18)
701 data_hid = H5Dopen (group_hid, "nz", octave_H5P_DEFAULT);
702#else
703 data_hid = H5Dopen (group_hid, "nz");
704#endif
705 space_hid = H5Dget_space (data_hid);
706 rank = H5Sget_simple_extent_ndims (space_hid);
707
708 if (rank != 0)
709 {
710 H5Dclose (data_hid);
711 H5Gclose (group_hid);
712 return false;
713 }
714
715 if (H5Dread (data_hid, H5T_NATIVE_IDX, octave_H5S_ALL,
717 {
718 H5Dclose (data_hid);
719 H5Gclose (group_hid);
720 return false;
721 }
722
723 H5Dclose (data_hid);
724
725 SparseComplexMatrix m (static_cast<octave_idx_type> (nr),
726 static_cast<octave_idx_type> (nc),
727 static_cast<octave_idx_type> (nz));
728
729#if defined (HAVE_HDF5_18)
730 data_hid = H5Dopen (group_hid, "cidx", octave_H5P_DEFAULT);
731#else
732 data_hid = H5Dopen (group_hid, "cidx");
733#endif
734 space_hid = H5Dget_space (data_hid);
735 rank = H5Sget_simple_extent_ndims (space_hid);
736
737 if (rank != 2)
738 {
739 H5Sclose (space_hid);
740 H5Dclose (data_hid);
741 H5Gclose (group_hid);
742 return false;
743 }
744
745 OCTAVE_LOCAL_BUFFER (hsize_t, hdims, rank);
746 OCTAVE_LOCAL_BUFFER (hsize_t, maxdims, rank);
747
748 H5Sget_simple_extent_dims (space_hid, hdims, maxdims);
749
750 if (static_cast<int> (hdims[0]) != nc + 1
751 || static_cast<int> (hdims[1]) != 1)
752 {
753 H5Sclose (space_hid);
754 H5Dclose (data_hid);
755 H5Gclose (group_hid);
756 return false;
757 }
758
759 octave_idx_type *itmp = m.xcidx ();
760 if (H5Dread (data_hid, H5T_NATIVE_IDX, octave_H5S_ALL,
762 {
763 H5Sclose (space_hid);
764 H5Dclose (data_hid);
765 H5Gclose (group_hid);
766 return false;
767 }
768
769 H5Sclose (space_hid);
770 H5Dclose (data_hid);
771
772#if defined (HAVE_HDF5_18)
773 data_hid = H5Dopen (group_hid, "ridx", octave_H5P_DEFAULT);
774#else
775 data_hid = H5Dopen (group_hid, "ridx");
776#endif
777 space_hid = H5Dget_space (data_hid);
778 rank = H5Sget_simple_extent_ndims (space_hid);
779
780 if (rank != 2)
781 {
782 H5Sclose (space_hid);
783 H5Dclose (data_hid);
784 H5Gclose (group_hid);
785 return false;
786 }
787
788 H5Sget_simple_extent_dims (space_hid, hdims, maxdims);
789
790 if (static_cast<int> (hdims[0]) != nz
791 || static_cast<int> (hdims[1]) != 1)
792 {
793 H5Sclose (space_hid);
794 H5Dclose (data_hid);
795 H5Gclose (group_hid);
796 return false;
797 }
798
799 itmp = m.xridx ();
800 if (H5Dread (data_hid, H5T_NATIVE_IDX, octave_H5S_ALL,
802 {
803 H5Sclose (space_hid);
804 H5Dclose (data_hid);
805 H5Gclose (group_hid);
806 return false;
807 }
808
809 H5Sclose (space_hid);
810 H5Dclose (data_hid);
811
812#if defined (HAVE_HDF5_18)
813 data_hid = H5Dopen (group_hid, "data", octave_H5P_DEFAULT);
814#else
815 data_hid = H5Dopen (group_hid, "data");
816#endif
817 hid_t type_hid = H5Dget_type (data_hid);
818
819 hid_t complex_type = hdf5_make_complex_type (H5T_NATIVE_DOUBLE);
820
821 if (! hdf5_types_compatible (type_hid, complex_type))
822 {
823 H5Tclose (complex_type);
824 H5Dclose (data_hid);
825 H5Gclose (group_hid);
826 return false;
827 }
828
829 space_hid = H5Dget_space (data_hid);
830 rank = H5Sget_simple_extent_ndims (space_hid);
831
832 if (rank != 2)
833 {
834 H5Sclose (space_hid);
835 H5Dclose (data_hid);
836 H5Gclose (group_hid);
837 return false;
838 }
839
840 H5Sget_simple_extent_dims (space_hid, hdims, maxdims);
841
842 if (static_cast<int> (hdims[0]) != nz
843 || static_cast<int> (hdims[1]) != 1)
844 {
845 H5Sclose (space_hid);
846 H5Dclose (data_hid);
847 H5Gclose (group_hid);
848 return false;
849 }
850
851 Complex *ctmp = m.xdata ();
852
853 if (H5Dread (data_hid, complex_type, octave_H5S_ALL, octave_H5S_ALL,
854 octave_H5P_DEFAULT, ctmp) >= 0
855 && m.indices_ok ())
856 {
857 retval = true;
858 matrix = m;
859 }
860
861 H5Tclose (complex_type);
862 H5Sclose (space_hid);
863 H5Dclose (data_hid);
864 H5Gclose (group_hid);
865
866#else
867 octave_unused_parameter (loc_id);
868 octave_unused_parameter (name);
869
870 warn_load ("hdf5");
871#endif
872
873 return retval;
874}
875
876mxArray *
878{
879 mwSize nz = nzmax ();
880 mwSize nr = rows ();
881 mwSize nc = columns ();
882
883 mxArray *retval = new mxArray (interleaved, mxDOUBLE_CLASS, nr, nc, nz,
884 mxCOMPLEX);
885
886 mwIndex *ir = retval->get_ir ();
887
888 const Complex *pdata = matrix.data ();
889 const octave_idx_type *pridx = matrix.ridx ();
890
891 if (interleaved)
892 {
893 mxComplexDouble *pd
894 = static_cast<mxComplexDouble *> (retval->get_data ());
895
896 for (mwIndex i = 0; i < nz; i++)
897 {
898 pd[i].real = pdata[i].real ();
899 pd[i].imag = pdata[i].imag ();
900
901 ir[i] = pridx[i];
902 }
903 }
904 else
905 {
906 mxDouble *pr = static_cast<mxDouble *> (retval->get_data ());
907 mxDouble *pi = static_cast<mxDouble *> (retval->get_imag_data ());
908
909 for (mwIndex i = 0; i < nz; i++)
910 {
911 pr[i] = pdata[i].real ();
912 pi[i] = pdata[i].imag ();
913
914 ir[i] = pridx[i];
915 }
916 }
917
918 mwIndex *jc = retval->get_jc ();
919
920 const octave_idx_type *pcidx = matrix.cidx ();
921
922 for (mwIndex i = 0; i < nc + 1; i++)
923 jc[i] = pcidx[i];
924
925 return retval;
926}
927
930{
931 switch (umap)
932 {
933 // Mappers handled specially.
934 case umap_real:
936 case umap_imag:
938
939#define ARRAY_METHOD_MAPPER(UMAP, FCN) \
940 case umap_ ## UMAP: \
941 return octave_value (matrix.FCN ())
942
944
945#define ARRAY_MAPPER(UMAP, TYPE, FCN) \
946 case umap_ ## UMAP: \
947 return octave_value (matrix.map<TYPE> (FCN))
948
951 ARRAY_MAPPER (angle, double, std::arg);
952 ARRAY_MAPPER (arg, double, std::arg);
963 ARRAY_MAPPER (conj, Complex, std::conj<double>);
964 ARRAY_MAPPER (cos, Complex, std::cos);
965 ARRAY_MAPPER (cosh, Complex, std::cosh);
966 ARRAY_MAPPER (exp, Complex, std::exp);
970 ARRAY_MAPPER (log, Complex, std::log);
972 ARRAY_MAPPER (log10, Complex, std::log10);
977 ARRAY_MAPPER (sin, Complex, std::sin);
978 ARRAY_MAPPER (sinh, Complex, std::sinh);
979 ARRAY_MAPPER (sqrt, Complex, std::sqrt);
980 ARRAY_MAPPER (tan, Complex, std::tan);
981 ARRAY_MAPPER (tanh, Complex, std::tanh);
986
987 default: // Attempt to go via dense matrix.
989 }
990}
ComplexColumnVector conj(const ComplexColumnVector &a)
Definition: CColVector.cc:217
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
Definition: dMatrix.h:42
OCTAVE_API bool any_element_is_nan(void) const
Definition: CSparse.cc:7273
OCTAVE_API bool all_elements_are_real(void) const
Definition: CSparse.cc:7305
OCTAVE_API bool all_integers(double &max_val, double &min_val) const
Definition: CSparse.cc:7315
OCTAVE_API bool too_large_for_float(void) const
Definition: CSparse.cc:7353
OCTAVE_API ComplexMatrix matrix_value(void) const
Definition: CSparse.cc:600
octave_idx_type rows(void) const
Definition: Sparse.h:351
T * data(void)
Definition: Sparse.h:574
std::size_t byte_size(void) const
Definition: Sparse.h:364
OCTAVE_API void resize(octave_idx_type r, octave_idx_type c)
Definition: Sparse.cc:991
T * xdata(void)
Definition: Sparse.h:576
octave_idx_type nnz(void) const
Actual number of nonzero terms.
Definition: Sparse.h:339
octave_idx_type * xridx(void)
Definition: Sparse.h:589
octave_idx_type * ridx(void)
Definition: Sparse.h:583
octave_idx_type * cidx(void)
Definition: Sparse.h:596
octave_idx_type * xcidx(void)
Definition: Sparse.h:602
Sparse< T, Alloc > maybe_compress(bool remove_zeros=false)
Definition: Sparse.h:531
octave_idx_type cols(void) const
Definition: Sparse.h:352
bool indices_ok(void) const
Definition: Sparse.h:749
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:94
octave_idx_type ndims(void) const
Number of dimensions.
Definition: dim-vector.h:257
mwIndex * get_jc(void) const
Definition: mxarray.h:634
mwIndex * get_ir(void) const
Definition: mxarray.h:632
void * get_data(void) const
Definition: mxarray.h:497
void * get_imag_data(void) const
Definition: mxarray.h:562
octave_idx_type nzmax(void) const
octave_idx_type numel(void) const
OCTINTERP_API octave_value map(octave_base_value::unary_mapper_t umap) const
octave_idx_type columns(void) const
Definition: ov-base.h:355
OCTINTERP_API void warn_load(const char *type) const
Definition: ov-base.cc:1152
octave_idx_type rows(void) const
Definition: ov-base.h:348
OCTINTERP_API void warn_save(const char *type) const
Definition: ov-base.cc:1161
bool isempty(void) const
Definition: ov-base.h:391
SparseComplexMatrix sparse_complex_matrix_value(bool=false) const
Definition: ov-cx-sparse.h:125
octave_value map(unary_mapper_t umap) const
Complex complex_value(bool=false) const
octave_value as_double(void) const
SparseBoolMatrix sparse_bool_matrix_value(bool warn=false) const
bool load_hdf5(octave_hdf5_id loc_id, const char *name)
ComplexNDArray complex_array_value(bool=false) const
Matrix matrix_value(bool=false) const
octave_base_value * try_narrowing_conversion(void)
Definition: ov-cx-sparse.cc:61
double double_value(bool=false) const
bool save_hdf5(octave_hdf5_id loc_id, const char *name, bool save_as_floats)
ComplexMatrix complex_matrix_value(bool=false) const
mxArray * as_mxArray(bool interleaved) const
SparseMatrix sparse_matrix_value(bool=false) const
charNDArray char_array_value(bool frc_str_conv=false) const
bool load_binary(std::istream &is, bool swap, octave::mach_info::float_format fmt)
bool save_binary(std::ostream &os, bool save_as_floats)
const octave_hdf5_id octave_H5P_DEFAULT
const octave_hdf5_id octave_H5S_ALL
ColumnVector real(const ComplexColumnVector &a)
Definition: dColVector.cc:137
ColumnVector imag(const ComplexColumnVector &a)
Definition: dColVector.cc:143
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:1055
void error(const char *fmt,...)
Definition: error.cc:980
void err_invalid_conversion(const std::string &from, const std::string &to)
Definition: errwarn.cc:71
void warn_logical_conversion(void)
Definition: errwarn.cc:365
void warn_implicit_conversion(const char *id, const char *from, const char *to)
Definition: errwarn.cc:344
QString name
int save_hdf5_empty(octave_hdf5_id loc_id, const char *name, const dim_vector &d)
Definition: ls-hdf5.cc:1251
int load_hdf5_empty(octave_hdf5_id loc_id, const char *name, dim_vector &d)
Definition: ls-hdf5.cc:1307
octave_hdf5_id save_type_to_hdf5(save_type st)
Definition: ls-hdf5.cc:1351
octave_hdf5_id hdf5_make_complex_type(octave_hdf5_id num_type)
Definition: ls-hdf5.cc:405
bool hdf5_types_compatible(octave_hdf5_id t1, octave_hdf5_id t2)
Definition: ls-hdf5.cc:269
OCTAVE_NAMESPACE_BEGIN save_type get_save_type(double, double)
Definition: ls-utils.cc:40
void mxArray
Definition: mex.h:58
class OCTAVE_API charNDArray
Definition: mx-fwd.h:43
class OCTAVE_API ComplexNDArray
Definition: mx-fwd.h:39
std::complex< double > erfi(std::complex< double > z, double relerr=0)
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 > erf(std::complex< double > z, double relerr=0)
double fix(double x)
Definition: lo-mappers.h:118
Complex atan(const Complex &x)
Definition: lo-mappers.h:71
double signum(double x)
Definition: lo-mappers.h:229
double asinh(double x)
Definition: lo-specfun.h:58
bool isna(double x)
Definition: lo-mappers.cc:47
double atanh(double x)
Definition: lo-specfun.h:63
bool isfinite(double x)
Definition: lo-mappers.h:192
double roundb(double x)
Definition: lo-mappers.h:147
static const double pi
Definition: lo-specfun.cc:1995
bool isnan(bool)
Definition: lo-mappers.h:178
Complex log1p(const Complex &x)
Definition: lo-specfun.cc:1958
bool isinf(double x)
Definition: lo-mappers.h:203
Complex acos(const Complex &x)
Definition: lo-mappers.cc:85
double round(double x)
Definition: lo-mappers.h:136
Complex asin(const Complex &x)
Definition: lo-mappers.cc:107
double erfcx(double x)
Definition: lo-specfun.cc:1755
Complex erfc(const Complex &x)
Definition: lo-specfun.cc:1652
double acosh(double x)
Definition: lo-specfun.h:40
double erfi(double x)
Definition: lo-specfun.cc:1774
std::complex< T > ceil(const std::complex< T > &x)
Definition: lo-mappers.h:103
std::complex< T > floor(const std::complex< T > &x)
Definition: lo-mappers.h:130
double dawson(double x)
Definition: lo-specfun.cc:1517
Complex log2(const Complex &x)
Definition: lo-mappers.cc:139
Complex erf(const Complex &x)
Definition: lo-specfun.cc:1637
Complex expm1(const Complex &x)
Definition: lo-specfun.cc:1874
void err_nan_to_logical_conversion(void)
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
bool Vsparse_auto_mutate
Definition: ov-base.cc:103
#define DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA(t, n, c)
Definition: ov-base.h:222
#define ARRAY_MAPPER(UMAP, TYPE, FCN)
#define ARRAY_METHOD_MAPPER(UMAP, FCN)
static T abs(T x)
Definition: pr-output.cc:1678