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