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