numpy migration (also posted to numpy-discussion)
Duncan Smith
buzzard at urubu.freeserve.co.uk
Mon Apr 23 23:03:12 EDT 2007
Travis E. Oliphant wrote:
> Duncan Smith wrote:
>
>> Hello,
>> Since moving to numpy I've had a few problems with my existing
>> code. It basically revolves around the numpy scalar types. e.g.
>>
>
> You will probably get more help on the numpy discussion list:
>
> numpy-discussion at scipy.org
>
>
> You are encountering problems because numpy scalar types don't raise
> errors (unless you have set the appropriate hardware flag using
> numpy.seterr).
>
Aha!
> You can get Python scalars out of NumPy arrays if you really want them
> using (for example...)
>
> a.item(0,0)
>
>
>>
>> An additional problem involves classes that have e.g. __rmul__ methods
>> defined and are sufficiently similar to numpy arrays that my classes'
>> __rmul__ methods are not invoked when using numpy scalars.
>>
>
> Could you please post an example showing the problem?
>
I'll try to post a minimal example tomorrow. But they are classes that
have an ndarray as an attribute, and with __getitem__ and __setitem__
methods which simply call the corresponding array methods. Maybe that's
enough to account for the behaviour? I'll check tomorrow.
>>
>> I might (I hope) be missing something obvious; but it seems like, to be
>> safe, I'm going to have to do a lot of explicit conversions to Python
>> types (or abandon catching zero division errors, and documenting some of
>> my classes to highlight that whether scalar * a equals a * scalar
>> depends on whether a.__rmul__ is called, which depends on the type of
>> scalar).
>>
>
> numpy scalars are try a lot more things before giving up on
> multiplication and letting the other class have a stab at it.
>
> Post your problems to the numpy discussion list for better help and more
> discussion.
>
Yes, I have done. But it's awaiting moderation; presumably because I
posted using a different e-mail address than the one I registered with
(I wasn't thinking). Thanks for the reply.
Duncan
More information about the Python-list
mailing list