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 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) |
4944
|
307 swap_bytes<4> (&mdims); |
4687
|
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) |
4944
|
324 swap_bytes<4> (&di); |
4687
|
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) |
4944
|
342 |
4687
|
343 bool |
|
344 octave_bool_matrix::save_hdf5 (hid_t loc_id, const char *name, |
|
345 bool /* save_as_floats */) |
|
346 { |
4837
|
347 dim_vector dv = dims (); |
|
348 int empty = save_hdf5_empty (loc_id, name, dv); |
|
349 if (empty) |
4805
|
350 return (empty > 0); |
|
351 |
4837
|
352 int rank = dv.length (); |
4687
|
353 hid_t space_hid = -1, data_hid = -1; |
|
354 bool retval = true; |
|
355 boolNDArray m = bool_array_value (); |
|
356 |
4805
|
357 OCTAVE_LOCAL_BUFFER (hsize_t, hdims, rank); |
|
358 |
4687
|
359 // Octave uses column-major, while HDF5 uses row-major ordering |
4805
|
360 for (int i = 0; i < rank; i++) |
4837
|
361 hdims[i] = dv (rank-i-1); |
4687
|
362 |
4815
|
363 space_hid = H5Screate_simple (rank, hdims, 0); |
4687
|
364 if (space_hid < 0) return false; |
|
365 |
|
366 data_hid = H5Dcreate (loc_id, name, H5T_NATIVE_HBOOL, space_hid, |
|
367 H5P_DEFAULT); |
|
368 if (data_hid < 0) |
|
369 { |
|
370 H5Sclose (space_hid); |
|
371 return false; |
|
372 } |
|
373 |
|
374 int nel = m.nelem (); |
|
375 bool *mtmp = m.fortran_vec (); |
|
376 hbool_t htmp[nel]; |
|
377 |
|
378 for (int i = 0; i < nel; i++) |
|
379 htmp[i] = mtmp[i]; |
|
380 |
|
381 retval = H5Dwrite (data_hid, H5T_NATIVE_HBOOL, H5S_ALL, H5S_ALL, |
4815
|
382 H5P_DEFAULT, htmp) >= 0; |
4687
|
383 |
|
384 H5Dclose (data_hid); |
|
385 H5Sclose (space_hid); |
4837
|
386 |
4687
|
387 return retval; |
|
388 } |
|
389 |
|
390 bool |
|
391 octave_bool_matrix::load_hdf5 (hid_t loc_id, const char *name, |
|
392 bool /* have_h5giterate_bug */) |
|
393 { |
4837
|
394 bool retval = false; |
|
395 |
4805
|
396 dim_vector dv; |
|
397 int empty = load_hdf5_empty (loc_id, name, dv); |
|
398 if (empty > 0) |
|
399 matrix.resize(dv); |
4837
|
400 if (empty) |
|
401 return (empty > 0); |
4805
|
402 |
4687
|
403 hid_t data_hid = H5Dopen (loc_id, name); |
|
404 hid_t space_id = H5Dget_space (data_hid); |
|
405 |
|
406 hsize_t rank = H5Sget_simple_extent_ndims (space_id); |
|
407 |
|
408 if (rank < 1) |
|
409 { |
|
410 H5Dclose (data_hid); |
|
411 return false; |
|
412 } |
|
413 |
|
414 OCTAVE_LOCAL_BUFFER (hsize_t, hdims, rank); |
|
415 OCTAVE_LOCAL_BUFFER (hsize_t, maxdims, rank); |
|
416 |
|
417 H5Sget_simple_extent_dims (space_id, hdims, maxdims); |
|
418 |
|
419 // Octave uses column-major, while HDF5 uses row-major ordering |
|
420 if (rank == 1) |
|
421 { |
|
422 dv.resize (2); |
|
423 dv(0) = 1; |
|
424 dv(1) = hdims[0]; |
|
425 } |
|
426 else |
|
427 { |
|
428 dv.resize (rank); |
4815
|
429 for (hsize_t i = 0, j = rank - 1; i < rank; i++, j--) |
4687
|
430 dv(j) = hdims[i]; |
|
431 } |
|
432 |
|
433 int nel = dv.numel (); |
|
434 hbool_t htmp[nel]; |
|
435 if (H5Dread (data_hid, H5T_NATIVE_HBOOL, H5S_ALL, H5S_ALL, |
4815
|
436 H5P_DEFAULT, htmp) >= 0) |
4687
|
437 { |
|
438 retval = true; |
|
439 |
|
440 boolNDArray btmp (dv); |
|
441 for (int i = 0; i < nel; i++) |
|
442 btmp.elem (i) = htmp[i]; |
|
443 |
|
444 matrix = btmp; |
|
445 } |
|
446 |
|
447 H5Dclose (data_hid); |
4837
|
448 |
4687
|
449 return retval; |
|
450 } |
4944
|
451 |
4687
|
452 #endif |
|
453 |
2871
|
454 /* |
|
455 ;;; Local Variables: *** |
|
456 ;;; mode: C++ *** |
|
457 ;;; End: *** |
|
458 */ |