4904
|
1 /* |
|
2 |
|
3 Copyright (C) 2004 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 2, or (at your option) any |
|
10 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, write to the Free |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 #include <cstdlib> |
|
24 |
|
25 #include <iostream> |
|
26 #include <string> |
|
27 |
|
28 #include "mx-base.h" |
|
29 #include "oct-alloc.h" |
|
30 #include "so-array.h" |
|
31 #include "str-vec.h" |
|
32 |
|
33 #include "error.h" |
4944
|
34 #include "oct-stream.h" |
4904
|
35 #include "ov-base.h" |
|
36 #include "ov-base-int.h" |
|
37 #include "ov-typeinfo.h" |
4982
|
38 #include "gripes.h" |
4904
|
39 |
|
40 class |
|
41 OCTAVE_VALUE_INT_MATRIX_T |
|
42 : public octave_base_int_matrix<OCTAVE_INT_NDARRAY_T> |
|
43 { |
|
44 public: |
|
45 |
|
46 OCTAVE_VALUE_INT_MATRIX_T (void) |
|
47 : octave_base_int_matrix<OCTAVE_INT_NDARRAY_T> () { } |
|
48 |
|
49 OCTAVE_VALUE_INT_MATRIX_T (const OCTAVE_INT_NDARRAY_T& nda) |
|
50 : octave_base_int_matrix<OCTAVE_INT_NDARRAY_T> (nda) { } |
|
51 |
|
52 ~OCTAVE_VALUE_INT_MATRIX_T (void) { } |
|
53 |
|
54 octave_value * |
|
55 clone (void) const |
|
56 { return new OCTAVE_VALUE_INT_MATRIX_T (*this); } |
|
57 |
|
58 octave_value * |
|
59 empty_clone (void) const |
|
60 { return new OCTAVE_VALUE_INT_MATRIX_T (); } |
|
61 |
|
62 OCTAVE_INT_NDARRAY_T |
4910
|
63 OCTAVE_VALUE_INT_NDARRAY_EXTRACTOR_FUNCTION (void) const |
4904
|
64 { return matrix; } |
|
65 |
4982
|
66 double |
|
67 double_value (bool = false) const |
|
68 { |
|
69 double retval = lo_ieee_nan_value (); |
|
70 |
|
71 if (numel () > 0) |
|
72 { |
|
73 // XXX FIXME XXX -- is warn_fortran_indexing the right variable here? |
|
74 if (Vwarn_fortran_indexing) |
|
75 gripe_implicit_conversion (type_name (), "real scalar"); |
|
76 |
|
77 retval = double (matrix (0, 0)); |
|
78 } |
|
79 else |
|
80 gripe_invalid_conversion (type_name (), "real scalar"); |
|
81 |
|
82 return retval; |
|
83 |
|
84 } |
|
85 |
|
86 double |
|
87 scalar_value (bool = false) const |
|
88 { |
|
89 return double_value (); |
|
90 } |
|
91 |
4915
|
92 NDArray |
|
93 array_value (bool = false) const |
|
94 { |
|
95 NDArray retval (matrix.dims ()); |
|
96 int nel = matrix.numel (); |
|
97 for (int i = 0; i < nel; i++) |
4963
|
98 retval(i) = double (matrix(i)); |
|
99 return retval; |
|
100 } |
|
101 |
|
102 ComplexNDArray |
|
103 complex_array_value (bool = false) const |
|
104 { |
|
105 ComplexNDArray retval (matrix.dims ()); |
|
106 int nel = matrix.numel (); |
|
107 for (int i = 0; i < nel; i++) |
|
108 retval(i) = Complex (matrix(i)); |
4915
|
109 return retval; |
|
110 } |
|
111 |
4938
|
112 idx_vector index_vector (void) const { return idx_vector (matrix); } |
|
113 |
4944
|
114 int write (octave_stream& os, int block_size, |
|
115 oct_data_conv::data_type output_type, int skip, |
|
116 oct_mach_info::float_format flt_fmt) const |
|
117 { return os.write (matrix, block_size, output_type, skip, flt_fmt); } |
|
118 |
4904
|
119 private: |
|
120 |
|
121 DECLARE_OCTAVE_ALLOCATOR |
|
122 |
|
123 DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA |
|
124 }; |
|
125 |
|
126 class |
|
127 OCTAVE_VALUE_INT_SCALAR_T |
|
128 : public octave_base_int_scalar<OCTAVE_INT_T> |
|
129 { |
|
130 public: |
|
131 |
|
132 OCTAVE_VALUE_INT_SCALAR_T (void) |
|
133 : octave_base_int_scalar<OCTAVE_INT_T> () { } |
|
134 |
|
135 OCTAVE_VALUE_INT_SCALAR_T (const OCTAVE_INT_T& nda) |
|
136 : octave_base_int_scalar<OCTAVE_INT_T> (nda) { } |
|
137 |
|
138 ~OCTAVE_VALUE_INT_SCALAR_T (void) { } |
|
139 |
|
140 octave_value * |
|
141 clone (void) const |
|
142 { return new OCTAVE_VALUE_INT_SCALAR_T (*this); } |
|
143 |
|
144 octave_value * |
|
145 empty_clone (void) const |
|
146 { return new OCTAVE_VALUE_INT_SCALAR_T (); } |
|
147 |
4964
|
148 octave_value do_index_op (const octave_value_list& idx, int resize_ok) |
|
149 { |
|
150 octave_value retval; |
|
151 |
|
152 if (idx.valid_scalar_indices ()) |
|
153 retval = scalar; |
|
154 else |
|
155 { |
|
156 // XXX FIXME XXX -- this doesn't solve the problem of |
|
157 // |
|
158 // a = 1; a([1,1], [1,1], [1,1]) |
|
159 // |
|
160 // and similar constructions. Hmm... |
|
161 |
|
162 // XXX FIXME XXX -- using this constructor avoids narrowing the |
|
163 // 1x1 matrix back to a scalar value. Need a better solution |
|
164 // to this problem. |
|
165 |
|
166 octave_value tmp (new OCTAVE_VALUE_INT_MATRIX_T ( |
|
167 OCTAVE_VALUE_INT_NDARRAY_EXTRACTOR_FUNCTION ())); |
|
168 retval = tmp.do_index_op (idx, resize_ok); |
|
169 } |
|
170 |
|
171 return retval; |
|
172 } |
|
173 |
4904
|
174 OCTAVE_INT_T |
4910
|
175 OCTAVE_VALUE_INT_SCALAR_EXTRACTOR_FUNCTION (void) const |
4904
|
176 { return scalar; } |
|
177 |
|
178 OCTAVE_INT_NDARRAY_T |
4910
|
179 OCTAVE_VALUE_INT_NDARRAY_EXTRACTOR_FUNCTION (void) const |
|
180 { return OCTAVE_INT_NDARRAY_T (dim_vector (1, 1), scalar); } |
4904
|
181 |
4915
|
182 octave_value resize (const dim_vector& dv) const |
|
183 { OCTAVE_INT_NDARRAY_T retval (dv); if (dv.numel()) retval(0) = scalar; return retval; } |
|
184 |
4982
|
185 double |
|
186 double_value (bool = false) const |
|
187 { |
|
188 return double (scalar); |
|
189 } |
|
190 |
|
191 double |
|
192 scalar_value (bool = false) const |
|
193 { |
|
194 return double (scalar); |
|
195 } |
|
196 |
4915
|
197 NDArray |
|
198 array_value (bool = false) const |
|
199 { |
|
200 NDArray retval (dim_vector (1,1)); |
4963
|
201 retval(0) = double (scalar); |
|
202 return retval; |
|
203 } |
|
204 |
|
205 ComplexNDArray |
|
206 complex_array_value (bool = false) const |
|
207 { |
|
208 ComplexNDArray retval (dim_vector (1,1)); |
|
209 retval(0) = Complex (scalar); |
4915
|
210 return retval; |
|
211 } |
|
212 |
4938
|
213 idx_vector index_vector (void) const { return idx_vector (scalar); } |
|
214 |
4944
|
215 int write (octave_stream& os, int block_size, |
|
216 oct_data_conv::data_type output_type, int skip, |
|
217 oct_mach_info::float_format flt_fmt) const |
|
218 { |
|
219 return os.write (OCTAVE_VALUE_INT_NDARRAY_EXTRACTOR_FUNCTION (), |
|
220 block_size, output_type, skip, flt_fmt); |
|
221 } |
|
222 |
4904
|
223 private: |
|
224 |
|
225 DECLARE_OCTAVE_ALLOCATOR |
|
226 |
|
227 DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA |
|
228 }; |
|
229 |
|
230 /* |
|
231 ;;; Local Variables: *** |
|
232 ;;; mode: C++ *** |
|
233 ;;; End: *** |
|
234 */ |