Skip to main content

Fast forward-mode automatic differentiation via dual numbers (C extension)

Project description

fastdual

Fast forward-mode automatic differentiation via dual numbers, implemented as a CPython C extension.

Computes exact gradients and Jacobians with minimal overhead — no taping, no graph construction, just numbers that carry their derivatives.

Install

pip install fastdual

For development:

pip install -e .[test]

Drop-in Gradients

Any function that works with floats works with Dual — no rewriting, no framework, no JIT warmup:

# your existing code
def my_function(x):
    return x**3 - 2*x + 1

# just swap float for Dual
from fastdual import Dual, der

x = Dual(3.0)
y = my_function(x)      # same function, unchanged
dy_dx = der(y, x)       # 25.0 (exact derivative, for free)

Two lines changed. Exact gradient at 1.3x the cost of a plain float.

Quick Start

from fastdual import Dual, der
import numpy as np

# Create independent variables
x = Dual(3.0)
y = Dual(5.0)

# Compute — gradients propagate automatically
z = x * y + np.sin(x)
print(z.val)        # 15.1411...
print(der(z, x))    # 5.99 (dz/dx = y + cos(x))
print(der(z, y))    # 3.0  (dz/dy = x)

Array Operations

from fastdual import seed_array, val, jac
import numpy as np

# Create array of independent seeds
xs = seed_array([1.0, 2.0, 3.0])

# NumPy ufuncs work directly — dispatched to C, not per-element Python
result = np.sin(xs) + xs ** 2

# Extract values and Jacobian
print(val(result))   # [sin(1)+1, sin(2)+4, sin(3)+9]
print(jac(result, xs))
# [[cos(1)+2,       0,       0],
#  [      0, cos(2)+4,       0],
#  [      0,       0, cos(3)+6]]

Automatic Jacobians

from fastdual import autojac
import numpy as np

@autojac
def f(x, y):
    return np.array([x**2 + y, x * y**2])

result, J = f(2.0, 3.0)
# result = [7.0, 18.0]
# J = [[4.0,  1.0],
#      [9.0, 12.0]]

Supported Operations

Arithmetic: +, -, *, /, **, abs, unary -/+

Transcendentals (25): sin, cos, tan, exp, log, log2, log10, sqrt, arcsin, arccos, arctan, sinh, cosh, tanh, arcsinh, arccosh, arctanh, exp2, log1p, expm1, square, cbrt, sign, conjugate, negative

NumPy integration: All of the above work as np.sin(dual), np.exp(dual), etc. — both on scalars and arrays. Array operations are batch-dispatched to C via DualArray, avoiding per-element Python overhead.

Comparisons: <, <=, ==, !=, >, >= (compare primal values)

API Reference

Function Description
Dual(value) Create an independent variable (seed)
Dual(value, seed=False) Create a constant (no gradient)
der(result, wrt) Partial derivative of result w.r.t. a seed
seed_array(values) Array of independent seeds from floats
val(array) Extract primal values from Dual array
jac(results, seeds) Full Jacobian matrix
autojac(fn) Decorator: fn(*floats) -> (values, jacobian)
reset() Reset variable ID counter

Performance

All hot paths are in C. Overhead vs plain floats:

Operation Overhead
Scalar arithmetic 1.3-1.5x
Transcendentals 1.2-1.7x
np.sin(arr[10]) ~4x
np.sin(arr[100]) ~8x
Jacobian (10x10) vs finite diff 10x faster
Jacobian (20x20) vs finite diff 10x faster

Test

pytest tests/ -v

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

fastdual-0.2.0.tar.gz (48.3 kB view details)

Uploaded Source

Built Distributions

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

fastdual-0.2.0-cp313-cp313-win_amd64.whl (45.6 kB view details)

Uploaded CPython 3.13Windows x86-64

