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 |
5958
|
152 octave_bool_matrix::save_ascii (std::ostream& os, bool& /* infnan_warned */) |
4687
|
153 { |
|
154 dim_vector d = dims (); |
|
155 if (d.length () > 2) |
|
156 { |
|
157 NDArray tmp = array_value (); |
|
158 os << "# ndims: " << d.length () << "\n"; |
|
159 |
5275
|
160 for (int i = 0; i < d.length (); i++) |
4687
|
161 os << " " << d (i); |
|
162 |
|
163 os << "\n" << tmp; |
|
164 } |
|
165 else |
|
166 { |
|
167 // Keep this case, rather than use generic code above for backward |
|
168 // compatiability. Makes load_ascii much more complex!! |
|
169 os << "# rows: " << rows () << "\n" |
|
170 << "# columns: " << columns () << "\n"; |
|
171 |
|
172 Matrix tmp = matrix_value (); |
|
173 |
|
174 os << tmp; |
|
175 } |
|
176 |
|
177 return true; |
|
178 } |
|
179 |
|
180 bool |
|
181 octave_bool_matrix::load_ascii (std::istream& is) |
|
182 { |
|
183 bool success = true; |
|
184 |
5099
|
185 string_vector keywords (2); |
4687
|
186 |
5099
|
187 keywords[0] = "ndims"; |
|
188 keywords[1] = "rows"; |
4687
|
189 |
5099
|
190 std::string kw; |
5275
|
191 octave_idx_type val = 0; |
4687
|
192 |
5099
|
193 if (extract_keyword (is, keywords, kw, val, true)) |
4687
|
194 { |
5099
|
195 if (kw == "ndims") |
|
196 { |
5275
|
197 int mdims = static_cast<int> (val); |
4687
|
198 |
5099
|
199 if (mdims >= 0) |
4687
|
200 { |
5099
|
201 dim_vector dv; |
|
202 dv.resize (mdims); |
|
203 |
|
204 for (int i = 0; i < mdims; i++) |
|
205 is >> dv(i); |
|
206 |
6717
|
207 if (is) |
|
208 { |
|
209 boolNDArray btmp (dv); |
|
210 |
|
211 if (btmp.is_empty ()) |
|
212 matrix = btmp; |
|
213 else |
|
214 { |
|
215 NDArray tmp(dv); |
|
216 is >> tmp; |
5099
|
217 |
6717
|
218 if (is) |
|
219 { |
|
220 for (octave_idx_type i = 0; i < btmp.nelem (); i++) |
|
221 btmp.elem (i) = (tmp.elem (i) != 0.); |
|
222 |
|
223 matrix = btmp; |
|
224 } |
|
225 else |
|
226 { |
|
227 error ("load: failed to load matrix constant"); |
|
228 success = false; |
|
229 } |
|
230 } |
|
231 } |
|
232 else |
4687
|
233 { |
6717
|
234 error ("load: failed to extract dimensions"); |
4687
|
235 success = false; |
|
236 } |
|
237 } |
|
238 else |
5099
|
239 { |
6717
|
240 error ("load: failed to extract number of dimensions"); |
5099
|
241 success = false; |
|
242 } |
4687
|
243 } |
5099
|
244 else if (kw == "rows") |
4687
|
245 { |
5275
|
246 octave_idx_type nr = val; |
|
247 octave_idx_type nc = 0; |
5099
|
248 |
|
249 if (nr >= 0 && extract_keyword (is, "columns", nc) && nc >= 0) |
|
250 { |
|
251 if (nr > 0 && nc > 0) |
|
252 { |
|
253 Matrix tmp (nr, nc); |
|
254 is >> tmp; |
6717
|
255 if (is) |
|
256 { |
|
257 boolMatrix btmp (nr, nc); |
|
258 for (octave_idx_type j = 0; j < nc; j++) |
|
259 for (octave_idx_type i = 0; i < nr; i++) |
|
260 btmp.elem (i,j) = (tmp.elem (i, j) != 0.); |
|
261 |
|
262 matrix = btmp; |
|
263 } |
|
264 else |
5099
|
265 { |
|
266 error ("load: failed to load matrix constant"); |
|
267 success = false; |
|
268 } |
|
269 } |
|
270 else if (nr == 0 || nc == 0) |
|
271 matrix = boolMatrix (nr, nc); |
|
272 else |
|
273 panic_impossible (); |
|
274 } |
|
275 else |
|
276 { |
|
277 error ("load: failed to extract number of rows and columns"); |
|
278 success = false; |
|
279 } |
4687
|
280 } |
5099
|
281 else |
|
282 panic_impossible (); |
|
283 } |
|
284 else |
|
285 { |
|
286 error ("load: failed to extract number of rows and columns"); |
|
287 success = false; |
4687
|
288 } |
|
289 |
|
290 return success; |
|
291 } |
|
292 |
|
293 bool |
|
294 octave_bool_matrix::save_binary (std::ostream& os, bool& /* save_as_floats */) |
|
295 { |
|
296 |
|
297 dim_vector d = dims (); |
|
298 if (d.length() < 1) |
|
299 return false; |
|
300 |
|
301 // Use negative value for ndims to differentiate with old format!! |
5828
|
302 int32_t tmp = - d.length(); |
5760
|
303 os.write (reinterpret_cast<char *> (&tmp), 4); |
5275
|
304 for (int i = 0; i < d.length (); i++) |
4687
|
305 { |
|
306 tmp = d(i); |
5760
|
307 os.write (reinterpret_cast<char *> (&tmp), 4); |
4687
|
308 } |
|
309 |
|
310 boolNDArray m = bool_array_value (); |
|
311 bool *mtmp = m.fortran_vec (); |
5275
|
312 octave_idx_type nel = m.nelem (); |
4687
|
313 OCTAVE_LOCAL_BUFFER (char, htmp, nel); |
|
314 |
5275
|
315 for (octave_idx_type i = 0; i < nel; i++) |
4687
|
316 htmp[i] = (mtmp[i] ? 1 : 0); |
|
317 |
|
318 os.write (htmp, nel); |
|
319 |
|
320 return true; |
|
321 } |
|
322 |
|
323 bool |
|
324 octave_bool_matrix::load_binary (std::istream& is, bool swap, |
|
325 oct_mach_info::float_format /* fmt */) |
|
326 { |
5828
|
327 int32_t mdims; |
5760
|
328 if (! is.read (reinterpret_cast<char *> (&mdims), 4)) |
4687
|
329 return false; |
|
330 if (swap) |
4944
|
331 swap_bytes<4> (&mdims); |
4687
|
332 if (mdims >= 0) |
|
333 return false; |
|
334 |
|
335 // mdims is negative for consistency with other matrices, where it is |
|
336 // negative to allow the positive value to be used for rows/cols for |
|
337 // backward compatibility |
|
338 mdims = - mdims; |
5828
|
339 int32_t di; |
4687
|
340 dim_vector dv; |
|
341 dv.resize (mdims); |
|
342 |
|
343 for (int i = 0; i < mdims; i++) |
|
344 { |
5760
|
345 if (! is.read (reinterpret_cast<char *> (&di), 4)) |
4687
|
346 return false; |
|
347 if (swap) |
4944
|
348 swap_bytes<4> (&di); |
4687
|
349 dv(i) = di; |
|
350 } |
|
351 |
5157
|
352 // Convert an array with a single dimension to be a row vector. |
|
353 // Octave should never write files like this, other software |
|
354 // might. |
|
355 |
|
356 if (mdims == 1) |
|
357 { |
|
358 mdims = 2; |
|
359 dv.resize (mdims); |
|
360 dv(1) = dv(0); |
|
361 dv(0) = 1; |
|
362 } |
|
363 |
5275
|
364 octave_idx_type nel = dv.numel (); |
4687
|
365 OCTAVE_LOCAL_BUFFER (char, htmp, nel); |
|
366 if (! is.read (htmp, nel)) |
|
367 return false; |
|
368 boolNDArray m(dv); |
|
369 bool *mtmp = m.fortran_vec (); |
5275
|
370 for (octave_idx_type i = 0; i < nel; i++) |
4687
|
371 mtmp[i] = (htmp[i] ? 1 : 0); |
|
372 matrix = m; |
|
373 |
|
374 return true; |
|
375 } |
|
376 |
|
377 #if defined (HAVE_HDF5) |
4944
|
378 |
4687
|
379 bool |
|
380 octave_bool_matrix::save_hdf5 (hid_t loc_id, const char *name, |
|
381 bool /* save_as_floats */) |
|
382 { |
4837
|
383 dim_vector dv = dims (); |
|
384 int empty = save_hdf5_empty (loc_id, name, dv); |
|
385 if (empty) |
4805
|
386 return (empty > 0); |
|
387 |
4837
|
388 int rank = dv.length (); |
4687
|
389 hid_t space_hid = -1, data_hid = -1; |
|
390 bool retval = true; |
|
391 boolNDArray m = bool_array_value (); |
|
392 |
4805
|
393 OCTAVE_LOCAL_BUFFER (hsize_t, hdims, rank); |
|
394 |
4687
|
395 // Octave uses column-major, while HDF5 uses row-major ordering |
4805
|
396 for (int i = 0; i < rank; i++) |
4837
|
397 hdims[i] = dv (rank-i-1); |
4687
|
398 |
4815
|
399 space_hid = H5Screate_simple (rank, hdims, 0); |
4687
|
400 if (space_hid < 0) return false; |
|
401 |
|
402 data_hid = H5Dcreate (loc_id, name, H5T_NATIVE_HBOOL, space_hid, |
|
403 H5P_DEFAULT); |
|
404 if (data_hid < 0) |
|
405 { |
|
406 H5Sclose (space_hid); |
|
407 return false; |
|
408 } |
|
409 |
5275
|
410 octave_idx_type nel = m.nelem (); |
4687
|
411 bool *mtmp = m.fortran_vec (); |
6276
|
412 OCTAVE_LOCAL_BUFFER (hbool_t, htmp, nel); |
4687
|
413 |
5275
|
414 for (octave_idx_type i = 0; i < nel; i++) |
4687
|
415 htmp[i] = mtmp[i]; |
|
416 |
|
417 retval = H5Dwrite (data_hid, H5T_NATIVE_HBOOL, H5S_ALL, H5S_ALL, |
4815
|
418 H5P_DEFAULT, htmp) >= 0; |
4687
|
419 |
|
420 H5Dclose (data_hid); |
|
421 H5Sclose (space_hid); |
4837
|
422 |
4687
|
423 return retval; |
|
424 } |
|
425 |
|
426 bool |
|
427 octave_bool_matrix::load_hdf5 (hid_t loc_id, const char *name, |
|
428 bool /* have_h5giterate_bug */) |
|
429 { |
4837
|
430 bool retval = false; |
|
431 |
4805
|
432 dim_vector dv; |
|
433 int empty = load_hdf5_empty (loc_id, name, dv); |
|
434 if (empty > 0) |
|
435 matrix.resize(dv); |
4837
|
436 if (empty) |
|
437 return (empty > 0); |
4805
|
438 |
4687
|
439 hid_t data_hid = H5Dopen (loc_id, name); |
|
440 hid_t space_id = H5Dget_space (data_hid); |
|
441 |
|
442 hsize_t rank = H5Sget_simple_extent_ndims (space_id); |
|
443 |
|
444 if (rank < 1) |
|
445 { |
|
446 H5Dclose (data_hid); |
|
447 return false; |
|
448 } |
|
449 |
|
450 OCTAVE_LOCAL_BUFFER (hsize_t, hdims, rank); |
|
451 OCTAVE_LOCAL_BUFFER (hsize_t, maxdims, rank); |
|
452 |
|
453 H5Sget_simple_extent_dims (space_id, hdims, maxdims); |
|
454 |
|
455 // Octave uses column-major, while HDF5 uses row-major ordering |
|
456 if (rank == 1) |
|
457 { |
|
458 dv.resize (2); |
|
459 dv(0) = 1; |
|
460 dv(1) = hdims[0]; |
|
461 } |
|
462 else |
|
463 { |
|
464 dv.resize (rank); |
4815
|
465 for (hsize_t i = 0, j = rank - 1; i < rank; i++, j--) |
4687
|
466 dv(j) = hdims[i]; |
|
467 } |
|
468 |
5275
|
469 octave_idx_type nel = dv.numel (); |
6276
|
470 OCTAVE_LOCAL_BUFFER (hbool_t, htmp, nel); |
4687
|
471 if (H5Dread (data_hid, H5T_NATIVE_HBOOL, H5S_ALL, H5S_ALL, |
6276
|
472 H5P_DEFAULT, &htmp[0]) >= 0) |
4687
|
473 { |
|
474 retval = true; |
|
475 |
|
476 boolNDArray btmp (dv); |
5275
|
477 for (octave_idx_type i = 0; i < nel; i++) |
4687
|
478 btmp.elem (i) = htmp[i]; |
|
479 |
|
480 matrix = btmp; |
|
481 } |
|
482 |
|
483 H5Dclose (data_hid); |
4837
|
484 |
4687
|
485 return retval; |
|
486 } |
4944
|
487 |
4687
|
488 #endif |
|
489 |
5900
|
490 mxArray * |
|
491 octave_bool_matrix::as_mxArray (void) const |
|
492 { |
|
493 mxArray *retval = new mxArray (mxLOGICAL_CLASS, dims (), mxREAL); |
|
494 |
|
495 bool *pr = static_cast<bool *> (retval->get_data ()); |
|
496 |
6686
|
497 mwSize nel = numel (); |
5900
|
498 |
|
499 const bool *p = matrix.data (); |
|
500 |
6686
|
501 for (mwIndex i = 0; i < nel; i++) |
5900
|
502 pr[i] = p[i]; |
|
503 |
|
504 return retval; |
|
505 } |
|
506 |
2871
|
507 /* |
|
508 ;;; Local Variables: *** |
|
509 ;;; mode: C++ *** |
|
510 ;;; End: *** |
|
511 */ |