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