Mercurial > hg > octave-lyh
annotate src/ov-base-mat.cc @ 8229:1bf51192fa1d
imported patch rundemos
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 16 Oct 2008 13:28:43 -0400 |
parents | 6c08e3921d3e |
children | 7cbe01c21986 |
rev | line source |
---|---|
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> |
8150
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
209 void |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
210 octave_base_matrix<MT>::delete_elements (const octave_value_list& idx) |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
211 { |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
212 octave_idx_type len = idx.length (); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
213 |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
214 Array<idx_vector> ra_idx (len); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
215 |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
216 for (octave_idx_type i = 0; i < len; i++) |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
217 ra_idx(i) = idx(i).index_vector (); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
218 |
8179
6c08e3921d3e
imported patch maybe_delete_fix.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8150
diff
changeset
|
219 matrix.maybe_delete_elements (ra_idx); |
8150
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
220 |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
221 // Invalidate the matrix type |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
222 typ.invalidate_type (); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
223 } |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
224 |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
225 template <class MT> |
5731 | 226 octave_value |
227 octave_base_matrix<MT>::resize (const dim_vector& dv, bool fill) const | |
228 { | |
229 MT retval (matrix); | |
230 if (fill) | |
231 retval.resize (dv, 0); | |
232 else | |
233 retval.resize (dv); | |
234 return retval; | |
235 } | |
236 | |
237 template <class MT> | |
3220 | 238 bool |
239 octave_base_matrix<MT>::is_true (void) const | |
240 { | |
241 bool retval = false; | |
4875 | 242 dim_vector dv = matrix.dims (); |
243 int nel = dv.numel (); | |
4915 | 244 |
4875 | 245 if (nel > 0) |
3220 | 246 { |
4875 | 247 MT t1 (matrix.reshape (dim_vector (nel, 1))); |
248 | |
249 boolNDArray t2 = t1.all (); | |
250 | |
4915 | 251 retval = t2(0); |
3220 | 252 } |
4875 | 253 |
3220 | 254 return retval; |
255 } | |
256 | |
257 template <class MT> | |
3219 | 258 bool |
259 octave_base_matrix<MT>::print_as_scalar (void) const | |
260 { | |
4654 | 261 dim_vector dv = dims (); |
3219 | 262 |
4654 | 263 return (dv.all_ones () || dv.any_zero ()); |
3219 | 264 } |
265 | |
266 template <class MT> | |
267 void | |
3523 | 268 octave_base_matrix<MT>::print (std::ostream& os, bool pr_as_read_syntax) const |
3219 | 269 { |
270 print_raw (os, pr_as_read_syntax); | |
271 newline (os); | |
272 } | |
273 | |
274 template <class MT> | |
275 void | |
3933 | 276 octave_base_matrix<MT>::print_info (std::ostream& os, |
277 const std::string& prefix) const | |
278 { | |
279 matrix.print_info (os, prefix); | |
280 } | |
281 | |
3219 | 282 /* |
283 ;;; Local Variables: *** | |
284 ;;; mode: C++ *** | |
285 ;;; End: *** | |
286 */ |