finite_diff

odl.discr.diff_ops.finite_diff(f, axis, dx=1.0, method='forward', out=None, pad_mode='constant', pad_const=0)[source]

Calculate the partial derivative of f along a given axis.

In the interior of the domain of f, the partial derivative is computed using first-order accurate forward or backward difference or second-order accurate central differences.

With padding the same method and thus accuracy is used on endpoints as in the interior i.e. forward and backward differences use first-order accuracy on edges while central differences use second-order accuracy at edges.

Without padding one-sided forward or backward differences are used at the boundaries. The accuracy at the endpoints can then also be triggered by the edge order.

The returned array has the same shape as the input array f.

Per default forward difference with dx=1 and no padding is used.

Parameters
farray-like

An N-dimensional array.

axisint

The axis along which the partial derivative is evaluated.

dxfloat, optional

Scalar specifying the distance between sampling points along axis.

method{‘central’, ‘forward’, ‘backward’}, optional
Finite difference method which is used in the interior of the domain

of f.

outnumpy.ndarray, optional

An N-dimensional array to which the output is written. Has to have the same shape as the input array f.

pad_modestring, optional

The padding mode to use outside the domain.

'constant': Fill with pad_const.

'symmetric': Reflect at the boundaries, not doubling the outmost values.

'periodic': Fill in values from the other side, keeping the order.

'order0': Extend constantly with the outmost values (ensures continuity).

'order1': Extend with constant slope (ensures continuity of the first derivative). This requires at least 2 values along each axis where padding is applied.

'order2': Extend with second order accuracy (ensures continuity of the second derivative). This requires at least 3 values along each axis where padding is applied.

pad_constfloat, optional

For pad_mode == 'constant', f assumes pad_const for indices outside the domain of f

Returns
outnumpy.ndarray

N-dimensional array of the same shape as f. If out was provided, the returned object is a reference to it.

Examples

>>> f = np.array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9.])
>>> finite_diff(f, axis=0)
array([ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  -9.])

Without arguments the above defaults to:

>>> finite_diff(f, axis=0, dx=1.0, method='forward', pad_mode='constant')
array([ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  -9.])

Parameters can be changed one by one:

>>> finite_diff(f, axis=0, dx=0.5)
array([ 2.,  2.,  2.,  2.,  2.,  2.,  2.,  2.,  2.,  -18.])
>>> finite_diff(f, axis=0, pad_mode='order1')
array([ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1., 1.])

Central differences and different edge orders:

>>> finite_diff(0.5 * f ** 2, axis=0, method='central', pad_mode='order1')
array([ 0.5,  1. ,  2. ,  3. ,  4. ,  5. ,  6. ,  7. ,  8. ,  8.5])
>>> finite_diff(0.5 * f ** 2, axis=0, method='central', pad_mode='order2')
array([-0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9.])

In-place evaluation:

>>> out = f.copy()
>>> out is finite_diff(f, axis=0, out=out)
True