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 { |
|
145 octave_value tmp = octave_value (matrix_value ()); |
|
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 int mdims = 0; |
|
191 bool success = true; |
|
192 std::streampos pos = is.tellg (); |
|
193 |
|
194 if (extract_keyword (is, "ndims", mdims, true)) |
|
195 { |
|
196 if (mdims >= 0) |
|
197 { |
|
198 dim_vector dv; |
|
199 dv.resize (mdims); |
|
200 |
|
201 for (int i = 0; i < mdims; i++) |
|
202 is >> dv(i); |
|
203 |
|
204 NDArray tmp(dv); |
|
205 is >> tmp; |
|
206 |
|
207 if (!is) |
|
208 { |
|
209 error ("load: failed to load matrix constant"); |
|
210 success = false; |
|
211 } |
|
212 |
|
213 boolNDArray btmp (dv); |
|
214 for (int i = 0; i < btmp.nelem (); i++) |
|
215 btmp.elem (i) = (tmp.elem (i) != 0.); |
|
216 |
|
217 matrix = btmp; |
|
218 } |
|
219 else |
|
220 { |
|
221 error ("load: failed to extract number of rows and columns"); |
|
222 success = false; |
|
223 } |
|
224 } |
|
225 else |
|
226 { |
|
227 int nr = 0; |
|
228 int nc = 0; |
|
229 |
|
230 // re-read the same line again |
|
231 is.clear (); |
|
232 is.seekg (pos); |
|
233 |
|
234 if (extract_keyword (is, "rows", nr) && nr >= 0 |
|
235 && extract_keyword (is, "columns", nc) && nc >= 0) |
|
236 { |
|
237 if (nr > 0 && nc > 0) |
|
238 { |
|
239 Matrix tmp (nr, nc); |
|
240 is >> tmp; |
|
241 if (!is) |
|
242 { |
|
243 error ("load: failed to load matrix constant"); |
|
244 success = false; |
|
245 } |
|
246 |
|
247 boolMatrix btmp (nr,nc); |
|
248 for (int j = 0; j < nc; j++) |
|
249 for (int i = 0; i < nr; i++) |
|
250 btmp.elem (i,j) = (tmp.elem (i, j) != 0.); |
|
251 |
|
252 matrix = btmp; |
|
253 } |
|
254 else if (nr == 0 || nc == 0) |
|
255 matrix = boolMatrix (nr, nc); |
|
256 else |
|
257 panic_impossible (); |
|
258 } |
|
259 else |
|
260 { |
|
261 error ("load: failed to extract number of rows and columns"); |
|
262 success = false; |
|
263 } |
|
264 } |
|
265 |
|
266 return success; |
|
267 } |
|
268 |
|
269 bool |
|
270 octave_bool_matrix::save_binary (std::ostream& os, bool& /* save_as_floats */) |
|
271 { |
|
272 |
|
273 dim_vector d = dims (); |
|
274 if (d.length() < 1) |
|
275 return false; |
|
276 |
|
277 // Use negative value for ndims to differentiate with old format!! |
|
278 FOUR_BYTE_INT tmp = - d.length(); |
|
279 os.write (X_CAST (char *, &tmp), 4); |
|
280 for (int i=0; i < d.length (); i++) |
|
281 { |
|
282 tmp = d(i); |
|
283 os.write (X_CAST (char *, &tmp), 4); |
|
284 } |
|
285 |
|
286 boolNDArray m = bool_array_value (); |
|
287 bool *mtmp = m.fortran_vec (); |
|
288 int nel = m.nelem (); |
|
289 OCTAVE_LOCAL_BUFFER (char, htmp, nel); |
|
290 |
|
291 for (int i = 0; i < nel; i++) |
|
292 htmp[i] = (mtmp[i] ? 1 : 0); |
|
293 |
|
294 os.write (htmp, nel); |
|
295 |
|
296 return true; |
|
297 } |
|
298 |
|
299 bool |
|
300 octave_bool_matrix::load_binary (std::istream& is, bool swap, |
|
301 oct_mach_info::float_format /* fmt */) |
|
302 { |
|
303 FOUR_BYTE_INT mdims; |
|
304 if (! is.read (X_CAST (char *, &mdims), 4)) |
|
305 return false; |
|
306 if (swap) |
|
307 swap_4_bytes (X_CAST (char *, &mdims)); |
|
308 if (mdims >= 0) |
|
309 return false; |
|
310 |
|
311 // mdims is negative for consistency with other matrices, where it is |
|
312 // negative to allow the positive value to be used for rows/cols for |
|
313 // backward compatibility |
|
314 mdims = - mdims; |
|
315 FOUR_BYTE_INT di; |
|
316 dim_vector dv; |
|
317 dv.resize (mdims); |
|
318 |
|
319 for (int i = 0; i < mdims; i++) |
|
320 { |
|
321 if (! is.read (X_CAST (char *, &di), 4)) |
|
322 return false; |
|
323 if (swap) |
|
324 swap_4_bytes (X_CAST (char *, &di)); |
|
325 dv(i) = di; |
|
326 } |
|
327 |
|
328 int nel = dv.numel (); |
|
329 OCTAVE_LOCAL_BUFFER (char, htmp, nel); |
|
330 if (! is.read (htmp, nel)) |
|
331 return false; |
|
332 boolNDArray m(dv); |
|
333 bool *mtmp = m.fortran_vec (); |
|
334 for (int i = 0; i < nel; i++) |
|
335 mtmp[i] = (htmp[i] ? 1 : 0); |
|
336 matrix = m; |
|
337 |
|
338 return true; |
|
339 } |
|
340 |
|
341 #if defined (HAVE_HDF5) |
|
342 bool |
|
343 octave_bool_matrix::save_hdf5 (hid_t loc_id, const char *name, |
|
344 bool /* save_as_floats */) |
|
345 { |
|
346 dim_vector d = dims (); |
4805
|
347 int empty = save_hdf5_empty (loc_id, name, d); |
|
348 if (empty != 0) |
|
349 return (empty > 0); |
|
350 |
|
351 int rank = d.length (); |
4687
|
352 hid_t space_hid = -1, data_hid = -1; |
|
353 bool retval = true; |
|
354 boolNDArray m = bool_array_value (); |
|
355 |
4805
|
356 OCTAVE_LOCAL_BUFFER (hsize_t, hdims, rank); |
|
357 |
4687
|
358 // Octave uses column-major, while HDF5 uses row-major ordering |
4805
|
359 for (int i = 0; i < rank; i++) |
|
360 hdims[i] = d (rank-i-1); |
4687
|
361 |
|
362 space_hid = H5Screate_simple (rank, hdims, (hsize_t*) 0); |
|
363 if (space_hid < 0) return false; |
|
364 |
|
365 data_hid = H5Dcreate (loc_id, name, H5T_NATIVE_HBOOL, space_hid, |
|
366 H5P_DEFAULT); |
|
367 if (data_hid < 0) |
|
368 { |
|
369 H5Sclose (space_hid); |
|
370 return false; |
|
371 } |
|
372 |
|
373 int nel = m.nelem (); |
|
374 bool *mtmp = m.fortran_vec (); |
|
375 hbool_t htmp[nel]; |
|
376 |
|
377 for (int i = 0; i < nel; i++) |
|
378 htmp[i] = mtmp[i]; |
|
379 |
|
380 retval = H5Dwrite (data_hid, H5T_NATIVE_HBOOL, H5S_ALL, H5S_ALL, |
|
381 H5P_DEFAULT, (void*) htmp) >= 0; |
|
382 |
|
383 H5Dclose (data_hid); |
|
384 H5Sclose (space_hid); |
|
385 return retval; |
|
386 } |
|
387 |
|
388 bool |
|
389 octave_bool_matrix::load_hdf5 (hid_t loc_id, const char *name, |
|
390 bool /* have_h5giterate_bug */) |
|
391 { |
4805
|
392 dim_vector dv; |
|
393 int empty = load_hdf5_empty (loc_id, name, dv); |
|
394 if (empty > 0) |
|
395 matrix.resize(dv); |
|
396 if (empty != 0) |
|
397 return (empty > 0); |
|
398 |
4687
|
399 bool retval = false; |
|
400 hid_t data_hid = H5Dopen (loc_id, name); |
|
401 hid_t space_id = H5Dget_space (data_hid); |
|
402 |
|
403 hsize_t rank = H5Sget_simple_extent_ndims (space_id); |
|
404 |
|
405 if (rank < 1) |
|
406 { |
|
407 H5Dclose (data_hid); |
|
408 return false; |
|
409 } |
|
410 |
|
411 OCTAVE_LOCAL_BUFFER (hsize_t, hdims, rank); |
|
412 OCTAVE_LOCAL_BUFFER (hsize_t, maxdims, rank); |
|
413 |
|
414 H5Sget_simple_extent_dims (space_id, hdims, maxdims); |
|
415 |
|
416 // Octave uses column-major, while HDF5 uses row-major ordering |
|
417 if (rank == 1) |
|
418 { |
|
419 dv.resize (2); |
|
420 dv(0) = 1; |
|
421 dv(1) = hdims[0]; |
|
422 } |
|
423 else |
|
424 { |
|
425 dv.resize (rank); |
|
426 for (int i = 0, j = rank - 1; i < (int)rank; i++, j--) |
|
427 dv(j) = hdims[i]; |
|
428 } |
|
429 |
|
430 int nel = dv.numel (); |
|
431 hbool_t htmp[nel]; |
|
432 if (H5Dread (data_hid, H5T_NATIVE_HBOOL, H5S_ALL, H5S_ALL, |
|
433 H5P_DEFAULT, (void *) htmp) >= 0) |
|
434 { |
|
435 retval = true; |
|
436 |
|
437 boolNDArray btmp (dv); |
|
438 for (int i = 0; i < nel; i++) |
|
439 btmp.elem (i) = htmp[i]; |
|
440 |
|
441 matrix = btmp; |
|
442 } |
|
443 |
|
444 H5Dclose (data_hid); |
|
445 return retval; |
|
446 } |
|
447 #endif |
|
448 |
2871
|
449 /* |
|
450 ;;; Local Variables: *** |
|
451 ;;; mode: C++ *** |
|
452 ;;; End: *** |
|
453 */ |