GNU Octave 10.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
ov-float.cc
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 1996-2025 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-flt-re-mat.h"
50#include "ov-typeinfo.h"
51#include "pr-output.h"
52#include "xdiv.h"
53#include "xpow.h"
54#include "ops.h"
55
56#include "ls-oct-text.h"
57#include "ls-hdf5.h"
58
59
61 "single");
62
65{
66 // FIXME: this doesn't solve the problem of
67 //
68 // a = 1; a([1,1], [1,1], [1,1])
69 //
70 // and similar constructions. Hmm...
71
72 // FIXME: using this constructor avoids narrowing the
73 // 1x1 matrix back to a scalar value. Need a better solution
74 // to this problem.
75
77
78 return tmp.index_op (idx, resize_ok);
79}
80
82octave_float_scalar::resize (const dim_vector& dv, bool fill) const
83{
84 if (fill)
85 {
86 FloatNDArray retval (dv, 0);
87
88 if (dv.numel ())
89 retval(0) = scalar;
90
91 return retval;
92 }
93 else
94 {
95 FloatNDArray retval (dv);
96
97 if (dv.numel ())
98 retval(0) = scalar;
99
100 return retval;
101 }
102}
103
106{
107 return static_cast<double> (scalar);
108}
109
112{
113 return scalar;
114}
115
118{
119 return octave_int8 (scalar);
120}
121
124{
125 return octave_int16 (scalar);
126}
127
130{
131 return octave_int32 (scalar);
132}
133
136{
137 return octave_int64 (scalar);
138}
139
142{
143 return octave_uint8 (scalar);
144}
145
148{
149 return octave_uint16 (scalar);
150}
151
154{
155 return octave_uint32 (scalar);
156}
157
160{
161 return octave_uint64 (scalar);
162}
163
169
172{
173 octave_value retval;
174
175 if (octave::math::isnan (scalar))
176 octave::err_nan_to_character_conversion ();
177
178 int ival = octave::math::nint (scalar);
179
180 if (ival < 0 || ival > std::numeric_limits<unsigned char>::max ())
181 {
182 // FIXME: is there something better we could do?
183
184 ival = 0;
185
186 ::warning ("range error for conversion to character value");
187 }
188
189 retval = octave_value (std::string (1, static_cast<char> (ival)), type);
190
191 return retval;
192}
193
194bool
196{
197 float d = float_value ();
198
199 octave::write_value<float> (os, d);
200
201 os << "\n";
202
203 return true;
204}
205
206bool
208{
209 scalar = octave::read_value<float> (is);
210 if (! is)
211 error ("load: failed to load scalar constant");
212
213 return true;
214}
215
216bool
217octave_float_scalar::save_binary (std::ostream& os, bool /* save_as_floats */)
218{
219 char tmp = LS_FLOAT;
220 os.write (reinterpret_cast<char *> (&tmp), 1);
221 float dtmp = float_value ();
222 os.write (reinterpret_cast<char *> (&dtmp), 4);
223
224 return true;
225}
226
227bool
228octave_float_scalar::load_binary (std::istream& is, bool swap,
229 octave::mach_info::float_format fmt)
230{
231 char tmp;
232 if (! is.read (reinterpret_cast<char *> (&tmp), 1))
233 return false;
234
235 float dtmp;
236 read_floats (is, &dtmp, static_cast<save_type> (tmp), 1, swap, fmt);
237
238 if (! is)
239 return false;
240
241 scalar = dtmp;
242 return true;
243}
244
245bool
247 bool /* save_as_floats */)
248{
249 bool retval = false;
250
251#if defined (HAVE_HDF5)
252
253 hsize_t dimens[3] = {0};
254 hid_t space_hid, data_hid;
255 space_hid = data_hid = -1;
256
257 space_hid = H5Screate_simple (0, dimens, nullptr);
258 if (space_hid < 0) return false;
259#if defined (HAVE_HDF5_18)
260 data_hid = H5Dcreate (loc_id, name, H5T_NATIVE_FLOAT, space_hid,
263#else
264 data_hid = H5Dcreate (loc_id, name, H5T_NATIVE_FLOAT, space_hid,
266#endif
267 if (data_hid < 0)
268 {
269 H5Sclose (space_hid);
270 return false;
271 }
272
273 float tmp = float_value ();
274 retval = H5Dwrite (data_hid, H5T_NATIVE_FLOAT, octave_H5S_ALL, octave_H5S_ALL,
275 octave_H5P_DEFAULT, &tmp) >= 0;
276
277 H5Dclose (data_hid);
278 H5Sclose (space_hid);
279
280#else
281 octave_unused_parameter (loc_id);
282 octave_unused_parameter (name);
283
284 warn_save ("hdf5");
285#endif
286
287 return retval;
288}
289
290bool
292{
293#if defined (HAVE_HDF5)
294
295#if defined (HAVE_HDF5_18)
296 hid_t data_hid = H5Dopen (loc_id, name, octave_H5P_DEFAULT);
297#else
298 hid_t data_hid = H5Dopen (loc_id, name);
299#endif
300 hid_t space_id = H5Dget_space (data_hid);
301
302 hsize_t rank = H5Sget_simple_extent_ndims (space_id);
303
304 if (rank != 0)
305 {
306 H5Dclose (data_hid);
307 return false;
308 }
309
310 float dtmp;
311 if (H5Dread (data_hid, H5T_NATIVE_FLOAT, octave_H5S_ALL, octave_H5S_ALL,
312 octave_H5P_DEFAULT, &dtmp) < 0)
313 {
314 H5Dclose (data_hid);
315 return false;
316 }
317
318 scalar = dtmp;
319
320 H5Dclose (data_hid);
321
322 return true;
323
324#else
325 octave_unused_parameter (loc_id);
326 octave_unused_parameter (name);
327
328 warn_load ("hdf5");
329
330 return false;
331#endif
332}
333
334mxArray *
335octave_float_scalar::as_mxArray (bool interleaved) const
336{
337 mxArray *retval = new mxArray (interleaved, mxSINGLE_CLASS, 1, 1, mxREAL);
338
339 mxSingle *pd = static_cast<mxSingle *> (retval->get_data ());
340
341 pd[0] = scalar;
342
343 return retval;
344}
345
348{
349 switch (umap)
350 {
351 case umap_imag:
352 return 0.0f;
353
354 case umap_real:
355 case umap_conj:
356 return scalar;
357
358#define SCALAR_MAPPER(UMAP, FCN) \
359 case umap_ ## UMAP: \
360 return octave_value (FCN (scalar))
361
362 SCALAR_MAPPER (abs, ::fabsf);
363 SCALAR_MAPPER (acos, octave::math::rc_acos);
364 SCALAR_MAPPER (acosh, octave::math::rc_acosh);
365 SCALAR_MAPPER (angle, std::arg);
366 SCALAR_MAPPER (arg, std::arg);
367 SCALAR_MAPPER (asin, octave::math::rc_asin);
368 SCALAR_MAPPER (asinh, octave::math::asinh);
369 SCALAR_MAPPER (atan, ::atanf);
370 SCALAR_MAPPER (atanh, octave::math::rc_atanh);
371 SCALAR_MAPPER (erf, octave::math::erf);
372 SCALAR_MAPPER (erfinv, octave::math::erfinv);
373 SCALAR_MAPPER (erfcinv, octave::math::erfcinv);
374 SCALAR_MAPPER (erfc, octave::math::erfc);
375 SCALAR_MAPPER (erfcx, octave::math::erfcx);
376 SCALAR_MAPPER (erfi, octave::math::erfi);
377 SCALAR_MAPPER (dawson, octave::math::dawson);
378 SCALAR_MAPPER (gamma, octave::math::gamma);
379 SCALAR_MAPPER (lgamma, octave::math::rc_lgamma);
380 SCALAR_MAPPER (cbrt, octave::math::cbrt);
381 SCALAR_MAPPER (ceil, ::ceilf);
382 SCALAR_MAPPER (cos, ::cosf);
383 SCALAR_MAPPER (cosh, ::coshf);
384 SCALAR_MAPPER (exp, ::expf);
385 SCALAR_MAPPER (expm1, octave::math::expm1);
386 SCALAR_MAPPER (fix, octave::math::fix);
387 SCALAR_MAPPER (floor, std::floor);
388 SCALAR_MAPPER (log, octave::math::rc_log);
389 SCALAR_MAPPER (log2, octave::math::rc_log2);
390 SCALAR_MAPPER (log10, octave::math::rc_log10);
391 SCALAR_MAPPER (log1p, octave::math::rc_log1p);
392 SCALAR_MAPPER (round, octave::math::round);
393 SCALAR_MAPPER (roundb, octave::math::roundb);
394 SCALAR_MAPPER (signum, octave::math::signum);
395 SCALAR_MAPPER (sin, ::sinf);
396 SCALAR_MAPPER (sinh, ::sinhf);
397 SCALAR_MAPPER (sqrt, octave::math::rc_sqrt);
398 SCALAR_MAPPER (tan, ::tanf);
399 SCALAR_MAPPER (tanh, ::tanhf);
400 SCALAR_MAPPER (isfinite, octave::math::isfinite);
401 SCALAR_MAPPER (isinf, octave::math::isinf);
402 SCALAR_MAPPER (isna, octave::math::isna);
403 SCALAR_MAPPER (isnan, octave::math::isnan);
404 SCALAR_MAPPER (xsignbit, octave::math::signbit);
405
406 // Special cases for Matlab compatibility.
407 case umap_xtolower:
408 case umap_xtoupper:
409 return scalar;
410
411 case umap_xisalnum:
412 case umap_xisalpha:
413 case umap_xisascii:
414 case umap_xiscntrl:
415 case umap_xisdigit:
416 case umap_xisgraph:
417 case umap_xislower:
418 case umap_xisprint:
419 case umap_xispunct:
420 case umap_xisspace:
421 case umap_xisupper:
422 case umap_xisxdigit:
423 {
424 octave_value str_conv = convert_to_str (true, true);
425 return str_conv.map (umap);
426 }
427
428 default:
429 return octave_base_value::map (umap);
430 }
431}
432
433bool
435 builtin_type_t btyp) const
436{
437
438 // Support inline real->complex conversion.
439 if (btyp == btyp_float)
440 {
441 *(reinterpret_cast<float *> (where)) = scalar;
442 return true;
443 }
444 else if (btyp == btyp_float_complex)
445 {
446 *(reinterpret_cast<FloatComplex *> (where)) = scalar;
447 return true;
448 }
449 else
450 return false;
451}
N Dimensional Array with copy-on-write semantics.
Definition Array.h:130
Vector representing the dimensions (size) of an Array.
Definition dim-vector.h:90
octave_idx_type numel(int n=0) const
Number of elements that a matrix with this dimensions would have.
Definition dim-vector.h:331
void * get_data() const
Definition mxarray.h:482
virtual octave_value map(unary_mapper_t) const
Definition ov-base.cc:1170
virtual octave_value convert_to_str(bool pad=false, bool force=false, char type='\'') const
Definition ov-base.cc:431
void warn_load(const char *type) const
Definition ov-base.cc:1152
friend class octave_value
Definition ov-base.h:278
void warn_save(const char *type) const
Definition ov-base.cc:1161
octave_value as_uint8() const
Definition ov-float.cc:141
bool save_ascii(std::ostream &os)
Definition ov-float.cc:195
octave_value as_int16() const
Definition ov-float.cc:123
octave_value as_int64() const
Definition ov-float.cc:135
bool load_hdf5(octave_hdf5_id loc_id, const char *name)
Definition ov-float.cc:291
bool fast_elem_insert_self(void *where, builtin_type_t btyp) const
Definition ov-float.cc:434
mxArray * as_mxArray(bool interleaved) const
Definition ov-float.cc:335
octave_value as_uint32() const
Definition ov-float.cc:153
octave_value as_uint64() const
Definition ov-float.cc:159
octave_value convert_to_str_internal(bool pad, bool force, char type) const
Definition ov-float.cc:171
octave_value diag(octave_idx_type m, octave_idx_type n) const
Definition ov-float.cc:165
octave_value as_int8() const
Definition ov-float.cc:117
octave_value as_single() const
Definition ov-float.cc:111
float float_value(bool=false) const
Definition ov-float.h:150
octave_value resize(const dim_vector &dv, bool fill=false) const
Definition ov-float.cc:82
octave_value map(unary_mapper_t umap) const
Definition ov-float.cc:347
octave_value as_double() const
Definition ov-float.cc:105
octave_value as_uint16() const
Definition ov-float.cc:147
bool load_binary(std::istream &is, bool swap, octave::mach_info::float_format fmt)
Definition ov-float.cc:228
octave_value as_int32() const
Definition ov-float.cc:129
FloatMatrix float_matrix_value(bool=false) const
Definition ov-float.h:160
bool save_binary(std::ostream &os, bool save_as_floats)
Definition ov-float.cc:217
octave_value do_index_op(const octave_value_list &idx, bool resize_ok=false)
Definition ov-float.cc:64
bool load_ascii(std::istream &is)
Definition ov-float.cc:207
bool save_hdf5(octave_hdf5_id loc_id, const char *name, bool save_as_floats)
Definition ov-float.cc:246
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:1528
const octave_hdf5_id octave_H5P_DEFAULT
const octave_hdf5_id octave_H5S_ALL
void read_floats(std::istream &is, float *data, save_type type, octave_idx_type len, bool swap, octave::mach_info::float_format fmt)
Definition data-conv.cc:862
save_type
Definition data-conv.h:85
@ LS_FLOAT
Definition data-conv.h:92
void warning(const char *fmt,...)
Definition error.cc:1078
void error(const char *fmt,...)
Definition error.cc:1003
function gamma(x)
Definition gamma.f:3
Complex log2(const Complex &x)
Complex asin(const Complex &x)
bool isna(double x)
Definition lo-mappers.cc:47
Complex acos(const Complex &x)
Definition lo-mappers.cc:85
std::complex< T > ceil(const std::complex< T > &x)
Definition lo-mappers.h:103
Complex atan(const Complex &x)
Definition lo-mappers.h:71
double roundb(double x)
Definition lo-mappers.h:147
std::complex< T > floor(const std::complex< T > &x)
Definition lo-mappers.h:130
bool isfinite(double x)
Definition lo-mappers.h:192
bool isinf(double x)
Definition lo-mappers.h:203
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
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)
double erfi(double x)
double dawson(double x)
Complex erf(const Complex &x)
double erfcinv(double x)
double erfcx(double x)
Complex expm1(const Complex &x)
Complex log1p(const Complex &x)
Complex erfc(const Complex &x)
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
std::complex< float > FloatComplex
Definition oct-cmplx.h:34
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:246
builtin_type_t
Definition ov-base.h:83
@ btyp_float_complex
Definition ov-base.h:87
@ btyp_float
Definition ov-base.h:85
#define SCALAR_MAPPER(UMAP, FCN)
template int8_t abs(int8_t)