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-float.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 "data-conv.h"
32 #include "mach-info.h"
33 #include "lo-specfun.h"
34 #include "lo-mappers.h"
35 
36 #include "defun.h"
37 #include "errwarn.h"
38 #include "mxarray.h"
39 #include "ovl.h"
40 #include "oct-hdf5.h"
41 #include "oct-stream.h"
42 #include "ov-scalar.h"
43 #include "ov-float.h"
44 #include "ov-base.h"
45 #include "ov-base-scalar.h"
46 #include "ov-base-scalar.cc"
47 #include "ov-flt-re-mat.h"
48 #include "ov-typeinfo.h"
49 #include "pr-output.h"
50 #include "xdiv.h"
51 #include "xpow.h"
52 #include "ops.h"
53 
54 #include "ls-oct-text.h"
55 #include "ls-hdf5.h"
56 
57 
58 template class octave_base_scalar<float>;
59 
61  "single");
62 
65 {
66  // FIXME: this doesn't solve the problem of
67  //
68  // a = 1; a([1,1], [1,1], [1,1])
69  //
70  // and similar constructions. Hmm...
71 
72  // FIXME: using this constructor avoids narrowing the
73  // 1x1 matrix back to a scalar value. Need a better solution
74  // to this problem.
75 
77 
78  return tmp.do_index_op (idx, resize_ok);
79 }
80 
82 octave_float_scalar::resize (const dim_vector& dv, bool fill) const
83 {
84  if (fill)
85  {
86  FloatNDArray retval (dv, 0);
87 
88  if (dv.numel ())
89  retval(0) = scalar;
90 
91  return retval;
92  }
93  else
94  {
95  FloatNDArray retval (dv);
96 
97  if (dv.numel ())
98  retval(0) = scalar;
99 
100  return retval;
101  }
102 }
103 
106 {
107  return static_cast<double> (scalar);
108 }
109 
112 {
113  return scalar;
114 }
115 
118 {
119  return octave_int8 (scalar);
120 }
121 
124 {
125  return octave_int16 (scalar);
126 }
127 
130 {
131  return octave_int32 (scalar);
132 }
133 
136 {
137  return octave_int64 (scalar);
138 }
139 
142 {
143  return octave_uint8 (scalar);
144 }
145 
148 {
149  return octave_uint16 (scalar);
150 }
151 
154 {
155  return octave_uint32 (scalar);
156 }
157 
160 {
161  return octave_uint64 (scalar);
162 }
163 
166 {
167  return FloatDiagMatrix (Array<float> (dim_vector (1, 1), scalar), m, n);
168 }
169 
172 {
174 
177 
178  int ival = octave::math::nint (scalar);
179 
180  if (ival < 0 || ival > std::numeric_limits<unsigned char>::max ())
181  {
182  // FIXME: is there something better we could do?
183 
184  ival = 0;
185 
186  ::warning ("range error for conversion to character value");
187  }
188 
189  retval = octave_value (std::string (1, static_cast<char> (ival)), type);
190 
191  return retval;
192 }
193 
194 bool
196 {
197  float d = float_value ();
198 
199  octave_write_float (os, d);
200 
201  os << "\n";
202 
203  return true;
204 }
205 
206 bool
208 {
209  scalar = octave_read_value<float> (is);
210  if (! is)
211  error ("load: failed to load scalar constant");
212 
213  return true;
214 }
215 
216 bool
217 octave_float_scalar::save_binary (std::ostream& os, bool& /* save_as_floats */)
218 {
219  char tmp = LS_FLOAT;
220  os.write (reinterpret_cast<char *> (&tmp), 1);
221  float dtmp = float_value ();
222  os.write (reinterpret_cast<char *> (&dtmp), 4);
223 
224  return true;
225 }
226 
227 bool
230 {
231  char tmp;
232  if (! is.read (reinterpret_cast<char *> (&tmp), 1))
233  return false;
234 
235  float dtmp;
236  read_floats (is, &dtmp, static_cast<save_type> (tmp), 1, swap, fmt);
237 
238  if (! is)
239  return false;
240 
241  scalar = dtmp;
242  return true;
243 }
244 
245 bool
247  bool /* save_as_floats */)
248 {
249  bool retval = false;
250 
251 #if defined (HAVE_HDF5)
252 
253  hsize_t dimens[3];
254  hid_t space_hid, data_hid;
255  space_hid = data_hid = -1;
256 
257  space_hid = H5Screate_simple (0, dimens, 0);
258  if (space_hid < 0) return false;
259 #if defined (HAVE_HDF5_18)
260  data_hid = H5Dcreate (loc_id, name, H5T_NATIVE_FLOAT, space_hid,
262 #else
263  data_hid = H5Dcreate (loc_id, name, H5T_NATIVE_FLOAT, space_hid,
265 #endif
266  if (data_hid < 0)
267  {
268  H5Sclose (space_hid);
269  return false;
270  }
271 
272  float tmp = float_value ();
273  retval = H5Dwrite (data_hid, H5T_NATIVE_FLOAT, octave_H5S_ALL, octave_H5S_ALL,
274  octave_H5P_DEFAULT, &tmp) >= 0;
275 
276  H5Dclose (data_hid);
277  H5Sclose (space_hid);
278 
279 #else
280  octave_unused_parameter (loc_id);
281  octave_unused_parameter (name);
282 
283  warn_save ("hdf5");
284 #endif
285 
286  return retval;
287 }
288 
289 bool
291 {
292 #if defined (HAVE_HDF5)
293 
294 #if defined (HAVE_HDF5_18)
295  hid_t data_hid = H5Dopen (loc_id, name, octave_H5P_DEFAULT);
296 #else
297  hid_t data_hid = H5Dopen (loc_id, name);
298 #endif
299  hid_t space_id = H5Dget_space (data_hid);
300 
301  hsize_t rank = H5Sget_simple_extent_ndims (space_id);
302 
303  if (rank != 0)
304  {
305  H5Dclose (data_hid);
306  return false;
307  }
308 
309  float dtmp;
310  if (H5Dread (data_hid, H5T_NATIVE_FLOAT, octave_H5S_ALL, octave_H5S_ALL,
311  octave_H5P_DEFAULT, &dtmp) < 0)
312  {
313  H5Dclose (data_hid);
314  return false;
315  }
316 
317  scalar = dtmp;
318 
319  H5Dclose (data_hid);
320 
321  return true;
322 
323 #else
324  octave_unused_parameter (loc_id);
325  octave_unused_parameter (name);
326 
327  warn_load ("hdf5");
328 
329  return false;
330 #endif
331 }
332 
333 mxArray *
335 {
336  mxArray *retval = new mxArray (mxSINGLE_CLASS, 1, 1, mxREAL);
337 
338  float *pr = static_cast<float *> (retval->get_data ());
339 
340  pr[0] = scalar;
341 
342  return retval;
343 }
344 
347 {
348  switch (umap)
349  {
350  case umap_imag:
351  return 0.0f;
352 
353  case umap_real:
354  case umap_conj:
355  return scalar;
356 
357 #define SCALAR_MAPPER(UMAP, FCN) \
358  case umap_ ## UMAP: \
359  return octave_value (FCN (scalar))
360 
361  SCALAR_MAPPER (abs, ::fabsf);
368  SCALAR_MAPPER (atan, ::atanf);
380  SCALAR_MAPPER (ceil, ::ceilf);
381  SCALAR_MAPPER (cos, ::cosf);
382  SCALAR_MAPPER (cosh, ::coshf);
383  SCALAR_MAPPER (exp, ::expf);
394  SCALAR_MAPPER (sin, ::sinf);
395  SCALAR_MAPPER (sinh, ::sinhf);
397  SCALAR_MAPPER (tan, ::tanf);
398  SCALAR_MAPPER (tanh, ::tanhf);
404 
405  // Special cases for Matlab compatibility.
406  case umap_xtolower:
407  case umap_xtoupper:
408  return scalar;
409 
410  case umap_xisalnum:
411  case umap_xisalpha:
412  case umap_xisascii:
413  case umap_xiscntrl:
414  case umap_xisdigit:
415  case umap_xisgraph:
416  case umap_xislower:
417  case umap_xisprint:
418  case umap_xispunct:
419  case umap_xisspace:
420  case umap_xisupper:
421  case umap_xisxdigit:
422  case umap_xtoascii:
423  {
424  octave_value str_conv = convert_to_str (true, true);
425  return str_conv.map (umap);
426  }
427 
428  default:
429  return octave_base_value::map (umap);
430  }
431 }
432 
433 bool
435  builtin_type_t btyp) const
436 {
437 
438  // Support inline real->complex conversion.
439  if (btyp == btyp_float)
440  {
441  *(reinterpret_cast<float *>(where)) = scalar;
442  return true;
443  }
444  else if (btyp == btyp_float_complex)
445  {
446  *(reinterpret_cast<FloatComplex *>(where)) = scalar;
447  return true;
448  }
449  else
450  return false;
451 }
octave_value convert_to_str_internal(bool pad, bool force, char type) const
Definition: ov-float.cc:171
octave_value as_uint64(void) const
Definition: ov-float.cc:159
octave_int< uint64_t > octave_uint64
Complex rc_log10(double x)
Definition: lo-mappers.cc:536
octave_value resize(const dim_vector &dv, bool fill=false) const
Definition: ov-float.cc:82
octave_value as_int64(void) const
Definition: ov-float.cc:135
float erfcx(float x)
Definition: lo-specfun.cc:271
octave_value as_int8(void) const
Definition: ov-float.cc:117
function erf(X)
Definition: erf.f:2
Complex rc_acosh(double x)
Definition: lo-mappers.cc:468
Complex rc_sqrt(double x)
Definition: lo-mappers.cc:551
std::complex< double > erfi(std::complex< double > z, double relerr=0)
const octave_hdf5_id octave_H5S_ALL
bool load_binary(std::istream &is, bool swap, octave::mach_info::float_format fmt)
Definition: ov-float.cc:228
bool isnan(double x)
Definition: lo-mappers.cc:347
octave_value as_uint32(void) const
Definition: ov-float.cc:153
octave_int< uint16_t > octave_uint16
Complex rc_lgamma(double x)
Definition: lo-specfun.cc:373
OCTAVE_EXPORT octave_value_list or N dimensional array whose elements are all equal to the base of natural logarithms The constant ex $e satisfies the equation log(e)
double ceil(double x)
Definition: lo-mappers.h:138
function atanh(X)
Definition: atanh.f:2
void error(const char *fmt,...)
Definition: error.cc:570
void * get_data(void) const
Definition: mxarray.h:449
octave_value map(octave_base_value::unary_mapper_t umap) const
Definition: ov.h:1413
#define DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA(t, n, c)
Definition: ov-base.h:169
bool load_ascii(std::istream &is)
Definition: ov-float.cc:207
octave_value diag(octave_idx_type m, octave_idx_type n) const
Definition: ov-float.cc:165
Complex acos(const Complex &x)
Definition: lo-mappers.cc:90
octave_value as_int32(void) const
Definition: ov-float.cc:129
FloatMatrix float_matrix_value(bool=false) const
Definition: ov-float.h:159
double expm1(double x)
Definition: lo-specfun.cc:473
float float_value(bool=false) const
Definition: ov-float.h:149
double asinh(double x)
Definition: lo-specfun.cc:105
double fix(double x)
Definition: lo-mappers.h:158
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:830
bool save_ascii(std::ostream &os)
Definition: ov-float.cc:195
Complex asin(const Complex &x)
Definition: lo-mappers.cc:150
builtin_type_t
Definition: ov-base.h:61
double cbrt(double x)
Definition: lo-specfun.cc:648
double arg(double x)
Definition: lo-mappers.h:83
octave_value arg
Definition: pr-output.cc:3440
octave_idx_type numel(int n=0) const
Number of elements that a matrix with this dimensions would have.
Definition: dim-vector.h:389
double round(double x)
Definition: lo-mappers.cc:333
bool is_NA(double x)
Definition: lo-mappers.cc:54
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
bool swap
Definition: load-save.cc:725
Complex rc_atanh(double x)
Definition: lo-mappers.cc:493
float erfi(float x)
Definition: lo-specfun.cc:289
void warn_load(const char *type) const
Definition: ov-base.cc:1151
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
double gamma(double x)
Definition: lo-specfun.cc:325
Complex atan(const Complex &x)
Definition: lo-mappers.cc:210
octave_value as_int16(void) const
Definition: ov-float.cc:123
function asinh(X)
Definition: asinh.f:2
nd deftypefn *octave_map m
Definition: ov-struct.cc:2058
octave_value as_double(void) const
Definition: ov-float.cc:105
double signum(double x)
Definition: lo-mappers.h:259
bool save_hdf5(octave_hdf5_id loc_id, const char *name, bool save_as_floats)
Definition: ov-float.cc:246
octave_value do_index_op(const octave_value_list &idx, bool resize_ok=false)
Definition: ov-float.cc:64
float dawson(float x)
Definition: lo-specfun.cc:307
Complex rc_log2(double x)
Definition: lo-mappers.cc:521
double tmp
Definition: data.cc:6300
octave_value retval
Definition: data.cc:6294
Complex rc_log(double x)
Definition: lo-mappers.cc:506
octave_value as_single(void) const
Definition: ov-float.cc:111
double signbit(double x)
Definition: lo-mappers.cc:321
bool finite(double x)
Definition: lo-mappers.cc:367
std::complex< double > erfcx(std::complex< double > z, double relerr=0)
int64_t octave_hdf5_id
idx type
Definition: ov.cc:3129
octave_value as_uint8(void) const
Definition: ov-float.cc:141
octave_value map(unary_mapper_t umap) const
Definition: ov-float.cc:346
bool load_hdf5(octave_hdf5_id loc_id, const char *name)
Definition: ov-float.cc:290
void warn_save(const char *type) const
Definition: ov-base.cc:1160
octave_int< uint32_t > octave_uint32
double erfcinv(double x)
Definition: lo-specfun.cc:3049
void mxArray
Definition: mex.h:55
bool isinf(double x)
Definition: lo-mappers.cc:387
function acosh(X)
Definition: acosh.f:2
friend class octave_value
Definition: ov-base.h:211
Complex rc_acos(double x)
Definition: lo-mappers.cc:455
void warning(const char *fmt,...)
Definition: error.cc:788
double erf(double x)
Definition: lo-specfun.cc:193
charNDArray max(char d, const charNDArray &m)
Definition: chNDArray.cc:228
void octave_write_float(std::ostream &os, float d)
Definition: lo-utils.cc:411
#define SCALAR_MAPPER(UMAP, FCN)
double log1p(double x)
Definition: lo-specfun.cc:587
OCTAVE_EXPORT octave_value_list return the value of the option it must match the dimension of the state and the relative tolerance must also be a vector of the same length tem it must match the dimension of the state and the absolute tolerance must also be a vector of the same length The local error test applied at each integration step is xample roup abs(local error in x(i))<
function gamma(X)
Definition: gamma.f:2
mxArray * as_mxArray(void) const
Definition: ov-float.cc:334
octave_int< int64_t > octave_int64
Complex rc_asin(double x)
Definition: lo-mappers.cc:480
double roundb(double x)
Definition: lo-mappers.h:189
Complex rc_log1p(double x)
Definition: lo-specfun.cc:2872
octave_value as_uint16(void) const
Definition: ov-float.cc:147
virtual octave_value map(unary_mapper_t) const
Definition: ov-base.cc:1175
double erfinv(double x)
Definition: lo-specfun.cc:2960
double floor(double x)
Definition: lo-mappers.cc:330
const octave_hdf5_id octave_H5P_DEFAULT
double erfc(double x)
Definition: lo-specfun.cc:232
std::complex< float > FloatComplex
Definition: oct-cmplx.h:32
octave_int< int16_t > octave_int16
double lgamma(double x)
Definition: lo-specfun.cc:353
Definition: mxarray.h:76
bool fast_elem_insert_self(void *where, builtin_type_t btyp) const
Definition: ov-float.cc:434
write the output to stdout if nargout is
Definition: load-save.cc:1576
void err_nan_to_character_conversion(void)
octave_int< uint8_t > octave_uint8
bool save_binary(std::ostream &os, bool &save_as_floats)
Definition: ov-float.cc:217
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:87
octave_int< int32_t > octave_int32
virtual octave_value convert_to_str(bool pad=false, bool force=false, char type= '\'') const
Definition: ov-base.cc:418
If this string is the system will ring the terminal sometimes it is useful to be able to print the original representation of the string
Definition: utils.cc:854
dim_vector dv
Definition: sub2ind.cc:263
double log2(double x)
Definition: lo-mappers.cc:233
octave_int< int8_t > octave_int8
int nint(double x)
Definition: lo-mappers.cc:433
std::complex< double > erfc(std::complex< double > z, double relerr=0)
octave_value do_index_op(const octave_value_list &idx, bool resize_ok=false)
Definition: ov.h:454