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.BareQuantity)
... except ValueError as e:
...     print(e)
Cannot get the dimension of <class 'unxt._src.quantity.unchecked.BareQuantity'>.
unxt.dimension_of(obj: Unit | UnitBase | CompositeUnit, /) 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[AbstractParametricQuantity], /) PhysicalType
Parameters:

obj (Any)

Return type:

Any

Return the dimension of a quantity.

Examples

>>> import unxt as u
>>> try:
...     u.dimension_of(u.Quantity)
... except Exception as e:
...     print(e)
can only get dimensions from parametrized Quantity -- Quantity[dim].
>>> u.dimension_of(u.Quantity["length"])
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 | 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: str, /) Unit | UnitBase | CompositeUnit
Parameters:

obj (Any)

Return type:

Any

Construct units from a string.

Examples

>>> import unxt as u
>>> m = u.unit("m")
>>> m
Unit("m")
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 | 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: AbstractQuantity, /) Unit | UnitBase | CompositeUnit
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: 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[Unit | UnitBase | CompositeUnit, ...]

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)
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"))
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.Quantity(value: Any, unit: Any)

Bases: Quantity

Arrays with associated units.

This class is parametrized by the dimensions of the units.

short_name

Short name ‘Q’ used for compact wadler-lindig printing.

Type:

str

Examples

>>> import unxt as u

From an integer:

