-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
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
ENH: add cumulative_simpson integration to scipy.integrate #18151
Conversation
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.
Thank you for the PR @nprav!
Before we start any review, we need to get the approval for inclusion in SciPy. The process involves sending an email to the dev mailing list. Could you do that?
https://mail.python.org/mailman3/lists/scipy-dev.python.org/
Hi @tupui , I have emailed scipy-dev-owner@python.org and scipy-dev@python.org. |
@nprav thank you. The first time you send an email to the list, an admin needs to validate. Once this is done your email should go through. |
This reverts commit fcf9d66.
@MatteoRaso Thank you for starting a review but please wait until there is a decision from a maintainer. We don't want people to spend time on something that could in the end be rejected. EDIT: we now got an approval 😃 |
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.
Some review comments. IMHO, it is already looking quite good and a very useful addition to scipy.
Co-authored-by: Christian Lorentzen <lorentzen.ch@gmail.com>
A comment from the sidelines. I would expect such a function to have the same functionality as the existing i.e. it should handle end points similarly, and the behaviour of overlapping keywords should be the same, etc. |
Thank you for the comment @andyfaff ! This is an important discussion point because there are currently some differences in the function signatures. Please find the differences and justifications below and let me know what you think. Current function signatures:
ComparisonsIntegrate.simpson(y, x=None, dx=1.0, axis=-1, even="avg")
The 'even' keyword relates to what happens if there are an odd number of intervals. Integrate.simpson will necessarily apply a trapezoidal rule for the odd interval. However, this approach violates the assumption of a quadratic relationship. In reality, the final interval can be approximated with a modified formula while maintaining the quadratic assumption. This is described in the wikipedia article. It is also depicted with figures here: Cumulative Simpson Derivation. Because of the above, I had implemented cumulative_simpson without the trapezoidal rule logic. This also ensures that when applied to samples with a quadratic relationship, cumulative_simpson will always give the exact solution. On the other hand, integrate.simpson will be inaccurate if there are an odd number of intervals. I also compared accuracy when integrating oscillatory functions and found cumulative_simpson to be exhibit equal or better accuracy than integrate.simpson (above notebook link). cumulative_trapezoid(y, x=None, dx=1.0, axis=-1, initial=None)
The second approach is more 'correct' because the 'initial' value is analogous to a constant of integration that must be added to every point in the cumulative integral. Consider the following practical example:
|
I don't have time to go through this PR myself (plus I'm not an expert in this area of the codebase). However, I should reiterate that behaviour for the proposed PR should tally exactly with existing behaviour for For example:
The Similarly I would expect the exact same behaviour for However, I do see the benefit of using the Cartwright2017 approach, as you linked to on Wikipedia. It may be that there's benefit to changing the existing code in |
@mdhaber I'ce prepped a fix for #5618 in https://github.com/andyfaff/scipy/tree/simpson. The mechanics are there but the API needs a bit of discussion. E.g.. how do we deal with back compat, do we ignore |
Yup, your recent email reminded me of those issues. |
Yes @mdhaber, I'm willing to help with any specific comments. |
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.
Here are some thoughts on the actual implementation to make the code easier to verify and improve performance.
Not sure why this is failing on just one platform. Maybe it does need to be Maybe except |
After #18151 (review) and nprav#2 are considered, I think this is good to go. Only merge them if you agree; LMK either way. Thanks for your patience @nprav! |
Co-authored-by: Matt Haberland <mhaberla@calpoly.edu>
TST: integrate.cumulative_simpson: simplify and strengthen tests
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.
There are some details to improve, but let's get this in. Thanks @nprav!
Thank you all for helping to add this to SciPy! @mdhaber @tupui @andyfaff @lucascolley @lorentzenchr @MatteoRaso @j-bowhay |
thanks @nprav and congratulations on your first contribution to SciPy! Keep them coming :) |
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.
@nprav Here are some things to consider for follow-up work. If you are willing to do this soon, we can get it in the version of the function that is released in SciPy 1.12.
There are other things about documentation that I'd change in all the related function, but I've avoided commenting about those sorts of things here if they were just copied over.
@mdhaber , should i add the follow up changes in a separate PR? |
Yes, thanks! |
@mdhaber Regarding the case of "... elements of But if only some elements are identical, it is tricky. Eg: x = [0, 0, 1, 1, 2, 2] We can do the same in cumulative_simpson, but the results may not match with simpson because cumulative_simpson explicitly calculates the intermediate points. More importantly, this does not make much mathematical sense. I think it should be up to the user to determine how to clean/handle their inputs. At the same time, I understand we want to keep the API consistent. Do you have any thoughts on this? |
All PRs should be to |
Reference issue
N/A
What does this implement/fix?
The cumulative integration and differentiation of numerical samples is a common operation in the engineering domains.
Scipy currently only provides the integrate.cumulative_trapezoid function, but this is insufficiently accurate for engineering purposes. The integrate.simpson method is available but only as a complete integral, not a cumulative integral. This PR adds an implementation for the cumulative integration of an array of samples using the Simpson's 1/3 rule (wiki link).
CAE softwares such as ANSYS/LS-DYNA provide simpson's integration as a default processing option. Adding this to Scipy.integrate will help expand usage in the engineering domains.
Additional information
Refer to this jupyter notebook: Cumulative Simpson derivation for further information.