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