Skip to main content

Unit-aware tensors for physics and scientific machine learning

Project description

dimtensor

Unit-aware tensors for physics and scientific machine learning

PyPI version Python versions License: MIT Downloads Documentation

Documentation | PyPI | Changelog | Contributing


dimtensor catches dimensional errors at operation time, not after hours of computation.

from dimtensor import DimArray, units

# Operations check dimensions automatically
velocity = DimArray([10, 20, 30], units.m / units.s)
time = DimArray([1, 2, 3], units.s)
distance = velocity * time  # [10 40 90] m

# Errors caught immediately
acceleration = DimArray([9.8], units.m / units.s**2)
velocity + acceleration  # DimensionError: cannot add m/s to m/s^2

Why dimtensor?

Problem Solution
Silent unit errors waste compute time Immediate DimensionError at operation time
PyTorch has no unit support Native DimTensor with full autograd and GPU support
JAX incompatible with unit libraries DimArray registered as pytree for jit/vmap/grad
Uncertainty handled separately Built-in uncertainty propagation through all operations
Units lost during I/O Save/load with units to JSON, HDF5, Parquet, NetCDF

Features

  • Dimensional Safety - Operations between incompatible dimensions raise DimensionError
  • Unit Conversion - Convert between compatible units with .to()
  • NumPy/PyTorch/JAX - Full integration with all three frameworks
  • Physical Constants - CODATA 2022 constants with proper units and uncertainties
  • Uncertainty Propagation - Track and propagate measurement uncertainties
  • I/O Support - JSON, HDF5, Parquet, NetCDF, pandas, xarray
  • Visualization - Matplotlib and Plotly with automatic unit labels
  • Domain Units - Astronomy, chemistry, and engineering units

Installation

pip install dimtensor

For framework-specific support:

pip install dimtensor[torch]  # PyTorch integration
pip install dimtensor[jax]    # JAX integration
pip install dimtensor[all]    # All optional dependencies

Quick Start

NumPy

from dimtensor import DimArray, units

v = DimArray([10], units.m / units.s)  # velocity
t = DimArray([5], units.s)              # time
d = v * t                               # distance = 50 m

PyTorch

import torch
from dimtensor.torch import DimTensor
from dimtensor import units

# Unit-aware tensors with autograd
v = DimTensor(torch.tensor([1.0, 2.0, 3.0], requires_grad=True), units.m / units.s)
t = DimTensor(torch.tensor([0.5, 1.0, 1.5]), units.s)
d = v * t  # distance in meters

# Gradients flow through
d.sum().backward()
print(v.grad)

# GPU support
v_cuda = v.cuda()

JAX

import jax
import jax.numpy as jnp
from dimtensor.jax import DimArray
from dimtensor import units

@jax.jit
def kinetic_energy(mass, velocity):
    return 0.5 * mass * velocity**2

m = DimArray(jnp.array([1.0, 2.0]), units.kg)
v = DimArray(jnp.array([10.0, 20.0]), units.m / units.s)
E = kinetic_energy(m, v)  # JIT-compiled, units preserved: [50. 400.] J

Physical Constants

from dimtensor import constants, DimArray, units

print(constants.c)   # Speed of light: 299792458.0 m/s
print(constants.h)   # Planck constant with uncertainty

E = constants.c**2 * DimArray([1.0], units.kg)  # E = mc^2

Uncertainty Propagation

from dimtensor import DimArray, units

length = DimArray([10.0], units.m, uncertainty=[0.1])
width = DimArray([5.0], units.m, uncertainty=[0.05])

area = length * width  # 50 +/- 0.71 m^2 (propagated in quadrature)

I/O

from dimtensor import DimArray, units
from dimtensor.io import save_json, load_json, save_hdf5, load_hdf5

arr = DimArray([1.0, 2.0, 3.0], units.m)

# JSON
save_json(arr, "data.json")
loaded = load_json("data.json")  # Units preserved

# HDF5
save_hdf5(arr, "data.h5", compression="gzip")
loaded = load_hdf5("data.h5")

Visualization

from dimtensor import DimArray, units
from dimtensor.visualization import plot

time = DimArray([0, 1, 2, 3], units.s)
distance = DimArray([0, 10, 40, 90], units.m)

plot(time, distance)  # Axes labeled automatically: [s], [m]

Domain-Specific Units

from dimtensor import DimArray
from dimtensor.domains.astronomy import parsec, light_year, solar_mass
from dimtensor.domains.chemistry import molar, dalton
from dimtensor.domains.engineering import MPa, hp, kWh

# Astronomy
distance = DimArray([4.24], light_year).to(parsec)  # ~1.3 pc

# Chemistry
concentration = DimArray([0.1], molar)  # 0.1 M

# Engineering
stress = DimArray([250], MPa)
power = DimArray([100], hp)

Useful Links

Call for Contributions

dimtensor is an open source project and welcomes contributions of all kinds. Here are ways to get involved:

  • Report bugs - Open an issue
  • Request features - Share ideas in discussions
  • Contribute code - See our contributing guide
  • Improve docs - Fix typos, add examples, clarify explanations
  • Share use cases - Write tutorials or blog posts

Writing code isn't the only way to contribute. Good issues, documentation improvements, and community engagement are just as valuable.

License

MIT

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

dimtensor-1.4.0.tar.gz (51.7 kB view details)

Uploaded Source

Built Distribution

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

dimtensor-1.4.0-py3-none-any.whl (70.5 kB view details)

Uploaded Python 3

File details

Details for the file dimtensor-1.4.0.tar.gz.

File metadata

  • Download URL: dimtensor-1.4.0.tar.gz
  • Upload date:
  • Size: 51.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.11

File hashes

Hashes for dimtensor-1.4.0.tar.gz
Algorithm Hash digest
SHA256 dd891346e4d1e66d1d30a81bb86e366b30785c4cf51a237b5fb75f9421895f1c
MD5 56867f5ea6e28c7dc02fd841753771f4
BLAKE2b-256 b53e54376bcae8d3be4c26cf8cbb24edf42061361d30b37b1ea014c0c0f0d015

See more details on using hashes here.

File details

Details for the file dimtensor-1.4.0-py3-none-any.whl.

File metadata

  • Download URL: dimtensor-1.4.0-py3-none-any.whl
  • Upload date:
  • Size: 70.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.11

File hashes

Hashes for dimtensor-1.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bec3ee6de4e6e6274a5516bc1888f52005a7e7945217fa5c0ebedddaf73e1f99
MD5 0b95c90b73af1ae2e98e6ec35626e054
BLAKE2b-256 4e235deb4b68cc78cdf9618cf834ccbfa5c59c34fa083beb5b3f9e2fdf8811de

See more details on using hashes here.

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