[Numpy-discussion] Dot products and casting

Ed Schofield schofield at ftw.at
Fri Feb 17 07:23:13 EST 2006


Hi all,

I think there's a bug in dot() that prevents it from operating on two
arrays, neither of which can be safely cast to the other.  Here's an
example:

>>> from numpy import *
>>> a = arange(10, dtype=float32)
>>> b = arange(10, dtype=float64)
>>> c = arange(10, dtype=int64)
>>> d = arange(10, dtype=int32)
>>> e = arange(10, dtype=int16)

# Dot products between b and either c or d work fine:
>>> dot(b,c)
285.0
>>> dot(b,d)
285.0

# Dot products with e also work fine:
>>> dot(a,e)
285.0
>>> dot(b,e)
285.0

# But dot products between a and either c or d don't work:
>>> dot(a,c)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: array cannot be safely cast to required type
>>> dot(a,d)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: array cannot be safely cast to required type


The problem seems to be with the PyArray_ObjectType() calls in
dotblas_matrixproduct(), which are returning typenum=PyArray_FLOAT, but
this isn't sufficiently large for a safe cast from the int32 and int64
arrays.  It seems like PyArray_ObjectType() should be returning
PyArray_DOUBLE here instead.

Here's another example:

>>> f = arange(10, dtype=complex64)
>>> dot(b, f)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: array cannot be safely cast to required type

So it seems like the problem isn't isolated to float32 arrays, but occurs
elsewhere when we need to find a minimum data type of two arrays when
*both* need to be upcasted.


-- Ed





More information about the NumPy-Discussion mailing list