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