[Numpy-discussion] Fast clip for native types, 2d version
David Cournapeau
david at ar.media.kyoto-u.ac.jp
Sun Jan 14 22:38:57 EST 2007
Robert Kern wrote:
> David Cournapeau wrote:
>
>> 2: the old implementation does not upcast the input array. If the
>> input is int32, and min/max are float32, the function fails; if input is
>> float32, and min/max float64, the output is still float32. Again, this
>> seems against the expected numpy behaviour ?
>
> The latter is expected. As discussed previously here,
Could you tell me where this was discussed, I think I missed it.
> the types of scalars are
> ignored. You do get an upcast when either min or max is a float64 array.
>
>
> In [1]: import numpy
>
> In [2]: a = numpy.linspace(0, 10, 101)
>
> In [3]: a = numpy.linspace(0, 10, 101).astype(numpy.float32)
>
> In [4]: a.clip(numpy.float64(5), numpy.float64(6)).dtype
> Out[4]: dtype('float32')
>
> In [5]: a.clip(numpy.ones(101, dtype=numpy.float64), numpy.float64(6)).dtype
> Out[5]: dtype('float64')
>
>
> The int32/float32 failure is odd, though.
>
Thanks for the precision. Is this the expected behaviour for endianness,
too ? And why integer input works for float64 but not for float32 ?
In [1]: import numpy
In [2]: a = numpy.linspace(0, 10, 101).astype(numpy.int32)
In [3]: a.clip(0, 1).dtype
Out[3]: dtype('int32')
In [4]: a.clip(numpy.float64(0), 1).dtype
Out[4]: dtype('float64')
In [5]: a.clip(numpy.float32(0), 1).dtype
---------------------------------------------------------------------------
exceptions.TypeError Traceback (most
recent call last)
/usr/media/boulot/<ipython console>
TypeError: array cannot be safely cast to required type
David
More information about the NumPy-Discussion
mailing list