[SciPy-Dev] Code review for trapz update

fizyxnrd fizyxnrd at gmail.com
Tue Mar 10 10:50:52 EDT 2015


Alan G Isaac <alan.isaac <at> gmail.com> writes:

> 
> On 3/10/2015 8:37 AM, fizyxnrd wrote:
> > 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).
> 
> I still don't get it.
> Why are you claiming the sign is currently wrong?
> $\int_{5}^{1}  2 dx  = |_5^1   2x  = 2 - 10  = -8$.
> Why would we not want `trapz` to embody the core property
> of integrals that $\int_a^b f(x) dx = - \int_b^a f(x) dx$?
> 
> Alan Isaac
> 


Trapz finds the area under a one-to-one association of y values with x 
values.  If y(x) > 0, then the area bounded by [a, b] between y(x) and 
x=0 should always be positive.  The core property you have referenced 
above is the very property that should be used in order to achieve the 
equivalence with integrating along a negative path.  Maintaining this 
separation preserves the equivalence of
np.trapz(y,x) == np.trapz(y[::-1], x[::-1]), which I believe is an 
equivalence that should hold true.
This form requires a user to recognize that $F_b = F_a + \int_a^b f(x) 
dx$, while $F(a) = F(b) - \int_a^b f(x) dx$ instead of $F(a) = F(b) + 
\int_b^a f(x) dx$. One way or another, the user must be aware of the 
ordering in certain cases.  However, by treating intervals as non-
negative, order matters in a more limited set of cases. 

Additionally, non-monotonic sequences currently give strange behavior, 
because they allow non-surjective mappings from x to y.  Thus, the 
meaning of
np.trapz([1 3 5 7 3], [1 2 6 1 2])
is ambiguous at best.  At a minimum, trapz should error on non-monotonic 
sequences.

Thoughts?




More information about the SciPy-Dev mailing list