asarray issue with type codes
Hi there, I ran into a little problem in some type checking code for a C extension I'm writing. I construct X as a C-long array and then I cast it to a C-int array Y, however the type code does not change. However, when I try constructing the array from scratch as a C-int, I get the right type code (ie 5). I assumed that when X gets casted to a C-int, no copying should occur but a new array view should be constructed with the C-int type code. What's wrong with this logic? Also note that casting from a C-long (type code 7) to a double to a C-int returns an array with the right type code, although a double copy occurs. Damian # Construct X as a C-long. In [16]: X=numpy.zeros((10,10),dtype='l') # Now cast X to a C-int. In [17]: Y=numpy.asarray(X, dtype='i') # Check X and Y's data type; they are the same. In [18]: X.dtype Out[18]: dtype('int32') In [19]: Y.dtype Out[19]: dtype('int32') # Their type codes are the same. In [20]: X.dtype.num Out[20]: 7 In [21]: Y.dtype.num Out[21]: 7 # Constructing with dtype='i', gives the right type code. In [22]: Z=numpy.zeros((10,10),dtype='i') In [23]: Z.dtype Out[23]: dtype('int32') In [24]: Z.dtype.num Out[24]: 5
Hi Damian 2008/7/29 Damian Eads <eads@soe.ucsc.edu>:
I ran into a little problem in some type checking code for a C extension I'm writing. I construct X as a C-long array and then I cast it to a C-int array Y, however the type code does not change. However, when I try constructing the array from scratch as a C-int, I get the right type code (ie 5).
I assumed that when X gets casted to a C-int, no copying should occur but a new array view should be constructed with the C-int type code. What's wrong with this logic?
I would guess that, somewhere along the line, the two dtypes are compared to see whether anything needs to be done: In [18]: np.dtype('i') == np.dtype('l') Out[18]: True Since int and c_long is the same on 32-bit platforms, it doesn't do anything. I agree; that looks like a bug. Unless someone else justifies this behaviour, please file a ticket so that we can fix it in time for 1.2. For now, you could achieve what you want by doing X.view(np.dtype('l')) But, of course, that would break on a platform where the widths of int and clong differ. Cheers Stéfan
participants (2)
-
Damian Eads -
Stéfan van der Walt