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 |
4247
|
42 octave_base_matrix<MT>::subsref (const std::string& type, |
4219
|
43 const std::list<octave_value_list>& idx) |
3933
|
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 |
4247
|
70 octave_base_matrix<MT>::subsasgn (const std::string& type, |
4219
|
71 const std::list<octave_value_list>& idx, |
3933
|
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); |
4436
|
82 else if (is_empty ()) |
|
83 { |
|
84 // Allow conversion of empty matrix to some other type in |
|
85 // cases like |
|
86 // |
|
87 // x = []; x(i).f = rhs |
|
88 |
|
89 octave_value tmp = octave_value::empty_conv (type, rhs); |
|
90 |
|
91 retval = tmp.subsasgn (type, idx, rhs); |
|
92 } |
3933
|
93 else |
|
94 { |
|
95 std::string nm = type_name (); |
4519
|
96 error ("in indexed assignment of %s, last lhs index must be ()", |
3933
|
97 nm.c_str ()); |
|
98 } |
|
99 } |
|
100 break; |
|
101 |
|
102 case '{': |
|
103 case '.': |
|
104 { |
|
105 std::string nm = type_name (); |
|
106 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); |
|
107 } |
|
108 break; |
|
109 |
|
110 default: |
|
111 panic_impossible (); |
|
112 } |
|
113 |
|
114 return retval; |
|
115 } |
|
116 |
|
117 template <class MT> |
|
118 octave_value |
|
119 octave_base_matrix<MT>::do_index_op (const octave_value_list& idx, |
|
120 int resize_ok) |
3220
|
121 { |
|
122 octave_value retval; |
|
123 |
4742
|
124 int n_idx = idx.length (); |
3220
|
125 |
4742
|
126 int nd = matrix.ndims (); |
3220
|
127 |
4742
|
128 switch (n_idx) |
|
129 { |
|
130 case 0: |
|
131 error ("invalid number of indices (= 0) for %d-dimensional array", nd); |
3220
|
132 break; |
|
133 |
|
134 case 1: |
|
135 { |
|
136 idx_vector i = idx (0).index_vector (); |
|
137 |
3933
|
138 retval = MT (matrix.index (i, resize_ok, MT::resize_fill_value ())); |
3220
|
139 } |
|
140 break; |
|
141 |
|
142 default: |
|
143 { |
4742
|
144 if (n_idx == 2 && nd == 2) |
|
145 { |
|
146 idx_vector i = idx (0).index_vector (); |
|
147 idx_vector j = idx (1).index_vector (); |
3220
|
148 |
4742
|
149 retval = MT (matrix.index (i, j, resize_ok, |
|
150 MT::resize_fill_value ())); |
|
151 } |
|
152 else |
|
153 { |
|
154 Array<idx_vector> idx_vec (n_idx); |
4513
|
155 |
4742
|
156 for (int i = 0; i < n_idx; i++) |
|
157 idx_vec(i) = idx(i).index_vector (); |
|
158 |
|
159 retval = MT (matrix.index (idx_vec, resize_ok, |
|
160 MT::resize_fill_value ())); |
|
161 } |
3220
|
162 } |
|
163 break; |
|
164 } |
|
165 |
|
166 return retval; |
|
167 } |
|
168 |
3928
|
169 template <class MT> |
|
170 void |
|
171 octave_base_matrix<MT>::assign (const octave_value_list& idx, const MT& rhs) |
|
172 { |
|
173 int len = idx.length (); |
|
174 |
4513
|
175 for (int i = 0; i < len; i++) |
|
176 matrix.set_index (idx(i).index_vector ()); |
3928
|
177 |
4513
|
178 ::assign (matrix, rhs, MT::resize_fill_value ()); |
3928
|
179 } |
|
180 |
3220
|
181 template <class MT> |
|
182 bool |
|
183 octave_base_matrix<MT>::is_true (void) const |
|
184 { |
|
185 bool retval = false; |
|
186 |
4556
|
187 if (matrix.dims () . length () == 2) |
3220
|
188 { |
4556
|
189 if (rows () > 0 && columns () > 0) |
|
190 { |
|
191 boolNDArray m = (matrix.all () . all ()); |
|
192 |
|
193 retval = (m.rows () == 1 && m.columns () == 1 && m(0,0)); |
|
194 } |
3220
|
195 } |
4556
|
196 else |
|
197 (*current_liboctave_error_handler) |
|
198 ("is_true not yet implemented for N-d Arrays"); |
|
199 |
3220
|
200 return retval; |
|
201 } |
|
202 |
|
203 template <class MT> |
3219
|
204 bool |
|
205 octave_base_matrix<MT>::print_as_scalar (void) const |
|
206 { |
4654
|
207 dim_vector dv = dims (); |
3219
|
208 |
4654
|
209 return (dv.all_ones () || dv.any_zero ()); |
3219
|
210 } |
|
211 |
|
212 template <class MT> |
|
213 void |
3523
|
214 octave_base_matrix<MT>::print (std::ostream& os, bool pr_as_read_syntax) const |
3219
|
215 { |
|
216 print_raw (os, pr_as_read_syntax); |
|
217 newline (os); |
|
218 } |
|
219 |
|
220 template <class MT> |
|
221 void |
3933
|
222 octave_base_matrix<MT>::print_info (std::ostream& os, |
|
223 const std::string& prefix) const |
|
224 { |
|
225 matrix.print_info (os, prefix); |
|
226 } |
|
227 |
3219
|
228 /* |
|
229 ;;; Local Variables: *** |
|
230 ;;; mode: C++ *** |
|
231 ;;; End: *** |
|
232 */ |