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