1
2
3 """
4 Module with several methods that function as operators for numerical grids.
5
6 """
7
8 import numpy as np
9
11
12 '''
13 Calculate the numerical central difference of a function with respect to a
14 single variable.
15
16 This is essentially what np.gradient does, but allows a non-equidistant
17 independent variable.
18
19 @param x: The independent variable
20 @type x: array
21 @param y: The variable dependent on x
22 @type y: array
23
24 @return: diff_central(y)/diff_central(x)
25 @rtype: array
26
27 '''
28
29 z1 = np.hstack((y[0], y[:-1]))
30 z2 = np.hstack((y[1:], y[-1]))
31 dx1 = np.hstack((0, np.diff(x)))
32 dx2 = np.hstack((np.diff(x), 0))
33 return (z2-z1) / (dx2+dx1)
34