[Numpy-discussion] int and float ufunc?
oliphant at ee.byu.edu
Mon Oct 7 08:52:03 EDT 2002
On Mon, 7 Oct 2002, Andrew P. Lentvorski wrote:
> On Mon, 7 Oct 2002, Travis Oliphant wrote:
> > > Could someone explain the reason why int() and float() don't work as
> > > ufuncs? Is it just that someone needs to write the code, or is there some
> > > subtlety at work that I am missing?
> > 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 didn't mean to confuse you. I said "suitable" arrays. Returning a
single Python integer from an array doesn't really make sense unless that
array is 0-dimensional (rank-0). Now, one could argue that an array of
length 1 could also be converted unambiguously, but that discussion has
never taken place.
Python uses the int() function to return a Python integer type whenever it
is explicitly needed.
> I could be persuaded either way. I was simply wondering what the
> arguments were.
The main argument is that int() should return a Python integer type
representation of your object. There is no way to unambiguously return a
Python integer object from a general array unless that array contains only
one element. Currently the int() function only converts rank-0 arrays
(which are Numeric scalars with array headers).
> My usage is in converting vectors of data before passing them to the
> screen for display. The conversion function is a bunch of ufuncs and then
> the resulting vector needs to get converted to int before being passed to
> X. The same code works for Python floats, scalars, or Numeric arrays with
> the exception of the int function.
I don't understand what the problem is. Why does X.astype() not work for
you here? What do you mean "passed to X?" Is this in C, in Python? The
int() function was never intended to work on general Numeric arrays. It
could be made to (and would be a very simple job), but that discussion has
never taken place.
instead of int() why don't you use
asarray(x).astype('l') for your code
> If the problem was simply a lack of someone to write the code, I thought I
> might do it. I thought it might be a little faster than me writing array
> versions of all of my conversion functions, although I haven't looked at
> the complexity of the Numeric code. So, I could be completely wrong about
If you look in SciPy (www.scipy.org), we have defined a dictionary of cast
functions for each of the types that works the way you apparently wanted
int() to work.
Using this command you would write
I don't know what your functions are, but I doubt array versions would be
that difficult to write. I guess I just always write array versions of my
functions. Almost every routine I write starts with
to convert inputs to array objects
More information about the NumPy-Discussion