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