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-float.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_float_h)
24 #define octave_ov_float_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-flt-re-mat.h"
42 #include "ov-base-scalar.h"
43 #include "ov-typeinfo.h"
44 
45 class octave_value_list;
46 
47 class tree_walker;
48 
49 // Real scalar values.
50 
51 class
54 {
55 public:
56 
58  : octave_base_scalar<float> (0.0) { }
59 
61  : octave_base_scalar<float> (d) { }
62 
64  : octave_base_scalar<float> (s) { }
65 
67 
68  octave_base_value *clone (void) const
69  { return new octave_float_scalar (*this); }
70 
71  // We return an octave_matrix here instead of an octave_float_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 (void) const
75  { return new octave_float_matrix (); }
76 
77  octave_value do_index_op (const octave_value_list& idx,
78  bool resize_ok = false);
79 
80  idx_vector index_vector (void) const { return idx_vector (scalar); }
81 
82  octave_value any (int = 0) const
83  { return (scalar != 0 && ! lo_ieee_isnan (scalar)); }
84 
85  builtin_type_t builtin_type (void) const { return btyp_float; }
86 
87  bool is_real_scalar (void) const { return true; }
88 
89  bool is_real_type (void) const { return true; }
90 
91  bool is_single_type (void) const { return true; }
92 
93  bool is_float_type (void) const { return true; }
94 
96  int8_array_value (void) const
97  { return int8NDArray (dim_vector (1, 1), scalar); }
98 
100  int16_array_value (void) const
101  { return int16NDArray (dim_vector (1, 1), scalar); }
102 
104  int32_array_value (void) const
105  { return int32NDArray (dim_vector (1, 1), scalar); }
106 
108  int64_array_value (void) const
109  { return int64NDArray (dim_vector (1, 1), scalar); }
110 
112  uint8_array_value (void) const
113  { return uint8NDArray (dim_vector (1, 1), scalar); }
114 
116  uint16_array_value (void) const
117  { return uint16NDArray (dim_vector (1, 1), scalar); }
118 
120  uint32_array_value (void) const
121  { return uint32NDArray (dim_vector (1, 1), scalar); }
122 
124  uint64_array_value (void) const
125  { return uint64NDArray (dim_vector (1, 1), scalar); }
126 
127 #define DEFINE_INT_SCALAR_VALUE(TYPE) \
128  octave_ ## TYPE \
129  TYPE ## _scalar_value (void) const \
130  { return octave_ ## TYPE (scalar); }
131 
140 
141 #undef DEFINE_INT_SCALAR_VALUE
142 
143  double double_value (bool = false) const
144  { return static_cast<double> (scalar); }
145 
146  float float_value (bool = false) const { return scalar; }
147 
148  double scalar_value (bool = false) const
149  { return static_cast<double> (scalar); }
150 
151  float float_scalar_value (bool = false) const { return scalar; }
152 
153  Matrix matrix_value (bool = false) const
154  { return Matrix (1, 1, scalar); }
155 
156  FloatMatrix float_matrix_value (bool = false) const
157  { return FloatMatrix (1, 1, scalar); }
158 
159  NDArray array_value (bool = false) const
160  { return NDArray (dim_vector (1, 1), scalar); }
161 
162  FloatNDArray float_array_value (bool = false) const
163  { return FloatNDArray (dim_vector (1, 1), scalar); }
164 
165  SparseMatrix sparse_matrix_value (bool = false) const
166  { return SparseMatrix (Matrix (1, 1, scalar)); }
167 
168  // FIXME Need SparseComplexMatrix (Matrix) constructor!!!
169  SparseComplexMatrix sparse_complex_matrix_value (bool = false) const
170  { return SparseComplexMatrix (sparse_matrix_value ()); }
171 
172  octave_value resize (const dim_vector& dv, bool fill = false) const;
173 
174  Complex complex_value (bool = false) const { return scalar; }
175 
176  FloatComplex float_complex_value (bool = false) const { return scalar; }
177 
178  ComplexMatrix complex_matrix_value (bool = false) const
179  { return ComplexMatrix (1, 1, Complex (scalar)); }
180 
181  FloatComplexMatrix float_complex_matrix_value (bool = false) const
182  { return FloatComplexMatrix (1, 1, FloatComplex (scalar)); }
183 
184  ComplexNDArray complex_array_value (bool = false) const
185  { return ComplexNDArray (dim_vector (1, 1), Complex (scalar)); }
186 
187  FloatComplexNDArray float_complex_array_value (bool = false) const
188  { return FloatComplexNDArray (dim_vector (1, 1), FloatComplex (scalar)); }
189 
191  char_array_value (bool = false) const
192  {
193  charNDArray retval (dim_vector (1, 1));
194  retval(0) = static_cast<char> (scalar);
195  return retval;
196  }
197 
198  bool bool_value (bool warn = false) const
199  {
200  if (xisnan (scalar))
202  else if (warn && scalar != 0 && scalar != 1)
204 
205  return scalar;
206  }
207 
208  boolNDArray bool_array_value (bool warn = false) const
209  {
210  if (xisnan (scalar))
212  else if (warn && scalar != 0 && scalar != 1)
214 
215  return boolNDArray (dim_vector (1, 1), scalar);
216  }
217 
218  octave_value diag (octave_idx_type m, octave_idx_type n) const;
219 
220  octave_value convert_to_str_internal (bool pad, bool force, char type) const;
221 
222  void increment (void) { ++scalar; }
223 
224  void decrement (void) { --scalar; }
225 
226  bool save_ascii (std::ostream& os);
227 
228  bool load_ascii (std::istream& is);
229 
230  bool save_binary (std::ostream& os, bool& save_as_floats);
231 
232  bool load_binary (std::istream& is, bool swap,
234 
235 #if defined (HAVE_HDF5)
236  bool save_hdf5 (hid_t loc_id, const char *name, bool save_as_floats);
237 
238  bool load_hdf5 (hid_t loc_id, const char *name);
239 #endif
240 
241  int write (octave_stream& os, int block_size,
242  oct_data_conv::data_type output_type, int skip,
243  oct_mach_info::float_format flt_fmt) const
244  {
245  return os.write (array_value (), block_size, output_type,
246  skip, flt_fmt);
247  }
248 
249  mxArray *as_mxArray (void) const;
250 
251  octave_value map (unary_mapper_t umap) const;
252 
253  bool fast_elem_insert_self (void *where, builtin_type_t btyp) const;
254 
255 private:
256 
258 
260 };
261 
262 #endif