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