[Numpy-discussion] Flattening an array
Tue Dec 8 23:55:06 CST 2009
On Tue, Dec 8, 2009 at 7:29 PM, Jake VanderPlas <firstname.lastname@example.org> wrote:
> 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.
maybe one way is to check the flags, and conditional on C or F, use
the corresponding order
in numpy.ravel(a, order='C') ?
not tried, and I don't know what the right condition for `is_c_contiguous` is
> Thanks for the help
> NumPy-Discussion mailing list
More information about the NumPy-Discussion