GNU Octave  4.2.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ov-bool.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1996-2017 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 3 of the License, or (at your
10 option) any 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, see
19 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if defined (HAVE_CONFIG_H)
24 # include "config.h"
25 #endif
26 
27 #include <iostream>
28 
29 #include "oct-inttypes.h"
30 
31 #include "mx-base.h"
32 
33 #include "errwarn.h"
34 #include "mxarray.h"
35 #include "oct-hdf5.h"
36 #include "ovl.h"
37 #include "ops.h"
38 #include "ov-bool.h"
39 #include "ov-bool-mat.h"
40 #include "ov-base.h"
41 #include "ov-base-scalar.h"
42 #include "ov-base-scalar.cc"
43 #include "ov-re-mat.h"
44 #include "ov-scalar.h"
45 #include "pr-output.h"
46 
47 #include "ls-oct-text.h"
48 #include "ls-hdf5.h"
49 
50 // Prevent implicit instantiations on some systems (Windows, others?)
51 // that can lead to duplicate definitions of static data members.
52 
53 extern template class OCTINTERP_API octave_base_scalar<double>;
54 
55 template class octave_base_scalar<bool>;
56 
58 
59 static octave_base_value *
61 {
62  const octave_bool& v = dynamic_cast<const octave_bool&> (a);
63 
64  return new octave_scalar (v.bool_value ());
65 }
66 
69 {
72 
73 }
74 
76 octave_bool::do_index_op (const octave_value_list& idx, bool resize_ok)
77 {
78  // FIXME: this doesn't solve the problem of
79  //
80  // a = 1; a([1,1], [1,1], [1,1])
81  //
82  // and similar constructions. Hmm...
83 
84  // FIXME: using this constructor avoids narrowing the
85  // 1x1 matrix back to a scalar value. Need a better solution
86  // to this problem.
87 
89 
90  return tmp.do_index_op (idx, resize_ok);
91 }
92 
95 {
96  return static_cast<double> (scalar);
97 }
98 
101 {
102  return static_cast<float> (scalar);
103 }
104 
107 {
108  return octave_int8 (scalar);
109 }
110 
113 {
114  return octave_int16 (scalar);
115 }
116 
119 {
120  return octave_int32 (scalar);
121 }
122 
125 {
126  return octave_int64 (scalar);
127 }
128 
131 {
132  return octave_uint8 (scalar);
133 }
134 
137 {
138  return octave_uint16 (scalar);
139 }
140 
143 {
144  return octave_uint32 (scalar);
145 }
146 
149 {
150  return octave_uint64 (scalar);
151 }
152 
154 octave_bool::resize (const dim_vector& dv, bool fill) const
155 {
156  if (fill)
157  {
158  boolNDArray retval (dv, false);
159  if (dv.numel ())
160  retval(0) = scalar;
161  return retval;
162  }
163  else
164  {
165  boolNDArray retval (dv);
166  if (dv.numel ())
167  retval(0) = scalar;
168  return retval;
169  }
170 }
171 
174 {
175  char s[2];
176  s[0] = static_cast<char> (scalar);
177  s[1] = '\0';
178 
179  return octave_value (s, type);
180 }
181 
182 bool
183 octave_bool::save_ascii (std::ostream& os)
184 {
185  double d = double_value ();
186 
187  octave_write_double (os, d);
188  os << "\n";
189 
190  return true;
191 }
192 
193 bool
195 {
196  scalar = (octave_read_value<double> (is) != 0.);
197 
198  if (! is)
199  error ("load: failed to load scalar constant");
200 
201  return true;
202 }
203 
204 bool
205 octave_bool::save_binary (std::ostream& os, bool& /* save_as_floats */)
206 {
207  char tmp = (scalar ? 1 : 0);
208  os.write (reinterpret_cast<char *> (&tmp), 1);
209 
210  return true;
211 }
212 
213 bool
214 octave_bool::load_binary (std::istream& is, bool /* swap */,
216 {
217  char tmp;
218  if (! is.read (reinterpret_cast<char *> (&tmp), 1))
219  return false;
220  scalar = (tmp ? 1 : 0);
221  return true;
222 }
223 
224 bool
226  bool /* save_as_floats */)
227 {
228  bool retval = false;
229 
230 #if defined (HAVE_HDF5)
231 
232  hsize_t dimens[3];
233  hid_t space_hid, data_hid;
234  space_hid = data_hid = -1;
235 
236  space_hid = H5Screate_simple (0, dimens, 0);
237  if (space_hid < 0) return false;
238 #if defined (HAVE_HDF5_18)
239  data_hid = H5Dcreate (loc_id, name, H5T_NATIVE_DOUBLE, space_hid,
241 #else
242  data_hid = H5Dcreate (loc_id, name, H5T_NATIVE_DOUBLE, space_hid,
244 #endif
245  if (data_hid < 0)
246  {
247  H5Sclose (space_hid);
248  return false;
249  }
250 
251  double tmp = double_value ();
252  retval = H5Dwrite (data_hid, H5T_NATIVE_DOUBLE, octave_H5S_ALL, octave_H5S_ALL,
253  octave_H5P_DEFAULT, &tmp) >= 0;
254 
255  H5Dclose (data_hid);
256  H5Sclose (space_hid);
257 
258 #else
259  octave_unused_parameter (loc_id);
260  octave_unused_parameter (name);
261 
262  warn_save ("hdf5");
263 #endif
264 
265  return retval;
266 }
267 
268 bool
270 {
271 #if defined (HAVE_HDF5)
272 
273 #if defined (HAVE_HDF5_18)
274  hid_t data_hid = H5Dopen (loc_id, name, octave_H5P_DEFAULT);
275 #else
276  hid_t data_hid = H5Dopen (loc_id, name);
277 #endif
278  hid_t space_id = H5Dget_space (data_hid);
279 
280  hsize_t rank = H5Sget_simple_extent_ndims (space_id);
281 
282  if (rank != 0)
283  {
284  H5Dclose (data_hid);
285  return false;
286  }
287 
288  double dtmp;
289  if (H5Dread (data_hid, H5T_NATIVE_DOUBLE, octave_H5S_ALL, octave_H5S_ALL,
290  octave_H5P_DEFAULT, &dtmp) < 0)
291  {
292  H5Dclose (data_hid);
293  return false;
294  }
295 
296  scalar = (dtmp != 0.);
297 
298  H5Dclose (data_hid);
299 
300 #else
301  octave_unused_parameter (loc_id);
302  octave_unused_parameter (name);
303 
304  warn_load ("hdf5");
305 #endif
306 
307  return true;
308 }
309 
310 mxArray *
312 {
313  mxArray *retval = new mxArray (mxLOGICAL_CLASS, 1, 1, mxREAL);
314 
315  bool *pr = static_cast<bool *> (retval->get_data ());
316 
317  pr[0] = scalar;
318 
319  return retval;
320 }
void octave_write_double(std::ostream &os, double d)
Definition: lo-utils.cc:388
type_conv_info numeric_conversion_function(void) const
Definition: ov-bool.cc:68
octave_int< uint64_t > octave_uint64
octave_value as_uint64(void) const
Definition: ov-bool.cc:148
octave_value as_uint16(void) const
Definition: ov-bool.cc:136
bool load_ascii(std::istream &is)
Definition: ov-bool.cc:194
const octave_hdf5_id octave_H5S_ALL
octave_value as_single(void) const
Definition: ov-bool.cc:100
octave_int< uint16_t > octave_uint16
void error(const char *fmt,...)
Definition: error.cc:570
double double_value(bool=false) const
Definition: ov-bool.h:148
void * get_data(void) const
Definition: mxarray.h:449
#define DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA(t, n, c)
Definition: ov-base.h:169
octave_value do_index_op(const octave_value_list &idx, bool resize_ok=false)
Definition: ov-bool.cc:76
octave_value as_double(void) const
Definition: ov-bool.cc:94
octave_value as_int8(void) const
Definition: ov-bool.cc:106
s
Definition: file-io.cc:2682
octave_idx_type numel(int n=0) const
Number of elements that a matrix with this dimensions would have.
Definition: dim-vector.h:389
bool load_hdf5(octave_hdf5_id loc_id, const char *name)
Definition: ov-bool.cc:269
F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T const F77_REAL const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T F77_REAL F77_REAL &F77_RET_T F77_DBLE F77_DBLE &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
octave_value as_uint32(void) const
Definition: ov-bool.cc:142
bool save_binary(std::ostream &os, bool &save_as_floats)
Definition: ov-bool.cc:205
calling an anonymous function involves an overhead quite comparable to the overhead of an m file function Passing a handle to a built in function is because the interpreter is not involved in the internal loop For a
Definition: cellfun.cc:398
void warn_load(const char *type) const
Definition: ov-base.cc:1151
octave_value as_int64(void) const
Definition: ov-bool.cc:124
OCTAVE_EXPORT octave_value_list any number nd example oindent prints the prompt xample Pick a any number!nd example oindent and waits for the user to enter a value The string entered by the user is evaluated as an so it may be a literal a variable name
Definition: input.cc:871
bool bool_value(bool=false) const
Definition: ov-bool.h:202
mxArray * as_mxArray(void) const
Definition: ov-bool.cc:311
octave_value as_int32(void) const
Definition: ov-bool.cc:118
#define OCTINTERP_API
Definition: mexproto.h:69
octave_value as_int16(void) const
Definition: ov-bool.cc:112
double tmp
Definition: data.cc:6300
octave_value retval
Definition: data.cc:6294
int64_t octave_hdf5_id
octave_value convert_to_str_internal(bool pad, bool force, char type) const
Definition: ov-bool.cc:173
idx type
Definition: ov.cc:3129
bool load_binary(std::istream &is, bool swap, octave::mach_info::float_format fmt)
Definition: ov-bool.cc:214
static octave_base_value * default_numeric_conversion_function(const octave_base_value &a)
Definition: ov-bool.cc:60
void warn_save(const char *type) const
Definition: ov-base.cc:1160
octave_int< uint32_t > octave_uint32
void mxArray
Definition: mex.h:55
friend class octave_value
Definition: ov-base.h:211
boolMatrix bool_matrix_value(bool=false) const
Definition: ov-bool.h:204
octave_int< int64_t > octave_int64
bool save_hdf5(octave_hdf5_id loc_id, const char *name, bool save_as_floats)
Definition: ov-bool.cc:225
bool save_ascii(std::ostream &os)
Definition: ov-bool.cc:183
const octave_hdf5_id octave_H5P_DEFAULT
octave_int< int16_t > octave_int16
Definition: mxarray.h:76
write the output to stdout if nargout is
Definition: load-save.cc:1576
octave_int< uint8_t > octave_uint8
static int static_type_id(void)
Definition: ov-scalar.h:270
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:87
octave_int< int32_t > octave_int32
dim_vector dv
Definition: sub2ind.cc:263
octave_value as_uint8(void) const
Definition: ov-bool.cc:130
octave_int< int8_t > octave_int8
octave_value resize(const dim_vector &dv, bool fill=false) const
Definition: ov-bool.cc:154
octave_value do_index_op(const octave_value_list &idx, bool resize_ok=false)
Definition: ov.h:454