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