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-scalar.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-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 // Prevent implicit instantiations on some systems (Windows, others?)
58 // that can lead to duplicate definitions of static data members.
59 
60 extern template class OCTINTERP_API octave_base_scalar<float>;
61 
62 template class octave_base_scalar<double>;
63 
65 
66 static octave_base_value *
68 {
69  const octave_scalar& v = dynamic_cast<const octave_scalar&> (a);
70 
71  return new octave_float_scalar (v.float_value ());
72 }
73 
76 {
80 }
81 
83 octave_scalar::do_index_op (const octave_value_list& idx, bool resize_ok)
84 {
85  // FIXME: this doesn't solve the problem of
86  //
87  // a = 1; a([1,1], [1,1], [1,1])
88  //
89  // and similar constructions. Hmm...
90 
91  // FIXME: using this constructor avoids narrowing the
92  // 1x1 matrix back to a scalar value. Need a better solution
93  // to this problem.
94 
96 
97  return tmp.do_index_op (idx, resize_ok);
98 }
99 
101 octave_scalar::resize (const dim_vector& dv, bool fill) const
102 {
103  if (fill)
104  {
105  NDArray retval (dv, 0);
106 
107  if (dv.numel ())
108  retval(0) = scalar;
109 
110  return retval;
111  }
112  else
113  {
114  NDArray retval (dv);
115 
116  if (dv.numel ())
117  retval(0) = scalar;
118 
119  return retval;
120  }
121 }
122 
125 {
126  return scalar;
127 }
128 
131 {
132  return static_cast<float> (scalar);
133 }
134 
137 {
138  return octave_int8 (scalar);
139 }
140 
143 {
144  return octave_int16 (scalar);
145 }
146 
149 {
150  return octave_int32 (scalar);
151 }
152 
155 {
156  return octave_int64 (scalar);
157 }
158 
161 {
162  return octave_uint8 (scalar);
163 }
164 
167 {
168  return octave_uint16 (scalar);
169 }
170 
173 {
174  return octave_uint32 (scalar);
175 }
176 
179 {
180  return octave_uint64 (scalar);
181 }
182 
185 {
186  return DiagMatrix (Array<double> (dim_vector (1, 1), scalar), m, n);
187 }
188 
191 {
193 
196 
197  int ival = octave::math::nint (scalar);
198 
199  if (ival < 0 || ival > std::numeric_limits<unsigned char>::max ())
200  {
201  // FIXME: is there something better we could do?
202 
203  ival = 0;
204 
205  ::warning ("range error for conversion to character value");
206  }
207 
208  retval = octave_value (std::string (1, static_cast<char> (ival)), type);
209 
210  return retval;
211 }
212 
213 bool
214 octave_scalar::save_ascii (std::ostream& os)
215 {
216  double d = double_value ();
217 
218  octave_write_double (os, d);
219 
220  os << "\n";
221 
222  return true;
223 }
224 
225 bool
227 {
228  scalar = octave_read_value<double> (is);
229 
230  if (! is)
231  error ("load: failed to load scalar constant");
232 
233  return true;
234 }
235 
236 bool
237 octave_scalar::save_binary (std::ostream& os, bool& /* save_as_floats */)
238 {
239  char tmp = LS_DOUBLE;
240  os.write (reinterpret_cast<char *> (&tmp), 1);
241  double dtmp = double_value ();
242  os.write (reinterpret_cast<char *> (&dtmp), 8);
243 
244  return true;
245 }
246 
247 bool
248 octave_scalar::load_binary (std::istream& is, bool swap,
250 {
251  char tmp;
252  if (! is.read (reinterpret_cast<char *> (&tmp), 1))
253  return false;
254 
255  double dtmp;
256  read_doubles (is, &dtmp, static_cast<save_type> (tmp), 1, swap, fmt);
257 
258  if (! is)
259  return false;
260 
261  scalar = dtmp;
262  return true;
263 }
264 
265 bool
267  bool /* save_as_floats */)
268 {
269  bool retval = false;
270 
271 #if defined (HAVE_HDF5)
272 
273  hsize_t dimens[3];
274  hid_t space_hid, data_hid;
275  space_hid = data_hid = -1;
276 
277  space_hid = H5Screate_simple (0, dimens, 0);
278  if (space_hid < 0) return false;
279 
280 #if defined (HAVE_HDF5_18)
281  data_hid = H5Dcreate (loc_id, name, H5T_NATIVE_DOUBLE, space_hid,
283 #else
284  data_hid = H5Dcreate (loc_id, name, H5T_NATIVE_DOUBLE, space_hid,
286 #endif
287  if (data_hid < 0)
288  {
289  H5Sclose (space_hid);
290  return false;
291  }
292 
293  double tmp = double_value ();
294  retval = H5Dwrite (data_hid, H5T_NATIVE_DOUBLE, octave_H5S_ALL, octave_H5S_ALL,
295  octave_H5P_DEFAULT, &tmp) >= 0;
296 
297  H5Dclose (data_hid);
298  H5Sclose (space_hid);
299 
300 #else
301  octave_unused_parameter (loc_id);
302  octave_unused_parameter (name);
303 
304  warn_save ("hdf5");
305 #endif
306 
307  return retval;
308 }
309 
310 bool
312 {
313 #if defined (HAVE_HDF5)
314 
315 #if defined (HAVE_HDF5_18)
316  hid_t data_hid = H5Dopen (loc_id, name, octave_H5P_DEFAULT);
317 #else
318  hid_t data_hid = H5Dopen (loc_id, name);
319 #endif
320  hid_t space_id = H5Dget_space (data_hid);
321 
322  hsize_t rank = H5Sget_simple_extent_ndims (space_id);
323 
324  if (rank != 0)
325  {
326  H5Dclose (data_hid);
327  return false;
328  }
329 
330  double dtmp;
331  if (H5Dread (data_hid, H5T_NATIVE_DOUBLE, octave_H5S_ALL, octave_H5S_ALL,
332  octave_H5P_DEFAULT, &dtmp) < 0)
333  {
334  H5Dclose (data_hid);
335  return false;
336  }
337 
338  scalar = dtmp;
339 
340  H5Dclose (data_hid);
341 
342  return true;
343 
344 #else
345  octave_unused_parameter (loc_id);
346  octave_unused_parameter (name);
347 
348  warn_load ("hdf5");
349 
350  return false;
351 #endif
352 }
353 
354 mxArray *
356 {
357  mxArray *retval = new mxArray (mxDOUBLE_CLASS, 1, 1, mxREAL);
358 
359  double *pr = static_cast<double *> (retval->get_data ());
360 
361  pr[0] = scalar;
362 
363  return retval;
364 }
365 
368 {
369  switch (umap)
370  {
371  case umap_imag:
372  return 0.0;
373 
374  case umap_real:
375  case umap_conj:
376  return scalar;
377 
378 #define SCALAR_MAPPER(UMAP, FCN) \
379  case umap_ ## UMAP: \
380  return octave_value (FCN (scalar))
381 
382  SCALAR_MAPPER (abs, ::fabs);
389  SCALAR_MAPPER (atan, ::atan);
401  SCALAR_MAPPER (ceil, ::ceil);
402  SCALAR_MAPPER (cos, ::cos);
403  SCALAR_MAPPER (cosh, ::cosh);
404  SCALAR_MAPPER (exp, ::exp);
415  SCALAR_MAPPER (sin, ::sin);
416  SCALAR_MAPPER (sinh, ::sinh);
418  SCALAR_MAPPER (tan, ::tan);
419  SCALAR_MAPPER (tanh, ::tanh);
425 
426  // Special cases for Matlab compatibility.
427  case umap_xtolower:
428  case umap_xtoupper:
429  return scalar;
430 
431  case umap_xisalnum:
432  case umap_xisalpha:
433  case umap_xisascii:
434  case umap_xiscntrl:
435  case umap_xisdigit:
436  case umap_xisgraph:
437  case umap_xislower:
438  case umap_xisprint:
439  case umap_xispunct:
440  case umap_xisspace:
441  case umap_xisupper:
442  case umap_xisxdigit:
443  case umap_xtoascii:
444  {
445  octave_value str_conv = convert_to_str (true, true);
446  return str_conv.map (umap);
447  }
448 
449  default:
450  return octave_base_value::map (umap);
451  }
452 }
453 
454 bool
456 {
457 
458  // Support inline real->complex conversion.
459  if (btyp == btyp_double)
460  {
461  *(reinterpret_cast<double *>(where)) = scalar;
462  return true;
463  }
464  else if (btyp == btyp_complex)
465  {
466  *(reinterpret_cast<Complex *>(where)) = scalar;
467  return true;
468  }
469  else
470  return false;
471 }
void octave_write_double(std::ostream &os, double d)
Definition: lo-utils.cc:388
Matrix matrix_value(bool=false) const
Definition: ov-scalar.h:155
bool load_ascii(std::istream &is)
Definition: ov-scalar.cc:226
octave_int< uint64_t > octave_uint64
Complex rc_log10(double x)
Definition: lo-mappers.cc:536
octave_value as_uint64(void) const
Definition: ov-scalar.cc:178
float erfcx(float x)
Definition: lo-specfun.cc:271
function erf(X)
Definition: erf.f:2
bool save_hdf5(octave_hdf5_id loc_id, const char *name, bool save_as_floats)
Definition: ov-scalar.cc:266
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
octave_value as_int64(void) const
Definition: ov-scalar.cc:154
octave_value as_int8(void) const
Definition: ov-scalar.cc:136
bool isnan(double x)
Definition: lo-mappers.cc:347
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
#define SCALAR_MAPPER(UMAP, FCN)
float float_value(bool=false) const
Definition: ov-scalar.h:147
Complex acos(const Complex &x)
Definition: lo-mappers.cc:90
double expm1(double x)
Definition: lo-specfun.cc:473
double asinh(double x)
Definition: lo-specfun.cc:105
double fix(double x)
Definition: lo-mappers.h:158
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 resize(const dim_vector &dv, bool fill=false) const
Definition: ov-scalar.cc:101
octave_value map(unary_mapper_t umap) const
Definition: ov-scalar.cc:367
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
octave_value as_double(void) const
Definition: ov-scalar.cc:124
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
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
bool fast_elem_insert_self(void *where, builtin_type_t btyp) const
Definition: ov-scalar.cc:455
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
#define OCTINTERP_API
Definition: mexproto.h:69
bool save_binary(std::ostream &os, bool &save_as_floats)
Definition: ov-scalar.cc:237
function asinh(X)
Definition: asinh.f:2
void read_doubles(std::istream &is, double *data, save_type type, octave_idx_type len, bool swap, octave::mach_info::float_format fmt)
Definition: data-conv.cc:771
nd deftypefn *octave_map m
Definition: ov-struct.cc:2058
octave_value as_uint16(void) const
Definition: ov-scalar.cc:166
double signum(double x)
Definition: lo-mappers.h:259
static int static_type_id(void)
Definition: ov-float.h:271
static octave_base_value * default_numeric_demotion_function(const octave_base_value &a)
Definition: ov-scalar.cc:67
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_uint32(void) const
Definition: ov-scalar.cc:172
octave_value diag(octave_idx_type m, octave_idx_type n) const
Definition: ov-scalar.cc:184
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
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
octave_value as_uint8(void) const
Definition: ov-scalar.cc:160
double erf(double x)
Definition: lo-specfun.cc:193
type_conv_info numeric_demotion_function(void) const
Definition: ov-scalar.cc:75
double double_value(bool=false) const
Definition: ov-scalar.h:145
charNDArray max(char d, const charNDArray &m)
Definition: chNDArray.cc:228
mxArray * as_mxArray(void) const
Definition: ov-scalar.cc:355
bool save_ascii(std::ostream &os)
Definition: ov-scalar.cc:214
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))<
octave_value as_int32(void) const
Definition: ov-scalar.cc:148
function gamma(X)
Definition: gamma.f:2
bool load_binary(std::istream &is, bool swap, octave::mach_info::float_format fmt)
Definition: ov-scalar.cc:248
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
virtual octave_value map(unary_mapper_t) const
Definition: ov-base.cc:1175
double erfinv(double x)
Definition: lo-specfun.cc:2960
bool load_hdf5(octave_hdf5_id loc_id, const char *name)
Definition: ov-scalar.cc:311
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
octave_int< int16_t > octave_int16
octave_value convert_to_str_internal(bool pad, bool force, char type) const
Definition: ov-scalar.cc:190
double lgamma(double x)
Definition: lo-specfun.cc:353
Definition: mxarray.h:76
std::complex< double > Complex
Definition: oct-cmplx.h:31
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
octave_value as_single(void) const
Definition: ov-scalar.cc:130
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_value as_int16(void) const
Definition: ov-scalar.cc:142
octave_value do_index_op(const octave_value_list &idx, bool resize_ok=false)
Definition: ov-scalar.cc:83
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