[Numpy-discussion] Flattening an array

Jake VanderPlas jakevdp at gmail.com
Tue Dec 8 19:29:47 EST 2009


Hello,
I have a function -- call it f() -- which takes a length-N 1D numpy
array as an argument, and returns a length-N 1D array.
I want to pass it the data in an N-D array, and obtain the N-D array
of the result.
I've thought about wrapping it as such:

#python code:
from my_module import f   # takes a 1D array, raises an exception otherwise
def f_wrap(A):
    A_1D = A.ravel()
    B = f(A_1D)
    return B.reshape(A.shape)
#end code

I expect A to be contiguous in memory, but I don't know if it will be
C_CONTIGUOUS or F_CONTIGUOUS.  Is there a way to implement this such
that
  1) the data in the arrays A and B_1D are not copied (memory issues)
  2) the function f is only called once (speed issues)?
The above implementation appears to copy data if A is fortran-ordered.
 Thanks for the help
   -Jake



More information about the NumPy-Discussion mailing list