unxts.linalg

unxts.linalg#

unxts.linalg provides QuantityMatrix (alias QM): a quantity container whose elements may each carry a different unit. It is backed by a single JAX array plus a static UnitsMatrix describing the per-element units, and supports both 1-D (heterogeneous vector) and 2-D (heterogeneous matrix) structures.

It is useful for objects whose entries have mixed physical dimensions โ€” Jacobians, metric tensors, and coordinate change-of-basis matrices โ€” where a single scalar unit (as on unxt.Quantity) is not expressive enough.

Install#

uv add unxts.linalg
pip install unxts.linalg

Throughout these guides we import unxt as u and unxts.linalg as ul (so QuantityMatrix is ul.QM):

>>> import jax.numpy as jnp
>>> import unxt as u
>>> import unxts.linalg as ul

At a glance#

A 1-D QuantityMatrix is a vector whose entries each have their own unit:

>>> qv = ul.QuantityMatrix(jnp.array([1.0, 2.0, 3.0]), unit=("m", "s", "kg"))
>>> qv.unit.to_string()
'(m, s, kg)'
>>> 2 * qv
QuantityMatrix(Array([2., 4., 6.], dtype=float32), unit='(m, s, kg)')

Indexing a single element yields an ordinary unxt.Quantity:

>>> qv[0]
Quantity(Array(1., dtype=float32), unit='m')

Guides#

Public API#

unxts.linalg exposes:

  • QuantityMatrix โ€” the heterogeneous-unit matrix/vector (alias QM).

  • UnitsMatrix โ€” the immutable, hashable per-element unit structure.

  • matmul, matvec, vecmat, vecdot โ€” the four Array-API matrix/vector products, each resolving the batched matrix-vs-vector ambiguity that @ alone cannot.

  • det, inv โ€” unit-tracking determinant and inverse (with their JAX primitives det_p, inv_p).

  • cdict_units โ€” extract per-key units from a component dictionary.

Importing unxts.linalg also registers, as import side effects, the Quax primitive rules (add/sub, mul/div, dot-general/matmul, transpose, gather/diag, reduce-sum) and the plum conversions/dispatch that let QuantityMatrix interoperate with the rest of unxt.