GNU Octave  9.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
ov-float.h
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 (octave_ov_float_h)
27 #define octave_ov_float_h 1
28 
29 #include "octave-config.h"
30 
31 #include <cstdlib>
32 
33 #include <iosfwd>
34 #include <string>
35 
36 #include "lo-ieee.h"
37 #include "lo-mappers.h"
38 #include "lo-utils.h"
39 #include "mx-base.h"
40 #include "str-vec.h"
41 
42 #include "errwarn.h"
43 #include "ov-base.h"
44 #include "ov-re-mat.h"
45 #include "ov-flt-re-mat.h"
46 #include "ov-base-scalar.h"
47 #include "ov-typeinfo.h"
48 
49 class octave_value_list;
50 
51 // Real scalar values.
52 
53 class
54 OCTINTERP_API
56 {
57 public:
58 
60  : octave_base_scalar<float> (0.0) { }
61 
63  : octave_base_scalar<float> (d) { }
64 
66  : octave_base_scalar<float> (s) { }
67 
68  ~octave_float_scalar () = default;
69 
71  { return new octave_float_scalar (*this); }
72 
73  // We return an octave_matrix here instead of an octave_float_scalar so
74  // that in expressions like A(2,2,2) = 2 (for A previously
75  // undefined), A will be empty instead of a 1x1 object.
77  { return new octave_float_matrix (); }
78 
79  octave_value do_index_op (const octave_value_list& idx,
80  bool resize_ok = false);
81 
82  octave::idx_vector index_vector (bool /* require_integers */ = false) const
83  { return octave::idx_vector (scalar); }
84 
85  octave_value any (int = 0) const
86  { return (scalar != 0 && ! lo_ieee_isnan (scalar)); }
87 
88  builtin_type_t builtin_type () const { return btyp_float; }
89 
90  bool is_real_scalar () const { return true; }
91 
92  bool isreal () const { return true; }
93 
94  bool is_single_type () const { return true; }
95 
96  bool isfloat () const { return true; }
97 
100  { return int8NDArray (dim_vector (1, 1), scalar); }
101 
104  { return int16NDArray (dim_vector (1, 1), scalar); }
105 
108  { return int32NDArray (dim_vector (1, 1), scalar); }
109 
112  { return int64NDArray (dim_vector (1, 1), scalar); }
113 
116  { return uint8NDArray (dim_vector (1, 1), scalar); }
117 
120  { return uint16NDArray (dim_vector (1, 1), scalar); }
121 
124  { return uint32NDArray (dim_vector (1, 1), scalar); }
125 
128  { return uint64NDArray (dim_vector (1, 1), scalar); }
129 
130 #define DEFINE_INT_SCALAR_VALUE(TYPE) \
131  octave_ ## TYPE \
132  TYPE ## _scalar_value () const \
133  { \
134  return octave_ ## TYPE (scalar); \
135  }
136 
145 
146 #undef DEFINE_INT_SCALAR_VALUE
147 
148  double double_value (bool = false) const
149  { return static_cast<double> (scalar); }
150 
151  float float_value (bool = false) const { return scalar; }
152 
153  double scalar_value (bool = false) const
154  { return static_cast<double> (scalar); }
155 
156  float float_scalar_value (bool = false) const { return scalar; }
157 
158  Matrix matrix_value (bool = false) const
159  { return Matrix (1, 1, scalar); }
160 
161  FloatMatrix float_matrix_value (bool = false) const
162  { return FloatMatrix (1, 1, scalar); }
163 
164  NDArray array_value (bool = false) const
165  { return NDArray (dim_vector (1, 1), scalar); }
166 
167  FloatNDArray float_array_value (bool = false) const
168  { return FloatNDArray (dim_vector (1, 1), scalar); }
169 
170  SparseMatrix sparse_matrix_value (bool = false) const
171  { return SparseMatrix (Matrix (1, 1, scalar)); }
172 
173  // FIXME: Need SparseComplexMatrix (Matrix) constructor!
175  { return SparseComplexMatrix (sparse_matrix_value ()); }
176 
177  octave_value resize (const dim_vector& dv, bool fill = false) const;
178 
179  Complex complex_value (bool = false) const { return scalar; }
180 
181  FloatComplex float_complex_value (bool = false) const { return scalar; }
182 
183  ComplexMatrix complex_matrix_value (bool = false) const
184  { return ComplexMatrix (1, 1, Complex (scalar)); }
185 
187  { return FloatComplexMatrix (1, 1, FloatComplex (scalar)); }
188 
189  ComplexNDArray complex_array_value (bool = false) const
190  { return ComplexNDArray (dim_vector (1, 1), Complex (scalar)); }
191 
193  { return FloatComplexNDArray (dim_vector (1, 1), FloatComplex (scalar)); }
194 
196  char_array_value (bool = false) const
197  {
198  charNDArray retval (dim_vector (1, 1));
199  retval(0) = static_cast<char> (scalar);
200  return retval;
201  }
202 
203  bool bool_value (bool warn = false) const
204  {
205  if (octave::math::isnan (scalar))
207  if (warn && scalar != 0 && scalar != 1)
209 
210  return scalar;
211  }
212 
213  boolNDArray bool_array_value (bool warn = false) const
214  {
215  if (octave::math::isnan (scalar))
217  if (warn && scalar != 0 && scalar != 1)
219 
220  return boolNDArray (dim_vector (1, 1), scalar);
221  }
222 
223  octave_value as_double () const;
224  octave_value as_single () const;
225 
226  octave_value as_int8 () const;
227  octave_value as_int16 () const;
228  octave_value as_int32 () const;
229  octave_value as_int64 () const;
230 
231  octave_value as_uint8 () const;
232  octave_value as_uint16 () const;
233  octave_value as_uint32 () const;
234  octave_value as_uint64 () const;
235 
236  // We don't need to override both forms of the diag method. The using
237  // declaration will avoid warnings about partially-overloaded virtual
238  // functions.
240 
242 
243  octave_value convert_to_str_internal (bool pad, bool force, char type) const;
244 
245  void increment () { ++scalar; }
246 
247  void decrement () { --scalar; }
248 
249  bool save_ascii (std::ostream& os);
250 
251  bool load_ascii (std::istream& is);
252 
253  bool save_binary (std::ostream& os, bool save_as_floats);
254 
255  bool load_binary (std::istream& is, bool swap,
257 
258  bool save_hdf5 (octave_hdf5_id loc_id, const char *name, bool save_as_floats);
259 
260  bool load_hdf5 (octave_hdf5_id loc_id, const char *name);
261 
262  int write (octave::stream& os, int block_size,
263  oct_data_conv::data_type output_type, int skip,
264  octave::mach_info::float_format flt_fmt) const
265  {
266  return os.write (array_value (), block_size, output_type,
267  skip, flt_fmt);
268  }
269 
270  mxArray * as_mxArray (bool interleaved) const;
271 
272  octave_value map (unary_mapper_t umap) const;
273 
274  bool fast_elem_insert_self (void *where, builtin_type_t btyp) const;
275 
276 private:
277 
279 };
280 
281 #endif
Definition: dMatrix.h:42
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:94
float float_scalar_value(bool=false) const
Definition: ov-float.h:156
SparseComplexMatrix sparse_complex_matrix_value(bool=false) const
Definition: ov-float.h:174
octave::idx_vector index_vector(bool=false) const
Definition: ov-float.h:82
bool isreal() const
Definition: ov-float.h:92
NDArray array_value(bool=false) const
Definition: ov-float.h:164
int16NDArray int16_array_value() const
Definition: ov-float.h:103
charNDArray char_array_value(bool=false) const
Definition: ov-float.h:196
uint16NDArray uint16_array_value() const
Definition: ov-float.h:119
int8NDArray int8_array_value() const
Definition: ov-float.h:99
int64NDArray int64_array_value() const
Definition: ov-float.h:111
FloatComplexNDArray float_complex_array_value(bool=false) const
Definition: ov-float.h:192
octave_float_scalar(const octave_float_scalar &s)
Definition: ov-float.h:65
octave_float_scalar(float d)
Definition: ov-float.h:62
bool is_single_type() const
Definition: ov-float.h:94
FloatNDArray float_array_value(bool=false) const
Definition: ov-float.h:167
int32NDArray int32_array_value() const
Definition: ov-float.h:107
int write(octave::stream &os, int block_size, oct_data_conv::data_type output_type, int skip, octave::mach_info::float_format flt_fmt) const
Definition: ov-float.h:262
float float_value(bool=false) const
Definition: ov-float.h:151
octave_base_value * empty_clone() const
Definition: ov-float.h:76
octave_base_value * clone() const
Definition: ov-float.h:70
uint64NDArray uint64_array_value() const
Definition: ov-float.h:127
builtin_type_t builtin_type() const
Definition: ov-float.h:88
FloatComplexMatrix float_complex_matrix_value(bool=false) const
Definition: ov-float.h:186
double double_value(bool=false) const
Definition: ov-float.h:148
bool isfloat() const
Definition: ov-float.h:96
boolNDArray bool_array_value(bool warn=false) const
Definition: ov-float.h:213
FloatMatrix float_matrix_value(bool=false) const
Definition: ov-float.h:161
FloatComplex float_complex_value(bool=false) const
Definition: ov-float.h:181
double scalar_value(bool=false) const
Definition: ov-float.h:153
bool bool_value(bool warn=false) const
Definition: ov-float.h:203
bool is_real_scalar() const
Definition: ov-float.h:90
octave_value any(int=0) const
Definition: ov-float.h:85
~octave_float_scalar()=default
uint8NDArray uint8_array_value() const
Definition: ov-float.h:115
Complex complex_value(bool=false) const
Definition: ov-float.h:179
ComplexNDArray complex_array_value(bool=false) const
Definition: ov-float.h:189
SparseMatrix sparse_matrix_value(bool=false) const
Definition: ov-float.h:170
ComplexMatrix complex_matrix_value(bool=false) const
Definition: ov-float.h:183
Matrix matrix_value(bool=false) const
Definition: ov-float.h:158
uint32NDArray uint32_array_value() const
Definition: ov-float.h:123
void resize(octave_idx_type n, const octave_value &rfv=octave_value())
Definition: ovl.h:117
Array< octave_value > array_value() const
Definition: ovl.h:90
void warn_logical_conversion()
Definition: errwarn.cc:365
octave::idx_vector idx_vector
Definition: idx-vector.h:1022
intNDArray< octave_int16 > int16NDArray
Definition: int16NDArray.h:36
intNDArray< octave_int32 > int32NDArray
Definition: int32NDArray.h:36
intNDArray< octave_int64 > int64NDArray
Definition: int64NDArray.h:36
intNDArray< octave_int8 > int8NDArray
Definition: int8NDArray.h:36
void err_nan_to_logical_conversion()
#define lo_ieee_isnan(x)
Definition: lo-ieee.h:116
bool isnan(bool)
Definition: lo-mappers.h:178
F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
float_format
Definition: mach-info.h:38
T octave_idx_type m
Definition: mx-inlines.cc:781
octave_idx_type n
Definition: mx-inlines.cc:761
std::complex< double > Complex
Definition: oct-cmplx.h:33
std::complex< float > FloatComplex
Definition: oct-cmplx.h:34
int64_t octave_hdf5_id
#define DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA
Definition: ov-base.h:181
builtin_type_t
Definition: ov-base.h:83
@ btyp_float
Definition: ov-base.h:85
#define DEFINE_INT_SCALAR_VALUE(TYPE)
Definition: ov-float.h:130
intNDArray< octave_uint16 > uint16NDArray
Definition: uint16NDArray.h:36
intNDArray< octave_uint32 > uint32NDArray
Definition: uint32NDArray.h:36
intNDArray< octave_uint64 > uint64NDArray
Definition: uint64NDArray.h:36
intNDArray< octave_uint8 > uint8NDArray
Definition: uint8NDArray.h:36