unxts.parametric#
unxts.parametric provides ParametricQuantity (alias PQ): a quantity that encodes its physical dimension in its type. It is the opt-in counterpart to the lightweight, non-parametric default unxt.Quantity.
ParametricQuantity used to be the default Quantity in unxt v1. As of v2 the non-parametric class is the default and the parametric class lives here, in its own package. See the migration guide for the full mapping.
Install#
uv add unxts.parametric
pip install unxts.parametric
Throughout these pages we import unxt as u and unxts.parametric as up (so ParametricQuantity is up.PQ):
>>> import unxt as u
>>> import unxts.parametric as up
At a glance#
ParametricQuantity is used just like Quantity, but it encodes the physical dimension in its type โ and can check it at construction:
>>> up.PQ(1.0, "m") # dimension inferred from the unit
ParametricQuantity(Array(1., dtype=float32, ...), unit='m')
>>> up.PQ["length"](1.0, "m") # dimension checked against the unit
ParametricQuantity(Array(1., dtype=float32, ...), unit='m')
Should you use it?#
Reach for ParametricQuantity only when you need one of its two extra features:
Runtime dimension checking โ
up.PQ["length"](1, "s")raises; the defaultu.Q["length"](1, "s")accepts the subscript for compatibility but does not check it.Dispatch on specific dimensions โ
up.PQ["length"]is a real type usable inplumdispatch annotations;u.Q["length"]is justQuantity.
Everything else โ arithmetic, unit conversion, JAX transforms, interop โ works identically with either class. The cost of the parametric class, and why the non-parametric one became the default, is set out in the core docs under Why Quantity is not parametric; the comparison table against StaticQuantity is in the sharp bits.
Pages#
Tutorial
Let the type system catch a unit mistake โ start here: watch a wrong unit get rejected at construction, then write a function that dispatches on physical dimension.
Reference
ParametricQuantityโ construction, runtime dimension checking, dimension-specific dispatch, promotion with the defaultQuantity, anddimension_ofon a parametrized class.Configuration โ the
include_paramsdisplay option.
How-to
How to check dimensions at runtime โ dimension annotations enforced by
jaxtyping.
Discussion
The parametric sharp bits โ pytree-type proliferation and
StaticValueequality.
Public API#
unxts.parametric exposes:
ParametricQuantityโ the dimension-parametrized quantity (aliasPQ).AbstractParametricQuantityโ its abstract base.configโ theunxts.parametric.configsingleton (see Configuration).
Importing unxts.parametric also registers, as import side effects, the promotion rules, plum conversions, and JAX primitive rules that let ParametricQuantity interoperate with the rest of unxt.