Parametric Quantities#
Constructing a ParametricQuantity and its runtime dimension checking. For the lightweight, non-parametric default, see the unxt Quantity guide.
>>> import unxt as u
>>> import unxts.parametric as up
Construction and runtime dimension checking#
When a ParametricQuantity is constructed it is parametrized by the unitβs dimension. This can be specified explicitly:
>>> up.PQ["length"](1, "m")
ParametricQuantity(Array(1, dtype=int32...), unit='m')
or inferred from the unit:
>>> up.PQ(1, "m")
ParametricQuantity(Array(1, dtype=int32...), unit='m')
When given explicitly, ParametricQuantity checks the input dimensions. Here a length-parametrized class (correctly) refuses a unit of time:
>>> try:
... up.PQ["length"](1, "s")
... except Exception as e:
... print(e)
Physical type mismatch.
That should catch some bugs! By contrast, the default Quantity accepts the subscript as a no-op and does not check:
>>> u.Q["length"](1, "s") # no error; subscript is informational only
Quantity(Array(1, dtype=int32...), unit='s')
Dispatch on specific dimensions#
Filling a ParametricQuantityβs parameter and constructing an instance may be separated β the parametrized class is a real type, usable in plum dispatch annotations:
>>> LengthQuantity = up.PQ["length"]
>>> LengthQuantity
<class 'unxt...ParametricQuantity[PhysicalType('length')]'>
The base class AbstractParametricQuantity and the concrete unxt.Quantity are not parametric β Quantity[<dimension>] does nothing and is informational only. Explore plum for more on parametric classes.