[Numpy-discussion] Flattening an array

josef.pktd@gmai... josef.pktd@gmai...
Tue Dec 8 23:55:06 CST 2009

On Tue, Dec 8, 2009 at 7:29 PM, Jake VanderPlas <jakevdp@gmail.com> wrote:
> 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.

maybe one way is to  check the flags, and conditional on C or F, use
the corresponding order
in numpy.ravel(a, order='C')  ?

if A.flags.c_contiguous:
elif A.flags.f_contiguous

not tried, and I don't know what the right condition for `is_c_contiguous` is


>  Thanks for the help
>   -Jake
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion

More information about the NumPy-Discussion mailing list