GNU Octave 7.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-2022 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
57extern template class octave_base_scalar<double>;
58
59template class octave_base_scalar<bool>;
60
62
63static octave_base_value *
65{
66 const octave_bool& v = dynamic_cast<const octave_bool&> (a);
67
68 return new octave_scalar (v.bool_value ());
69}
70
73{
76
77}
78
80octave_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
158octave_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
177octave_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
186bool
187octave_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
197bool
198octave_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
208bool
209octave_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
217bool
218octave_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
228bool
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,
245#else
246 data_hid = H5Dcreate (loc_id, name, H5T_NATIVE_DOUBLE, space_hid,
248#endif
249 if (data_hid < 0)
250 {
251 H5Sclose (space_hid);
252 return false;
253 }
254
255 double tmp = double_value ();
256 retval = H5Dwrite (data_hid, H5T_NATIVE_DOUBLE, octave_H5S_ALL, octave_H5S_ALL,
257 octave_H5P_DEFAULT, &tmp) >= 0;
258
259 H5Dclose (data_hid);
260 H5Sclose (space_hid);
261
262#else
263 octave_unused_parameter (loc_id);
264 octave_unused_parameter (name);
265
266 warn_save ("hdf5");
267#endif
268
269 return retval;
270}
271
272bool
274{
275#if defined (HAVE_HDF5)
276
277#if defined (HAVE_HDF5_18)
278 hid_t data_hid = H5Dopen (loc_id, name, octave_H5P_DEFAULT);
279#else
280 hid_t data_hid = H5Dopen (loc_id, name);
281#endif
282 hid_t space_id = H5Dget_space (data_hid);
283
284 hsize_t rank = H5Sget_simple_extent_ndims (space_id);
285
286 if (rank != 0)
287 {
288 H5Dclose (data_hid);
289 return false;
290 }
291
292 double dtmp;
293 if (H5Dread (data_hid, H5T_NATIVE_DOUBLE, octave_H5S_ALL, octave_H5S_ALL,
294 octave_H5P_DEFAULT, &dtmp) < 0)
295 {
296 H5Dclose (data_hid);
297 return false;
298 }
299
300 scalar = (dtmp != 0.0);
301
302 H5Dclose (data_hid);
303
304#else
305 octave_unused_parameter (loc_id);
306 octave_unused_parameter (name);
307
308 warn_load ("hdf5");
309#endif
310
311 return true;
312}
313
314mxArray *
315octave_bool::as_mxArray (bool interleaved) const
316{
317 mxArray *retval = new mxArray (interleaved, mxLOGICAL_CLASS, 1, 1, mxREAL);
318
319 mxLogical *pd = static_cast<mxLogical *> (retval->get_data ());
320
321 pd[0] = scalar;
322
323 return retval;
324}
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(void) const
Definition: mxarray.h:497
OCTINTERP_API void warn_load(const char *type) const
Definition: ov-base.cc:1152
friend class octave_value
Definition: ov-base.h:256
OCTINTERP_API void warn_save(const char *type) const
Definition: ov-base.cc:1161
octave_value as_uint8(void) const
Definition: ov-bool.cc:134
octave_value convert_to_str_internal(bool pad, bool force, char type) const
Definition: ov-bool.cc:177
octave_value as_int32(void) const
Definition: ov-bool.cc:122
octave_value as_single(void) const
Definition: ov-bool.cc:104
boolMatrix bool_matrix_value(bool=false) const
Definition: ov-bool.h:206
octave_value as_uint64(void) const
Definition: ov-bool.cc:152
double double_value(bool=false) const
Definition: ov-bool.h:150
bool load_ascii(std::istream &is)
Definition: ov-bool.cc:198
octave_value as_int8(void) const
Definition: ov-bool.cc:110
octave_value as_double(void) const
Definition: ov-bool.cc:98
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
octave_value as_int16(void) const
Definition: ov-bool.cc:116
bool save_hdf5(octave_hdf5_id loc_id, const char *name, bool save_as_floats)
Definition: ov-bool.cc:229
octave_value as_int64(void) const
Definition: ov-bool.cc:128
bool load_hdf5(octave_hdf5_id loc_id, const char *name)
Definition: ov-bool.cc:273
octave_value as_uint16(void) const
Definition: ov-bool.cc:140
bool save_ascii(std::ostream &os)
Definition: ov-bool.cc:187
type_conv_info numeric_conversion_function(void) const
Definition: ov-bool.cc:72
octave_value as_uint32(void) const
Definition: ov-bool.cc:146
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:315
static int static_type_id(void)
Definition: ov-scalar.h:281
octave_value index_op(const octave_value_list &idx, bool resize_ok=false)
Definition: ov.h:550
const octave_hdf5_id octave_H5P_DEFAULT
const octave_hdf5_id octave_H5S_ALL
void error(const char *fmt,...)
Definition: error.cc:980
QString name
F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
void mxArray
Definition: mex.h:58
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:222
static octave_base_value * default_numeric_conversion_function(const octave_base_value &a)
Definition: ov-bool.cc:64