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