GNU Octave  9.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
ov-bool.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1996-2024 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if defined (HAVE_CONFIG_H)
27 # include "config.h"
28 #endif
29 
30 #include <istream>
31 #include <ostream>
32 
33 #include "oct-inttypes-fwd.h"
34 
35 #include "mx-base.h"
36 
37 #include "errwarn.h"
38 #include "mxarray.h"
39 #include "oct-hdf5.h"
40 #include "ovl.h"
41 #include "ops.h"
42 #include "ov-bool.h"
43 #include "ov-bool-mat.h"
44 #include "ov-base.h"
45 #include "ov-base-scalar.h"
46 #include "ov-base-scalar.cc"
47 #include "ov-re-mat.h"
48 #include "ov-scalar.h"
49 #include "pr-output.h"
50 
51 #include "ls-oct-text.h"
52 #include "ls-hdf5.h"
53 
54 // Prevent implicit instantiations on some systems (Windows, others?)
55 // that can lead to duplicate definitions of static data members.
56 
57 extern template class octave_base_scalar<double>;
58 
59 template class octave_base_scalar<bool>;
60 
62 
63 static octave_base_value *
64 default_numeric_conversion_function (const octave_base_value& a)
65 {
66  const octave_bool& v = dynamic_cast<const octave_bool&> (a);
67 
68  return new octave_scalar (v.bool_value ());
69 }
70 
73 {
74  return octave_base_value::type_conv_info (default_numeric_conversion_function,
76 
77 }
78 
80 octave_bool::do_index_op (const octave_value_list& idx, bool resize_ok)
81 {
82  // FIXME: this doesn't solve the problem of
83  //
84  // a = 1; a([1,1], [1,1], [1,1])
85  //
86  // and similar constructions. Hmm...
87 
88  // FIXME: using this constructor avoids narrowing the
89  // 1x1 matrix back to a scalar value. Need a better solution
90  // to this problem.
91 
93 
94  return tmp.index_op (idx, resize_ok);
95 }
96 
99 {
100  return static_cast<double> (scalar);
101 }
102 
105 {
106  return static_cast<float> (scalar);
107 }
108 
111 {
112  return octave_int8 (scalar);
113 }
114 
117 {
118  return octave_int16 (scalar);
119 }
120 
123 {
124  return octave_int32 (scalar);
125 }
126 
129 {
130  return octave_int64 (scalar);
131 }
132 
135 {
136  return octave_uint8 (scalar);
137 }
138 
141 {
142  return octave_uint16 (scalar);
143 }
144 
147 {
148  return octave_uint32 (scalar);
149 }
150 
153 {
154  return octave_uint64 (scalar);
155 }
156 
158 octave_bool::resize (const dim_vector& dv, bool fill) const
159 {
160  if (fill)
161  {
162  boolNDArray retval (dv, false);
163  if (dv.numel ())
164  retval(0) = scalar;
165  return retval;
166  }
167  else
168  {
169  boolNDArray retval (dv);
170  if (dv.numel ())
171  retval(0) = scalar;
172  return retval;
173  }
174 }
175 
177 octave_bool::convert_to_str_internal (bool, bool, char type) const
178 {
179  char s[2];
180  s[0] = static_cast<char> (scalar);
181  s[1] = '\0';
182 
183  return octave_value (s, type);
184 }
185 
186 bool
187 octave_bool::save_ascii (std::ostream& os)
188 {
189  double d = double_value ();
190 
191  octave::write_value<double> (os, d);
192  os << "\n";
193 
194  return true;
195 }
196 
197 bool
198 octave_bool::load_ascii (std::istream& is)
199 {
200  scalar = (octave::read_value<double> (is) != 0.0);
201 
202  if (! is)
203  error ("load: failed to load scalar constant");
204 
205  return true;
206 }
207 
208 bool
209 octave_bool::save_binary (std::ostream& os, bool /* save_as_floats */)
210 {
211  char tmp = (scalar ? 1 : 0);
212  os.write (reinterpret_cast<char *> (&tmp), 1);
213 
214  return true;
215 }
216 
217 bool
218 octave_bool::load_binary (std::istream& is, bool /* swap */,
220 {
221  char tmp;
222  if (! is.read (reinterpret_cast<char *> (&tmp), 1))
223  return false;
224  scalar = (tmp ? 1 : 0);
225  return true;
226 }
227 
228 bool
229 octave_bool::save_hdf5 (octave_hdf5_id loc_id, const char *name,
230  bool /* save_as_floats */)
231 {
232  bool retval = false;
233 
234 #if defined (HAVE_HDF5)
235 
236  hsize_t dimens[3] = {0};
237  hid_t space_hid, data_hid;
238  space_hid = data_hid = -1;
239 
240  space_hid = H5Screate_simple (0, dimens, nullptr);
241  if (space_hid < 0) return false;
242 #if defined (HAVE_HDF5_18)
243  data_hid = H5Dcreate (loc_id, name, H5T_NATIVE_DOUBLE, space_hid,
246 #else
247  data_hid = H5Dcreate (loc_id, name, H5T_NATIVE_DOUBLE, space_hid,
249 #endif
250  if (data_hid < 0)
251  {
252  H5Sclose (space_hid);
253  return false;
254  }
255 
256  double tmp = double_value ();
257  retval = H5Dwrite (data_hid, H5T_NATIVE_DOUBLE, octave_H5S_ALL,
258  octave_H5S_ALL, octave_H5P_DEFAULT, &tmp) >= 0;
259 
260  H5Dclose (data_hid);
261  H5Sclose (space_hid);
262 
263 #else
264  octave_unused_parameter (loc_id);
265  octave_unused_parameter (name);
266 
267  warn_save ("hdf5");
268 #endif
269 
270  return retval;
271 }
272 
273 bool
274 octave_bool::load_hdf5 (octave_hdf5_id loc_id, const char *name)
275 {
276 #if defined (HAVE_HDF5)
277 
278 #if defined (HAVE_HDF5_18)
279  hid_t data_hid = H5Dopen (loc_id, name, octave_H5P_DEFAULT);
280 #else
281  hid_t data_hid = H5Dopen (loc_id, name);
282 #endif
283  hid_t space_id = H5Dget_space (data_hid);
284 
285  hsize_t rank = H5Sget_simple_extent_ndims (space_id);
286 
287  if (rank != 0)
288  {
289  H5Dclose (data_hid);
290  return false;
291  }
292 
293  double dtmp;
294  if (H5Dread (data_hid, H5T_NATIVE_DOUBLE, octave_H5S_ALL, octave_H5S_ALL,
295  octave_H5P_DEFAULT, &dtmp) < 0)
296  {
297  H5Dclose (data_hid);
298  return false;
299  }
300 
301  scalar = (dtmp != 0.0);
302 
303  H5Dclose (data_hid);
304 
305 #else
306  octave_unused_parameter (loc_id);
307  octave_unused_parameter (name);
308 
309  warn_load ("hdf5");
310 #endif
311 
312  return true;
313 }
314 
315 mxArray *
316 octave_bool::as_mxArray (bool interleaved) const
317 {
318  mxArray *retval = new mxArray (interleaved, mxLOGICAL_CLASS, 1, 1, mxREAL);
319 
320  mxLogical *pd = static_cast<mxLogical *> (retval->get_data ());
321 
322  pd[0] = scalar;
323 
324  return retval;
325 }
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:94
octave_idx_type numel(int n=0) const
Number of elements that a matrix with this dimensions would have.
Definition: dim-vector.h:335
void * get_data() const
Definition: mxarray.h:473
void warn_load(const char *type) const
Definition: ov-base.cc:1157
friend class octave_value
Definition: ov-base.h:269
void warn_save(const char *type) const
Definition: ov-base.cc:1166
type_conv_info numeric_conversion_function() const
Definition: ov-bool.cc:72
octave_value as_int8() const
Definition: ov-bool.cc:110
octave_value as_int64() const
Definition: ov-bool.cc:128
octave_value as_uint64() const
Definition: ov-bool.cc:152
octave_value convert_to_str_internal(bool pad, bool force, char type) const
Definition: ov-bool.cc:177
octave_value as_int16() const
Definition: ov-bool.cc:116
octave_value as_single() const
Definition: ov-bool.cc:104
octave_value as_uint16() const
Definition: ov-bool.cc:140
boolMatrix bool_matrix_value(bool=false) const
Definition: ov-bool.h:206
octave_value as_uint8() const
Definition: ov-bool.cc:134
octave_value as_uint32() const
Definition: ov-bool.cc:146
double double_value(bool=false) const
Definition: ov-bool.h:150
octave_value as_double() const
Definition: ov-bool.cc:98
bool load_ascii(std::istream &is)
Definition: ov-bool.cc:198
bool bool_value(bool=false) const
Definition: ov-bool.h:204
bool load_binary(std::istream &is, bool swap, octave::mach_info::float_format fmt)
Definition: ov-bool.cc:218
octave_value resize(const dim_vector &dv, bool fill=false) const
Definition: ov-bool.cc:158
bool save_hdf5(octave_hdf5_id loc_id, const char *name, bool save_as_floats)
Definition: ov-bool.cc:229
bool load_hdf5(octave_hdf5_id loc_id, const char *name)
Definition: ov-bool.cc:274
bool save_ascii(std::ostream &os)
Definition: ov-bool.cc:187
bool save_binary(std::ostream &os, bool save_as_floats)
Definition: ov-bool.cc:209
octave_value do_index_op(const octave_value_list &idx, bool resize_ok=false)
Definition: ov-bool.cc:80
mxArray * as_mxArray(bool interleaved) const
Definition: ov-bool.cc:316
octave_value as_int32() const
Definition: ov-bool.cc:122
static int static_type_id()
Definition: ov-scalar.h:291
octave_value index_op(const octave_value_list &idx, bool resize_ok=false)
Definition: ov.h:504
const octave_hdf5_id octave_H5P_DEFAULT
const octave_hdf5_id octave_H5S_ALL
void() error(const char *fmt,...)
Definition: error.cc:988
F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
float_format
Definition: mach-info.h:38
void mxArray
Definition: mex.h:58
@ mxLOGICAL_CLASS
Definition: mxtypes.h:61
@ mxREAL
Definition: mxtypes.h:80
unsigned char mxLogical
Definition: mxtypes.h:89
int64_t octave_hdf5_id
octave_int< uint32_t > octave_uint32
octave_int< int32_t > octave_int32
octave_int< int16_t > octave_int16
octave_int< int8_t > octave_int8
octave_int< int64_t > octave_int64
octave_int< uint64_t > octave_uint64
octave_int< uint16_t > octave_uint16
octave_int< uint8_t > octave_uint8
#define DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA(t, n, c)
Definition: ov-base.h:235