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