maroba / findiff

Python package for numerical derivatives and partial differential equations in any number of dimensions.
MIT License
413 stars 59 forks source link

Coefficients for odd order #30

Closed stephenbeckr closed 3 years ago

stephenbeckr commented 3 years ago

I just spent some time trying to figure out what was wrong when I asked findiff.coefficients(deriv=1, acc=...) for odd values of acc. The source code says "This module determines finite difference coefficients for uniform and non-uniform grids for any desired accuracy order." but then later it it says that it's not for any accuracy order, it's only for even order. I didn't notice that second documentation at first, and there was no error message. Perhaps instead of rounding the integer to an even number, the code can return an error? I don't know what the use cases are for when you want the code to change an odd to an even, so maybe this would mess up established workflows, but as a very casual user, I'd prefer to get an error that tells me to use an even integer.

And out of curiosity, since these formulas do exist for odd order, is it just that there is no demand for them?

maroba commented 3 years ago

You are right. It should rather say "any even accuracy order". In case of requested odd accuracy orders, I chose to automatically fall back to the next higher even accuracy order, because - being more accurate - this should do no harm. However, I see your point and it may be cleaner to raise an exception instead.

Regarding your question on why only even orders: When I originally wrote the package, I couldn't think of a use case where one would need to use asymmetric coefficients in the interior of the grid instead of symmetric ones. And for symmetric coefficients, odd accuracy orders do not exist. Since one wouldn't want to mix different accuracy orders on the same grid, I saw no need for odd orders.

stephenbeckr commented 3 years ago

I see. Yes, I think an exception probably makes more sense.

A simple use case for asymmetric odd-order are the backward differentiation formulas (https://en.wikipedia.org/wiki/Backward_differentiation_formula). Of course these only need to be derived once, and we don't use arbitrarily high-order BDF since they're unstable.