scipy 1.8.0 Pypi GitHub Homepage
Other Docs
ParametersReturnsBackRef
check_derivative(fun, jac, x0, bounds=(-inf, inf), args=(), kwargs={})

Parameters

fun : callable

Function of which to estimate the derivatives. The argument x passed to this function is ndarray of shape (n,) (never a scalar even if n=1). It must return 1-D array_like of shape (m,) or a scalar.

jac : callable

Function which computes Jacobian matrix of :None:None:`fun`. It must work with argument x the same way as :None:None:`fun`. The return value must be array_like or sparse matrix with an appropriate shape.

x0 : array_like of shape (n,) or float

Point at which to estimate the derivatives. Float will be converted to 1-D array.

bounds : 2-tuple of array_like, optional

Lower and upper bounds on independent variables. Defaults to no bounds. Each bound must match the size of :None:None:`x0` or be a scalar, in the latter case the bound will be the same for all variables. Use it to limit the range of function evaluation.

args, kwargs : tuple and dict, optional

Additional arguments passed to :None:None:`fun` and :None:None:`jac`. Both empty by default. The calling signature is fun(x, *args, **kwargs) and the same for :None:None:`jac`.

Returns

accuracy : float

The maximum among all relative errors for elements with absolute values higher than 1 and absolute errors for elements with absolute values less or equal than 1. If :None:None:`accuracy` is on the order of 1e-6 or lower, then it is likely that your :None:None:`jac` implementation is correct.

Check correctness of a function computing derivatives (Jacobian or gradient) by comparison with a finite difference approximation.

See Also

approx_derivative

Compute finite difference approximation of derivative.

Examples

This example is valid syntax, but raise an exception at execution
>>> import numpy as np
... from scipy.optimize import check_derivative >>> >>>
>>> def f(x, c1, c2):
...  return np.array([x[0] * np.sin(c1 * x[1]),
...  x[0] * np.cos(c2 * x[1])]) ...
>>> def jac(x, c1, c2):
...  return np.array([
...  [np.sin(c1 * x[1]), c1 * x[0] * np.cos(c1 * x[1])],
...  [np.cos(c2 * x[1]), -c2 * x[0] * np.sin(c2 * x[1])]
...  ]) ... >>>
>>> x0 = np.array([1.0, 0.5 * np.pi])
... check_derivative(f, jac, x0, args=(1, 2)) 2.4492935982947064e-16
See :

Back References

The following pages refer to to this document either explicitly or contain code examples using this.

scipy.optimize._numdiff.approx_derivative scipy.optimize._numdiff.check_derivative

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


GitHub : /scipy/optimize/_numdiff.py#686
type: <class 'function'>
Commit: