[Numpy-discussion] Flattening an array

Jake VanderPlas jakevdp@gmail....
Tue Dec 8 18:29:47 CST 2009

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
  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

More information about the NumPy-Discussion mailing list