
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'): """ Differentiate f w.r.t. x and return the diff with same samples, or different depending on retval. retval can be: same: return data for same data points as the indata. This is done through interpolation at internal points and extrapolation at endpoint simple: just return the scaled diff samesimple: return with same size vector, just shifted. Last data point is repeated. new: return at new datapoints and length - 1 """ df = f[1:] - f[:-1] dx = x[1:] - x[:-1] dfodx = df/dx if retval == 'new': return dfodx, x[:-1] + dx/2 elif retval == 'same': cdfodx = np.zeros(len(f)) cdfodx[1:-1] = (dfodx[:-1] + dfodx[1:])/2 cdfodx[0] = dfodx[0]-0.5*(dfodx[1] - dfodx[0])/dx[0] cdfodx[-1] = dfodx[-1]+0.5*(dfodx[-1] - dfodx[-2])/dx[-1] return cdfodx, x elif retval == 'simple': return dfodx, x[:-1] elif retval == 'samesimple': cdfodx = np.zeros(len(f)) cdfodx[:-1] = dfodx cdfodx[-1] = dfodx[-1] return cdfodx, x And generates the following results (top plot original function, bottom different differentiates) from the code blow: x = np.linspace(0,2*np.pi,10) f = np.sin(x) plt.subplot(211) plt.plot(x, f, marker="*") plt.ylabel('$\sin(y)$') plt.grid(True) plt.subplot(212) df,xp = diff(f, x, 'simple') plt.plot(xp, df, marker="*", label='simple', linewidth=4) df,xp = diff(f, x, 'new') plt.plot(xp, df, marker="*", label='new') df,xp = diff(f, x, 'same') plt.plot(xp, df, marker="*", label='same') df,xp = diff(f, x, 'samesimple') plt.plot(xp, df, marker="*", label='samesimple') plt.ylabel('$d\sin(x)/dx$') plt.xlabel('$x$') plt.grid(True) plt.legend(loc='lower right') What do you think about adding it to scipy? Best, Robert

Dear Robert, 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?
I am not a SciPy developer myself but I thought that I would point out NumPy's diff function. It does not go as far as your routine in terms of options but goes most of the way. It applies the differentiation to order 'n' as an option that is similar to the recursive application. Given this existing routine, the incentive to add another one might be low. Regards, Pierre

Dear Pierre, Thank you for your reply. I am note fully aware of the difference between numpy and scipy and also not a developer, but it seems to me this function could fit in scipy where you often will want to differentiate w.r.t. a particular variable. I am aware of diff, but it is really a much more basic function. In many areas of science and engineering is the sampling points of crucial importance, and having a function giving you the appropriate sampling points can be very important for accuracy. I have been missing this function several times my self, and hence the reason why I submitted it here. Anyway, if there is no interest, I will of course not peruse the matter further. Best, Robert On 2016-11-05 22:53, Pierre de Buyl wrote:
Dear Robert,
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? I am not a SciPy developer myself but I thought that I would point out NumPy's diff function. It does not go as far as your routine in terms of options but goes most of the way. It applies the differentiation to order 'n' as an option
On Thu, Nov 03, 2016 at 08:37:07PM +0100, Robert Rehammar wrote: that is similar to the recursive application. Given this existing routine, the incentive to add another one might be low.
Regards,
Pierre
_______________________________________________ SciPy-Dev mailing list SciPy-Dev@scipy.org https://mail.scipy.org/mailman/listinfo/scipy-dev

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. 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). 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). Also, the name should not be "diff" to avoid confusion with numpy.diff. Regards, Jonathan On 11/6/16 23:53 , Robert Rehammar wrote:
Dear Pierre,
Thank you for your reply. I am note fully aware of the difference between numpy and scipy and also not a developer, but it seems to me this function could fit in scipy where you often will want to differentiate w.r.t. a particular variable. I am aware of diff, but it is really a much more basic function. In many areas of science and engineering is the sampling points of crucial importance, and having a function giving you the appropriate sampling points can be very important for accuracy.
I have been missing this function several times my self, and hence the reason why I submitted it here. Anyway, if there is no interest, I will of course not peruse the matter further.
Best,
Robert
On 2016-11-05 22:53, Pierre de Buyl wrote:
Dear Robert,
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? I am not a SciPy developer myself but I thought that I would point out NumPy's diff function. It does not go as far as your routine in terms of
On Thu, Nov 03, 2016 at 08:37:07PM +0100, Robert Rehammar wrote: options but goes most of the way. It applies the differentiation to order 'n' as an option that is similar to the recursive application. Given this existing routine, the incentive to add another one might be low.
Regards,
Pierre
_______________________________________________ SciPy-Dev mailing list SciPy-Dev@scipy.org https://mail.scipy.org/mailman/listinfo/scipy-dev
_______________________________________________ SciPy-Dev mailing list SciPy-Dev@scipy.org https://mail.scipy.org/mailman/listinfo/scipy-dev

