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