GNU Octave  6.2.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
ov-float.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1996-2021 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.h"
34 
35 #include "data-conv.h"
36 #include "mach-info.h"
37 #include "lo-specfun.h"
38 #include "lo-mappers.h"
39 
40 #include "defun.h"
41 #include "errwarn.h"
42 #include "mxarray.h"
43 #include "ovl.h"
44 #include "oct-hdf5.h"
45 #include "oct-stream.h"
46 #include "ov-scalar.h"
47 #include "ov-float.h"
48 #include "ov-base.h"
49 #include "ov-base-scalar.h"
50 #include "ov-base-scalar.cc"
51 #include "ov-flt-re-mat.h"
52 #include "ov-typeinfo.h"
53 #include "pr-output.h"
54 #include "xdiv.h"
55 #include "xpow.h"
56 #include "ops.h"
57 
58 #include "ls-oct-text.h"
59 #include "ls-hdf5.h"
60 
61 
62 template class octave_base_scalar<float>;
63 
65  "single");
66 
69 {
70  // FIXME: this doesn't solve the problem of
71  //
72  // a = 1; a([1,1], [1,1], [1,1])
73  //
74  // and similar constructions. Hmm...
75 
76  // FIXME: using this constructor avoids narrowing the
77  // 1x1 matrix back to a scalar value. Need a better solution
78  // to this problem.
79 
81 
82  return tmp.do_index_op (idx, resize_ok);
83 }
84 
86 octave_float_scalar::resize (const dim_vector& dv, bool fill) const
87 {
88  if (fill)
89  {
90  FloatNDArray retval (dv, 0);
91 
92  if (dv.numel ())
93  retval(0) = scalar;
94 
95  return retval;
96  }
97  else
98  {
99  FloatNDArray retval (dv);
100 
101  if (dv.numel ())
102  retval(0) = scalar;
103 
104  return retval;
105  }
106 }
107 
110 {
111  return static_cast<double> (scalar);
112 }
113 
116 {
117  return scalar;
118 }
119 
122 {
123  return octave_int8 (scalar);
124 }
125 
128 {
129  return octave_int16 (scalar);
130 }
131 
134 {
135  return octave_int32 (scalar);
136 }
137 
140 {
141  return octave_int64 (scalar);
142 }
143 
146 {
147  return octave_uint8 (scalar);
148 }
149 
152 {
153  return octave_uint16 (scalar);
154 }
155 
158 {
159  return octave_uint32 (scalar);
160 }
161 
164 {
165  return octave_uint64 (scalar);
166 }
167 
170 {
171  return FloatDiagMatrix (Array<float> (dim_vector (1, 1), scalar), m, n);
172 }
173 
176 {
178 
181 
182  int ival = octave::math::nint (scalar);
183 
184  if (ival < 0 || ival > std::numeric_limits<unsigned char>::max ())
185  {
186  // FIXME: is there something better we could do?
187 
188  ival = 0;
189 
190  ::warning ("range error for conversion to character value");
191  }
192 
193  retval = octave_value (std::string (1, static_cast<char> (ival)), type);
194 
195  return retval;
196 }
197 
198 bool
200 {
201  float d = float_value ();
202 
203  octave_write_float (os, d);
204 
205  os << "\n";
206 
207  return true;
208 }
209 
210 bool
212 {
213  scalar = octave_read_value<float> (is);
214  if (! is)
215  error ("load: failed to load scalar constant");
216 
217  return true;
218 }
219 
220 bool
221 octave_float_scalar::save_binary (std::ostream& os, bool /* save_as_floats */)
222 {
223  char tmp = LS_FLOAT;
224  os.write (reinterpret_cast<char *> (&tmp), 1);
225  float dtmp = float_value ();
226  os.write (reinterpret_cast<char *> (&dtmp), 4);
227 
228  return true;
229 }
230 
231 bool
232 octave_float_scalar::load_binary (std::istream& is, bool swap,
234 {
235  char tmp;
236  if (! is.read (reinterpret_cast<char *> (&tmp), 1))
237  return false;
238 
239  float dtmp;
240  read_floats (is, &dtmp, static_cast<save_type> (tmp), 1, swap, fmt);
241 
242  if (! is)
243  return false;
244 
245  scalar = dtmp;
246  return true;
247 }
248 
249 bool
251  bool /* save_as_floats */)
252 {
253  bool retval = false;
254 
255 #if defined (HAVE_HDF5)
256 
257  hsize_t dimens[3];
258  hid_t space_hid, data_hid;
259  space_hid = data_hid = -1;
260 
261  space_hid = H5Screate_simple (0, dimens, nullptr);
262  if (space_hid < 0) return false;
263 #if defined (HAVE_HDF5_18)
264  data_hid = H5Dcreate (loc_id, name, H5T_NATIVE_FLOAT, space_hid,
266 #else
267  data_hid = H5Dcreate (loc_id, name, H5T_NATIVE_FLOAT, space_hid,
269 #endif
270  if (data_hid < 0)
271  {
272  H5Sclose (space_hid);
273  return false;
274  }
275 
276  float tmp = float_value ();
277  retval = H5Dwrite (data_hid, H5T_NATIVE_FLOAT, octave_H5S_ALL, octave_H5S_ALL,
278  octave_H5P_DEFAULT, &tmp) >= 0;
279 
280  H5Dclose (data_hid);
281  H5Sclose (space_hid);
282 
283 #else
284  octave_unused_parameter (loc_id);
285  octave_unused_parameter (name);
286 
287  warn_save ("hdf5");
288 #endif
289 
290  return retval;
291 }
292 
293 bool
295 {
296 #if defined (HAVE_HDF5)
297 
298 #if defined (HAVE_HDF5_18)
299  hid_t data_hid = H5Dopen (loc_id, name, octave_H5P_DEFAULT);
300 #else
301  hid_t data_hid = H5Dopen (loc_id, name);
302 #endif
303  hid_t space_id = H5Dget_space (data_hid);
304 
305  hsize_t rank = H5Sget_simple_extent_ndims (space_id);
306 
307  if (rank != 0)
308  {
309  H5Dclose (data_hid);
310  return false;
311  }
312 
313  float dtmp;
314  if (H5Dread (data_hid, H5T_NATIVE_FLOAT, octave_H5S_ALL, octave_H5S_ALL,
315  octave_H5P_DEFAULT, &dtmp) < 0)
316  {
317  H5Dclose (data_hid);
318  return false;
319  }
320 
321  scalar = dtmp;
322 
323  H5Dclose (data_hid);
324 
325  return true;
326 
327 #else
328  octave_unused_parameter (loc_id);
329  octave_unused_parameter (name);
330 
331  warn_load ("hdf5");
332 
333  return false;
334 #endif
335 }
336 
337 mxArray *
339 {
340  mxArray *retval = new mxArray (mxSINGLE_CLASS, 1, 1, mxREAL);
341 
342  float *pr = static_cast<float *> (retval->get_data ());
343 
344  pr[0] = scalar;
345 
346  return retval;
347 }
348 
351 {
352  switch (umap)
353  {
354  case umap_imag:
355  return 0.0f;
356 
357  case umap_real:
358  case umap_conj:
359  return scalar;
360 
361 #define SCALAR_MAPPER(UMAP, FCN) \
362  case umap_ ## UMAP: \
363  return octave_value (FCN (scalar))
364 
365  SCALAR_MAPPER (abs, ::fabsf);
368  SCALAR_MAPPER (angle, std::arg);
369  SCALAR_MAPPER (arg, std::arg);
372  SCALAR_MAPPER (atan, ::atanf);
384  SCALAR_MAPPER (ceil, ::ceilf);
385  SCALAR_MAPPER (cos, ::cosf);
386  SCALAR_MAPPER (cosh, ::coshf);
387  SCALAR_MAPPER (exp, ::expf);
398  SCALAR_MAPPER (sin, ::sinf);
399  SCALAR_MAPPER (sinh, ::sinhf);
401  SCALAR_MAPPER (tan, ::tanf);
402  SCALAR_MAPPER (tanh, ::tanhf);
408 
409  // Special cases for Matlab compatibility.
410  case umap_xtolower:
411  case umap_xtoupper:
412  return scalar;
413 
414  case umap_xisalnum:
415  case umap_xisalpha:
416  case umap_xisascii:
417  case umap_xiscntrl:
418  case umap_xisdigit:
419  case umap_xisgraph:
420  case umap_xislower:
421  case umap_xisprint:
422  case umap_xispunct:
423  case umap_xisspace:
424  case umap_xisupper:
425  case umap_xisxdigit:
426  {
427  octave_value str_conv = convert_to_str (true, true);
428  return str_conv.map (umap);
429  }
430 
431  default:
432  return octave_base_value::map (umap);
433  }
434 }
435 
436 bool
438  builtin_type_t btyp) const
439 {
440 
441  // Support inline real->complex conversion.
442  if (btyp == btyp_float)
443  {
444  *(reinterpret_cast<float *>(where)) = scalar;
445  return true;
446  }
447  else if (btyp == btyp_float_complex)
448  {
449  *(reinterpret_cast<FloatComplex *>(where)) = scalar;
450  return true;
451  }
452  else
453  return false;
454 }
charNDArray max(char d, const charNDArray &m)
Definition: chNDArray.cc:230
N Dimensional Array with copy-on-write semantics.
Definition: Array.h:128
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:95
octave_idx_type numel(int n=0) const
Number of elements that a matrix with this dimensions would have.
Definition: dim-vector.h:401
virtual octave_value map(unary_mapper_t) const
Definition: ov-base.cc:1114
virtual octave_value convert_to_str(bool pad=false, bool force=false, char type='\'') const
Definition: ov-base.cc:377
void warn_load(const char *type) const
Definition: ov-base.cc:1090
friend class octave_value
Definition: ov-base.h:228
void warn_save(const char *type) const
Definition: ov-base.cc:1099
bool save_ascii(std::ostream &os)
Definition: ov-float.cc:199
octave_value as_int64(void) const
Definition: ov-float.cc:139
bool load_hdf5(octave_hdf5_id loc_id, const char *name)
Definition: ov-float.cc:294
octave_value as_single(void) const
Definition: ov-float.cc:115
bool fast_elem_insert_self(void *where, builtin_type_t btyp) const
Definition: ov-float.cc:437
octave_value as_uint64(void) const
Definition: ov-float.cc:163
mxArray * as_mxArray(void) const
Definition: ov-float.cc:338
octave_value convert_to_str_internal(bool pad, bool force, char type) const
Definition: ov-float.cc:175
octave_value diag(octave_idx_type m, octave_idx_type n) const
Definition: ov-float.cc:169
octave_value as_int32(void) const
Definition: ov-float.cc:133
octave_value as_double(void) const
Definition: ov-float.cc:109
float float_value(bool=false) const
Definition: ov-float.h:150
octave_value resize(const dim_vector &dv, bool fill=false) const
Definition: ov-float.cc:86
octave_value map(unary_mapper_t umap) const
Definition: ov-float.cc:350
bool load_binary(std::istream &is, bool swap, octave::mach_info::float_format fmt)
Definition: ov-float.cc:232
octave_value as_uint32(void) const
Definition: ov-float.cc:157
FloatMatrix float_matrix_value(bool=false) const
Definition: ov-float.h:160
bool save_binary(std::ostream &os, bool save_as_floats)
Definition: ov-float.cc:221
octave_value do_index_op(const octave_value_list &idx, bool resize_ok=false)
Definition: ov-float.cc:68
bool load_ascii(std::istream &is)
Definition: ov-float.cc:211
octave_value as_uint16(void) const
Definition: ov-float.cc:151
octave_value as_int16(void) const
Definition: ov-float.cc:127
octave_value as_int8(void) const
Definition: ov-float.cc:121
bool save_hdf5(octave_hdf5_id loc_id, const char *name, bool save_as_floats)
Definition: ov-float.cc:250
octave_value as_uint8(void) const
Definition: ov-float.cc:145
octave_value do_index_op(const octave_value_list &idx, bool resize_ok=false)
Definition: ov.h:475
octave_value map(octave_base_value::unary_mapper_t umap) const
Definition: ov.h:1436
const octave_hdf5_id octave_H5P_DEFAULT
const octave_hdf5_id octave_H5S_ALL
void read_floats(std::istream &is, float *data, save_type type, octave_idx_type len, bool swap, octave::mach_info::float_format fmt)
Definition: data-conv.cc:835
save_type
Definition: data-conv.h:87
@ LS_FLOAT
Definition: data-conv.h:94
void warning(const char *fmt,...)
Definition: error.cc:1050
void error(const char *fmt,...)
Definition: error.cc:968
function gamma(X)
Definition: gamma.f:3
QString name
F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
void octave_write_float(std::ostream &os, float d)
Definition: lo-utils.cc:410
void mxArray
Definition: mex.h:58
T octave_idx_type m
Definition: mx-inlines.cc:773
octave_idx_type n
Definition: mx-inlines.cc:753
@ mxSINGLE_CLASS
Definition: mxarray.in.h:65
@ mxREAL
Definition: mxarray.in.h:80
std::complex< double > erfc(std::complex< double > z, double relerr=0)
std::complex< double > erfcx(std::complex< double > z, double relerr=0)
std::complex< double > erfi(std::complex< double > z, double relerr=0)
std::complex< double > erf(std::complex< double > z, double relerr=0)
double fix(double x)
Definition: lo-mappers.h:118
Complex atan(const Complex &x)
Definition: lo-mappers.h:71
double signum(double x)
Definition: lo-mappers.h:222
double asinh(double x)
Definition: lo-specfun.h:69
double gamma(double x)
Definition: lo-specfun.cc:1912
bool isna(double x)
Definition: lo-mappers.cc:47
Complex rc_acos(double x)
Definition: lo-mappers.cc:230
double signbit(double x)
Definition: lo-mappers.h:54
double atanh(double x)
Definition: lo-specfun.h:74
bool isfinite(double x)
Definition: lo-mappers.h:192
int nint(double x)
Definition: lo-mappers.cc:208
double roundb(double x)
Definition: lo-mappers.h:147
bool isnan(bool)
Definition: lo-mappers.h:178
Complex log1p(const Complex &x)
Definition: lo-specfun.cc:1958
bool isinf(double x)
Definition: lo-mappers.h:203
Complex acos(const Complex &x)
Definition: lo-mappers.cc:85
double round(double x)
Definition: lo-mappers.h:136
Complex asin(const Complex &x)
Definition: lo-mappers.cc:107
double erfcx(double x)
Definition: lo-specfun.cc:1755
Complex rc_log2(double x)
Definition: lo-mappers.cc:294
Complex rc_log(double x)
Definition: lo-mappers.cc:281
Complex erfc(const Complex &x)
Definition: lo-specfun.cc:1652
double acosh(double x)
Definition: lo-specfun.h:51
Complex rc_sqrt(double x)
Definition: lo-mappers.cc:324
double erfcinv(double x)
Definition: lo-specfun.cc:1744
Complex rc_atanh(double x)
Definition: lo-mappers.cc:268
std::complex< T > ceil(const std::complex< T > &x)
Definition: lo-mappers.h:103
Complex rc_acosh(double x)
Definition: lo-mappers.cc:243
double erfi(double x)
Definition: lo-specfun.cc:1774
Complex rc_log1p(double x)
Definition: lo-specfun.cc:2247
double lgamma(double x)
Definition: lo-specfun.h:347
double erfinv(double x)
Definition: lo-specfun.cc:1863
Complex rc_log10(double x)
Definition: lo-mappers.cc:309
Complex rc_lgamma(double x)
Definition: lo-specfun.cc:2210
double dawson(double x)
Definition: lo-specfun.cc:1517
double cbrt(double x)
Definition: lo-specfun.h:300
Complex rc_asin(double x)
Definition: lo-mappers.cc:255
Complex log2(const Complex &x)
Definition: lo-mappers.cc:139
std::complex< T > floor(const std::complex< T > &x)
Definition: lo-mappers.h:130
Complex erf(const Complex &x)
Definition: lo-specfun.cc:1637
Complex expm1(const Complex &x)
Definition: lo-specfun.cc:1874
void err_nan_to_character_conversion(void)
std::complex< float > FloatComplex
Definition: oct-cmplx.h:34
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:180
builtin_type_t
Definition: ov-base.h:72
@ btyp_float_complex
Definition: ov-base.h:76
@ btyp_float
Definition: ov-base.h:74
#define SCALAR_MAPPER(UMAP, FCN)
octave_value::octave_value(const Array< char > &chm, char type) return retval
Definition: ov.cc:811
static T abs(T x)
Definition: pr-output.cc:1678