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