GNU Octave  6.2.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-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 (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 (void) = default;
69 
70  octave_base_value * clone (void) const
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  idx_vector index_vector (bool /* require_integers */ = false) const { return idx_vector (scalar); }
83 
84  octave_value any (int = 0) const
85  { return (scalar != 0 && ! lo_ieee_isnan (scalar)); }
86 
87  builtin_type_t builtin_type (void) const { return btyp_float; }
88 
89  bool is_real_scalar (void) const { return true; }
90 
91  bool isreal (void) const { return true; }
92 
93  bool is_single_type (void) const { return true; }
94 
95  bool isfloat (void) const { return true; }
96 
98  int8_array_value (void) const
99  { return int8NDArray (dim_vector (1, 1), scalar); }
100 
102  int16_array_value (void) const
103  { return int16NDArray (dim_vector (1, 1), scalar); }
104 
106  int32_array_value (void) const
107  { return int32NDArray (dim_vector (1, 1), scalar); }
108 
110  int64_array_value (void) const
111  { return int64NDArray (dim_vector (1, 1), scalar); }
112 
114  uint8_array_value (void) const
115  { return uint8NDArray (dim_vector (1, 1), scalar); }
116 
118  uint16_array_value (void) const
119  { return uint16NDArray (dim_vector (1, 1), scalar); }
120 
122  uint32_array_value (void) const
123  { return uint32NDArray (dim_vector (1, 1), scalar); }
124 
126  uint64_array_value (void) const
127  { return uint64NDArray (dim_vector (1, 1), scalar); }
128 
129 #define DEFINE_INT_SCALAR_VALUE(TYPE) \
130  octave_ ## TYPE \
131  TYPE ## _scalar_value (void) const \
132  { \
133  return octave_ ## TYPE (scalar); \
134  }
135 
144 
145 #undef DEFINE_INT_SCALAR_VALUE
146 
147  double double_value (bool = false) const
148  { return static_cast<double> (scalar); }
149 
150  float float_value (bool = false) const { return scalar; }
151 
152  double scalar_value (bool = false) const
153  { return static_cast<double> (scalar); }
154 
155  float float_scalar_value (bool = false) const { return scalar; }
156 
157  Matrix matrix_value (bool = false) const
158  { return Matrix (1, 1, scalar); }
159 
160  FloatMatrix float_matrix_value (bool = false) const
161  { return FloatMatrix (1, 1, scalar); }
162 
163  NDArray array_value (bool = false) const
164  { return NDArray (dim_vector (1, 1), scalar); }
165 
166  FloatNDArray float_array_value (bool = false) const
167  { return FloatNDArray (dim_vector (1, 1), scalar); }
168 
169  SparseMatrix sparse_matrix_value (bool = false) const
170  { return SparseMatrix (Matrix (1, 1, scalar)); }
171 
172  // FIXME: Need SparseComplexMatrix (Matrix) constructor!
174  { return SparseComplexMatrix (sparse_matrix_value ()); }
175 
176  octave_value resize (const dim_vector& dv, bool fill = false) const;
177 
178  Complex complex_value (bool = false) const { return scalar; }
179 
180  FloatComplex float_complex_value (bool = false) const { return scalar; }
181 
182  ComplexMatrix complex_matrix_value (bool = false) const
183  { return ComplexMatrix (1, 1, Complex (scalar)); }
184 
186  { return FloatComplexMatrix (1, 1, FloatComplex (scalar)); }
187 
188  ComplexNDArray complex_array_value (bool = false) const
189  { return ComplexNDArray (dim_vector (1, 1), Complex (scalar)); }
190 
192  { return FloatComplexNDArray (dim_vector (1, 1), FloatComplex (scalar)); }
193 
195  char_array_value (bool = false) const
196  {
197  charNDArray retval (dim_vector (1, 1));
198  retval(0) = static_cast<char> (scalar);
199  return retval;
200  }
201 
202  bool bool_value (bool warn = false) const
203  {
206  if (warn && scalar != 0 && scalar != 1)
208 
209  return scalar;
210  }
211 
212  boolNDArray bool_array_value (bool warn = false) const
213  {
216  if (warn && scalar != 0 && scalar != 1)
218 
219  return boolNDArray (dim_vector (1, 1), scalar);
220  }
221 
222  octave_value as_double (void) const;
223  octave_value as_single (void) const;
224 
225  octave_value as_int8 (void) const;
226  octave_value as_int16 (void) const;
227  octave_value as_int32 (void) const;
228  octave_value as_int64 (void) const;
229 
230  octave_value as_uint8 (void) const;
231  octave_value as_uint16 (void) const;
232  octave_value as_uint32 (void) const;
233  octave_value as_uint64 (void) const;
234 
235  // We don't need to override both forms of the diag method. The using
236  // declaration will avoid warnings about partially-overloaded virtual
237  // functions.
239 
241 
242  octave_value convert_to_str_internal (bool pad, bool force, char type) const;
243 
244  void increment (void) { ++scalar; }
245 
246  void decrement (void) { --scalar; }
247 
248  bool save_ascii (std::ostream& os);
249 
250  bool load_ascii (std::istream& is);
251 
252  bool save_binary (std::ostream& os, bool save_as_floats);
253 
254  bool load_binary (std::istream& is, bool swap,
256 
257  bool save_hdf5 (octave_hdf5_id loc_id, const char *name, bool save_as_floats);
258 
259  bool load_hdf5 (octave_hdf5_id loc_id, const char *name);
260 
261  int write (octave::stream& os, int block_size,
262  oct_data_conv::data_type output_type, int skip,
263  octave::mach_info::float_format flt_fmt) const
264  {
265  return os.write (array_value (), block_size, output_type,
266  skip, flt_fmt);
267  }
268 
269  mxArray * as_mxArray (void) const;
270 
271  octave_value map (unary_mapper_t umap) const;
272 
273  bool fast_elem_insert_self (void *where, builtin_type_t btyp) const;
274 
275 private:
276 
278 };
279 
280 #endif
Definition: dMatrix.h:42
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:95
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:6777
float float_scalar_value(bool=false) const
Definition: ov-float.h:155
octave_float_scalar(void)
Definition: ov-float.h:59
SparseComplexMatrix sparse_complex_matrix_value(bool=false) const
Definition: ov-float.h:173
void increment(void)
Definition: ov-float.h:244
NDArray array_value(bool=false) const
Definition: ov-float.h:163
uint64NDArray uint64_array_value(void) const
Definition: ov-float.h:126
int8NDArray int8_array_value(void) const
Definition: ov-float.h:98
uint16NDArray uint16_array_value(void) const
Definition: ov-float.h:118
charNDArray char_array_value(bool=false) const
Definition: ov-float.h:195
idx_vector index_vector(bool=false) const
Definition: ov-float.h:82
int16NDArray int16_array_value(void) const
Definition: ov-float.h:102
octave_base_value * clone(void) const
Definition: ov-float.h:70
bool is_real_scalar(void) const
Definition: ov-float.h:89
FloatComplexNDArray float_complex_array_value(bool=false) const
Definition: ov-float.h:191
octave_float_scalar(const octave_float_scalar &s)
Definition: ov-float.h:65
octave_base_value * empty_clone(void) const
Definition: ov-float.h:76
octave_float_scalar(float d)
Definition: ov-float.h:62
FloatNDArray float_array_value(bool=false) const
Definition: ov-float.h:166
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:261
float float_value(bool=false) const
Definition: ov-float.h:150
bool isfloat(void) const
Definition: ov-float.h:95
uint32NDArray uint32_array_value(void) const
Definition: ov-float.h:122
int32NDArray int32_array_value(void) const
Definition: ov-float.h:106
int64NDArray int64_array_value(void) const
Definition: ov-float.h:110
uint8NDArray uint8_array_value(void) const
Definition: ov-float.h:114
void decrement(void)
Definition: ov-float.h:246
~octave_float_scalar(void)=default
bool isreal(void) const
Definition: ov-float.h:91
FloatComplexMatrix float_complex_matrix_value(bool=false) const
Definition: ov-float.h:185
double double_value(bool=false) const
Definition: ov-float.h:147
boolNDArray bool_array_value(bool warn=false) const
Definition: ov-float.h:212
FloatMatrix float_matrix_value(bool=false) const
Definition: ov-float.h:160
FloatComplex float_complex_value(bool=false) const
Definition: ov-float.h:180
builtin_type_t builtin_type(void) const
Definition: ov-float.h:87
double scalar_value(bool=false) const
Definition: ov-float.h:152
bool bool_value(bool warn=false) const
Definition: ov-float.h:202
bool is_single_type(void) const
Definition: ov-float.h:93
octave_value any(int=0) const
Definition: ov-float.h:84
Complex complex_value(bool=false) const
Definition: ov-float.h:178
ComplexNDArray complex_array_value(bool=false) const
Definition: ov-float.h:188
SparseMatrix sparse_matrix_value(bool=false) const
Definition: ov-float.h:169
ComplexMatrix complex_matrix_value(bool=false) const
Definition: ov-float.h:182
Matrix matrix_value(bool=false) const
Definition: ov-float.h:157
void resize(octave_idx_type n, const octave_value &rfv=octave_value())
Definition: ovl.h:117
Array< octave_value > array_value(void) const
Definition: ovl.h:90
void warn_logical_conversion(void)
Definition: errwarn.cc:365
QString name
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
#define lo_ieee_isnan(x)
Definition: lo-ieee.h:112
F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
T octave_idx_type m
Definition: mx-inlines.cc:773
octave_idx_type n
Definition: mx-inlines.cc:753
bool isnan(bool)
Definition: lo-mappers.h:178
void err_nan_to_logical_conversion(void)
std::complex< double > Complex
Definition: oct-cmplx.h:33
std::complex< float > FloatComplex
Definition: oct-cmplx.h:34
int64_t octave_hdf5_id
static double as_double(time_t sec, long usec)
Definition: oct-time.h:37
#define DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA
Definition: ov-base.h:158
builtin_type_t
Definition: ov-base.h:72
@ btyp_float
Definition: ov-base.h:74
#define DEFINE_INT_SCALAR_VALUE(TYPE)
Definition: ov-float.h:129
static bool scalar(const dim_vector &dims)
Definition: ov-struct.cc:669
octave_value::octave_value(const Array< char > &chm, char type) return retval
Definition: ov.cc:811
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