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