Skip to main content

JAX + Units

Project description

JAX + Units

Built with JAX and Pint!

This module provides an interface between JAX and Pint to allow JAX to support operations with units. The propagation of units happens at trace time, so jitted functions should see no runtime cost. This library is experimental so expect some sharp edges.

For example:

>>> import jax
>>> import jax.numpy as jnp
>>> import jpu
>>>
>>> u = jpu.UnitRegistry()
>>>
>>> @jax.jit
... def add_two_lengths(a, b):
...     return a + b
...
>>> add_two_lengths(3 * u.m, jnp.array([4.5, 1.2, 3.9]) * u.cm)
<Quantity([3.045 3.012 3.039], 'meter')>

Installation

To install, use pip:

python -m pip install jpu

The only dependencies are jax and pint, and these will also be installed, if not already in your environment. Take a look at the JAX docs for more information about installing JAX on different systems.

Usage

Here is a slightly more complete example:

>>> import jax
>>> import numpy as np
>>> from jpu import UnitRegistry, numpy as jnpu
>>>
>>> u = UnitRegistry()
>>>
>>> @jax.jit
... def projectile_motion(v_init, theta, time, g=u.standard_gravity):
...     """Compute the motion of a projectile with support for units"""
...     x = v_init * time * jnpu.cos(theta)
...     y = v_init * time * jnpu.sin(theta) - 0.5 * g * jnpu.square(time)
...     return x.to(u.m), y.to(u.m)
...
>>> x, y = projectile_motion(
...     5.0 * u.km / u.h, 60 * u.deg, np.linspace(0, 1, 50) * u.s
... )

Technical details & limitations

The most significant limitation of this library is the fact that users must use jpu.numpy functions when interacting with "quantities" with units instead of the jax.numpy interface. This is because JAX does not (yet?) provide a general interface for dispatching of ufuncs on custom array classes. I have played around with the undocumented __jax_array__ interface, but it's not really flexible enough, and it isn't currently compatible with Pytree objects.

So far, only a subset of the numpy/jax.numpy interface is implemented. Pull requests adding broader support (including submodules) would be welcome!

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

jpu-0.0.5rc1.tar.gz (19.0 kB view details)

Uploaded Source

Built Distribution

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

jpu-0.0.5rc1-py3-none-any.whl (16.0 kB view details)

Uploaded Python 3

File details

Details for the file jpu-0.0.5rc1.tar.gz.

File metadata

  • Download URL: jpu-0.0.5rc1.tar.gz
  • Upload date:
  • Size: 19.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for jpu-0.0.5rc1.tar.gz
Algorithm Hash digest
SHA256 bdf74d032a9c68ea404e5465567b2af703ee2cad6f30ca9ff79e19c061dd2e26
MD5 6cab7f30b2a48516ff5ea7671be3ba8c
BLAKE2b-256 560590aaaa7d04a7226cb6c1394737dba837a3aec511c8faf663952cf6a3c0e1

See more details on using hashes here.

Provenance

The following attestation bundles were made for jpu-0.0.5rc1.tar.gz:

Publisher: tests.yml on dfm/jpu

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

File details

Details for the file jpu-0.0.5rc1-py3-none-any.whl.

File metadata

  • Download URL: jpu-0.0.5rc1-py3-none-any.whl
  • Upload date:
  • Size: 16.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for jpu-0.0.5rc1-py3-none-any.whl
Algorithm Hash digest
SHA256 82babc5410463a998d0784a59eb7b1d620fa6a37b4843ac805fbdbdf5bb1740c
MD5 461f66b942042e8a12d138aa3c6260dc
BLAKE2b-256 10c30814f4abfbe02807490ad90ecb1363779dd43b531aa964927bf92df4a5bc

See more details on using hashes here.

Provenance

The following attestation bundles were made for jpu-0.0.5rc1-py3-none-any.whl:

Publisher: tests.yml on dfm/jpu

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