Lightweight forward-mode automatic differentiation for NumPy
Project description
dualpy
Lightweight forward-mode automatic differentiation for NumPy.
Dualpy lets you compute exact derivatives of ordinary NumPy code. It hooks
into NumPy's own dispatch protocols (__array_ufunc__ and
__array_function__) to propagate dual numbers alongside the primal
computation, so your existing functions work unchanged. NumPy is the only
dependency — no compilation step, no new array API to learn, and nothing
heavy to install.
Dualpy ships derivative rules for a broad set of NumPy operations — elementary
arithmetic, trigonometry, exponentials, linear algebra (including matrix
decompositions like SVD, QR, Cholesky, and eigendecomposition), reductions,
shape manipulation, and numerical routines — covering the needs of scientific
research, engineering, and education. Unregistered operations raise
NotImplementedError, more operations will be added in future releases,
but for the most part, most are there.
Installation
pip install dualpy
or with uv:
uv add dualpy
Quickstart
Gradient
import numpy as np
from dualpy import gradient
def rosenbrock(x):
return (1 - x[0])**2 + 100 * (x[1] - x[0]**2)**2
grad = gradient(rosenbrock)
grad(np.array([1.0, 1.0])) # array([0., 0.]) — at the minimum
Jacobian
from dualpy import jacobian
def f(x):
return np.stack([x[0]**2, x[0] * x[1]])
jacobian(f)(np.array([2.0, 3.0]))
# array([[4., 0.],
# [3., 2.]])
Hessian
from dualpy import hessian
def quadratic(x):
return x[0]**2 + 3 * x[1]**2
hessian(quadratic)(np.array([1.0, 1.0]))
# array([[2., 0.],
# [0., 6.]])
Low-level JVP
from dualpy import jvp
f = lambda x: np.sin(x) * np.exp(x)
primal, tangent = jvp(f, np.array(1.0), np.array(1.0))
# tangent is df/dx at x=1
API
| Function | Signature | Description |
|---|---|---|
jvp |
jvp(func, primals, tangents) |
Jacobian-vector product |
jacobian |
jacobian(func, argnums=0) |
Full Jacobian matrix |
derivative |
derivative(func, argnums=0) |
Scalar-to-scalar derivative |
nth_derivative |
nth_derivative(func, n) |
n-th order derivative via nesting |
gradient |
gradient(func, argnums=0) |
Gradient of a scalar-valued function |
hessian |
hessian(func, argnums=0) |
Hessian via forward-over-forward |
hvp |
hvp(func, v) |
Hessian-vector product in O(n) |
curl |
curl(func) |
Curl of R^3 -> R^3 |
divergence |
divergence(func) |
Divergence of R^n -> R^n |
laplacian |
laplacian(func) |
Laplacian of a scalar field |
Features
- Complex derivatives. Complex-valued arrays are supported and produce correct derivatives for holomorphic functions. Support for Wirtinger derivatives (non-holomorphic functions) is planned for a future release.
- Higher-order derivatives. Exact n-th order derivatives via nested forward-mode (forward-over-forward).
- Multi-argument differentiation. Differentiate with respect to any
positional argument (or several) using
argnums.
Limitations
- Forward-mode only. Most efficient when the number of inputs is small relative to the number of outputs. For high-dimensional inputs with scalar output (e.g. neural network loss), reverse-mode (backpropagation) is faster: consider JAX or PyTorch for that use case.
- Non-holomorphic complex functions. For non-holomorphic operations
(e.g.
np.abs,np.conjon complex inputs), the tangent propagation does not produce Wirtinger derivatives. This is planned for a future release. - Registered operations only. If your code calls a NumPy function that
dualpy hasn't registered, you'll get
NotImplementedError. The set of registered operations covers common scientific computing needs, and it will be expanded further. - No GPU support. Dualpy operates on CPU NumPy arrays.
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file dualpy-0.1.0.tar.gz.
File metadata
- Download URL: dualpy-0.1.0.tar.gz
- Upload date:
- Size: 79.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8bb00e6c7455f35ee634209321486ef52d93d0d512ea8f0ddc4fc1c22af6d4e1
|
|
| MD5 |
9fec728e3a7cbe274fa12279645bb87e
|
|
| BLAKE2b-256 |
67a4e81557fda0dea3ab4e9060249dbcdf5c38b2017050ca339cdc8ccbb4a145
|
Provenance
The following attestation bundles were made for dualpy-0.1.0.tar.gz:
Publisher:
publish.yml on hmackk/dualpy
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
dualpy-0.1.0.tar.gz -
Subject digest:
8bb00e6c7455f35ee634209321486ef52d93d0d512ea8f0ddc4fc1c22af6d4e1 - Sigstore transparency entry: 1210508774
- Sigstore integration time:
-
Permalink:
hmackk/dualpy@be2ee45e7fd9dca78ad5686eb0c162f682c680d9 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/hmackk
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@be2ee45e7fd9dca78ad5686eb0c162f682c680d9 -
Trigger Event:
push
-
Statement type:
File details
Details for the file dualpy-0.1.0-py3-none-any.whl.
File metadata
- Download URL: dualpy-0.1.0-py3-none-any.whl
- Upload date:
- Size: 28.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4dbf54f1418ae0675a09b5dbaea861bc89eb0040cc2b7e7cb73f288b02e15c15
|
|
| MD5 |
d446355966b015edb0949d20d70b708b
|
|
| BLAKE2b-256 |
1cd911c449c8d17946a448fadaf38efccdf5ad61ee35b8cd7507a3481298180c
|
Provenance
The following attestation bundles were made for dualpy-0.1.0-py3-none-any.whl:
Publisher:
publish.yml on hmackk/dualpy
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
dualpy-0.1.0-py3-none-any.whl -
Subject digest:
4dbf54f1418ae0675a09b5dbaea861bc89eb0040cc2b7e7cb73f288b02e15c15 - Sigstore transparency entry: 1210508878
- Sigstore integration time:
-
Permalink:
hmackk/dualpy@be2ee45e7fd9dca78ad5686eb0c162f682c680d9 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/hmackk
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@be2ee45e7fd9dca78ad5686eb0c162f682c680d9 -
Trigger Event:
push
-
Statement type: