[Numpy-discussion] Should non ufunc numpy functions behave like ufunc regarding casting to output argument ?
A. M. Archibald
peridot.faceted at gmail.com
Mon Jan 15 20:57:22 CST 2007
On 15/01/07, David Cournapeau <david at ar.media.kyoto-u.ac.jp> wrote:
> I am trying to add support for out argument to one C function using
> numpy API (still the clip function). I was wondering about the expected
> behaviour when out does not have the "expected" type.
> For example, using again the clip function (but the question is not
> specific to this function)
> In : import numpy
> In : a = numpy.linspace(0, 10, 101)
> In : b = numpy.zeros(a.shape, dtype = numpy.int32)
> In : print a.dtype
> In : a.clip(0.1, 0.5, b)
> Should this be equivalent to b = a.clip(0.1, 0.5); b =
> b.astype(numpy.int32) (ie, the casting is done at the end, similar to an
> ufunc) ?
Since the point of output arguments is to avoid allocating new
storage, I'm not sure whether to say yes or no here... but if you're
given an output array to store the answer in, you're more or less
forced to convert it to that type (and layout, and whatnot) for
storage - imagine, for example, it is every third element of a bigger
array (and remember you do not have access to the name "b").
a = numpy.arange(10)
b = numpy.zeros(40).astype(numpy.uint8)
You can't do anything about the data type of b, so you don't really
have any choice but to convert to uint8; one hopes that this will not
require the allocation of an extra array of floats.
A. M. Archibald
More information about the Numpy-discussion