[Numpy-discussion] On my Cython/NumPy project

Dag Sverre Seljebotn dagss@student.matnat.uio...
Sat Jun 21 13:55:54 CDT 2008

Matthew Brett wrote:
> Hi,
> Thanks a lot for the email - it's an exciting project.
>> cdef int i = 4, j = 6
>> cdef np.ndarray[np.float64, 2] arr = np.zeros((10, 10),
>> dtype=np.float64)
>> arr[i, j] = 1
> I'm afraid I'm going to pitch into an area I'm ignorant of, and I'm
> sorry for that, but do you think there is any way of allowing fast
> indexing and operations on arrays that do not have their type declared
> at compile time?

Well... the Cython compiler definitely needs to know about which type it
is -- the array simply has a byte buffer, and one needs to know how to
interpret that (how many bytes per element etc.).

However, I could make it so that if you left out the type, it would be
auto-detected. I.e.:

cdef np.ndarray[2] arr = ...
cdef np.float64 x = arr[3,4]

However, if you then did something like this on a 32-bit-system:

cdef np.ndarray[2] arr = ...
print arr[3,4]

Then it would assume arr is an object, and if arr is really float64 you
would segfault.

So the real answer is: Yes, the type is needed.

Dag Sverre

More information about the Numpy-discussion mailing list