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