[Numpy-discussion] numpy.trapz() doesn't respect subclass

Ryan May rmay31 at gmail.com
Mon Mar 22 10:14:41 EDT 2010

On Sun, Mar 21, 2010 at 11:57 PM,  <josef.pktd at gmail.com> wrote:
> On Mon, Mar 22, 2010 at 12:49 AM, Ryan May <rmay31 at gmail.com> wrote:
>> Hi,
>> I found that trapz() doesn't work with subclasses:
>> http://projects.scipy.org/numpy/ticket/1438
>> 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?


Ryan May
Graduate Research Assistant
School of Meteorology
University of Oklahoma

More information about the NumPy-Discussion mailing list