Note that `approx_derivative` implements several finite-difference schemes, https://github.com/scipy/scipy/blob/master/scipy/optimize/_numdiff.py#L179 For the moment, it's well hidden in scipy.optimize, but in the long run the idea is to offer public functionality. So far the main issue was the lack of bandwidth --- meaning if someone feels like working on it, great! On Mon, Nov 7, 2016 at 7:21 PM, Jonathan Stickel <jjstickel@gmail.com> wrote:
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.
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).
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).
Also, the name should not be "diff" to avoid confusion with numpy.diff.
Regards, Jonathan
On 11/6/16 23:53 , Robert Rehammar wrote:
Dear Pierre,
Thank you for your reply. I am note fully aware of the difference between numpy and scipy and also not a developer, but it seems to me this function could fit in scipy where you often will want to differentiate w.r.t. a particular variable. I am aware of diff, but it is really a much more basic function. In many areas of science and engineering is the sampling points of crucial importance, and having a function giving you the appropriate sampling points can be very important for accuracy.
I have been missing this function several times my self, and hence the reason why I submitted it here. Anyway, if there is no interest, I will of course not peruse the matter further.
Best,
Robert
On 2016-11-05 22:53, Pierre de Buyl wrote:
Dear Robert,
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?
I am not a SciPy developer myself but I thought that I would point out NumPy's diff function. It does not go as far as your routine in terms of options but goes most of the way. It applies the differentiation to order 'n' as an option that is similar to the recursive application. Given this existing routine, the incentive to add another one might be low.
Regards,
Pierre
_______________________________________________ SciPy-Dev mailing list SciPy-Dev@scipy.org https://mail.scipy.org/mailman/listinfo/scipy-dev
_______________________________________________ SciPy-Dev mailing list SciPy-Dev@scipy.org https://mail.scipy.org/mailman/listinfo/scipy-dev
_______________________________________________ SciPy-Dev mailing list SciPy-Dev@scipy.org https://mail.scipy.org/mailman/listinfo/scipy-dev

