2376
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
2376
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
2376
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
3503
|
28 #include <iostream> |
4726
|
29 #include <vector> |
2901
|
30 |
4944
|
31 #include "data-conv.h" |
2376
|
32 #include "lo-ieee.h" |
|
33 #include "mx-base.h" |
4944
|
34 #include "mach-info.h" |
2376
|
35 |
|
36 #include "gripes.h" |
|
37 #include "oct-obj.h" |
4944
|
38 #include "oct-stream.h" |
2410
|
39 #include "ops.h" |
3219
|
40 #include "ov-base.h" |
|
41 #include "ov-base-mat.h" |
|
42 #include "ov-base-mat.cc" |
2410
|
43 #include "ov-complex.h" |
2376
|
44 #include "ov-cx-mat.h" |
2410
|
45 #include "ov-re-mat.h" |
|
46 #include "ov-scalar.h" |
2376
|
47 #include "pr-output.h" |
|
48 |
4687
|
49 #include "byte-swap.h" |
|
50 #include "ls-oct-ascii.h" |
|
51 #include "ls-hdf5.h" |
|
52 #include "ls-utils.h" |
|
53 |
4513
|
54 template class octave_base_matrix<ComplexNDArray>; |
2376
|
55 |
3219
|
56 DEFINE_OCTAVE_ALLOCATOR (octave_complex_matrix); |
2477
|
57 |
4612
|
58 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_complex_matrix, |
|
59 "complex matrix", "double"); |
2376
|
60 |
5759
|
61 octave_base_value * |
2410
|
62 octave_complex_matrix::try_narrowing_conversion (void) |
|
63 { |
5759
|
64 octave_base_value *retval = 0; |
2410
|
65 |
4513
|
66 if (matrix.ndims () == 2) |
|
67 { |
|
68 ComplexMatrix cm = matrix.matrix_value (); |
2410
|
69 |
5275
|
70 octave_idx_type nr = cm.rows (); |
|
71 octave_idx_type nc = cm.cols (); |
4513
|
72 |
|
73 if (nr == 1 && nc == 1) |
|
74 { |
|
75 Complex c = matrix (0, 0); |
2410
|
76 |
5450
|
77 double im = std::imag (c); |
|
78 |
|
79 if (im == 0.0 && ! lo_ieee_signbit (im)) |
4513
|
80 retval = new octave_scalar (std::real (c)); |
|
81 else |
|
82 retval = new octave_complex (c); |
|
83 } |
|
84 else if (nr == 0 || nc == 0) |
|
85 retval = new octave_matrix (Matrix (nr, nc)); |
|
86 else if (cm.all_elements_are_real ()) |
|
87 retval = new octave_matrix (::real (cm)); |
2410
|
88 } |
4923
|
89 else if (matrix.all_elements_are_real ()) |
|
90 retval = new octave_matrix (::real (matrix)); |
2410
|
91 |
|
92 return retval; |
|
93 } |
2376
|
94 |
|
95 void |
|
96 octave_complex_matrix::assign (const octave_value_list& idx, |
4686
|
97 const ComplexNDArray& rhs) |
4418
|
98 { |
4513
|
99 octave_base_matrix<ComplexNDArray>::assign (idx, rhs); |
4418
|
100 } |
|
101 |
|
102 void |
|
103 octave_complex_matrix::assign (const octave_value_list& idx, |
4686
|
104 const NDArray& rhs) |
2376
|
105 { |
5275
|
106 octave_idx_type len = idx.length (); |
2376
|
107 |
5275
|
108 for (octave_idx_type i = 0; i < len; i++) |
4513
|
109 matrix.set_index (idx(i).index_vector ()); |
2376
|
110 |
4513
|
111 ::assign (matrix, rhs); |
2376
|
112 } |
|
113 |
|
114 bool |
|
115 octave_complex_matrix::valid_as_scalar_index (void) const |
|
116 { |
5775
|
117 // FIXME |
2376
|
118 return false; |
|
119 } |
|
120 |
|
121 double |
|
122 octave_complex_matrix::double_value (bool force_conversion) const |
|
123 { |
4102
|
124 double retval = lo_ieee_nan_value (); |
2376
|
125 |
5781
|
126 if (! force_conversion) |
|
127 gripe_implicit_conversion ("Octave:imag-to-real", |
|
128 "complex matrix", "real scalar"); |
2376
|
129 |
4455
|
130 if (rows () > 0 && columns () > 0) |
|
131 { |
5781
|
132 gripe_implicit_conversion ("Octave:array-as-scalar", |
|
133 "complex matrix", "real scalar"); |
4455
|
134 |
|
135 retval = std::real (matrix (0, 0)); |
|
136 } |
2376
|
137 else |
|
138 gripe_invalid_conversion ("complex matrix", "real scalar"); |
|
139 |
|
140 return retval; |
|
141 } |
|
142 |
|
143 Matrix |
|
144 octave_complex_matrix::matrix_value (bool force_conversion) const |
|
145 { |
|
146 Matrix retval; |
|
147 |
5781
|
148 if (! force_conversion) |
|
149 gripe_implicit_conversion ("Octave:imag-to-real", |
|
150 "complex matrix", "real matrix"); |
2376
|
151 |
4513
|
152 retval = ::real (matrix.matrix_value ()); |
2376
|
153 |
|
154 return retval; |
|
155 } |
|
156 |
|
157 Complex |
|
158 octave_complex_matrix::complex_value (bool) const |
|
159 { |
4102
|
160 double tmp = lo_ieee_nan_value (); |
|
161 |
|
162 Complex retval (tmp, tmp); |
2376
|
163 |
4455
|
164 if (rows () > 0 && columns () > 0) |
|
165 { |
5781
|
166 gripe_implicit_conversion ("Octave:array-as-scalar", |
|
167 "complex matrix", "complex scalar"); |
4455
|
168 |
|
169 retval = matrix (0, 0); |
|
170 } |
2376
|
171 else |
|
172 gripe_invalid_conversion ("complex matrix", "complex scalar"); |
|
173 |
|
174 return retval; |
|
175 } |
|
176 |
|
177 ComplexMatrix |
|
178 octave_complex_matrix::complex_matrix_value (bool) const |
|
179 { |
4513
|
180 return matrix.matrix_value (); |
2376
|
181 } |
|
182 |
5164
|
183 SparseMatrix |
|
184 octave_complex_matrix::sparse_matrix_value (bool force_conversion) const |
|
185 { |
|
186 SparseMatrix retval; |
|
187 |
5781
|
188 if (! force_conversion) |
|
189 gripe_implicit_conversion ("Octave:imag-to-real", |
|
190 "complex matrix", "real matrix"); |
5164
|
191 |
|
192 retval = SparseMatrix (::real (matrix.matrix_value ())); |
|
193 |
|
194 return retval; |
|
195 } |
|
196 |
|
197 SparseComplexMatrix |
|
198 octave_complex_matrix::sparse_complex_matrix_value (bool) const |
|
199 { |
|
200 return SparseComplexMatrix (matrix.matrix_value ()); |
|
201 } |
|
202 |
4687
|
203 bool |
|
204 octave_complex_matrix::save_ascii (std::ostream& os, bool& infnan_warned, |
5956
|
205 int strip_nan_and_inf) |
4687
|
206 { |
|
207 dim_vector d = dims (); |
|
208 if (d.length () > 2) |
|
209 { |
|
210 ComplexNDArray tmp = complex_array_value (); |
|
211 |
|
212 if (strip_nan_and_inf) |
|
213 { |
|
214 warning ("save: Can not strip Inf or NaN values"); |
|
215 warning ("save: Inf or NaN values may not be reloadable"); |
|
216 infnan_warned = true; |
|
217 } |
|
218 else if (! infnan_warned && tmp.any_element_is_inf_or_nan ()) |
|
219 { |
|
220 warning ("save: Inf or NaN values may not be reloadable"); |
|
221 infnan_warned = true; |
|
222 } |
|
223 |
|
224 os << "# ndims: " << d.length () << "\n"; |
|
225 |
5275
|
226 for (int i = 0; i < d.length (); i++) |
4687
|
227 os << " " << d (i); |
|
228 |
|
229 os << "\n" << tmp; |
|
230 } |
|
231 else |
|
232 { |
|
233 // Keep this case, rather than use generic code above for backward |
|
234 // compatiability. Makes load_ascii much more complex!! |
|
235 os << "# rows: " << rows () << "\n" |
|
236 << "# columns: " << columns () << "\n"; |
|
237 |
|
238 ComplexMatrix tmp = complex_matrix_value (); |
|
239 |
5956
|
240 tmp.save_ascii (os, infnan_warned, strip_nan_and_inf); |
4687
|
241 } |
|
242 |
|
243 return true; |
|
244 } |
|
245 |
|
246 bool |
|
247 octave_complex_matrix::load_ascii (std::istream& is) |
|
248 { |
|
249 bool success = true; |
5099
|
250 |
|
251 string_vector keywords(2); |
|
252 |
|
253 keywords[0] = "ndims"; |
|
254 keywords[1] = "rows"; |
4687
|
255 |
5099
|
256 std::string kw; |
5275
|
257 octave_idx_type val = 0; |
5099
|
258 |
|
259 if (extract_keyword (is, keywords, kw, val, true)) |
4687
|
260 { |
5099
|
261 if (kw == "ndims") |
4687
|
262 { |
5275
|
263 int mdims = static_cast<int> (val); |
4687
|
264 |
5099
|
265 if (mdims >= 0) |
4687
|
266 { |
5099
|
267 dim_vector dv; |
|
268 dv.resize (mdims); |
4687
|
269 |
5099
|
270 for (int i = 0; i < mdims; i++) |
|
271 is >> dv(i); |
4687
|
272 |
5099
|
273 ComplexNDArray tmp(dv); |
4687
|
274 is >> tmp; |
5099
|
275 |
4687
|
276 if (!is) |
|
277 { |
|
278 error ("load: failed to load matrix constant"); |
|
279 success = false; |
|
280 } |
|
281 matrix = tmp; |
|
282 } |
|
283 else |
5099
|
284 { |
|
285 error ("load: failed to extract number of rows and columns"); |
|
286 success = false; |
|
287 } |
4687
|
288 } |
5099
|
289 else if (kw == "rows") |
4687
|
290 { |
5275
|
291 octave_idx_type nr = val; |
|
292 octave_idx_type nc = 0; |
5099
|
293 |
|
294 if (nr >= 0 && extract_keyword (is, "columns", nc) && nc >= 0) |
|
295 { |
|
296 if (nr > 0 && nc > 0) |
|
297 { |
|
298 ComplexMatrix tmp (nr, nc); |
|
299 is >> tmp; |
|
300 if (!is) |
|
301 { |
|
302 error ("load: failed to load matrix constant"); |
|
303 success = false; |
|
304 } |
|
305 matrix = tmp; |
|
306 } |
|
307 else if (nr == 0 || nc == 0) |
|
308 matrix = ComplexMatrix (nr, nc); |
|
309 else |
|
310 panic_impossible (); |
|
311 } |
|
312 else |
|
313 { |
|
314 error ("load: failed to extract number of rows and columns"); |
|
315 success = false; |
|
316 } |
4687
|
317 } |
5099
|
318 else |
|
319 panic_impossible (); |
|
320 } |
|
321 else |
|
322 { |
|
323 error ("load: failed to extract number of rows and columns"); |
|
324 success = false; |
4687
|
325 } |
|
326 |
|
327 return success; |
|
328 } |
|
329 |
|
330 bool |
|
331 octave_complex_matrix::save_binary (std::ostream& os, bool& save_as_floats) |
|
332 { |
|
333 dim_vector d = dims (); |
|
334 if (d.length() < 1) |
|
335 return false; |
|
336 |
|
337 // Use negative value for ndims to differentiate with old format!! |
5828
|
338 int32_t tmp = - d.length(); |
5760
|
339 os.write (reinterpret_cast<char *> (&tmp), 4); |
5275
|
340 for (int i = 0; i < d.length (); i++) |
4687
|
341 { |
|
342 tmp = d(i); |
5760
|
343 os.write (reinterpret_cast<char *> (&tmp), 4); |
4687
|
344 } |
|
345 |
|
346 ComplexNDArray m = complex_array_value (); |
|
347 save_type st = LS_DOUBLE; |
|
348 if (save_as_floats) |
|
349 { |
|
350 if (m.too_large_for_float ()) |
|
351 { |
|
352 warning ("save: some values too large to save as floats --"); |
|
353 warning ("save: saving as doubles instead"); |
|
354 } |
|
355 else |
|
356 st = LS_FLOAT; |
|
357 } |
5775
|
358 else if (d.numel () > 4096) // FIXME -- make this configurable. |
4687
|
359 { |
|
360 double max_val, min_val; |
|
361 if (m.all_integers (max_val, min_val)) |
|
362 st = get_save_type (max_val, min_val); |
|
363 } |
|
364 |
|
365 |
|
366 const Complex *mtmp = m.data (); |
5760
|
367 write_doubles (os, reinterpret_cast<const double *> (mtmp), st, 2 * d.numel ()); |
4687
|
368 |
|
369 return true; |
|
370 } |
|
371 |
|
372 bool |
|
373 octave_complex_matrix::load_binary (std::istream& is, bool swap, |
|
374 oct_mach_info::float_format fmt) |
|
375 { |
|
376 char tmp; |
5828
|
377 int32_t mdims; |
5760
|
378 if (! is.read (reinterpret_cast<char *> (&mdims), 4)) |
4687
|
379 return false; |
|
380 if (swap) |
4944
|
381 swap_bytes<4> (&mdims); |
4687
|
382 if (mdims < 0) |
|
383 { |
|
384 mdims = - mdims; |
5828
|
385 int32_t di; |
4687
|
386 dim_vector dv; |
|
387 dv.resize (mdims); |
|
388 |
|
389 for (int i = 0; i < mdims; i++) |
|
390 { |
5760
|
391 if (! is.read (reinterpret_cast<char *> (&di), 4)) |
4687
|
392 return false; |
|
393 if (swap) |
4944
|
394 swap_bytes<4> (&di); |
4687
|
395 dv(i) = di; |
|
396 } |
|
397 |
5157
|
398 // Convert an array with a single dimension to be a row vector. |
|
399 // Octave should never write files like this, other software |
|
400 // might. |
|
401 |
|
402 if (mdims == 1) |
|
403 { |
|
404 mdims = 2; |
|
405 dv.resize (mdims); |
|
406 dv(1) = dv(0); |
|
407 dv(0) = 1; |
|
408 } |
|
409 |
5760
|
410 if (! is.read (reinterpret_cast<char *> (&tmp), 1)) |
4687
|
411 return false; |
|
412 |
|
413 ComplexNDArray m(dv); |
|
414 Complex *im = m.fortran_vec (); |
5760
|
415 read_doubles (is, reinterpret_cast<double *> (im), |
|
416 static_cast<save_type> (tmp), 2 * dv.numel (), swap, fmt); |
4687
|
417 if (error_state || ! is) |
|
418 return false; |
|
419 matrix = m; |
|
420 } |
|
421 else |
|
422 { |
5828
|
423 int32_t nr, nc; |
4687
|
424 nr = mdims; |
5760
|
425 if (! is.read (reinterpret_cast<char *> (&nc), 4)) |
4687
|
426 return false; |
|
427 if (swap) |
4944
|
428 swap_bytes<4> (&nc); |
5760
|
429 if (! is.read (reinterpret_cast<char *> (&tmp), 1)) |
4687
|
430 return false; |
|
431 ComplexMatrix m (nr, nc); |
|
432 Complex *im = m.fortran_vec (); |
5275
|
433 octave_idx_type len = nr * nc; |
5760
|
434 read_doubles (is, reinterpret_cast<double *> (im), |
|
435 static_cast<save_type> (tmp), 2*len, swap, fmt); |
4687
|
436 if (error_state || ! is) |
|
437 return false; |
|
438 matrix = m; |
|
439 } |
|
440 return true; |
|
441 } |
|
442 |
|
443 #if defined (HAVE_HDF5) |
4944
|
444 |
4687
|
445 bool |
|
446 octave_complex_matrix::save_hdf5 (hid_t loc_id, const char *name, |
|
447 bool save_as_floats) |
|
448 { |
4837
|
449 dim_vector dv = dims (); |
|
450 int empty = save_hdf5_empty (loc_id, name, dv); |
|
451 if (empty) |
4805
|
452 return (empty > 0); |
|
453 |
4837
|
454 int rank = dv.length (); |
4805
|
455 hid_t space_hid = -1, data_hid = -1, type_hid = -1; |
4687
|
456 bool retval = true; |
|
457 ComplexNDArray m = complex_array_value (); |
|
458 |
4805
|
459 OCTAVE_LOCAL_BUFFER (hsize_t, hdims, rank); |
|
460 |
4687
|
461 // Octave uses column-major, while HDF5 uses row-major ordering |
4805
|
462 for (int i = 0; i < rank; i++) |
4837
|
463 hdims[i] = dv (rank-i-1); |
4805
|
464 |
4815
|
465 space_hid = H5Screate_simple (rank, hdims, 0); |
4687
|
466 if (space_hid < 0) return false; |
|
467 |
|
468 hid_t save_type_hid = H5T_NATIVE_DOUBLE; |
|
469 |
|
470 if (save_as_floats) |
|
471 { |
|
472 if (m.too_large_for_float ()) |
|
473 { |
|
474 warning ("save: some values too large to save as floats --"); |
|
475 warning ("save: saving as doubles instead"); |
|
476 } |
|
477 else |
|
478 save_type_hid = H5T_NATIVE_FLOAT; |
|
479 } |
|
480 #if HAVE_HDF5_INT2FLOAT_CONVERSIONS |
|
481 // hdf5 currently doesn't support float/integer conversions |
|
482 else |
|
483 { |
|
484 double max_val, min_val; |
|
485 |
|
486 if (m.all_integers (max_val, min_val)) |
|
487 save_type_hid |
|
488 = save_type_to_hdf5 (get_save_type (max_val, min_val)); |
|
489 } |
|
490 #endif /* HAVE_HDF5_INT2FLOAT_CONVERSIONS */ |
|
491 |
|
492 type_hid = hdf5_make_complex_type (save_type_hid); |
|
493 if (type_hid < 0) |
|
494 { |
|
495 H5Sclose (space_hid); |
|
496 return false; |
|
497 } |
|
498 |
|
499 data_hid = H5Dcreate (loc_id, name, type_hid, space_hid, H5P_DEFAULT); |
|
500 if (data_hid < 0) |
|
501 { |
|
502 H5Sclose (space_hid); |
|
503 H5Tclose (type_hid); |
|
504 return false; |
|
505 } |
|
506 |
|
507 hid_t complex_type_hid = hdf5_make_complex_type (H5T_NATIVE_DOUBLE); |
|
508 if (complex_type_hid < 0) retval = false; |
|
509 |
|
510 if (retval) |
|
511 { |
|
512 Complex *mtmp = m.fortran_vec (); |
|
513 if (H5Dwrite (data_hid, complex_type_hid, H5S_ALL, H5S_ALL, H5P_DEFAULT, |
4815
|
514 mtmp) < 0) |
4687
|
515 { |
|
516 H5Tclose (complex_type_hid); |
|
517 retval = false; |
|
518 } |
|
519 } |
|
520 |
|
521 H5Tclose (complex_type_hid); |
|
522 H5Dclose (data_hid); |
|
523 H5Tclose (type_hid); |
|
524 H5Sclose (space_hid); |
4837
|
525 |
4687
|
526 return retval; |
|
527 } |
|
528 |
|
529 bool |
|
530 octave_complex_matrix::load_hdf5 (hid_t loc_id, const char *name, |
|
531 bool /* have_h5giterate_bug */) |
|
532 { |
4837
|
533 bool retval = false; |
|
534 |
4805
|
535 dim_vector dv; |
|
536 int empty = load_hdf5_empty (loc_id, name, dv); |
|
537 if (empty > 0) |
|
538 matrix.resize(dv); |
4837
|
539 if (empty) |
4805
|
540 return (empty > 0); |
|
541 |
4687
|
542 hid_t data_hid = H5Dopen (loc_id, name); |
|
543 hid_t type_hid = H5Dget_type (data_hid); |
|
544 |
|
545 hid_t complex_type = hdf5_make_complex_type (H5T_NATIVE_DOUBLE); |
|
546 |
|
547 if (! hdf5_types_compatible (type_hid, complex_type)) |
|
548 { |
4837
|
549 H5Tclose (complex_type); |
4687
|
550 H5Dclose (data_hid); |
|
551 return false; |
|
552 } |
|
553 |
|
554 hid_t space_id = H5Dget_space (data_hid); |
|
555 |
|
556 hsize_t rank = H5Sget_simple_extent_ndims (space_id); |
|
557 |
|
558 if (rank < 1) |
|
559 { |
4837
|
560 H5Tclose (complex_type); |
4687
|
561 H5Sclose (space_id); |
|
562 H5Dclose (data_hid); |
|
563 return false; |
|
564 } |
|
565 |
|
566 OCTAVE_LOCAL_BUFFER (hsize_t, hdims, rank); |
|
567 OCTAVE_LOCAL_BUFFER (hsize_t, maxdims, rank); |
|
568 |
|
569 H5Sget_simple_extent_dims (space_id, hdims, maxdims); |
|
570 |
|
571 // Octave uses column-major, while HDF5 uses row-major ordering |
|
572 if (rank == 1) |
|
573 { |
|
574 dv.resize (2); |
|
575 dv(0) = 1; |
|
576 dv(1) = hdims[0]; |
|
577 } |
|
578 else |
|
579 { |
|
580 dv.resize (rank); |
4815
|
581 for (hsize_t i = 0, j = rank - 1; i < rank; i++, j--) |
4687
|
582 dv(j) = hdims[i]; |
|
583 } |
|
584 |
|
585 ComplexNDArray m (dv); |
|
586 Complex *reim = m.fortran_vec (); |
4815
|
587 if (H5Dread (data_hid, complex_type, H5S_ALL, H5S_ALL, H5P_DEFAULT, |
|
588 reim) >= 0) |
4687
|
589 { |
|
590 retval = true; |
|
591 matrix = m; |
|
592 } |
|
593 |
4837
|
594 H5Tclose (complex_type); |
4687
|
595 H5Sclose (space_id); |
|
596 H5Dclose (data_hid); |
4837
|
597 |
4687
|
598 return retval; |
|
599 } |
4944
|
600 |
4687
|
601 #endif |
|
602 |
4643
|
603 void |
|
604 octave_complex_matrix::print_raw (std::ostream& os, |
|
605 bool pr_as_read_syntax) const |
|
606 { |
|
607 octave_print_internal (os, matrix, pr_as_read_syntax, |
|
608 current_print_indent_level ()); |
|
609 } |
|
610 |
5900
|
611 mxArray * |
|
612 octave_complex_matrix::as_mxArray (void) const |
|
613 { |
|
614 mxArray *retval = new mxArray (mxDOUBLE_CLASS, dims (), mxCOMPLEX); |
|
615 |
|
616 double *pr = static_cast<double *> (retval->get_data ()); |
|
617 double *pi = static_cast<double *> (retval->get_imag_data ()); |
|
618 |
|
619 int nel = numel (); |
|
620 |
|
621 const Complex *p = matrix.data (); |
|
622 |
|
623 for (int i = 0; i < nel; i++) |
|
624 { |
|
625 pr[i] = real (p[i]); |
|
626 pi[i] = imag (p[i]); |
|
627 } |
|
628 |
|
629 return retval; |
|
630 } |
|
631 |
2376
|
632 /* |
|
633 ;;; Local Variables: *** |
|
634 ;;; mode: C++ *** |
|
635 ;;; End: *** |
|
636 */ |