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