This is also a good function to have, but it seems complementary to the function I propose since it operate on functions and mine operates on arrays. Best, Robert On 2016-11-07 17:52, Evgeni Burovski wrote:
Note that `approx_derivative` implements several finite-difference schemes,
https://github.com/scipy/scipy/blob/master/scipy/optimize/_numdiff.py#L179
For the moment, it's well hidden in scipy.optimize, but in the long run the idea is to offer public functionality. So far the main issue was the lack of bandwidth --- meaning if someone feels like working on it, great!
On Mon, Nov 7, 2016 at 7:21 PM, Jonathan Stickel <jjstickel@gmail.com> wrote:
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.
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).
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).
Also, the name should not be "diff" to avoid confusion with numpy.diff.
Regards, Jonathan
On 11/6/16 23:53 , Robert Rehammar wrote:
Dear Pierre,
Thank you for your reply. I am note fully aware of the difference between numpy and scipy and also not a developer, but it seems to me this function could fit in scipy where you often will want to differentiate w.r.t. a particular variable. I am aware of diff, but it is really a much more basic function. In many areas of science and engineering is the sampling points of crucial importance, and having a function giving you the appropriate sampling points can be very important for accuracy.
I have been missing this function several times my self, and hence the reason why I submitted it here. Anyway, if there is no interest, I will of course not peruse the matter further.
Best,
Robert
On 2016-11-05 22:53, Pierre de Buyl wrote:
Dear Robert,
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? I am not a SciPy developer myself but I thought that I would point out NumPy's diff function. It does not go as far as your routine in terms of
On Thu, Nov 03, 2016 at 08:37:07PM +0100, Robert Rehammar wrote: options but goes most of the way. It applies the differentiation to order 'n' as an option that is similar to the recursive application. Given this existing routine, the incentive to add another one might be low.
Regards,
Pierre
_______________________________________________ SciPy-Dev mailing list SciPy-Dev@scipy.org https://mail.scipy.org/mailman/listinfo/scipy-dev
_______________________________________________ SciPy-Dev mailing list SciPy-Dev@scipy.org https://mail.scipy.org/mailman/listinfo/scipy-dev
SciPy-Dev mailing list SciPy-Dev@scipy.org https://mail.scipy.org/mailman/listinfo/scipy-dev
SciPy-Dev mailing list SciPy-Dev@scipy.org https://mail.scipy.org/mailman/listinfo/scipy-dev

Mon, 07 Nov 2016 19:52:09 +0300, Evgeni Burovski kirjoitti:
Note that `approx_derivative` implements several finite-difference schemes,
In addition, I'd remind of https://pypi.python.org/pypi/Numdifftools

On Wed, Nov 9, 2016 at 8:01 AM, Pauli Virtanen <pav@iki.fi> wrote:
Mon, 07 Nov 2016 19:52:09 +0300, Evgeni Burovski kirjoitti:
Note that `approx_derivative` implements several finite-difference schemes,
In addition, I'd remind of
And https://github.com/scipy/scipy/wiki/Proposal:-add-finite-difference-numerica... Ralf

The current discussion lacks a reference to the existing Savitzky-Golay filter https://scipy.github.io/devdocs/generated/scipy.signal.savgol_filter.html which - to my understanding - should solves most of Robert's problems. thomas On Thu, Nov 10, 2016 at 8:10 AM, Ralf Gommers <ralf.gommers@gmail.com> wrote:
On Wed, Nov 9, 2016 at 8:01 AM, Pauli Virtanen <pav@iki.fi> wrote:
Mon, 07 Nov 2016 19:52:09 +0300, Evgeni Burovski kirjoitti:
Note that `approx_derivative` implements several finite-difference schemes,
In addition, I'd remind of
And https://github.com/scipy/scipy/wiki/Proposal:-add- finite-difference-numerical-derivatives-as-scipy.diff
Ralf
_______________________________________________ SciPy-Dev mailing list SciPy-Dev@scipy.org https://mail.scipy.org/mailman/listinfo/scipy-dev
-- --- Thomas Haslwanter Hubertusgasse 26, A-4060 Leonding http://home.thaslwanter.at

