
This has started to be a mess of top and bottom posting. I'll clean this up a bit and provide my response below. On 11/8/16 00:20 , Robert Rehammar wrote:
Jonathan,
Please see below.
Robert,
I think, conceptually, something like this could be of interest in SciPy, but the devil is in the details. There are a lot of ways to implement finite differences; the code you shared provides the forward/backward finite differences with optional shift in the x positions. These are first-order accurate. Your interpolation of forward differences effectively results in the second-order accurate central-difference method. Agree
Evaluation of the endpoints is a bit more tricky; your approaches are simple but lack rigor (I am not sure of the accuracy). I know of formulas for second-order accurate endpoint finite differences for equally spaced x, but I am not sure about unequally spaced data (I am sure they exist but I haven't looked them up). If there is an interest in the function I can put a bit of more work into it. At it looks now, it is just a very fast hack that I did since I could not find it anywhere. Of course the things you point out should be
On 2016-11-07 17:21, Jonathan Stickel wrote: treated more rigorously. For this function to be useful it is important that it can operate on unequally spaced grids since real-world data often is.
So, should this functionality exist in SciPy, what methods should be implemented and made available? I think many of us (including myself) have implemented our own methods that are satisfactory for our particular needs, and no one has submitted general utility finite-differences functions that provides multiple methods with appropriate mathematical rigor. Such a function should also provide higher-order derivatives (at least second, but maybe up to fourth; or perhaps nth-order methods using a series formula).
Sure. The simplest way of doing that I guess is to do it recursively. But I can look into this and see what possibilities there are to do it more accurately.
Since differentiating is a non-regulating operator, when working with real-world data, it is somethings of interest to also convolute with some windowing function to reduce noise. If the function should provide higher order derivatives, it might also need to be able to do that.
Also, the name should not be "diff" to avoid confusion with numpy.diff.
Yes of course. What do you propose? It could be differentiate, but it is a bit long. Some other possibilities derivative ndiff frac_diff fdiff
Best, Robert
Regards, Jonathan
On Thu, Nov 03, 2016 at 08:37:07PM +0100, Robert Rehammar wrote:
Dear all,
I implemented a simple function to differentiate an array which seems other people might like to have. It looks like: def diff(f, x, retval = 'same'): (...)
What do you think about adding it to scipy?
Robert Since you seem interested in putting in the work to provide a finite-difference method function to SciPy, I encourage you to work more on it. I would be happy to help you, although you should know that I am not a developer myself. I have use cases for both differentiating experimental data as well as using finite differences in custom partial differential equation solvers, sometimes with unequal grids. I am familiar with the noise problem of finite differences of data. I implemented a Tikhonov-regularization data smoother that can be found here: https://github.com/jjstickel/scikit-datasmooth I provides an option for returning a smooth derivative (instead of the smooth signal), but the endpoints are neglected. Also, SciPy provides smoothing splines in scipy.interpolate. In both cases, a general-use finite differences function would be helpful for obtaining derivatives of the smooth signal with appropriate handling of endpoints. Another question is where to locate such a function. Perhaps in scipy.signal? For a name, I would suggest finitediff or findiff. Regards. Jonathan