GNU Octave  4.0.0
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-2015 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 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26 
27 #include <iostream>
28 
29 #include "data-conv.h"
30 #include "mach-info.h"
31 #include "lo-specfun.h"
32 #include "lo-mappers.h"
33 
34 #include "defun.h"
35 #include "gripes.h"
36 #include "mxarray.h"
37 #include "oct-obj.h"
38 #include "oct-hdf5.h"
39 #include "oct-stream.h"
40 #include "ov-scalar.h"
41 #include "ov-float.h"
42 #include "ov-base.h"
43 #include "ov-base-scalar.h"
44 #include "ov-base-scalar.cc"
45 #include "ov-flt-re-mat.h"
46 #include "ov-typeinfo.h"
47 #include "pr-output.h"
48 #include "xdiv.h"
49 #include "xpow.h"
50 #include "ops.h"
51 
52 #include "ls-oct-ascii.h"
53 #include "ls-hdf5.h"
54 
55 template class octave_base_scalar<float>;
56 
57 
59  "single");
60 
63 {
64  // FIXME: this doesn't solve the problem of
65  //
66  // a = 1; a([1,1], [1,1], [1,1])
67  //
68  // and similar constructions. Hmm...
69 
70  // FIXME: using this constructor avoids narrowing the
71  // 1x1 matrix back to a scalar value. Need a better solution
72  // to this problem.
73 
75 
76  return tmp.do_index_op (idx, resize_ok);
77 }
78 
80 octave_float_scalar::resize (const dim_vector& dv, bool fill) const
81 {
82  if (fill)
83  {
84  FloatNDArray retval (dv, 0);
85 
86  if (dv.numel ())
87  retval(0) = scalar;
88 
89  return retval;
90  }
91  else
92  {
93  FloatNDArray retval (dv);
94 
95  if (dv.numel ())
96  retval(0) = scalar;
97 
98  return retval;
99  }
100 }
101 
104 {
105  return FloatDiagMatrix (Array<float> (dim_vector (1, 1), scalar), m, n);
106 }
107 
110 {
111  octave_value retval;
112 
113  if (xisnan (scalar))
115  else
116  {
117  int ival = NINT (scalar);
118 
119  if (ival < 0 || ival > std::numeric_limits<unsigned char>::max ())
120  {
121  // FIXME: is there something better we could do?
122 
123  ival = 0;
124 
125  ::warning ("range error for conversion to character value");
126  }
127 
128  retval = octave_value (std::string (1, static_cast<char> (ival)), type);
129  }
130 
131  return retval;
132 }
133 
134 bool
136 {
137  float d = float_value ();
138 
139  octave_write_float (os, d);
140 
141  os << "\n";
142 
143  return true;
144 }
145 
146 bool
148 {
149  scalar = octave_read_value<float> (is);
150  if (!is)
151  {
152  error ("load: failed to load scalar constant");
153  return false;
154  }
155 
156  return true;
157 }
158 
159 bool
160 octave_float_scalar::save_binary (std::ostream& os, bool& /* save_as_floats */)
161 {
162  char tmp = LS_FLOAT;
163  os.write (reinterpret_cast<char *> (&tmp), 1);
164  float dtmp = float_value ();
165  os.write (reinterpret_cast<char *> (&dtmp), 4);
166 
167  return true;
168 }
169 
170 bool
171 octave_float_scalar::load_binary (std::istream& is, bool swap,
173 {
174  char tmp;
175  if (! is.read (reinterpret_cast<char *> (&tmp), 1))
176  return false;
177 
178  float dtmp;
179  read_floats (is, &dtmp, static_cast<save_type> (tmp), 1, swap, fmt);
180  if (error_state || ! is)
181  return false;
182 
183  scalar = dtmp;
184  return true;
185 }
186 
187 bool
189  bool /* save_as_floats */)
190 {
191  bool retval = false;
192 
193 #if defined (HAVE_HDF5)
194 
195  hsize_t dimens[3];
196  hid_t space_hid, data_hid;
197  space_hid = data_hid = -1;
198 
199  space_hid = H5Screate_simple (0, dimens, 0);
200  if (space_hid < 0) return false;
201 #if HAVE_HDF5_18
202  data_hid = H5Dcreate (loc_id, name, H5T_NATIVE_FLOAT, space_hid,
203  H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
204 #else
205  data_hid = H5Dcreate (loc_id, name, H5T_NATIVE_FLOAT, space_hid,
206  H5P_DEFAULT);
207 #endif
208  if (data_hid < 0)
209  {
210  H5Sclose (space_hid);
211  return false;
212  }
213 
214  float tmp = float_value ();
215  retval = H5Dwrite (data_hid, H5T_NATIVE_FLOAT, H5S_ALL, H5S_ALL,
216  H5P_DEFAULT, &tmp) >= 0;
217 
218  H5Dclose (data_hid);
219  H5Sclose (space_hid);
220 
221 #else
222  gripe_save ("hdf5");
223 #endif
224 
225  return retval;
226 }
227 
228 bool
230 {
231 #if defined (HAVE_HDF5)
232 
233 #if HAVE_HDF5_18
234  hid_t data_hid = H5Dopen (loc_id, name, H5P_DEFAULT);
235 #else
236  hid_t data_hid = H5Dopen (loc_id, name);
237 #endif
238  hid_t space_id = H5Dget_space (data_hid);
239 
240  hsize_t rank = H5Sget_simple_extent_ndims (space_id);
241 
242  if (rank != 0)
243  {
244  H5Dclose (data_hid);
245  return false;
246  }
247 
248  float dtmp;
249  if (H5Dread (data_hid, H5T_NATIVE_FLOAT, H5S_ALL, H5S_ALL,
250  H5P_DEFAULT, &dtmp) < 0)
251  {
252  H5Dclose (data_hid);
253  return false;
254  }
255 
256  scalar = dtmp;
257 
258  H5Dclose (data_hid);
259 
260  return true;
261 
262 #else
263  gripe_load ("hdf5");
264  return false;
265 #endif
266 }
267 
268 mxArray *
270 {
271  mxArray *retval = new mxArray (mxSINGLE_CLASS, 1, 1, mxREAL);
272 
273  float *pr = static_cast<float *> (retval->get_data ());
274 
275  pr[0] = scalar;
276 
277  return retval;
278 }
279 
282 {
283  switch (umap)
284  {
285  case umap_imag:
286  return 0.0f;
287 
288  case umap_real:
289  case umap_conj:
290  return scalar;
291 
292 #define SCALAR_MAPPER(UMAP, FCN) \
293  case umap_ ## UMAP: \
294  return octave_value (FCN (scalar))
295 
296  SCALAR_MAPPER (abs, ::fabsf);
299  SCALAR_MAPPER (angle, ::arg);
300  SCALAR_MAPPER (arg, ::arg);
303  SCALAR_MAPPER (atan, ::atanf);
305  SCALAR_MAPPER (erf, ::erff);
308  SCALAR_MAPPER (erfc, ::erfcf);
310  SCALAR_MAPPER (erfi, ::erfi);
313  SCALAR_MAPPER (lgamma, rc_lgamma);
314  SCALAR_MAPPER (cbrt, ::cbrtf);
315  SCALAR_MAPPER (ceil, ::ceilf);
316  SCALAR_MAPPER (cos, ::cosf);
317  SCALAR_MAPPER (cosh, ::coshf);
318  SCALAR_MAPPER (exp, ::expf);
320  SCALAR_MAPPER (fix, ::fix);
321  SCALAR_MAPPER (floor, gnulib::floorf);
322  SCALAR_MAPPER (log, rc_log);
323  SCALAR_MAPPER (log2, rc_log2);
324  SCALAR_MAPPER (log10, rc_log10);
326  SCALAR_MAPPER (round, xround);
327  SCALAR_MAPPER (roundb, xroundb);
329  SCALAR_MAPPER (sin, ::sinf);
330  SCALAR_MAPPER (sinh, ::sinhf);
331  SCALAR_MAPPER (sqrt, rc_sqrt);
332  SCALAR_MAPPER (tan, ::tanf);
333  SCALAR_MAPPER (tanh, ::tanhf);
334  SCALAR_MAPPER (finite, xfinite);
335  SCALAR_MAPPER (isinf, xisinf);
336  SCALAR_MAPPER (isna, octave_is_NA);
337  SCALAR_MAPPER (isnan, xisnan);
339 
340  // Special cases for Matlab compatibility.
341  case umap_xtolower:
342  case umap_xtoupper:
343  return scalar;
344 
345  case umap_xisalnum:
346  case umap_xisalpha:
347  case umap_xisascii:
348  case umap_xiscntrl:
349  case umap_xisdigit:
350  case umap_xisgraph:
351  case umap_xislower:
352  case umap_xisprint:
353  case umap_xispunct:
354  case umap_xisspace:
355  case umap_xisupper:
356  case umap_xisxdigit:
357  case umap_xtoascii:
358  {
359  octave_value str_conv = convert_to_str (true, true);
360  return error_state ? octave_value () : str_conv.map (umap);
361  }
362 
363  default:
364  return octave_base_value::map (umap);
365  }
366 }
367 
368 bool
370  builtin_type_t btyp) const
371 {
372 
373  // Support inline real->complex conversion.
374  if (btyp == btyp_float)
375  {
376  *(reinterpret_cast<float *>(where)) = scalar;
377  return true;
378  }
379  else if (btyp == btyp_float_complex)
380  {
381  *(reinterpret_cast<FloatComplex *>(where)) = scalar;
382  return true;
383  }
384  else
385  return false;
386 }
octave_value convert_to_str_internal(bool pad, bool force, char type) const
Definition: ov-float.cc:109
float erfcf(float x)
Definition: lo-specfun.cc:280
Complex rc_asin(double x)
Definition: lo-mappers.cc:536
T xsignbit(T x)
Definition: lo-mappers.h:378
octave_value resize(const dim_vector &dv, bool fill=false) const
Definition: ov-float.cc:80
double xround(double x)
Definition: lo-mappers.cc:63
bool xisnan(double x)
Definition: lo-mappers.cc:144
double cbrt(double x)
Definition: lo-specfun.cc:662
double log1p(double x)
Definition: lo-specfun.cc:620
double erfcinv(double x)
Definition: lo-specfun.cc:3151
std::complex< double > erfi(std::complex< double > z, double relerr=0)
void gripe_load(const char *type) const
Definition: ov-base.cc:1258
Complex rc_acos(double x)
Definition: lo-mappers.cc:512
float erff(float x)
Definition: lo-specfun.cc:260
float cbrtf(float x)
Definition: lo-specfun.cc:721
function atanh(X)
Definition: atanh.f:2
void error(const char *fmt,...)
Definition: error.cc:476
void * get_data(void) const
Definition: mxarray.h:433
octave_value map(octave_base_value::unary_mapper_t umap) const
Definition: ov.h:1226
bool xisinf(double x)
Definition: lo-mappers.cc:160
#define DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA(t, n, c)
Definition: ov-base.h:164
void gripe_save(const char *type) const
Definition: ov-base.cc:1267
bool load_ascii(std::istream &is)
Definition: ov-float.cc:147
octave_value diag(octave_idx_type m, octave_idx_type n) const
Definition: ov-float.cc:103
double expm1(double x)
Definition: lo-specfun.cc:510
FloatMatrix float_matrix_value(bool=false) const
Definition: ov-float.h:155
float float_value(bool=false) const
Definition: ov-float.h:145
bool save_ascii(std::ostream &os)
Definition: ov-float.cc:135
builtin_type_t
Definition: ov-base.h:59
std::complex< double > erf(std::complex< double > z, double relerr=0)
octave_idx_type numel(int n=0) const
Number of elements that a matrix with this dimensions would have.
Definition: dim-vector.h:361
F77_RET_T const double const double double * d
subroutine xgamma(x, result)
Definition: xgamma.f:1
double signum(double x)
Definition: lo-mappers.cc:80
Complex rc_log1p(double x)
Definition: lo-specfun.cc:2977
Complex rc_lgamma(double x)
Definition: lo-specfun.cc:411
function asinh(X)
Definition: asinh.f:2
std::complex< T > ceil(const std::complex< T > &x)
Definition: lo-mappers.h:275
bool save_hdf5(octave_hdf5_id loc_id, const char *name, bool save_as_floats)
Definition: ov-float.cc:188
octave_value do_index_op(const octave_value_list &idx, bool resize_ok=false)
Definition: ov-float.cc:62
int error_state
Definition: error.cc:101
Complex rc_sqrt(double x)
Definition: lo-mappers.cc:606
void gripe_nan_to_character_conversion(void)
Complex rc_log(double x)
Definition: lo-mappers.cc:561
Complex rc_log2(double x)
Definition: lo-mappers.cc:577
std::complex< double > erfcx(std::complex< double > z, double relerr=0)
double xroundb(double x)
Definition: lo-mappers.cc:69
Complex rc_atanh(double x)
Definition: lo-mappers.cc:548
octave_value map(unary_mapper_t umap) const
Definition: ov-float.cc:281
int NINT(double x)
Definition: lo-mappers.cc:657
bool load_hdf5(octave_hdf5_id loc_id, const char *name)
Definition: ov-float.cc:229
double erfinv(double x)
Definition: lo-specfun.cc:3063
Complex rc_log10(double x)
Definition: lo-mappers.cc:591
void mxArray
Definition: mex.h:53
function acosh(X)
Definition: acosh.f:2
double arg(double x)
Definition: lo-mappers.h:37
friend class octave_value
Definition: ov-base.h:206
void warning(const char *fmt,...)
Definition: error.cc:681
charNDArray max(char d, const charNDArray &m)
Definition: chNDArray.cc:233
float expm1f(float x)
Definition: lo-specfun.cc:565
bool xfinite(double x)
Definition: lo-mappers.cc:152
Complex rc_acosh(double x)
Definition: lo-mappers.cc:524
float asinhf(float x)
Definition: lo-specfun.cc:220
void octave_write_float(std::ostream &os, float d)
Definition: lo-utils.cc:409
#define SCALAR_MAPPER(UMAP, FCN)
function gamma(X)
Definition: gamma.f:2
mxArray * as_mxArray(void) const
Definition: ov-float.cc:269
virtual octave_value map(unary_mapper_t) const
Definition: ov-base.cc:1276
double fix(double x)
Definition: lo-mappers.h:39
float dawson(float x)
Definition: lo-specfun.cc:349
Complex asin(const Complex &x)
Definition: lo-mappers.cc:204
std::complex< float > FloatComplex
Definition: oct-cmplx.h:30
std::complex< T > floor(const std::complex< T > &x)
Definition: lo-mappers.h:282
bool load_binary(std::istream &is, bool swap, oct_mach_info::float_format fmt)
Definition: ov-float.cc:171
Definition: mxarray.h:52
bool fast_elem_insert_self(void *where, builtin_type_t btyp) const
Definition: ov-float.cc:369
Complex acos(const Complex &x)
Definition: lo-mappers.cc:177
bool save_binary(std::ostream &os, bool &save_as_floats)
Definition: ov-float.cc:160
virtual octave_value convert_to_str(bool pad=false, bool force=false, char type= '\'') const
Definition: ov-base.cc:373
T abs(T x)
Definition: pr-output.cc:3062
Complex atan(const Complex &x)
Definition: lo-mappers.cc:231
void read_floats(std::istream &is, float *data, save_type type, octave_idx_type len, bool swap, oct_mach_info::float_format fmt)
Definition: data-conv.cc:837
bool octave_is_NA(double x)
Definition: lo-mappers.cc:167
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:438