[Numeric] why is Float32 incorrect for ufuncs?

C

Curzio Basso

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?
array([[ 1., 1.],
[ 1., 1.]],'f')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.
 
D

Duncan Smith

Curzio Basso said:
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?
array([[ 1., 1.],
[ 1., 1.]],'f')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.

Duncan
 
R

Russell E. Owen

Curzio Basso said:
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?
array([[ 1., 1.],
[ 1., 1.]],'f')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.

As another poster noted, the division is probably casting the result up
to python float type (which, if true, corresponds to Float64), and the
error comes from trying to reassign that result to the original array.

I suggest you try numarray (the planned replacement for Numeric; it is
similar enough that you will probably not have to change your code and
it handles casting better.
array([[ 1., 1.],
[ 1., 1.]], type=Float32)array([[ 0.5, 0.5],
[ 0.5, 0.5]], type=Float32)

-- Russell
 
D

David M. Cooke

At some point said:
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?
array([[ 1., 1.],
[ 1., 1.]],'f')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:
array([[ 1., 1.],
[ 1., 1.]], type=Float32)array([[ 0.5, 0.5],
[ 0.5, 0.5]], type=Float32)
 
T

Tim Hochberg

Duncan said:
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')
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:
array([[ 1., 1.],
[ 1., 1.]],'f')array([[ 0.5, 0.5],
[ 0.5, 0.5]],'f')Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: return array has incorrect type


-tim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top