Skip to main content

pkg_img tests_img Documentation Maintainability coverage_img versions_img PyPI - Downloads

The numdifftools library is a suite of tools written in _Python to solve automatic numerical differentiation problems in one or more variables. Finite differences are used in an adaptive manner, coupled with a Richardson extrapolation methodology to provide a maximally accurate result. The user can configure many options like; changing the order of the method or the extrapolation, even allowing the user to specify whether complex-step, central, forward or backward differences are used.

The methods provided are:

  • Derivative: Compute the derivatives of order 1 through 10 on any scalar function.

  • directionaldiff: Compute directional derivative of a function of n variables

  • Gradient: Compute the gradient vector of a scalar function of one or more variables.

  • Jacobian: Compute the Jacobian matrix of a vector valued function of one or more variables.

  • Hessian: Compute the Hessian matrix of all 2nd partial derivatives of a scalar function of one or more variables.

  • Hessdiag: Compute only the diagonal elements of the Hessian matrix

All of these methods also produce error estimates on the result.

Numdifftools also provide an easy to use interface to derivatives calculated with in _AlgoPy. Algopy stands for Algorithmic Differentiation in Python. The purpose of AlgoPy is the evaluation of higher-order derivatives in the forward and reverse mode of Algorithmic Differentiation (AD) of functions that are implemented as Python programs.

Getting Started

Visualize high order derivatives of the tanh function

>>> import numpy as np
>>> import numdifftools as nd
>>> import matplotlib.pyplot as plt
>>> x = np.linspace(-2, 2, 100)
>>> for i in range(10):
...    df = nd.Derivative(np.tanh, n=i)
...    y = df(x)
...    h = plt.plot(x, y/np.abs(y).max())
>>> plt.show() # doctest: +SKIP
https://raw.githubusercontent.com/pbrod/numdifftools/master/examples/fun.png

Compute 1’st and 2’nd derivative of exp(x), at x == 1:

>>> fd = nd.Derivative(np.exp)        # 1'st derivative
>>> fdd = nd.Derivative(np.exp, n=2)  # 2'nd derivative
>>> np.allclose(fd(1), 2.7182818284590424)
True
>>> np.allclose(fdd(1), 2.7182818284590424)
True

Nonlinear least squares:

>>> xdata = np.reshape(np.arange(0,1,0.1),(-1,1))
>>> ydata = 1+2*np.exp(0.75*xdata)
>>> fun = lambda c: (c[0]+c[1]*np.exp(c[2]*xdata) - ydata)**2
>>> Jfun = nd.Jacobian(fun)
>>> np.allclose(np.abs(Jfun([1,2,0.75])), 0) # should be numerically zero
True

Compute gradient of sum(x**2):

>>> fun = lambda x: np.sum(x**2)
>>> dfun = nd.Gradient(fun)
>>> np.allclose(dfun([1,2,3]), [ 2.,  4.,  6.])
True

Compute the same with the easy to use interface to AlgoPy:

>>> import numdifftools.nd_algopy as nda
>>> import numpy as np
>>> fd = nda.Derivative(np.exp)        # 1'st derivative
>>> fdd = nda.Derivative(np.exp, n=2)  # 2'nd derivative
>>> np.allclose(fd(1), 2.7182818284590424)
True
>>> np.allclose(fdd(1), 2.7182818284590424)
True

Nonlinear least squares:

>>> xdata = np.reshape(np.arange(0,1,0.1),(-1,1))
>>> ydata = 1+2*np.exp(0.75*xdata)
>>> fun = lambda c: (c[0]+c[1]*np.exp(c[2]*xdata) - ydata)**2
>>> Jfun = nda.Jacobian(fun, method='reverse')
>>> np.allclose(np.abs(Jfun([1,2,0.75])), 0) # should be numerically zero
True

Compute gradient of sum(x**2):

>>> fun = lambda x: np.sum(x**2)
>>> dfun = nda.Gradient(fun)
>>> np.allclose(dfun([1,2,3]), [ 2.,  4.,  6.])
True

See also

scipy.misc.derivative

Documentation and code

Official releases available at: http://pypi.python.org/pypi/numdifftools pkg_img

Official documentation available at: http://numdifftools.readthedocs.io/en/latest/ Documentation

Bleeding edge: https://github.com/pbrod/numdifftools.

Installation

If you have pip installed, then simply type:

$ pip install numdifftools

to get the lastest stable version. Using pip also has the advantage that all requirements are automatically installed.

Unit tests

To test if the toolbox is working paste the following in an interactive python session:

import numdifftools as nd
nd.test('--doctest-modules', '--disable-warnings')

Acknowledgement

The numdifftools package for Python was written by Per A. Brodtkorb based on the adaptive numerical differentiation toolbox written in Matlab by John D’Errico [DErrico06].

