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