prophys.units

prophys.units#

Physical units and dimensional checking.

Frame and UncertainAttribute carry a unit string. This module turns those labels into checkable quantities, so a metre frame is never silently combined with a kilometre field, and an attribute declared in "kW" is never calibrated against observations in "MW" without conversion.

A Unit is a symbol, a Dimension (the seven SI base exponents), and a scale factor to the SI base. So km is (length=1, scale=1000), kW is (mass=1, length=2, time=-3, scale=1000), and m/s is a compound of the two. parse() builds one from a string, accepting SI prefixes and the usual *, / and ^ composition.

Two rules follow:

  • Compatible units share a dimension and can be converted between (convert()).

  • Incompatible units cannot be combined at all, and the attempt raises UnitError when the model is built rather than producing a number.

Checking happens in Python when the model is built, never inside the JAX trace, so it costs nothing at run time, the same discipline Frame compatibility already follows.

Units the SI does not define (currency, counts, arbitrary index scales) are supported as dimensionless tagged units: two different tags never convert into each other, so EUR and USD stay distinct while both remain dimensionless.

Classes

Dimension

Exponents of the seven SI base dimensions.

Unit

A unit of measurement: a symbol, a dimension, and a scale to SI.

Functions

parse

Parse a unit expression such as "m", "km/h", "kg*m/s^2".

convert

Convert value from unit from_ to unit to.

compatible

Return whether two units share a dimension and a tag, and so convert into each other.

check_compatible

Raise UnitError unless a and b are compatible.

conversion_factor

Return the multiplier taking a value from unit from_ into unit to.

register_unit

Add a unit to the registry under its symbol and any aliases.

Exceptions

UnitError

Raised when two quantities' units are incompatible, or a unit string cannot be parsed.

Constants and objects

DIMENSIONLESS

The dimension of a pure number.