[Numpy-discussion] numpy.trapz() doesn't respect subclass
Sat Mar 27 12:12:50 CDT 2010
On Sat, Mar 27, 2010 at 1:00 PM, Ryan May <email@example.com> wrote:
> On Mon, Mar 22, 2010 at 8:14 AM, Ryan May <firstname.lastname@example.org> wrote:
>> 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.
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:
> Anyone else care to weigh in here?
> Ryan May
> Graduate Research Assistant
> School of Meteorology
> University of Oklahoma
> NumPy-Discussion mailing list
More information about the NumPy-Discussion