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