NumericalDerivative

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

Bases: odl.operator.operator.Operator

The derivative of an operator by finite differences.

See also

NumericalGradient

Compute gradient of a functional

Attributes
adjoint

Adjoint of this operator (abstract).

domain

Set of objects on which this operator can be evaluated.

inverse

Return the operator inverse.

is_functional

True if this operator’s range is a Field.

is_linear

True if this operator is linear.

range

Set in which the result of an evaluation of this operator lies.

Methods

_call(self, dx)

Return self(x).

derivative(self, point)

Return the operator derivative at point.

norm(self[, estimate])

Return the operator norm of this operator.

__init__(self, 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.

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.

Examples

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

>>> space = odl.rn(3)
>>> func = odl.solvers.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