unxt API

unxt API#


unxt: Quantities in JAX.

unxt is a library for working with physical quantities in JAX, supporting JAX’s autodiff and JIT compilation, and easy integration with existing codes. If you’re seeing this then you’re in the main module of the unxt package, where we provide exports for the main functionality of the library. Sub-modules are available for more specialized functionality, such as unit systems and experimental features.

Note that unxt uses multiple-dispatch to provide a flexible and extensible interface. In the docs you’ll see the function signatures without type annotation and then subsections for specific function dispatches based on the type annotations. However dispatches registered from other modules may not be included in the rendered docs. To see all the dispatches execute <func or class>.methods in an interactive Python session. For more information on multiple-dispatch see the [plum](https://beartype.github.io/plum/intro.html) documentation.


unxt.AbstractDimension

alias of PhysicalType

unxt.dimension(obj: Any, /)

Construct the dimension.

Note

This function uses multiple dispatch. Dispatches made in other modules may not be included in the rendered docs. To see the full range of options, execute unxt.dims.dimension.methods in an interactive Python session.

unxt.dimension(obj: PhysicalType, /) PhysicalType
Parameters:

obj (Any)

Return type:

Any

Construct dimension from a dimension object.

Examples

>>> import unxt as u
>>> import astropy.units as apyu
>>> length = apyu.get_physical_type("length")
>>> length
PhysicalType('length')
>>> u.dimension(length) is length
True
unxt.dimension(obj: str, /) PhysicalType
Parameters:

obj (Any)

Return type:

Any

Construct dimension from a string.

The string can be: 1. A simple dimension name (e.g., “length”, “time”, “mass”) 2. A multi-word dimension name (e.g., “amount of substance”, “absement”) 3. A mathematical expression using , /, and * operators

Mathematical Expressions:

Expressions are evaluated using operator precedence (PEMDAS): - ** (exponentiation, highest precedence) - * and / (multiplication and division, equal precedence, left-to-right)

Parentheses are supported for grouping and for dimension names with spaces.

Operators Supported: - * : Multiplication (e.g., “length * time”) - / : Division (e.g., “length / time”) - ** : Exponentiation (e.g., “length**2”)

Unsupported Operators: - + and - are NOT supported as operators since dimensions are invariant

under addition and subtraction. They are treated as part of dimension names.

Rules for Dimension Names in Expressions: - Single-word names don’t need parentheses: “length * time” - Multi-word names MUST be parenthesized: “(amount of substance) * time” - Parenthesized single-word names are allowed: “(length) / (time)” - Whitespace is flexible: “length / time”, “length/time”, “length / time**2”

Examples

>>> from unxt.dims import dimension

Simple dimension names:

>>> dimension("length")
PhysicalType('length')
>>> dimension("time")
PhysicalType('time')
>>> dimension("mass")
PhysicalType('mass')

Multi-word dimension names:

>>> dimension("amount of substance")
PhysicalType('amount of substance')

Mathematical expressions with single-word names:

>>> dimension("length / time")
PhysicalType({'speed', 'velocity'})
>>> dimension("length**2")
PhysicalType('area')
>>> dimension("length * mass / time**2")
PhysicalType('force')

Parenthesized expressions:

>>> dimension("(length) / (time)")
PhysicalType({'speed', 'velocity'})

Expressions with multi-word dimension names:

>>> dimension("(amount of substance) / (time)")
PhysicalType('catalytic activity')

Mixed expressions (multi-word with parentheses, single-word without):

>>> dimension("length * (amount of substance)")
PhysicalType('unknown')
>>> dimension("(absement) / (time)")
PhysicalType('length')

See also

dimension_of

Get the dimension of an object

unxt.units

Unit specifications can also use dimension expressions

Parameters:

obj (Any)

Return type:

Any

unxt.dimension_of(obj: Any, /)

Return the dimension of the given units.

Note

This function uses multiple dispatch. Dispatches made in other modules may not be included in the rendered docs. To see the full range of options, execute unxt.dimension_of.methods in an interactive Python session.

unxt.dimension_of(obj: Any, /) NoneType
Parameters:

obj (Any)

Return type:

Any

Most objects have no dimension.

Examples

>>> from unxt.dims import dimension_of
>>> print(dimension_of(1))
None
>>> print(dimension_of("length"))
None
unxt.dimension_of(obj: PhysicalType, /) PhysicalType
Parameters:

obj (Any)

Return type:

Any

Return the dimension of the given units.

Examples

>>> from unxt.dims import dimension, dimension_of
>>> dimension_of(dimension("length"))
PhysicalType('length')
unxt.dimension_of(obj: type, /) NoReturn
Parameters:

obj (Any)

Return type:

Any

Get the dimension of a type.

Examples

>>> import unxt as u
>>> try:
...     u.dimension_of(u.quantity.Quantity)
... except ValueError as e:
...     print(e)
Cannot get the dimension of <class 'unxt._src.quantity.quantity.Quantity'>.
unxt.dimension_of(obj: UnitBase | FunctionUnitBase, /) PhysicalType
Parameters:

obj (Any)

Return type:

Any

Return the dimensions of the given units.

Examples

>>> import unxt as u
>>> u.dimension_of(u.unit("km"))
PhysicalType('length')
unxt.dimension_of(obj: AbstractQuantity, /) PhysicalType
Parameters:

obj (Any)

Return type:

Any

Return the dimension of a quantity.

Examples

>>> import unxt as u
>>> q = u.Q(1, "m")
>>> u.dimension_of(q)
PhysicalType('length')
unxt.dimension_of(obj: type[AbstractAngle], /) PhysicalType
Parameters:

obj (Any)

Return type:

Any

Get the dimension of an angle class.

Examples

>>> import unxt as u
>>> u.dimension_of(u.Angle)
PhysicalType('angle')
unxt.dimension_of(obj: Quantity, /) PhysicalType
Parameters:

obj (Any)

Return type:

Any

Return the dimension of a quantity.

Examples

>>> import unxt as u
>>> import astropy.units as apyu
>>> q = apyu.Quantity(1, "m")
>>> u.dimension_of(q)
PhysicalType('length')
Parameters:

obj (Any)

Return type:

Any

unxt.unit(obj: Any, /)

Construct the units from a units object.

unxt.unit(obj: UnitBase | FunctionUnitBase, /) UnitBase | FunctionUnitBase
Parameters:

obj (Any)

Return type:

Any

Construct the units from a units object.

Examples

>>> import unxt as u
>>> m = u.unit("m")
>>> u.unit(m) is m
True
unxt.unit(obj: str, /) UnitBase | FunctionUnitBase
Parameters:

obj (Any)

Return type:

Any

Construct units from a string.

Examples

>>> import unxt as u
>>> m = u.unit("m")
>>> m
Unit("m")

Astropy function units (magnitudes, dex, decibels) are also supported:

>>> u.unit("mag(AB)")
Unit("mag(AB)")
>>> u.unit("dex(cm/s2)")
Unit("dex(cm / s2)")
unxt.unit(obj: UnitBase | Unit, /) UnitBase | Unit | FunctionUnitBase | StructuredUnit
Parameters:

obj (Any)

Return type:

Any

Construct the units from an Astropy unit.

Examples

>>> import astropy.units as apyu
>>> import unxt as u
>>> u.unit(apyu.km)
Unit("km")
unxt.unit(obj: Quantity, /) UnitBase | Unit | FunctionUnitBase | StructuredUnit
Parameters:

obj (Any)

Return type:

Any

Construct the units from an Astropy quantity.

Examples

>>> import astropy.units as apyu
>>> import unxt as u
>>> u.unit(apyu.Quantity(2, "km"))
Unit("2 km")
Parameters:

obj (Any)

Return type:

Any

unxt.unit_of(obj: Any, /)

Return the units of an object.

unxt.unit_of(obj: Any, /) NoneType
Parameters:

obj (Any)

Return type:

Any

Return the units of an object.

Examples

>>> import unxt as u
>>> print(u.unit_of(1))
None
unxt.unit_of(obj: UnitBase | FunctionUnitBase, /) UnitBase | FunctionUnitBase
Parameters:

obj (Any)

Return type:

Any

Return the units of an unit.

Examples

>>> import unxt as u
>>> m = u.unit("m")
>>> u.unit_of(m)
Unit("m")
unxt.unit_of(obj: AbstractQuantity, /) UnitBase | FunctionUnitBase
Parameters:

obj (Any)

Return type:

Any

Return the units of an object.

Examples

>>> from unxt import unit_of, Quantity
>>> q = Quantity(1, "m")
>>> unit_of(q)
Unit("m")
unxt.unit_of(obj: UnitBase | Unit, /) UnitBase | Unit | FunctionUnitBase | StructuredUnit
Parameters:

obj (Any)

Return type:

Any

Return the units of an object.

Examples

>>> import astropy.units as apyu
>>> import unxt as u
>>> u.unit_of(apyu.km)
Unit("km")
unxt.unit_of(obj: Quantity, /) UnitBase | Unit | FunctionUnitBase | StructuredUnit
Parameters:

obj (Any)

Return type:

Any

Return the units of an Astropy quantity.

Examples

>>> import astropy.units as apyu
>>> import unxt as u
>>> u.unit_of(apyu.Quantity(1, "km"))
Unit("km")
Parameters:

obj (Any)

Return type:

Any

class unxt.AbstractUnitSystem

Bases: object

Represents a system of units.

This class behaves like a dictionary with keys set by physical types (i.e. “length”, “velocity”, “energy”, etc.). If a unit for a particular physical type is not specified on creation, a composite unit will be created with the base units. See the examples below for some demonstrations.

Examples

If only base units are specified, any physical type specified as a key to this object will be composed out of the base units:

>>> from unxt import unitsystem
>>> usys = unitsystem("m", "s", "kg", "radian")
>>> usys
unitsystem(m, s, kg, rad)
>>> usys["velocity"]
Unit("m / s")

This unit system defines energy:

>>> usys = unitsystem("m", "s", "kg", "radian", "erg")
>>> usys["energy"]
Unit("erg")

This is useful for Galactic dynamics where lengths and times are usually given in terms of kpc and Myr, but velocities are often specified in km/s:

>>> usys = unitsystem("kpc", "Myr", "Msun", "radian", "km / s")
>>> usys["velocity"]
Unit("km / s")

Unit systems can be hashed:

>>> isinstance(hash(usys), int)
True

And iterated over:

>>> [x for x in usys]
[Unit("kpc"), Unit("Myr"), Unit("solMass"), Unit("rad"), Unit("km / s")]

With length equal to the number of base units

>>> len(usys)
5
property base_dimensions: tuple[PhysicalType, ...]

Dimensions required for the unit system.

property base_units: tuple[UnitBase | FunctionUnitBase, ...]

List of core units.

unxt.unitsystem(usys: AbstractUnitSystem, /)

Convert a UnitSystem to a UnitSystem.

Examples

>>> from unxt.unitsystems import unitsystem
>>> usys = unitsystem("kpc", "Myr", "Msun", "radian")
>>> usys
unitsystem(kpc, Myr, solMass, rad)
>>> unitsystem(usys) is usys
True
unxt.unitsystem(seq: Sequence[Any], /) AbstractUnitSystem
Parameters:

usys (AbstractUnitSystem)

Return type:

AbstractUnitSystem

Convert a UnitSystem or tuple of arguments to a UnitSystem.

Examples

>>> import unxt as u
>>> u.unitsystem(())
DimensionlessUnitSystem()
>>> u.unitsystem(("kpc", "Myr", "Msun", "radian"))
unitsystem(kpc, Myr, solMass, rad)
>>> u.unitsystem(["kpc", "Myr", "Msun", "radian"])
unitsystem(kpc, Myr, solMass, rad)
unxt.unitsystem(_: NoneType, /) DimensionlessUnitSystem
Parameters:

usys (AbstractUnitSystem)

Return type:

AbstractUnitSystem

Dimensionless unit system from None.

Examples

>>> from unxt.unitsystems import unitsystem
>>> unitsystem(None)
DimensionlessUnitSystem()
unxt.unitsystem(*args: Any) AbstractUnitSystem
Parameters:

usys (AbstractUnitSystem)

Return type:

AbstractUnitSystem

Convert a set of arguments to a UnitSystem.

Examples

>>> from unxt.unitsystems import unitsystem
>>> unitsystem("kpc", "Myr", "Msun", "radian")
unitsystem(kpc, Myr, solMass, rad)

With no arguments it is the dimensionless system, agreeing with unitsystem(None) and unitsystem([]):

>>> unitsystem()
DimensionlessUnitSystem()
unxt.unitsystem(name: str, /) AbstractUnitSystem
Parameters:

usys (AbstractUnitSystem)

Return type:

AbstractUnitSystem

Return unit system from name.

Examples

>>> from unxt.unitsystems import unitsystem
>>> unitsystem("galactic")
unitsystem(kpc, Myr, solMass, rad)
>>> unitsystem("solarsystem")
unitsystem(AU, yr, solMass, rad)
>>> unitsystem("dimensionless")
DimensionlessUnitSystem()
unxt.unitsystem(usys: AbstractUnitSystem, *args: Any) AbstractUnitSystem
Parameters:

usys (AbstractUnitSystem)

Return type:

AbstractUnitSystem

Create a unit system from an existing unit system and additional units.

Examples

We can add a new unit definition to an existing unit system:

>>> from unxt.unitsystems import unitsystem
>>> usys = unitsystem("galactic")
>>> unitsystem(usys, "km/s")
LengthTimeMassAngleSpeedUnitSystem(length=Unit("kpc"), time=Unit("Myr"), mass=Unit("solMass"), angle=Unit("rad"), speed=Unit("km / s"))

We can also override the base unit of an existing unit system:

>>> new_usys = unitsystem(usys, "pc")
>>> new_usys
TimeMassAngleLengthUnitSystem(time=Unit("Myr"), mass=Unit("solMass"), angle=Unit("rad"), length=Unit("pc"))
unxt.unitsystem(flag: type[AbstractUSysFlag], *_: Any) AbstractUnitSystem
Parameters:

usys (AbstractUnitSystem)

Return type:

AbstractUnitSystem

Raise an exception since the flag is abstract.

unxt.unitsystem(flag: type[StandardUSysFlag], *args: Any) AbstractUnitSystem
Parameters:

usys (AbstractUnitSystem)

Return type:

AbstractUnitSystem

Create a standard unit system using the inputted units.

Examples

>>> from unxt import unitsystem, unitsystems
>>> unitsystem(unitsystems.StandardUSysFlag, "kpc", "Myr", "Msun")
LengthTimeMassUnitSystem(length=Unit("kpc"), time=Unit("Myr"), mass=Unit("solMass"))
unxt.unitsystem(flag: type[DynamicalSimUSysFlag], *args: Any, G: float | int = 1.0) AbstractUnitSystem
Parameters:

usys (AbstractUnitSystem)

Return type:

AbstractUnitSystem

Make a dynamical unit system.

Examples

>>> from unxt.unitsystems import unitsystem, DynamicalSimUSysFlag
>>> unitsystem(DynamicalSimUSysFlag, "m", "kg")
LengthMassTimeUnitSystem(length=Unit("m"), mass=Unit("kg"), time=Unit("122404 s"))
unxt.unitsystem(value: gala.units.UnitSystem, /) AbstractUnitSystem
Parameters:

usys (AbstractUnitSystem)

Return type:

AbstractUnitSystem

Return a gala.units.UnitSystem as a unxt.AbstractUnitSystem.

Examples

>>> import unxt as u
>>> import gala.units as gu
>>> import astropy.units as apyu
>>> usys = gu.UnitSystem(apyu.km, apyu.s, apyu.Msun, apyu.radian)
>>> u.unitsystem(usys)
unitsystem(km, s, solMass, rad)
unxt.unitsystem(_: gala.units.DimensionlessUnitSystem, /) DimensionlessUnitSystem
Parameters:

usys (AbstractUnitSystem)

Return type:

AbstractUnitSystem

Return a gala.units.DimensionlessUnitSystem as a unxt.DimensionlessUnitSystem.

Examples

>>> import unxt as u
>>> from gala.units import DimensionlessUnitSystem
>>> usys = DimensionlessUnitSystem()
>>> u.unitsystem(usys)
DimensionlessUnitSystem()
Parameters:

usys (AbstractUnitSystem)

Return type:

AbstractUnitSystem

unxt.unitsystem_of(obj: Any, /)

Return the unit system of an object.

unxt.unitsystem_of(obj: Any, /) DimensionlessUnitSystem
Parameters:

obj (Any)

Return type:

Any

Return the unit system of the object.

Examples

>>> from unxt.unitsystems import unitsystem_of
>>> unitsystem_of(1)
DimensionlessUnitSystem()
unxt.unitsystem_of(obj: AbstractUnitSystem, /) AbstractUnitSystem
Parameters:

obj (Any)

Return type:

Any

Return the unit system from the unit system.

Examples

>>> from unxt.unitsystems import galactic, unitsystem_of
>>> unitsystem_of(galactic) is galactic
True
Parameters:

obj (Any)

Return type:

Any

class unxt.AbstractQuantity

Bases: AstropyQuantityCompatMixin, NumPyCompatMixin, IPythonReprMixin, ArrayValue, NumpyBinaryOpsMixin[Any, AbstractQuantity], NumpyComparisonMixin[Any, Bool[Array, '*shape']], NumpyUnaryMixin[AbstractQuantity], NumpyRoundMixin[AbstractQuantity], NumpyTruncMixin[AbstractQuantity], NumpyFloorMixin[AbstractQuantity], NumpyCeilMixin[AbstractQuantity], LaxLenMixin, LaxLengthHintMixin

Represents a quantity with a value and a unit.

short_name

Optional short name for the class used in wadler-lindig printing when use_short_name=True. Defaults to None.

Type:

str | None

Examples

>>> import unxt as u

From an integer:

>>> u.Q(1, "m")
Quantity(Array(1, dtype=int32...), unit='m')

From a float:

>>> u.Q(1.0, "m")
Quantity(Array(1., dtype=float32...), unit='m')

From a list:

>>> u.Q([1, 2, 3], "m")
Quantity(Array([1, 2, 3], dtype=int32), unit='m')

From a tuple:

>>> u.Q((1, 2, 3), "m")
Quantity(Array([1, 2, 3], dtype=int32), unit='m')

From a numpy.ndarray:

>>> import numpy as np
>>> u.Q(np.array([1, 2, 3]), "m")
Quantity(Array([1, 2, 3], dtype=int32), unit='m')

From a jax.Array:

>>> import jax.numpy as jnp
>>> u.Q(jnp.array([1, 2, 3]), "m")
Quantity(Array([1, 2, 3], dtype=int32), unit='m')

The unit can also be given as a astropy.units.Unit:

>>> import astropy.units as apyu
>>> u.Q(1, apyu.m)
Quantity(Array(1, dtype=int32...), unit='m')
value: AbstractVar[Shaped[Array, '*shape'] | Shaped[StaticValue, '*shape']]

The value of the AbstractQuantity.

unit: AbstractVar[UnitBase | FunctionUnitBase]

The unit associated with this value.

classmethod from_(cls: type[AbstractQuantity], *args: Any, **kwargs: Any)
from_(cls: type[AbstractQuantity], value: ArrayLike | list[Shaped[Array, ''] | Shaped[ndarray, ''] | bool | number | bool | int | float | complex] | tuple[Shaped[Array, ''] | Shaped[ndarray, ''] | bool | number | bool | int | float | complex, ...], unit: Any, /, *, dtype: Any = None) AbstractQuantity
Parameters:
Return type:

AbstractQuantity

Construct a unxt.AbstractQuantity from an array-like value and a unit.

Parameters:
  • value – The array-like value.

  • unit – The unit of the value.

  • dtype – The data type of the array (keyword-only).

  • args (Any)

  • kwargs (Any)

Return type:

AbstractQuantity

Examples

For this example we’ll use the Quantity class. The same applies to any subclass of AbstractQuantity.

>>> import jax.numpy as jnp
>>> import unxt as u
>>> x = jnp.array([1.0, 2, 3])
>>> u.Q.from_(x, "m")
Quantity(Array([1., 2., 3.], dtype=float32), unit='m')
>>> u.Q.from_([1.0, 2, 3], "m")
Quantity(Array([1., 2., 3.], dtype=float32), unit='m')
>>> u.Q.from_((1.0, 2, 3), "m")
Quantity(Array([1., 2., 3.], dtype=float32), unit='m')
from_(cls: type[AbstractQuantity], value: ArrayLike | list[Shaped[Array, ''] | Shaped[ndarray, ''] | bool | number | bool | int | float | complex] | tuple[Shaped[Array, ''] | Shaped[ndarray, ''] | bool | number | bool | int | float | complex, ...], /, *, unit: Any, dtype: Any = None) AbstractQuantity
Parameters:
Return type:

AbstractQuantity

Make a unxt.AbstractQuantity from an array-like value and a unit kwarg.

Examples

For this example we’ll use the unxt.Quantity class. The same applies to any subclass of unxt.AbstractQuantity.

>>> import unxt as u
>>> u.Q.from_([1.0, 2, 3], unit="m")
Quantity(Array([1., 2., 3.], dtype=float32), unit='m')
from_(cls: type[AbstractQuantity], *, value: Any, unit: Any, dtype: Any = None) AbstractQuantity
Parameters:
Return type:

AbstractQuantity

Construct a AbstractQuantity from value and unit kwargs.

Examples

For this example we’ll use the Quantity class. The same applies to any subclass of AbstractQuantity.

>>> import unxt as u
>>> u.Q.from_(value=[1.0, 2, 3], unit="m")
Quantity(Array([1., 2., 3.], dtype=float32), unit='m')
from_(cls: type[AbstractQuantity], mapping: Mapping[str, Any]) AbstractQuantity
Parameters:
Return type:

AbstractQuantity

Construct a quantity from a Mapping.

Examples

For this example we’ll use the Quantity class. The same applies to any subclass of AbstractQuantity.

>>> import jax.numpy as jnp
>>> import unxt as u
>>> x = jnp.array([1.0, 2, 3])
>>> q = u.Q.from_({"value": x, "unit": "m"})
>>> q
Quantity(Array([1., 2., 3.], dtype=float32), unit='m')
>>> u.Q.from_({"value": q, "unit": "km"})
Quantity(Array([0.001, 0.002, 0.003], dtype=float32), unit='km')
from_(cls: type[AbstractQuantity], value: AbstractQuantity, unit: Any, /, *, dtype: Any = None) AbstractQuantity
Parameters:
Return type:

AbstractQuantity

Construct a quantity from another quantity.

The value is converted to the new unit.

Examples

>>> import unxt as u
>>> q = u.Q(1, "m")
>>> u.Q.from_(q, "cm")
Quantity(Array(100., dtype=float32, ...), unit='cm')
from_(cls: type[AbstractQuantity], value: AbstractQuantity, unit: NoneType, /, *, dtype: Any = None) AbstractQuantity
Parameters:
Return type:

AbstractQuantity

Construct a quantity from another quantity.

The value is converted to the new unit.

Examples

>>> import unxt as u
>>> q = u.Q(1, "m")
>>> u.Q.from_(q, None)
Quantity(Array(1, dtype=int32...), unit='m')
from_(cls: type[AbstractQuantity], value: AbstractQuantity, /, *, unit: Any | None = None, dtype: Any = None) AbstractQuantity
Parameters:
Return type:

AbstractQuantity

Construct a quantity from another quantity.

The unit is unchanged.

from_(cls: type[StaticQuantity], value: ArrayLike | list[Shaped[Array, ''] | Shaped[ndarray, ''] | bool | number | bool | int | float | complex] | tuple[Shaped[Array, ''] | Shaped[ndarray, ''] | bool | number | bool | int | float | complex, ...], unit: Any, /, *, dtype: Any = None) StaticQuantity
Parameters:
Return type:

AbstractQuantity

Construct a StaticQuantity, keeping the value on NumPy dtypes.

The generic AbstractQuantity.from_ routes the value through jnp.asarray, which applies JAX’s x64-disabled dtype rules and silently downcasts int64 / float64 to int32 / float32. A StaticQuantity stores its value verbatim, so hand the value straight to __init__ and let the StaticValue.from_ converter convert it – that preserves the NumPy dtype. Delegating (rather than calling np.asarray here) also keeps .from_ and __init__ under the same policy for JAX inputs, instead of materialising an array the constructor would reject. (The keyword-unit overload delegates here, so it is covered too.)

Examples

>>> import numpy as np
>>> import unxt as u
>>> u.StaticQuantity.from_(
...     np.array([1, 2, 3], dtype=np.int64), "m"
... ).value.array.dtype
dtype('int64')

.from_ applies the same JAX-input policy as __init__ – it delegates rather than converting first, so the two cannot drift:

>>> import jax.numpy as jnp
>>> u.StaticQuantity.from_(jnp.array([1.0, 2.0]), "m")
StaticQuantity(array([1., 2.], dtype=float32), unit='m')
from_(cls: type[AbstractQuantity], value: Quantity, /, **kwargs: Any) AbstractQuantity
Parameters:
Return type:

AbstractQuantity

Construct a quantity from an astropy Quantity.

The value is converted to the new unit.

Examples

>>> import unxt as u
>>> import astropy.units as apyu
>>> u.Q.from_(apyu.Quantity(1, "m"))
Quantity(Array(1., dtype=float32), unit='m')
from_(cls: type[AbstractQuantity], value: Quantity, u: Any, /, **kwargs: Any) AbstractQuantity
Parameters:
Return type:

AbstractQuantity

Construct a quantity from an astropy Quantity, converting to a target unit.

The value is converted to the new unit.

Examples

>>> import unxt as u
>>> import astropy.units as apyu
>>> u.Q.from_(apyu.Quantity(1, "m"), "cm")
Quantity(Array(100., dtype=float32), unit='cm')
Parameters:
Return type:

AbstractQuantity

uconvert(u: Any, /)

Convert the quantity to the given units.

See also

None

convert a quantity to a new unit.

Examples

>>> import unxt as u
>>> q = u.Q(1, "m")
>>> q.uconvert("cm")
Quantity(Array(100., dtype=float32, ...), unit='cm')
Parameters:

u (Any)

Return type:

AbstractQuantity

ustrip(u: Any, /)

Return the value in the given units.

See also

None

strip the units from a quantity.

Examples

>>> import unxt as u
>>> q = u.Q(1, "m")
>>> q.ustrip("cm")
Array(100., dtype=float32, weak_type=True)
Parameters:

u (Any)

Return type:

Array

property shape: tuple[int, ...]

Shape of the array.

property dtype: dtype

Data type of the array.

Examples

>>> import unxt as u
>>> u.Q(1, "m").dtype
dtype('int32')
property device: Device

Device where the array is located.

Examples

>>> import unxt as u
>>> u.Q(1, "m").device
CpuDevice(id=0)
property mT: AbstractQuantity

Matrix transpose of the array.

Examples

>>> import unxt as u
>>> q = u.Q([[0, 1], [1, 2]], "m")
>>> q.mT
Quantity(Array([[0, 1],
                          [1, 2]], dtype=int32), unit='m')
property ndim: int

Number of dimensions.

Examples

>>> import unxt as u
>>> q = u.Q([[1]], "m")
>>> q.ndim
2
property size: int

Total number of elements.

Examples

>>> import unxt as u
>>> q = u.Q([1, 2, 3], "m")
>>> q.size
3
property T: AbstractQuantity

Transpose of the array.

Examples

>>> import unxt as u
>>> q = u.Q([[0, 1], [1, 2]], "m")
>>> q.T
Quantity(Array([[0, 1],
                          [1, 2]], dtype=int32), unit='m')
to_device(device: None | Device = None)

Move the array to a new device.

Examples

>>> import unxt as u
>>> q = u.Q(1, "m")
>>> q.to_device(None)
Quantity(Array(1, dtype=int32...), unit='m')
Parameters:

device (None | Device)

Return type:

AbstractQuantity

argmax(*args: Any, **kwargs: Any)

Return the indices of the maximum value.

Examples

>>> import unxt as u
>>> q = u.Q([1, 2, 3], "m")
>>> q.argmax()
Array(2, dtype=int32)
Parameters:
Return type:

Array

argmin(*args: Any, **kwargs: Any)

Return the indices of the minimum value.

Examples

>>> import unxt as u
>>> q = u.Q([1, 2, 3], "m")
>>> q.argmin()
Array(0, dtype=int32)
Parameters:
Return type:

Array

astype(*args: Any, **kwargs: Any)

Copy the array and cast to a specified dtype.

Examples

>>> import unxt as u
>>> q = u.Q([1, 2, 3], "m")
>>> q.dtype
dtype('int32')
>>> q.astype(float)
Quantity(Array([1., 2., 3.], dtype=float32), unit='m')
Parameters:
Return type:

AbstractQuantity

property at: _QuantityIndexUpdateHelper

Helper property for index update functionality.

The at property provides a functionally pure equivalent of in-place array modifications.

In particular:

Alternate syntax

Equivalent In-place expression

x = x.at[idx].set(y)

x[idx] = y

x = x.at[idx].add(y)

x[idx] += y

x = x.at[idx].subtract(y)

x[idx] -= y

x = x.at[idx].multiply(y)

x[idx] *= y

x = x.at[idx].divide(y)

x[idx] /= y

x = x.at[idx].power(y)

x[idx] **= y

x = x.at[idx].min(y)

x[idx] = minimum(x[idx], y)

x = x.at[idx].max(y)

x[idx] = maximum(x[idx], y)

x = x.at[idx].apply(ufunc)

ufunc.at(x, idx)

x = x.at[idx].get()

x = x[idx]

None of the x.at expressions modify the original x; instead they return a modified copy of x. However, inside a jit() compiled function, expressions like x = x.at[idx].set(y) are guaranteed to be applied in-place.

Unlike NumPy in-place operations such as x[idx] += y, if multiple indices refer to the same location, all updates will be applied (NumPy would only apply the last update, rather than applying all updates.) The order in which conflicting updates are applied is implementation-defined and may be nondeterministic (e.g., due to concurrency on some hardware platforms).

By default, JAX assumes that all indices are in-bounds. Alternative out-of-bound index semantics can be specified via the mode parameter (see below).

Parameters:
  • mode –

    string specifying out-of-bound indexing mode. Options are:

    • "promise_in_bounds": (default) The user promises that indices are in bounds. No additional checking will be performed. In practice, this means that out-of-bounds indices in get() will be clipped, and out-of-bounds indices in set(), add(), etc. will be dropped.

    • "clip": clamp out of bounds indices into valid range.

    • "drop": ignore out-of-bound indices.

    • "fill": alias for "drop". For get(), the optional fill_value argument specifies the value that will be returned.

    See jax.lax.GatherScatterMode for more details.

  • wrap_negative_indices – If True (default) then negative indices indicate position from the end of the array, similar to Python and NumPy indexing. If False, then negative indices are considered out-of-bounds and behave according to the mode parameter.

  • fill_value – Only applies to the get() method: the fill value to return for out-of-bounds slices when mode is 'fill'. Ignored otherwise. Defaults to NaN for inexact types, the largest negative value for signed types, the largest positive value for unsigned types, and True for booleans.

  • indices_are_sorted – If True, the implementation will assume that the (normalized) indices passed to at[] are sorted in ascending order, which can lead to more efficient execution on some backends. If True but the indices are not actually sorted, the output is undefined.

  • unique_indices – If True, the implementation will assume that the (normalized) indices passed to at[] are unique, which can result in more efficient execution on some backends. If True but the indices are not actually unique, the output is undefined.

Examples

>>> x = jnp.arange(5.0)
>>> x
Array([0., 1., 2., 3., 4.], dtype=float32)
>>> x.at[2].get()
Array(2., dtype=float32)
>>> x.at[2].add(10)
Array([ 0.,  1., 12.,  3.,  4.], dtype=float32)

By default, out-of-bound indices are ignored in updates, but this behavior can be controlled with the mode parameter:

>>> x.at[10].add(10)  # dropped
Array([0., 1., 2., 3., 4.], dtype=float32)
>>> x.at[20].add(10, mode='clip')  # clipped
Array([ 0.,  1.,  2.,  3., 14.], dtype=float32)

For get(), out-of-bound indices are clipped by default:

>>> x.at[20].get()  # out-of-bounds indices clipped
Array(4., dtype=float32)
>>> x.at[20].get(mode='fill')  # out-of-bounds indices filled with NaN
Array(nan, dtype=float32)
>>> x.at[20].get(mode='fill', fill_value=-1)  # custom fill value
Array(-1., dtype=float32)

Negative indices count from the end of the array, but this behavior can be disabled by setting wrap_negative_indices = False:

>>> x.at[-1].set(99)
Array([ 0.,  1.,  2.,  3., 99.], dtype=float32)
>>> x.at[-1].set(99, wrap_negative_indices=False, mode='drop')  # dropped!
Array([0., 1., 2., 3., 4.], dtype=float32)
block_until_ready()

Block until the array is ready.

Return type:

AbstractQuantity

Examples

>>> import unxt as u
>>> q = u.Q(1, "m")
>>> q.block_until_ready() is q
True
devices()

Return the devices where the array is located.

Return type:

set[Device]

Examples

>>> import unxt as u
>>> q = u.Q(1, "m")
>>> q.devices()
{CpuDevice(id=0)}
flatten()

Return a flattened version of the array.

Return type:

AbstractQuantity

Examples

>>> import unxt as u
>>> q = u.Q([[1, 2], [3, 4]], "m")
>>> q.flatten()
Quantity(Array([1, 2, 3, 4], dtype=int32), unit='m')
max(*args: Any, **kwargs: Any)

Return the maximum value.

Examples

>>> import unxt as u
>>> q = u.Q([1, 2, 3], "m")
>>> q.max()
Quantity(Array(3, dtype=int32), unit='m')
Parameters:
Return type:

AbstractQuantity

mean(*args: Any, **kwargs: Any)

Return the mean value.

Examples

>>> import unxt as u
>>> q = u.Q([1, 2, 3], "m")
>>> q.mean()
Quantity(Array(2., dtype=float32), unit='m')
Parameters:
Return type:

AbstractQuantity

min(*args: Any, **kwargs: Any)

Return the minimum value.

Examples

>>> import unxt as u
>>> q = u.Q([1, 2, 3], "m")
>>> q.min()
Quantity(Array(1, dtype=int32), unit='m')
Parameters:
Return type:

AbstractQuantity

ravel()

Return a flattened version of the array.

Return type:

AbstractQuantity

Examples

>>> import unxt as u
>>> q = u.Q([[1, 2], [3, 4]], "m")
>>> q.ravel()
Quantity(Array([1, 2, 3, 4], dtype=int32), unit='m')
reshape(*args: Any, order: str = 'C')

Return a reshaped version of the array.

Examples

>>> import unxt as u
>>> q = u.Q([1, 2, 3, 4], "m")
>>> q.reshape(2, 2)
Quantity(Array([[1, 2],
                          [3, 4]], dtype=int32), unit='m')
Parameters:
Return type:

AbstractQuantity

round(*args: Any, **kwargs: Any)

Round the array to the given number of decimals.

Examples

>>> import unxt as u
>>> q = u.Q([1.1, 2.2, 3.3], "m")
>>> q.round(0)
Quantity(Array([1., 2., 3.], dtype=float32), unit='m')
Parameters:
Return type:

AbstractQuantity

property sharding: Any

Return the sharding configuration of the array.

Examples

>>> import unxt as u
>>> q = u.Q([1, 2, 3], "m")
>>> q.sharding
SingleDeviceSharding(device=..., memory_kind=...)
squeeze(*args: Any, **kwargs: Any)

Return the array with all single-dimensional entries removed.

Examples

>>> import unxt as u
>>> q = u.Q([[[1], [2], [3]]], "m")
>>> q.squeeze()
Quantity(Array([1, 2, 3], dtype=int32), unit='m')
Parameters:
Return type:

AbstractQuantity

is_equivalent(other: AbstractQuantity, /)

Whether self and other are physically equal (unit-aware).

The method form of unxt.equivalent; unlike == (which is unit-blind for StaticValue-backed quantities) this accounts for unit conversion.

Examples

>>> import unxt as u
>>> u.Q(1000.0, "m").is_equivalent(u.Q(1.0, "km"))
Quantity(Array(True, dtype=bool...), unit='')
Parameters:

other (AbstractQuantity)

Return type:

Any

decompose(bases: Sequence[UnitBase | FunctionUnitBase | str], /)

Decompose the quantity into the given bases.

Examples

>>> from unxt import Quantity
>>> q = Quantity(1, "m")
>>> q.decompose(["cm", "s"])
Quantity(Array(100., dtype=float32, ...), unit='cm')
Parameters:

bases (Sequence[UnitBase | FunctionUnitBase | str])

Return type:

AbstractQuantity

to(u: Any, /)

Convert the quantity to the given units.

See unxt.quantity.AbstractQuantity.uconvert.

Examples

>>> from unxt import Quantity
>>> q = Quantity(1, "m")
>>> q.to("cm")
Quantity(Array(100., dtype=float32, ...), unit='cm')
Parameters:

u (Any)

Return type:

AbstractQuantity

to_value(u: Any, /)

Return the value in the given units.

See unxt.AbstractQuantity.ustrip.

Examples

>>> from unxt import Quantity
>>> q = Quantity(1, "m")
>>> q.to_value("cm")
Array(100., dtype=float32, weak_type=True)
Parameters:

u (Any)

Return type:

Union[Array, ndarray, bool, number, bool, int, float, complex]

final class unxt.Angle(value: Any, unit: Any)

Bases: AbstractAngle

Angular quantity.

Examples

>>> import unxt as u

Create an Angle:

>>> q = u.Angle(1, "rad")
>>> q
Angle(Array(1, dtype=int32...), unit='rad')

Wrap an Angle to a range:

>>> q = u.Angle(370, "deg")
>>> q.wrap_to(u.Q(0, "deg"), u.Q(360, "deg"))
Angle(Array(10, dtype=int32...), unit='deg')

Create an Angle array:

>>> q = u.Angle([1, 2, 3], "deg")
>>> q
Angle(Array([1, 2, 3], dtype=int32), unit='deg')

Do math on an Angle:

>>> 2 * q
Angle(Array([2, 4, 6], dtype=int32), unit='deg')
>>> q % u.Q(4, "deg")
Angle(Array([1, 2, 3], dtype=int32), unit='deg')
Parameters:
value: Real[Array, '*shape'] | Real[StaticValue, '*shape']

The value of the unxt.AbstractQuantity.

property T: AbstractQuantity

Transpose of the array.

Examples

>>> import unxt as u
>>> q = u.Q([[0, 1], [1, 2]], "m")
>>> q.T
Quantity(Array([[0, 1],
                          [1, 2]], dtype=int32), unit='m')
argmax(*args: Any, **kwargs: Any)

Return the indices of the maximum value.

Examples

>>> import unxt as u
>>> q = u.Q([1, 2, 3], "m")
>>> q.argmax()
Array(2, dtype=int32)
Parameters:
Return type:

Array

argmin(*args: Any, **kwargs: Any)

Return the indices of the minimum value.

Examples

>>> import unxt as u
>>> q = u.Q([1, 2, 3], "m")
>>> q.argmin()
Array(0, dtype=int32)
Parameters:
Return type:

Array

astype(*args: Any, **kwargs: Any)

Copy the array and cast to a specified dtype.

Examples

>>> import unxt as u
>>> q = u.Q([1, 2, 3], "m")
>>> q.dtype
dtype('int32')
>>> q.astype(float)
Quantity(Array([1., 2., 3.], dtype=float32), unit='m')
Parameters:
Return type:

AbstractQuantity

property at: _QuantityIndexUpdateHelper

Helper property for index update functionality.

The at property provides a functionally pure equivalent of in-place array modifications.

In particular:

Alternate syntax

Equivalent In-place expression

x = x.at[idx].set(y)

x[idx] = y

x = x.at[idx].add(y)

x[idx] += y

x = x.at[idx].subtract(y)

x[idx] -= y

x = x.at[idx].multiply(y)

x[idx] *= y

x = x.at[idx].divide(y)

x[idx] /= y

x = x.at[idx].power(y)

x[idx] **= y

x = x.at[idx].min(y)

x[idx] = minimum(x[idx], y)

x = x.at[idx].max(y)

x[idx] = maximum(x[idx], y)

x = x.at[idx].apply(ufunc)

ufunc.at(x, idx)

x = x.at[idx].get()

x = x[idx]

None of the x.at expressions modify the original x; instead they return a modified copy of x. However, inside a jit() compiled function, expressions like x = x.at[idx].set(y) are guaranteed to be applied in-place.

Unlike NumPy in-place operations such as x[idx] += y, if multiple indices refer to the same location, all updates will be applied (NumPy would only apply the last update, rather than applying all updates.) The order in which conflicting updates are applied is implementation-defined and may be nondeterministic (e.g., due to concurrency on some hardware platforms).

By default, JAX assumes that all indices are in-bounds. Alternative out-of-bound index semantics can be specified via the mode parameter (see below).

Parameters:
  • mode –

    string specifying out-of-bound indexing mode. Options are:

    • "promise_in_bounds": (default) The user promises that indices are in bounds. No additional checking will be performed. In practice, this means that out-of-bounds indices in get() will be clipped, and out-of-bounds indices in set(), add(), etc. will be dropped.

    • "clip": clamp out of bounds indices into valid range.

    • "drop": ignore out-of-bound indices.

    • "fill": alias for "drop". For get(), the optional fill_value argument specifies the value that will be returned.

    See jax.lax.GatherScatterMode for more details.

  • wrap_negative_indices – If True (default) then negative indices indicate position from the end of the array, similar to Python and NumPy indexing. If False, then negative indices are considered out-of-bounds and behave according to the mode parameter.

  • fill_value – Only applies to the get() method: the fill value to return for out-of-bounds slices when mode is 'fill'. Ignored otherwise. Defaults to NaN for inexact types, the largest negative value for signed types, the largest positive value for unsigned types, and True for booleans.

  • indices_are_sorted – If True, the implementation will assume that the (normalized) indices passed to at[] are sorted in ascending order, which can lead to more efficient execution on some backends. If True but the indices are not actually sorted, the output is undefined.

  • unique_indices – If True, the implementation will assume that the (normalized) indices passed to at[] are unique, which can result in more efficient execution on some backends. If True but the indices are not actually unique, the output is undefined.

Examples

>>> x = jnp.arange(5.0)
>>> x
Array([0., 1., 2., 3., 4.], dtype=float32)
>>> x.at[2].get()
Array(2., dtype=float32)
>>> x.at[2].add(10)
Array([ 0.,  1., 12.,  3.,  4.], dtype=float32)

By default, out-of-bound indices are ignored in updates, but this behavior can be controlled with the mode parameter:

>>> x.at[10].add(10)  # dropped
Array([0., 1., 2., 3., 4.], dtype=float32)
>>> x.at[20].add(10, mode='clip')  # clipped
Array([ 0.,  1.,  2.,  3., 14.], dtype=float32)

For get(), out-of-bound indices are clipped by default:

>>> x.at[20].get()  # out-of-bounds indices clipped
Array(4., dtype=float32)
>>> x.at[20].get(mode='fill')  # out-of-bounds indices filled with NaN
Array(nan, dtype=float32)
>>> x.at[20].get(mode='fill', fill_value=-1)  # custom fill value
Array(-1., dtype=float32)

Negative indices count from the end of the array, but this behavior can be disabled by setting wrap_negative_indices = False:

>>> x.at[-1].set(99)
Array([ 0.,  1.,  2.,  3., 99.], dtype=float32)
>>> x.at[-1].set(99, wrap_negative_indices=False, mode='drop')  # dropped!
Array([0., 1., 2., 3., 4.], dtype=float32)
block_until_ready()

Block until the array is ready.

Return type:

AbstractQuantity

Examples

>>> import unxt as u
>>> q = u.Q(1, "m")
>>> q.block_until_ready() is q
True
decompose(bases: Sequence[UnitBase | FunctionUnitBase | str], /)

Decompose the quantity into the given bases.

Examples

>>> from unxt import Quantity
>>> q = Quantity(1, "m")
>>> q.decompose(["cm", "s"])
Quantity(Array(100., dtype=float32, ...), unit='cm')
Parameters:

bases (Sequence[UnitBase | FunctionUnitBase | str])

Return type:

AbstractQuantity

property device: Device

Device where the array is located.

Examples

>>> import unxt as u
>>> u.Q(1, "m").device
CpuDevice(id=0)
devices()

Return the devices where the array is located.

Return type:

set[Device]

Examples

>>> import unxt as u
>>> q = u.Q(1, "m")
>>> q.devices()
{CpuDevice(id=0)}
property dtype: dtype

Data type of the array.

Examples

>>> import unxt as u
>>> u.Q(1, "m").dtype
dtype('int32')
flatten()

Return a flattened version of the array.

Return type:

AbstractQuantity

Examples

>>> import unxt as u
>>> q = u.Q([[1, 2], [3, 4]], "m")
>>> q.flatten()
Quantity(Array([1, 2, 3, 4], dtype=int32), unit='m')
classmethod from_(cls: type[AbstractQuantity], *args: Any, **kwargs: Any)
from_(cls: type[AbstractQuantity], value: ArrayLike | list[Shaped[Array, ''] | Shaped[ndarray, ''] | bool | number | bool | int | float | complex] | tuple[Shaped[Array, ''] | Shaped[ndarray, ''] | bool | number | bool | int | float | complex, ...], unit: Any, /, *, dtype: Any = None) AbstractQuantity
Parameters:
Return type:

AbstractQuantity

Construct a unxt.AbstractQuantity from an array-like value and a unit.

Parameters:
  • value – The array-like value.

  • unit – The unit of the value.

  • dtype – The data type of the array (keyword-only).

  • args (Any)

  • kwargs (Any)

Return type:

AbstractQuantity

Examples

For this example we’ll use the Quantity class. The same applies to any subclass of AbstractQuantity.

>>> import jax.numpy as jnp
>>> import unxt as u
>>> x = jnp.array([1.0, 2, 3])
>>> u.Q.from_(x, "m")
Quantity(Array([1., 2., 3.], dtype=float32), unit='m')
>>> u.Q.from_([1.0, 2, 3], "m")
Quantity(Array([1., 2., 3.], dtype=float32), unit='m')
>>> u.Q.from_((1.0, 2, 3), "m")
Quantity(Array([1., 2., 3.], dtype=float32), unit='m')
from_(cls: type[AbstractQuantity], value: ArrayLike | list[Shaped[Array, ''] | Shaped[ndarray, ''] | bool | number | bool | int | float | complex] | tuple[Shaped[Array, ''] | Shaped[ndarray, ''] | bool | number | bool | int | float | complex, ...], /, *, unit: Any, dtype: Any = None) AbstractQuantity
Parameters:
Return type:

AbstractQuantity

Make a unxt.AbstractQuantity from an array-like value and a unit kwarg.

Examples

For this example we’ll use the unxt.Quantity class. The same applies to any subclass of unxt.AbstractQuantity.

>>> import unxt as u
>>> u.Q.from_([1.0, 2, 3], unit="m")
Quantity(Array([1., 2., 3.], dtype=float32), unit='m')
from_(cls: type[AbstractQuantity], *, value: Any, unit: Any, dtype: Any = None) AbstractQuantity
Parameters:
Return type:

AbstractQuantity

Construct a AbstractQuantity from value and unit kwargs.

Examples

For this example we’ll use the Quantity class. The same applies to any subclass of AbstractQuantity.

>>> import unxt as u
>>> u.Q.from_(value=[1.0, 2, 3], unit="m")
Quantity(Array([1., 2., 3.], dtype=float32), unit='m')
from_(cls: type[AbstractQuantity], mapping: Mapping[str, Any]) AbstractQuantity
Parameters:
Return type:

AbstractQuantity

Construct a quantity from a Mapping.

Examples

For this example we’ll use the Quantity class. The same applies to any subclass of AbstractQuantity.

>>> import jax.numpy as jnp
>>> import unxt as u
>>> x = jnp.array([1.0, 2, 3])
>>> q = u.Q.from_({"value": x, "unit": "m"})
>>> q
Quantity(Array([1., 2., 3.], dtype=float32), unit='m')
>>> u.Q.from_({"value": q, "unit": "km"})
Quantity(Array([0.001, 0.002, 0.003], dtype=float32), unit='km')
from_(cls: type[AbstractQuantity], value: AbstractQuantity, unit: Any, /, *, dtype: Any = None) AbstractQuantity
Parameters:
Return type:

AbstractQuantity

Construct a quantity from another quantity.

The value is converted to the new unit.

Examples

>>> import unxt as u
>>> q = u.Q(1, "m")
>>> u.Q.from_(q, "cm")
Quantity(Array(100., dtype=float32, ...), unit='cm')
from_(cls: type[AbstractQuantity], value: AbstractQuantity, unit: NoneType, /, *, dtype: Any = None) AbstractQuantity
Parameters:
Return type:

AbstractQuantity

Construct a quantity from another quantity.

The value is converted to the new unit.

Examples

>>> import unxt as u
>>> q = u.Q(1, "m")
>>> u.Q.from_(q, None)
Quantity(Array(1, dtype=int32...), unit='m')
from_(cls: type[AbstractQuantity], value: AbstractQuantity, /, *, unit: Any | None = None, dtype: Any = None) AbstractQuantity
Parameters:
Return type:

AbstractQuantity

Construct a quantity from another quantity.

The unit is unchanged.

from_(cls: type[StaticQuantity], value: ArrayLike | list[Shaped[Array, ''] | Shaped[ndarray, ''] | bool | number | bool | int | float | complex] | tuple[Shaped[Array, ''] | Shaped[ndarray, ''] | bool | number | bool | int | float | complex, ...], unit: Any, /, *, dtype: Any = None) StaticQuantity
Parameters:
Return type:

AbstractQuantity

Construct a StaticQuantity, keeping the value on NumPy dtypes.

The generic AbstractQuantity.from_ routes the value through jnp.asarray, which applies JAX’s x64-disabled dtype rules and silently downcasts int64 / float64 to int32 / float32. A StaticQuantity stores its value verbatim, so hand the value straight to __init__ and let the StaticValue.from_ converter convert it – that preserves the NumPy dtype. Delegating (rather than calling np.asarray here) also keeps .from_ and __init__ under the same policy for JAX inputs, instead of materialising an array the constructor would reject. (The keyword-unit overload delegates here, so it is covered too.)

Examples

>>> import numpy as np
>>> import unxt as u
>>> u.StaticQuantity.from_(
...     np.array([1, 2, 3], dtype=np.int64), "m"
... ).value.array.dtype
dtype('int64')

.from_ applies the same JAX-input policy as __init__ – it delegates rather than converting first, so the two cannot drift:

>>> import jax.numpy as jnp
>>> u.StaticQuantity.from_(jnp.array([1.0, 2.0]), "m")
StaticQuantity(array([1., 2.], dtype=float32), unit='m')
from_(cls: type[AbstractQuantity], value: Quantity, /, **kwargs: Any) AbstractQuantity
Parameters:
Return type:

AbstractQuantity

Construct a quantity from an astropy Quantity.

The value is converted to the new unit.

Examples

>>> import unxt as u
>>> import astropy.units as apyu
>>> u.Q.from_(apyu.Quantity(1, "m"))
Quantity(Array(1., dtype=float32), unit='m')
from_(cls: type[AbstractQuantity], value: Quantity, u: Any, /, **kwargs: Any) AbstractQuantity
Parameters:
Return type:

AbstractQuantity

Construct a quantity from an astropy Quantity, converting to a target unit.

The value is converted to the new unit.

Examples

>>> import unxt as u
>>> import astropy.units as apyu
>>> u.Q.from_(apyu.Quantity(1, "m"), "cm")
Quantity(Array(100., dtype=float32), unit='cm')
Parameters:
Return type:

AbstractQuantity

is_equivalent(other: AbstractQuantity, /)

Whether self and other are physically equal (unit-aware).

The method form of unxt.equivalent; unlike == (which is unit-blind for StaticValue-backed quantities) this accounts for unit conversion.

Examples

>>> import unxt as u
>>> u.Q(1000.0, "m").is_equivalent(u.Q(1.0, "km"))
Quantity(Array(True, dtype=bool...), unit='')
Parameters:

other (AbstractQuantity)

Return type:

Any

property mT: AbstractQuantity

Matrix transpose of the array.

Examples

>>> import unxt as u
>>> q = u.Q([[0, 1], [1, 2]], "m")
>>> q.mT
Quantity(Array([[0, 1],
                          [1, 2]], dtype=int32), unit='m')
max(*args: Any, **kwargs: Any)

Return the maximum value.

Examples

>>> import unxt as u
>>> q = u.Q([1, 2, 3], "m")
>>> q.max()
Quantity(Array(3, dtype=int32), unit='m')
Parameters:
Return type:

AbstractQuantity

mean(*args: Any, **kwargs: Any)

Return the mean value.

Examples

>>> import unxt as u
>>> q = u.Q([1, 2, 3], "m")
>>> q.mean()
Quantity(Array(2., dtype=float32), unit='m')
Parameters:
Return type:

AbstractQuantity

min(*args: Any, **kwargs: Any)

Return the minimum value.

Examples

>>> import unxt as u
>>> q = u.Q([1, 2, 3], "m")
>>> q.min()
Quantity(Array(1, dtype=int32), unit='m')
Parameters:
Return type:

AbstractQuantity

property ndim: int

Number of dimensions.

Examples

>>> import unxt as u
>>> q = u.Q([[1]], "m")
>>> q.ndim
2
ravel()

Return a flattened version of the array.

Return type:

AbstractQuantity

Examples

>>> import unxt as u
>>> q = u.Q([[1, 2], [3, 4]], "m")
>>> q.ravel()
Quantity(Array([1, 2, 3, 4], dtype=int32), unit='m')
reshape(*args: Any, order: str = 'C')

Return a reshaped version of the array.

Examples

>>> import unxt as u
>>> q = u.Q([1, 2, 3, 4], "m")
>>> q.reshape(2, 2)
Quantity(Array([[1, 2],
                          [3, 4]], dtype=int32), unit='m')
Parameters:
Return type:

AbstractQuantity

round(*args: Any, **kwargs: Any)

Round the array to the given number of decimals.

Examples

>>> import unxt as u
>>> q = u.Q([1.1, 2.2, 3.3], "m")
>>> q.round(0)
Quantity(Array([1., 2., 3.], dtype=float32), unit='m')
Parameters:
Return type:

AbstractQuantity

property shape: tuple[int, ...]

Shape of the array.

property sharding: Any

Return the sharding configuration of the array.

Examples

>>> import unxt as u
>>> q = u.Q([1, 2, 3], "m")
>>> q.sharding
SingleDeviceSharding(device=..., memory_kind=...)
property size: int

Total number of elements.

Examples

>>> import unxt as u
>>> q = u.Q([1, 2, 3], "m")
>>> q.size
3
squeeze(*args: Any, **kwargs: Any)

Return the array with all single-dimensional entries removed.

Examples

>>> import unxt as u
>>> q = u.Q([[[1], [2], [3]]], "m")
>>> q.squeeze()
Quantity(Array([1, 2, 3], dtype=int32), unit='m')
Parameters:
Return type:

AbstractQuantity

to(u: Any, /)

Convert the quantity to the given units.

See unxt.quantity.AbstractQuantity.uconvert.

Examples

>>> from unxt import Quantity
>>> q = Quantity(1, "m")
>>> q.to("cm")
Quantity(Array(100., dtype=float32, ...), unit='cm')
Parameters:

u (Any)

Return type:

AbstractQuantity

to_device(device: None | Device = None)

Move the array to a new device.

Examples

>>> import unxt as u
>>> q = u.Q(1, "m")
>>> q.to_device(None)
Quantity(Array(1, dtype=int32...), unit='m')
Parameters:

device (None | Device)

Return type:

AbstractQuantity

to_value(u: Any, /)

Return the value in the given units.

See unxt.AbstractQuantity.ustrip.

Examples

>>> from unxt import Quantity
>>> q = Quantity(1, "m")
>>> q.to_value("cm")
Array(100., dtype=float32, weak_type=True)
Parameters:

u (Any)

Return type:

Union[Array, ndarray, bool, number, bool, int, float, complex]

uconvert(u: Any, /)

Convert the quantity to the given units.

See also

None

convert a quantity to a new unit.

Examples

>>> import unxt as u
>>> q = u.Q(1, "m")
>>> q.uconvert("cm")
Quantity(Array(100., dtype=float32, ...), unit='cm')
Parameters:

u (Any)

Return type:

AbstractQuantity

ustrip(u: Any, /)

Return the value in the given units.

See also

None

strip the units from a quantity.

Examples

>>> import unxt as u
>>> q = u.Q(1, "m")
>>> q.ustrip("cm")
Array(100., dtype=float32, weak_type=True)
Parameters:

u (Any)

Return type:

Array

wrap_to(min: AbstractQuantity, max: AbstractQuantity)

Wrap the angle to the range [min, max).

Parameters:
Return type:

AbstractAngle

See also

None

functional version of this method.

Return type:

AbstractAngle

Parameters:

Examples

>>> import unxt as u
>>> angle = u.Angle(370, "deg")
>>> angle.wrap_to(min=u.Q(0, "deg"), max=u.Q(360, "deg"))
Angle(Array(10, dtype=int32...), unit='deg')
unit: UnitBase | FunctionUnitBase

The unit associated with this value.

unxt.Q

alias of Quantity

class unxt.Quantity(value: Any, unit: Any)

Bases: AbstractQuantity

The default quantity: units without dimension parametrization.

This class is not parametrized by its dimensionality, making it a single class (and a single JAX pytree type) regardless of dimension. For runtime dimension checking and dimension-specific dispatch, see unxts.parametric.ParametricQuantity.

Examples

>>> import unxt as u
>>> u.Quantity(1, "m")
Quantity(Array(1, dtype=int32...), unit='m')
Parameters:
value: Shaped[Array, '*shape'] | Shaped[StaticValue, '*shape']

The value of the Quantity.

unit: UnitBase | FunctionUnitBase

The unit associated with this value.

short_name: ClassVar[str] = 'Q'
property T: AbstractQuantity

Transpose of the array.

Examples

>>> import unxt as u
>>> q = u.Q([[0, 1], [1, 2]], "m")
>>> q.T
Quantity(Array([[0, 1],
                          [1, 2]], dtype=int32), unit='m')
argmax(*args: Any, **kwargs: Any)

Return the indices of the maximum value.

Examples

>>> import unxt as u
>>> q = u.Q([1, 2, 3], "m")
>>> q.argmax()
Array(2, dtype=int32)
Parameters:
Return type:

Array

argmin(*args: Any, **kwargs: Any)

Return the indices of the minimum value.

Examples

>>> import unxt as u
>>> q = u.Q([1, 2, 3], "m")
>>> q.argmin()
Array(0, dtype=int32)
Parameters:
Return type:

Array

astype(*args: Any, **kwargs: Any)

Copy the array and cast to a specified dtype.

Examples

>>> import unxt as u
>>> q = u.Q([1, 2, 3], "m")
>>> q.dtype
dtype('int32')
>>> q.astype(float)
Quantity(Array([1., 2., 3.], dtype=float32), unit='m')
Parameters:
Return type:

AbstractQuantity

property at: _QuantityIndexUpdateHelper

Helper property for index update functionality.

The at property provides a functionally pure equivalent of in-place array modifications.

In particular:

Alternate syntax

Equivalent In-place expression

x = x.at[idx].set(y)

x[idx] = y

x = x.at[idx].add(y)

x[idx] += y

x = x.at[idx].subtract(y)

x[idx] -= y

x = x.at[idx].multiply(y)

x[idx] *= y

x = x.at[idx].divide(y)

x[idx] /= y

x = x.at[idx].power(y)

x[idx] **= y

x = x.at[idx].min(y)

x[idx] = minimum(x[idx], y)

x = x.at[idx].max(y)

x[idx] = maximum(x[idx], y)

x = x.at[idx].apply(ufunc)

ufunc.at(x, idx)

x = x.at[idx].get()

x = x[idx]

None of the x.at expressions modify the original x; instead they return a modified copy of x. However, inside a jit() compiled function, expressions like x = x.at[idx].set(y) are guaranteed to be applied in-place.

Unlike NumPy in-place operations such as x[idx] += y, if multiple indices refer to the same location, all updates will be applied (NumPy would only apply the last update, rather than applying all updates.) The order in which conflicting updates are applied is implementation-defined and may be nondeterministic (e.g., due to concurrency on some hardware platforms).

By default, JAX assumes that all indices are in-bounds. Alternative out-of-bound index semantics can be specified via the mode parameter (see below).

Parameters:
  • mode –

    string specifying out-of-bound indexing mode. Options are:

    • "promise_in_bounds": (default) The user promises that indices are in bounds. No additional checking will be performed. In practice, this means that out-of-bounds indices in get() will be clipped, and out-of-bounds indices in set(), add(), etc. will be dropped.

    • "clip": clamp out of bounds indices into valid range.

    • "drop": ignore out-of-bound indices.

    • "fill": alias for "drop". For get(), the optional fill_value argument specifies the value that will be returned.

    See jax.lax.GatherScatterMode for more details.

  • wrap_negative_indices – If True (default) then negative indices indicate position from the end of the array, similar to Python and NumPy indexing. If False, then negative indices are considered out-of-bounds and behave according to the mode parameter.

  • fill_value – Only applies to the get() method: the fill value to return for out-of-bounds slices when mode is 'fill'. Ignored otherwise. Defaults to NaN for inexact types, the largest negative value for signed types, the largest positive value for unsigned types, and True for booleans.

  • indices_are_sorted – If True, the implementation will assume that the (normalized) indices passed to at[] are sorted in ascending order, which can lead to more efficient execution on some backends. If True but the indices are not actually sorted, the output is undefined.

  • unique_indices – If True, the implementation will assume that the (normalized) indices passed to at[] are unique, which can result in more efficient execution on some backends. If True but the indices are not actually unique, the output is undefined.

Examples

>>> x = jnp.arange(5.0)
>>> x
Array([0., 1., 2., 3., 4.], dtype=float32)
>>> x.at[2].get()
Array(2., dtype=float32)
>>> x.at[2].add(10)
Array([ 0.,  1., 12.,  3.,  4.], dtype=float32)

By default, out-of-bound indices are ignored in updates, but this behavior can be controlled with the mode parameter:

>>> x.at[10].add(10)  # dropped
Array([0., 1., 2., 3., 4.], dtype=float32)
>>> x.at[20].add(10, mode='clip')  # clipped
Array([ 0.,  1.,  2.,  3., 14.], dtype=float32)

For get(), out-of-bound indices are clipped by default:

>>> x.at[20].get()  # out-of-bounds indices clipped
Array(4., dtype=float32)
>>> x.at[20].get(mode='fill')  # out-of-bounds indices filled with NaN
Array(nan, dtype=float32)
>>> x.at[20].get(mode='fill', fill_value=-1)  # custom fill value
Array(-1., dtype=float32)

Negative indices count from the end of the array, but this behavior can be disabled by setting wrap_negative_indices = False:

>>> x.at[-1].set(99)
Array([ 0.,  1.,  2.,  3., 99.], dtype=float32)
>>> x.at[-1].set(99, wrap_negative_indices=False, mode='drop')  # dropped!
Array([0., 1., 2., 3., 4.], dtype=float32)
block_until_ready()

Block until the array is ready.

Return type:

AbstractQuantity

Examples

>>> import unxt as u
>>> q = u.Q(1, "m")
>>> q.block_until_ready() is q
True
decompose(bases: Sequence[UnitBase | FunctionUnitBase | str], /)

Decompose the quantity into the given bases.

Examples

>>> from unxt import Quantity
>>> q = Quantity(1, "m")
>>> q.decompose(["cm", "s"])
Quantity(Array(100., dtype=float32, ...), unit='cm')
Parameters:

bases (Sequence[UnitBase | FunctionUnitBase | str])

Return type:

AbstractQuantity

property device: Device

Device where the array is located.

Examples

>>> import unxt as u
>>> u.Q(1, "m").device
CpuDevice(id=0)
devices()

Return the devices where the array is located.

Return type:

set[Device]

Examples

>>> import unxt as u
>>> q = u.Q(1, "m")
>>> q.devices()
{CpuDevice(id=0)}
property dtype: dtype

Data type of the array.

Examples

>>> import unxt as u
>>> u.Q(1, "m").dtype
dtype('int32')
flatten()

Return a flattened version of the array.

Return type:

AbstractQuantity

Examples

>>> import unxt as u
>>> q = u.Q([[1, 2], [3, 4]], "m")
>>> q.flatten()
Quantity(Array([1, 2, 3, 4], dtype=int32), unit='m')
classmethod from_(cls: type[AbstractQuantity], *args: Any, **kwargs: Any)
from_(cls: type[AbstractQuantity], value: ArrayLike | list[Shaped[Array, ''] | Shaped[ndarray, ''] | bool | number | bool | int | float | complex] | tuple[Shaped[Array, ''] | Shaped[ndarray, ''] | bool | number | bool | int | float | complex, ...], unit: Any, /, *, dtype: Any = None) AbstractQuantity
Parameters:
Return type:

AbstractQuantity

Construct a unxt.AbstractQuantity from an array-like value and a unit.

Parameters:
  • value – The array-like value.

  • unit – The unit of the value.

  • dtype – The data type of the array (keyword-only).

  • args (Any)

  • kwargs (Any)

Return type:

AbstractQuantity

Examples

For this example we’ll use the Quantity class. The same applies to any subclass of AbstractQuantity.

>>> import jax.numpy as jnp
>>> import unxt as u
>>> x = jnp.array([1.0, 2, 3])
>>> u.Q.from_(x, "m")
Quantity(Array([1., 2., 3.], dtype=float32), unit='m')
>>> u.Q.from_([1.0, 2, 3], "m")
Quantity(Array([1., 2., 3.], dtype=float32), unit='m')
>>> u.Q.from_((1.0, 2, 3), "m")
Quantity(Array([1., 2., 3.], dtype=float32), unit='m')
from_(cls: type[AbstractQuantity], value: ArrayLike | list[Shaped[Array, ''] | Shaped[ndarray, ''] | bool | number | bool | int | float | complex] | tuple[Shaped[Array, ''] | Shaped[ndarray, ''] | bool | number | bool | int | float | complex, ...], /, *, unit: Any, dtype: Any = None) AbstractQuantity
Parameters:
Return type:

AbstractQuantity

Make a unxt.AbstractQuantity from an array-like value and a unit kwarg.

Examples

For this example we’ll use the unxt.Quantity class. The same applies to any subclass of unxt.AbstractQuantity.

>>> import unxt as u
>>> u.Q.from_([1.0, 2, 3], unit="m")
Quantity(Array([1., 2., 3.], dtype=float32), unit='m')
from_(cls: type[AbstractQuantity], *, value: Any, unit: Any, dtype: Any = None) AbstractQuantity
Parameters:
Return type:

AbstractQuantity

Construct a AbstractQuantity from value and unit kwargs.

Examples

For this example we’ll use the Quantity class. The same applies to any subclass of AbstractQuantity.

>>> import unxt as u
>>> u.Q.from_(value=[1.0, 2, 3], unit="m")
Quantity(Array([1., 2., 3.], dtype=float32), unit='m')
from_(cls: type[AbstractQuantity], mapping: Mapping[str, Any]) AbstractQuantity
Parameters:
Return type:

AbstractQuantity

Construct a quantity from a Mapping.

Examples

For this example we’ll use the Quantity class. The same applies to any subclass of AbstractQuantity.

>>> import jax.numpy as jnp
>>> import unxt as u
>>> x = jnp.array([1.0, 2, 3])
>>> q = u.Q.from_({"value": x, "unit": "m"})
>>> q
Quantity(Array([1., 2., 3.], dtype=float32), unit='m')
>>> u.Q.from_({"value": q, "unit": "km"})
Quantity(Array([0.001, 0.002, 0.003], dtype=float32), unit='km')
from_(cls: type[AbstractQuantity], value: AbstractQuantity, unit: Any, /, *, dtype: Any = None) AbstractQuantity
Parameters:
Return type:

AbstractQuantity

Construct a quantity from another quantity.

The value is converted to the new unit.

Examples

>>> import unxt as u
>>> q = u.Q(1, "m")
>>> u.Q.from_(q, "cm")
Quantity(Array(100., dtype=float32, ...), unit='cm')
from_(cls: type[AbstractQuantity], value: AbstractQuantity, unit: NoneType, /, *, dtype: Any = None) AbstractQuantity
Parameters:
Return type:

AbstractQuantity

Construct a quantity from another quantity.

The value is converted to the new unit.

Examples

>>> import unxt as u
>>> q = u.Q(1, "m")
>>> u.Q.from_(q, None)
Quantity(Array(1, dtype=int32...), unit='m')
from_(cls: type[AbstractQuantity], value: AbstractQuantity, /, *, unit: Any | None = None, dtype: Any = None) AbstractQuantity
Parameters:
Return type:

AbstractQuantity

Construct a quantity from another quantity.

The unit is unchanged.

from_(cls: type[StaticQuantity], value: ArrayLike | list[Shaped[Array, ''] | Shaped[ndarray, ''] | bool | number | bool | int | float | complex] | tuple[Shaped[Array, ''] | Shaped[ndarray, ''] | bool | number | bool | int | float | complex, ...], unit: Any, /, *, dtype: Any = None) StaticQuantity
Parameters:
Return type:

AbstractQuantity

Construct a StaticQuantity, keeping the value on NumPy dtypes.

The generic AbstractQuantity.from_ routes the value through jnp.asarray, which applies JAX’s x64-disabled dtype rules and silently downcasts int64 / float64 to int32 / float32. A StaticQuantity stores its value verbatim, so hand the value straight to __init__ and let the StaticValue.from_ converter convert it – that preserves the NumPy dtype. Delegating (rather than calling np.asarray here) also keeps .from_ and __init__ under the same policy for JAX inputs, instead of materialising an array the constructor would reject. (The keyword-unit overload delegates here, so it is covered too.)

Examples

>>> import numpy as np
>>> import unxt as u
>>> u.StaticQuantity.from_(
...     np.array([1, 2, 3], dtype=np.int64), "m"
... ).value.array.dtype
dtype('int64')

.from_ applies the same JAX-input policy as __init__ – it delegates rather than converting first, so the two cannot drift:

>>> import jax.numpy as jnp
>>> u.StaticQuantity.from_(jnp.array([1.0, 2.0]), "m")
StaticQuantity(array([1., 2.], dtype=float32), unit='m')
from_(cls: type[AbstractQuantity], value: Quantity, /, **kwargs: Any) AbstractQuantity
Parameters:
Return type:

AbstractQuantity

Construct a quantity from an astropy Quantity.

The value is converted to the new unit.

Examples

>>> import unxt as u
>>> import astropy.units as apyu
>>> u.Q.from_(apyu.Quantity(1, "m"))
Quantity(Array(1., dtype=float32), unit='m')
from_(cls: type[AbstractQuantity], value: Quantity, u: Any, /, **kwargs: Any) AbstractQuantity
Parameters:
Return type:

AbstractQuantity

Construct a quantity from an astropy Quantity, converting to a target unit.

The value is converted to the new unit.

Examples

>>> import unxt as u
>>> import astropy.units as apyu
>>> u.Q.from_(apyu.Quantity(1, "m"), "cm")
Quantity(Array(100., dtype=float32), unit='cm')
Parameters:
Return type:

AbstractQuantity

is_equivalent(other: AbstractQuantity, /)

Whether self and other are physically equal (unit-aware).

The method form of unxt.equivalent; unlike == (which is unit-blind for StaticValue-backed quantities) this accounts for unit conversion.

Examples

>>> import unxt as u
>>> u.Q(1000.0, "m").is_equivalent(u.Q(1.0, "km"))
Quantity(Array(True, dtype=bool...), unit='')
Parameters:

other (AbstractQuantity)

Return type:

Any

property mT: AbstractQuantity

Matrix transpose of the array.

Examples

>>> import unxt as u
>>> q = u.Q([[0, 1], [1, 2]], "m")
>>> q.mT
Quantity(Array([[0, 1],
                          [1, 2]], dtype=int32), unit='m')
max(*args: Any, **kwargs: Any)

Return the maximum value.

Examples

>>> import unxt as u
>>> q = u.Q([1, 2, 3], "m")
>>> q.max()
Quantity(Array(3, dtype=int32), unit='m')
Parameters:
Return type:

AbstractQuantity

mean(*args: Any, **kwargs: Any)

Return the mean value.

Examples

>>> import unxt as u
>>> q = u.Q([1, 2, 3], "m")
>>> q.mean()
Quantity(Array(2., dtype=float32), unit='m')
Parameters:
Return type:

AbstractQuantity

min(*args: Any, **kwargs: Any)

Return the minimum value.

Examples

>>> import unxt as u
>>> q = u.Q([1, 2, 3], "m")
>>> q.min()
Quantity(Array(1, dtype=int32), unit='m')
Parameters:
Return type:

AbstractQuantity

property ndim: int

Number of dimensions.

Examples

>>> import unxt as u
>>> q = u.Q([[1]], "m")
>>> q.ndim
2
ravel()

Return a flattened version of the array.

Return type:

AbstractQuantity

Examples

>>> import unxt as u
>>> q = u.Q([[1, 2], [3, 4]], "m")
>>> q.ravel()
Quantity(Array([1, 2, 3, 4], dtype=int32), unit='m')
reshape(*args: Any, order: str = 'C')

Return a reshaped version of the array.

Examples

>>> import unxt as u
>>> q = u.Q([1, 2, 3, 4], "m")
>>> q.reshape(2, 2)
Quantity(Array([[1, 2],
                          [3, 4]], dtype=int32), unit='m')
Parameters:
Return type:

AbstractQuantity

round(*args: Any, **kwargs: Any)

Round the array to the given number of decimals.

Examples

>>> import unxt as u
>>> q = u.Q([1.1, 2.2, 3.3], "m")
>>> q.round(0)
Quantity(Array([1., 2., 3.], dtype=float32), unit='m')
Parameters:
Return type:

AbstractQuantity

property shape: tuple[int, ...]

Shape of the array.

property sharding: Any

Return the sharding configuration of the array.

Examples

>>> import unxt as u
>>> q = u.Q([1, 2, 3], "m")
>>> q.sharding
SingleDeviceSharding(device=..., memory_kind=...)
property size: int

Total number of elements.

Examples

>>> import unxt as u
>>> q = u.Q([1, 2, 3], "m")
>>> q.size
3
squeeze(*args: Any, **kwargs: Any)

Return the array with all single-dimensional entries removed.

Examples

>>> import unxt as u
>>> q = u.Q([[[1], [2], [3]]], "m")
>>> q.squeeze()
Quantity(Array([1, 2, 3], dtype=int32), unit='m')
Parameters:
Return type:

AbstractQuantity

to(u: Any, /)

Convert the quantity to the given units.

See unxt.quantity.AbstractQuantity.uconvert.

Examples

>>> from unxt import Quantity
>>> q = Quantity(1, "m")
>>> q.to("cm")
Quantity(Array(100., dtype=float32, ...), unit='cm')
Parameters:

u (Any)

Return type:

AbstractQuantity

to_device(device: None | Device = None)

Move the array to a new device.

Examples

>>> import unxt as u
>>> q = u.Q(1, "m")
>>> q.to_device(None)
Quantity(Array(1, dtype=int32...), unit='m')
Parameters:

device (None | Device)

Return type:

AbstractQuantity

to_value(u: Any, /)

Return the value in the given units.

See unxt.AbstractQuantity.ustrip.

Examples

>>> from unxt import Quantity
>>> q = Quantity(1, "m")
>>> q.to_value("cm")
Array(100., dtype=float32, weak_type=True)
Parameters:

u (Any)

Return type:

Union[Array, ndarray, bool, number, bool, int, float, complex]

uconvert(u: Any, /)

Convert the quantity to the given units.

See also

None

convert a quantity to a new unit.

Examples

>>> import unxt as u
>>> q = u.Q(1, "m")
>>> q.uconvert("cm")
Quantity(Array(100., dtype=float32, ...), unit='cm')
Parameters:

u (Any)

Return type:

AbstractQuantity

ustrip(u: Any, /)

Return the value in the given units.

See also

None

strip the units from a quantity.

Examples

>>> import unxt as u
>>> q = u.Q(1, "m")
>>> q.ustrip("cm")
Array(100., dtype=float32, weak_type=True)
Parameters:

u (Any)

Return type:

Array

final class unxt.StaticQuantity(value: Any, unit: Any)

Bases: AbstractQuantity

A non-parametric quantity whose value is always a static NumPy array.

Unlike ~unxt.Quantity, its value is stored as a static (hashable) NumPy array, which lets a StaticQuantity be passed as a static argument to a jax.jit-compiled function. It accepts Python scalars and array-like inputs convertible to NumPy arrays; a concrete (eager) JAX array is materialised back to NumPy, and only a traced value – which cannot be static – is rejected.

Examples

>>> import numpy as np
>>> import unxt as u

Basic construction:

>>> q = u.StaticQuantity(np.array([1.0, 2.0]), "m")
>>> q
StaticQuantity(array([1., 2.]), unit='m')

Values are static and hashable:

>>> isinstance(hash(q), int)
True

A concrete JAX array is materialised back to NumPy (a static value is just data); only traced values (under jit/vmap/grad) are rejected:

>>> import jax.numpy as jnp
>>> u.StaticQuantity(jnp.array([1.0, 2.0]), "m")
StaticQuantity(array([1., 2.], dtype=float32), unit='m')

The Wadler-Lindig representation hides the internal static wrapper:

>>> import wadler_lindig as wl
>>> wl.pprint(q, short_arrays=False)
StaticQuantity(array([1., 2.]), unit='m')
Parameters:
value: StaticValue

The static value of the AbstractQuantity.

unit: UnitBase | FunctionUnitBase

The unit associated with this value.

property T: AbstractQuantity

Transpose of the array.

Examples

>>> import unxt as u
>>> q = u.Q([[0, 1], [1, 2]], "m")
>>> q.T
Quantity(Array([[0, 1],
                          [1, 2]], dtype=int32), unit='m')
argmax(*args: Any, **kwargs: Any)

Return the indices of the maximum value.

Examples

>>> import unxt as u
>>> q = u.Q([1, 2, 3], "m")
>>> q.argmax()
Array(2, dtype=int32)
Parameters:
Return type:

Array

argmin(*args: Any, **kwargs: Any)

Return the indices of the minimum value.

Examples

>>> import unxt as u
>>> q = u.Q([1, 2, 3], "m")
>>> q.argmin()
Array(0, dtype=int32)
Parameters:
Return type:

Array

astype(*args: Any, **kwargs: Any)

Copy the array and cast to a specified dtype.

Examples

>>> import unxt as u
>>> q = u.Q([1, 2, 3], "m")
>>> q.dtype
dtype('int32')
>>> q.astype(float)
Quantity(Array([1., 2., 3.], dtype=float32), unit='m')
Parameters:
Return type:

AbstractQuantity

property at: _QuantityIndexUpdateHelper

Helper property for index update functionality.

The at property provides a functionally pure equivalent of in-place array modifications.

In particular:

Alternate syntax

Equivalent In-place expression

x = x.at[idx].set(y)

x[idx] = y

x = x.at[idx].add(y)

x[idx] += y

x = x.at[idx].subtract(y)

x[idx] -= y

x = x.at[idx].multiply(y)

x[idx] *= y

x = x.at[idx].divide(y)

x[idx] /= y

x = x.at[idx].power(y)

x[idx] **= y

x = x.at[idx].min(y)

x[idx] = minimum(x[idx], y)

x = x.at[idx].max(y)

x[idx] = maximum(x[idx], y)

x = x.at[idx].apply(ufunc)

ufunc.at(x, idx)

x = x.at[idx].get()

x = x[idx]

None of the x.at expressions modify the original x; instead they return a modified copy of x. However, inside a jit() compiled function, expressions like x = x.at[idx].set(y) are guaranteed to be applied in-place.

Unlike NumPy in-place operations such as x[idx] += y, if multiple indices refer to the same location, all updates will be applied (NumPy would only apply the last update, rather than applying all updates.) The order in which conflicting updates are applied is implementation-defined and may be nondeterministic (e.g., due to concurrency on some hardware platforms).

By default, JAX assumes that all indices are in-bounds. Alternative out-of-bound index semantics can be specified via the mode parameter (see below).

Parameters:
  • mode –

    string specifying out-of-bound indexing mode. Options are:

    • "promise_in_bounds": (default) The user promises that indices are in bounds. No additional checking will be performed. In practice, this means that out-of-bounds indices in get() will be clipped, and out-of-bounds indices in set(), add(), etc. will be dropped.

    • "clip": clamp out of bounds indices into valid range.

    • "drop": ignore out-of-bound indices.

    • "fill": alias for "drop". For get(), the optional fill_value argument specifies the value that will be returned.

    See jax.lax.GatherScatterMode for more details.

  • wrap_negative_indices – If True (default) then negative indices indicate position from the end of the array, similar to Python and NumPy indexing. If False, then negative indices are considered out-of-bounds and behave according to the mode parameter.

  • fill_value – Only applies to the get() method: the fill value to return for out-of-bounds slices when mode is 'fill'. Ignored otherwise. Defaults to NaN for inexact types, the largest negative value for signed types, the largest positive value for unsigned types, and True for booleans.

  • indices_are_sorted – If True, the implementation will assume that the (normalized) indices passed to at[] are sorted in ascending order, which can lead to more efficient execution on some backends. If True but the indices are not actually sorted, the output is undefined.

  • unique_indices – If True, the implementation will assume that the (normalized) indices passed to at[] are unique, which can result in more efficient execution on some backends. If True but the indices are not actually unique, the output is undefined.

Examples

>>> x = jnp.arange(5.0)
>>> x
Array([0., 1., 2., 3., 4.], dtype=float32)
>>> x.at[2].get()
Array(2., dtype=float32)
>>> x.at[2].add(10)
Array([ 0.,  1., 12.,  3.,  4.], dtype=float32)

By default, out-of-bound indices are ignored in updates, but this behavior can be controlled with the mode parameter:

>>> x.at[10].add(10)  # dropped
Array([0., 1., 2., 3., 4.], dtype=float32)
>>> x.at[20].add(10, mode='clip')  # clipped
Array([ 0.,  1.,  2.,  3., 14.], dtype=float32)

For get(), out-of-bound indices are clipped by default:

>>> x.at[20].get()  # out-of-bounds indices clipped
Array(4., dtype=float32)
>>> x.at[20].get(mode='fill')  # out-of-bounds indices filled with NaN
Array(nan, dtype=float32)
>>> x.at[20].get(mode='fill', fill_value=-1)  # custom fill value
Array(-1., dtype=float32)

Negative indices count from the end of the array, but this behavior can be disabled by setting wrap_negative_indices = False:

>>> x.at[-1].set(99)
Array([ 0.,  1.,  2.,  3., 99.], dtype=float32)
>>> x.at[-1].set(99, wrap_negative_indices=False, mode='drop')  # dropped!
Array([0., 1., 2., 3., 4.], dtype=float32)
block_until_ready()

Block until the array is ready.

Return type:

AbstractQuantity

Examples

>>> import unxt as u
>>> q = u.Q(1, "m")
>>> q.block_until_ready() is q
True
decompose(bases: Sequence[UnitBase | FunctionUnitBase | str], /)

Decompose the quantity into the given bases.

Examples

>>> from unxt import Quantity
>>> q = Quantity(1, "m")
>>> q.decompose(["cm", "s"])
Quantity(Array(100., dtype=float32, ...), unit='cm')
Parameters:

bases (Sequence[UnitBase | FunctionUnitBase | str])

Return type:

AbstractQuantity

property device: Device

Device where the array is located.

Examples

>>> import unxt as u
>>> u.Q(1, "m").device
CpuDevice(id=0)
devices()

Return the devices where the array is located.

Return type:

set[Device]

Examples

>>> import unxt as u
>>> q = u.Q(1, "m")
>>> q.devices()
{CpuDevice(id=0)}
property dtype: dtype

Data type of the array.

Examples

>>> import unxt as u
>>> u.Q(1, "m").dtype
dtype('int32')
flatten()

Return a flattened version of the array.

Return type:

AbstractQuantity

Examples

>>> import unxt as u
>>> q = u.Q([[1, 2], [3, 4]], "m")
>>> q.flatten()
Quantity(Array([1, 2, 3, 4], dtype=int32), unit='m')
classmethod from_(cls: type[AbstractQuantity], *args: Any, **kwargs: Any)
from_(cls: type[AbstractQuantity], value: ArrayLike | list[Shaped[Array, ''] | Shaped[ndarray, ''] | bool | number | bool | int | float | complex] | tuple[Shaped[Array, ''] | Shaped[ndarray, ''] | bool | number | bool | int | float | complex, ...], unit: Any, /, *, dtype: Any = None) AbstractQuantity
Parameters:
Return type:

AbstractQuantity

Construct a unxt.AbstractQuantity from an array-like value and a unit.

Parameters:
  • value – The array-like value.

  • unit – The unit of the value.

  • dtype – The data type of the array (keyword-only).

  • args (Any)

  • kwargs (Any)

Return type:

AbstractQuantity

Examples

For this example we’ll use the Quantity class. The same applies to any subclass of AbstractQuantity.

>>> import jax.numpy as jnp
>>> import unxt as u
>>> x = jnp.array([1.0, 2, 3])
>>> u.Q.from_(x, "m")
Quantity(Array([1., 2., 3.], dtype=float32), unit='m')
>>> u.Q.from_([1.0, 2, 3], "m")
Quantity(Array([1., 2., 3.], dtype=float32), unit='m')
>>> u.Q.from_((1.0, 2, 3), "m")
Quantity(Array([1., 2., 3.], dtype=float32), unit='m')
from_(cls: type[AbstractQuantity], value: ArrayLike | list[Shaped[Array, ''] | Shaped[ndarray, ''] | bool | number | bool | int | float | complex] | tuple[Shaped[Array, ''] | Shaped[ndarray, ''] | bool | number | bool | int | float | complex, ...], /, *, unit: Any, dtype: Any = None) AbstractQuantity
Parameters:
Return type:

AbstractQuantity

Make a unxt.AbstractQuantity from an array-like value and a unit kwarg.

Examples

For this example we’ll use the unxt.Quantity class. The same applies to any subclass of unxt.AbstractQuantity.

>>> import unxt as u
>>> u.Q.from_([1.0, 2, 3], unit="m")
Quantity(Array([1., 2., 3.], dtype=float32), unit='m')
from_(cls: type[AbstractQuantity], *, value: Any, unit: Any, dtype: Any = None) AbstractQuantity
Parameters:
Return type:

AbstractQuantity

Construct a AbstractQuantity from value and unit kwargs.

Examples

For this example we’ll use the Quantity class. The same applies to any subclass of AbstractQuantity.

>>> import unxt as u
>>> u.Q.from_(value=[1.0, 2, 3], unit="m")
Quantity(Array([1., 2., 3.], dtype=float32), unit='m')
from_(cls: type[AbstractQuantity], mapping: Mapping[str, Any]) AbstractQuantity
Parameters:
Return type:

AbstractQuantity

Construct a quantity from a Mapping.

Examples

For this example we’ll use the Quantity class. The same applies to any subclass of AbstractQuantity.

>>> import jax.numpy as jnp
>>> import unxt as u
>>> x = jnp.array([1.0, 2, 3])
>>> q = u.Q.from_({"value": x, "unit": "m"})
>>> q
Quantity(Array([1., 2., 3.], dtype=float32), unit='m')
>>> u.Q.from_({"value": q, "unit": "km"})
Quantity(Array([0.001, 0.002, 0.003], dtype=float32), unit='km')
from_(cls: type[AbstractQuantity], value: AbstractQuantity, unit: Any, /, *, dtype: Any = None) AbstractQuantity
Parameters:
Return type:

AbstractQuantity

Construct a quantity from another quantity.

The value is converted to the new unit.

Examples

>>> import unxt as u
>>> q = u.Q(1, "m")
>>> u.Q.from_(q, "cm")
Quantity(Array(100., dtype=float32, ...), unit='cm')
from_(cls: type[AbstractQuantity], value: AbstractQuantity, unit: NoneType, /, *, dtype: Any = None) AbstractQuantity
Parameters:
Return type:

AbstractQuantity

Construct a quantity from another quantity.

The value is converted to the new unit.

Examples

>>> import unxt as u
>>> q = u.Q(1, "m")
>>> u.Q.from_(q, None)
Quantity(Array(1, dtype=int32...), unit='m')
from_(cls: type[AbstractQuantity], value: AbstractQuantity, /, *, unit: Any | None = None, dtype: Any = None) AbstractQuantity
Parameters:
Return type:

AbstractQuantity

Construct a quantity from another quantity.

The unit is unchanged.

from_(cls: type[StaticQuantity], value: ArrayLike | list[Shaped[Array, ''] | Shaped[ndarray, ''] | bool | number | bool | int | float | complex] | tuple[Shaped[Array, ''] | Shaped[ndarray, ''] | bool | number | bool | int | float | complex, ...], unit: Any, /, *, dtype: Any = None) StaticQuantity
Parameters:
Return type:

AbstractQuantity

Construct a StaticQuantity, keeping the value on NumPy dtypes.

The generic AbstractQuantity.from_ routes the value through jnp.asarray, which applies JAX’s x64-disabled dtype rules and silently downcasts int64 / float64 to int32 / float32. A StaticQuantity stores its value verbatim, so hand the value straight to __init__ and let the StaticValue.from_ converter convert it – that preserves the NumPy dtype. Delegating (rather than calling np.asarray here) also keeps .from_ and __init__ under the same policy for JAX inputs, instead of materialising an array the constructor would reject. (The keyword-unit overload delegates here, so it is covered too.)

Examples

>>> import numpy as np
>>> import unxt as u
>>> u.StaticQuantity.from_(
...     np.array([1, 2, 3], dtype=np.int64), "m"
... ).value.array.dtype
dtype('int64')

.from_ applies the same JAX-input policy as __init__ – it delegates rather than converting first, so the two cannot drift:

>>> import jax.numpy as jnp
>>> u.StaticQuantity.from_(jnp.array([1.0, 2.0]), "m")
StaticQuantity(array([1., 2.], dtype=float32), unit='m')
from_(cls: type[AbstractQuantity], value: Quantity, /, **kwargs: Any) AbstractQuantity
Parameters:
Return type:

AbstractQuantity

Construct a quantity from an astropy Quantity.

The value is converted to the new unit.

Examples

>>> import unxt as u
>>> import astropy.units as apyu
>>> u.Q.from_(apyu.Quantity(1, "m"))
Quantity(Array(1., dtype=float32), unit='m')
from_(cls: type[AbstractQuantity], value: Quantity, u: Any, /, **kwargs: Any) AbstractQuantity
Parameters:
Return type:

AbstractQuantity

Construct a quantity from an astropy Quantity, converting to a target unit.

The value is converted to the new unit.

Examples

>>> import unxt as u
>>> import astropy.units as apyu
>>> u.Q.from_(apyu.Quantity(1, "m"), "cm")
Quantity(Array(100., dtype=float32), unit='cm')
Parameters:
Return type:

AbstractQuantity

is_equivalent(other: AbstractQuantity, /)

Whether self and other are physically equal (unit-aware).

The method form of unxt.equivalent; unlike == (which is unit-blind for StaticValue-backed quantities) this accounts for unit conversion.

Examples

>>> import unxt as u
>>> u.Q(1000.0, "m").is_equivalent(u.Q(1.0, "km"))
Quantity(Array(True, dtype=bool...), unit='')
Parameters:

other (AbstractQuantity)

Return type:

Any

property mT: AbstractQuantity

Matrix transpose of the array.

Examples

>>> import unxt as u
>>> q = u.Q([[0, 1], [1, 2]], "m")
>>> q.mT
Quantity(Array([[0, 1],
                          [1, 2]], dtype=int32), unit='m')
max(*args: Any, **kwargs: Any)

Return the maximum value.

Examples

>>> import unxt as u
>>> q = u.Q([1, 2, 3], "m")
>>> q.max()
Quantity(Array(3, dtype=int32), unit='m')
Parameters:
Return type:

AbstractQuantity

mean(*args: Any, **kwargs: Any)

Return the mean value.

Examples

>>> import unxt as u
>>> q = u.Q([1, 2, 3], "m")
>>> q.mean()
Quantity(Array(2., dtype=float32), unit='m')
Parameters:
Return type:

AbstractQuantity

min(*args: Any, **kwargs: Any)

Return the minimum value.

Examples

>>> import unxt as u
>>> q = u.Q([1, 2, 3], "m")
>>> q.min()
Quantity(Array(1, dtype=int32), unit='m')
Parameters:
Return type:

AbstractQuantity

property ndim: int

Number of dimensions.

Examples

>>> import unxt as u
>>> q = u.Q([[1]], "m")
>>> q.ndim
2
ravel()

Return a flattened version of the array.

Return type:

AbstractQuantity

Examples

>>> import unxt as u
>>> q = u.Q([[1, 2], [3, 4]], "m")
>>> q.ravel()
Quantity(Array([1, 2, 3, 4], dtype=int32), unit='m')
reshape(*args: Any, order: str = 'C')

Return a reshaped version of the array.

Examples

>>> import unxt as u
>>> q = u.Q([1, 2, 3, 4], "m")
>>> q.reshape(2, 2)
Quantity(Array([[1, 2],
                          [3, 4]], dtype=int32), unit='m')
Parameters:
Return type:

AbstractQuantity

round(*args: Any, **kwargs: Any)

Round the array to the given number of decimals.

Examples

>>> import unxt as u
>>> q = u.Q([1.1, 2.2, 3.3], "m")
>>> q.round(0)
Quantity(Array([1., 2., 3.], dtype=float32), unit='m')
Parameters:
Return type:

AbstractQuantity

property shape: tuple[int, ...]

Shape of the array.

property sharding: Any

Return the sharding configuration of the array.

Examples

>>> import unxt as u
>>> q = u.Q([1, 2, 3], "m")
>>> q.sharding
SingleDeviceSharding(device=..., memory_kind=...)
property size: int

Total number of elements.

Examples

>>> import unxt as u
>>> q = u.Q([1, 2, 3], "m")
>>> q.size
3
squeeze(*args: Any, **kwargs: Any)

Return the array with all single-dimensional entries removed.

Examples

>>> import unxt as u
>>> q = u.Q([[[1], [2], [3]]], "m")
>>> q.squeeze()
Quantity(Array([1, 2, 3], dtype=int32), unit='m')
Parameters:
Return type:

AbstractQuantity

to(u: Any, /)

Convert the quantity to the given units.

See unxt.quantity.AbstractQuantity.uconvert.

Examples

>>> from unxt import Quantity
>>> q = Quantity(1, "m")
>>> q.to("cm")
Quantity(Array(100., dtype=float32, ...), unit='cm')
Parameters:

u (Any)

Return type:

AbstractQuantity

to_device(device: None | Device = None)

Move the array to a new device.

Examples

>>> import unxt as u
>>> q = u.Q(1, "m")
>>> q.to_device(None)
Quantity(Array(1, dtype=int32...), unit='m')
Parameters:

device (None | Device)

Return type:

AbstractQuantity

to_value(u: Any, /)

Return the value in the given units.

See unxt.AbstractQuantity.ustrip.

Examples

>>> from unxt import Quantity
>>> q = Quantity(1, "m")
>>> q.to_value("cm")
Array(100., dtype=float32, weak_type=True)
Parameters:

u (Any)

Return type:

Union[Array, ndarray, bool, number, bool, int, float, complex]

uconvert(u: Any, /)

Convert the quantity to the given units.

See also

None

convert a quantity to a new unit.

Examples

>>> import unxt as u
>>> q = u.Q(1, "m")
>>> q.uconvert("cm")
Quantity(Array(100., dtype=float32, ...), unit='cm')
Parameters:

u (Any)

Return type:

AbstractQuantity

ustrip(u: Any, /)

Return the value in the given units.

See also

None

strip the units from a quantity.

Examples

>>> import unxt as u
>>> q = u.Q(1, "m")
>>> q.ustrip("cm")
Array(100., dtype=float32, weak_type=True)
Parameters:

u (Any)

Return type:

Array

unxt.uconvert_value(uto: Any, ufrom: Any, x: Any, /)

Convert the value from specified units to specified units.

General signature: (to_unit, from_unit, value) -> converted_value. Other signatures are defined via method dispatch. See uconvert_value.methods for details.

Examples

>>> import unxt as u  # implements `unxts.api.uconvert_value`
>>> u.uconvert_value(u.unit("m"), u.unit("km"), 1)
1000.0
>>> u.uconvert_value("m", "km", 1)
1000.0

For further examples, see the other method dispatches.

unxt.uconvert_value(tousys: AbstractUnitSystem, ufrom: UnitBase | FunctionUnitBase, x: ArrayLike, /) ArrayLike
Parameters:
Return type:

Any

Convert the value from units to a unitsystem’s preferred units.

Examples

>>> import unxt as u
>>> u.uconvert_value(u.unitsystems.galactic, u.unit("km"), 1e17)  # kpc
3.2407792894443648
>>> u.unitsystems.galactic[u.dimension("length")]  # checking the units
Unit("kpc")
unxt.uconvert_value(tousys: AbstractUnitSystem, ufrom: str, x: ArrayLike, /) ArrayLike
Parameters:
Return type:

Any

Convert the value from units to a unitsystem’s preferred units.

Examples

>>> import unxt as u
>>> u.uconvert_value(u.unitsystems.galactic, "km", 1e17)  # in kpc
3.2407792894443648
>>> u.unitsystems.galactic[u.dimension("length")]  # checking the units
Unit("kpc")
unxt.uconvert_value(uto: str, ufrom: str, x: ArrayLike, /) ArrayLike
Parameters:
Return type:

Any

Convert the value to the specified units.

Examples

>>> import unxt as u
>>> u.uconvert_value("m", "km", 1)
1000.0
unxt.uconvert_value(uto: Any, ufrom: Any, x: AbstractQuantity, /) AbstractQuantity
Parameters:
Return type:

Any

Convert the quantity to the specified units.

This is a convenience dispatch so that users can use the lower-level function with a Quantity and not break their code. This dispatch simply calls uconvert, checking first that the units are convertible.

Examples

>>> import unxt as u
>>> q = u.Q(1, "km")
>>> u.uconvert_value("m", "km", q)
Quantity(Array(1000., dtype=float32...), unit='m')
unxt.uconvert_value(uto: UnitBase | Unit | FunctionUnitBase | StructuredUnit, ufrom: UnitBase | Unit | FunctionUnitBase | StructuredUnit, x: ArrayLike, /) ArrayLike
Parameters:
Return type:

Any

Convert the value to the specified units.

Examples

>>> import astropy.units as apyu
>>> import unxt as u
>>> u.uconvert_value(apyu.Unit("km"), apyu.Unit("m"), 1000)
1.0
unxt.uconvert_value(uto: UnitBase | Unit | FunctionUnitBase | StructuredUnit, ufrom: UnitBase | Unit | FunctionUnitBase | StructuredUnit, x: Quantity, /) Quantity
Parameters:
Return type:

Any

Convert an astropy quantity to the specified units.

This is a convenience dispatch mirroring the AbstractQuantity one: users may pass a quantity to the lower-level value function and it keeps working. Like that dispatch, it checks that the quantity’s own unit is convertible to the caller-supplied ufrom – ~unxt.is_unit_convertible takes the target first, so is_unit_convertible(ufrom, x.unit) reads “x.unit -> ufrom” – then defers to the quantity’s own unit for the arithmetic, returning a relabelled quantity rather than a bare value.

This dispatch is required: an astropy ~astropy.units.Quantity subclasses numpy.ndarray, so it satisfies the x: ArrayLike annotation of the bare-value dispatch above and is not rejected even under beartype. That body’s ufrom.to(uto, x) converts the magnitude but returns a quantity still carrying ufrom’s unit, mislabelling the result (e.g. uconvert_value("m", "km", 1 km) gave 1000.0 km).

Examples

>>> import astropy.units as apyu
>>> import unxt as u
>>> u.uconvert_value(apyu.Unit("m"), apyu.Unit("km"), apyu.Quantity(1.0, "km"))
<Quantity 1000. m>
>>> u.uconvert_value(apyu.Unit("m"), apyu.Unit("m"), apyu.Quantity(5.0, "m"))
<Quantity 5. m>
Parameters:
Return type:

Any

unxt.uconvert(u: Any, x: Any, /)

Convert the quantity to the specified units.

General signature: (to_unit, quantity) -> converted_quantity. Other signatures are defined via method dispatch. See uconvert.methods for details.

Internally, {func}`unxts.api.uconvert` often calls to {func}`unxts.api.uconvert_value` to perform the numerical conversion on the Quantity’s value.

Examples

>>> import unxt as u  # implements `unxts.api.uconvert`
>>> q = u.Q(1, "km")
>>> u.uconvert(u.unit("m"), q)
Quantity(Array(1000., dtype=float32, ...), unit='m')
>>> u.uconvert("m", q)
Quantity(Array(1000., dtype=float32, ...), unit='m')

For further examples, see the other method dispatches.

unxt.uconvert(ustr: str, x: AbstractQuantity, /) AbstractQuantity
Parameters:
Return type:

Any

Convert the quantity to the specified units.

Examples

>>> from unxt import Quantity, units
>>> x = Quantity(1000, "m")
>>> uconvert("km", x)
Quantity(Array(1., dtype=float32...), unit='km')
unxt.uconvert(usys: AbstractUnitSystem, x: AbstractQuantity, /) AbstractQuantity
Parameters:
Return type:

Any

Convert the quantity to the specified units.

Examples

>>> from unxt import Quantity, units
>>> from unxt.unitsystems import galactic
>>> q = Quantity(1e17, "km")
>>> uconvert(galactic, q)
Quantity(Array(3.2407792, dtype=float32...), unit='kpc')
unxt.uconvert(u: UnitBase | Unit | FunctionUnitBase | StructuredUnit, x: AbstractQuantity, /) AbstractQuantity
Parameters:
Return type:

Any

Convert the quantity to the specified units.

Examples

>>> import astropy.units as apyu
>>> import unxt as u
>>> x = u.Q(1000, "m")
>>> u.uconvert(u.unit("km"), x)
Quantity(Array(1., dtype=float32, ...), unit='km')
>>> x = u.Q([1, 2, 3], "Kelvin")
>>> with apyu.add_enabled_equivalencies(apyu.temperature()):
...     y = x.uconvert("deg_C")
>>> y
Quantity( Array([-272.15, -271.15, -270.15], dtype=float32, ...), unit='deg_C' )
>>> x = u.Q([1, 2, 3], "radian")
>>> with apyu.add_enabled_equivalencies(apyu.dimensionless_angles()):
...     y = x.uconvert("")
>>> y
Quantity(Array([1., 2., 3.], dtype=float32, ...), unit='')
Parameters:
Return type:

Any

unxt.ustrip(*args: Any)

Strip the units from the quantity, first converting if necessary.

General signature: (to_unit, quantity) -> value_array. Other signatures are defined via method dispatch. See ustrip.methods for details.

Examples

>>> import unxt as u  # implements `unxts.api.ustrip`
>>> q = u.Q(1, "km")
>>> ustrip(u.unit("m"), q)
Array(1000., dtype=float32, ...)
>>> u.ustrip("m", q)
Array(1000., dtype=float32, ...)

For further examples, see the other method dispatches.

unxt.ustrip(flag: type[AllowValue], unit: Any, x: Any, /) Any
Parameters:

args (Any)

Return type:

Any

Strip the units from a value. This is a no-op.

Examples

>>> import jax.numpy as jnp
>>> import unxt as u
>>> from unxt.quantity import AllowValue
>>> x = jnp.array(1)
>>> y = u.ustrip(AllowValue, "km", x)
>>> y is x
True
>>> x = 1_000
>>> y = u.ustrip(AllowValue, "km", x)
>>> y is x
True
>>> x = "hello"
>>> y = u.ustrip(AllowValue, "km", x)
>>> y is x
True
unxt.ustrip(flag: type[AllowValue], x: Any, /) Any
Parameters:

args (Any)

Return type:

Any

Strip the units from a value. This is a no-op.

Examples

>>> import jax.numpy as jnp
>>> import unxt as u
>>> from unxt.quantity import AllowValue
>>> x = jnp.array(1)
>>> y = u.ustrip(AllowValue, x)
>>> y is x
True
>>> x = 1_000
>>> y = u.ustrip(AllowValue, x)
>>> y is x
True
>>> x = "hello"
>>> y = u.ustrip(AllowValue, x)
>>> y is x
True
unxt.ustrip(flag: type[AllowValue], unit: Any, x: AbstractQuantity, /) Any
Parameters:

args (Any)

Return type:

Any

Strip the units from a quantity.

Examples

>>> import unxt as u
>>> from unxt.quantity import AllowValue
>>> q = u.Q(1000, "m")
>>> u.ustrip(AllowValue, "km", q)
Array(1., dtype=float32, ...)
unxt.ustrip(flag: type[AllowValue], x: AbstractQuantity, /) Any
Parameters:

args (Any)

Return type:

Any

Strip the units from a quantity.

Examples

>>> import jax.numpy as jnp
>>> import unxt as u
>>> from unxt.quantity import AllowValue
>>> x = u.Q(1, "kpc")
>>> y = u.ustrip(AllowValue, x)
>>> not isinstance(y, u.Q)
True
>>> y == 1
Array(True, dtype=bool...)
unxt.ustrip(x: StaticQuantity, /) ndarray
Parameters:

args (Any)

Return type:

Any

Strip the units from a static quantity.

unxt.ustrip(x: AbstractQuantity, /) Array | ndarray
Parameters:

args (Any)

Return type:

Any

Strip the units from the quantity.

Examples

>>> import unxt as u
>>> q = u.Q(1000, "m")
>>> u.ustrip(q)
Array(1000, dtype=int32...)
>>> u.ustrip(q) is q.value
True
unxt.ustrip(u: UnitBase | FunctionUnitBase, x: AbstractQuantity, /) Array | ndarray
Parameters:

args (Any)

Return type:

Any

Strip the units from the quantity.

Examples

>>> import unxt as u
>>> q = u.Q(1000, "m")
>>> u.ustrip(u.unit("km"), q)
Array(1., dtype=float32...)
unxt.ustrip(u: str, x: AbstractQuantity, /) Array | ndarray
Parameters:

args (Any)

Return type:

Any

Strip the units from the quantity.

Examples

>>> import unxt as u
>>> q = u.Q(1000, "m")
>>> u.ustrip("km", q)
Array(1., dtype=float32...)
unxt.ustrip(u: AbstractUnitSystem, x: AbstractQuantity, /) Array | ndarray
Parameters:

args (Any)

Return type:

Any

Strip the units from the quantity.

Examples

>>> import unxt as u
>>> from unxt.unitsystems import galactic
>>> q = u.Q(1e17, "km")
>>> u.ustrip(galactic, q)
Array(3.2407792, dtype=float32...)
unxt.ustrip(u: Any, x: Quantity) Any
Parameters:

args (Any)

Return type:

Any

Strip the units from the quantity.

Examples

>>> import astropy.units as apyu
>>> import unxt as u
>>> x = apyu.Quantity(1000, "m")
>>> float(u.ustrip(u.unit("m"), x))
1000.0
unxt.ustrip(flag: type[AllowValue], u: Any, x: Quantity, /) Any
Parameters:

args (Any)

Return type:

Any

Strip the units from a quantity.

Examples

>>> import astropy.units as apyu
>>> import unxt as u
>>> from unxt.quantity import AllowValue
>>> q = apyu.Quantity(1000, "m")
>>> float(u.ustrip(AllowValue, "km", q))
1.0
unxt.ustrip(flag: type[AllowValue], x: Quantity, /) Any
Parameters:

args (Any)

Return type:

Any

Strip the units from an astropy quantity, allowing bare values through.

The two-argument AllowValue form takes no target unit, so it returns the value in the quantity’s own unit. This dispatch disambiguates ustrip(AllowValue, <astropy Quantity>), which otherwise matched both ustrip(type[AllowValue], Any) and ustrip(Any, AstropyQuantity) with neither dominating. It mirrors the (type[AllowValue], AbstractQuantity) form for unxt quantities.

Examples

>>> import astropy.units as apyu
>>> import unxt as u
>>> from unxt.quantity import AllowValue
>>> q = apyu.Quantity(1000, "m")
>>> float(u.ustrip(AllowValue, q))
1000.0
Parameters:

args (Any)

Return type:

Any

unxt.is_unit_convertible(to_unit: Any, from_: Any, /)

Check if the units are convertible.

General signature is (to_unit, from_unit) -> bool. Other signatures are defined via method dispatch. See is_unit_convertible.methods for details.

Examples

>>> import unxt as u  # implements `unxts.api.is_unit_convertible`
>>> u.is_unit_convertible(u.unit("m"), u.unit("km"))
True
>>> u.is_unit_convertible(u.unit("m"), u.unit("s"))
False
unxt.is_unit_convertible(to_unit: Any, from_: Any, /) bool
Parameters:
Return type:

bool

Check if a unit can be converted to another unit.

Parameters:
  • to_unit (Any) – The unit to convert to. Converted to a unit object using unxt.unit.

  • from_ (Any) – The unit to convert from. Converted to a unit object using unxt.unit, Note this means it also support Quantity objects and many others.

Return type:

bool

Examples

>>> from unxt import is_unit_convertible
>>> is_unit_convertible("cm", "m")
True
>>> is_unit_convertible("m", "Gyr")
False
unxt.is_unit_convertible(to_unit: Any, from_: AbstractQuantity, /) bool
Parameters:
Return type:

bool

Check if a quantity can be converted to another unit.

Examples

>>> from unxt import Quantity, is_unit_convertible
>>> q = Quantity(1, "m")
>>> is_unit_convertible("cm", q)
True
>>> is_unit_convertible("Gyr", q)
False
unxt.equivalent(a: AbstractUnitSystem, b: AbstractUnitSystem, /)

Check if two unit systems are equivalent.

unxt.equivalent(a: AbstractQuantity, b: AbstractQuantity, /) Any
Parameters:
Return type:

bool

Whether two quantities are physically equal, accounting for units.

This is the unit-aware counterpart to == (which is unit-blind for StaticValue-backed quantities – see AbstractQuantity.__eq__). The result mirrors ==’s shape: a scalar bool for StaticValue-backed operands, and an element-wise dimensionless Quantity of booleans for array-backed ones. Quantities with incompatible dimensions are never equivalent (and this never raises).

Examples

>>> import numpy as np
>>> import unxt as u
>>> from unxt.quantity import StaticValue

Physically-equal static quantities in different units are equivalent, even though unit-blind == reports False:

>>> a = u.Q(StaticValue(np.array([1.0, 2.0])), "m")
>>> b = u.Q(StaticValue(np.array([0.001, 0.002])), "km")
>>> a == b
False
>>> u.equivalent(a, b)
True
>>> a.is_equivalent(b)
True

Array-backed quantities compare element-wise (unit-aware):

>>> u.equivalent(u.Q([1.0, 2.0], "m"), u.Q([0.001, 0.009], "km"))
Quantity(Array([ True, False], dtype=bool), unit='')

Incompatible dimensions are never equivalent:

>>> u.equivalent(u.Q(1.0, "m"), u.Q(1.0, "s"))
False
Parameters:
Return type:

bool