GNU Octave  9.1.0
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-2024 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-fwd.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-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 #include "ov-inline.h"
58 
59 #include "ls-oct-text.h"
60 #include "ls-hdf5.h"
61 
62 // Prevent implicit instantiations on some systems (Windows, others?)
63 // that can lead to duplicate definitions of static data members.
64 
65 extern template class octave_base_scalar<float>;
66 
67 template class octave_base_scalar<double>;
68 
70 
71 static octave_base_value *
72 default_numeric_demotion_function (const octave_base_value& a)
73 {
74  const octave_scalar& v = dynamic_cast<const octave_scalar&> (a);
75 
76  return new octave_float_scalar (v.float_value ());
77 }
78 
81 {
83  (default_numeric_demotion_function,
85 }
86 
88 octave_scalar::do_index_op (const octave_value_list& idx, bool resize_ok)
89 {
90  // FIXME: this doesn't solve the problem of
91  //
92  // a = 1; a([1,1], [1,1], [1,1])
93  //
94  // and similar constructions. Hmm...
95 
96  // FIXME: using this constructor avoids narrowing the
97  // 1x1 matrix back to a scalar value. Need a better solution
98  // to this problem.
99 
100  octave_value tmp (new octave_matrix (matrix_value ()));
101 
102  return tmp.index_op (idx, resize_ok);
103 }
104 
106 octave_scalar::resize (const dim_vector& dv, bool fill) const
107 {
108  if (fill)
109  {
110  NDArray retval (dv, 0);
111 
112  if (dv.numel ())
113  retval(0) = scalar;
114 
115  return retval;
116  }
117  else
118  {
119  NDArray retval (dv);
120 
121  if (dv.numel ())
122  retval(0) = scalar;
123 
124  return retval;
125  }
126 }
127 
130 {
131  return octave_value_factory::make_copy (this);
132 }
133 
136 {
137  return scalar;
138 }
139 
142 {
143  return static_cast<float> (scalar);
144 }
145 
148 {
149  return octave_int8 (scalar);
150 }
151 
154 {
155  return octave_int16 (scalar);
156 }
157 
160 {
161  return octave_int32 (scalar);
162 }
163 
166 {
167  return octave_int64 (scalar);
168 }
169 
172 {
173  return octave_uint8 (scalar);
174 }
175 
178 {
179  return octave_uint16 (scalar);
180 }
181 
184 {
185  return octave_uint32 (scalar);
186 }
187 
190 {
191  return octave_uint64 (scalar);
192 }
193 
196 {
197  return DiagMatrix (Array<double> (dim_vector (1, 1), scalar), m, n);
198 }
199 
201 octave_scalar::convert_to_str_internal (bool, bool, char type) const
202 {
203  octave_value retval;
204 
207 
208  int ival = octave::math::nint (scalar);
209 
210  if (ival < 0 || ival > std::numeric_limits<unsigned char>::max ())
211  {
212  // FIXME: is there something better we could do?
213 
214  ival = 0;
215 
216  ::warning ("range error for conversion to character value");
217  }
218 
219  retval = octave_value (std::string (1, static_cast<char> (ival)), type);
220 
221  return retval;
222 }
223 
224 bool
225 octave_scalar::save_ascii (std::ostream& os)
226 {
227  double d = double_value ();
228 
229  octave::write_value<double> (os, d);
230 
231  os << "\n";
232 
233  return true;
234 }
235 
236 bool
237 octave_scalar::load_ascii (std::istream& is)
238 {
239  scalar = octave::read_value<double> (is);
240 
241  if (! is)
242  error ("load: failed to load scalar constant");
243 
244  return true;
245 }
246 
247 bool
248 octave_scalar::save_binary (std::ostream& os, bool /* save_as_floats */)
249 {
250  char tmp = LS_DOUBLE;
251  os.write (reinterpret_cast<char *> (&tmp), 1);
252  double dtmp = double_value ();
253  os.write (reinterpret_cast<char *> (&dtmp), 8);
254 
255  return true;
256 }
257 
258 bool
259 octave_scalar::load_binary (std::istream& is, bool swap,
261 {
262  char tmp;
263  if (! is.read (reinterpret_cast<char *> (&tmp), 1))
264  return false;
265 
266  double dtmp;
267  read_doubles (is, &dtmp, static_cast<save_type> (tmp), 1, swap, fmt);
268 
269  if (! is)
270  return false;
271 
272  scalar = dtmp;
273  return true;
274 }
275 
276 bool
277 octave_scalar::save_hdf5 (octave_hdf5_id loc_id, const char *name,
278  bool /* save_as_floats */)
279 {
280  bool retval = false;
281 
282 #if defined (HAVE_HDF5)
283 
284  hsize_t dimens[3] = {0};
285  hid_t space_hid, data_hid;
286  space_hid = data_hid = -1;
287 
288  space_hid = H5Screate_simple (0, dimens, nullptr);
289  if (space_hid < 0) return false;
290 
291 #if defined (HAVE_HDF5_18)
292  data_hid = H5Dcreate (loc_id, name, H5T_NATIVE_DOUBLE, space_hid,
295 #else
296  data_hid = H5Dcreate (loc_id, name, H5T_NATIVE_DOUBLE, space_hid,
298 #endif
299  if (data_hid < 0)
300  {
301  H5Sclose (space_hid);
302  return false;
303  }
304 
305  double tmp = double_value ();
306  retval = H5Dwrite (data_hid, H5T_NATIVE_DOUBLE, octave_H5S_ALL,
307  octave_H5S_ALL, octave_H5P_DEFAULT, &tmp) >= 0;
308 
309  H5Dclose (data_hid);
310  H5Sclose (space_hid);
311 
312 #else
313  octave_unused_parameter (loc_id);
314  octave_unused_parameter (name);
315 
316  warn_save ("hdf5");
317 #endif
318 
319  return retval;
320 }
321 
322 bool
323 octave_scalar::load_hdf5 (octave_hdf5_id loc_id, const char *name)
324 {
325 #if defined (HAVE_HDF5)
326 
327 #if defined (HAVE_HDF5_18)
328  hid_t data_hid = H5Dopen (loc_id, name, octave_H5P_DEFAULT);
329 #else
330  hid_t data_hid = H5Dopen (loc_id, name);
331 #endif
332  hid_t space_id = H5Dget_space (data_hid);
333 
334  hsize_t rank = H5Sget_simple_extent_ndims (space_id);
335 
336  if (rank != 0)
337  {
338  H5Dclose (data_hid);
339  return false;
340  }
341 
342  double dtmp;
343  if (H5Dread (data_hid, H5T_NATIVE_DOUBLE, octave_H5S_ALL, octave_H5S_ALL,
344  octave_H5P_DEFAULT, &dtmp) < 0)
345  {
346  H5Dclose (data_hid);
347  return false;
348  }
349 
350  scalar = dtmp;
351 
352  H5Dclose (data_hid);
353 
354  return true;
355 
356 #else
357  octave_unused_parameter (loc_id);
358  octave_unused_parameter (name);
359 
360  warn_load ("hdf5");
361 
362  return false;
363 #endif
364 }
365 
366 mxArray *
367 octave_scalar::as_mxArray (bool interleaved) const
368 {
369  mxArray *retval = new mxArray (interleaved, mxDOUBLE_CLASS, 1, 1, mxREAL);
370 
371  mxDouble *pd = static_cast<mxDouble *> (retval->get_data ());
372 
373  pd[0] = scalar;
374 
375  return retval;
376 }
377 
380 {
381  switch (umap)
382  {
383  case umap_imag:
384  return 0.0;
385 
386  case umap_real:
387  case umap_conj:
388  return scalar;
389 
390 #define SCALAR_MAPPER(UMAP, FCN) \
391  case umap_ ## UMAP: \
392  return octave_value (FCN (scalar))
393 
394  SCALAR_MAPPER (abs, ::fabs);
397  SCALAR_MAPPER (angle, std::arg);
398  SCALAR_MAPPER (arg, std::arg);
401  SCALAR_MAPPER (atan, ::atan);
413  SCALAR_MAPPER (ceil, ::ceil);
414  SCALAR_MAPPER (cos, ::cos);
415  SCALAR_MAPPER (cosh, ::cosh);
416  SCALAR_MAPPER (exp, ::exp);
427  SCALAR_MAPPER (sin, ::sin);
428  SCALAR_MAPPER (sinh, ::sinh);
430  SCALAR_MAPPER (tan, ::tan);
431  SCALAR_MAPPER (tanh, ::tanh);
437 
438  // Special cases for Matlab compatibility.
439  case umap_xtolower:
440  case umap_xtoupper:
441  return scalar;
442 
443  case umap_xisalnum:
444  case umap_xisalpha:
445  case umap_xisascii:
446  case umap_xiscntrl:
447  case umap_xisdigit:
448  case umap_xisgraph:
449  case umap_xislower:
450  case umap_xisprint:
451  case umap_xispunct:
452  case umap_xisspace:
453  case umap_xisupper:
454  case umap_xisxdigit:
455  {
456  octave_value str_conv = convert_to_str (true, true);
457  return str_conv.map (umap);
458  }
459 
460  default:
461  return octave_base_value::map (umap);
462  }
463 }
464 
465 bool
467 {
468 
469  // Support inline real->complex conversion.
470  if (btyp == btyp_double)
471  {
472  *(reinterpret_cast<double *> (where)) = scalar;
473  return true;
474  }
475  else if (btyp == btyp_complex)
476  {
477  *(reinterpret_cast<Complex *> (where)) = scalar;
478  return true;
479  }
480  else
481  return false;
482 }
charNDArray max(char d, const charNDArray &m)
Definition: chNDArray.cc:230
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:94
octave_idx_type numel(int n=0) const
Number of elements that a matrix with this dimensions would have.
Definition: dim-vector.h:335
void * get_data() const
Definition: mxarray.h:473
virtual octave_value map(unary_mapper_t) const
Definition: ov-base.cc:1181
virtual octave_value convert_to_str(bool pad=false, bool force=false, char type='\'') const
Definition: ov-base.cc:434
void warn_load(const char *type) const
Definition: ov-base.cc:1157
friend class octave_value
Definition: ov-base.h:269
void warn_save(const char *type) const
Definition: ov-base.cc:1166
static int static_type_id()
Definition: ov-float.h:278
octave_value map(unary_mapper_t umap) const
Definition: ov-scalar.cc:379
octave_value as_int64() const
Definition: ov-scalar.cc:165
type_conv_info numeric_demotion_function() const
Definition: ov-scalar.cc:80
octave_value as_int8() const
Definition: ov-scalar.cc:147
bool fast_elem_insert_self(void *where, builtin_type_t btyp) const
Definition: ov-scalar.cc:466
octave_value as_uint16() const
Definition: ov-scalar.cc:177
bool load_hdf5(octave_hdf5_id loc_id, const char *name)
Definition: ov-scalar.cc:323
octave_value as_uint64() const
Definition: ov-scalar.cc:189
bool load_ascii(std::istream &is)
Definition: ov-scalar.cc:237
octave_value do_index_op(const octave_value_list &idx, bool resize_ok=false)
Definition: ov-scalar.cc:88
octave_value as_uint32() const
Definition: ov-scalar.cc:183
octave_value convert_to_str_internal(bool pad, bool force, char type) const
Definition: ov-scalar.cc:201
octave_value as_double() const
Definition: ov-scalar.cc:135
double double_value(bool=false) const
Definition: ov-scalar.h:147
octave_value as_single() const
Definition: ov-scalar.cc:141
bool save_hdf5(octave_hdf5_id loc_id, const char *name, bool save_as_floats)
Definition: ov-scalar.cc:277
bool save_ascii(std::ostream &os)
Definition: ov-scalar.cc:225
octave_value as_uint8() const
Definition: ov-scalar.cc:171
octave_value diag(octave_idx_type m, octave_idx_type n) const
Definition: ov-scalar.cc:195
octave_value as_double_or_copy()
Definition: ov-scalar.cc:129
float float_value(bool=false) const
Definition: ov-scalar.h:149
Matrix matrix_value(bool=false) const
Definition: ov-scalar.h:157
bool save_binary(std::ostream &os, bool save_as_floats)
Definition: ov-scalar.cc:248
octave_value as_int16() const
Definition: ov-scalar.cc:153
bool load_binary(std::istream &is, bool swap, octave::mach_info::float_format fmt)
Definition: ov-scalar.cc:259
mxArray * as_mxArray(bool interleaved) const
Definition: ov-scalar.cc:367
octave_value resize(const dim_vector &dv, bool fill=false) const
Definition: ov-scalar.cc:106
octave_value as_int32() const
Definition: ov-scalar.cc:159
static octave_value make_copy(octave_base_value *rep)
Definition: ov-inline.h:129
octave_value index_op(const octave_value_list &idx, bool resize_ok=false)
Definition: ov.h:504
octave_value map(octave_base_value::unary_mapper_t umap) const
Definition: ov.h:1513
const octave_hdf5_id octave_H5P_DEFAULT
const octave_hdf5_id octave_H5S_ALL
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:776
save_type
Definition: data-conv.h:87
@ LS_DOUBLE
Definition: data-conv.h:95
void warning(const char *fmt,...)
Definition: error.cc:1063
void() error(const char *fmt,...)
Definition: error.cc:988
function gamma(X)
Definition: gamma.f:3
void err_nan_to_character_conversion()
Complex log2(const Complex &x)
Definition: lo-mappers.cc:141
Complex rc_acosh(double x)
Definition: lo-mappers.cc:253
Complex rc_atanh(double x)
Definition: lo-mappers.cc:278
Complex rc_log(double x)
Definition: lo-mappers.cc:291
Complex asin(const Complex &x)
Definition: lo-mappers.cc:107
int nint(double x)
Definition: lo-mappers.cc:216
Complex rc_asin(double x)
Definition: lo-mappers.cc:265
bool isna(double x)
Definition: lo-mappers.cc:47
Complex rc_acos(double x)
Definition: lo-mappers.cc:240
Complex rc_sqrt(double x)
Definition: lo-mappers.cc:334
Complex rc_log10(double x)
Definition: lo-mappers.cc:319
Complex acos(const Complex &x)
Definition: lo-mappers.cc:85
Complex rc_log2(double x)
Definition: lo-mappers.cc:304
Complex atan(const Complex &x)
Definition: lo-mappers.h:71
double roundb(double x)
Definition: lo-mappers.h:147
bool isfinite(double x)
Definition: lo-mappers.h:192
bool isinf(double x)
Definition: lo-mappers.h:203
double signbit(double x)
Definition: lo-mappers.h:54
double signum(double x)
Definition: lo-mappers.h:229
double round(double x)
Definition: lo-mappers.h:136
bool isnan(bool)
Definition: lo-mappers.h:178
std::complex< T > floor(const std::complex< T > &x)
Definition: lo-mappers.h:130
std::complex< T > ceil(const std::complex< T > &x)
Definition: lo-mappers.h:103
double fix(double x)
Definition: lo-mappers.h:118
F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
double erfinv(double x)
Definition: lo-specfun.cc:1823
double dawson(double x)
Definition: lo-specfun.cc:1467
double erfcinv(double x)
Definition: lo-specfun.cc:1697
Complex rc_log1p(double x)
Definition: lo-specfun.cc:2215
Complex rc_lgamma(double x)
Definition: lo-specfun.cc:2177
Complex expm1(const Complex &x)
Definition: lo-specfun.cc:1835
Complex log1p(const Complex &x)
Definition: lo-specfun.cc:1919
double cbrt(double x)
Definition: lo-specfun.h:289
double asinh(double x)
Definition: lo-specfun.h:58
double atanh(double x)
Definition: lo-specfun.h:63
double acosh(double x)
Definition: lo-specfun.h:40
double lgamma(double x)
Definition: lo-specfun.h:336
float_format
Definition: mach-info.h:38
void mxArray
Definition: mex.h:58
T octave_idx_type m
Definition: mx-inlines.cc:781
octave_idx_type n
Definition: mx-inlines.cc:761
@ mxDOUBLE_CLASS
Definition: mxtypes.h:64
double mxDouble
Definition: mxtypes.h:91
@ mxREAL
Definition: mxtypes.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)
std::complex< double > Complex
Definition: oct-cmplx.h:33
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:235
builtin_type_t
Definition: ov-base.h:83
@ btyp_double
Definition: ov-base.h:84
@ btyp_complex
Definition: ov-base.h:86
#define SCALAR_MAPPER(UMAP, FCN)
template int8_t abs(int8_t)