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

josef.pktd@gmai... josef.pktd@gmai...
Sat Mar 27 12:12:50 CDT 2010


On Sat, Mar 27, 2010 at 1:00 PM, Ryan May <rmay31@gmail.com> wrote:
> On Mon, Mar 22, 2010 at 8:14 AM, Ryan May <rmay31@gmail.com> wrote:
>> On Sun, Mar 21, 2010 at 11:57 PM,  <josef.pktd@gmail.com> wrote:
>>> On Mon, Mar 22, 2010 at 12:49 AM, Ryan May <rmay31@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.

With asarray it gives correct results for matrices and all array_like
and subclasses, it just doesn't preserve the type.
Your patch would break matrices and possibly other types, masked_arrays?, ...

One solution would be using arraywrap as in numpy.linalg.

for related discussion:
http://mail.scipy.org/pipermail/scipy-dev/2009-June/012061.html

Josef

>
> Anyone else care to weigh in here?
>
> Ryan
>
> --
> Ryan May
> Graduate Research Assistant
> School of Meteorology
> University of Oklahoma
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>


More information about the NumPy-Discussion mailing list