<div dir="ltr"><br><div class="gmail_quote">On Tue, Mar 10, 2015 at 11:30 AM Eric Moore <<a href="mailto:ewm@redtetrahedron.org">ewm@redtetrahedron.org</a>> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
This data should integrate to negative "area":<br>
x = [1,  2,  3,  4,  5]<br>
y = [0, -1, -2, -1,  0]<br>
(-4)<br>
<br></blockquote><div><br></div><div>Yes. </div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
This data should integrate to positive "area", even though the x data is<br>
decreasing (and we have negative intervals):<br>
x = [5, 4, 3, 2, 1]<br>
y = [2, 2, 2, 2, 2]<br>
(currently, get -8, but should get +8).</blockquote><div><br></div><div>No. In general the path one integrates over matters.  If you want to think about intervals they must be signed. </div><div> <br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
It might seem reasonable to require a user to always make their x<br>
sequence monotonically increasing.  However, trapz is silent if this is<br>
not the case, and at the very least should be modified throw an error<br>
for non monotonically increasing x sequences.<br>
<br></blockquote><div><br></div><div>Erroring in this case is probably fine. </div></blockquote><div><br></div><div>Sorting or checking for sorting isn't a good option.</div><div>As a toy example, consider evaluating the residue of 1/z at the origin </div><div>using a contour integral.</div><div><br></div><div><div>import numpy as np</div><div>t = np.linspace(0, 2 * np.pi, 100)</div><div>circ = np.exp(1.0j * t)</div><div>reciprocals = 1. / circ</div><div>np.trapz(reciprocals, circ) / (2.0j * np.pi)</div></div><div><br></div><div>As it is right now, this (correctly) returns an answer very close to 1.</div><div>The output is garbage if the integral is evaluated in sorted form.</div><div><br></div><div>-Ian Henriksen</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
This issue arises, for example, in the conversion of spectral quantities<br>
from wavenumber (units of inverse length) to wavelength (units of<br>
length).  When the spectral quantity is inverted, a monotonically<br>
increasing sequence becomes a monotonically decreasing sequence, and<br>
vice versa.  This gives the unexpected result that trapz(<quantity>,<br>
<spectrum>) works as expected before conversion, but returns values with<br>
the wrong sign after conversion.</blockquote><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"></blockquote><div><br></div><div> I understand why this seems wrong, but the fact that you have to reverse the order is actually correct. </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"> <br></blockquote><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
This behavior is even more difficult to recognize if the <quantity> (y-<br>
value) term is not strictly positive, and a user may utilize trapz<br>
incorrectly with no indication that it is not behaving as expected.<br>
<br>
_______________________________________________<br>
SciPy-Dev mailing list<br>
<a>SciPy-Dev@scipy.org</a><br>
<a href="http://mail.scipy.org/mailman/listinfo/scipy-dev" target="_blank">http://mail.scipy.org/mailman/listinfo/scipy-dev</a><br>
</blockquote>
______________________________<u></u>_________________<br>
SciPy-Dev mailing list<br>
<a href="mailto:SciPy-Dev@scipy.org" target="_blank">SciPy-Dev@scipy.org</a><br>
<a href="http://mail.scipy.org/mailman/listinfo/scipy-dev" target="_blank">http://mail.scipy.org/mailman/<u></u>listinfo/scipy-dev</a><br>
</blockquote></div></div>