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