GNU Octave  4.4.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
ov-scalar.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1996-2018 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
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License 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 <https://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
215 {
216  double d = double_value ();
217 
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, nullptr);
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);
385  SCALAR_MAPPER (angle, std::arg);
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  {
444  octave_value str_conv = convert_to_str (true, true);
445  return str_conv.map (umap);
446  }
447 
448  default:
449  return octave_base_value::map (umap);
450  }
451 }
452 
453 bool
455 {
456 
457  // Support inline real->complex conversion.
458  if (btyp == btyp_double)
459  {
460  *(reinterpret_cast<double *>(where)) = scalar;
461  return true;
462  }
463  else if (btyp == btyp_complex)
464  {
465  *(reinterpret_cast<Complex *>(where)) = scalar;
466  return true;
467  }
468  else
469  return false;
470 }
octave_idx_type write(const octave_value &data, octave_idx_type block_size, oct_data_conv::data_type output_type, octave_idx_type skip, mach_info::float_format flt_fmt)
Definition: oct-stream.cc:6704
double erfcx(double x)
Definition: lo-specfun.cc:1756
void octave_write_double(std::ostream &os, double d)
Definition: lo-utils.cc:395
bool load_ascii(std::istream &is)
Definition: ov-scalar.cc:226
octave_value as_uint32(void) const
Definition: ov-scalar.cc:172
octave_int< uint64_t > octave_uint64
octave_value map(octave_base_value::unary_mapper_t umap) const
Definition: ov.h:1473
Complex rc_log10(double x)
Definition: lo-mappers.cc:307
Matrix matrix_value(bool=false) const
Definition: ov-scalar.h:153
type_conv_info numeric_demotion_function(void) const
Definition: ov-scalar.cc:75
float float_value(bool=false) const
Definition: ov-scalar.h:145
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:241
Complex rc_sqrt(double x)
Definition: lo-mappers.cc:322
std::complex< double > erfi(std::complex< double > z, double relerr=0)
const octave_hdf5_id octave_H5S_ALL
octave_value map(unary_mapper_t umap) const
Definition: ov-scalar.cc:367
double erfi(double x)
Definition: lo-specfun.cc:1775
double asinh(double x)
Definition: lo-specfun.h:67
bool isna(double x)
Definition: lo-mappers.cc:45
octave_int< uint16_t > octave_uint16
Complex rc_lgamma(double x)
Definition: lo-specfun.cc:2211
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)
bool isnan(bool)
Definition: lo-mappers.h:187
std::complex< T > ceil(const std::complex< T > &x)
Definition: lo-mappers.h:112
void error(const char *fmt,...)
Definition: error.cc:578
bool isinf(double x)
Definition: lo-mappers.h:225
std::complex< T > floor(const std::complex< T > &x)
Definition: lo-mappers.h:139
static T abs(T x)
Definition: pr-output.cc:1696
#define DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA(t, n, c)
Definition: ov-base.h:180
#define SCALAR_MAPPER(UMAP, FCN)
octave_value as_uint16(void) const
Definition: ov-scalar.cc:166
Complex acos(const Complex &x)
Definition: lo-mappers.cc:83
octave_value resize(const dim_vector &dv, bool fill=false) const
Definition: ov-scalar.cc:101
Complex atan(const Complex &x)
Definition: lo-mappers.h:80
double fix(double x)
Definition: lo-mappers.h:127
Complex asin(const Complex &x)
Definition: lo-mappers.cc:105
builtin_type_t
Definition: ov-base.h:71
Complex log2(const Complex &x)
Definition: lo-mappers.cc:137
Complex erfc(const Complex &x)
Definition: lo-specfun.cc:1653
std::complex< double > erf(std::complex< double > z, double relerr=0)
virtual octave_value map(unary_mapper_t) const
Definition: ov-base.cc:1121
octave_value arg
Definition: pr-output.cc:3244
octave_value as_int16(void) const
Definition: ov-scalar.cc:142
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 const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
void warn_load(const char *type) const
Definition: ov-base.cc:1097
octave_value convert_to_str_internal(bool pad, bool force, char type) const
Definition: ov-scalar.cc:190
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:400
bool swap
Definition: load-save.cc:738
Complex rc_atanh(double x)
Definition: lo-mappers.cc:266
double lgamma(double x)
Definition: lo-specfun.h:377
mxArray * as_mxArray(void) const
Definition: ov-scalar.cc:355
Complex expm1(const Complex &x)
Definition: lo-specfun.cc:1875
nd deftypefn *std::string name
Definition: sysdep.cc:647
double acosh(double x)
Definition: lo-specfun.h:49
double gamma(double x)
Definition: lo-specfun.cc:1913
virtual octave_value convert_to_str(bool pad=false, bool force=false, char type='\'') const
Definition: ov-base.cc:375
bool save_binary(std::ostream &os, bool &save_as_floats)
Definition: ov-scalar.cc:237
double double_value(bool=false) const
Definition: ov-scalar.h:143
bool fast_elem_insert_self(void *where, builtin_type_t btyp) const
Definition: ov-scalar.cc:454
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:772
Complex log1p(const Complex &x)
Definition: lo-specfun.cc:1959
octave_value as_int64(void) const
Definition: ov-scalar.cc:154
octave_value as_double(void) const
Definition: ov-scalar.cc:124
double signum(double x)
Definition: lo-mappers.h:244
static int static_type_id(void)
Definition: ov-float.h:269
double dawson(double x)
Definition: lo-specfun.cc:1518
static octave_base_value * default_numeric_demotion_function(const octave_base_value &a)
Definition: ov-scalar.cc:67
Complex rc_log2(double x)
Definition: lo-mappers.cc:292
double tmp
Definition: data.cc:6252
octave_value retval
Definition: data.cc:6246
Complex rc_log(double x)
Definition: lo-mappers.cc:279
std::complex< double > erfcx(std::complex< double > z, double relerr=0)
int64_t octave_hdf5_id
double atanh(double x)
Definition: lo-specfun.h:72
void warn_save(const char *type) const
Definition: ov-base.cc:1106
idx type
Definition: ov.cc:3114
Complex erf(const Complex &x)
Definition: lo-specfun.cc:1638
double cbrt(double x)
Definition: lo-specfun.h:298
octave_int< uint32_t > octave_uint32
double erfcinv(double x)
Definition: lo-specfun.cc:1745
void mxArray
Definition: mex.h:55
friend class octave_value
Definition: ov-base.h:228
octave_value diag(octave_idx_type m, octave_idx_type n) const
Definition: ov-scalar.cc:184
Complex rc_acos(double x)
Definition: lo-mappers.cc:228
octave_value as_uint8(void) const
Definition: ov-scalar.cc:160
octave_value as_single(void) const
Definition: ov-scalar.cc:130
void warning(const char *fmt,...)
Definition: error.cc:801
charNDArray max(char d, const charNDArray &m)
Definition: chNDArray.cc:227
octave_value as_int8(void) const
Definition: ov-scalar.cc:136
bool save_ascii(std::ostream &os)
Definition: ov-scalar.cc:214
octave_idx_type numel(int n=0) const
Number of elements that a matrix with this dimensions would have.
Definition: dim-vector.h:362
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:253
bool isfinite(double x)
Definition: lo-mappers.h:201
double roundb(double x)
Definition: lo-mappers.h:156
Complex rc_log1p(double x)
Definition: lo-specfun.cc:2248
octave_value as_uint64(void) const
Definition: ov-scalar.cc:178
double erfinv(double x)
Definition: lo-specfun.cc:1864
bool load_hdf5(octave_hdf5_id loc_id, const char *name)
Definition: ov-scalar.cc:311
double round(double x)
Definition: lo-mappers.h:145
const octave_hdf5_id octave_H5P_DEFAULT
octave_int< int16_t > octave_int16
octave_value as_int32(void) const
Definition: ov-scalar.cc:148
std::complex< double > Complex
Definition: oct-cmplx.h:31
write the output to stdout if nargout is
Definition: load-save.cc:1612
void err_nan_to_character_conversion(void)
octave_int< uint8_t > octave_uint8
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:87
octave_int< int32_t > octave_int32
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:888
dim_vector dv
Definition: sub2ind.cc:263
double signbit(double x)
Definition: lo-mappers.h:63
octave::stream os
Definition: file-io.cc:627
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:206
std::complex< double > erfc(std::complex< double > z, double relerr=0)