[Numeric] why is Float32 incorrect for ufuncs?

Tim Hochberg tim.hochberg at ieee.org
Thu Mar 4 22:15:05 CET 2004


Duncan Smith wrote:
> "Curzio Basso" <curzio.basso at unibas.ch> wrote in message
> news:404757b9$1 at maser.urz.unibas.ch...
> 
>>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.
>>
> 
> 
> What *I think* is happening is that Numeric is converting the integer to a
> rank-0 array of type Int64 before division.  The consequence is an upcast to
> Float64 for the return array.  Look for 'Coercion and Casting' in the
> Numeric documentation.  Cheers.

Also look for "savespace", using savespace=1 will prevent upcasting when 
operating on scalars:

 >>> import Numeric
 >>> a=Numeric.ones((2,2),Numeric.Float32, savespace=1)
 >>> a
array([[ 1.,  1.],
        [ 1.,  1.]],'f')
 >>> a/=2
 >>> a
array([[ 0.5,  0.5],
        [ 0.5,  0.5]],'f')
 >>> a=Numeric.ones((2,2),Numeric.Float32, savespace=0)
 >>> a/=2
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
TypeError: return array has incorrect type


-tim





More information about the Python-list mailing list