Later the package was extended with some of the functionality found in the statsmodels.tools.numdiff module written by Josef Perktold [JPerktold14] which is based on [Rid09]. The implementation of bicomplex numbers is based on the matlab implementation described in the project report of [Ver14] which is based on [GLD12]. For completeness the [For98] method for computing the weights and points in general finite difference formulas as well as the [For81] method for cumputing the taylor coefficients of complex analytic function using FFT, was added.

References

[Ver14]

Adriaen Verheyleweghen, (2014) “Computation of higher-order derivatives using the multi-complex step method”, Project report, NTNU

[GLD12]

Gregory Lantoine, R.P. Russell, and T. Dargent (2012) “Using multicomplex variables for automatic computation of high-order derivatives”, ACM Transactions on Mathematical Software, Vol. 38, No. 3, Article 16, April 2012, 21 pages, http://doi.acm.org/10.1145/2168773.2168774

[MELEV12]

M.E. Luna-Elizarraras, M. Shapiro, D.C. Struppa1, A. Vajiac (2012), “Bicomplex Numbers and Their Elementary Functions”, CUBO A Mathematical Journal, Vol. 14, No 2, (61-80). June 2012.

[Lan10]

Gregory Lantoine (2010), “A methodology for robust optimization of low-thrust trajectories in multi-body environments”, Phd thesis, Georgia Institute of Technology

[Rid09]

Ridout, M.S. (2009) “Statistical applications of the complex-step method of numerical differentiation”, The American Statistician, 63, 66-74

[DErrico06]

D’Errico, J. R. (2006), “Adaptive Robust Numerical Differentiation”, http://www.mathworks.com/matlabcentral/fileexchange/13490-adaptive-robust-numerical-differentiation

[KLLK05]

K.-L. Lai, J.L. Crassidis, Y. Cheng, J. Kim (2005), “New complex step derivative approximations with application to second-order kalman filtering”, AIAA Guidance, Navigation and Control Conference, San Francisco, California, August 2005, AIAA-2005-5944.

[For98]

B. Fornberg (1998) “Calculation of weights_and_points in finite difference formulas”, SIAM Review 40, pp. 685-691.

[For81]

Fornberg, B. (1981). “Numerical Differentiation of Analytic Functions”, ACM Transactions on Mathematical Software (TOMS), 7(4), 512-526. http://doi.org/10.1145/355972.355979

[JML69]

Lyness, J. M., Moler, C. B. (1969). “Generalized Romberg Methods for Integrals of Derivatives”, Numerische Mathematik.

[JML66]

Lyness, J. M., Moler, C. B. (1966). “Vandermonde Systems and Numerical Differentiation”, Numerische Mathematik.

[NAG]

NAG Library. NAG Fortran Library Document: D04AAF

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

numdifftools-0.11.1.tar.gz (82.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

numdifftools-0.11.1-py3-none-any.whl (96.5 kB view details)

Uploaded Python 3

File details

Details for the file numdifftools-0.11.1.tar.gz.

File metadata

  • Download URL: numdifftools-0.11.1.tar.gz
  • Upload date:
  • Size: 82.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: pdm/2.29.0 CPython/3.13.15 Linux/6.17.0-1022-azure

File hashes

Hashes for numdifftools-0.11.1.tar.gz
Algorithm Hash digest
SHA256 af6d58c22eb201fef9aa4dfc8caf286b14e10154a736c03a45f0a1df2bcec452
MD5 660d9cd304ef6ef83148541ba34805ed
BLAKE2b-256 75e033c9240387173d4a92ae777a91deac759f0b8e9e47e4e3fbd984e17980d2

See more details on using hashes here.

File details

Details for the file numdifftools-0.11.1-py3-none-any.whl.

File metadata

  • Download URL: numdifftools-0.11.1-py3-none-any.whl
  • Upload date:
  • Size: 96.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: pdm/2.29.0 CPython/3.13.15 Linux/6.17.0-1022-azure

File hashes

Hashes for numdifftools-0.11.1-py3-none-any.whl
Algorithm Hash digest
SHA256 bae72cba59a45cd1bc842d8438a7643a3013d3bdfc0192829c536fbb03a3679f
MD5 ec76861b423a5e5b1bcff8199aea61e5
BLAKE2b-256 39841a5e50aa81a84ef740e968913b06c78845d3f57e07d022c99a4cea887faa

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.11.1 This release

2 files

0.11.0

2 files

0.10.1

2 files

0.9.42

2 files

0.9.41

2 files

0.9.40

2 files

0.9.39

2 files

0.9.38

2 files

0.9.20

2 files

0.9.17

2 files

0.9.16

1 file

0.9.15

1 file

0.9.14

2 files

0.9.13

2 files

0.9.12

2 files

0.9.11

0.9.10

2 files

0.9.2

2 files

0.7.7

2 files

0.7.3

2 files

0.6.0

2 files

0.5.0

2 files

0.4.0

2 files

0.3.5

2 files

0.3.4

2 files

0.3.3

2 files

0.3.1

2 files

0.2.1

2 files

0.2

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page