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