[SciPy-Dev] Code review for trapz update

Sturla Molden sturla.molden at gmail.com
Wed Mar 11 09:51:59 EDT 2015


On 11/03/15 13:48, fizyxnrd wrote:

> I was not clear in what I meant should be sorted.  I am envisioning
> something like

> # Assume that x should be an ordered quantity
> idx = np.argsort(x)
>
> result = np.trapz(y[idx], x[idx])

I understood what you meant, but sorting x values is not permissible:

$\int_a^b f(x) dx = - \int_b^a f(x) dx$

The x values therefore cannot be sorted.

Consider a more complex situation like a line integral. What do you 
suppose sorting x values might do?


> The intent is not to sort the y values by value, only to assume that
> there is no meaningful path information in the x values, and so to sort
> them so that only the total area under y is computed, without regard to
> direction.

Right. You want to estimate the integral of y = f(x) from x.min() to 
x.max() and have random samples of (x,y) pairs. But then you should 
preprocess your data before you call trapz. trapz provides a numerical 
integral given two input vectors. It is the user's responsibility to 
make sure it is the integral the user actually wants. The necessary 
preprosessing will vary from case to case. The order of x and y must be 
retained because it is actually required to produce the correct result.


Sturla




More information about the SciPy-Dev mailing list