5.1.6.1. numdifftools.nd_algopy.Derivative
- class Derivative(fun, n=1, method='forward', full_output=False)[source]
Calculate n-th derivative with Algorithmic Differentiation method
- Parameters:
fun (function) – function of one array fun(x, *args, **kwds)
n (int, optional) – Order of the derivative.
method (string, optional {'forward', 'reverse'}) – defines method used in the approximation
- __call__: callable with the following parameters:
- x: array_like
value at which function derivative is evaluated
- args: tuple
Arguments for function fun.
- kwds: dict
Keyword arguments for function fun.
- Returns:
der – array of derivatives
- Return type:
ndarray
Notes
Algorithmic differentiation is a set of techniques to numerically evaluate the derivative of a function specified by a computer program. AD exploits the fact that every computer program, no matter how complicated, executes a sequence of elementary arithmetic operations (addition, subtraction, multiplication, division, etc.) and elementary functions (exp, log, sin, cos, etc.). By applying the chain rule repeatedly to these operations, derivatives of arbitrary order can be computed automatically, accurately to working precision, and using at most a small constant factor more arithmetic operations than the original program.
References
Sebastian F. Walter and Lutz Lehmann 2013, “Algorithmic differentiation in Python with AlgoPy”, in Journal of Computational Science, vol 4, no 5, pp 334 - 344, http://www.sciencedirect.com/science/article/pii/S1877750311001013
https://en.wikipedia.org/wiki/Automatic_differentiation
Examples
# 1’st and 2’nd derivative of exp(x), at x == 1
>>> import numpy as np >>> import numdifftools.nd_algopy as nda >>> fd = nda.Derivative(np.exp) # 1'st derivative >>> np.allclose(fd(1), 2.718281828459045) True >>> fd5 = nda.Derivative(np.exp, n=5) # 5'th derivative >>> np.allclose(fd5(1), 2.718281828459045) True
# 1’st derivative of x^3+x^4, at x = [0,1]
>>> fun = lambda x: x**3 + x**4 >>> fd3 = nda.Derivative(fun) >>> np.allclose(fd3([0,1]), [ 0., 7.]) True
- __init__(fun, n=1, method='forward', full_output=False)
Methods
__init__(fun[, n, method, full_output])computational_graph(x, *args, **kwds)Attributes
fun