Skip to main content

Quantities in JAX

Project description

unxt

Unitful Quantities in JAX


Unxt is unitful quantities and calculations in JAX, built on Equinox and Quax.

Yes, it supports auto-differentiation (grad, jacobian, hessian) and vectorization (vmap, etc).

Installation

PyPI version PyPI platforms

pip install unxt

Documentation

Documentation Status

Quick example

from unxt import Quantity

x = Quantity(jnp.arange(1, 5, dtype=float), "kpc")
print(x)
# Quantity['length'](Array([1., 2., 3., 4.], dtype=float64), unit='kpc')

# Addition / Subtraction
print(x + x)
# Quantity['length'](Array([2., 4., 6., 8.], dtype=float64), unit='kpc')

# Multiplication / Division
print(2 * x)
# Quantity['length'](Array([2., 4., 6., 8.], dtype=float64), unit='kpc')

y = Quantity(jnp.arange(4, 8, dtype=float), "Gyr")

print(x / y)
# Quantity['speed'](Array([0.25      , 0.4       , 0.5       , 0.57142857], dtype=float64), unit='kpc / Gyr')

# Exponentiation
print(x**2)
# Quantity['area'](Array([0., 1., 4., 9.], dtype=float64), unit='kpc2')

# Unit Checking on operations
try:
    x + y
except Exception as e:
    print(e)
# 'Gyr' (time) and 'kpc' (length) are not convertible

unxt is built on quax, which enables custom array-ish objects in JAX. For convenience we use the quaxed library, which is just a quax.quaxify wrapper around jax to avoid boilerplate code.

from quaxed import grad, vmap
import quaxed.numpy as jnp

print(jnp.square(x))
# Quantity['area'](Array([ 1.,  4.,  9., 16.], dtype=float64), unit='kpc2')

print(qnp.power(x, 3))
# Quantity['volume'](Array([ 1.,  8., 27., 64.], dtype=float64), unit='kpc3')

print(vmap(grad(lambda x: x**3))(x))
# Quantity['area'](Array([ 3., 12., 27., 48.], dtype=float64), unit='kpc2')

Since Quantity is parametric, it can do runtime dimension checking!

LengthQuantity = Quantity["length"]
print(LengthQuantity(2, "km"))
# Quantity['length'](Array(2, dtype=int64, weak_type=True), unit='km')

try:
    LengthQuantity(2, "s")
except ValueError as e:
    print(e)
# Physical type mismatch.

Citation

DOI

If you found this library to be useful and want to support the development and maintenance of lower-level code libraries for the scientific community, please consider citing this work.

Development

codecov Actions Status

We welcome contributions!

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

unxt-0.20.3.tar.gz (658.1 kB view details)

Uploaded Source

Built Distribution

unxt-0.20.3-py3-none-any.whl (55.4 kB view details)

Uploaded Python 3

File details

Details for the file unxt-0.20.3.tar.gz.

File metadata

  • Download URL: unxt-0.20.3.tar.gz
  • Upload date:
  • Size: 658.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for unxt-0.20.3.tar.gz
Algorithm Hash digest
SHA256 e6625ee0022853ee2e1404d6a35c7570444028af3560853029ce3c05d6d284c6
MD5 3920732880f0512ddc344753dedd7dc1
BLAKE2b-256 95b3cadb7bc4fb3b89e3601c4c978043a98d4506ab80ef82951eeee551e8e698

See more details on using hashes here.

Provenance

The following attestation bundles were made for unxt-0.20.3.tar.gz:

Publisher: cd.yml on GalacticDynamics/unxt

Attestations:

File details

Details for the file unxt-0.20.3-py3-none-any.whl.

File metadata

  • Download URL: unxt-0.20.3-py3-none-any.whl
  • Upload date:
  • Size: 55.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for unxt-0.20.3-py3-none-any.whl
Algorithm Hash digest
SHA256 173801b97c4caff2498c48752f95e02467842a820e96996961e7dc885a410826
MD5 b6b41cb24e2759810ab92d6f764fb16d
BLAKE2b-256 188a8cd55dafc8e3fe13ecd3b11082578ea699abeae27ae40c3d01ad1bd178bc

See more details on using hashes here.

Provenance

The following attestation bundles were made for unxt-0.20.3-py3-none-any.whl:

Publisher: cd.yml on GalacticDynamics/unxt

Attestations:

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