[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Can I de-loop?

OK, I'm trying to remove the loops in two IDL routines, and I
can't come up with good methods for either.  Thought I'd ask for
suggestions in case I'm missing the obvious.

a) I am adding a constant amount K to several elements N of array A.
The elements Ni are not unique; if an Ni is repeated, I want to add it
to A each time.  For instance, given

A = [0,1,2]
N = [2,2,2]
K = 4

I want the result A = [0,1,14], not A = [0,1,6]

The obvious solution is

A[N] = A[N] + K

However, this doesn't work if Ni appears multiple times in N.  Using a
loop does the job correctly, but is several times slower:

for i = 0, n_elements(N)-1 do A[N[i]] = A[N[i]] - K

b) I'm looking for a general `outer product' routine that operates on
arrays in the same way IDL's # operator will take the outer product of
two vectors.  What I want is a function that's passed N- &
M-dimensional arrays, and returns an N+M-dimensional result, which
contains every pairwise combination of products.

It's easy enough to do this using nested loops.  Is it possible to do
it without loops, even for small dimensions (e.g., N=1, M=2)?



| Henry Throop  throop@broccoli.colorado.edu  (303) 492-1628 |
|       Laboratory for Atmospheric and Space Physics         |
|      University of Colorado, Boulder      80309-0392       |

Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.