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