On 11/10/16 01:19 , Thomas Haslwanter wrote:
The current discussion lacks a reference to the existing Savitzky-Golay filter https://scipy.github.io/devdocs/generated/scipy.signal.savgol_filter.html which - to my understanding - should solves most of Robert's problems.
thomas
No, I don't think this addresses Robert's needs. That is simply a data smoother (and arguably inferior to other data-smoothing methods). Although it does have an option to provide a derivative, it presumes the data are equally spaced.
On Thu, Nov 10, 2016 at 8:10 AM, Ralf Gommers <ralf.gommers@gmail.com <mailto:ralf.gommers@gmail.com>> wrote:
On Wed, Nov 9, 2016 at 8:01 AM, Pauli Virtanen <pav@iki.fi <mailto:pav@iki.fi>> wrote:
Mon, 07 Nov 2016 19:52:09 +0300, Evgeni Burovski kirjoitti: > Note that `approx_derivative` implements several finite-difference > schemes,
In addition, I'd remind of
https://pypi.python.org/pypi/Numdifftools <https://pypi.python.org/pypi/Numdifftools>
And https://github.com/scipy/scipy/wiki/Proposal:-add-finite-difference-numerica... <https://github.com/scipy/scipy/wiki/Proposal:-add-finite-difference-numerica...>
Ralf
These are tools for finite-differences of a known function. Robert (and I) are interested in finite-differences of y vs. x vectors, whether obtained from experiment or as part of a higher-level numerical method.

Maybe this addresses Robert's needs: http://www.scholarpedia.org/article/Finite_difference_method#FD_formulas_in_... https://github.com/pbrod/numdifftools/blob/master/numdifftools/fornberg.py Per A. -----Original Message----- From: SciPy-Dev [mailto:scipy-dev-bounces@scipy.org] On Behalf Of Jonathan Stickel Sent: 10. november 2016 18:32 To: scipy-dev@scipy.org Subject: Re: [SciPy-Dev] Differentiate function On 11/10/16 01:19 , Thomas Haslwanter wrote:
The current discussion lacks a reference to the existing Savitzky-Golay filter https://scipy.github.io/devdocs/generated/scipy.signal.savgol_filter.h tml which - to my understanding - should solves most of Robert's problems.
thomas
No, I don't think this addresses Robert's needs. That is simply a data smoother (and arguably inferior to other data-smoothing methods). Although it does have an option to provide a derivative, it presumes the data are equally spaced.
On Thu, Nov 10, 2016 at 8:10 AM, Ralf Gommers <ralf.gommers@gmail.com <mailto:ralf.gommers@gmail.com>> wrote:
On Wed, Nov 9, 2016 at 8:01 AM, Pauli Virtanen <pav@iki.fi <mailto:pav@iki.fi>> wrote:
Mon, 07 Nov 2016 19:52:09 +0300, Evgeni Burovski kirjoitti: > Note that `approx_derivative` implements several finite-difference > schemes,
In addition, I'd remind of
https://pypi.python.org/pypi/Numdifftools <https://pypi.python.org/pypi/Numdifftools>
And https://github.com/scipy/scipy/wiki/Proposal:-add-finite-difference-numerica...
<https://github.com/scipy/scipy/wiki/Proposal:-add-finite-difference-n umerical-derivatives-as-scipy.diff>
Ralf
These are tools for finite-differences of a known function. Robert (and I) are interested in finite-differences of y vs. x vectors, whether obtained from experiment or as part of a higher-level numerical method. _______________________________________________ SciPy-Dev mailing list SciPy-Dev@scipy.org https://mail.scipy.org/mailman/listinfo/scipy-dev