>>> u.Quantity(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.Quantity([1, 2, 3], "m")
Quantity(Array([1, 2, 3], dtype=int32), unit='m')

From a tuple:

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

From a numpy.ndarray:

>>> import numpy as np
>>> u.Quantity(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.Quantity(jnp.array([1, 2, 3]), "m")
Quantity(Array([1, 2, 3], dtype=int32), unit='m')

The unit can also be given as a units object:

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

In the previous examples, the dimension parameter was inferred from the values. It can also be given explicitly:

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

This can be used for runtime checking of the input dimension!

>>> try:
...     u.Quantity["length"](1, "s")
... except Exception as e:
...     print(e)
Physical type mismatch.

The dimension can also be given as a dimension object:

>>> dims = u.dimension("length")
>>> dims
PhysicalType('length')
>>> u.Quantity[dims](1.0, "m")
Quantity(Array(1., dtype=float32, ...), unit='m')

Or as a unit:

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

Some tricky cases are when the physical type is unknown:

>>> unit = u.unit("m2 / (kg s2)")
>>> u.dimension_of(unit)
PhysicalType('unknown')

The dimension can be given as a string in all cases, but is necessary when the physical type is unknown:

>>> print(u.Quantity["m2 kg-1 s-2"](1.0, unit))  # to show the [dim]
Quantity['m2 kg-1 s-2'](1., unit='m2 / (kg s2)')
Parameters:
property T: AbstractQuantity

Transpose of the array.

Examples

>>> import unxt as u
>>> q = u.Quantity([[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.Quantity([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.Quantity([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.Quantity([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.Quantity(1, "m")
>>> q.block_until_ready() is q
True
decompose(bases: Sequence[Unit | UnitBase | CompositeUnit | 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[Unit | UnitBase | CompositeUnit | str])

Return type:

AbstractQuantity

property device: Device

Device where the array is located.

Examples

>>> import unxt as u
>>> u.Quantity(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.Quantity(1, "m")
>>> q.devices()
{CpuDevice(id=0)}
property dtype: dtype

Data type of the array.

Examples

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

Return a flattened version of the array.

Return type:

AbstractQuantity

Examples

>>> import unxt as u
>>> q = u.Quantity([[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 | Shaped[ArrayLike, '']] | tuple[Shaped[Array, ''] | Shaped[ndarray, ''] | bool | number | bool | int | float | complex | Shaped[ArrayLike, ''], ...], unit: Any, /, *, dtype: Any = None) AbstractQuantity
Parameters:
Return type:

AbstractQuantity

Construct a unxt.Quantity 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.Quantity.from_(x, "m")
Quantity(Array([1., 2., 3.], dtype=float32), unit='m')
>>> u.Quantity.from_([1.0, 2, 3], "m")
Quantity(Array([1., 2., 3.], dtype=float32), unit='m')
>>> u.Quantity.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 | Shaped[ArrayLike, '']] | tuple[Shaped[Array, ''] | Shaped[ndarray, ''] | bool | number | bool | int | float | complex | Shaped[ArrayLike, ''], ...], /, *, 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.Quantity.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.Quantity.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.Quantity.from_({"value": x, "unit": "m"})
>>> q
Quantity(Array([1., 2., 3.], dtype=float32), unit='m')
>>> u.Quantity.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.Quantity(1, "m")
>>> u.Quantity.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.Quantity(1, "m")
>>> u.Quantity.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, with no unit change.

from_(cls: type[AbstractQuantity], value: Quantity, /, **kwargs: Any) AbstractQuantity
Parameters:
Return type:

AbstractQuantity

Construct a Quantity from another Quantity.

The value is converted to the new unit.

Examples

>>> import unxt as u
>>> import astropy.units as apyu
>>> u.Quantity.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 another Quantity.

The value is converted to the new unit.

Examples

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

AbstractQuantity

property mT: AbstractQuantity

Matrix transpose of the array.

Examples

>>> import unxt as u
>>> q = u.Quantity([[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.Quantity([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.Quantity([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.Quantity([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.Quantity([[1]], "m")
>>> q.ndim
2
ravel()

Return a flattened version of the array.

Return type:

AbstractQuantity

Examples

>>> import unxt as u
>>> q = u.Quantity([[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.Quantity([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.Quantity([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.Quantity([1, 2, 3], "m")
>>> q.sharding
SingleDeviceSharding(device=..., memory_kind=...)
short_name: ClassVar[str] = 'Q'

Short name for compact printing.

property size: int

Total number of elements.

Examples

>>> import unxt as u
>>> q = u.Quantity([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.Quantity([[[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.Quantity(1, "m")
>>> q.to_device(None)
Quantity(Array(1, dtype=int32, weak_type=True), 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, ArrayLike]

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.Quantity(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.Quantity(1, "m")
>>> q.ustrip("cm")
Array(100., dtype=float32, weak_type=True)
Parameters:

u (Any)

Return type:

Array

value: Shaped[Array, '*shape'] | StaticValue

The value of the AbstractQuantity.

unit: AbstractUnit

The unit associated with this value.

unxt.Q

alias of Quantity

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

Bases: StaticQuantity

Static quantities with associated units.

This class is parametrized by the dimensions of the units, just like {class}`~unxt.quantity.Quantity`, but its value is always stored as a static NumPy array. It accepts Python scalars and array-like inputs that can be converted to NumPy arrays, and it rejects JAX arrays.

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

JAX arrays are rejected:

>>> import jax.numpy as jnp
>>> try:
...     u.StaticQuantity(jnp.array([1.0, 2.0]), "m")
... except TypeError as e:
...     print(e)
StaticQuantity does not accept JAX arrays. Use Quantity for traced values.

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:
property T: AbstractQuantity

Transpose of the array.

Examples

>>> import unxt as u
>>> q = u.Quantity([[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.Quantity([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.Quantity([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.Quantity([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.Quantity(1, "m")
>>> q.block_until_ready() is q
True
decompose(bases: Sequence[Unit | UnitBase | CompositeUnit | 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[Unit | UnitBase | CompositeUnit | str])

Return type:

AbstractQuantity

property device: Device

Device where the array is located.

Examples

>>> import unxt as u
>>> u.Quantity(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.Quantity(1, "m")
>>> q.devices()
{CpuDevice(id=0)}
property dtype: dtype

Data type of the array.

Examples

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

Return a flattened version of the array.

Return type:

AbstractQuantity

Examples

>>> import unxt as u
>>> q = u.Quantity([[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 | Shaped[ArrayLike, '']] | tuple[Shaped[Array, ''] | Shaped[ndarray, ''] | bool | number | bool | int | float | complex | Shaped[ArrayLike, ''], ...], unit: Any, /, *, dtype: Any = None) AbstractQuantity
Parameters:
Return type:

AbstractQuantity

Construct a unxt.Quantity 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.Quantity.from_(x, "m")
Quantity(Array([1., 2., 3.], dtype=float32), unit='m')
>>> u.Quantity.from_([1.0, 2, 3], "m")
Quantity(Array([1., 2., 3.], dtype=float32), unit='m')
>>> u.Quantity.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 | Shaped[ArrayLike, '']] | tuple[Shaped[Array, ''] | Shaped[ndarray, ''] | bool | number | bool | int | float | complex | Shaped[ArrayLike, ''], ...], /, *, 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.Quantity.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.Quantity.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.Quantity.from_({"value": x, "unit": "m"})
>>> q
Quantity(Array([1., 2., 3.], dtype=float32), unit='m')
>>> u.Quantity.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.Quantity(1, "m")
>>> u.Quantity.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.Quantity(1, "m")
>>> u.Quantity.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, with no unit change.

from_(cls: type[AbstractQuantity], value: Quantity, /, **kwargs: Any) AbstractQuantity
Parameters:
Return type:

AbstractQuantity

Construct a Quantity from another Quantity.

The value is converted to the new unit.

Examples

>>> import unxt as u
>>> import astropy.units as apyu
>>> u.Quantity.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 another Quantity.

The value is converted to the new unit.

Examples

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

AbstractQuantity

property mT: AbstractQuantity

Matrix transpose of the array.

Examples

>>> import unxt as u
>>> q = u.Quantity([[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.Quantity([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.Quantity([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.Quantity([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.Quantity([[1]], "m")
>>> q.ndim
2
ravel()

Return a flattened version of the array.

Return type:

AbstractQuantity

Examples

>>> import unxt as u
>>> q = u.Quantity([[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.Quantity([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.Quantity([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.Quantity([1, 2, 3], "m")
>>> q.sharding
SingleDeviceSharding(device=..., memory_kind=...)
property size: int

Total number of elements.

Examples

>>> import unxt as u
>>> q = u.Quantity([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.Quantity([[[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.Quantity(1, "m")
>>> q.to_device(None)
Quantity(Array(1, dtype=int32, weak_type=True), 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, ArrayLike]

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.Quantity(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.Quantity(1, "m")
>>> q.ustrip("cm")
Array(100., dtype=float32, weak_type=True)
Parameters:

u (Any)

Return type:

Array

value: StaticValue

The static value of the AbstractQuantity.

unit: AbstractUnit

The unit associated with this value.

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 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.Quantity(1, "m")
Quantity(Array(1, dtype=int32, ...), unit='m')

From a float:

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

From a list:

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

From a tuple:

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

From a numpy.ndarray:

>>> import numpy as np
>>> u.Quantity(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.Quantity(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.Quantity(1, apyu.m)
Quantity(Array(1, dtype=int32, ...), unit='m')
property T: AbstractQuantity

Transpose of the array.

Examples

>>> import unxt as u
>>> q = u.Quantity([[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.Quantity([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.Quantity([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.Quantity([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.Quantity(1, "m")
>>> q.block_until_ready() is q
True
decompose(bases: Sequence[Unit | UnitBase | CompositeUnit | 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[Unit | UnitBase | CompositeUnit | str])

Return type:

AbstractQuantity

property device: Device

Device where the array is located.

Examples

>>> import unxt as u
>>> u.Quantity(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.Quantity(1, "m")
>>> q.devices()
{CpuDevice(id=0)}
property dtype: dtype

Data type of the array.

Examples

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

Return a flattened version of the array.

Return type:

AbstractQuantity

Examples

>>> import unxt as u
>>> q = u.Quantity([[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 | Shaped[ArrayLike, '']] | tuple[Shaped[Array, ''] | Shaped[ndarray, ''] | bool | number | bool | int | float | complex | Shaped[ArrayLike, ''], ...], unit: Any, /, *, dtype: Any = None) AbstractQuantity
Parameters:
Return type:

AbstractQuantity

Construct a unxt.Quantity 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.Quantity.from_(x, "m")
Quantity(Array([1., 2., 3.], dtype=float32), unit='m')
>>> u.Quantity.from_([1.0, 2, 3], "m")
Quantity(Array([1., 2., 3.], dtype=float32), unit='m')
>>> u.Quantity.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 | Shaped[ArrayLike, '']] | tuple[Shaped[Array, ''] | Shaped[ndarray, ''] | bool | number | bool | int | float | complex | Shaped[ArrayLike, ''], ...], /, *, 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.Quantity.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.Quantity.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.Quantity.from_({"value": x, "unit": "m"})
>>> q
Quantity(Array([1., 2., 3.], dtype=float32), unit='m')
>>> u.Quantity.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.Quantity(1, "m")
>>> u.Quantity.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.Quantity(1, "m")
>>> u.Quantity.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, with no unit change.

from_(cls: type[AbstractQuantity], value: Quantity, /, **kwargs: Any) AbstractQuantity
Parameters:
Return type:

AbstractQuantity

Construct a Quantity from another Quantity.

The value is converted to the new unit.

Examples

>>> import unxt as u
>>> import astropy.units as apyu
>>> u.Quantity.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 another Quantity.

The value is converted to the new unit.

Examples

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

AbstractQuantity

property mT: AbstractQuantity

Matrix transpose of the array.

Examples

>>> import unxt as u
>>> q = u.Quantity([[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.Quantity([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.Quantity([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.Quantity([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.Quantity([[1]], "m")
>>> q.ndim
2
ravel()

Return a flattened version of the array.

Return type:

AbstractQuantity

Examples

>>> import unxt as u
>>> q = u.Quantity([[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.Quantity([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.Quantity([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.Quantity([1, 2, 3], "m")
>>> q.sharding
SingleDeviceSharding(device=..., memory_kind=...)
property size: int

Total number of elements.

Examples

>>> import unxt as u
>>> q = u.Quantity([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.Quantity([[[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.Quantity(1, "m")
>>> q.to_device(None)
Quantity(Array(1, dtype=int32, weak_type=True), 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, ArrayLike]

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.Quantity(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.Quantity(1, "m")
>>> q.ustrip("cm")
Array(100., dtype=float32, weak_type=True)
Parameters:

u (Any)

Return type:

Array

value: AbstractVar[Union[Shaped[Array, '*shape'], Shaped[StaticValue, '*shape']]]

The value of the AbstractQuantity.

unit: AbstractVar[Unit | UnitBase | CompositeUnit]

The unit associated with this value.

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:
property T: AbstractQuantity

Transpose of the array.

Examples

>>> import unxt as u
>>> q = u.Quantity([[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.Quantity([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.Quantity([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.Quantity([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.Quantity(1, "m")
>>> q.block_until_ready() is q
True
decompose(bases: Sequence[Unit | UnitBase | CompositeUnit | 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[Unit | UnitBase | CompositeUnit | str])

Return type:

AbstractQuantity

property device: Device

Device where the array is located.

Examples

>>> import unxt as u
>>> u.Quantity(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.Quantity(1, "m")
>>> q.devices()
{CpuDevice(id=0)}
property dtype: dtype

Data type of the array.

Examples

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

Return a flattened version of the array.

Return type:

AbstractQuantity

Examples

>>> import unxt as u
>>> q = u.Quantity([[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 | Shaped[ArrayLike, '']] | tuple[Shaped[Array, ''] | Shaped[ndarray, ''] | bool | number | bool | int | float | complex | Shaped[ArrayLike, ''], ...], unit: Any, /, *, dtype: Any = None) AbstractQuantity
Parameters:
Return type:

AbstractQuantity

Construct a unxt.Quantity 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.Quantity.from_(x, "m")
Quantity(Array([1., 2., 3.], dtype=float32), unit='m')
>>> u.Quantity.from_([1.0, 2, 3], "m")
Quantity(Array([1., 2., 3.], dtype=float32), unit='m')
>>> u.Quantity.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 | Shaped[ArrayLike, '']] | tuple[Shaped[Array, ''] | Shaped[ndarray, ''] | bool | number | bool | int | float | complex | Shaped[ArrayLike, ''], ...], /, *, 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.Quantity.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.Quantity.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.Quantity.from_({"value": x, "unit": "m"})
>>> q
Quantity(Array([1., 2., 3.], dtype=float32), unit='m')
>>> u.Quantity.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.Quantity(1, "m")
>>> u.Quantity.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.Quantity(1, "m")
>>> u.Quantity.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, with no unit change.

from_(cls: type[AbstractQuantity], value: Quantity, /, **kwargs: Any) AbstractQuantity
Parameters:
Return type:

AbstractQuantity

Construct a Quantity from another Quantity.

The value is converted to the new unit.

Examples

>>> import unxt as u
>>> import astropy.units as apyu
>>> u.Quantity.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 another Quantity.

The value is converted to the new unit.

Examples

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

AbstractQuantity

property mT: AbstractQuantity

Matrix transpose of the array.

Examples

>>> import unxt as u
>>> q = u.Quantity([[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.Quantity([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.Quantity([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.Quantity([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.Quantity([[1]], "m")
>>> q.ndim
2
ravel()

Return a flattened version of the array.

Return type:

AbstractQuantity

Examples

>>> import unxt as u
>>> q = u.Quantity([[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.Quantity([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.Quantity([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.Quantity([1, 2, 3], "m")
>>> q.sharding
SingleDeviceSharding(device=..., memory_kind=...)
property size: int

Total number of elements.

Examples

>>> import unxt as u
>>> q = u.Quantity([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.Quantity([[[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.Quantity(1, "m")
>>> q.to_device(None)
Quantity(Array(1, dtype=int32, weak_type=True), 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, ArrayLike]

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.Quantity(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.Quantity(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.

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')
value: Union[Real[Array, '*shape'], Real[StaticValue, '*shape']]

The value of the unxt.AbstractQuantity.

unit: Unit | UnitBase | CompositeUnit

The unit associated with this value.

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 `unxt_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: Unit | UnitBase | CompositeUnit, 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 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
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}`unxt_api.uconvert` often calls to {func}`unxt_api.uconvert_value` to perform the numerical conversion on the Quantity’s value.

Examples

>>> import unxt as u  # implements `unxt_api.uconvert`
>>> q = u.Quantity(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.Quantity(1000, "m")
>>> u.uconvert(u.unit("km"), x)
Quantity(Array(1., dtype=float32, ...), unit='km')
>>> x = u.Quantity([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 = Quantity([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 `unxt_api.ustrip`
>>> q = u.Quantity(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.Quantity)
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, weak_type=True)
>>> u.ustrip(q) is q.value
True
unxt.ustrip(u: Unit | UnitBase | CompositeUnit, 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, weak_type=True)
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 unxt as u
>>> q = u.Quantity(1000, "m")
>>> u.ustrip(AllowValue, "km", q)
Array(1., dtype=float32, ...)
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 `unxt_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.

  • from_ (Any)

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
Parameters:
Return type:

bool