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 { |
|
42 if (ra_idx(i) < 0 || ra_idx(i) > dimensions (i)) |
|
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 |
|
211 for (int i = 0; i < n; i++) |
|
212 retval(i) = ra_idx(i).freeze (dimensions(i), "XXX FIXME XXX", resize_ok); |
|
213 |
|
214 return retval; |
|
215 } |
|
216 |
|
217 bool |
|
218 vector_equivalent (const Array<int>& ra_idx) |
|
219 { |
|
220 int n = ra_idx.length (); |
|
221 |
|
222 bool found_first = false; |
|
223 |
|
224 for (int i = 0; i < n; i++) |
|
225 { |
|
226 if (ra_idx(i) != 1) |
|
227 { |
|
228 if (! found_first) |
|
229 found_first = true; |
|
230 else |
|
231 return false; |
|
232 } |
|
233 } |
|
234 |
|
235 return true; |
|
236 } |
|
237 |
|
238 bool |
|
239 equal_arrays (const dim_vector& a, const dim_vector& b) |
|
240 { |
|
241 bool retval = true; |
|
242 |
|
243 if (a.length () != b.length ()) |
|
244 retval = false; |
|
245 else |
|
246 { |
|
247 for (int i = 0; i < a.length (); i++) |
|
248 { |
|
249 if (a(i) != b(i)) |
|
250 retval = false; |
|
251 } |
|
252 } |
|
253 |
|
254 return retval; |
|
255 } |
|
256 |
|
257 bool |
|
258 all_ok (const Array<idx_vector>& ra_idx) |
|
259 { |
|
260 bool retval = true; |
|
261 |
|
262 int n = ra_idx.length (); |
|
263 |
|
264 for (int i = 0; i < n; i++) |
|
265 { |
|
266 if (! ra_idx(i)) |
|
267 { |
|
268 retval = false; |
|
269 break; |
|
270 } |
|
271 } |
|
272 |
|
273 return retval; |
|
274 } |
|
275 |
|
276 bool |
|
277 any_orig_empty (const Array<idx_vector>& ra_idx) |
|
278 { |
|
279 bool retval = false; |
|
280 |
|
281 int n = ra_idx.length (); |
|
282 |
|
283 for (int i = 0; i < n; i++) |
|
284 { |
|
285 if (ra_idx(i).orig_empty ()) |
|
286 { |
|
287 retval = true; |
|
288 break; |
|
289 } |
|
290 } |
|
291 |
|
292 return retval; |
|
293 } |
|
294 |
|
295 bool |
|
296 any_zero_len (const dim_vector& frozen_lengths) |
|
297 { |
|
298 bool retval = false; |
|
299 |
|
300 int n = frozen_lengths.length (); |
|
301 |
|
302 for (int i = 0; i < n; i++) |
|
303 { |
|
304 if (frozen_lengths(i) == 0) |
|
305 { |
|
306 retval = true; |
|
307 break; |
|
308 } |
|
309 } |
|
310 |
|
311 return retval; |
|
312 } |
|
313 |
|
314 dim_vector |
4663
|
315 get_zero_len_size (const dim_vector& /* frozen_lengths */, |
|
316 const dim_vector& /* dimensions */) |
4588
|
317 { |
|
318 dim_vector retval; |
|
319 assert (0); |
|
320 return retval; |
|
321 } |
|
322 |
|
323 bool |
|
324 all_colon_equiv (const Array<idx_vector>& ra_idx, |
|
325 const dim_vector& frozen_lengths) |
|
326 { |
|
327 bool retval = true; |
|
328 |
|
329 int idx_n = ra_idx.length (); |
|
330 |
|
331 int n = frozen_lengths.length (); |
|
332 |
|
333 assert (idx_n == n); |
|
334 |
|
335 for (int i = 0; i < n; i++) |
|
336 { |
|
337 if (! ra_idx(i).is_colon_equiv (frozen_lengths(i))) |
|
338 { |
|
339 retval = false; |
|
340 break; |
|
341 } |
|
342 } |
|
343 |
|
344 return retval; |
|
345 } |
|
346 |
|
347 bool |
|
348 is_in (int num, const idx_vector& idx) |
|
349 { |
|
350 int n = idx.capacity (); |
|
351 |
|
352 for (int i = 0; i < n; i++) |
|
353 if (idx.elem (i) == num) |
|
354 return true; |
|
355 |
|
356 return false; |
|
357 } |
|
358 |
|
359 int |
|
360 how_many_lgt (const int num, idx_vector& idxv) |
|
361 { |
|
362 int retval = 0; |
|
363 |
|
364 int n = idxv.capacity (); |
|
365 |
|
366 for (int i = 0; i < n; i++) |
|
367 { |
|
368 if (num > idxv.elem (i)) |
|
369 retval++; |
|
370 } |
|
371 |
|
372 return retval; |
|
373 } |
|
374 |
|
375 bool |
|
376 all_ones (const Array<int>& arr) |
|
377 { |
|
378 bool retval = true; |
|
379 |
|
380 for (int i = 0; i < arr.length (); i++) |
|
381 { |
|
382 if (arr(i) != 1) |
|
383 { |
|
384 retval = false; |
|
385 break; |
|
386 } |
|
387 } |
|
388 |
|
389 return retval; |
|
390 } |
|
391 |
|
392 Array<int> |
|
393 get_elt_idx (const Array<idx_vector>& ra_idx, const Array<int>& result_idx) |
|
394 { |
|
395 int n = ra_idx.length (); |
|
396 |
|
397 Array<int> retval (n); |
|
398 |
|
399 for (int i = 0; i < n; i++) |
|
400 retval(i) = ra_idx(i).elem (result_idx(i)); |
|
401 |
|
402 return retval; |
|
403 } |
|
404 |
|
405 int |
|
406 number_of_elements (const dim_vector ra_idx) |
|
407 { |
|
408 int retval = 1; |
|
409 |
|
410 int n = ra_idx.length (); |
|
411 |
|
412 if (n == 0) |
|
413 retval = 0; |
|
414 |
|
415 for (int i = 0; i < n; i++) |
|
416 retval *= ra_idx(i); |
|
417 |
|
418 return retval; |
|
419 } |
|
420 |
|
421 Array<int> |
|
422 get_ra_idx (int idx, const dim_vector& dims) |
|
423 { |
|
424 Array<int> retval; |
|
425 |
|
426 int n_dims = dims.length (); |
|
427 |
|
428 retval.resize (n_dims); |
|
429 |
|
430 for (int i = 0; i < n_dims; i++) |
|
431 retval(i) = 0; |
|
432 |
|
433 assert (idx > 0 || idx < number_of_elements (dims)); |
|
434 |
|
435 for (int i = 0; i < idx; i++) |
|
436 increment_index (retval, dims); |
|
437 |
|
438 // XXX FIXME XXX -- the solution using increment_index is not |
|
439 // efficient. |
|
440 |
|
441 #if 0 |
|
442 int var = 1; |
|
443 for (int i = 0; i < n_dims; i++) |
|
444 { |
|
445 std::cout << "idx: " << idx << ", var: " << var << ", dims(" << i << "): " << dims(i) <<"\n"; |
|
446 retval(i) = ((int)floor(((idx) / (double)var))) % dims(i); |
|
447 idx -= var * retval(i); |
|
448 var = dims(i); |
|
449 } |
|
450 #endif |
|
451 |
|
452 return retval; |
|
453 } |
|
454 |
|
455 dim_vector |
|
456 short_freeze (Array<idx_vector>& ra_idx, const dim_vector& dimensions, |
|
457 int resize_ok) |
|
458 { |
|
459 dim_vector retval; |
|
460 |
|
461 int n = ra_idx.length (); |
|
462 |
|
463 int n_dims = dimensions.length (); |
|
464 |
|
465 if (n == n_dims) |
|
466 { |
|
467 retval = freeze (ra_idx, dimensions, resize_ok); |
|
468 } |
|
469 else if (n < n_dims) |
|
470 { |
|
471 retval.resize (n); |
|
472 |
|
473 for (int i = 0; i < n - 1; i++) |
|
474 retval(i) = ra_idx(i).freeze (dimensions(i), "dimension", resize_ok); |
|
475 |
|
476 int size_left = 1; |
|
477 |
|
478 for (int i = n - 1; i < n_dims; i++) |
|
479 size_left *= dimensions(i); |
|
480 |
|
481 if (ra_idx(n-1).is_colon()) |
|
482 { |
|
483 retval(n-1) = size_left; |
|
484 } |
|
485 else |
|
486 { |
|
487 int last_ra_idx = ra_idx(n-1)(0); |
|
488 |
|
489 if (last_ra_idx < dimensions(n - 1)) |
|
490 { |
|
491 retval(n - 1) = ra_idx(n - 1).freeze (dimensions(n-1), |
|
492 "dimension", resize_ok); |
|
493 } |
|
494 else |
|
495 { |
|
496 if (size_left <= last_ra_idx) |
|
497 { |
|
498 // Make it larger than it should be to get an error |
|
499 // later. |
|
500 |
|
501 retval.resize(n_dims + 1); |
|
502 |
|
503 (*current_liboctave_error_handler) |
|
504 ("index exceeds N-d array dimensions"); |
|
505 } |
|
506 else |
|
507 { |
|
508 retval(n-1) = 1; |
|
509 } |
|
510 } |
|
511 } |
|
512 } |
|
513 |
|
514 return retval; |
|
515 } |
|
516 |
4593
|
517 Array<int> |
|
518 calc_permutated_idx (const Array<int>& old_idx, |
|
519 const Array<int>& perm_vec, bool inv) |
|
520 { |
|
521 int n_el = old_idx.length (); |
|
522 |
|
523 Array<int> retval (n_el); |
|
524 |
|
525 for (int i = 0; i < n_el; i++) |
|
526 { |
|
527 if (inv) |
|
528 retval(perm_vec(i)-1) = old_idx(i); |
|
529 else |
|
530 retval(i) = old_idx(perm_vec(i)-1); |
|
531 } |
|
532 |
|
533 return retval; |
|
534 } |
|
535 |
4669
|
536 void |
|
537 gripe_nonconformant (const char *op, int op1_len, int op2_len) |
|
538 { |
|
539 (*current_liboctave_error_handler) |
|
540 ("%s: nonconformant arguments (op1 len: %d, op2 len: %d)", |
|
541 op, op1_len, op2_len); |
|
542 } |
|
543 |
|
544 void |
|
545 gripe_nonconformant (const char *op, int op1_nr, int op1_nc, |
|
546 int op2_nr, int op2_nc) |
|
547 { |
|
548 (*current_liboctave_error_handler) |
|
549 ("%s: nonconformant arguments (op1 is %dx%d, op2 is %dx%d)", |
|
550 op, op1_nr, op1_nc, op2_nr, op2_nc); |
|
551 } |
|
552 |
|
553 void |
|
554 gripe_nonconformant (const char *op, dim_vector& op1_dims, |
|
555 dim_vector& op2_dims) |
|
556 { |
|
557 std::string op1_dims_str = op1_dims.str (); |
|
558 std::string op2_dims_str = op2_dims.str (); |
|
559 |
|
560 (*current_liboctave_error_handler) |
|
561 ("%s: nonconformant arguments (op1 is %s, op2 is %s)", |
|
562 op, op1_dims_str.c_str (), op2_dims_str.c_str ()); |
|
563 } |
|
564 |
4588
|
565 /* |
|
566 ;;; Local Variables: *** |
|
567 ;;; mode: C++ *** |
|
568 ;;; End: *** |
|
569 */ |