Skip to main content

'Solves automatic numerical differentiation problems in one or more variables.'

Project description

Numdifftools

https://badge.fury.io/py/numdifftools.png https://travis-ci.org/pbrod/numdifftools.svg?branch=master https://readthedocs.org/projects/pip/badge/?version=latest Code Health https://coveralls.io/repos/pbrod/numdifftools/badge.svg?branch=master

Numdifftools 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.

  • 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

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

>>> import numpy as np
>>> import numdifftools as nd
>>> 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)
>>> dfun([1,2,3])
array([ 2.,  4.,  6.])

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)
>>> dfun([1,2,3])
array([ 2.,  4.,  6.])

See also

scipy.misc.derivative

Documentation and code

Numdifftools works on Python 2.7+ and Python 3.0+.

Current version is 0.9.13

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

Official documentation: available at: http://numdifftools.readthedocs.org/

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

Installation and upgrade:

with pip $ pip install numdifftools

with easy_install $ easy_install numdifftools for installation $ easy_install upgrade numdifftools to upgrade to the newest version

Unit tests

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

import numdifftools as nd
nd.test(coverage=True, doctests=True)

Project details


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.9.13.zip (225.8 kB view details)

Uploaded Source

Built Distribution

numdifftools-0.9.13-py2.py3-none-any.whl (2.4 MB view details)

Uploaded Python 2Python 3

File details

Details for the file numdifftools-0.9.13.zip.

File metadata

  • Download URL: numdifftools-0.9.13.zip
  • Upload date:
  • Size: 225.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for numdifftools-0.9.13.zip
Algorithm Hash digest
SHA256 1aad0d9eba96846f5c3b82e6a8df0b1eaf4cd13e0b665cba64646756a0f2add6
MD5 86295fdf387780028ffc97eecdc9dce9
BLAKE2b-256 66d8334c7a0adc90d91634f03f85b1230610a74db9e08338d3e34439baf58d85

See more details on using hashes here.

File details

Details for the file numdifftools-0.9.13-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for numdifftools-0.9.13-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 8e61f23b904c5976c7b9ce301ef72ac0cdf1ca253b78c9f20f3f69a6cbadffc0
MD5 539ee070e50d6b7d38a8921c1d89a483
BLAKE2b-256 cfce0fd5723ed538506328d4ca2b716ed296accbcd4c5d24a70988878a631fb2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page