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