3219
|
1 /* |
|
2 |
7017
|
3 Copyright (C) 1996, 1997, 1998, 2000, 2002, 2003, 2004, 2005, 2006, |
|
4 2007 John W. Eaton |
3219
|
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. |
3219
|
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/>. |
3219
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
3503
|
28 #include <iostream> |
3219
|
29 |
3933
|
30 #include "Cell.h" |
3220
|
31 #include "oct-obj.h" |
3933
|
32 #include "oct-map.h" |
3219
|
33 #include "ov-base.h" |
|
34 #include "ov-base-mat.h" |
3572
|
35 #include "pr-output.h" |
3219
|
36 |
|
37 template <class MT> |
3220
|
38 octave_value |
4247
|
39 octave_base_matrix<MT>::subsref (const std::string& type, |
4219
|
40 const std::list<octave_value_list>& idx) |
3933
|
41 { |
|
42 octave_value retval; |
|
43 |
|
44 switch (type[0]) |
|
45 { |
|
46 case '(': |
|
47 retval = do_index_op (idx.front ()); |
|
48 break; |
|
49 |
|
50 case '{': |
|
51 case '.': |
|
52 { |
|
53 std::string nm = type_name (); |
|
54 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); |
|
55 } |
|
56 break; |
|
57 |
|
58 default: |
|
59 panic_impossible (); |
|
60 } |
|
61 |
|
62 return retval.next_subsref (type, idx); |
|
63 } |
|
64 |
|
65 template <class MT> |
|
66 octave_value |
4247
|
67 octave_base_matrix<MT>::subsasgn (const std::string& type, |
4219
|
68 const std::list<octave_value_list>& idx, |
3933
|
69 const octave_value& rhs) |
|
70 { |
|
71 octave_value retval; |
|
72 |
|
73 switch (type[0]) |
|
74 { |
|
75 case '(': |
|
76 { |
|
77 if (type.length () == 1) |
|
78 retval = numeric_assign (type, idx, rhs); |
4436
|
79 else if (is_empty ()) |
|
80 { |
|
81 // Allow conversion of empty matrix to some other type in |
|
82 // cases like |
|
83 // |
|
84 // x = []; x(i).f = rhs |
|
85 |
4846
|
86 if (type[1] == '.') |
|
87 { |
|
88 octave_value tmp = octave_value::empty_conv (type, rhs); |
4436
|
89 |
4846
|
90 retval = tmp.subsasgn (type, idx, rhs); |
|
91 } |
|
92 else |
|
93 error ("invalid assignment expression"); |
4436
|
94 } |
3933
|
95 else |
|
96 { |
|
97 std::string nm = type_name (); |
4519
|
98 error ("in indexed assignment of %s, last lhs index must be ()", |
3933
|
99 nm.c_str ()); |
|
100 } |
|
101 } |
|
102 break; |
|
103 |
|
104 case '{': |
|
105 case '.': |
|
106 { |
4834
|
107 if (is_empty ()) |
|
108 { |
|
109 octave_value tmp = octave_value::empty_conv (type, rhs); |
|
110 |
|
111 retval = tmp.subsasgn (type, idx, rhs); |
|
112 } |
|
113 else |
|
114 { |
|
115 std::string nm = type_name (); |
|
116 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); |
|
117 } |
3933
|
118 } |
|
119 break; |
|
120 |
|
121 default: |
|
122 panic_impossible (); |
|
123 } |
|
124 |
|
125 return retval; |
|
126 } |
|
127 |
|
128 template <class MT> |
|
129 octave_value |
|
130 octave_base_matrix<MT>::do_index_op (const octave_value_list& idx, |
5885
|
131 bool resize_ok) |
3220
|
132 { |
|
133 octave_value retval; |
|
134 |
5275
|
135 octave_idx_type n_idx = idx.length (); |
3220
|
136 |
4742
|
137 int nd = matrix.ndims (); |
3220
|
138 |
4742
|
139 switch (n_idx) |
|
140 { |
|
141 case 0: |
5539
|
142 retval = matrix; |
3220
|
143 break; |
|
144 |
|
145 case 1: |
|
146 { |
|
147 idx_vector i = idx (0).index_vector (); |
|
148 |
4858
|
149 if (! error_state) |
|
150 retval = MT (matrix.index (i, resize_ok, MT::resize_fill_value ())); |
3220
|
151 } |
|
152 break; |
|
153 |
|
154 default: |
|
155 { |
4742
|
156 if (n_idx == 2 && nd == 2) |
|
157 { |
|
158 idx_vector i = idx (0).index_vector (); |
4858
|
159 |
|
160 if (! error_state) |
|
161 { |
|
162 idx_vector j = idx (1).index_vector (); |
3220
|
163 |
4858
|
164 if (! error_state) |
|
165 retval = MT (matrix.index (i, j, resize_ok, |
|
166 MT::resize_fill_value ())); |
|
167 } |
4742
|
168 } |
|
169 else |
|
170 { |
|
171 Array<idx_vector> idx_vec (n_idx); |
4513
|
172 |
5275
|
173 for (octave_idx_type i = 0; i < n_idx; i++) |
4858
|
174 { |
|
175 idx_vec(i) = idx(i).index_vector (); |
4742
|
176 |
4858
|
177 if (error_state) |
|
178 break; |
|
179 } |
|
180 |
|
181 if (! error_state) |
|
182 retval = MT (matrix.index (idx_vec, resize_ok, |
|
183 MT::resize_fill_value ())); |
4742
|
184 } |
3220
|
185 } |
|
186 break; |
|
187 } |
|
188 |
|
189 return retval; |
|
190 } |
|
191 |
3928
|
192 template <class MT> |
|
193 void |
|
194 octave_base_matrix<MT>::assign (const octave_value_list& idx, const MT& rhs) |
|
195 { |
5275
|
196 octave_idx_type len = idx.length (); |
3928
|
197 |
5275
|
198 for (octave_idx_type i = 0; i < len; i++) |
4513
|
199 matrix.set_index (idx(i).index_vector ()); |
3928
|
200 |
4513
|
201 ::assign (matrix, rhs, MT::resize_fill_value ()); |
6195
|
202 |
|
203 |
|
204 // Invalidate the matrix type |
|
205 typ.invalidate_type (); |
3928
|
206 } |
|
207 |
3220
|
208 template <class MT> |
5731
|
209 octave_value |
|
210 octave_base_matrix<MT>::resize (const dim_vector& dv, bool fill) const |
|
211 { |
|
212 MT retval (matrix); |
|
213 if (fill) |
|
214 retval.resize (dv, 0); |
|
215 else |
|
216 retval.resize (dv); |
|
217 return retval; |
|
218 } |
|
219 |
|
220 template <class MT> |
3220
|
221 bool |
|
222 octave_base_matrix<MT>::is_true (void) const |
|
223 { |
|
224 bool retval = false; |
4875
|
225 dim_vector dv = matrix.dims (); |
|
226 int nel = dv.numel (); |
4915
|
227 |
4875
|
228 if (nel > 0) |
3220
|
229 { |
4875
|
230 MT t1 (matrix.reshape (dim_vector (nel, 1))); |
|
231 |
|
232 boolNDArray t2 = t1.all (); |
|
233 |
4915
|
234 retval = t2(0); |
3220
|
235 } |
4875
|
236 |
3220
|
237 return retval; |
|
238 } |
|
239 |
|
240 template <class MT> |
3219
|
241 bool |
|
242 octave_base_matrix<MT>::print_as_scalar (void) const |
|
243 { |
4654
|
244 dim_vector dv = dims (); |
3219
|
245 |
4654
|
246 return (dv.all_ones () || dv.any_zero ()); |
3219
|
247 } |
|
248 |
|
249 template <class MT> |
|
250 void |
3523
|
251 octave_base_matrix<MT>::print (std::ostream& os, bool pr_as_read_syntax) const |
3219
|
252 { |
|
253 print_raw (os, pr_as_read_syntax); |
|
254 newline (os); |
|
255 } |
|
256 |
|
257 template <class MT> |
|
258 void |
3933
|
259 octave_base_matrix<MT>::print_info (std::ostream& os, |
|
260 const std::string& prefix) const |
|
261 { |
|
262 matrix.print_info (os, prefix); |
|
263 } |
|
264 |
3219
|
265 /* |
|
266 ;;; Local Variables: *** |
|
267 ;;; mode: C++ *** |
|
268 ;;; End: *** |
|
269 */ |