[Numpy-discussion] Fast clip for native types, 2d version

Robert Kern robert.kern at gmail.com
Sun Jan 14 05:54:46 EST 2007

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

Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco

More information about the NumPy-Discussion mailing list