discretize.tests.check_derivative#
- discretize.tests.check_derivative(fctn, x0, num=7, plotIt=False, dx=None, expectedOrder=2, tolerance=0.85, eps=1e-10, ax=None, random_seed=None)[source]#
Perform a basic derivative check.
Compares error decay of 0th and 1st order Taylor approximation at point x0 for a randomized search direction.
- Parameters:
- fctn
callable()
The function to test.
- x0
numpy.ndarray
Point at which to check derivative
- num
int
,optional
number of times to reduce step length to evaluate derivative
- plotItbool,
optional
If True, plot the convergence of the approximation of the derivative
- dx
numpy.ndarray
,optional
Step direction. By default, this parameter is set to None and a random step direction is chosen using rng.
- expectedOrder
int
,optional
The expected order of convergence for the numerical derivative
- tolerance
float
,optional
The tolerance on the expected order
- eps
float
,optional
A threshold value for approximately equal to zero
- ax
matplotlib.pyplot.Axes
,optional
An axis object for the convergence plot if plotIt = True. Otherwise, the function will create a new axis.
- random_seed
numpy.random.Generator
,int
,optional
If dx is
None
, this is the random number generator to use for generating a step direction. If an integer or None, it is used to seed a new numpy.random.default_rng.
- fctn
- Returns:
- bool
Whether you passed the test.
Examples
>>> from discretize import tests, utils >>> import numpy as np >>> import matplotlib.pyplot as plt >>> rng = np.random.default_rng(786412)
>>> def simplePass(x): ... return np.sin(x), utils.sdiag(np.cos(x)) >>> passed = tests.check_derivative(simplePass, rng.standard_normal(5), random_seed=rng) ==================== check_derivative ==================== iter h |ft-f0| |ft-f0-h*J0*dx| Order --------------------------------------------------------- 0 1.00e-01 1.690e-01 8.400e-03 nan 1 1.00e-02 1.636e-02 8.703e-05 1.985 2 1.00e-03 1.630e-03 8.732e-07 1.999 3 1.00e-04 1.629e-04 8.735e-09 2.000 4 1.00e-05 1.629e-05 8.736e-11 2.000 5 1.00e-06 1.629e-06 8.736e-13 2.000 6 1.00e-07 1.629e-07 8.822e-15 1.996 ========================= PASS! ========================= Once upon a time, a happy little test passed.