2871
|
1 /* |
|
2 |
|
3 Copyright (C) 1996, 1997 John W. Eaton |
|
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 |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
2871
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
3503
|
28 #include <iostream> |
4726
|
29 #include <vector> |
2901
|
30 |
2871
|
31 #include "lo-ieee.h" |
|
32 #include "mx-base.h" |
|
33 |
|
34 #include "gripes.h" |
|
35 #include "oct-obj.h" |
|
36 #include "ops.h" |
3219
|
37 #include "ov-base.h" |
|
38 #include "ov-base-mat.h" |
|
39 #include "ov-base-mat.cc" |
2871
|
40 #include "ov-bool.h" |
|
41 #include "ov-bool-mat.h" |
|
42 #include "ov-re-mat.h" |
|
43 #include "pr-output.h" |
|
44 |
4687
|
45 #include "byte-swap.h" |
|
46 #include "ls-oct-ascii.h" |
|
47 #include "ls-hdf5.h" |
|
48 #include "ls-utils.h" |
|
49 |
4513
|
50 template class octave_base_matrix<boolNDArray>; |
2871
|
51 |
3219
|
52 DEFINE_OCTAVE_ALLOCATOR (octave_bool_matrix); |
2871
|
53 |
4612
|
54 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_bool_matrix, |
|
55 "bool matrix", "logical"); |
2871
|
56 |
5759
|
57 static octave_base_value * |
|
58 default_numeric_conversion_function (const octave_base_value& a) |
2871
|
59 { |
|
60 CAST_CONV_ARG (const octave_bool_matrix&); |
|
61 |
4668
|
62 return new octave_matrix (NDArray (v.bool_array_value ())); |
2871
|
63 } |
|
64 |
5759
|
65 octave_base_value::type_conv_fcn |
2871
|
66 octave_bool_matrix::numeric_conversion_function (void) const |
|
67 { |
|
68 return default_numeric_conversion_function; |
|
69 } |
|
70 |
5759
|
71 octave_base_value * |
2871
|
72 octave_bool_matrix::try_narrowing_conversion (void) |
|
73 { |
5759
|
74 octave_base_value *retval = 0; |
2871
|
75 |
4513
|
76 if (matrix.ndims () == 2) |
|
77 { |
|
78 boolMatrix bm = matrix.matrix_value (); |
2871
|
79 |
5275
|
80 octave_idx_type nr = bm.rows (); |
|
81 octave_idx_type nc = bm.cols (); |
4513
|
82 |
|
83 if (nr == 1 && nc == 1) |
|
84 retval = new octave_bool (bm (0, 0)); |
|
85 } |
2871
|
86 |
|
87 return retval; |
|
88 } |
|
89 |
|
90 bool |
|
91 octave_bool_matrix::valid_as_scalar_index (void) const |
|
92 { |
5775
|
93 // FIXME |
2871
|
94 return false; |
|
95 } |
|
96 |
|
97 double |
|
98 octave_bool_matrix::double_value (bool) const |
|
99 { |
4102
|
100 double retval = lo_ieee_nan_value (); |
2871
|
101 |
4455
|
102 if (rows () > 0 && columns () > 0) |
|
103 { |
5781
|
104 gripe_implicit_conversion ("Octave:array-as-scalar", |
|
105 "bool matrix", "real scalar"); |
4455
|
106 |
|
107 retval = matrix (0, 0); |
|
108 } |
2871
|
109 else |
|
110 gripe_invalid_conversion ("bool matrix", "real scalar"); |
|
111 |
|
112 return retval; |
|
113 } |
|
114 |
|
115 Complex |
|
116 octave_bool_matrix::complex_value (bool) const |
|
117 { |
4102
|
118 double tmp = lo_ieee_nan_value (); |
|
119 |
|
120 Complex retval (tmp, tmp); |
2871
|
121 |
4455
|
122 if (rows () > 0 && columns () > 0) |
|
123 { |
5781
|
124 gripe_implicit_conversion ("Octave:array-as-scalar", |
|
125 "bool matrix", "complex scalar"); |
4455
|
126 |
|
127 retval = matrix (0, 0); |
|
128 } |
2871
|
129 else |
|
130 gripe_invalid_conversion ("bool matrix", "complex scalar"); |
|
131 |
|
132 return retval; |
|
133 } |
|
134 |
4457
|
135 octave_value |
5279
|
136 octave_bool_matrix::convert_to_str_internal (bool pad, bool force, |
|
137 char type) const |
4457
|
138 { |
4844
|
139 octave_value tmp = octave_value (array_value ()); |
5279
|
140 return tmp.convert_to_str (pad, force, type); |
4457
|
141 } |
|
142 |
4643
|
143 void |
|
144 octave_bool_matrix::print_raw (std::ostream& os, |
|
145 bool pr_as_read_syntax) const |
|
146 { |
|
147 octave_print_internal (os, matrix, pr_as_read_syntax, |
|
148 current_print_indent_level ()); |
|
149 } |
|
150 |
4687
|
151 bool |
|
152 octave_bool_matrix::save_ascii (std::ostream& os, bool& /* infnan_warned */, |
|
153 bool /* strip_nan_and_inf */) |
|
154 { |
|
155 dim_vector d = dims (); |
|
156 if (d.length () > 2) |
|
157 { |
|
158 NDArray tmp = array_value (); |
|
159 os << "# ndims: " << d.length () << "\n"; |
|
160 |
5275
|
161 for (int i = 0; i < d.length (); i++) |
4687
|
162 os << " " << d (i); |
|
163 |
|
164 os << "\n" << tmp; |
|
165 } |
|
166 else |
|
167 { |
|
168 // Keep this case, rather than use generic code above for backward |
|
169 // compatiability. Makes load_ascii much more complex!! |
|
170 os << "# rows: " << rows () << "\n" |
|
171 << "# columns: " << columns () << "\n"; |
|
172 |
|
173 Matrix tmp = matrix_value (); |
|
174 |
|
175 os << tmp; |
|
176 } |
|
177 |
|
178 return true; |
|
179 } |
|
180 |
|
181 bool |
|
182 octave_bool_matrix::load_ascii (std::istream& is) |
|
183 { |
|
184 bool success = true; |
|
185 |
5099
|
186 string_vector keywords (2); |
4687
|
187 |
5099
|
188 keywords[0] = "ndims"; |
|
189 keywords[1] = "rows"; |
4687
|
190 |
5099
|
191 std::string kw; |
5275
|
192 octave_idx_type val = 0; |
4687
|
193 |
5099
|
194 if (extract_keyword (is, keywords, kw, val, true)) |
4687
|
195 { |
5099
|
196 if (kw == "ndims") |
|
197 { |
5275
|
198 int mdims = static_cast<int> (val); |
4687
|
199 |
5099
|
200 if (mdims >= 0) |
4687
|
201 { |
5099
|
202 dim_vector dv; |
|
203 dv.resize (mdims); |
|
204 |
|
205 for (int i = 0; i < mdims; i++) |
|
206 is >> dv(i); |
|
207 |
|
208 NDArray tmp(dv); |
4687
|
209 is >> tmp; |
5099
|
210 |
|
211 if (!is) |
4687
|
212 { |
|
213 error ("load: failed to load matrix constant"); |
|
214 success = false; |
|
215 } |
|
216 |
5099
|
217 boolNDArray btmp (dv); |
5275
|
218 for (octave_idx_type i = 0; i < btmp.nelem (); i++) |
5099
|
219 btmp.elem (i) = (tmp.elem (i) != 0.); |
|
220 |
4687
|
221 matrix = btmp; |
|
222 } |
|
223 else |
5099
|
224 { |
|
225 error ("load: failed to extract number of rows and columns"); |
|
226 success = false; |
|
227 } |
4687
|
228 } |
5099
|
229 else if (kw == "rows") |
4687
|
230 { |
5275
|
231 octave_idx_type nr = val; |
|
232 octave_idx_type nc = 0; |
5099
|
233 |
|
234 if (nr >= 0 && extract_keyword (is, "columns", nc) && nc >= 0) |
|
235 { |
|
236 if (nr > 0 && nc > 0) |
|
237 { |
|
238 Matrix tmp (nr, nc); |
|
239 is >> tmp; |
|
240 if (!is) |
|
241 { |
|
242 error ("load: failed to load matrix constant"); |
|
243 success = false; |
|
244 } |
|
245 |
5275
|
246 boolMatrix btmp (nr, nc); |
|
247 for (octave_idx_type j = 0; j < nc; j++) |
|
248 for (octave_idx_type i = 0; i < nr; i++) |
5099
|
249 btmp.elem (i,j) = (tmp.elem (i, j) != 0.); |
|
250 |
|
251 matrix = btmp; |
|
252 } |
|
253 else if (nr == 0 || nc == 0) |
|
254 matrix = boolMatrix (nr, nc); |
|
255 else |
|
256 panic_impossible (); |
|
257 } |
|
258 else |
|
259 { |
|
260 error ("load: failed to extract number of rows and columns"); |
|
261 success = false; |
|
262 } |
4687
|
263 } |
5099
|
264 else |
|
265 panic_impossible (); |
|
266 } |
|
267 else |
|
268 { |
|
269 error ("load: failed to extract number of rows and columns"); |
|
270 success = false; |
4687
|
271 } |
|
272 |
|
273 return success; |
|
274 } |
|
275 |
|
276 bool |
|
277 octave_bool_matrix::save_binary (std::ostream& os, bool& /* save_as_floats */) |
|
278 { |
|
279 |
|
280 dim_vector d = dims (); |
|
281 if (d.length() < 1) |
|
282 return false; |
|
283 |
|
284 // Use negative value for ndims to differentiate with old format!! |
|
285 FOUR_BYTE_INT tmp = - d.length(); |
5760
|
286 os.write (reinterpret_cast<char *> (&tmp), 4); |
5275
|
287 for (int i = 0; i < d.length (); i++) |
4687
|
288 { |
|
289 tmp = d(i); |
5760
|
290 os.write (reinterpret_cast<char *> (&tmp), 4); |
4687
|
291 } |
|
292 |
|
293 boolNDArray m = bool_array_value (); |
|
294 bool *mtmp = m.fortran_vec (); |
5275
|
295 octave_idx_type nel = m.nelem (); |
4687
|
296 OCTAVE_LOCAL_BUFFER (char, htmp, nel); |
|
297 |
5275
|
298 for (octave_idx_type i = 0; i < nel; i++) |
4687
|
299 htmp[i] = (mtmp[i] ? 1 : 0); |
|
300 |
|
301 os.write (htmp, nel); |
|
302 |
|
303 return true; |
|
304 } |
|
305 |
|
306 bool |
|
307 octave_bool_matrix::load_binary (std::istream& is, bool swap, |
|
308 oct_mach_info::float_format /* fmt */) |
|
309 { |
|
310 FOUR_BYTE_INT mdims; |
5760
|
311 if (! is.read (reinterpret_cast<char *> (&mdims), 4)) |
4687
|
312 return false; |
|
313 if (swap) |
4944
|
314 swap_bytes<4> (&mdims); |
4687
|
315 if (mdims >= 0) |
|
316 return false; |
|
317 |
|
318 // mdims is negative for consistency with other matrices, where it is |
|
319 // negative to allow the positive value to be used for rows/cols for |
|
320 // backward compatibility |
|
321 mdims = - mdims; |
|
322 FOUR_BYTE_INT di; |
|
323 dim_vector dv; |
|
324 dv.resize (mdims); |
|
325 |
|
326 for (int i = 0; i < mdims; i++) |
|
327 { |
5760
|
328 if (! is.read (reinterpret_cast<char *> (&di), 4)) |
4687
|
329 return false; |
|
330 if (swap) |
4944
|
331 swap_bytes<4> (&di); |
4687
|
332 dv(i) = di; |
|
333 } |
|
334 |
5157
|
335 // Convert an array with a single dimension to be a row vector. |
|
336 // Octave should never write files like this, other software |
|
337 // might. |
|
338 |
|
339 if (mdims == 1) |
|
340 { |
|
341 mdims = 2; |
|
342 dv.resize (mdims); |
|
343 dv(1) = dv(0); |
|
344 dv(0) = 1; |
|
345 } |
|
346 |
5275
|
347 octave_idx_type nel = dv.numel (); |
4687
|
348 OCTAVE_LOCAL_BUFFER (char, htmp, nel); |
|
349 if (! is.read (htmp, nel)) |
|
350 return false; |
|
351 boolNDArray m(dv); |
|
352 bool *mtmp = m.fortran_vec (); |
5275
|
353 for (octave_idx_type i = 0; i < nel; i++) |
4687
|
354 mtmp[i] = (htmp[i] ? 1 : 0); |
|
355 matrix = m; |
|
356 |
|
357 return true; |
|
358 } |
|
359 |
|
360 #if defined (HAVE_HDF5) |
4944
|
361 |
4687
|
362 bool |
|
363 octave_bool_matrix::save_hdf5 (hid_t loc_id, const char *name, |
|
364 bool /* save_as_floats */) |
|
365 { |
4837
|
366 dim_vector dv = dims (); |
|
367 int empty = save_hdf5_empty (loc_id, name, dv); |
|
368 if (empty) |
4805
|
369 return (empty > 0); |
|
370 |
4837
|
371 int rank = dv.length (); |
4687
|
372 hid_t space_hid = -1, data_hid = -1; |
|
373 bool retval = true; |
|
374 boolNDArray m = bool_array_value (); |
|
375 |
4805
|
376 OCTAVE_LOCAL_BUFFER (hsize_t, hdims, rank); |
|
377 |
4687
|
378 // Octave uses column-major, while HDF5 uses row-major ordering |
4805
|
379 for (int i = 0; i < rank; i++) |
4837
|
380 hdims[i] = dv (rank-i-1); |
4687
|
381 |
4815
|
382 space_hid = H5Screate_simple (rank, hdims, 0); |
4687
|
383 if (space_hid < 0) return false; |
|
384 |
|
385 data_hid = H5Dcreate (loc_id, name, H5T_NATIVE_HBOOL, space_hid, |
|
386 H5P_DEFAULT); |
|
387 if (data_hid < 0) |
|
388 { |
|
389 H5Sclose (space_hid); |
|
390 return false; |
|
391 } |
|
392 |
5275
|
393 octave_idx_type nel = m.nelem (); |
4687
|
394 bool *mtmp = m.fortran_vec (); |
|
395 hbool_t htmp[nel]; |
|
396 |
5275
|
397 for (octave_idx_type i = 0; i < nel; i++) |
4687
|
398 htmp[i] = mtmp[i]; |
|
399 |
|
400 retval = H5Dwrite (data_hid, H5T_NATIVE_HBOOL, H5S_ALL, H5S_ALL, |
4815
|
401 H5P_DEFAULT, htmp) >= 0; |
4687
|
402 |
|
403 H5Dclose (data_hid); |
|
404 H5Sclose (space_hid); |
4837
|
405 |
4687
|
406 return retval; |
|
407 } |
|
408 |
|
409 bool |
|
410 octave_bool_matrix::load_hdf5 (hid_t loc_id, const char *name, |
|
411 bool /* have_h5giterate_bug */) |
|
412 { |
4837
|
413 bool retval = false; |
|
414 |
4805
|
415 dim_vector dv; |
|
416 int empty = load_hdf5_empty (loc_id, name, dv); |
|
417 if (empty > 0) |
|
418 matrix.resize(dv); |
4837
|
419 if (empty) |
|
420 return (empty > 0); |
4805
|
421 |
4687
|
422 hid_t data_hid = H5Dopen (loc_id, name); |
|
423 hid_t space_id = H5Dget_space (data_hid); |
|
424 |
|
425 hsize_t rank = H5Sget_simple_extent_ndims (space_id); |
|
426 |
|
427 if (rank < 1) |
|
428 { |
|
429 H5Dclose (data_hid); |
|
430 return false; |
|
431 } |
|
432 |
|
433 OCTAVE_LOCAL_BUFFER (hsize_t, hdims, rank); |
|
434 OCTAVE_LOCAL_BUFFER (hsize_t, maxdims, rank); |
|
435 |
|
436 H5Sget_simple_extent_dims (space_id, hdims, maxdims); |
|
437 |
|
438 // Octave uses column-major, while HDF5 uses row-major ordering |
|
439 if (rank == 1) |
|
440 { |
|
441 dv.resize (2); |
|
442 dv(0) = 1; |
|
443 dv(1) = hdims[0]; |
|
444 } |
|
445 else |
|
446 { |
|
447 dv.resize (rank); |
4815
|
448 for (hsize_t i = 0, j = rank - 1; i < rank; i++, j--) |
4687
|
449 dv(j) = hdims[i]; |
|
450 } |
|
451 |
5275
|
452 octave_idx_type nel = dv.numel (); |
4687
|
453 hbool_t htmp[nel]; |
|
454 if (H5Dread (data_hid, H5T_NATIVE_HBOOL, H5S_ALL, H5S_ALL, |
4815
|
455 H5P_DEFAULT, htmp) >= 0) |
4687
|
456 { |
|
457 retval = true; |
|
458 |
|
459 boolNDArray btmp (dv); |
5275
|
460 for (octave_idx_type i = 0; i < nel; i++) |
4687
|
461 btmp.elem (i) = htmp[i]; |
|
462 |
|
463 matrix = btmp; |
|
464 } |
|
465 |
|
466 H5Dclose (data_hid); |
4837
|
467 |
4687
|
468 return retval; |
|
469 } |
4944
|
470 |
4687
|
471 #endif |
|
472 |
2871
|
473 /* |
|
474 ;;; Local Variables: *** |
|
475 ;;; mode: C++ *** |
|
476 ;;; End: *** |
|
477 */ |