-
-
Notifications
You must be signed in to change notification settings - Fork 11k
MAINT: trapz works on non-monotonic x #10428
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Think the error comes because the x arr needs to be broadcast against y. |
Only one test failing now, but I'm not sure how to use |
x = np.copy(x).reshape(shape) | ||
|
||
x, y = np.broadcast_arrays(x, y) | ||
idxs = _argsort_indices(x, axis) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm fairly sure you just want argsort
here, and your test is too simple to tell the difference - argsort([1, 3, 2]) == _argsort_indices([1, 2, 3])
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
plain argsort
gives the indices for a multidimensional array, but you can't directly use the output of argsort
to sort that array.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
>>> a = np.arange(9)
>>> np.random.shuffle(a)
>>> a = a.reshape((3, 3))
>>> print(a)
[[7 6 5]
[4 3 1]
[0 2 8]]
>>> i = np.argsort(a)
>>> print(i)
[[2 1 0]
[2 1 0]
[0 1 2]]
>>> print(a[i])
[[[0 2 8]
[4 3 1]
[7 6 5]]
[[0 2 8]
[4 3 1]
[7 6 5]]
[[7 6 5]
[4 3 1]
[0 2 8]]]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apologies, I muddled this with the permutation PR (#9880), which at one point used a similar name
I should also point out that the documentation of
The test example given is:
However, the result is incorrect according to the comment in the test. If the comment is correct then the answer should be 18.0.
|
The current behavior is already correct:
You can check that the answer is correct graphically:
The area to the bottom-right of the line is indeed 7.5 squares |
The rationale for
There will be a non-negligible population who think the answer should be 0.5 * 5 * 5 = 12.5. I don't mind if the PR doesn't progress further, but then the documentation should probably be changed to say that the order of x matters. |
So to elaborate, the current behavior allows integration of parametric functions that are not monotonic in If your data is un-ordered, then I think it's up to you as the user to put it in the order that makes sense for your function. Another example of why the existing implementation is useful:
It does sound like the documentation could be improved |
These people will probably realize that they need to order their data when they try to do a line plot in matplotlib, which also cares about the order of their data. More worryingly, if you organically have unordered data (random measurements from a process), sorting by one coordinate is a very bad way to produce a fit, and integration under that fit doesn't sound like a meaningful thing to do to me. |
This PR allows
trapz
to take non monotonicx
arrays. It achieves this by a sort along the required axis.1 and 3-D tests are added.