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