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