[SciPy-Dev] Code review for trapz update

Eric Moore ewm at redtetrahedron.org
Tue Mar 10 13:29:54 EDT 2015


>
> This data should integrate to negative "area":
> x = [1,  2,  3,  4,  5]
> y = [0, -1, -2, -1,  0]
> (-4)
>
>
Yes.


> This data should integrate to positive "area", even though the x data is
> decreasing (and we have negative intervals):
> x = [5, 4, 3, 2, 1]
> y = [2, 2, 2, 2, 2]
> (currently, get -8, but should get +8).


No. In general the path one integrates over matters.  If you want to think
about intervals they must be signed.


>
> It might seem reasonable to require a user to always make their x
> sequence monotonically increasing.  However, trapz is silent if this is
> not the case, and at the very least should be modified throw an error
> for non monotonically increasing x sequences.
>
>
Erroring in this case is probably fine.


> This issue arises, for example, in the conversion of spectral quantities
> from wavenumber (units of inverse length) to wavelength (units of
> length).  When the spectral quantity is inverted, a monotonically
> increasing sequence becomes a monotonically decreasing sequence, and
> vice versa.  This gives the unexpected result that trapz(<quantity>,
> <spectrum>) works as expected before conversion, but returns values with
> the wrong sign after conversion.


 I understand why this seems wrong, but the fact that you have to reverse
the order is actually correct.

>
>
This behavior is even more difficult to recognize if the <quantity> (y-
> value) term is not strictly positive, and a user may utilize trapz
> incorrectly with no indication that it is not behaving as expected.
>
> _______________________________________________
> SciPy-Dev mailing list
> SciPy-Dev at scipy.org <javascript:;>
> http://mail.scipy.org/mailman/listinfo/scipy-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/scipy-dev/attachments/20150310/806d801e/attachment.html>


More information about the SciPy-Dev mailing list