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