[Numeric] why is Float32 incorrect for ufuncs?
David M. Cooke
cookedm+news at physics.mcmaster.ca
Thu Mar 4 15:51:44 EST 2004
At some point, Curzio Basso <curzio.basso at unibas.ch> wrote:
> Hello everyone,
>
> I am a beginner with Numerical Python, and there is a thing I do not
> understand in the behaviour of the ufuncs. Maybe someone can enlighten
> me.
>
> Why is the following not working?
>
> >>> import Numeric
> >>> a=Numeric.ones((2,2),Numeric.Float32)
> >>> a
> array([[ 1., 1.],
> [ 1., 1.]],'f')
> >>> a/=2
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> TypeError: return array has incorrect type
>
> If I use Numeric.Float64 as typecode everything works fine, but I do
> not understand why is this needed.
>
> thanks for your help. curzio.
Because a/2 returns an array with typecode of Numeric.Float64, which
can not be copied into an array of typecode Numeric.Float32. That's
becasue Numeric does type upcasting. Note that a Python float is
equivalent to Numeric.Float64.
This is a bit of a wart in Numeric. You can work around it by calling
ones with savespace=1, or calling a.savespace(1). Or, use numarray,
where the default behaviour is not to do upcasting:
>>> import numarray
>>> a = numarray.ones( (2,2), type=numarray.Float32)
>>> print a
array([[ 1., 1.],
[ 1., 1.]], type=Float32)
>>> a /= 2
>>> print a
array([[ 0.5, 0.5],
[ 0.5, 0.5]], type=Float32)
--
|>|\/|<
/--------------------------------------------------------------------------\
|David M. Cooke
|cookedm(at)physics(dot)mcmaster(dot)ca
More information about the Python-list
mailing list