There’s also scipy.misc.derivative <https://docs.scipy.org/doc/scipy/reference/generated/scipy.misc.derivative.h...> which also finds the derivative of a function. There’s not only the method of finite differences, but there’s also automatic differentiation <https://en.wikipedia.org/wiki/Automatic_differentiation> In this, you keep track of what operations are performed on the input, then use the chain rule to find the derivative. Because it only keeps track of the functions performed, this method support if-statments, while-loops and recursion. Here’s a package that implements this method with a thin wrapper around NumPy: https://github.com/HIPS/autograd (though it looks like numdifftools also support this) Numerical Optimization 2nd edition by Stephen Wright also has more detail on chapter 8 methods to compute derivatives (and chapter 9 is on derivative free optimization!). Scott On November 11, 2016 at 5:04:12 PM, per.brodtkorb@ffi.no ( per.brodtkorb@ffi.no) wrote: Maybe this addresses Robert's needs: http://www.scholarpedia.org/article/Finite_difference_method#FD_formulas_in_... https://github.com/pbrod/numdifftools/blob/master/numdifftools/fornberg.py Per A. -----Original Message----- From: SciPy-Dev [mailto:scipy-dev-bounces@scipy.org] On Behalf Of Jonathan Stickel Sent: 10. november 2016 18:32 To: scipy-dev@scipy.org Subject: Re: [SciPy-Dev] Differentiate function On 11/10/16 01:19 , Thomas Haslwanter wrote:
The current discussion lacks a reference to the existing Savitzky-Golay filter https://scipy.github.io/devdocs/generated/scipy.signal.savgol_filter.h tml which - to my understanding - should solves most of Robert's problems.
thomas
No, I don't think this addresses Robert's needs. That is simply a data smoother (and arguably inferior to other data-smoothing methods). Although it does have an option to provide a derivative, it presumes the data are equally spaced.
On Thu, Nov 10, 2016 at 8:10 AM, Ralf Gommers <ralf.gommers@gmail.com <mailto:ralf.gommers@gmail.com>> wrote:
On Wed, Nov 9, 2016 at 8:01 AM, Pauli Virtanen <pav@iki.fi <mailto:pav@iki.fi>> wrote:
Mon, 07 Nov 2016 19:52:09 +0300, Evgeni Burovski kirjoitti:
Note that `approx_derivative` implements several finite-difference schemes,
In addition, I'd remind of
https://pypi.python.org/pypi/Numdifftools <https://pypi.python.org/pypi/Numdifftools>
And
https://github.com/scipy/scipy/wiki/Proposal:-add-finite-difference-numerica...
<https://github.com/scipy/scipy/wiki/Proposal:-add-finite-difference-n umerical-derivatives-as-scipy.diff>
Ralf
These are tools for finite-differences of a known function. Robert (and I) are interested in finite-differences of y vs. x vectors, whether obtained from experiment or as part of a higher-level numerical method. _______________________________________________ SciPy-Dev mailing list SciPy-Dev@scipy.org https://mail.scipy.org/mailman/listinfo/scipy-dev _______________________________________________ SciPy-Dev mailing list SciPy-Dev@scipy.org https://mail.scipy.org/mailman/listinfo/scipy-dev

