Skip to main content

Compute numerical derivatives.

Project description

https://img.shields.io/pypi/v/jacobi https://img.shields.io/badge/github-docs-success https://img.shields.io/badge/github-source-blue

Fast numerical derivatives for real analytic functions with arbitrary round-off error.

Features

  • Robustly compute the generalised Jacobi matrix for an arbitrary real analytic mapping ℝⁿ → ℝⁱ¹ × … × ℝⁱⁿ

  • Derivative is either computed to specified accuracy (to save computing time) or until maximum precision of function is reached

  • Algorithm based on John D’Errico’s DERIVEST: works even with functions that have large round-off error

  • Up to 1000x faster than numdifftools at equivalent precision

  • Returns error estimates for derivatives

  • Supports arbitrary auxiliary function arguments

  • Perform statistical error propagation based on numerically computed jacobian

  • Lightweight package, only depends on numpy

Planned features

  • Compute the Hessian matrix numerically with the same algorithm

  • Further generalize the calculation to support function arguments with shape (N, K), in that case compute the Jacobi matrix for each of the K vectors of length N

Examples

from matplotlib import pyplot as plt
import numpy as np
from jacobi import jacobi


def f(x):
    return np.sin(x) / x

x = np.linspace(-10, 10, 1000)

fx = f(x)

# f(x) is a simple vectorized function, jacobian is diagonal
fdx, fdxe = jacobi(f, x, diagonal=True)
# fdxe is uncertainty estimate for derivative

plt.plot(x, fx, label="f(x) = sin(x) / x")
plt.plot(x, fdx, ls="--", label="f'(x)")
plt.legend()
https://hdembinski.github.io/jacobi/_images/example.svg
from jacobi import propagate
import numpy as np
from scipy.special import gamma


# arbitrarily complex function that calls compiled libraries, numba-jitted code, etc.
def fn(x):
    r = np.empty(3)
    r[0] = 1.5 * np.exp(-x[0] ** 2)
    r[1] = gamma(x[1] ** 3.1)
    r[2] = np.polyval([1, 2, 3], x[0])
    return r  # x and r have different lengths

# fn accepts a parameter vector x, which has an associated covariance matrix xcov
x = [1.0, 2.0]
xcov = [[1.1, 0.1], [0.1, 2.3]]
y, ycov = propagate(fn, x, xcov)  # y=f(x) and ycov = J xcov J^T

Comparison to numdifftools

Speed

Jacobi makes better use of vectorized computation than numdifftools and converges rapidly if the derivative is trivial. This leads to a dramatic speedup in some cases.

Smaller run-time is better (and ratio > 1).

https://hdembinski.github.io/jacobi/_images/speed.svg

Precision

The machine precision is indicated by the dashed line.

https://hdembinski.github.io/jacobi/_images/precision.svg

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

jacobi-0.4.0.tar.gz (440.0 kB view details)

Uploaded Source

Built Distribution

jacobi-0.4.0-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

Details for the file jacobi-0.4.0.tar.gz.

File metadata

  • Download URL: jacobi-0.4.0.tar.gz
  • Upload date:
  • Size: 440.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.14

File hashes

Hashes for jacobi-0.4.0.tar.gz
Algorithm Hash digest
SHA256 a319b6dc9798308ba5dffb937d09ce1cc829264aa4892d588f791bfc98a78b75
MD5 cfe3f22fee9492e4f3c2e33010512d03
BLAKE2b-256 c2fab3f294ced3ed326facd4762e07c710d058289992bbed428d51eb9cf1128b

See more details on using hashes here.

File details

Details for the file jacobi-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: jacobi-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 8.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.14

File hashes

Hashes for jacobi-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c6e4c58fb058303d8079d7b58f0649c151734b0ca56d4e7791442aed48ecd1aa
MD5 835c56e2e44f9d3498404e4aa58d5e05
BLAKE2b-256 e880a88385f1f7f5cd63a081d58e83d50822b602e67cd12db4c8fc7728c35d56

See more details on using hashes here.

Supported by

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