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 |
4192
|
23 #if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) |
3219
|
24 #pragma implementation |
|
25 #endif |
|
26 |
|
27 #ifdef HAVE_CONFIG_H |
|
28 #include <config.h> |
|
29 #endif |
|
30 |
3503
|
31 #include <iostream> |
3219
|
32 |
3933
|
33 #include "Cell.h" |
3220
|
34 #include "oct-obj.h" |
3933
|
35 #include "oct-map.h" |
3219
|
36 #include "ov-base.h" |
|
37 #include "ov-base-mat.h" |
3572
|
38 #include "pr-output.h" |
3219
|
39 |
|
40 template <class MT> |
3220
|
41 octave_value |
3933
|
42 octave_base_matrix<MT>::subsref (const std::string type, |
|
43 const SLList<octave_value_list>& idx) |
|
44 { |
|
45 octave_value retval; |
|
46 |
|
47 switch (type[0]) |
|
48 { |
|
49 case '(': |
|
50 retval = do_index_op (idx.front ()); |
|
51 break; |
|
52 |
|
53 case '{': |
|
54 case '.': |
|
55 { |
|
56 std::string nm = type_name (); |
|
57 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); |
|
58 } |
|
59 break; |
|
60 |
|
61 default: |
|
62 panic_impossible (); |
|
63 } |
|
64 |
|
65 return retval.next_subsref (type, idx); |
|
66 } |
|
67 |
|
68 template <class MT> |
|
69 octave_value |
|
70 octave_base_matrix<MT>::subsasgn (const std::string type, |
|
71 const SLList<octave_value_list>& idx, |
|
72 const octave_value& rhs) |
|
73 { |
|
74 octave_value retval; |
|
75 |
|
76 switch (type[0]) |
|
77 { |
|
78 case '(': |
|
79 { |
|
80 if (type.length () == 1) |
|
81 retval = numeric_assign (type, idx, rhs); |
|
82 else |
|
83 { |
|
84 std::string nm = type_name (); |
|
85 error ("in indexed assignment of %s, last rhs index must be ()", |
|
86 nm.c_str ()); |
|
87 } |
|
88 } |
|
89 break; |
|
90 |
|
91 case '{': |
|
92 case '.': |
|
93 { |
|
94 std::string nm = type_name (); |
|
95 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); |
|
96 } |
|
97 break; |
|
98 |
|
99 default: |
|
100 panic_impossible (); |
|
101 } |
|
102 |
|
103 return retval; |
|
104 } |
|
105 |
|
106 template <class MT> |
|
107 octave_value |
|
108 octave_base_matrix<MT>::do_index_op (const octave_value_list& idx, |
|
109 int resize_ok) |
3220
|
110 { |
|
111 octave_value retval; |
|
112 |
|
113 int len = idx.length (); |
|
114 |
|
115 switch (len) |
|
116 { |
|
117 case 2: |
|
118 { |
|
119 idx_vector i = idx (0).index_vector (); |
|
120 idx_vector j = idx (1).index_vector (); |
|
121 |
3933
|
122 retval = MT (matrix.index (i, j, resize_ok, MT::resize_fill_value ())); |
3220
|
123 } |
|
124 break; |
|
125 |
|
126 case 1: |
|
127 { |
|
128 idx_vector i = idx (0).index_vector (); |
|
129 |
3933
|
130 retval = MT (matrix.index (i, resize_ok, MT::resize_fill_value ())); |
3220
|
131 } |
|
132 break; |
|
133 |
|
134 default: |
|
135 { |
3523
|
136 std::string n = type_name (); |
3220
|
137 |
|
138 error ("invalid number of indices (%d) for %s value", |
|
139 len, n.c_str ()); |
|
140 } |
|
141 break; |
|
142 } |
|
143 |
|
144 return retval; |
|
145 } |
|
146 |
3928
|
147 #if !defined (CXX_NEW_FRIEND_TEMPLATE_DECL) |
|
148 template <class MT> |
|
149 extern void assign (MT&, const MT&); |
|
150 #endif |
|
151 |
|
152 template <class MT> |
|
153 void |
|
154 octave_base_matrix<MT>::assign (const octave_value_list& idx, const MT& rhs) |
|
155 { |
|
156 int len = idx.length (); |
|
157 |
|
158 switch (len) |
|
159 { |
|
160 case 2: |
|
161 { |
|
162 idx_vector i = idx (0).index_vector (); |
|
163 idx_vector j = idx (1).index_vector (); |
|
164 |
|
165 matrix.set_index (i); |
|
166 matrix.set_index (j); |
|
167 |
|
168 ::assign (matrix, rhs); |
|
169 } |
|
170 break; |
|
171 |
|
172 case 1: |
|
173 { |
|
174 idx_vector i = idx (0).index_vector (); |
|
175 |
|
176 matrix.set_index (i); |
|
177 |
|
178 ::assign (matrix, rhs); |
|
179 } |
|
180 break; |
|
181 |
|
182 default: |
|
183 error ("invalid number of indices (%d) for indexed assignment", |
|
184 len); |
|
185 break; |
|
186 } |
|
187 } |
|
188 |
3220
|
189 template <class MT> |
|
190 bool |
|
191 octave_base_matrix<MT>::is_true (void) const |
|
192 { |
|
193 bool retval = false; |
|
194 |
|
195 if (rows () == 0 || columns () == 0) |
|
196 { |
|
197 int flag = Vpropagate_empty_matrices; |
|
198 |
|
199 if (flag < 0) |
|
200 warning ("empty matrix used in conditional expression"); |
|
201 else if (flag == 0) |
|
202 error ("empty matrix used in conditional expression"); |
|
203 } |
|
204 else |
|
205 { |
4017
|
206 boolMatrix m = (matrix.all () . all ()); |
3220
|
207 |
4017
|
208 retval = (m.rows () == 1 && m.columns () == 1 && m(0,0)); |
3220
|
209 } |
|
210 |
|
211 return retval; |
|
212 } |
|
213 |
|
214 template <class MT> |
3219
|
215 bool |
|
216 octave_base_matrix<MT>::print_as_scalar (void) const |
|
217 { |
|
218 int nr = rows (); |
|
219 int nc = columns (); |
|
220 |
|
221 return (nr == 1 && nc == 1 || (nr == 0 || nc == 0)); |
|
222 } |
|
223 |
|
224 template <class MT> |
|
225 void |
3523
|
226 octave_base_matrix<MT>::print (std::ostream& os, bool pr_as_read_syntax) const |
3219
|
227 { |
|
228 print_raw (os, pr_as_read_syntax); |
|
229 newline (os); |
|
230 } |
|
231 |
|
232 template <class MT> |
|
233 void |
3933
|
234 octave_base_matrix<MT>::print_raw (std::ostream& os, |
|
235 bool pr_as_read_syntax) const |
3219
|
236 { |
|
237 octave_print_internal (os, matrix, pr_as_read_syntax, |
|
238 current_print_indent_level ()); |
|
239 } |
|
240 |
|
241 template <class MT> |
|
242 bool |
3933
|
243 octave_base_matrix<MT>::print_name_tag (std::ostream& os, |
|
244 const std::string& name) const |
3219
|
245 { |
|
246 bool retval = false; |
|
247 |
|
248 indent (os); |
|
249 |
|
250 if (print_as_scalar ()) |
|
251 os << name << " = "; |
|
252 else |
|
253 { |
|
254 os << name << " ="; |
|
255 newline (os); |
|
256 newline (os); |
|
257 retval = true; |
|
258 } |
|
259 |
|
260 return retval; |
|
261 } |
|
262 |
3933
|
263 template <class MT> |
|
264 void |
|
265 octave_base_matrix<MT>::print_info (std::ostream& os, |
|
266 const std::string& prefix) const |
|
267 { |
|
268 matrix.print_info (os, prefix); |
|
269 } |
|
270 |
3219
|
271 /* |
|
272 ;;; Local Variables: *** |
|
273 ;;; mode: C++ *** |
|
274 ;;; End: *** |
|
275 */ |