On Fri, Nov 11, 2016 at 6:57 PM, Scott Sievert <sievert.scott@gmail.com> wrote:
There’s also scipy.misc.derivative <https://docs.scipy.org/doc/scipy/reference/generated/scipy.misc.derivative.h...> which also finds the derivative of a function.
There’s not only the method of finite differences, but there’s also automatic differentiation <https://en.wikipedia.org/wiki/Automatic_differentiation> In this, you keep track of what operations are performed on the input, then use the chain rule to find the derivative. Because it only keeps track of the functions performed, this method support if-statments, while-loops and recursion.
Here’s a package that implements this method with a thin wrapper around NumPy: https://github.com/HIPS/autograd (though it looks like numdifftools also support this)
Numerical Optimization 2nd edition by Stephen Wright also has more detail on chapter 8 methods to compute derivatives (and chapter 9 is on derivative free optimization!).
Scott
On November 11, 2016 at 5:04:12 PM, per.brodtkorb@ffi.no ( per.brodtkorb@ffi.no) wrote:
Maybe this addresses Robert's needs:
http://www.scholarpedia.org/article/Finite_difference_ method#FD_formulas_in_higher-D
https://github.com/pbrod/numdifftools/blob/master/numdifftools/fornberg.py
Per A.
-----Original Message----- From: SciPy-Dev [mailto:scipy-dev-bounces@scipy.org] On Behalf Of Jonathan Stickel Sent: 10. november 2016 18:32 To: scipy-dev@scipy.org Subject: Re: [SciPy-Dev] Differentiate function
On 11/10/16 01:19 , Thomas Haslwanter wrote:
The current discussion lacks a reference to the existing Savitzky-Golay filter https://scipy.github.io/devdocs/generated/scipy.signal.savgol_filter.h tml which - to my understanding - should solves most of Robert's problems.
thomas
No, I don't think this addresses Robert's needs. That is simply a data smoother (and arguably inferior to other data-smoothing methods). Although it does have an option to provide a derivative, it presumes the data are equally spaced.
On Thu, Nov 10, 2016 at 8:10 AM, Ralf Gommers <ralf.gommers@gmail.com <mailto:ralf.gommers@gmail.com>> wrote:
On Wed, Nov 9, 2016 at 8:01 AM, Pauli Virtanen <pav@iki.fi <mailto:pav@iki.fi>> wrote:
Mon, 07 Nov 2016 19:52:09 +0300, Evgeni Burovski kirjoitti:
Note that `approx_derivative` implements several finite-difference schemes,
In addition, I'd remind of
https://pypi.python.org/pypi/Numdifftools <https://pypi.python.org/pypi/Numdifftools>
And https://github.com/scipy/scipy/wiki/Proposal:-add- finite-difference-numerical-derivatives-as-scipy.diff
<https://github.com/scipy/scipy/wiki/Proposal:-add-finite-difference-n umerical-derivatives-as-scipy.diff>
Ralf
These are tools for finite-differences of a known function. Robert (and I) are interested in finite-differences of y vs. x vectors, whether obtained from experiment or as part of a higher-level numerical method. _______________________________________________ SciPy-Dev mailing list SciPy-Dev@scipy.org https://mail.scipy.org/mailman/listinfo/scipy-dev _______________________________________________ SciPy-Dev mailing list SciPy-Dev@scipy.org https://mail.scipy.org/mailman/listinfo/scipy-dev
_______________________________________________ SciPy-Dev mailing list SciPy-Dev@scipy.org https://mail.scipy.org/mailman/listinfo/scipy-dev
To throw one more in here local polynomial regression (local kernel or windows) would be another way. AFAIR, it would be similar to Savitsky-Golay for non-equal spaced grid. IIRC, the slope coefficient of the local linear regression is the derivative of the smooth function and has relatively good edge behavior because of the local linear structure. I think higher order derivatives would follow from the coefficients of higher order polynomial regression. That would be the kernel regression analog of smoothing splines version. Josef

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
Jonathan, Please see below. 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 11/6/16 23:53 , Robert Rehammar wrote:
Dear Pierre,
Thank you for your reply. I am note fully aware of the difference between numpy and scipy and also not a developer, but it seems to me this function could fit in scipy where you often will want to differentiate w.r.t. a particular variable. I am aware of diff, but it is really a much more basic function. In many areas of science and engineering is the sampling points of crucial importance, and having a function giving you the appropriate sampling points can be very important for accuracy.
I have been missing this function several times my self, and hence the reason why I submitted it here. Anyway, if there is no interest, I will of course not peruse the matter further.
Best,
Robert
On 2016-11-05 22:53, Pierre de Buyl wrote:
Dear Robert,
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? I am not a SciPy developer myself but I thought that I would point out NumPy's diff function. It does not go as far as your routine in terms of
On Thu, Nov 03, 2016 at 08:37:07PM +0100, Robert Rehammar wrote: options but goes most of the way. It applies the differentiation to order 'n' as an option that is similar to the recursive application. Given this existing routine, the incentive to add another one might be low.
Regards,
Pierre
_______________________________________________ SciPy-Dev mailing list SciPy-Dev@scipy.org https://mail.scipy.org/mailman/listinfo/scipy-dev
_______________________________________________ SciPy-Dev mailing list SciPy-Dev@scipy.org https://mail.scipy.org/mailman/listinfo/scipy-dev

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
participants (10)
-
Evgeni Burovski
-
Jonathan Stickel
-
josef.pktd@gmail.com
-
Pauli Virtanen
-
Per.Brodtkorb@ffi.no
-
Pierre de Buyl
-
Ralf Gommers
-
Robert Rehammar
-
Scott Sievert
-
Thomas Haslwanter