[Numpy-discussion] int and float ufunc?
pearu at cens.ioc.ee
Mon Oct 7 05:43:02 CDT 2002
On Mon, 7 Oct 2002, Andrew P. Lentvorski wrote:
> On Mon, 7 Oct 2002, Travis Oliphant wrote:
> > What would you have them do? Their current definition works for returning
> > suitable arrays as integers and floats respectively, just as the Python
> > documentation says these two functions should.
> Okay, now I'm really confused ...
> >>> import Numeric
> >>> a = Numeric.array([1.5, 2.5, 3.5], Numeric.Float64)
> >>> a
> array([ 1.5, 2.5, 3.5])
> >>> int(a)
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> TypeError: Only rank-0 arrays can be converted to Python scalars.
> I guess I'm really dense, but how does that result constitute a definition
> that "works"?
See ^^^ above. In this case, suitable arrays are rank-0 arrays. So, the
definition works (see also below).
> > It would be an easy thing to map int() to oldarray.astype(Int) and so
> > forth but is this a good policy. I submit it is not.
> I could be persuaded either way. I was simply wondering what the
> arguments were.
According to int.__doc__, int(..) should *always* return a Python
integer. But you are asking for an integer array as a result of
int(array(..)); and that would be a contradiction with the Python
definition for int(anyobj).
More information about the Numpy-discussion