[Numpy-discussion] In-place operations

Pierre Thibault thibault at physics.cornell.edu
Tue Sep 12 08:52:03 CDT 2006


I would like to have information on the best techniques to do in-place
calculations and to minimize temporary array creations. To me this
seems to be very important whenever the arrays become very large.

I already know about the ufunc in-place functionality (which is great).

More specifically, here are examples that occured in my code

1) FFTs:  Let A and B be two large arrays, already allocated. I want
the fft of A to be stored in B. If I just type B = fft(A), there is a
temprary array creation, right? Is it possible to avoid that?

2) Function output: In general, I think the same thing happens with
functions like

def f1(array_in):
   array_out = # something using array_in
   return array_out

Then, if B is already allocated, writing B = f1(A) involves again a
temporary array creation

I thought instead of doing something like

def f2(array_in, array_out):
  array_out[:] = # something
  # Is this good practice?

and call f2(A,B).

If I understand well, this still requires a temporary array creation.
Is there another way of doing that (appart from actually looping
through the indices of A and B)?

I know that the use of f2 has one clear advantage: it makes sure that
whatever was in B is discarded. With f1, the following could happen:

A # contains something
B # contains something
C = B
B = f1(A)

C still contains whatever was in B. This could be what you wanted, but
if you consider C just as a reference to B, this is not good.

I guess these considerations are not standard python problems because
you expect python to take care of memory issues. With big arrays in
scientific computations, I feel the question is more relevant. I might
be wrong...


Pierre Thibault
616 Clark Hall, Cornell University
(607) 255-5522

More information about the Numpy-discussion mailing list