5164
|
1 /* |
|
2 |
|
3 Copyright (C) 2004 David Bateman |
7016
|
4 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004 Andy Adler |
|
5 |
|
6 This file is part of Octave. |
5164
|
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 |
7016
|
10 Free Software Foundation; either version 3 of the License, or (at your |
|
11 option) any later version. |
5164
|
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 |
7016
|
19 along with Octave; see the file COPYING. If not, see |
|
20 <http://www.gnu.org/licenses/>. |
5164
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
|
28 #include <climits> |
|
29 |
|
30 #include <iostream> |
|
31 #include <vector> |
|
32 |
|
33 #include "ov-base.h" |
|
34 #include "ov-scalar.h" |
|
35 #include "ov-complex.h" |
|
36 #include "gripes.h" |
|
37 |
|
38 #include "ov-re-sparse.h" |
|
39 #include "ov-cx-sparse.h" |
|
40 |
|
41 #include "ov-base-sparse.h" |
|
42 #include "ov-base-sparse.cc" |
|
43 |
|
44 #include "ov-bool-sparse.h" |
|
45 |
6109
|
46 template class OCTINTERP_API octave_base_sparse<SparseComplexMatrix>; |
5164
|
47 |
|
48 DEFINE_OCTAVE_ALLOCATOR (octave_sparse_complex_matrix); |
|
49 |
6823
|
50 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_sparse_complex_matrix, "sparse complex matrix", "double"); |
5164
|
51 |
5759
|
52 octave_base_value * |
5164
|
53 octave_sparse_complex_matrix::try_narrowing_conversion (void) |
|
54 { |
5759
|
55 octave_base_value *retval = 0; |
5164
|
56 |
|
57 int nr = matrix.rows (); |
|
58 int nc = matrix.cols (); |
|
59 |
|
60 // Don't use numel, since it can overflow for very large matrices |
|
61 // Note that for the tests on matrix size, they become approximative |
|
62 // since they involves a cast to double to avoid issues of overflow |
|
63 if (matrix.rows () == 1 && matrix.cols () == 1) |
|
64 { |
|
65 // Const copy of the matrix, so the right version of () operator used |
|
66 const SparseComplexMatrix tmp (matrix); |
|
67 |
|
68 Complex c = tmp (0, 0); |
|
69 |
|
70 if (imag (c) == 0.0) |
|
71 retval = new octave_scalar (std::real (c)); |
|
72 else |
|
73 retval = new octave_complex (c); |
|
74 } |
|
75 else if (nr == 0 || nc == 0) |
|
76 retval = new octave_matrix (Matrix (nr, nc)); |
|
77 else if (matrix.all_elements_are_real ()) |
|
78 if (matrix.cols () > 0 && matrix.rows () > 0 && |
|
79 double (matrix.byte_size ()) > double (matrix.rows ()) * |
|
80 double (matrix.cols ()) * sizeof (double)) |
|
81 retval = new octave_matrix (::real (matrix.matrix_value ())); |
|
82 else |
|
83 retval = new octave_sparse_matrix (::real (matrix)); |
|
84 else if (matrix.cols () > 0 && matrix.rows () > 0 && |
|
85 double (matrix.byte_size ()) > double (matrix.rows ()) * |
|
86 double (matrix.cols ()) * sizeof (Complex)) |
|
87 retval = new octave_complex_matrix (matrix.matrix_value ()); |
|
88 |
|
89 return retval; |
|
90 } |
|
91 |
|
92 void |
|
93 octave_sparse_complex_matrix::assign (const octave_value_list& idx, |
|
94 const SparseComplexMatrix& rhs) |
|
95 { |
|
96 octave_base_sparse<SparseComplexMatrix>::assign (idx, rhs); |
|
97 } |
|
98 |
|
99 void |
|
100 octave_sparse_complex_matrix::assign (const octave_value_list& idx, |
|
101 const SparseMatrix& rhs) |
|
102 { |
|
103 int len = idx.length (); |
|
104 |
|
105 for (int i = 0; i < len; i++) |
|
106 matrix.set_index (idx(i).index_vector ()); |
|
107 |
|
108 ::assign (matrix, rhs); |
|
109 } |
|
110 |
|
111 bool |
|
112 octave_sparse_complex_matrix::valid_as_scalar_index (void) const |
|
113 { |
5775
|
114 // FIXME |
5164
|
115 return false; |
|
116 } |
|
117 |
|
118 double |
|
119 octave_sparse_complex_matrix::double_value (bool force_conversion) const |
|
120 { |
|
121 double retval = lo_ieee_nan_value (); |
|
122 |
5781
|
123 if (! force_conversion) |
|
124 gripe_implicit_conversion ("Octave:imag-to-real", |
|
125 "complex sparse matrix", "real scalar"); |
5164
|
126 |
5775
|
127 // FIXME -- maybe this should be a function, valid_as_scalar() |
5164
|
128 if (numel () > 0) |
|
129 { |
6221
|
130 if (numel () > 1) |
|
131 gripe_implicit_conversion ("Octave:array-as-scalar", |
|
132 "complex sparse matrix", "real scalar"); |
5164
|
133 |
|
134 retval = std::real (matrix (0, 0)); |
|
135 } |
|
136 else |
|
137 gripe_invalid_conversion ("complex sparse matrix", "real scalar"); |
|
138 |
|
139 return retval; |
|
140 } |
|
141 |
|
142 Matrix |
|
143 octave_sparse_complex_matrix::matrix_value (bool force_conversion) const |
|
144 { |
|
145 Matrix retval; |
|
146 |
5781
|
147 if (! force_conversion) |
|
148 gripe_implicit_conversion ("Octave:imag-to-real", |
|
149 "complex sparse matrix", "real matrix"); |
5164
|
150 |
|
151 retval = ::real (matrix.matrix_value ()); |
|
152 |
|
153 return retval; |
|
154 } |
|
155 |
|
156 Complex |
|
157 octave_sparse_complex_matrix::complex_value (bool) const |
|
158 { |
|
159 double tmp = lo_ieee_nan_value (); |
|
160 |
|
161 Complex retval (tmp, tmp); |
|
162 |
5775
|
163 // FIXME -- maybe this should be a function, valid_as_scalar() |
5164
|
164 if (numel () > 0) |
|
165 { |
6221
|
166 if (numel () > 1) |
|
167 gripe_implicit_conversion ("Octave:array-as-scalar", |
|
168 "complex sparse matrix", "real scalar"); |
5164
|
169 |
|
170 retval = matrix (0, 0); |
|
171 } |
|
172 else |
|
173 gripe_invalid_conversion ("complex sparse matrix", "real scalar"); |
|
174 |
|
175 return retval; |
|
176 } |
|
177 |
|
178 ComplexMatrix |
|
179 octave_sparse_complex_matrix::complex_matrix_value (bool) const |
|
180 { |
|
181 return matrix.matrix_value (); |
|
182 } |
|
183 |
|
184 ComplexNDArray |
|
185 octave_sparse_complex_matrix::complex_array_value (bool) const |
|
186 { |
|
187 return ComplexNDArray (matrix.matrix_value ()); |
|
188 } |
|
189 |
|
190 SparseMatrix |
|
191 octave_sparse_complex_matrix::sparse_matrix_value (bool force_conversion) const |
|
192 { |
|
193 SparseMatrix retval; |
|
194 |
5781
|
195 if (! force_conversion) |
|
196 gripe_implicit_conversion ("Octave:imag-to-real", |
|
197 "complex sparse matrix", |
5164
|
198 "real sparse matrix"); |
|
199 |
|
200 retval = ::real (matrix); |
|
201 |
|
202 return retval; |
|
203 } |
|
204 |
|
205 bool |
|
206 octave_sparse_complex_matrix::save_binary (std::ostream& os, |
|
207 bool&save_as_floats) |
|
208 { |
|
209 dim_vector d = this->dims (); |
|
210 if (d.length() < 1) |
|
211 return false; |
|
212 |
|
213 // Ensure that additional memory is deallocated |
|
214 matrix.maybe_compress (); |
|
215 |
|
216 int nr = d(0); |
|
217 int nc = d(1); |
5604
|
218 int nz = nzmax (); |
5164
|
219 |
5828
|
220 int32_t itmp; |
5164
|
221 // Use negative value for ndims to be consistent with other formats |
|
222 itmp= -2; |
5760
|
223 os.write (reinterpret_cast<char *> (&itmp), 4); |
5164
|
224 |
|
225 itmp= nr; |
5760
|
226 os.write (reinterpret_cast<char *> (&itmp), 4); |
5164
|
227 |
|
228 itmp= nc; |
5760
|
229 os.write (reinterpret_cast<char *> (&itmp), 4); |
5164
|
230 |
|
231 itmp= nz; |
5760
|
232 os.write (reinterpret_cast<char *> (&itmp), 4); |
5164
|
233 |
|
234 save_type st = LS_DOUBLE; |
|
235 if (save_as_floats) |
|
236 { |
|
237 if (matrix.too_large_for_float ()) |
|
238 { |
|
239 warning ("save: some values too large to save as floats --"); |
|
240 warning ("save: saving as doubles instead"); |
|
241 } |
|
242 else |
|
243 st = LS_FLOAT; |
|
244 } |
5775
|
245 else if (matrix.nzmax () > 8192) // FIXME -- make this configurable. |
5164
|
246 { |
|
247 double max_val, min_val; |
|
248 if (matrix.all_integers (max_val, min_val)) |
|
249 st = get_save_type (max_val, min_val); |
|
250 } |
|
251 |
|
252 // add one to the printed indices to go from |
|
253 // zero-based to one-based arrays |
|
254 for (int i = 0; i < nc+1; i++) |
|
255 { |
|
256 OCTAVE_QUIT; |
|
257 itmp = matrix.cidx(i); |
5760
|
258 os.write (reinterpret_cast<char *> (&itmp), 4); |
5164
|
259 } |
|
260 |
|
261 for (int i = 0; i < nz; i++) |
|
262 { |
|
263 OCTAVE_QUIT; |
|
264 itmp = matrix.ridx(i); |
5760
|
265 os.write (reinterpret_cast<char *> (&itmp), 4); |
5164
|
266 } |
|
267 |
5760
|
268 write_doubles (os, reinterpret_cast<const double *> (matrix.data()), st, 2 * nz); |
5164
|
269 |
|
270 return true; |
|
271 } |
|
272 |
|
273 bool |
|
274 octave_sparse_complex_matrix::load_binary (std::istream& is, bool swap, |
|
275 oct_mach_info::float_format fmt) |
|
276 { |
5828
|
277 int32_t nz, nc, nr, tmp; |
5327
|
278 char ctmp; |
|
279 |
5760
|
280 if (! is.read (reinterpret_cast<char *> (&tmp), 4)) |
5164
|
281 return false; |
|
282 |
|
283 if (swap) |
|
284 swap_bytes<4> (&tmp); |
|
285 |
|
286 if (tmp != -2) { |
|
287 error("load: only 2D sparse matrices are supported"); |
|
288 return false; |
|
289 } |
|
290 |
5760
|
291 if (! is.read (reinterpret_cast<char *> (&nr), 4)) |
5164
|
292 return false; |
5760
|
293 if (! is.read (reinterpret_cast<char *> (&nc), 4)) |
5164
|
294 return false; |
5760
|
295 if (! is.read (reinterpret_cast<char *> (&nz), 4)) |
5164
|
296 return false; |
|
297 |
|
298 if (swap) |
|
299 { |
|
300 swap_bytes<4> (&nr); |
|
301 swap_bytes<4> (&nc); |
|
302 swap_bytes<4> (&nz); |
|
303 } |
|
304 |
5275
|
305 SparseComplexMatrix m (static_cast<octave_idx_type> (nr), |
|
306 static_cast<octave_idx_type> (nc), |
|
307 static_cast<octave_idx_type> (nz)); |
5164
|
308 |
|
309 for (int i = 0; i < nc+1; i++) |
|
310 { |
|
311 OCTAVE_QUIT; |
5760
|
312 if (! is.read (reinterpret_cast<char *> (&tmp), 4)) |
5164
|
313 return false; |
|
314 if (swap) |
|
315 swap_bytes<4> (&tmp); |
|
316 m.cidx(i) = tmp; |
|
317 } |
|
318 |
|
319 for (int i = 0; i < nz; i++) |
|
320 { |
|
321 OCTAVE_QUIT; |
5760
|
322 if (! is.read (reinterpret_cast<char *> (&tmp), 4)) |
5164
|
323 return false; |
|
324 if (swap) |
|
325 swap_bytes<4> (&tmp); |
|
326 m.ridx(i) = tmp; |
|
327 } |
|
328 |
5760
|
329 if (! is.read (reinterpret_cast<char *> (&ctmp), 1)) |
5164
|
330 return false; |
|
331 |
5760
|
332 read_doubles (is, reinterpret_cast<double *> (m.data ()), |
|
333 static_cast<save_type> (ctmp), 2 * nz, swap, fmt); |
5164
|
334 |
|
335 if (error_state || ! is) |
|
336 return false; |
|
337 matrix = m; |
|
338 |
|
339 return true; |
|
340 } |
|
341 |
|
342 #if defined (HAVE_HDF5) |
5900
|
343 |
5164
|
344 bool |
|
345 octave_sparse_complex_matrix::save_hdf5 (hid_t loc_id, const char *name, |
|
346 bool save_as_floats) |
|
347 { |
|
348 dim_vector dv = dims (); |
|
349 int empty = save_hdf5_empty (loc_id, name, dv); |
|
350 if (empty) |
|
351 return (empty > 0); |
|
352 |
|
353 // Ensure that additional memory is deallocated |
|
354 matrix.maybe_compress (); |
|
355 |
|
356 hid_t group_hid = H5Gcreate (loc_id, name, 0); |
|
357 if (group_hid < 0) |
|
358 return false; |
|
359 |
|
360 hid_t space_hid = -1, data_hid = -1; |
|
361 bool retval = true; |
|
362 SparseComplexMatrix m = sparse_complex_matrix_value (); |
5351
|
363 octave_idx_type tmp; |
5164
|
364 hsize_t hdims[2]; |
|
365 |
5760
|
366 space_hid = H5Screate_simple (0, hdims, 0); |
5164
|
367 if (space_hid < 0) |
|
368 { |
|
369 H5Gclose (group_hid); |
|
370 return false; |
|
371 } |
|
372 |
5351
|
373 data_hid = H5Dcreate (group_hid, "nr", H5T_NATIVE_IDX, space_hid, |
5164
|
374 H5P_DEFAULT); |
|
375 if (data_hid < 0) |
|
376 { |
|
377 H5Sclose (space_hid); |
|
378 H5Gclose (group_hid); |
|
379 return false; |
|
380 } |
|
381 |
|
382 tmp = m.rows (); |
5351
|
383 retval = H5Dwrite (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL, |
5760
|
384 H5P_DEFAULT, &tmp) >= 0; |
5164
|
385 H5Dclose (data_hid); |
|
386 if (!retval) |
|
387 { |
|
388 H5Sclose (space_hid); |
|
389 H5Gclose (group_hid); |
|
390 return false; |
|
391 } |
|
392 |
5351
|
393 data_hid = H5Dcreate (group_hid, "nc", H5T_NATIVE_IDX, space_hid, |
5164
|
394 H5P_DEFAULT); |
|
395 if (data_hid < 0) |
|
396 { |
|
397 H5Sclose (space_hid); |
|
398 H5Gclose (group_hid); |
|
399 return false; |
|
400 } |
|
401 |
|
402 tmp = m.cols (); |
5351
|
403 retval = H5Dwrite (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL, |
5760
|
404 H5P_DEFAULT, &tmp) >= 0; |
5164
|
405 H5Dclose (data_hid); |
|
406 if (!retval) |
|
407 { |
|
408 H5Sclose (space_hid); |
|
409 H5Gclose (group_hid); |
|
410 return false; |
|
411 } |
|
412 |
5351
|
413 data_hid = H5Dcreate (group_hid, "nz", H5T_NATIVE_IDX, space_hid, |
5164
|
414 H5P_DEFAULT); |
|
415 if (data_hid < 0) |
|
416 { |
|
417 H5Sclose (space_hid); |
|
418 H5Gclose (group_hid); |
|
419 return false; |
|
420 } |
|
421 |
5604
|
422 tmp = m.nzmax (); |
5351
|
423 retval = H5Dwrite (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL, |
5760
|
424 H5P_DEFAULT, &tmp) >= 0; |
5164
|
425 H5Dclose (data_hid); |
|
426 if (!retval) |
|
427 { |
|
428 H5Sclose (space_hid); |
|
429 H5Gclose (group_hid); |
|
430 return false; |
|
431 } |
|
432 |
|
433 H5Sclose (space_hid); |
|
434 |
|
435 hdims[0] = m.cols() + 1; |
|
436 hdims[1] = 1; |
|
437 |
|
438 space_hid = H5Screate_simple (2, hdims, 0); |
|
439 |
|
440 if (space_hid < 0) |
|
441 { |
|
442 H5Gclose (group_hid); |
|
443 return false; |
|
444 } |
|
445 |
5351
|
446 data_hid = H5Dcreate (group_hid, "cidx", H5T_NATIVE_IDX, space_hid, |
5164
|
447 H5P_DEFAULT); |
|
448 if (data_hid < 0) |
|
449 { |
|
450 H5Sclose (space_hid); |
|
451 H5Gclose (group_hid); |
|
452 return false; |
|
453 } |
|
454 |
5351
|
455 octave_idx_type * itmp = m.xcidx (); |
|
456 retval = H5Dwrite (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL, |
5760
|
457 H5P_DEFAULT, itmp) >= 0; |
5164
|
458 H5Dclose (data_hid); |
|
459 if (!retval) |
|
460 { |
|
461 H5Sclose (space_hid); |
|
462 H5Gclose (group_hid); |
|
463 return false; |
|
464 } |
|
465 |
|
466 H5Sclose (space_hid); |
|
467 |
5604
|
468 hdims[0] = m.nzmax (); |
5164
|
469 hdims[1] = 1; |
|
470 |
|
471 space_hid = H5Screate_simple (2, hdims, 0); |
|
472 |
|
473 if (space_hid < 0) |
|
474 { |
|
475 H5Gclose (group_hid); |
|
476 return false; |
|
477 } |
|
478 |
5351
|
479 data_hid = H5Dcreate (group_hid, "ridx", H5T_NATIVE_IDX, space_hid, |
5164
|
480 H5P_DEFAULT); |
|
481 if (data_hid < 0) |
|
482 { |
|
483 H5Sclose (space_hid); |
|
484 H5Gclose (group_hid); |
|
485 return false; |
|
486 } |
|
487 |
|
488 itmp = m.xridx (); |
5760
|
489 retval = H5Dwrite (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL, H5P_DEFAULT, itmp) >= 0; |
5164
|
490 H5Dclose (data_hid); |
|
491 if (!retval) |
|
492 { |
|
493 H5Sclose (space_hid); |
|
494 H5Gclose (group_hid); |
|
495 return false; |
|
496 } |
|
497 |
|
498 hid_t save_type_hid = H5T_NATIVE_DOUBLE; |
|
499 |
|
500 if (save_as_floats) |
|
501 { |
|
502 if (m.too_large_for_float ()) |
|
503 { |
|
504 warning ("save: some values too large to save as floats --"); |
|
505 warning ("save: saving as doubles instead"); |
|
506 } |
|
507 else |
|
508 save_type_hid = H5T_NATIVE_FLOAT; |
|
509 } |
|
510 #if HAVE_HDF5_INT2FLOAT_CONVERSIONS |
|
511 // hdf5 currently doesn't support float/integer conversions |
|
512 else |
|
513 { |
|
514 double max_val, min_val; |
|
515 |
|
516 if (m.all_integers (max_val, min_val)) |
|
517 save_type_hid |
|
518 = save_type_to_hdf5 (get_save_type (max_val, min_val)); |
|
519 } |
|
520 #endif /* HAVE_HDF5_INT2FLOAT_CONVERSIONS */ |
|
521 |
|
522 hid_t type_hid = hdf5_make_complex_type (save_type_hid); |
|
523 if (type_hid < 0) |
|
524 { |
|
525 H5Sclose (space_hid); |
|
526 H5Gclose (group_hid); |
|
527 return false; |
|
528 } |
|
529 |
5760
|
530 data_hid = H5Dcreate (group_hid, "data", type_hid, space_hid, H5P_DEFAULT); |
5164
|
531 if (data_hid < 0) |
|
532 { |
|
533 H5Sclose (space_hid); |
|
534 H5Tclose (type_hid); |
|
535 H5Gclose (group_hid); |
|
536 return false; |
|
537 } |
|
538 |
|
539 hid_t complex_type_hid = hdf5_make_complex_type (H5T_NATIVE_DOUBLE); |
|
540 retval = false; |
|
541 if (complex_type_hid >= 0) |
|
542 { |
|
543 Complex * ctmp = m.xdata (); |
|
544 |
|
545 retval = H5Dwrite (data_hid, complex_type_hid, H5S_ALL, H5S_ALL, |
5760
|
546 H5P_DEFAULT, ctmp) >= 0; |
5164
|
547 } |
|
548 |
|
549 H5Dclose (data_hid); |
|
550 H5Sclose (space_hid); |
|
551 H5Tclose (type_hid); |
|
552 H5Gclose (group_hid); |
|
553 |
|
554 return retval; |
|
555 } |
|
556 |
|
557 bool |
|
558 octave_sparse_complex_matrix::load_hdf5 (hid_t loc_id, const char *name, |
|
559 bool /* have_h5giterate_bug */) |
|
560 { |
5351
|
561 octave_idx_type nr, nc, nz; |
5164
|
562 hid_t group_hid, data_hid, space_hid; |
|
563 hsize_t rank; |
|
564 |
|
565 dim_vector dv; |
|
566 int empty = load_hdf5_empty (loc_id, name, dv); |
|
567 if (empty > 0) |
|
568 matrix.resize(dv); |
|
569 if (empty) |
|
570 return (empty > 0); |
|
571 |
|
572 group_hid = H5Gopen (loc_id, name); |
|
573 if (group_hid < 0 ) return false; |
|
574 |
|
575 data_hid = H5Dopen (group_hid, "nr"); |
|
576 space_hid = H5Dget_space (data_hid); |
|
577 rank = H5Sget_simple_extent_ndims (space_hid); |
|
578 |
|
579 if (rank != 0) |
|
580 { |
|
581 H5Dclose (data_hid); |
|
582 H5Gclose (group_hid); |
|
583 return false; |
|
584 } |
|
585 |
5760
|
586 if (H5Dread (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL, H5P_DEFAULT, &nr) < 0) |
5164
|
587 { |
|
588 H5Dclose (data_hid); |
|
589 H5Gclose (group_hid); |
|
590 return false; |
|
591 } |
|
592 |
|
593 H5Dclose (data_hid); |
|
594 |
|
595 data_hid = H5Dopen (group_hid, "nc"); |
|
596 space_hid = H5Dget_space (data_hid); |
|
597 rank = H5Sget_simple_extent_ndims (space_hid); |
|
598 |
|
599 if (rank != 0) |
|
600 { |
|
601 H5Dclose (data_hid); |
|
602 H5Gclose (group_hid); |
|
603 return false; |
|
604 } |
|
605 |
5760
|
606 if (H5Dread (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL, H5P_DEFAULT, &nc) < 0) |
5164
|
607 { |
|
608 H5Dclose (data_hid); |
|
609 H5Gclose (group_hid); |
|
610 return false; |
|
611 } |
|
612 |
|
613 H5Dclose (data_hid); |
|
614 |
|
615 data_hid = H5Dopen (group_hid, "nz"); |
|
616 space_hid = H5Dget_space (data_hid); |
|
617 rank = H5Sget_simple_extent_ndims (space_hid); |
|
618 |
|
619 if (rank != 0) |
|
620 { |
|
621 H5Dclose (data_hid); |
|
622 H5Gclose (group_hid); |
|
623 return false; |
|
624 } |
|
625 |
5760
|
626 if (H5Dread (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL, H5P_DEFAULT, &nz) < 0) |
5164
|
627 { |
|
628 H5Dclose (data_hid); |
|
629 H5Gclose (group_hid); |
|
630 return false; |
|
631 } |
|
632 |
|
633 H5Dclose (data_hid); |
|
634 |
5275
|
635 SparseComplexMatrix m (static_cast<octave_idx_type> (nr), |
|
636 static_cast<octave_idx_type> (nc), |
|
637 static_cast<octave_idx_type> (nz)); |
5164
|
638 |
|
639 data_hid = H5Dopen (group_hid, "cidx"); |
|
640 space_hid = H5Dget_space (data_hid); |
|
641 rank = H5Sget_simple_extent_ndims (space_hid); |
|
642 |
|
643 if (rank != 2) |
|
644 { |
|
645 H5Sclose (space_hid); |
|
646 H5Dclose (data_hid); |
|
647 H5Gclose (group_hid); |
|
648 return false; |
|
649 } |
|
650 |
|
651 OCTAVE_LOCAL_BUFFER (hsize_t, hdims, rank); |
|
652 OCTAVE_LOCAL_BUFFER (hsize_t, maxdims, rank); |
|
653 |
|
654 H5Sget_simple_extent_dims (space_hid, hdims, maxdims); |
|
655 |
5322
|
656 if (static_cast<int> (hdims[0]) != nc + 1 || |
|
657 static_cast<int> (hdims[1]) != 1) |
5164
|
658 { |
|
659 H5Sclose (space_hid); |
|
660 H5Dclose (data_hid); |
|
661 H5Gclose (group_hid); |
|
662 return false; |
|
663 } |
|
664 |
5351
|
665 octave_idx_type *itmp = m.xcidx (); |
5760
|
666 if (H5Dread (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL, H5P_DEFAULT, itmp) < 0) |
5164
|
667 { |
|
668 H5Sclose (space_hid); |
|
669 H5Dclose (data_hid); |
|
670 H5Gclose (group_hid); |
|
671 return false; |
|
672 } |
|
673 |
|
674 H5Sclose (space_hid); |
|
675 H5Dclose (data_hid); |
|
676 |
|
677 data_hid = H5Dopen (group_hid, "ridx"); |
|
678 space_hid = H5Dget_space (data_hid); |
|
679 rank = H5Sget_simple_extent_ndims (space_hid); |
|
680 |
|
681 if (rank != 2) |
|
682 { |
|
683 H5Sclose (space_hid); |
|
684 H5Dclose (data_hid); |
|
685 H5Gclose (group_hid); |
|
686 return false; |
|
687 } |
|
688 |
|
689 H5Sget_simple_extent_dims (space_hid, hdims, maxdims); |
|
690 |
5351
|
691 if (static_cast<int> (hdims[0]) != nz || |
|
692 static_cast<int> (hdims[1]) != 1) |
5164
|
693 { |
|
694 H5Sclose (space_hid); |
|
695 H5Dclose (data_hid); |
|
696 H5Gclose (group_hid); |
|
697 return false; |
|
698 } |
|
699 |
|
700 itmp = m.xridx (); |
5760
|
701 if (H5Dread (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL, H5P_DEFAULT, itmp) < 0) |
5164
|
702 { |
|
703 H5Sclose (space_hid); |
|
704 H5Dclose (data_hid); |
|
705 H5Gclose (group_hid); |
|
706 return false; |
|
707 } |
|
708 |
|
709 H5Sclose (space_hid); |
|
710 H5Dclose (data_hid); |
|
711 |
|
712 data_hid = H5Dopen (group_hid, "data"); |
|
713 hid_t type_hid = H5Dget_type (data_hid); |
|
714 |
|
715 hid_t complex_type = hdf5_make_complex_type (H5T_NATIVE_DOUBLE); |
|
716 |
|
717 if (! hdf5_types_compatible (type_hid, complex_type)) |
|
718 { |
|
719 H5Tclose (complex_type); |
|
720 H5Dclose (data_hid); |
|
721 H5Gclose (group_hid); |
|
722 return false; |
|
723 } |
|
724 |
|
725 space_hid = H5Dget_space (data_hid); |
|
726 rank = H5Sget_simple_extent_ndims (space_hid); |
|
727 |
|
728 if (rank != 2) |
|
729 { |
|
730 H5Sclose (space_hid); |
|
731 H5Dclose (data_hid); |
|
732 H5Gclose (group_hid); |
|
733 return false; |
|
734 } |
|
735 |
|
736 H5Sget_simple_extent_dims (space_hid, hdims, maxdims); |
|
737 |
5351
|
738 if (static_cast<int> (hdims[0]) != nz || |
|
739 static_cast<int> (hdims[1]) != 1) |
5164
|
740 { |
|
741 H5Sclose (space_hid); |
|
742 H5Dclose (data_hid); |
|
743 H5Gclose (group_hid); |
|
744 return false; |
|
745 } |
|
746 |
|
747 Complex *ctmp = m.xdata (); |
|
748 bool retval = false; |
5760
|
749 if (H5Dread (data_hid, complex_type, H5S_ALL, H5S_ALL, H5P_DEFAULT, ctmp) >= 0) |
5164
|
750 { |
|
751 retval = true; |
|
752 matrix = m; |
|
753 } |
|
754 |
|
755 H5Tclose (complex_type); |
|
756 H5Sclose (space_hid); |
|
757 H5Dclose (data_hid); |
|
758 H5Gclose (group_hid); |
|
759 |
|
760 return retval; |
|
761 } |
|
762 |
|
763 #endif |
|
764 |
5900
|
765 mxArray * |
|
766 octave_sparse_complex_matrix::as_mxArray (void) const |
|
767 { |
6686
|
768 mwSize nz = nzmax (); |
5903
|
769 mxArray *retval = new mxArray (mxDOUBLE_CLASS, rows (), columns (), |
|
770 nz, mxCOMPLEX); |
|
771 double *pr = static_cast<double *> (retval->get_data ()); |
|
772 double *pi = static_cast<double *> (retval->get_imag_data ()); |
6686
|
773 mwIndex *ir = retval->get_ir (); |
|
774 mwIndex *jc = retval->get_jc (); |
5903
|
775 |
6686
|
776 for (mwIndex i = 0; i < nz; i++) |
5903
|
777 { |
|
778 Complex val = matrix.data(i); |
|
779 pr[i] = real (val); |
|
780 pi[i] = imag (val); |
|
781 ir[i] = matrix.ridx(i); |
|
782 } |
|
783 |
6686
|
784 for (mwIndex i = 0; i < columns() + 1; i++) |
5903
|
785 jc[i] = matrix.cidx(i); |
|
786 |
|
787 return retval; |
5900
|
788 } |
|
789 |
5164
|
790 /* |
|
791 ;;; Local Variables: *** |
|
792 ;;; mode: C++ *** |
|
793 ;;; End: *** |
|
794 */ |