GNU Octave  9.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
ov-scalar.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_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-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-base-scalar.h"
46 #include "ov-typeinfo.h"
47 
48 class octave_value_list;
49 
50 // Real scalar values.
51 
52 class
53 OCTINTERP_API
55 {
56 public:
57 
59  : octave_base_scalar<double> (0.0) { }
60 
61  octave_scalar (double d)
62  : octave_base_scalar<double> (d) { }
63 
65  : octave_base_scalar<double> (s) { }
66 
67  ~octave_scalar () = default;
68 
69  octave_base_value * clone () const { return new octave_scalar (*this); }
70 
71  // We return an octave_matrix here instead of an octave_scalar so
72  // that in expressions like A(2,2,2) = 2 (for A previously
73  // undefined), A will be empty instead of a 1x1 object.
74  octave_base_value * empty_clone () const { return new octave_matrix (); }
75 
76  octave_value do_index_op (const octave_value_list& idx,
77  bool resize_ok = false);
78 
79  type_conv_info numeric_demotion_function () const;
80 
81  octave::idx_vector index_vector (bool /* require_integers */ = false) const
82  { return octave::idx_vector (scalar); }
83 
84  octave_value any (int = 0) const
85  { return (scalar != 0 && ! lo_ieee_isnan (scalar)); }
86 
88 
89  bool is_real_scalar () const { return true; }
90 
91  bool isreal () const { return true; }
92 
93  bool is_double_type () const { return true; }
94 
95  bool isfloat () const { return true; }
96 
99  { return int8NDArray (dim_vector (1, 1), scalar); }
100 
103  { return int16NDArray (dim_vector (1, 1), scalar); }
104 
107  { return int32NDArray (dim_vector (1, 1), scalar); }
108 
111  { return int64NDArray (dim_vector (1, 1), scalar); }
112 
115  { return uint8NDArray (dim_vector (1, 1), scalar); }
116 
119  { return uint16NDArray (dim_vector (1, 1), scalar); }
120 
123  { return uint32NDArray (dim_vector (1, 1), scalar); }
124 
127  { return uint64NDArray (dim_vector (1, 1), scalar); }
128 
129 #define DEFINE_INT_SCALAR_VALUE(TYPE) \
130  octave_ ## TYPE \
131  TYPE ## _scalar_value () const \
132  { \
133  return octave_ ## TYPE (scalar); \
134  }
135 
144 
145 #undef DEFINE_INT_SCALAR_VALUE
146 
147  double double_value (bool = false) const { return scalar; }
148 
149  float float_value (bool = false) const
150  { return static_cast<float> (scalar); }
151 
152  double scalar_value (bool = false) const { return scalar; }
153 
154  float float_scalar_value (bool = false) const
155  { return float_value (); }
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, float_value ()); }
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), float_value ()); }
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
181  { return FloatComplex (float_value ()); }
182 
183  ComplexMatrix complex_matrix_value (bool = false) const
184  { return ComplexMatrix (1, 1, Complex (scalar)); }
185 
187  { return FloatComplexMatrix (1, 1, FloatComplex (float_value ())); }
188 
189  ComplexNDArray complex_array_value (bool = false) const
190  { return ComplexNDArray (dim_vector (1, 1), Complex (scalar)); }
191 
193  {
194  return FloatComplexNDArray (dim_vector (1, 1),
195  FloatComplex (float_value ()));
196  }
197 
199  char_array_value (bool = false) const
200  {
201  charNDArray retval (dim_vector (1, 1));
202  retval(0) = static_cast<char> (scalar);
203  return retval;
204  }
205 
206  bool bool_value (bool warn = false) const
207  {
208  if (octave::math::isnan (scalar))
210  if (warn && scalar != 0 && scalar != 1)
212 
213  return scalar;
214  }
215 
216  boolNDArray bool_array_value (bool warn = false) const
217  {
218  if (octave::math::isnan (scalar))
220  if (warn && scalar != 0 && scalar != 1)
222 
223  return boolNDArray (dim_vector (1, 1), scalar);
224  }
225 
226  octave_value as_double_or_copy ();
227 
228  bool maybe_update_double (double d)
229  {
230  if (m_count != 1)
231  return false;
232  scalar = d;
233  return true;
234  }
235 
236  octave_value as_double () const;
237  octave_value as_single () const;
238 
239  octave_value as_int8 () const;
240  octave_value as_int16 () const;
241  octave_value as_int32 () const;
242  octave_value as_int64 () const;
243 
244  octave_value as_uint8 () const;
245  octave_value as_uint16 () const;
246  octave_value as_uint32 () const;
247  octave_value as_uint64 () const;
248 
249  // We don't need to override both forms of the diag method. The using
250  // declaration will avoid warnings about partially-overloaded virtual
251  // functions.
253 
255 
256  octave_value convert_to_str_internal (bool pad, bool force, char type) const;
257 
258  void increment () { ++scalar; }
259 
260  void decrement () { --scalar; }
261 
262  bool save_ascii (std::ostream& os);
263 
264  bool load_ascii (std::istream& is);
265 
266  bool save_binary (std::ostream& os, bool save_as_floats);
267 
268  bool load_binary (std::istream& is, bool swap,
270 
271  bool save_hdf5 (octave_hdf5_id loc_id, const char *name, bool save_as_floats);
272 
273  bool load_hdf5 (octave_hdf5_id loc_id, const char *name);
274 
275  int write (octave::stream& os, int block_size,
276  oct_data_conv::data_type output_type, int skip,
277  octave::mach_info::float_format flt_fmt) const
278  {
279  return os.write (array_value (), block_size, output_type,
280  skip, flt_fmt);
281  }
282 
283  mxArray * as_mxArray (bool interleaved) const;
284 
285  octave_value map (unary_mapper_t umap) const;
286 
287  bool fast_elem_insert_self (void *where, builtin_type_t btyp) const;
288 
289 private:
290 
292 };
293 
294 #endif
Definition: dMatrix.h:42
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:94
FloatComplexMatrix float_complex_matrix_value(bool=false) const
Definition: ov-scalar.h:186
octave::idx_vector index_vector(bool=false) const
Definition: ov-scalar.h:81
FloatMatrix float_matrix_value(bool=false) const
Definition: ov-scalar.h:160
FloatComplex float_complex_value(bool=false) const
Definition: ov-scalar.h:180
octave_base_value * empty_clone() const
Definition: ov-scalar.h:74
builtin_type_t builtin_type() const
Definition: ov-scalar.h:87
uint16NDArray uint16_array_value() const
Definition: ov-scalar.h:118
uint64NDArray uint64_array_value() const
Definition: ov-scalar.h:126
boolNDArray bool_array_value(bool warn=false) const
Definition: ov-scalar.h:216
int16NDArray int16_array_value() const
Definition: ov-scalar.h:102
FloatComplexNDArray float_complex_array_value(bool=false) const
Definition: ov-scalar.h:192
bool isfloat() const
Definition: ov-scalar.h:95
octave_value any(int=0) const
Definition: ov-scalar.h:84
double double_value(bool=false) const
Definition: ov-scalar.h:147
int32NDArray int32_array_value() const
Definition: ov-scalar.h:106
bool maybe_update_double(double d)
Definition: ov-scalar.h:228
FloatNDArray float_array_value(bool=false) const
Definition: ov-scalar.h:166
void decrement()
Definition: ov-scalar.h:260
ComplexMatrix complex_matrix_value(bool=false) const
Definition: ov-scalar.h:183
NDArray array_value(bool=false) const
Definition: ov-scalar.h:163
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:275
octave_scalar(const octave_scalar &s)
Definition: ov-scalar.h:64
octave_base_value * clone() const
Definition: ov-scalar.h:69
bool is_real_scalar() const
Definition: ov-scalar.h:89
uint8NDArray uint8_array_value() const
Definition: ov-scalar.h:114
float float_value(bool=false) const
Definition: ov-scalar.h:149
SparseMatrix sparse_matrix_value(bool=false) const
Definition: ov-scalar.h:169
Matrix matrix_value(bool=false) const
Definition: ov-scalar.h:157
uint32NDArray uint32_array_value() const
Definition: ov-scalar.h:122
void increment()
Definition: ov-scalar.h:258
charNDArray char_array_value(bool=false) const
Definition: ov-scalar.h:199
double scalar_value(bool=false) const
Definition: ov-scalar.h:152
bool is_double_type() const
Definition: ov-scalar.h:93
Complex complex_value(bool=false) const
Definition: ov-scalar.h:178
float float_scalar_value(bool=false) const
Definition: ov-scalar.h:154
SparseComplexMatrix sparse_complex_matrix_value(bool=false) const
Definition: ov-scalar.h:173
int64NDArray int64_array_value() const
Definition: ov-scalar.h:110
bool isreal() const
Definition: ov-scalar.h:91
~octave_scalar()=default
octave_scalar(double d)
Definition: ov-scalar.h:61
int8NDArray int8_array_value() const
Definition: ov-scalar.h:98
ComplexNDArray complex_array_value(bool=false) const
Definition: ov-scalar.h:189
bool bool_value(bool warn=false) const
Definition: ov-scalar.h:206
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_double
Definition: ov-base.h:84
#define DEFINE_INT_SCALAR_VALUE(TYPE)
Definition: ov-scalar.h:129
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