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.1.0.tar.gz (27.7 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.1.0-cp313-cp313-win_amd64.whl (36.9 kB view details)

Uploaded CPython 3.13Windows x86-64

fastdual-0.1.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (105.8 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

fastdual-0.1.0-cp313-cp313-macosx_11_0_arm64.whl (34.5 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

fastdual-0.1.0-cp313-cp313-macosx_10_13_x86_64.whl (34.7 kB view details)

Uploaded CPython 3.13macOS 10.13+ x86-64

fastdual-0.1.0-cp312-cp312-win_amd64.whl (36.9 kB view details)

Uploaded CPython 3.12Windows x86-64

fastdual-0.1.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (105.8 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

fastdual-0.1.0-cp312-cp312-macosx_11_0_arm64.whl (34.5 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

fastdual-0.1.0-cp312-cp312-macosx_10_13_x86_64.whl (34.7 kB view details)

Uploaded CPython 3.12macOS 10.13+ x86-64

fastdual-0.1.0-cp311-cp311-win_amd64.whl (36.7 kB view details)

Uploaded CPython 3.11Windows x86-64

fastdual-0.1.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (103.4 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

fastdual-0.1.0-cp311-cp311-macosx_11_0_arm64.whl (34.3 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

fastdual-0.1.0-cp311-cp311-macosx_10_9_x86_64.whl (34.5 kB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

fastdual-0.1.0-cp310-cp310-win_amd64.whl (36.7 kB view details)

Uploaded CPython 3.10Windows x86-64

fastdual-0.1.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (100.5 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

fastdual-0.1.0-cp310-cp310-macosx_11_0_arm64.whl (34.3 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

fastdual-0.1.0-cp310-cp310-macosx_10_9_x86_64.whl (34.5 kB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

File details

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

File metadata

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

File hashes

Hashes for fastdual-0.1.0.tar.gz
Algorithm Hash digest
SHA256 57a546d3267ea94176cc322d4378423f4d1df4b540691392994181e03a7f7db8
MD5 be3cbc067b64cbfd17d6c9e284e3e5cd
BLAKE2b-256 57fca83d0716dc75d5a53eaea77a3f7e0bc6010a1fa934b18b9801c0687e17b8

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastdual-0.1.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.1.0-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: fastdual-0.1.0-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 36.9 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.1.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 ee9c2c8b86438d31dbd6b584b6681c31dff4d201df271daa146ff285f4f2cda3
MD5 490fb4f1f465ba2a2fb7f63eeca80579
BLAKE2b-256 4c0c9082729e25b65d591d8ede40dd89ab7ead0bb60e537bc9d8e43e6d81a2e2

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastdual-0.1.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.1.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for fastdual-0.1.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 57ff1446b6652191d9ae9608318f7ac2627b7f2aa8b969dbb58da4366d63a3a4
MD5 f85219bb14337038dc65580643920220
BLAKE2b-256 91aecf27a9be017b47833811305126f042ae48deb59181be93c21dd1ed52b553

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastdual-0.1.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.1.0-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for fastdual-0.1.0-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a258fa88da733f95bf9fe1db5126683d0e2bde8e942c5700f8a7e511b99d41aa
MD5 c0551d0975876c85fcb65632eeb6b1fb
BLAKE2b-256 70a144a8a0d34f22eed19360ccdc38d77f48744ed72cc0fb69fa670eae874d61

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastdual-0.1.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.1.0-cp313-cp313-macosx_10_13_x86_64.whl.

File metadata

File hashes

Hashes for fastdual-0.1.0-cp313-cp313-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 92af1fd58e10c6e0e7b17862196439e8ed8509bd5d7884158036d368671b013e
MD5 7961ef63264deb427cd83c7c2c718dc5
BLAKE2b-256 4274a5d62cfc5dca32b09286b4d06d212b0352e281d6679ade69623a807e4e49

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastdual-0.1.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.1.0-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: fastdual-0.1.0-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 36.9 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.1.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 e6e3b20c75f0d98b2e0e604b6b0b7bf19b4d536b9ea1062105d81d1693edcd3a
MD5 c1695e4151c4146b29886198f5cb2e85
BLAKE2b-256 43e7e723f47c48a4fdfc1693fa58b96706a15c200ed264a14220ad205a0a31f1

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastdual-0.1.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.1.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for fastdual-0.1.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 9f8f1da286d34ff2878ae7081b3b9c68eb9355d84277b743b17cfe9ca488ac46
MD5 443d0244b6bb5a48ead6bab204c88aa4
BLAKE2b-256 6e811116d6ce5fedba16e221419c891f780042df7feaf90bb52282f2f6992278

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastdual-0.1.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.1.0-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for fastdual-0.1.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 5034c4bb63418dd6b9dad18f3fcb0ffbc702cd7397b358aa7a4a652ed35229a5
MD5 2007588d69cef843dc55fa87369334b1
BLAKE2b-256 91603ff5dc51eac3d2b7812ce027d9027610499434ecbb490bd7bc851e0571b0

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastdual-0.1.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.1.0-cp312-cp312-macosx_10_13_x86_64.whl.

File metadata

File hashes

Hashes for fastdual-0.1.0-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 0a8b6e65dd0991db33100aeb394f9eb42089cf1df2acc6b8366cffdcfb4350a0
MD5 eb93aab4fe2534dd8eec0abc80243e84
BLAKE2b-256 9e562e36f4891351ad73dd5010d5e1ed42b649fb9ebf5d026ed73cc04a99deb4

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastdual-0.1.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.1.0-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: fastdual-0.1.0-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 36.7 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.1.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 d67d6a6f727731632a726bc6763e701286ce458454675b2e99fa49aa85d90274
MD5 9732c66fd6bf2e62243944d6e342781a
BLAKE2b-256 c814b494d7931e7e9a51dd0b37de9bc4e8f6eed820ca0fda4ee86ec7fd6c698e

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastdual-0.1.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.1.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for fastdual-0.1.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 0febb3d0a8225494b37cdb3b6f0c31123dfc9bb8317cb5907113ca3210861ce2
MD5 dc84d90882e43552441d534656d6049a
BLAKE2b-256 40a3caf7878ddb92e4447d6c794cac698bbbb1f520e2c9fd6efb8295a3627fc1

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastdual-0.1.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.1.0-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for fastdual-0.1.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 6519a935e62897823066384afd78e4873a0e935b4fe8de9378ee827724a9cf73
MD5 27c83abe980cf7fcae4d179ece3d741c
BLAKE2b-256 498db07e4eaf6d9733db5c7da4cb7f9b193680b00a928c115442d39ddd3ba6d3

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastdual-0.1.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.1.0-cp311-cp311-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for fastdual-0.1.0-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 67fae1f1dc7fe1b8246eb24929fda0e6cd75e0100b339972a185c79c4c592606
MD5 a06ccf4a21c7bc98539e0e5603381cee
BLAKE2b-256 a17e6e8430c78e607774763658481f5bbdc09a0b526e5e7c7c89326b10c1910c

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastdual-0.1.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.1.0-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: fastdual-0.1.0-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 36.7 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.1.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 ab71ef7f6b67797001b16b0a0db90b28e4038a8a5936c6234cb2166f4e3dd175
MD5 98033777497e3d585fd25746f374333b
BLAKE2b-256 bc64abb7a914faf8b57a843c326be2c10afc84ad628d1ae4e53abd600402a5ec

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastdual-0.1.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.1.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for fastdual-0.1.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 331551e103971eef117dcd118adb1f37183d0d00ab453bc0d2c72b0eaf9e122b
MD5 5614486dda2e5154977f39fe267085f4
BLAKE2b-256 62a8243adf54f2dae17bb1f5014be2957f9f6eda00a9038d72323d53e57a830b

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastdual-0.1.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.1.0-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for fastdual-0.1.0-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 ee0d69d729cf0b2964c10752af77c0702cf4fed916e2258a24dc8b72bd992fc6
MD5 90ffef94437317a417ef6224308a94b7
BLAKE2b-256 48d5b09e0a1e5368b8933ebd0bb1871be6319aea7f38ca308aa81d6ce8ecb504

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastdual-0.1.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.1.0-cp310-cp310-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for fastdual-0.1.0-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 9d9a2517c7a7ca7649e31c9b6eac0e3a7cf855c52ca725c3edf7637a09d2b8d5
MD5 a26f8ffc8c8f5be14c2b6473cbd909bb
BLAKE2b-256 d40936e9f9403fe8bde116991401100f33c70cff5dab3318c09309477552852e

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastdual-0.1.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