[Numpy-discussion] numpy.trapz() doesn't respect subclass
Mon Mar 22 09:14:41 CDT 2010
On Sun, Mar 21, 2010 at 11:57 PM, <email@example.com> wrote:
> On Mon, Mar 22, 2010 at 12:49 AM, Ryan May <firstname.lastname@example.org> wrote:
>> I found that trapz() doesn't work with subclasses:
>> A simple patch (attached) to change asarray() to asanyarray() fixes
>> the problem fine.
> Are you sure this function works with matrices and other subclasses?
> Looking only very briefly at it: the multiplication might be a problem.
Correct, it probably *is* a problem in some cases with matrices. In
this case, I was using quantities (Darren Dale's unit-aware array
package), and the result was that units were stripped off.
The patch can't make trapz() work with all subclasses. However, right
now, you have *no* hope of getting a subclass out of trapz(). With
this change, subclasses that don't redefine operators can work fine.
If you're passing a Matrix to trapz() and expecting it to work, IMHO
you're doing it wrong. You can still pass one in by using asarray()
yourself. Without this patch, I'm left with copying and maintaining a
copy of the code elsewhere, just so I can loosen the function's input
processing. That seems wrong, since there's really no need in my case
to drop down to an ndarray. The input I'm giving it supports all the
operations it needs, so it should just work with my original input.
Or am I just off base here?
Graduate Research Assistant
School of Meteorology
University of Oklahoma
More information about the NumPy-Discussion