fastdual-0.2.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (114.2 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

fastdual-0.2.0-cp313-cp313-macosx_11_0_arm64.whl (42.9 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

fastdual-0.2.0-cp313-cp313-macosx_10_13_x86_64.whl (43.2 kB view details)

Uploaded CPython 3.13macOS 10.13+ x86-64

fastdual-0.2.0-cp312-cp312-win_amd64.whl (45.6 kB view details)

Uploaded CPython 3.12Windows x86-64

fastdual-0.2.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (114.3 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

fastdual-0.2.0-cp312-cp312-macosx_11_0_arm64.whl (42.9 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

fastdual-0.2.0-cp312-cp312-macosx_10_13_x86_64.whl (43.2 kB view details)

Uploaded CPython 3.12macOS 10.13+ x86-64

fastdual-0.2.0-cp311-cp311-win_amd64.whl (45.4 kB view details)

Uploaded CPython 3.11Windows x86-64

fastdual-0.2.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (111.9 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

fastdual-0.2.0-cp311-cp311-macosx_11_0_arm64.whl (42.8 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

fastdual-0.2.0-cp311-cp311-macosx_10_9_x86_64.whl (43.0 kB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

fastdual-0.2.0-cp310-cp310-win_amd64.whl (45.4 kB view details)

Uploaded CPython 3.10Windows x86-64

fastdual-0.2.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (109.0 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

fastdual-0.2.0-cp310-cp310-macosx_11_0_arm64.whl (42.8 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

fastdual-0.2.0-cp310-cp310-macosx_10_9_x86_64.whl (43.0 kB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

File details

Details for the file fastdual-0.2.0.tar.gz.

File metadata

  • Download URL: fastdual-0.2.0.tar.gz
  • Upload date:
  • Size: 48.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for fastdual-0.2.0.tar.gz
Algorithm Hash digest
SHA256 483e1f8090d7778dcf2565fa6903e044e09a27bf0a7ef84556af65d8f6b79c4a
MD5 50288021ec79b51da58de13fc598aa3c
BLAKE2b-256 fe8a7cecf70b796b4f468ff5512f64e301e807b29b828a172178b9e2b39d3b4f

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastdual-0.2.0.tar.gz:

Publisher: publish.yml on milanofthe/fastdual

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fastdual-0.2.0-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: fastdual-0.2.0-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 45.6 kB
  • Tags: CPython 3.13, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for fastdual-0.2.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 b0918547e838572fa6c2ac1b68755a536b086faf274d942b45d69be7e784bc89
MD5 4ad1aba2bedd60eebcc06da14bf1fc95
BLAKE2b-256 d2c0cb4b30a068dd6abce052ffd3059966b4e4902ad5e45d3b9f83d0170dbb8a

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastdual-0.2.0-cp313-cp313-win_amd64.whl:

Publisher: publish.yml on milanofthe/fastdual

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fastdual-0.2.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for fastdual-0.2.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 752c5c5e02e8238cdb7cc534e075fa99a62281e4f8520297758e91c74d69ac4e
MD5 ca3b38b3725be81b665174fc44c47635
BLAKE2b-256 7f2d275af609cdb51f1661eeffe003e804f5cc601b40d9ada623e7dff2148b84

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastdual-0.2.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: publish.yml on milanofthe/fastdual

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fastdual-0.2.0-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for fastdual-0.2.0-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a99c2193e8ae730933ea8f25f84924b5daaaea65c2cf9cf129e4f3e507337639
MD5 8c28e9ad077088a778d8d0e54651b55f
BLAKE2b-256 a7df8161779349e6836aee36196bf053a307517c5e144dab04c7c069e4d6f23f

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastdual-0.2.0-cp313-cp313-macosx_11_0_arm64.whl:

Publisher: publish.yml on milanofthe/fastdual

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fastdual-0.2.0-cp313-cp313-macosx_10_13_x86_64.whl.

File metadata

File hashes

Hashes for fastdual-0.2.0-cp313-cp313-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 d6a0115ef7cf47eddadf3eebed8dc4650f67ab62efa5dd8b03088b39c76a6e71
MD5 50f355e6e852ad8af6d64671bd78b0ee
BLAKE2b-256 93fddb308ea67c3503295e22328383d371539e138427fe2c232e18972870f922

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastdual-0.2.0-cp313-cp313-macosx_10_13_x86_64.whl:

Publisher: publish.yml on milanofthe/fastdual

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fastdual-0.2.0-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: fastdual-0.2.0-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 45.6 kB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for fastdual-0.2.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 0771cb88510b9eab0c21d40981c7c6552076d82559ba008bea327fd8b436999f
MD5 7e4dabf2cc1cb87eda2b98a68265128a
BLAKE2b-256 0661594739eff8ce894e63990d5a4c6fd5854f97860cafa5a24cffd8dcfb2262

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastdual-0.2.0-cp312-cp312-win_amd64.whl:

Publisher: publish.yml on milanofthe/fastdual

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fastdual-0.2.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for fastdual-0.2.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 6dd8de96326d1c8f301df737c10578e31e0448846642d7e523174415c84dda39
MD5 ddcc2f43c85b5623bab038537e8995ef
BLAKE2b-256 ba41bd6b8ad4346dc17ff3528a9b8601fcab9fc53631325737e75479e9372f2c

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastdual-0.2.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: publish.yml on milanofthe/fastdual

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fastdual-0.2.0-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for fastdual-0.2.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 4965a87e5f2445f01ca687f43efad03f6fc1d7fd833ca8fc9d729a7078c4e42d
MD5 ec39a7754f6b587469931725d710469b
BLAKE2b-256 95e1ef580997ce93a091c90b7b814961633ed74d0eb5b31c3cca8c3c28e22644

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastdual-0.2.0-cp312-cp312-macosx_11_0_arm64.whl:

Publisher: publish.yml on milanofthe/fastdual

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fastdual-0.2.0-cp312-cp312-macosx_10_13_x86_64.whl.

File metadata

File hashes

Hashes for fastdual-0.2.0-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 13a31722ff2e7c5cfe0a55ed8616d16578410acd3330f1e2123bf4d0c23b3e67
MD5 1debe665f4fdcd65b571fff6eaa5b262
BLAKE2b-256 759bea06faad6829bdba90383fc3576c898aa16e91bf7360babeb44088f16c8d

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastdual-0.2.0-cp312-cp312-macosx_10_13_x86_64.whl:

Publisher: publish.yml on milanofthe/fastdual

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fastdual-0.2.0-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: fastdual-0.2.0-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 45.4 kB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for fastdual-0.2.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 ce4c2c3347446cd98072be153e075c5538d4ad1c79c29a0966a7381b752b29d8
MD5 b9cfb4f8a9abcb0212ec0b0560f3eb07
BLAKE2b-256 5b7c26fc3e2b1c924c7eeacc9a4347807b4f7b5585d11e94f9ee9748a4b87459

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastdual-0.2.0-cp311-cp311-win_amd64.whl:

Publisher: publish.yml on milanofthe/fastdual

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fastdual-0.2.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for fastdual-0.2.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b5462eab595f51ce145d551f7fe4d0a04c16fd462a0043743683e50970d42f3e
MD5 e2a07599195faaf95342be881699d55e
BLAKE2b-256 ef3e6357f3dccdc69e2cb4498701d1e9e2153bf721bb766925c86882f48c0db6

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastdual-0.2.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: publish.yml on milanofthe/fastdual

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fastdual-0.2.0-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for fastdual-0.2.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 60bac1746087654628ab0ea41117354aea2ecfcd3fdd4b4bafbd85f349ee9d42
MD5 957a0dbb314712ded5c8f8893c4fc5bd
BLAKE2b-256 cddbfa56fd99b879c2f2b20a10ea284bcf94a509389141597681c5e3297e9835

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastdual-0.2.0-cp311-cp311-macosx_11_0_arm64.whl:

Publisher: publish.yml on milanofthe/fastdual

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fastdual-0.2.0-cp311-cp311-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for fastdual-0.2.0-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 4dcfa31ce76a56bb23b5c237dd9874f024596e9d0a437c01badd5095ba6fc596
MD5 80c51a5fad5183948cff3697d8672877
BLAKE2b-256 f79b68f9328599eae2f0c428f14959822294856a833e402258757f7c2cfba5de

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastdual-0.2.0-cp311-cp311-macosx_10_9_x86_64.whl:

Publisher: publish.yml on milanofthe/fastdual

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fastdual-0.2.0-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: fastdual-0.2.0-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 45.4 kB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for fastdual-0.2.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 5fd28a8894e08f3fcdff6ff415d493074021fcb7e7bd67d7e57f0bd9604892fa
MD5 5f74a3ecfd82c70a26b63c2a434403b2
BLAKE2b-256 91362bb8dfe932d8a727043763a07e2871094f29555e16e2decd38040cd0e57f

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastdual-0.2.0-cp310-cp310-win_amd64.whl:

Publisher: publish.yml on milanofthe/fastdual

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fastdual-0.2.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for fastdual-0.2.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 94ad5e3795798c64fbc180147ebf702a5c2e832807880320b83c89da80606c00
MD5 cb637135116ed6e86d80a61047fbcf0d
BLAKE2b-256 999f22eb4408570670f7c980ff9ffa7c012c02b288f7ac2db48406d05dad3119

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastdual-0.2.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: publish.yml on milanofthe/fastdual

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fastdual-0.2.0-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for fastdual-0.2.0-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 79abbaf82b0ebc7b6c374c18f057e0b2d7a241421431bbfee692918ce886ffdd
MD5 17dbf952721255eb0c5029657e7b9006
BLAKE2b-256 1b21531f068b65698b53b08ca3dd5ba0ba7c74d131b3bebbf83e940bd72ea00c

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastdual-0.2.0-cp310-cp310-macosx_11_0_arm64.whl:

Publisher: publish.yml on milanofthe/fastdual

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fastdual-0.2.0-cp310-cp310-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for fastdual-0.2.0-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 ef0dfd4145991ee123e0f7b1c623e108a67f4ae1ef276e1e135eb8f85ca066bc
MD5 bef4182f45868c7868f445e1e59e71ae
BLAKE2b-256 4e3dc9ea50bfb56655d080124807a81630feb780de33bc3d585fdeb3ba6cdcdc

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastdual-0.2.0-cp310-cp310-macosx_10_9_x86_64.whl:

Publisher: publish.yml on milanofthe/fastdual

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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