4588
|
1 /* |
|
2 |
|
3 Copyright (C) 2003 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 |
|
27 #include "Array-util.h" |
4669
|
28 #include "dim-vector.h" |
|
29 #include "lo-error.h" |
4588
|
30 |
|
31 bool |
|
32 index_in_bounds (const Array<int>& ra_idx, const dim_vector& dimensions) |
|
33 { |
|
34 bool retval = true; |
|
35 |
|
36 int n = ra_idx.length (); |
|
37 |
|
38 if (n == dimensions.length ()) |
|
39 { |
|
40 for (int i = 0; i < n; i++) |
|
41 { |
4747
|
42 if (ra_idx(i) < 0 || ra_idx(i) >= dimensions(i)) |
4588
|
43 { |
|
44 retval = false; |
|
45 break; |
|
46 } |
|
47 } |
|
48 } |
|
49 else |
|
50 retval = false; |
|
51 |
|
52 return retval; |
|
53 } |
|
54 |
|
55 void |
|
56 increment_index (Array<int>& ra_idx, const dim_vector& dimensions, |
|
57 int start_dimension) |
|
58 { |
|
59 ra_idx(start_dimension)++; |
|
60 |
|
61 int n = ra_idx.length () - 1; |
|
62 |
|
63 for (int i = start_dimension; i < n; i++) |
|
64 { |
|
65 if (ra_idx(i) < dimensions(i)) |
|
66 break; |
|
67 else |
|
68 { |
|
69 ra_idx(i) = 0; |
|
70 ra_idx(i+1)++; |
|
71 } |
|
72 } |
|
73 } |
|
74 |
|
75 int |
|
76 get_scalar_idx (Array<int>& idx, dim_vector& dims) |
|
77 { |
|
78 int retval (-1); |
|
79 |
|
80 int n = idx.length (); |
|
81 |
|
82 if (n > 0) |
|
83 { |
|
84 retval = idx(--n); |
|
85 |
|
86 while (--n >= 0) |
|
87 { |
|
88 retval *= dims (n); |
|
89 |
|
90 retval += idx(n); |
|
91 } |
|
92 } |
|
93 return retval; |
|
94 } |
|
95 |
|
96 int |
|
97 num_ones (const Array<int>& ra_idx) |
|
98 { |
4635
|
99 int retval = 0; |
|
100 |
4588
|
101 for (int i = 0; i < ra_idx.length (); i++) |
|
102 { |
|
103 if (ra_idx (i) == 1) |
|
104 retval++; |
|
105 } |
4635
|
106 |
4588
|
107 return retval; |
|
108 } |
|
109 |
|
110 bool |
|
111 is_scalar (const dim_vector& dim) |
|
112 { |
|
113 bool retval = true; |
|
114 |
|
115 int n = dim.length (); |
|
116 |
|
117 if (n == 0) |
|
118 { |
|
119 retval = false; |
|
120 } |
|
121 else |
|
122 { |
|
123 for (int i = 0; i < n; i ++) |
|
124 { |
|
125 if (dim (i) != 1) |
|
126 { |
|
127 retval = false; |
|
128 |
|
129 break; |
|
130 } |
|
131 } |
|
132 } |
|
133 return retval; |
|
134 } |
|
135 |
|
136 bool |
|
137 any_ones (const Array<int>& arr) |
|
138 { |
|
139 bool retval = false; |
|
140 |
|
141 for (int i = 0; i < arr.length (); i++) |
|
142 { |
|
143 if (arr (i) == 1) |
|
144 { |
|
145 retval = true; |
|
146 |
|
147 break; |
|
148 } |
|
149 } |
|
150 return retval; |
|
151 } |
|
152 |
|
153 int |
|
154 compute_index (const Array<int>& ra_idx, const dim_vector& dims) |
|
155 { |
|
156 int retval = -1; |
|
157 |
|
158 int n = dims.length (); |
|
159 |
|
160 if (n > 0 && n == ra_idx.length ()) |
|
161 { |
|
162 retval = ra_idx(--n); |
|
163 |
|
164 while (--n >= 0) |
|
165 { |
|
166 retval *= dims(n); |
|
167 |
|
168 retval += ra_idx(n); |
|
169 } |
|
170 } |
|
171 else |
|
172 (*current_liboctave_error_handler) |
|
173 ("ArrayN<T>::compute_index: invalid ra_idxing operation"); |
|
174 |
|
175 return retval; |
|
176 } |
|
177 |
|
178 Array<int> |
|
179 conv_to_int_array (const Array<idx_vector>& a) |
|
180 { |
|
181 Array<int> retval (a.length ()); |
|
182 |
|
183 for (int i = 0; i < a.length (); i++) |
|
184 retval (i) = a(i).elem (0); |
|
185 |
|
186 return retval; |
|
187 } |
|
188 |
|
189 Array<idx_vector> |
|
190 conv_to_array (const idx_vector *tmp, const int len) |
|
191 { |
|
192 Array<idx_vector> retval (len); |
|
193 |
|
194 for (int i = 0; i < len; i++) |
|
195 retval (i) = tmp[i]; |
|
196 |
|
197 return retval; |
|
198 } |
|
199 |
|
200 dim_vector |
|
201 freeze (Array<idx_vector>& ra_idx, const dim_vector& dimensions, int resize_ok) |
|
202 { |
|
203 dim_vector retval; |
|
204 |
|
205 int n = ra_idx.length (); |
|
206 |
|
207 assert (n == dimensions.length ()); |
|
208 |
|
209 retval.resize (n); |
|
210 |
5136
|
211 static const char *tag[3] = { "row", "column", 0 }; |
|
212 |
4588
|
213 for (int i = 0; i < n; i++) |
5136
|
214 retval(i) = ra_idx(i).freeze (dimensions(i), tag[i < 2 ? i : 3], |
|
215 resize_ok); |
4588
|
216 |
|
217 return retval; |
|
218 } |
|
219 |
|
220 bool |
|
221 vector_equivalent (const Array<int>& ra_idx) |
|
222 { |
|
223 int n = ra_idx.length (); |
|
224 |
|
225 bool found_first = false; |
|
226 |
|
227 for (int i = 0; i < n; i++) |
|
228 { |
|
229 if (ra_idx(i) != 1) |
|
230 { |
|
231 if (! found_first) |
|
232 found_first = true; |
|
233 else |
|
234 return false; |
|
235 } |
|
236 } |
|
237 |
|
238 return true; |
|
239 } |
|
240 |
|
241 bool |
|
242 all_ok (const Array<idx_vector>& ra_idx) |
|
243 { |
|
244 bool retval = true; |
|
245 |
|
246 int n = ra_idx.length (); |
|
247 |
|
248 for (int i = 0; i < n; i++) |
|
249 { |
|
250 if (! ra_idx(i)) |
|
251 { |
|
252 retval = false; |
|
253 break; |
|
254 } |
|
255 } |
|
256 |
|
257 return retval; |
|
258 } |
|
259 |
|
260 bool |
|
261 any_orig_empty (const Array<idx_vector>& ra_idx) |
|
262 { |
|
263 bool retval = false; |
|
264 |
|
265 int n = ra_idx.length (); |
|
266 |
|
267 for (int i = 0; i < n; i++) |
|
268 { |
|
269 if (ra_idx(i).orig_empty ()) |
|
270 { |
|
271 retval = true; |
|
272 break; |
|
273 } |
|
274 } |
|
275 |
|
276 return retval; |
|
277 } |
|
278 |
|
279 bool |
|
280 all_colon_equiv (const Array<idx_vector>& ra_idx, |
|
281 const dim_vector& frozen_lengths) |
|
282 { |
|
283 bool retval = true; |
|
284 |
|
285 int idx_n = ra_idx.length (); |
|
286 |
|
287 int n = frozen_lengths.length (); |
|
288 |
|
289 assert (idx_n == n); |
|
290 |
|
291 for (int i = 0; i < n; i++) |
|
292 { |
|
293 if (! ra_idx(i).is_colon_equiv (frozen_lengths(i))) |
|
294 { |
|
295 retval = false; |
|
296 break; |
|
297 } |
|
298 } |
|
299 |
|
300 return retval; |
|
301 } |
|
302 |
|
303 bool |
|
304 is_in (int num, const idx_vector& idx) |
|
305 { |
|
306 int n = idx.capacity (); |
|
307 |
|
308 for (int i = 0; i < n; i++) |
|
309 if (idx.elem (i) == num) |
|
310 return true; |
|
311 |
|
312 return false; |
|
313 } |
|
314 |
|
315 int |
|
316 how_many_lgt (const int num, idx_vector& idxv) |
|
317 { |
|
318 int retval = 0; |
|
319 |
|
320 int n = idxv.capacity (); |
|
321 |
|
322 for (int i = 0; i < n; i++) |
|
323 { |
|
324 if (num > idxv.elem (i)) |
|
325 retval++; |
|
326 } |
|
327 |
|
328 return retval; |
|
329 } |
|
330 |
|
331 bool |
|
332 all_ones (const Array<int>& arr) |
|
333 { |
|
334 bool retval = true; |
|
335 |
|
336 for (int i = 0; i < arr.length (); i++) |
|
337 { |
|
338 if (arr(i) != 1) |
|
339 { |
|
340 retval = false; |
|
341 break; |
|
342 } |
|
343 } |
|
344 |
|
345 return retval; |
|
346 } |
|
347 |
|
348 Array<int> |
|
349 get_elt_idx (const Array<idx_vector>& ra_idx, const Array<int>& result_idx) |
|
350 { |
|
351 int n = ra_idx.length (); |
|
352 |
|
353 Array<int> retval (n); |
|
354 |
|
355 for (int i = 0; i < n; i++) |
|
356 retval(i) = ra_idx(i).elem (result_idx(i)); |
|
357 |
|
358 return retval; |
|
359 } |
|
360 |
|
361 Array<int> |
|
362 get_ra_idx (int idx, const dim_vector& dims) |
|
363 { |
|
364 Array<int> retval; |
|
365 |
|
366 int n_dims = dims.length (); |
|
367 |
|
368 retval.resize (n_dims); |
|
369 |
|
370 for (int i = 0; i < n_dims; i++) |
|
371 retval(i) = 0; |
|
372 |
4747
|
373 assert (idx > 0 || idx < dims.numel ()); |
4588
|
374 |
|
375 for (int i = 0; i < idx; i++) |
|
376 increment_index (retval, dims); |
|
377 |
|
378 // XXX FIXME XXX -- the solution using increment_index is not |
|
379 // efficient. |
|
380 |
|
381 #if 0 |
|
382 int var = 1; |
|
383 for (int i = 0; i < n_dims; i++) |
|
384 { |
|
385 std::cout << "idx: " << idx << ", var: " << var << ", dims(" << i << "): " << dims(i) <<"\n"; |
|
386 retval(i) = ((int)floor(((idx) / (double)var))) % dims(i); |
|
387 idx -= var * retval(i); |
|
388 var = dims(i); |
|
389 } |
|
390 #endif |
|
391 |
|
392 return retval; |
|
393 } |
|
394 |
|
395 dim_vector |
|
396 short_freeze (Array<idx_vector>& ra_idx, const dim_vector& dimensions, |
|
397 int resize_ok) |
|
398 { |
|
399 dim_vector retval; |
|
400 |
|
401 int n = ra_idx.length (); |
|
402 |
|
403 int n_dims = dimensions.length (); |
|
404 |
|
405 if (n == n_dims) |
|
406 { |
|
407 retval = freeze (ra_idx, dimensions, resize_ok); |
|
408 } |
|
409 else if (n < n_dims) |
|
410 { |
|
411 retval.resize (n); |
|
412 |
|
413 for (int i = 0; i < n - 1; i++) |
|
414 retval(i) = ra_idx(i).freeze (dimensions(i), "dimension", resize_ok); |
|
415 |
|
416 int size_left = 1; |
|
417 |
|
418 for (int i = n - 1; i < n_dims; i++) |
|
419 size_left *= dimensions(i); |
|
420 |
|
421 if (ra_idx(n-1).is_colon()) |
|
422 { |
|
423 retval(n-1) = size_left; |
|
424 } |
|
425 else |
|
426 { |
|
427 int last_ra_idx = ra_idx(n-1)(0); |
4887
|
428 for (int i = 1; i < ra_idx (n - 1).capacity (); i++) |
|
429 last_ra_idx = (last_ra_idx > ra_idx(n-1)(i) ? last_ra_idx : |
|
430 ra_idx(n-1)(i)); |
4588
|
431 |
4887
|
432 if (last_ra_idx < size_left) |
4588
|
433 { |
4887
|
434 retval(n-1) = ra_idx(n-1).freeze (size_left, |
|
435 "dimension", resize_ok); |
4588
|
436 } |
|
437 else |
|
438 { |
4887
|
439 // Make it larger than it should be to get an error |
|
440 // later. |
4588
|
441 |
4887
|
442 retval.resize (n_dims+1); |
|
443 |
|
444 (*current_liboctave_error_handler) |
|
445 ("index exceeds N-d array dimensions"); |
4588
|
446 } |
|
447 } |
|
448 } |
|
449 |
|
450 return retval; |
|
451 } |
|
452 |
4593
|
453 Array<int> |
|
454 calc_permutated_idx (const Array<int>& old_idx, |
|
455 const Array<int>& perm_vec, bool inv) |
|
456 { |
|
457 int n_el = old_idx.length (); |
|
458 |
|
459 Array<int> retval (n_el); |
|
460 |
|
461 for (int i = 0; i < n_el; i++) |
|
462 { |
|
463 if (inv) |
|
464 retval(perm_vec(i)-1) = old_idx(i); |
|
465 else |
|
466 retval(i) = old_idx(perm_vec(i)-1); |
|
467 } |
|
468 |
|
469 return retval; |
|
470 } |
|
471 |
4669
|
472 void |
|
473 gripe_nonconformant (const char *op, int op1_len, int op2_len) |
|
474 { |
|
475 (*current_liboctave_error_handler) |
|
476 ("%s: nonconformant arguments (op1 len: %d, op2 len: %d)", |
|
477 op, op1_len, op2_len); |
|
478 } |
|
479 |
|
480 void |
|
481 gripe_nonconformant (const char *op, int op1_nr, int op1_nc, |
|
482 int op2_nr, int op2_nc) |
|
483 { |
|
484 (*current_liboctave_error_handler) |
|
485 ("%s: nonconformant arguments (op1 is %dx%d, op2 is %dx%d)", |
|
486 op, op1_nr, op1_nc, op2_nr, op2_nc); |
|
487 } |
|
488 |
|
489 void |
|
490 gripe_nonconformant (const char *op, dim_vector& op1_dims, |
|
491 dim_vector& op2_dims) |
|
492 { |
|
493 std::string op1_dims_str = op1_dims.str (); |
|
494 std::string op2_dims_str = op2_dims.str (); |
|
495 |
|
496 (*current_liboctave_error_handler) |
|
497 ("%s: nonconformant arguments (op1 is %s, op2 is %s)", |
|
498 op, op1_dims_str.c_str (), op2_dims_str.c_str ()); |
|
499 } |
|
500 |
4588
|
501 /* |
|
502 ;;; Local Variables: *** |
|
503 ;;; mode: C++ *** |
|
504 ;;; End: *** |
|
505 */ |