[Numpy-discussion] Array-casting problem.
Travis Oliphant
Oliphant.Travis at mayo.edu
Fri Feb 25 14:23:01 CST 2000
Hi Herb,
It has taken awhile for me to respond to this, but your problem's here
illustrate exactly the kinds of difficulties one encounters with the
current NumPy coercion rules:
You do not have a bad version of Numeric. The behavior you describe is
exactly what "should" happen though it needs to be fixed. I'll trace
for you exactly what is going on as it could be illustrative to others:
>>> a = zeros((5,5),'b')
# You've just created a 5x5 byte array that follows "normal" coercion
# rules filled with zeros.
>>> a[3,3] = 8
# This line copies the rank-0 array of type 'b' created from the Python
# Integer 8 (by a direct coercion in C) into element (3,3) of matrix a
>>> temp = a[3,3]
# This selects out the rank-0 array of typecode 'b' at position (3,3). As
# of 15.2 this is nolonger changed to a scalar. Note that rank-0 arrays
# act alot like scalars, but because there is not a one-to-one
# correspondence between the Python Scalars and rank-0 arrays, this is not
# automatically converted to a Python scalar (this is a change in 15.2)
>>> temp = temp + 3
# This is the problem line for you right here. Something is wrong though,
# since it should not be, a problem.
# You are adding a rank-0 array of typecode 'b' to a Python Integer which
# is interpreted by Numeric as a rank-0 array of typecode 'l'. The result
# should be a Python Integer. For some reason this is returning an array
# of typecode 'i' (which does not get automatically converted to a Python
# scalar).
>>> a[3,3] = temp
# This would work fine if temp were the Python scalar it should be.
# Right now, assignment doesn't let you assign an array of a "larger" type
# to elements of a smaller type (except for Python scalars). Since temp
# is (incorrectly I think) a type 'i' rank-0 array, it does not let you
# make the assignment. At any rate it is inconsistent to let you assign
# Python scalars but not rank-0 arrays of arbitrary precision, this should
# be fixed. It is also a problem that temp + 3 returns an array of
# typecode 'i'.
I will look into fixing the above problems this example points out. Of
course, it could also be fixed by having long integers lower in the
coercion tree than byte arrays.
Thanks for the feedback,
Travis Oliphant
More information about the Numpy-discussion
mailing list