Mercurial > hg > octave-nkf
annotate src/ov-cx-sparse.cc @ 9892:ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
author | Kacper Kowalik <xarthisius.kk@gmail.com> |
---|---|
date | Tue, 01 Dec 2009 00:49:07 +0100 |
parents | b3089dba88bf |
children | 829e69ec3110 |
rev | line source |
---|---|
5164 | 1 /* |
2 | |
8920 | 3 Copyright (C) 2004, 2005, 2006, 2007, 2008 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 | |
7503
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7193
diff
changeset
|
33 #include "lo-specfun.h" |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7193
diff
changeset
|
34 #include "lo-mappers.h" |
8377
25bc2d31e1bf
improve OCTAVE_LOCAL_BUFFER
Jaroslav Hajek <highegg@gmail.com>
parents:
7740
diff
changeset
|
35 #include "oct-locbuf.h" |
7503
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7193
diff
changeset
|
36 |
5164 | 37 #include "ov-base.h" |
38 #include "ov-scalar.h" | |
39 #include "ov-complex.h" | |
40 #include "gripes.h" | |
41 | |
42 #include "ov-re-sparse.h" | |
43 #include "ov-cx-sparse.h" | |
44 | |
45 #include "ov-base-sparse.h" | |
46 #include "ov-base-sparse.cc" | |
47 | |
48 #include "ov-bool-sparse.h" | |
49 | |
6109 | 50 template class OCTINTERP_API octave_base_sparse<SparseComplexMatrix>; |
5164 | 51 |
52 DEFINE_OCTAVE_ALLOCATOR (octave_sparse_complex_matrix); | |
53 | |
6823 | 54 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_sparse_complex_matrix, "sparse complex matrix", "double"); |
5164 | 55 |
5759 | 56 octave_base_value * |
5164 | 57 octave_sparse_complex_matrix::try_narrowing_conversion (void) |
58 { | |
5759 | 59 octave_base_value *retval = 0; |
5164 | 60 |
7193 | 61 if (Vsparse_auto_mutate) |
62 { | |
63 int nr = matrix.rows (); | |
64 int nc = matrix.cols (); | |
5164 | 65 |
7193 | 66 // Don't use numel, since it can overflow for very large matrices |
67 // Note that for the tests on matrix size, they become approximative | |
68 // since they involves a cast to double to avoid issues of overflow | |
69 if (matrix.rows () == 1 && matrix.cols () == 1) | |
70 { | |
71 // Const copy of the matrix, so the right version of () operator used | |
72 const SparseComplexMatrix tmp (matrix); | |
5164 | 73 |
7193 | 74 Complex c = tmp (0, 0); |
5164 | 75 |
7503
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7193
diff
changeset
|
76 if (std::imag (c) == 0.0) |
7193 | 77 retval = new octave_scalar (std::real (c)); |
78 else | |
79 retval = new octave_complex (c); | |
80 } | |
81 else if (nr == 0 || nc == 0) | |
82 retval = new octave_matrix (Matrix (nr, nc)); | |
83 else if (matrix.all_elements_are_real ()) | |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
84 if (matrix.cols () > 0 && matrix.rows () > 0 |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
85 && (double (matrix.byte_size ()) > double (matrix.rows ()) |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
86 * double (matrix.cols ()) * sizeof (double))) |
7193 | 87 retval = new octave_matrix (::real (matrix.matrix_value ())); |
88 else | |
89 retval = new octave_sparse_matrix (::real (matrix)); | |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
90 else if (matrix.cols () > 0 && matrix.rows () > 0 |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
91 && (double (matrix.byte_size ()) > double (matrix.rows ()) |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
92 * double (matrix.cols ()) * sizeof (Complex))) |
7193 | 93 retval = new octave_complex_matrix (matrix.matrix_value ()); |
5164 | 94 } |
7193 | 95 else |
96 { | |
97 if (matrix.all_elements_are_real ()) | |
98 retval = new octave_sparse_matrix (::real (matrix)); | |
99 } | |
5164 | 100 |
101 return retval; | |
102 } | |
103 | |
104 void | |
105 octave_sparse_complex_matrix::assign (const octave_value_list& idx, | |
106 const SparseComplexMatrix& rhs) | |
107 { | |
108 octave_base_sparse<SparseComplexMatrix>::assign (idx, rhs); | |
109 } | |
110 | |
111 void | |
112 octave_sparse_complex_matrix::assign (const octave_value_list& idx, | |
113 const SparseMatrix& rhs) | |
114 { | |
115 int len = idx.length (); | |
116 | |
117 for (int i = 0; i < len; i++) | |
118 matrix.set_index (idx(i).index_vector ()); | |
119 | |
120 ::assign (matrix, rhs); | |
121 } | |
122 | |
123 double | |
124 octave_sparse_complex_matrix::double_value (bool force_conversion) const | |
125 { | |
126 double retval = lo_ieee_nan_value (); | |
127 | |
5781 | 128 if (! force_conversion) |
129 gripe_implicit_conversion ("Octave:imag-to-real", | |
130 "complex sparse matrix", "real scalar"); | |
5164 | 131 |
5775 | 132 // FIXME -- maybe this should be a function, valid_as_scalar() |
5164 | 133 if (numel () > 0) |
134 { | |
6221 | 135 if (numel () > 1) |
136 gripe_implicit_conversion ("Octave:array-as-scalar", | |
137 "complex sparse matrix", "real scalar"); | |
5164 | 138 |
139 retval = std::real (matrix (0, 0)); | |
140 } | |
141 else | |
142 gripe_invalid_conversion ("complex sparse matrix", "real scalar"); | |
143 | |
144 return retval; | |
145 } | |
146 | |
147 Matrix | |
148 octave_sparse_complex_matrix::matrix_value (bool force_conversion) const | |
149 { | |
150 Matrix retval; | |
151 | |
5781 | 152 if (! force_conversion) |
153 gripe_implicit_conversion ("Octave:imag-to-real", | |
154 "complex sparse matrix", "real matrix"); | |
5164 | 155 |
156 retval = ::real (matrix.matrix_value ()); | |
157 | |
158 return retval; | |
159 } | |
160 | |
161 Complex | |
162 octave_sparse_complex_matrix::complex_value (bool) const | |
163 { | |
164 double tmp = lo_ieee_nan_value (); | |
165 | |
166 Complex retval (tmp, tmp); | |
167 | |
5775 | 168 // FIXME -- maybe this should be a function, valid_as_scalar() |
5164 | 169 if (numel () > 0) |
170 { | |
6221 | 171 if (numel () > 1) |
172 gripe_implicit_conversion ("Octave:array-as-scalar", | |
173 "complex sparse matrix", "real scalar"); | |
5164 | 174 |
175 retval = matrix (0, 0); | |
176 } | |
177 else | |
178 gripe_invalid_conversion ("complex sparse matrix", "real scalar"); | |
179 | |
180 return retval; | |
181 } | |
182 | |
183 ComplexMatrix | |
184 octave_sparse_complex_matrix::complex_matrix_value (bool) const | |
185 { | |
186 return matrix.matrix_value (); | |
187 } | |
188 | |
189 ComplexNDArray | |
190 octave_sparse_complex_matrix::complex_array_value (bool) const | |
191 { | |
192 return ComplexNDArray (matrix.matrix_value ()); | |
193 } | |
194 | |
7503
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7193
diff
changeset
|
195 charNDArray |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7193
diff
changeset
|
196 octave_sparse_complex_matrix::char_array_value (bool frc_str_conv) const |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7193
diff
changeset
|
197 { |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7193
diff
changeset
|
198 charNDArray retval; |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7193
diff
changeset
|
199 |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7193
diff
changeset
|
200 if (! frc_str_conv) |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7193
diff
changeset
|
201 gripe_implicit_conversion ("Octave:num-to-str", |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7193
diff
changeset
|
202 "sparse complex matrix", "string"); |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7193
diff
changeset
|
203 else |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7193
diff
changeset
|
204 { |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7193
diff
changeset
|
205 retval = charNDArray (dims (), 0); |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7193
diff
changeset
|
206 octave_idx_type nc = matrix.cols (); |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7193
diff
changeset
|
207 octave_idx_type nr = matrix.rows (); |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7193
diff
changeset
|
208 |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7193
diff
changeset
|
209 for (octave_idx_type j = 0; j < nc; j++) |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7193
diff
changeset
|
210 for (octave_idx_type i = matrix.cidx(j); i < matrix.cidx(j+1); i++) |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7193
diff
changeset
|
211 retval(matrix.ridx(i) + nr * j) = |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7193
diff
changeset
|
212 static_cast<char>(std::real (matrix.data (i))); |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7193
diff
changeset
|
213 } |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7193
diff
changeset
|
214 |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7193
diff
changeset
|
215 return retval; |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7193
diff
changeset
|
216 } |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7193
diff
changeset
|
217 |
5164 | 218 SparseMatrix |
219 octave_sparse_complex_matrix::sparse_matrix_value (bool force_conversion) const | |
220 { | |
221 SparseMatrix retval; | |
222 | |
5781 | 223 if (! force_conversion) |
224 gripe_implicit_conversion ("Octave:imag-to-real", | |
225 "complex sparse matrix", | |
5164 | 226 "real sparse matrix"); |
227 | |
228 retval = ::real (matrix); | |
229 | |
230 return retval; | |
231 } | |
232 | |
9853
8d9e4752441a
implement complex built-in logical conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
9813
diff
changeset
|
233 SparseBoolMatrix |
8d9e4752441a
implement complex built-in logical conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
9813
diff
changeset
|
234 octave_sparse_complex_matrix::sparse_bool_matrix_value (bool warn) const |
8d9e4752441a
implement complex built-in logical conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
9813
diff
changeset
|
235 { |
8d9e4752441a
implement complex built-in logical conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
9813
diff
changeset
|
236 if (matrix.any_element_is_nan ()) |
8d9e4752441a
implement complex built-in logical conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
9813
diff
changeset
|
237 error ("invalid conversion from NaN to logical"); |
8d9e4752441a
implement complex built-in logical conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
9813
diff
changeset
|
238 else if (warn && (! matrix.all_elements_are_real () |
8d9e4752441a
implement complex built-in logical conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
9813
diff
changeset
|
239 || real (matrix).any_element_not_one_or_zero ())) |
8d9e4752441a
implement complex built-in logical conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
9813
diff
changeset
|
240 gripe_logical_conversion (); |
8d9e4752441a
implement complex built-in logical conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
9813
diff
changeset
|
241 |
8d9e4752441a
implement complex built-in logical conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
9813
diff
changeset
|
242 return mx_el_ne (matrix, Complex (0.0)); |
8d9e4752441a
implement complex built-in logical conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
9813
diff
changeset
|
243 } |
8d9e4752441a
implement complex built-in logical conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
9813
diff
changeset
|
244 |
5164 | 245 bool |
246 octave_sparse_complex_matrix::save_binary (std::ostream& os, | |
247 bool&save_as_floats) | |
248 { | |
249 dim_vector d = this->dims (); | |
250 if (d.length() < 1) | |
251 return false; | |
252 | |
253 // Ensure that additional memory is deallocated | |
254 matrix.maybe_compress (); | |
255 | |
256 int nr = d(0); | |
257 int nc = d(1); | |
5604 | 258 int nz = nzmax (); |
5164 | 259 |
5828 | 260 int32_t itmp; |
5164 | 261 // Use negative value for ndims to be consistent with other formats |
262 itmp= -2; | |
5760 | 263 os.write (reinterpret_cast<char *> (&itmp), 4); |
5164 | 264 |
265 itmp= nr; | |
5760 | 266 os.write (reinterpret_cast<char *> (&itmp), 4); |
5164 | 267 |
268 itmp= nc; | |
5760 | 269 os.write (reinterpret_cast<char *> (&itmp), 4); |
5164 | 270 |
271 itmp= nz; | |
5760 | 272 os.write (reinterpret_cast<char *> (&itmp), 4); |
5164 | 273 |
274 save_type st = LS_DOUBLE; | |
275 if (save_as_floats) | |
276 { | |
277 if (matrix.too_large_for_float ()) | |
278 { | |
279 warning ("save: some values too large to save as floats --"); | |
280 warning ("save: saving as doubles instead"); | |
281 } | |
282 else | |
283 st = LS_FLOAT; | |
284 } | |
5775 | 285 else if (matrix.nzmax () > 8192) // FIXME -- make this configurable. |
5164 | 286 { |
287 double max_val, min_val; | |
288 if (matrix.all_integers (max_val, min_val)) | |
289 st = get_save_type (max_val, min_val); | |
290 } | |
291 | |
292 // add one to the printed indices to go from | |
293 // zero-based to one-based arrays | |
294 for (int i = 0; i < nc+1; i++) | |
295 { | |
296 OCTAVE_QUIT; | |
297 itmp = matrix.cidx(i); | |
5760 | 298 os.write (reinterpret_cast<char *> (&itmp), 4); |
5164 | 299 } |
300 | |
301 for (int i = 0; i < nz; i++) | |
302 { | |
303 OCTAVE_QUIT; | |
304 itmp = matrix.ridx(i); | |
5760 | 305 os.write (reinterpret_cast<char *> (&itmp), 4); |
5164 | 306 } |
307 | |
5760 | 308 write_doubles (os, reinterpret_cast<const double *> (matrix.data()), st, 2 * nz); |
5164 | 309 |
310 return true; | |
311 } | |
312 | |
313 bool | |
314 octave_sparse_complex_matrix::load_binary (std::istream& is, bool swap, | |
315 oct_mach_info::float_format fmt) | |
316 { | |
5828 | 317 int32_t nz, nc, nr, tmp; |
5327 | 318 char ctmp; |
319 | |
5760 | 320 if (! is.read (reinterpret_cast<char *> (&tmp), 4)) |
5164 | 321 return false; |
322 | |
323 if (swap) | |
324 swap_bytes<4> (&tmp); | |
325 | |
326 if (tmp != -2) { | |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
327 error ("load: only 2D sparse matrices are supported"); |
5164 | 328 return false; |
329 } | |
330 | |
5760 | 331 if (! is.read (reinterpret_cast<char *> (&nr), 4)) |
5164 | 332 return false; |
5760 | 333 if (! is.read (reinterpret_cast<char *> (&nc), 4)) |
5164 | 334 return false; |
5760 | 335 if (! is.read (reinterpret_cast<char *> (&nz), 4)) |
5164 | 336 return false; |
337 | |
338 if (swap) | |
339 { | |
340 swap_bytes<4> (&nr); | |
341 swap_bytes<4> (&nc); | |
342 swap_bytes<4> (&nz); | |
343 } | |
344 | |
5275 | 345 SparseComplexMatrix m (static_cast<octave_idx_type> (nr), |
346 static_cast<octave_idx_type> (nc), | |
347 static_cast<octave_idx_type> (nz)); | |
5164 | 348 |
349 for (int i = 0; i < nc+1; i++) | |
350 { | |
351 OCTAVE_QUIT; | |
5760 | 352 if (! is.read (reinterpret_cast<char *> (&tmp), 4)) |
5164 | 353 return false; |
354 if (swap) | |
355 swap_bytes<4> (&tmp); | |
356 m.cidx(i) = tmp; | |
357 } | |
358 | |
359 for (int i = 0; i < nz; i++) | |
360 { | |
361 OCTAVE_QUIT; | |
5760 | 362 if (! is.read (reinterpret_cast<char *> (&tmp), 4)) |
5164 | 363 return false; |
364 if (swap) | |
365 swap_bytes<4> (&tmp); | |
366 m.ridx(i) = tmp; | |
367 } | |
368 | |
5760 | 369 if (! is.read (reinterpret_cast<char *> (&ctmp), 1)) |
5164 | 370 return false; |
371 | |
5760 | 372 read_doubles (is, reinterpret_cast<double *> (m.data ()), |
373 static_cast<save_type> (ctmp), 2 * nz, swap, fmt); | |
5164 | 374 |
375 if (error_state || ! is) | |
376 return false; | |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
377 |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
378 if (! m.indices_ok ()) |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
379 return false; |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
380 |
5164 | 381 matrix = m; |
382 | |
383 return true; | |
384 } | |
385 | |
386 #if defined (HAVE_HDF5) | |
5900 | 387 |
5164 | 388 bool |
389 octave_sparse_complex_matrix::save_hdf5 (hid_t loc_id, const char *name, | |
390 bool save_as_floats) | |
391 { | |
392 dim_vector dv = dims (); | |
393 int empty = save_hdf5_empty (loc_id, name, dv); | |
394 if (empty) | |
395 return (empty > 0); | |
396 | |
397 // Ensure that additional memory is deallocated | |
398 matrix.maybe_compress (); | |
399 | |
9892
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
400 #if HAVE_HDF5_18 |
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
401 hid_t group_hid = H5Gcreate (loc_id, name, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); |
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
402 #else |
5164 | 403 hid_t group_hid = H5Gcreate (loc_id, name, 0); |
9892
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
404 #endif |
5164 | 405 if (group_hid < 0) |
406 return false; | |
407 | |
408 hid_t space_hid = -1, data_hid = -1; | |
409 bool retval = true; | |
410 SparseComplexMatrix m = sparse_complex_matrix_value (); | |
5351 | 411 octave_idx_type tmp; |
5164 | 412 hsize_t hdims[2]; |
413 | |
5760 | 414 space_hid = H5Screate_simple (0, hdims, 0); |
5164 | 415 if (space_hid < 0) |
416 { | |
417 H5Gclose (group_hid); | |
418 return false; | |
419 } | |
420 | |
9892
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
421 #if HAVE_HDF5_18 |
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
422 data_hid = H5Dcreate (group_hid, "nr", H5T_NATIVE_IDX, space_hid, |
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
423 H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); |
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
424 #else |
5351 | 425 data_hid = H5Dcreate (group_hid, "nr", H5T_NATIVE_IDX, space_hid, |
5164 | 426 H5P_DEFAULT); |
9892
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
427 #endif |
5164 | 428 if (data_hid < 0) |
429 { | |
430 H5Sclose (space_hid); | |
431 H5Gclose (group_hid); | |
432 return false; | |
433 } | |
434 | |
435 tmp = m.rows (); | |
5351 | 436 retval = H5Dwrite (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL, |
5760 | 437 H5P_DEFAULT, &tmp) >= 0; |
5164 | 438 H5Dclose (data_hid); |
439 if (!retval) | |
440 { | |
441 H5Sclose (space_hid); | |
442 H5Gclose (group_hid); | |
443 return false; | |
444 } | |
445 | |
9892
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
446 #if HAVE_HDF5_18 |
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
447 data_hid = H5Dcreate (group_hid, "nc", H5T_NATIVE_IDX, space_hid, |
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
448 H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); |
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
449 #else |
5351 | 450 data_hid = H5Dcreate (group_hid, "nc", H5T_NATIVE_IDX, space_hid, |
5164 | 451 H5P_DEFAULT); |
9892
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
452 #endif |
5164 | 453 if (data_hid < 0) |
454 { | |
455 H5Sclose (space_hid); | |
456 H5Gclose (group_hid); | |
457 return false; | |
458 } | |
459 | |
460 tmp = m.cols (); | |
5351 | 461 retval = H5Dwrite (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL, |
5760 | 462 H5P_DEFAULT, &tmp) >= 0; |
5164 | 463 H5Dclose (data_hid); |
464 if (!retval) | |
465 { | |
466 H5Sclose (space_hid); | |
467 H5Gclose (group_hid); | |
468 return false; | |
469 } | |
470 | |
9892
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
471 #if HAVE_HDF5_18 |
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
472 data_hid = H5Dcreate (group_hid, "nz", H5T_NATIVE_IDX, space_hid, |
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
473 H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); |
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
474 #else |
5351 | 475 data_hid = H5Dcreate (group_hid, "nz", H5T_NATIVE_IDX, space_hid, |
5164 | 476 H5P_DEFAULT); |
9892
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
477 #endif |
5164 | 478 if (data_hid < 0) |
479 { | |
480 H5Sclose (space_hid); | |
481 H5Gclose (group_hid); | |
482 return false; | |
483 } | |
484 | |
5604 | 485 tmp = m.nzmax (); |
5351 | 486 retval = H5Dwrite (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL, |
5760 | 487 H5P_DEFAULT, &tmp) >= 0; |
5164 | 488 H5Dclose (data_hid); |
489 if (!retval) | |
490 { | |
491 H5Sclose (space_hid); | |
492 H5Gclose (group_hid); | |
493 return false; | |
494 } | |
495 | |
496 H5Sclose (space_hid); | |
497 | |
498 hdims[0] = m.cols() + 1; | |
499 hdims[1] = 1; | |
500 | |
501 space_hid = H5Screate_simple (2, hdims, 0); | |
502 | |
503 if (space_hid < 0) | |
504 { | |
505 H5Gclose (group_hid); | |
506 return false; | |
507 } | |
508 | |
9892
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
509 #if HAVE_HDF5_18 |
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
510 data_hid = H5Dcreate (group_hid, "cidx", H5T_NATIVE_IDX, space_hid, |
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
511 H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); |
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
512 #else |
5351 | 513 data_hid = H5Dcreate (group_hid, "cidx", H5T_NATIVE_IDX, space_hid, |
5164 | 514 H5P_DEFAULT); |
9892
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
515 #endif |
5164 | 516 if (data_hid < 0) |
517 { | |
518 H5Sclose (space_hid); | |
519 H5Gclose (group_hid); | |
520 return false; | |
521 } | |
522 | |
5351 | 523 octave_idx_type * itmp = m.xcidx (); |
524 retval = H5Dwrite (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL, | |
5760 | 525 H5P_DEFAULT, itmp) >= 0; |
5164 | 526 H5Dclose (data_hid); |
527 if (!retval) | |
528 { | |
529 H5Sclose (space_hid); | |
530 H5Gclose (group_hid); | |
531 return false; | |
532 } | |
533 | |
534 H5Sclose (space_hid); | |
535 | |
5604 | 536 hdims[0] = m.nzmax (); |
5164 | 537 hdims[1] = 1; |
538 | |
539 space_hid = H5Screate_simple (2, hdims, 0); | |
540 | |
541 if (space_hid < 0) | |
542 { | |
543 H5Gclose (group_hid); | |
544 return false; | |
545 } | |
546 | |
9892
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
547 #if HAVE_HDF5_18 |
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
548 data_hid = H5Dcreate (group_hid, "ridx", H5T_NATIVE_IDX, space_hid, |
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
549 H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); |
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
550 #else |
5351 | 551 data_hid = H5Dcreate (group_hid, "ridx", H5T_NATIVE_IDX, space_hid, |
5164 | 552 H5P_DEFAULT); |
9892
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
553 #endif |
5164 | 554 if (data_hid < 0) |
555 { | |
556 H5Sclose (space_hid); | |
557 H5Gclose (group_hid); | |
558 return false; | |
559 } | |
560 | |
561 itmp = m.xridx (); | |
5760 | 562 retval = H5Dwrite (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL, H5P_DEFAULT, itmp) >= 0; |
5164 | 563 H5Dclose (data_hid); |
564 if (!retval) | |
565 { | |
566 H5Sclose (space_hid); | |
567 H5Gclose (group_hid); | |
568 return false; | |
569 } | |
570 | |
571 hid_t save_type_hid = H5T_NATIVE_DOUBLE; | |
572 | |
573 if (save_as_floats) | |
574 { | |
575 if (m.too_large_for_float ()) | |
576 { | |
577 warning ("save: some values too large to save as floats --"); | |
578 warning ("save: saving as doubles instead"); | |
579 } | |
580 else | |
581 save_type_hid = H5T_NATIVE_FLOAT; | |
582 } | |
583 #if HAVE_HDF5_INT2FLOAT_CONVERSIONS | |
584 // hdf5 currently doesn't support float/integer conversions | |
585 else | |
586 { | |
587 double max_val, min_val; | |
588 | |
589 if (m.all_integers (max_val, min_val)) | |
590 save_type_hid | |
591 = save_type_to_hdf5 (get_save_type (max_val, min_val)); | |
592 } | |
593 #endif /* HAVE_HDF5_INT2FLOAT_CONVERSIONS */ | |
594 | |
595 hid_t type_hid = hdf5_make_complex_type (save_type_hid); | |
596 if (type_hid < 0) | |
597 { | |
598 H5Sclose (space_hid); | |
599 H5Gclose (group_hid); | |
600 return false; | |
601 } | |
9892
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
602 #if HAVE_HDF5_18 |
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
603 data_hid = H5Dcreate (group_hid, "data", type_hid, space_hid, |
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
604 H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); |
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
605 #else |
5760 | 606 data_hid = H5Dcreate (group_hid, "data", type_hid, space_hid, H5P_DEFAULT); |
9892
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
607 #endif |
5164 | 608 if (data_hid < 0) |
609 { | |
610 H5Sclose (space_hid); | |
611 H5Tclose (type_hid); | |
612 H5Gclose (group_hid); | |
613 return false; | |
614 } | |
615 | |
616 hid_t complex_type_hid = hdf5_make_complex_type (H5T_NATIVE_DOUBLE); | |
617 retval = false; | |
618 if (complex_type_hid >= 0) | |
619 { | |
620 Complex * ctmp = m.xdata (); | |
621 | |
622 retval = H5Dwrite (data_hid, complex_type_hid, H5S_ALL, H5S_ALL, | |
5760 | 623 H5P_DEFAULT, ctmp) >= 0; |
5164 | 624 } |
625 | |
626 H5Dclose (data_hid); | |
627 H5Sclose (space_hid); | |
628 H5Tclose (type_hid); | |
629 H5Gclose (group_hid); | |
630 | |
631 return retval; | |
632 } | |
633 | |
634 bool | |
9881
b3089dba88bf
Remove HDF5 cruft for older versions of HDF5
Kacper Kowalik
parents:
9853
diff
changeset
|
635 octave_sparse_complex_matrix::load_hdf5 (hid_t loc_id, const char *name) |
5164 | 636 { |
5351 | 637 octave_idx_type nr, nc, nz; |
5164 | 638 hid_t group_hid, data_hid, space_hid; |
639 hsize_t rank; | |
640 | |
641 dim_vector dv; | |
642 int empty = load_hdf5_empty (loc_id, name, dv); | |
643 if (empty > 0) | |
644 matrix.resize(dv); | |
645 if (empty) | |
646 return (empty > 0); | |
647 | |
9892
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
648 #if HAVE_HDF5_18 |
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
649 group_hid = H5Gopen (loc_id, name, H5P_DEFAULT); |
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
650 #else |
5164 | 651 group_hid = H5Gopen (loc_id, name); |
9892
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
652 #endif |
5164 | 653 if (group_hid < 0 ) return false; |
654 | |
9892
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
655 #if HAVE_HDF5_18 |
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
656 data_hid = H5Dopen (group_hid, "nr", H5P_DEFAULT); |
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
657 #else |
5164 | 658 data_hid = H5Dopen (group_hid, "nr"); |
9892
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
659 #endif |
5164 | 660 space_hid = H5Dget_space (data_hid); |
661 rank = H5Sget_simple_extent_ndims (space_hid); | |
662 | |
663 if (rank != 0) | |
664 { | |
665 H5Dclose (data_hid); | |
666 H5Gclose (group_hid); | |
667 return false; | |
668 } | |
669 | |
5760 | 670 if (H5Dread (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL, H5P_DEFAULT, &nr) < 0) |
5164 | 671 { |
672 H5Dclose (data_hid); | |
673 H5Gclose (group_hid); | |
674 return false; | |
675 } | |
676 | |
677 H5Dclose (data_hid); | |
678 | |
9892
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
679 #if HAVE_HDF5_18 |
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
680 data_hid = H5Dopen (group_hid, "nc", H5P_DEFAULT); |
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
681 #else |
5164 | 682 data_hid = H5Dopen (group_hid, "nc"); |
9892
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
683 #endif |
5164 | 684 space_hid = H5Dget_space (data_hid); |
685 rank = H5Sget_simple_extent_ndims (space_hid); | |
686 | |
687 if (rank != 0) | |
688 { | |
689 H5Dclose (data_hid); | |
690 H5Gclose (group_hid); | |
691 return false; | |
692 } | |
693 | |
5760 | 694 if (H5Dread (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL, H5P_DEFAULT, &nc) < 0) |
5164 | 695 { |
696 H5Dclose (data_hid); | |
697 H5Gclose (group_hid); | |
698 return false; | |
699 } | |
700 | |
701 H5Dclose (data_hid); | |
9892
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
702 |
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
703 #if HAVE_HDF5_18 |
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
704 data_hid = H5Dopen (group_hid, "nz", H5P_DEFAULT); |
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
705 #else |
5164 | 706 data_hid = H5Dopen (group_hid, "nz"); |
9892
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
707 #endif |
5164 | 708 space_hid = H5Dget_space (data_hid); |
709 rank = H5Sget_simple_extent_ndims (space_hid); | |
710 | |
711 if (rank != 0) | |
712 { | |
713 H5Dclose (data_hid); | |
714 H5Gclose (group_hid); | |
715 return false; | |
716 } | |
717 | |
5760 | 718 if (H5Dread (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL, H5P_DEFAULT, &nz) < 0) |
5164 | 719 { |
720 H5Dclose (data_hid); | |
721 H5Gclose (group_hid); | |
722 return false; | |
723 } | |
724 | |
725 H5Dclose (data_hid); | |
726 | |
5275 | 727 SparseComplexMatrix m (static_cast<octave_idx_type> (nr), |
728 static_cast<octave_idx_type> (nc), | |
729 static_cast<octave_idx_type> (nz)); | |
5164 | 730 |
9892
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
731 #if HAVE_HDF5_18 |
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
732 data_hid = H5Dopen (group_hid, "cidx", H5P_DEFAULT); |
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
733 #else |
5164 | 734 data_hid = H5Dopen (group_hid, "cidx"); |
9892
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
735 #endif |
5164 | 736 space_hid = H5Dget_space (data_hid); |
737 rank = H5Sget_simple_extent_ndims (space_hid); | |
738 | |
739 if (rank != 2) | |
740 { | |
741 H5Sclose (space_hid); | |
742 H5Dclose (data_hid); | |
743 H5Gclose (group_hid); | |
744 return false; | |
745 } | |
746 | |
747 OCTAVE_LOCAL_BUFFER (hsize_t, hdims, rank); | |
748 OCTAVE_LOCAL_BUFFER (hsize_t, maxdims, rank); | |
749 | |
750 H5Sget_simple_extent_dims (space_hid, hdims, maxdims); | |
751 | |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
752 if (static_cast<int> (hdims[0]) != nc + 1 |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
753 || static_cast<int> (hdims[1]) != 1) |
5164 | 754 { |
755 H5Sclose (space_hid); | |
756 H5Dclose (data_hid); | |
757 H5Gclose (group_hid); | |
758 return false; | |
759 } | |
760 | |
5351 | 761 octave_idx_type *itmp = m.xcidx (); |
5760 | 762 if (H5Dread (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL, H5P_DEFAULT, itmp) < 0) |
5164 | 763 { |
764 H5Sclose (space_hid); | |
765 H5Dclose (data_hid); | |
766 H5Gclose (group_hid); | |
767 return false; | |
768 } | |
769 | |
770 H5Sclose (space_hid); | |
771 H5Dclose (data_hid); | |
772 | |
9892
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
773 #if HAVE_HDF5_18 |
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
774 data_hid = H5Dopen (group_hid, "ridx", H5P_DEFAULT); |
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
775 #else |
5164 | 776 data_hid = H5Dopen (group_hid, "ridx"); |
9892
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
777 #endif |
5164 | 778 space_hid = H5Dget_space (data_hid); |
779 rank = H5Sget_simple_extent_ndims (space_hid); | |
780 | |
781 if (rank != 2) | |
782 { | |
783 H5Sclose (space_hid); | |
784 H5Dclose (data_hid); | |
785 H5Gclose (group_hid); | |
786 return false; | |
787 } | |
788 | |
789 H5Sget_simple_extent_dims (space_hid, hdims, maxdims); | |
790 | |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
791 if (static_cast<int> (hdims[0]) != nz |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
792 || static_cast<int> (hdims[1]) != 1) |
5164 | 793 { |
794 H5Sclose (space_hid); | |
795 H5Dclose (data_hid); | |
796 H5Gclose (group_hid); | |
797 return false; | |
798 } | |
799 | |
800 itmp = m.xridx (); | |
5760 | 801 if (H5Dread (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL, H5P_DEFAULT, itmp) < 0) |
5164 | 802 { |
803 H5Sclose (space_hid); | |
804 H5Dclose (data_hid); | |
805 H5Gclose (group_hid); | |
806 return false; | |
807 } | |
808 | |
809 H5Sclose (space_hid); | |
810 H5Dclose (data_hid); | |
811 | |
9892
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
812 #if HAVE_HDF5_18 |
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
813 data_hid = H5Dopen (group_hid, "data", H5P_DEFAULT); |
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
814 #else |
5164 | 815 data_hid = H5Dopen (group_hid, "data"); |
9892
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
816 #endif |
5164 | 817 hid_t type_hid = H5Dget_type (data_hid); |
818 | |
819 hid_t complex_type = hdf5_make_complex_type (H5T_NATIVE_DOUBLE); | |
820 | |
821 if (! hdf5_types_compatible (type_hid, complex_type)) | |
822 { | |
823 H5Tclose (complex_type); | |
824 H5Dclose (data_hid); | |
825 H5Gclose (group_hid); | |
826 return false; | |
827 } | |
828 | |
829 space_hid = H5Dget_space (data_hid); | |
830 rank = H5Sget_simple_extent_ndims (space_hid); | |
831 | |
832 if (rank != 2) | |
833 { | |
834 H5Sclose (space_hid); | |
835 H5Dclose (data_hid); | |
836 H5Gclose (group_hid); | |
837 return false; | |
838 } | |
839 | |
840 H5Sget_simple_extent_dims (space_hid, hdims, maxdims); | |
841 | |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
842 if (static_cast<int> (hdims[0]) != nz |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
843 || static_cast<int> (hdims[1]) != 1) |
5164 | 844 { |
845 H5Sclose (space_hid); | |
846 H5Dclose (data_hid); | |
847 H5Gclose (group_hid); | |
848 return false; | |
849 } | |
850 | |
851 Complex *ctmp = m.xdata (); | |
852 bool retval = false; | |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
853 if (H5Dread (data_hid, complex_type, H5S_ALL, H5S_ALL, |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
854 H5P_DEFAULT, ctmp) >= 0 |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
855 && m.indices_ok ()) |
5164 | 856 { |
857 retval = true; | |
858 matrix = m; | |
859 } | |
860 | |
861 H5Tclose (complex_type); | |
862 H5Sclose (space_hid); | |
863 H5Dclose (data_hid); | |
864 H5Gclose (group_hid); | |
865 | |
866 return retval; | |
867 } | |
868 | |
869 #endif | |
870 | |
5900 | 871 mxArray * |
872 octave_sparse_complex_matrix::as_mxArray (void) const | |
873 { | |
6686 | 874 mwSize nz = nzmax (); |
5903 | 875 mxArray *retval = new mxArray (mxDOUBLE_CLASS, rows (), columns (), |
876 nz, mxCOMPLEX); | |
877 double *pr = static_cast<double *> (retval->get_data ()); | |
878 double *pi = static_cast<double *> (retval->get_imag_data ()); | |
6686 | 879 mwIndex *ir = retval->get_ir (); |
880 mwIndex *jc = retval->get_jc (); | |
5903 | 881 |
6686 | 882 for (mwIndex i = 0; i < nz; i++) |
5903 | 883 { |
884 Complex val = matrix.data(i); | |
7503
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7193
diff
changeset
|
885 pr[i] = std::real (val); |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7193
diff
changeset
|
886 pi[i] = std::imag (val); |
5903 | 887 ir[i] = matrix.ridx(i); |
888 } | |
889 | |
6686 | 890 for (mwIndex i = 0; i < columns() + 1; i++) |
5903 | 891 jc[i] = matrix.cidx(i); |
892 | |
893 return retval; | |
5900 | 894 } |
895 | |
9812
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
896 octave_value |
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
897 octave_sparse_complex_matrix::map (unary_mapper_t umap) const |
7503
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7193
diff
changeset
|
898 { |
9812
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
899 switch (umap) |
7667
fb3a6c53c2b2
Allow negative zero imaginary part to be treated as zero for erf, erfc, gamma and lgamma mapper function
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
900 { |
9812
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
901 // Mappers handled specially. |
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
902 case umap_real: |
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
903 return ::real (matrix); |
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
904 case umap_imag: |
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
905 return ::imag (matrix); |
7667
fb3a6c53c2b2
Allow negative zero imaginary part to be treated as zero for erf, erfc, gamma and lgamma mapper function
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
906 |
9812
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
907 #define ARRAY_METHOD_MAPPER(UMAP, FCN) \ |
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
908 case umap_ ## UMAP: \ |
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
909 return octave_value (matrix.FCN ()) |
7667
fb3a6c53c2b2
Allow negative zero imaginary part to be treated as zero for erf, erfc, gamma and lgamma mapper function
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
910 |
9812
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
911 ARRAY_METHOD_MAPPER (abs, abs); |
7667
fb3a6c53c2b2
Allow negative zero imaginary part to be treated as zero for erf, erfc, gamma and lgamma mapper function
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
912 |
9812
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
913 #define ARRAY_MAPPER(UMAP, TYPE, FCN) \ |
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
914 case umap_ ## UMAP: \ |
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
915 return octave_value (matrix.map<TYPE> (FCN)) |
7503
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7193
diff
changeset
|
916 |
9812
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
917 ARRAY_MAPPER (acos, Complex, ::acos); |
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
918 ARRAY_MAPPER (acosh, Complex, ::acosh); |
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
919 ARRAY_MAPPER (angle, double, std::arg); |
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
920 ARRAY_MAPPER (arg, double, std::arg); |
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
921 ARRAY_MAPPER (asin, Complex, ::asin); |
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
922 ARRAY_MAPPER (asinh, Complex, ::asinh); |
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
923 ARRAY_MAPPER (atan, Complex, ::atan); |
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
924 ARRAY_MAPPER (atanh, Complex, ::atanh); |
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
925 ARRAY_MAPPER (ceil, Complex, ::ceil); |
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
926 ARRAY_MAPPER (conj, Complex, std::conj); |
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
927 ARRAY_MAPPER (cos, Complex, std::cos); |
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
928 ARRAY_MAPPER (cosh, Complex, std::cosh); |
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
929 ARRAY_MAPPER (exp, Complex, std::exp); |
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
930 ARRAY_MAPPER (expm1, Complex, ::expm1); |
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
931 ARRAY_MAPPER (fix, Complex, ::fix); |
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
932 ARRAY_MAPPER (floor, Complex, ::floor); |
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
933 ARRAY_MAPPER (log, Complex, std::log); |
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
934 ARRAY_MAPPER (log2, Complex, xlog2); |
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
935 ARRAY_MAPPER (log10, Complex, std::log10); |
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
936 ARRAY_MAPPER (log1p, Complex, ::log1p); |
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
937 ARRAY_MAPPER (round, Complex, xround); |
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
938 ARRAY_MAPPER (roundb, Complex, xroundb); |
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
939 ARRAY_MAPPER (signum, Complex, ::signum); |
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
940 ARRAY_MAPPER (sin, Complex, std::sin); |
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
941 ARRAY_MAPPER (sinh, Complex, std::sinh); |
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
942 ARRAY_MAPPER (sqrt, Complex, std::sqrt); |
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
943 ARRAY_MAPPER (tan, Complex, std::tan); |
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
944 ARRAY_MAPPER (tanh, Complex, std::tanh); |
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
945 ARRAY_MAPPER (isnan, bool, xisnan); |
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
946 ARRAY_MAPPER (isna, bool, octave_is_NA); |
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
947 ARRAY_MAPPER (isinf, bool, xisinf); |
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
948 ARRAY_MAPPER (finite, bool, xfinite); |
7667
fb3a6c53c2b2
Allow negative zero imaginary part to be treated as zero for erf, erfc, gamma and lgamma mapper function
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
949 |
9812
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
950 default: // Attempt to go via dense matrix. |
9813
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
951 return octave_base_sparse<SparseComplexMatrix>::map (umap); |
9812
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
952 } |
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
953 } |
7503
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7193
diff
changeset
|
954 |
5164 | 955 /* |
956 ;;; Local Variables: *** | |
957 ;;; mode: C++ *** | |
958 ;;; End: *** | |
959 */ |