GNU Octave  3.8.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-2013 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 "oct-alloc.h"
36 #include "str-vec.h"
37 
38 #include "gripes.h"
39 #include "ov-base.h"
40 #include "ov-re-mat.h"
41 #include "ov-base-scalar.h"
42 #include "ov-typeinfo.h"
43 
44 class octave_value_list;
45 
46 class tree_walker;
47 
48 // Real scalar values.
49 
50 class
53 {
54 public:
55 
57  : octave_base_scalar<double> (0.0) { }
58 
59  octave_scalar (double d)
60  : octave_base_scalar<double> (d) { }
61 
63  : octave_base_scalar<double> (s) { }
64 
65  ~octave_scalar (void) { }
66 
67  octave_base_value *clone (void) const { return new octave_scalar (*this); }
68 
69  // We return an octave_matrix here instead of an octave_scalar so
70  // that in expressions like A(2,2,2) = 2 (for A previously
71  // undefined), A will be empty instead of a 1x1 object.
72  octave_base_value *empty_clone (void) const { return new octave_matrix (); }
73 
74  octave_value do_index_op (const octave_value_list& idx,
75  bool resize_ok = false);
76 
77  type_conv_info numeric_demotion_function (void) const;
78 
79  idx_vector index_vector (void) const { return idx_vector (scalar); }
80 
81  octave_value any (int = 0) const
82  { return (scalar != 0 && ! lo_ieee_isnan (scalar)); }
83 
84  builtin_type_t builtin_type (void) const { return btyp_double; }
85 
86  bool is_real_scalar (void) const { return true; }
87 
88  bool is_real_type (void) const { return true; }
89 
90  bool is_double_type (void) const { return true; }
91 
92  bool is_float_type (void) const { return true; }
93 
95  int8_array_value (void) const
96  { return int8NDArray (dim_vector (1, 1), scalar); }
97 
99  int16_array_value (void) const
100  { return int16NDArray (dim_vector (1, 1), scalar); }
101 
103  int32_array_value (void) const
104  { return int32NDArray (dim_vector (1, 1), scalar); }
105 
107  int64_array_value (void) const
108  { return int64NDArray (dim_vector (1, 1), scalar); }
109 
111  uint8_array_value (void) const
112  { return uint8NDArray (dim_vector (1, 1), scalar); }
113 
115  uint16_array_value (void) const
116  { return uint16NDArray (dim_vector (1, 1), scalar); }
117 
119  uint32_array_value (void) const
120  { return uint32NDArray (dim_vector (1, 1), scalar); }
121 
123  uint64_array_value (void) const
124  { return uint64NDArray (dim_vector (1, 1), scalar); }
125 
126 #define DEFINE_INT_SCALAR_VALUE(TYPE) \
127  octave_ ## TYPE \
128  TYPE ## _scalar_value (void) const \
129  { return octave_ ## TYPE (scalar); }
130 
139 
140 #undef DEFINE_INT_SCALAR_VALUE
141 
142  double double_value (bool = false) const { return scalar; }
143 
144  float float_value (bool = false) const
145  { return static_cast<float> (scalar); }
146 
147  double scalar_value (bool = false) const { return scalar; }
148 
149  float float_scalar_value (bool = false) const
150  { return static_cast<float> (scalar); }
151 
152  Matrix matrix_value (bool = false) const
153  { return Matrix (1, 1, scalar); }
154 
155  FloatMatrix float_matrix_value (bool = false) const
156  { return FloatMatrix (1, 1, scalar); }
157 
158  NDArray array_value (bool = false) const
159  { return NDArray (dim_vector (1, 1), scalar); }
160 
161  FloatNDArray float_array_value (bool = false) const
162  { return FloatNDArray (dim_vector (1, 1), scalar); }
163 
164  SparseMatrix sparse_matrix_value (bool = false) const
165  { return SparseMatrix (Matrix (1, 1, scalar)); }
166 
167  // FIXME Need SparseComplexMatrix (Matrix) constructor!!!
168  SparseComplexMatrix sparse_complex_matrix_value (bool = false) const
169  { return SparseComplexMatrix (sparse_matrix_value ()); }
170 
171  octave_value resize (const dim_vector& dv, bool fill = false) const;
172 
173  Complex complex_value (bool = false) const { return scalar; }
174 
175  FloatComplex float_complex_value (bool = false) const { return scalar; }
176 
177  ComplexMatrix complex_matrix_value (bool = false) const
178  { return ComplexMatrix (1, 1, Complex (scalar)); }
179 
180  FloatComplexMatrix float_complex_matrix_value (bool = false) const
181  { return FloatComplexMatrix (1, 1, FloatComplex (scalar)); }
182 
183  ComplexNDArray complex_array_value (bool = false) const
184  { return ComplexNDArray (dim_vector (1, 1), Complex (scalar)); }
185 
186  FloatComplexNDArray float_complex_array_value (bool = false) const
187  { return FloatComplexNDArray (dim_vector (1, 1), FloatComplex (scalar)); }
188 
190  char_array_value (bool = false) const
191  {
192  charNDArray retval (dim_vector (1, 1));
193  retval(0) = static_cast<char> (scalar);
194  return retval;
195  }
196 
197  bool bool_value (bool warn = false) const
198  {
199  if (xisnan (scalar))
201  else if (warn && scalar != 0 && scalar != 1)
203 
204  return scalar;
205  }
206 
207  boolNDArray bool_array_value (bool warn = false) const
208  {
209  if (xisnan (scalar))
211  else if (warn && scalar != 0 && scalar != 1)
213 
214  return boolNDArray (dim_vector (1, 1), scalar);
215  }
216 
217  octave_value diag (octave_idx_type m, octave_idx_type n) const;
218 
219  octave_value convert_to_str_internal (bool pad, bool force, char type) const;
220 
221  void increment (void) { ++scalar; }
222 
223  void decrement (void) { --scalar; }
224 
225  bool save_ascii (std::ostream& os);
226 
227  bool load_ascii (std::istream& is);
228 
229  bool save_binary (std::ostream& os, bool& save_as_floats);
230 
231  bool load_binary (std::istream& is, bool swap,
233 
234 #if defined (HAVE_HDF5)
235  bool save_hdf5 (hid_t loc_id, const char *name, bool save_as_floats);
236 
237  bool load_hdf5 (hid_t loc_id, const char *name);
238 #endif
239 
240  int write (octave_stream& os, int block_size,
241  oct_data_conv::data_type output_type, int skip,
242  oct_mach_info::float_format flt_fmt) const
243  {
244  return os.write (array_value (), block_size, output_type,
245  skip, flt_fmt);
246  }
247 
248  mxArray *as_mxArray (void) const;
249 
250  octave_value map (unary_mapper_t umap) const;
251 
252  bool fast_elem_insert_self (void *where, builtin_type_t btyp) const;
253 
254 private:
255 
257 
259 };
260 
261 #endif