NumericalDerivative

class odl.functionals.derivatives.NumericalDerivative(*args, **kwargs)[source]

Bases: Operator

The derivative of an operator by finite differences.

See Also

NumericalGradient : Compute gradient of a functional

__init__(operator, point, method='forward', step=None)[source]

Initialize a new instance.

Parameters

operatorOperator

The operator whose derivative should be computed numerically. Its domain and range must be TensorSpace spaces.

pointoperator.domain element-like

The point to compute the derivative in.

method{‘backward’, ‘forward’, ‘central’}, optional

The method to use to compute the derivative.

stepfloat, optional

The step length used in the derivative computation. Default: selects the step according to the dtype of the space.

Examples

Compute a numerical estimate of the derivative (Hessian) of the squared L2 norm:

>>> space = odl.rn(3)
>>> func = odl.functionals.L2NormSquared(space)
>>> hess = NumericalDerivative(func.gradient, [1, 1, 1])
>>> hess([0, 0, 1])
rn(3).element([ 0.,  0.,  2.])

Find the Hessian matrix:

>>> hess_matrix = odl.matrix_representation(hess)
>>> np.allclose(hess_matrix, 2 * np.eye(3))
True

Notes

If the operator is A and step size h is used, the derivative in the point x and direction dx is computed as follows.

method='backward':

\partial A(x)(dx) =
(A(x) - A(x - dx \cdot h / \| dx \|))
\cdot \frac{\| dx \|}{h}

method='forward':

\partial A(x)(dx) =
(A(x + dx \cdot h / \| dx \|) - A(x))
\cdot \frac{\| dx \|}{h}

method='central':

\partial A(x)(dx) =
(A(x + dx \cdot h / (2 \| dx \|)) -
 A(x - dx \cdot h / (2 \| dx \|))
\cdot \frac{\| dx \|}{h}

The number of operator evaluations is 2, regardless of parameters.