GNU Octave  4.0.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ov-scalar.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1996-2015 John W. Eaton
4 
5 This file is part of Octave.
6 
7 Octave is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if !defined (octave_ov_scalar_h)
24 #define octave_ov_scalar_h 1
25 
26 #include <cstdlib>
27 
28 #include <iosfwd>
29 #include <string>
30 
31 #include "lo-ieee.h"
32 #include "lo-mappers.h"
33 #include "lo-utils.h"
34 #include "mx-base.h"
35 #include "str-vec.h"
36 
37 #include "gripes.h"
38 #include "ov-base.h"
39 #include "ov-re-mat.h"
40 #include "ov-base-scalar.h"
41 #include "ov-typeinfo.h"
42 
43 class octave_value_list;
44 
45 class tree_walker;
46 
47 // Real scalar values.
48 
49 class
52 {
53 public:
54 
56  : octave_base_scalar<double> (0.0) { }
57 
58  octave_scalar (double d)
59  : octave_base_scalar<double> (d) { }
60 
62  : octave_base_scalar<double> (s) { }
63 
64  ~octave_scalar (void) { }
65 
66  octave_base_value *clone (void) const { return new octave_scalar (*this); }
67 
68  // We return an octave_matrix here instead of an octave_scalar so
69  // that in expressions like A(2,2,2) = 2 (for A previously
70  // undefined), A will be empty instead of a 1x1 object.
71  octave_base_value *empty_clone (void) const { return new octave_matrix (); }
72 
73  octave_value do_index_op (const octave_value_list& idx,
74  bool resize_ok = false);
75 
76  type_conv_info numeric_demotion_function (void) const;
77 
78  idx_vector index_vector (bool /* require_integers */ = false) const { return idx_vector (scalar); }
79 
80  octave_value any (int = 0) const
81  { return (scalar != 0 && ! lo_ieee_isnan (scalar)); }
82 
83  builtin_type_t builtin_type (void) const { return btyp_double; }
84 
85  bool is_real_scalar (void) const { return true; }
86 
87  bool is_real_type (void) const { return true; }
88 
89  bool is_double_type (void) const { return true; }
90 
91  bool is_float_type (void) const { return true; }
92 
94  int8_array_value (void) const
95  { return int8NDArray (dim_vector (1, 1), scalar); }
96 
98  int16_array_value (void) const
99  { return int16NDArray (dim_vector (1, 1), scalar); }
100 
102  int32_array_value (void) const
103  { return int32NDArray (dim_vector (1, 1), scalar); }
104 
106  int64_array_value (void) const
107  { return int64NDArray (dim_vector (1, 1), scalar); }
108 
110  uint8_array_value (void) const
111  { return uint8NDArray (dim_vector (1, 1), scalar); }
112 
114  uint16_array_value (void) const
115  { return uint16NDArray (dim_vector (1, 1), scalar); }
116 
118  uint32_array_value (void) const
119  { return uint32NDArray (dim_vector (1, 1), scalar); }
120 
122  uint64_array_value (void) const
123  { return uint64NDArray (dim_vector (1, 1), scalar); }
124 
125 #define DEFINE_INT_SCALAR_VALUE(TYPE) \
126  octave_ ## TYPE \
127  TYPE ## _scalar_value (void) const \
128  { return octave_ ## TYPE (scalar); }
129 
138 
139 #undef DEFINE_INT_SCALAR_VALUE
140 
141  double double_value (bool = false) const { return scalar; }
142 
143  float float_value (bool = false) const
144  { return static_cast<float> (scalar); }
145 
146  double scalar_value (bool = false) const { return scalar; }
147 
148  float float_scalar_value (bool = false) const
149  { return static_cast<float> (scalar); }
150 
151  Matrix matrix_value (bool = false) const
152  { return Matrix (1, 1, scalar); }
153 
154  FloatMatrix float_matrix_value (bool = false) const
155  { return FloatMatrix (1, 1, scalar); }
156 
157  NDArray array_value (bool = false) const
158  { return NDArray (dim_vector (1, 1), scalar); }
159 
160  FloatNDArray float_array_value (bool = false) const
161  { return FloatNDArray (dim_vector (1, 1), scalar); }
162 
163  SparseMatrix sparse_matrix_value (bool = false) const
164  { return SparseMatrix (Matrix (1, 1, scalar)); }
165 
166  // FIXME Need SparseComplexMatrix (Matrix) constructor!!!
168  { return SparseComplexMatrix (sparse_matrix_value ()); }
169 
170  octave_value resize (const dim_vector& dv, bool fill = false) const;
171 
172  Complex complex_value (bool = false) const { return scalar; }
173 
174  FloatComplex float_complex_value (bool = false) const { return scalar; }
175 
176  ComplexMatrix complex_matrix_value (bool = false) const
177  { return ComplexMatrix (1, 1, Complex (scalar)); }
178 
180  { return FloatComplexMatrix (1, 1, FloatComplex (scalar)); }
181 
182  ComplexNDArray complex_array_value (bool = false) const
183  { return ComplexNDArray (dim_vector (1, 1), Complex (scalar)); }
184 
186  { return FloatComplexNDArray (dim_vector (1, 1), FloatComplex (scalar)); }
187 
189  char_array_value (bool = false) const
190  {
191  charNDArray retval (dim_vector (1, 1));
192  retval(0) = static_cast<char> (scalar);
193  return retval;
194  }
195 
196  bool bool_value (bool warn = false) const
197  {
198  if (xisnan (scalar))
200  else if (warn && scalar != 0 && scalar != 1)
202 
203  return scalar;
204  }
205 
206  boolNDArray bool_array_value (bool warn = false) const
207  {
208  if (xisnan (scalar))
210  else if (warn && scalar != 0 && scalar != 1)
212 
213  return boolNDArray (dim_vector (1, 1), scalar);
214  }
215 
216  octave_value diag (octave_idx_type m, octave_idx_type n) const;
217 
218  octave_value convert_to_str_internal (bool pad, bool force, char type) const;
219 
220  void increment (void) { ++scalar; }
221 
222  void decrement (void) { --scalar; }
223 
224  bool save_ascii (std::ostream& os);
225 
226  bool load_ascii (std::istream& is);
227 
228  bool save_binary (std::ostream& os, bool& save_as_floats);
229 
230  bool load_binary (std::istream& is, bool swap,
232 
233  bool save_hdf5 (octave_hdf5_id loc_id, const char *name, bool save_as_floats);
234 
235  bool load_hdf5 (octave_hdf5_id loc_id, const char *name);
236 
237  int write (octave_stream& os, int block_size,
238  oct_data_conv::data_type output_type, int skip,
239  oct_mach_info::float_format flt_fmt) const
240  {
241  return os.write (array_value (), block_size, output_type,
242  skip, flt_fmt);
243  }
244 
245  mxArray *as_mxArray (void) const;
246 
247  octave_value map (unary_mapper_t umap) const;
248 
249  bool fast_elem_insert_self (void *where, builtin_type_t btyp) const;
250 
251 private:
252 
253 
255 };
256 
257 #endif
Matrix matrix_value(bool=false) const
Definition: ov-scalar.h:151
octave_scalar(void)
Definition: ov-scalar.h:55
intNDArray< octave_int64 > int64NDArray
Definition: int64NDArray.h:31
bool is_double_type(void) const
Definition: ov-scalar.h:89
ComplexNDArray complex_array_value(bool=false) const
Definition: ov-scalar.h:182
Complex complex_value(bool=false) const
Definition: ov-scalar.h:172
bool xisnan(double x)
Definition: lo-mappers.cc:144
octave_scalar(double d)
Definition: ov-scalar.h:58
builtin_type_t builtin_type(void) const
Definition: ov-scalar.h:83
double scalar_value(bool=false) const
Definition: ov-scalar.h:146
idx_vector index_vector(bool=false) const
Definition: ov-scalar.h:78
#define DEFINE_INT_SCALAR_VALUE(TYPE)
Definition: ov-scalar.h:125
intNDArray< octave_uint32 > uint32NDArray
Definition: uint32NDArray.h:31
intNDArray< octave_uint8 > uint8NDArray
Definition: uint8NDArray.h:31
intNDArray< octave_uint16 > uint16NDArray
Definition: uint16NDArray.h:31
int16NDArray int16_array_value(void) const
Definition: ov-scalar.h:98
boolNDArray bool_array_value(bool warn=false) const
Definition: ov-scalar.h:206
octave_base_value * clone(void) const
Definition: ov-scalar.h:66
SparseComplexMatrix sparse_complex_matrix_value(bool=false) const
Definition: ov-scalar.h:167
uint16NDArray uint16_array_value(void) const
Definition: ov-scalar.h:114
int32NDArray int32_array_value(void) const
Definition: ov-scalar.h:102
charNDArray char_array_value(bool=false) const
Definition: ov-scalar.h:189
uint64NDArray uint64_array_value(void) const
Definition: ov-scalar.h:122
float float_value(bool=false) const
Definition: ov-scalar.h:143
SparseMatrix sparse_matrix_value(bool=false) const
Definition: ov-scalar.h:163
FloatComplexNDArray float_complex_array_value(bool=false) const
Definition: ov-scalar.h:185
intNDArray< octave_int16 > int16NDArray
Definition: int16NDArray.h:31
uint32NDArray uint32_array_value(void) const
Definition: ov-scalar.h:118
builtin_type_t
Definition: ov-base.h:59
F77_RET_T const double const double double * d
#define lo_ieee_isnan(x)
Definition: lo-ieee.h:101
octave_scalar(const octave_scalar &s)
Definition: ov-scalar.h:61
#define DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA
Definition: ov-base.h:142
bool bool_value(bool warn=false) const
Definition: ov-scalar.h:196
#define OCTINTERP_API
Definition: mexproto.h:66
~octave_scalar(void)
Definition: ov-scalar.h:64
intNDArray< octave_int8 > int8NDArray
Definition: int8NDArray.h:31
void gripe_nan_to_logical_conversion(void)
FloatComplex float_complex_value(bool=false) const
Definition: ov-scalar.h:174
Definition: dMatrix.h:35
FloatNDArray float_array_value(bool=false) const
Definition: ov-scalar.h:160
void increment(void)
Definition: ov-scalar.h:220
int8NDArray int8_array_value(void) const
Definition: ov-scalar.h:94
double double_value(bool=false) const
Definition: ov-scalar.h:141
intNDArray< octave_int32 > int32NDArray
Definition: int32NDArray.h:31
void gripe_logical_conversion(void)
Definition: gripes.cc:201
FloatComplexMatrix float_complex_matrix_value(bool=false) const
Definition: ov-scalar.h:179
octave_value any(int=0) const
Definition: ov-scalar.h:80
uint8NDArray uint8_array_value(void) const
Definition: ov-scalar.h:110
octave_idx_type write(const octave_value &data, octave_idx_type block_size, oct_data_conv::data_type output_type, octave_idx_type skip, oct_mach_info::float_format flt_fmt)
Definition: oct-stream.cc:3546
bool is_real_type(void) const
Definition: ov-scalar.h:87
bool is_float_type(void) const
Definition: ov-scalar.h:91
bool is_real_scalar(void) const
Definition: ov-scalar.h:85
std::complex< float > FloatComplex
Definition: oct-cmplx.h:30
std::complex< double > Complex
Definition: oct-cmplx.h:29
int64NDArray int64_array_value(void) const
Definition: ov-scalar.h:106
FloatMatrix float_matrix_value(bool=false) const
Definition: ov-scalar.h:154
NDArray array_value(bool=false) const
Definition: ov-scalar.h:157
int write(octave_stream &os, int block_size, oct_data_conv::data_type output_type, int skip, oct_mach_info::float_format flt_fmt) const
Definition: ov-scalar.h:237
float float_scalar_value(bool=false) const
Definition: ov-scalar.h:148
octave_base_value * empty_clone(void) const
Definition: ov-scalar.h:71
void decrement(void)
Definition: ov-scalar.h:222
ComplexMatrix complex_matrix_value(bool=false) const
Definition: ov-scalar.h:176
static bool scalar(const dim_vector &dims)
Definition: ov-struct.cc:736
intNDArray< octave_uint64 > uint64NDArray
Definition: uint64NDArray.h:31