GNU Octave 10.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
ov-bool.cc
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 1996-2025 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-re-mat.h"
46#include "ov-scalar.h"
47#include "pr-output.h"
48
49#include "ls-oct-text.h"
50#include "ls-hdf5.h"
51
53
54static octave_base_value *
55default_numeric_conversion_function (const octave_base_value& a)
56{
57 const octave_bool& v = dynamic_cast<const octave_bool&> (a);
58
59 return new octave_scalar (v.bool_value ());
60}
61
64{
65 return octave_base_value::type_conv_info (default_numeric_conversion_function,
67
68}
69
71octave_bool::do_index_op (const octave_value_list& idx, bool resize_ok)
72{
73 // FIXME: this doesn't solve the problem of
74 //
75 // a = 1; a([1,1], [1,1], [1,1])
76 //
77 // and similar constructions. Hmm...
78
79 // FIXME: using this constructor avoids narrowing the
80 // 1x1 matrix back to a scalar value. Need a better solution
81 // to this problem.
82
84
85 return tmp.index_op (idx, resize_ok);
86}
87
90{
91 return static_cast<double> (scalar);
92}
93
96{
97 return static_cast<float> (scalar);
98}
99
102{
103 return octave_int8 (scalar);
104}
105
108{
109 return octave_int16 (scalar);
110}
111
114{
115 return octave_int32 (scalar);
116}
117
120{
121 return octave_int64 (scalar);
122}
123
126{
127 return octave_uint8 (scalar);
128}
129
132{
133 return octave_uint16 (scalar);
134}
135
138{
139 return octave_uint32 (scalar);
140}
141
144{
145 return octave_uint64 (scalar);
146}
147
149octave_bool::resize (const dim_vector& dv, bool fill) const
150{
151 if (fill)
152 {
153 boolNDArray retval (dv, false);
154 if (dv.numel ())
155 retval(0) = scalar;
156 return retval;
157 }
158 else
159 {
160 boolNDArray retval (dv);
161 if (dv.numel ())
162 retval(0) = scalar;
163 return retval;
164 }
165}
166
168octave_bool::convert_to_str_internal (bool, bool, char type) const
169{
170 char s[2];
171 s[0] = static_cast<char> (scalar);
172 s[1] = '\0';
173
174 return octave_value (s, type);
175}
176
177bool
178octave_bool::save_ascii (std::ostream& os)
179{
180 double d = double_value ();
181
182 octave::write_value<double> (os, d);
183 os << "\n";
184
185 return true;
186}
187
188bool
189octave_bool::load_ascii (std::istream& is)
190{
191 scalar = (octave::read_value<double> (is) != 0.0);
192
193 if (! is)
194 error ("load: failed to load scalar constant");
195
196 return true;
197}
198
199bool
200octave_bool::save_binary (std::ostream& os, bool /* save_as_floats */)
201{
202 char tmp = (scalar ? 1 : 0);
203 os.write (reinterpret_cast<char *> (&tmp), 1);
204
205 return true;
206}
207
208bool
209octave_bool::load_binary (std::istream& is, bool /* swap */,
210 octave::mach_info::float_format /* fmt */)
211{
212 char tmp;
213 if (! is.read (reinterpret_cast<char *> (&tmp), 1))
214 return false;
215 scalar = (tmp ? 1 : 0);
216 return true;
217}
218
219bool
220octave_bool::save_hdf5 (octave_hdf5_id loc_id, const char *name,
221 bool /* save_as_floats */)
222{
223 bool retval = false;
224
225#if defined (HAVE_HDF5)
226
227 hsize_t dimens[3] = {0};
228 hid_t space_hid, data_hid;
229 space_hid = data_hid = -1;
230
231 space_hid = H5Screate_simple (0, dimens, nullptr);
232 if (space_hid < 0) return false;
233#if defined (HAVE_HDF5_18)
234 data_hid = H5Dcreate (loc_id, name, H5T_NATIVE_DOUBLE, space_hid,
237#else
238 data_hid = H5Dcreate (loc_id, name, H5T_NATIVE_DOUBLE, space_hid,
240#endif
241 if (data_hid < 0)
242 {
243 H5Sclose (space_hid);
244 return false;
245 }
246
247 double tmp = double_value ();
248 retval = H5Dwrite (data_hid, H5T_NATIVE_DOUBLE, octave_H5S_ALL,
250
251 H5Dclose (data_hid);
252 H5Sclose (space_hid);
253
254#else
255 octave_unused_parameter (loc_id);
256 octave_unused_parameter (name);
257
258 warn_save ("hdf5");
259#endif
260
261 return retval;
262}
263
264bool
265octave_bool::load_hdf5 (octave_hdf5_id loc_id, const char *name)
266{
267#if defined (HAVE_HDF5)
268
269#if defined (HAVE_HDF5_18)
270 hid_t data_hid = H5Dopen (loc_id, name, octave_H5P_DEFAULT);
271#else
272 hid_t data_hid = H5Dopen (loc_id, name);
273#endif
274 hid_t space_id = H5Dget_space (data_hid);
275
276 hsize_t rank = H5Sget_simple_extent_ndims (space_id);
277
278 if (rank != 0)
279 {
280 H5Dclose (data_hid);
281 return false;
282 }
283
284 double dtmp;
285 if (H5Dread (data_hid, H5T_NATIVE_DOUBLE, octave_H5S_ALL, octave_H5S_ALL,
286 octave_H5P_DEFAULT, &dtmp) < 0)
287 {
288 H5Dclose (data_hid);
289 return false;
290 }
291
292 scalar = (dtmp != 0.0);
293
294 H5Dclose (data_hid);
295
296#else
297 octave_unused_parameter (loc_id);
298 octave_unused_parameter (name);
299
300 warn_load ("hdf5");
301#endif
302
303 return true;
304}
305
306mxArray *
307octave_bool::as_mxArray (bool interleaved) const
308{
309 mxArray *retval = new mxArray (interleaved, mxLOGICAL_CLASS, 1, 1, mxREAL);
310
311 mxLogical *pd = static_cast<mxLogical *> (retval->get_data ());
312
313 pd[0] = scalar;
314
315 return retval;
316}
Vector representing the dimensions (size) of an Array.
Definition dim-vector.h:90
octave_idx_type numel(int n=0) const
Number of elements that a matrix with this dimensions would have.
Definition dim-vector.h:331
void * get_data() const
Definition mxarray.h:482
void warn_load(const char *type) const
Definition ov-base.cc:1152
friend class octave_value
Definition ov-base.h:278
void warn_save(const char *type) const
Definition ov-base.cc:1161
type_conv_info numeric_conversion_function() const
Definition ov-bool.cc:63
octave_value as_int8() const
Definition ov-bool.cc:101
octave_value as_int64() const
Definition ov-bool.cc:119
octave_value as_uint64() const
Definition ov-bool.cc:143
octave_value convert_to_str_internal(bool pad, bool force, char type) const
Definition ov-bool.cc:168
octave_value as_int16() const
Definition ov-bool.cc:107
octave_value as_single() const
Definition ov-bool.cc:95
octave_value as_uint16() const
Definition ov-bool.cc:131
boolMatrix bool_matrix_value(bool=false) const
Definition ov-bool.h:206
octave_value as_uint8() const
Definition ov-bool.cc:125
octave_value as_uint32() const
Definition ov-bool.cc:137
double double_value(bool=false) const
Definition ov-bool.h:150
octave_value as_double() const
Definition ov-bool.cc:89
bool load_ascii(std::istream &is)
Definition ov-bool.cc:189
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:209
octave_value resize(const dim_vector &dv, bool fill=false) const
Definition ov-bool.cc:149
bool save_hdf5(octave_hdf5_id loc_id, const char *name, bool save_as_floats)
Definition ov-bool.cc:220
bool load_hdf5(octave_hdf5_id loc_id, const char *name)
Definition ov-bool.cc:265
bool save_ascii(std::ostream &os)
Definition ov-bool.cc:178
bool save_binary(std::ostream &os, bool save_as_floats)
Definition ov-bool.cc:200
octave_value do_index_op(const octave_value_list &idx, bool resize_ok=false)
Definition ov-bool.cc:71
mxArray * as_mxArray(bool interleaved) const
Definition ov-bool.cc:307
octave_value as_int32() const
Definition ov-bool.cc:113
static int static_type_id()
Definition ov-scalar.h:290
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:1003
F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
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:246