4

I have sampled functions on 2D and 3D numpy arrays and I need a way to take partial derivatives from these arrays. I could code the finite difference schemes manually, but I need more than just 2nd order accuracy, probably 4th or even sixth order. With higher accuracy orders coding it manually quickly becomes tedious, especially because I need it for arrays of different dimensions.

Is there function in numpy or scipy or some other package that can do that conveniently?

lvdhorst
  • 68
  • 1
  • 5
  • 2
    Are you looking for [`numpy.diff`](https://docs.scipy.org/doc/numpy/reference/generated/numpy.diff.html)? – user2357112 Apr 06 '18 at 16:54
  • Have you seen this [SO](https://stackoverflow.com/questions/18991408/python-finite-difference-functions) it has a really good discussion and a solution — this might be what you’re looking for ? – gyx-hh Apr 06 '18 at 16:57
  • Nah, probably not `numpy.diff`. After searching for information on finite differences, I'm not sure how accuracy orders work, but I'm pretty sure `numpy.diff` doesn't support them. – user2357112 Apr 06 '18 at 16:57
  • Not really, numpy.diff is for the n-th difference along a certain access, but it doesn't provide me with the finite difference weights appropriate for the desired accuracy and derivative order. – lvdhorst Apr 06 '18 at 16:58
  • @Hamza: yes, have seen that, it's a good one, but with that solution I would have to dig out and insert the weights manually. – lvdhorst Apr 06 '18 at 17:01

1 Answers1

6

You may want to take a look at the findiff package. It let's you conveniently take derivatives of numpy arrays of any dimension, any derivative order and any desired accuracy order. The project website says that it features:

  • Differentiate arrays of any number of dimensions along any axis
  • Partial derivatives of any desired order
  • Standard operators from vector calculus like gradient, divergence and curl
  • Can handle uniform and non-uniform grids
  • Can handle arbitrary linear combinations of derivatives with constant and variable coefficients
  • Accuracy order can be specified
  • Fully vectorized for speed
  • Calculate raw finite difference coefficients for any order and accuracy for uniform and non-uniform grids
owelk
  • 176
  • 1
  • 3