Mercurial > hg > octave-lyh
annotate src/ov-scalar.h @ 9812:f80c566bc751
improve unary mapper system
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Thu, 12 Nov 2009 15:47:58 +0100 |
parents | e793865ede63 |
children | b3089dba88bf |
rev | line source |
---|---|
2376 | 1 /* |
2 | |
7017 | 3 Copyright (C) 1996, 1997, 1998, 2000, 2002, 2003, 2004, 2005, 2006, |
8920 | 4 2007, 2008, 2009 John W. Eaton |
2376 | 5 |
6 This file is part of Octave. | |
7 | |
8 Octave is free software; you can redistribute it and/or modify it | |
9 under the terms of the GNU General Public License as published by the | |
7016 | 10 Free Software Foundation; either version 3 of the License, or (at your |
11 option) any later version. | |
2376 | 12 |
13 Octave is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
20 <http://www.gnu.org/licenses/>. | |
2376 | 21 |
22 */ | |
23 | |
24 #if !defined (octave_scalar_h) | |
25 #define octave_scalar_h 1 | |
26 | |
27 #include <cstdlib> | |
28 | |
8950
d865363208d6
include <iosfwd> instead of <iostream> in header files
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
29 #include <iosfwd> |
2376 | 30 #include <string> |
31 | |
5110 | 32 #include "lo-ieee.h" |
2889 | 33 #include "lo-mappers.h" |
2376 | 34 #include "lo-utils.h" |
35 #include "mx-base.h" | |
2477 | 36 #include "oct-alloc.h" |
2376 | 37 #include "str-vec.h" |
38 | |
5943 | 39 #include "gripes.h" |
2376 | 40 #include "ov-base.h" |
4749 | 41 #include "ov-re-mat.h" |
3223 | 42 #include "ov-base-scalar.h" |
2376 | 43 #include "ov-typeinfo.h" |
44 | |
45 class Octave_map; | |
46 class octave_value_list; | |
47 | |
48 class tree_walker; | |
49 | |
50 // Real scalar values. | |
51 | |
52 class | |
6153 | 53 OCTINTERP_API |
3223 | 54 octave_scalar : public octave_base_scalar<double> |
2376 | 55 { |
56 public: | |
57 | |
58 octave_scalar (void) | |
3223 | 59 : octave_base_scalar<double> (0.0) { } |
2376 | 60 |
61 octave_scalar (double d) | |
3223 | 62 : octave_base_scalar<double> (d) { } |
2376 | 63 |
64 octave_scalar (const octave_scalar& s) | |
3223 | 65 : octave_base_scalar<double> (s) { } |
2376 | 66 |
67 ~octave_scalar (void) { } | |
68 | |
5759 | 69 octave_base_value *clone (void) const { return new octave_scalar (*this); } |
4749 | 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. | |
5759 | 74 octave_base_value *empty_clone (void) const { return new octave_matrix (); } |
2376 | 75 |
5885 | 76 octave_value do_index_op (const octave_value_list& idx, |
77 bool resize_ok = false); | |
2376 | 78 |
8345
c777f3ce02d8
smarter conversion lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8202
diff
changeset
|
79 type_conv_info numeric_demotion_function (void) const; |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7740
diff
changeset
|
80 |
2376 | 81 idx_vector index_vector (void) const { return idx_vector (scalar); } |
82 | |
5110 | 83 octave_value any (int = 0) const |
84 { return (scalar != 0 && ! lo_ieee_isnan (scalar)); } | |
85 | |
9685 | 86 builtin_type_t builtin_type (void) const { return btyp_double; } |
87 | |
2376 | 88 bool is_real_scalar (void) const { return true; } |
89 | |
90 bool is_real_type (void) const { return true; } | |
91 | |
5895 | 92 bool is_double_type (void) const { return true; } |
93 | |
7576 | 94 bool is_float_type (void) const { return true; } |
95 | |
5533 | 96 int8NDArray |
97 int8_array_value (void) const | |
98 { return int8NDArray (dim_vector (1, 1), scalar); } | |
99 | |
100 int16NDArray | |
101 int16_array_value (void) const | |
102 { return int16NDArray (dim_vector (1, 1), scalar); } | |
103 | |
104 int32NDArray | |
105 int32_array_value (void) const | |
106 { return int32NDArray (dim_vector (1, 1), scalar); } | |
107 | |
108 int64NDArray | |
109 int64_array_value (void) const | |
110 { return int64NDArray (dim_vector (1, 1), scalar); } | |
111 | |
112 uint8NDArray | |
113 uint8_array_value (void) const | |
114 { return uint8NDArray (dim_vector (1, 1), scalar); } | |
115 | |
116 uint16NDArray | |
117 uint16_array_value (void) const | |
118 { return uint16NDArray (dim_vector (1, 1), scalar); } | |
119 | |
120 uint32NDArray | |
121 uint32_array_value (void) const | |
122 { return uint32NDArray (dim_vector (1, 1), scalar); } | |
123 | |
124 uint64NDArray | |
125 uint64_array_value (void) const | |
126 { return uint64NDArray (dim_vector (1, 1), scalar); } | |
127 | |
8854
97d146d1648b
add missing scalar->int scalar conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8437
diff
changeset
|
128 #define DEFINE_INT_SCALAR_VALUE(TYPE) \ |
97d146d1648b
add missing scalar->int scalar conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8437
diff
changeset
|
129 octave_ ## TYPE \ |
97d146d1648b
add missing scalar->int scalar conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8437
diff
changeset
|
130 TYPE ## _scalar_value (void) const \ |
97d146d1648b
add missing scalar->int scalar conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8437
diff
changeset
|
131 { return octave_ ## TYPE (scalar); } |
97d146d1648b
add missing scalar->int scalar conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8437
diff
changeset
|
132 |
97d146d1648b
add missing scalar->int scalar conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8437
diff
changeset
|
133 DEFINE_INT_SCALAR_VALUE (int8) |
97d146d1648b
add missing scalar->int scalar conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8437
diff
changeset
|
134 DEFINE_INT_SCALAR_VALUE (int16) |
97d146d1648b
add missing scalar->int scalar conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8437
diff
changeset
|
135 DEFINE_INT_SCALAR_VALUE (int32) |
97d146d1648b
add missing scalar->int scalar conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8437
diff
changeset
|
136 DEFINE_INT_SCALAR_VALUE (int64) |
97d146d1648b
add missing scalar->int scalar conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8437
diff
changeset
|
137 DEFINE_INT_SCALAR_VALUE (uint8) |
97d146d1648b
add missing scalar->int scalar conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8437
diff
changeset
|
138 DEFINE_INT_SCALAR_VALUE (uint16) |
97d146d1648b
add missing scalar->int scalar conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8437
diff
changeset
|
139 DEFINE_INT_SCALAR_VALUE (uint32) |
97d146d1648b
add missing scalar->int scalar conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8437
diff
changeset
|
140 DEFINE_INT_SCALAR_VALUE (uint64) |
97d146d1648b
add missing scalar->int scalar conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8437
diff
changeset
|
141 |
97d146d1648b
add missing scalar->int scalar conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8437
diff
changeset
|
142 #undef DEFINE_INT_SCALAR_VALUE |
97d146d1648b
add missing scalar->int scalar conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8437
diff
changeset
|
143 |
2376 | 144 double double_value (bool = false) const { return scalar; } |
145 | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7740
diff
changeset
|
146 float float_value (bool = false) const { return static_cast<float> (scalar); } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7740
diff
changeset
|
147 |
2916 | 148 double scalar_value (bool = false) const { return scalar; } |
149 | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7740
diff
changeset
|
150 float float_scalar_value (bool = false) const { return static_cast<float> (scalar); } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7740
diff
changeset
|
151 |
3145 | 152 Matrix matrix_value (bool = false) const |
153 { return Matrix (1, 1, scalar); } | |
2376 | 154 |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7740
diff
changeset
|
155 FloatMatrix float_matrix_value (bool = false) const |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7740
diff
changeset
|
156 { return FloatMatrix (1, 1, scalar); } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7740
diff
changeset
|
157 |
4569 | 158 NDArray array_value (bool = false) const |
4630 | 159 { return NDArray (dim_vector (1, 1), scalar); } |
4505 | 160 |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7740
diff
changeset
|
161 FloatNDArray float_array_value (bool = false) const |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7740
diff
changeset
|
162 { return FloatNDArray (dim_vector (1, 1), scalar); } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7740
diff
changeset
|
163 |
5989 | 164 SparseMatrix sparse_matrix_value (bool = false) const |
165 { return SparseMatrix (Matrix (1, 1, scalar)); } | |
166 | |
8202
cf59d542f33e
replace all TODOs and XXXs with FIXMEs
Jaroslav Hajek <highegg@gmail.com>
parents:
7922
diff
changeset
|
167 // FIXME Need SparseComplexMatrix (Matrix) constructor!!! |
5989 | 168 SparseComplexMatrix sparse_complex_matrix_value (bool = false) const |
169 { return SparseComplexMatrix (sparse_matrix_value ()); } | |
170 | |
5731 | 171 octave_value resize (const dim_vector& dv, bool fill = false) const; |
4915 | 172 |
2376 | 173 Complex complex_value (bool = false) const { return scalar; } |
174 | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7740
diff
changeset
|
175 FloatComplex float_complex_value (bool = false) const { return scalar; } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7740
diff
changeset
|
176 |
2376 | 177 ComplexMatrix complex_matrix_value (bool = false) const |
178 { return ComplexMatrix (1, 1, Complex (scalar)); } | |
179 | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7740
diff
changeset
|
180 FloatComplexMatrix float_complex_matrix_value (bool = false) const |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7740
diff
changeset
|
181 { return FloatComplexMatrix (1, 1, FloatComplex (scalar)); } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7740
diff
changeset
|
182 |
4569 | 183 ComplexNDArray complex_array_value (bool = false) const |
4630 | 184 { return ComplexNDArray (dim_vector (1, 1), Complex (scalar)); } |
4569 | 185 |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7740
diff
changeset
|
186 FloatComplexNDArray float_complex_array_value (bool = false) const |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7740
diff
changeset
|
187 { return FloatComplexNDArray (dim_vector (1, 1), FloatComplex (scalar)); } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7740
diff
changeset
|
188 |
5533 | 189 charNDArray |
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 | |
5943 | 197 bool bool_value (bool warn = false) const |
198 { | |
7922
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7885
diff
changeset
|
199 if (xisnan (scalar)) |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7885
diff
changeset
|
200 error ("invalid conversion from NaN to logical"); |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7885
diff
changeset
|
201 else if (warn && scalar != 0 && scalar != 1) |
5943 | 202 gripe_logical_conversion (); |
203 | |
204 return scalar; | |
205 } | |
5533 | 206 |
5943 | 207 boolNDArray bool_array_value (bool warn = false) const |
208 { | |
7922
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7885
diff
changeset
|
209 if (xisnan (scalar)) |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7885
diff
changeset
|
210 error ("invalid conversion from NaN to logical"); |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7885
diff
changeset
|
211 else if (warn && scalar != 0 && scalar != 1) |
5943 | 212 gripe_logical_conversion (); |
213 | |
214 return boolNDArray (dim_vector (1, 1), scalar); | |
215 } | |
5533 | 216 |
5279 | 217 octave_value convert_to_str_internal (bool pad, bool force, char type) const; |
3223 | 218 |
2376 | 219 void increment (void) { ++scalar; } |
220 | |
221 void decrement (void) { --scalar; } | |
222 | |
6974 | 223 bool save_ascii (std::ostream& os); |
4687 | 224 |
225 bool load_ascii (std::istream& is); | |
226 | |
227 bool save_binary (std::ostream& os, bool& save_as_floats); | |
228 | |
229 bool load_binary (std::istream& is, bool swap, | |
230 oct_mach_info::float_format fmt); | |
231 | |
232 #if defined (HAVE_HDF5) | |
233 bool save_hdf5 (hid_t loc_id, const char *name, bool save_as_floats); | |
234 | |
235 bool load_hdf5 (hid_t loc_id, const char *name, bool have_h5giterate_bug); | |
236 #endif | |
237 | |
4944 | 238 int write (octave_stream& os, int block_size, |
239 oct_data_conv::data_type output_type, int skip, | |
240 oct_mach_info::float_format flt_fmt) const | |
241 { | |
242 return os.write (array_value (), block_size, output_type, | |
243 skip, flt_fmt); | |
244 } | |
245 | |
5900 | 246 mxArray *as_mxArray (void) const; |
247 | |
9812
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9685
diff
changeset
|
248 octave_value map (unary_mapper_t umap) const; |
7503
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
249 |
2376 | 250 private: |
251 | |
4612 | 252 DECLARE_OCTAVE_ALLOCATOR |
2477 | 253 |
4612 | 254 DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA |
2376 | 255 }; |
256 | |
257 #endif | |
258 | |
259 /* | |
260 ;;; Local Variables: *** | |
261 ;;; mode: C++ *** | |
262 ;;; End: *** | |
263 */ |