coordinax.distances#
coordinax.distances module.
- class coordinax.distances.AbstractDistance#
Bases:
AbstractQuantityDistance quantities.
- property distance: AbstractDistance#
The distance.
Examples
>>> import coordinax.distances as cxd >>> d = cxd.Distance(10, "km") >>> d.distance is d True
>>> import coordinaxs.astro as cxastro >>> cxastro.DistanceModulus(10, "mag").distance Distance(1000., 'pc')
>>> p = cxastro.Parallax(1, "mas") >>> p.distance.to("kpc") Distance(1., 'kpc')
- 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)
- 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)
- 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:
- property at: _QuantityIndexUpdateHelper#
Helper property for index update functionality.
The
atproperty 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] = yx = x.at[idx].add(y)x[idx] += yx = x.at[idx].subtract(y)x[idx] -= yx = x.at[idx].multiply(y)x[idx] *= yx = x.at[idx].divide(y)x[idx] /= yx = x.at[idx].power(y)x[idx] **= yx = 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.atexpressions modify the originalx; instead they return a modified copy ofx. However, inside ajit()compiled function, expressions likex = 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
modeparameter (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 inget()will be clipped, and out-of-bounds indices inset(),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 optionalfill_valueargument specifies the value that will be returned.
See
jax.lax.GatherScatterModefor 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
modeparameter.fill_value β Only applies to the
get()method: the fill value to return for out-of-bounds slices whenmodeis'fill'. Ignored otherwise. Defaults toNaNfor inexact types, the largest negative value for signed types, the largest positive value for unsigned types, andTruefor 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
modeparameter:>>> 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:
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:
- 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.
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:
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_(*args, **kwargs)#
- from_(cls: type[AbstractQuantity], value: ArrayLike | list[jaxtyping.Shaped[Array, ''] | jaxtyping.Shaped[ndarray, ''] | bool | number | bool | int | float | complex] | tuple[jaxtyping.Shaped[Array, ''] | jaxtyping.Shaped[ndarray, ''] | bool | number | bool | int | float | complex, ...], unit: Any, /, *, dtype: Any = None) AbstractQuantity
Construct a unxt.AbstractQuantity from an array-like value and a unit.
- Parameters:
- Return type:
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[jaxtyping.Shaped[Array, ''] | jaxtyping.Shaped[ndarray, ''] | bool | number | bool | int | float | complex] | tuple[jaxtyping.Shaped[Array, ''] | jaxtyping.Shaped[ndarray, ''] | bool | number | bool | int | float | complex, ...], /, *, unit: Any, dtype: Any = None) 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
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
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
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
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
Construct a quantity from another quantity.
The unit is unchanged.
- from_(cls: type[StaticQuantity], value: ArrayLike | list[jaxtyping.Shaped[Array, ''] | jaxtyping.Shaped[ndarray, ''] | bool | number | bool | int | float | complex] | tuple[jaxtyping.Shaped[Array, ''] | jaxtyping.Shaped[ndarray, ''] | bool | number | bool | int | float | complex, ...], unit: Any, /, *, dtype: Any = None) StaticQuantity
Construct a StaticQuantity, keeping the value on NumPy dtypes.
The generic
AbstractQuantity.from_routes the value throughjnp.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 theStaticValue.from_converter convert it β that preserves the NumPy dtype. Delegating (rather than callingnp.asarrayhere) also keeps.from_and__init__under the same policy for JAX inputs, instead of materialising an array the constructor would reject. (The keyword-unitoverload 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
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
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')
Construct a distance.
>>> import unxt as u >>> import coordinax.distances as cxd >>> cxd.Distance.from_(1, "kpc") Distance(1, 'kpc')
Compute distance from distance.
>>> import unxt as u >>> import coordinax.distances as cxd
>>> d = cxd.Distance(1, "kpc") >>> cxd.Distance.from_(d) is d True
>>> cxd.Distance.from_(d, dtype=float) Distance(1., 'kpc')
- from_(cls: type[Distance], q: AbstractQuantity, /, **kw: Any) Distance
Construct a distance from a quantity, dispatching on its dimensions.
From a length quantity:
>>> import unxt as u >>> import coordinax.distances as cxd >>> q = u.Q(1, "kpc") >>> cxd.Distance.from_(q, dtype=float) Distance(1., 'kpc')
From a parallax angle:
>>> q = u.Q(1, "mas") >>> cxd.Distance.from_(q).uconvert("pc").round(2) Distance(1000., 'pc')
From a distance modulus:
>>> q = u.Q(10, "mag") >>> cxd.Distance.from_(q).uconvert("pc").round(2) Distance(1000., 'pc')
- from_(cls: type[DistanceModulus], value: ArrayLike, unit: Any, /, **kw: Any) DistanceModulus
Construct a distance.
>>> import unxt as u >>> from coordinaxs.astro import DistanceModulus
>>> DistanceModulus.from_(1, "mag") DistanceModulus(1, 'mag')
- from_(cls: type[DistanceModulus], dm: DistanceModulus, /, **kw: Any) DistanceModulus
Compute distance modulus from distance modulus.
>>> import unxt as u >>> from coordinaxs.astro import DistanceModulus
>>> dm = DistanceModulus(1, "mag") >>> DistanceModulus.from_(dm) is dm True
>>> DistanceModulus.from_(dm, dtype=float) DistanceModulus(1., 'mag')
- from_(cls: type[DistanceModulus], q: AbstractQuantity, /, **kw: Any) DistanceModulus
Construct a distance modulus from a quantity, dispatching on dimensions.
From a distance:
>>> import unxt as u >>> import coordinax.distances as cxd >>> from coordinaxs.astro import DistanceModulus
>>> d = cxd.Distance(1, "pc") >>> DistanceModulus.from_(d) DistanceModulus(-5., 'mag')
>>> q = u.Q(1, "pc") >>> DistanceModulus.from_(q) DistanceModulus(-5., 'mag')
From a parallax angle:
>>> q = u.Q(1, "mas") >>> DistanceModulus.from_(q) DistanceModulus(10., 'mag')
From a distance modulus (magnitude):
>>> q = u.Q(1, "mag") >>> DistanceModulus.from_(q) DistanceModulus(1, 'mag')
- from_(cls: type[Distance], dm: DistanceModulus, /, **kw: Any) Distance
Compute distance from distance modulus.
>>> import coordinax.distances as cxd >>> from coordinaxs.astro import DistanceModulus
>>> dm = DistanceModulus(10, "mag") >>> cxd.Distance.from_(dm).uconvert("pc").round(2) Distance(1000., 'pc')
Construct a distance.
>>> import unxt as u >>> from coordinaxs.astro import Parallax
>>> Parallax.from_(1, "mas") Parallax(1, 'mas')
Compute parallax from parallax.
>>> import unxt as u >>> from coordinaxs.astro import Parallax
>>> p = Parallax(1, "mas") >>> Parallax.from_(p) is p True
>>> Parallax.from_(p, dtype=float) Parallax(1., 'mas')
- from_(cls: type[Parallax], q: AbstractQuantity, /, **kw: Any) Parallax
Construct a parallax from a quantity, dispatching on its dimensions.
From a parallax angle:
>>> import unxt as u >>> import coordinax.distances as cxd >>> from coordinaxs.astro import Parallax
>>> q = u.Q(1, "mas") >>> Parallax.from_(q, dtype=float) Parallax(1., 'mas')
From a distance:
>>> d = cxd.Distance(10, "pc") >>> Parallax.from_(d).uconvert("mas").round(2) Parallax(100., 'mas')
>>> q = u.Q(10, "pc") >>> Parallax.from_(q).uconvert("mas").round(2) Parallax(100., 'mas')
From a distance modulus:
>>> dm = u.Q(10, "mag") >>> Parallax.from_(dm).uconvert("mas").round(2) Parallax(1., 'mas')
Compute distance from parallax.
>>> import coordinax.distances as cxd >>> from coordinaxs.astro import Parallax
>>> p = Parallax(1, "mas") >>> cxd.Distance.from_(p).uconvert("pc").round(2) Distance(1000., 'pc')
- from_(cls: type[DistanceModulus], p: Parallax, /, **kw: Any) DistanceModulus
Compute distance modulus from parallax.
>>> from coordinaxs.astro import DistanceModulus, Parallax >>> p = Parallax(1, "mas") >>> DistanceModulus.from_(p) DistanceModulus(10., 'mag')
- from_(cls: type[Parallax], dm: DistanceModulus, /, **kw: Any) Parallax
Convert distance modulus to parallax.
>>> import unxt as u >>> from coordinaxs.astro import DistanceModulus, Parallax >>> dm = DistanceModulus(10, "mag") >>> Parallax.from_(dm).uconvert("mas").round(2) Parallax(1., 'mas')
- Parameters:
cls (
type[AbstractQuantity])args (
Any)kwargs (
Any)
- Return type:
AbstractQuantity
- is_equivalent(other: AbstractQuantity, /)#
Whether
selfandotherare 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:
- property mT: AbstractQuantity#
Matrix transpose of the array.
Examples
>>> import unxt as u >>> q = u.Q([[0, 1], [2, 3]], "m") >>> q.mT Quantity(Array([[0, 2], [1, 3]], dtype=int32), unit='m')
It also works for a
StaticQuantity:>>> u.StaticQuantity([[0, 1], [2, 3]], "m").mT.value.tolist() [[0, 2], [1, 3]]
- 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:
- 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:
- 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:
- 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:
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:
- 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:
- 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:
- 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:
- 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:
- Return type:
- 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)
- uconvert(u: Any, /)#
Convert the quantity to the given units.
See also
Noneconvert 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:
- ustrip(u: Any, /)#
Return the value in the given units.
See also
Nonestrip 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)
- value: AbstractVar[Shaped[Array, '*shape'] | Shaped[StaticValue, '*shape']]#
The value of the AbstractQuantity.
- unit: AbstractVar[UnitBase | FunctionUnitBase]#
The unit associated with this value.
- final class coordinax.distances.Distance(value: Any, unit: Any, *, check_negative: bool = True)#
Bases:
AbstractDistanceDistance quantities.
The distance is a quantity with dimensions of length.
Examples
>>> import coordinax.distances as cxd >>> cxd.Distance(10, "km") Distance(10, 'km')
The units are checked to have length dimensions.
>>> try: cxd.Distance(10, "s") ... except ValueError as e: print(e) Distance must have dimensions length.
- 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)
- 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)
- 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:
- property at: _QuantityIndexUpdateHelper#
Helper property for index update functionality.
The
atproperty 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] = yx = x.at[idx].add(y)x[idx] += yx = x.at[idx].subtract(y)x[idx] -= yx = x.at[idx].multiply(y)x[idx] *= yx = x.at[idx].divide(y)x[idx] /= yx = x.at[idx].power(y)x[idx] **= yx = 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.atexpressions modify the originalx; instead they return a modified copy ofx. However, inside ajit()compiled function, expressions likex = 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
modeparameter (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 inget()will be clipped, and out-of-bounds indices inset(),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 optionalfill_valueargument specifies the value that will be returned.
See
jax.lax.GatherScatterModefor 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
modeparameter.fill_value β Only applies to the
get()method: the fill value to return for out-of-bounds slices whenmodeis'fill'. Ignored otherwise. Defaults toNaNfor inexact types, the largest negative value for signed types, the largest positive value for unsigned types, andTruefor 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
modeparameter:>>> 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:
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:
- 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.
Examples
>>> import unxt as u >>> q = u.Q(1, "m") >>> q.devices() {CpuDevice(id=0)}
- property distance: AbstractDistance#
The distance.
Examples
>>> import coordinax.distances as cxd >>> d = cxd.Distance(10, "km") >>> d.distance is d True
>>> import coordinaxs.astro as cxastro >>> cxastro.DistanceModulus(10, "mag").distance Distance(1000., 'pc')
>>> p = cxastro.Parallax(1, "mas") >>> p.distance.to("kpc") Distance(1., 'kpc')
- 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:
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_(*args, **kwargs)#
- from_(cls: type[AbstractQuantity], value: ArrayLike | list[jaxtyping.Shaped[Array, ''] | jaxtyping.Shaped[ndarray, ''] | bool | number | bool | int | float | complex] | tuple[jaxtyping.Shaped[Array, ''] | jaxtyping.Shaped[ndarray, ''] | bool | number | bool | int | float | complex, ...], unit: Any, /, *, dtype: Any = None) AbstractQuantity
Construct a unxt.AbstractQuantity from an array-like value and a unit.
- Parameters:
- Return type:
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[jaxtyping.Shaped[Array, ''] | jaxtyping.Shaped[ndarray, ''] | bool | number | bool | int | float | complex] | tuple[jaxtyping.Shaped[Array, ''] | jaxtyping.Shaped[ndarray, ''] | bool | number | bool | int | float | complex, ...], /, *, unit: Any, dtype: Any = None) 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
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
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
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
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
Construct a quantity from another quantity.
The unit is unchanged.
- from_(cls: type[StaticQuantity], value: ArrayLike | list[jaxtyping.Shaped[Array, ''] | jaxtyping.Shaped[ndarray, ''] | bool | number | bool | int | float | complex] | tuple[jaxtyping.Shaped[Array, ''] | jaxtyping.Shaped[ndarray, ''] | bool | number | bool | int | float | complex, ...], unit: Any, /, *, dtype: Any = None) StaticQuantity
Construct a StaticQuantity, keeping the value on NumPy dtypes.
The generic
AbstractQuantity.from_routes the value throughjnp.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 theStaticValue.from_converter convert it β that preserves the NumPy dtype. Delegating (rather than callingnp.asarrayhere) also keeps.from_and__init__under the same policy for JAX inputs, instead of materialising an array the constructor would reject. (The keyword-unitoverload 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
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
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')
Construct a distance.
>>> import unxt as u >>> import coordinax.distances as cxd >>> cxd.Distance.from_(1, "kpc") Distance(1, 'kpc')
Compute distance from distance.
>>> import unxt as u >>> import coordinax.distances as cxd
>>> d = cxd.Distance(1, "kpc") >>> cxd.Distance.from_(d) is d True
>>> cxd.Distance.from_(d, dtype=float) Distance(1., 'kpc')
- from_(cls: type[Distance], q: AbstractQuantity, /, **kw: Any) Distance
Construct a distance from a quantity, dispatching on its dimensions.
From a length quantity:
>>> import unxt as u >>> import coordinax.distances as cxd >>> q = u.Q(1, "kpc") >>> cxd.Distance.from_(q, dtype=float) Distance(1., 'kpc')
From a parallax angle:
>>> q = u.Q(1, "mas") >>> cxd.Distance.from_(q).uconvert("pc").round(2) Distance(1000., 'pc')
From a distance modulus:
>>> q = u.Q(10, "mag") >>> cxd.Distance.from_(q).uconvert("pc").round(2) Distance(1000., 'pc')
- from_(cls: type[DistanceModulus], value: ArrayLike, unit: Any, /, **kw: Any) DistanceModulus
Construct a distance.
>>> import unxt as u >>> from coordinaxs.astro import DistanceModulus
>>> DistanceModulus.from_(1, "mag") DistanceModulus(1, 'mag')
- from_(cls: type[DistanceModulus], dm: DistanceModulus, /, **kw: Any) DistanceModulus
Compute distance modulus from distance modulus.
>>> import unxt as u >>> from coordinaxs.astro import DistanceModulus
>>> dm = DistanceModulus(1, "mag") >>> DistanceModulus.from_(dm) is dm True
>>> DistanceModulus.from_(dm, dtype=float) DistanceModulus(1., 'mag')
- from_(cls: type[DistanceModulus], q: AbstractQuantity, /, **kw: Any) DistanceModulus
Construct a distance modulus from a quantity, dispatching on dimensions.
From a distance:
>>> import unxt as u >>> import coordinax.distances as cxd >>> from coordinaxs.astro import DistanceModulus
>>> d = cxd.Distance(1, "pc") >>> DistanceModulus.from_(d) DistanceModulus(-5., 'mag')
>>> q = u.Q(1, "pc") >>> DistanceModulus.from_(q) DistanceModulus(-5., 'mag')
From a parallax angle:
>>> q = u.Q(1, "mas") >>> DistanceModulus.from_(q) DistanceModulus(10., 'mag')
From a distance modulus (magnitude):
>>> q = u.Q(1, "mag") >>> DistanceModulus.from_(q) DistanceModulus(1, 'mag')
- from_(cls: type[Distance], dm: DistanceModulus, /, **kw: Any) Distance
Compute distance from distance modulus.
>>> import coordinax.distances as cxd >>> from coordinaxs.astro import DistanceModulus
>>> dm = DistanceModulus(10, "mag") >>> cxd.Distance.from_(dm).uconvert("pc").round(2) Distance(1000., 'pc')
Construct a distance.
>>> import unxt as u >>> from coordinaxs.astro import Parallax
>>> Parallax.from_(1, "mas") Parallax(1, 'mas')
Compute parallax from parallax.
>>> import unxt as u >>> from coordinaxs.astro import Parallax
>>> p = Parallax(1, "mas") >>> Parallax.from_(p) is p True
>>> Parallax.from_(p, dtype=float) Parallax(1., 'mas')
- from_(cls: type[Parallax], q: AbstractQuantity, /, **kw: Any) Parallax
Construct a parallax from a quantity, dispatching on its dimensions.
From a parallax angle:
>>> import unxt as u >>> import coordinax.distances as cxd >>> from coordinaxs.astro import Parallax
>>> q = u.Q(1, "mas") >>> Parallax.from_(q, dtype=float) Parallax(1., 'mas')
From a distance:
>>> d = cxd.Distance(10, "pc") >>> Parallax.from_(d).uconvert("mas").round(2) Parallax(100., 'mas')
>>> q = u.Q(10, "pc") >>> Parallax.from_(q).uconvert("mas").round(2) Parallax(100., 'mas')
From a distance modulus:
>>> dm = u.Q(10, "mag") >>> Parallax.from_(dm).uconvert("mas").round(2) Parallax(1., 'mas')
Compute distance from parallax.
>>> import coordinax.distances as cxd >>> from coordinaxs.astro import Parallax
>>> p = Parallax(1, "mas") >>> cxd.Distance.from_(p).uconvert("pc").round(2) Distance(1000., 'pc')
- from_(cls: type[DistanceModulus], p: Parallax, /, **kw: Any) DistanceModulus
Compute distance modulus from parallax.
>>> from coordinaxs.astro import DistanceModulus, Parallax >>> p = Parallax(1, "mas") >>> DistanceModulus.from_(p) DistanceModulus(10., 'mag')
- from_(cls: type[Parallax], dm: DistanceModulus, /, **kw: Any) Parallax
Convert distance modulus to parallax.
>>> import unxt as u >>> from coordinaxs.astro import DistanceModulus, Parallax >>> dm = DistanceModulus(10, "mag") >>> Parallax.from_(dm).uconvert("mas").round(2) Parallax(1., 'mas')
- Parameters:
cls (
type[AbstractQuantity])args (
Any)kwargs (
Any)
- Return type:
AbstractQuantity
- is_equivalent(other: AbstractQuantity, /)#
Whether
selfandotherare 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:
- property mT: AbstractQuantity#
Matrix transpose of the array.
Examples
>>> import unxt as u >>> q = u.Q([[0, 1], [2, 3]], "m") >>> q.mT Quantity(Array([[0, 2], [1, 3]], dtype=int32), unit='m')
It also works for a
StaticQuantity:>>> u.StaticQuantity([[0, 1], [2, 3]], "m").mT.value.tolist() [[0, 2], [1, 3]]
- 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:
- 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:
- 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:
- 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:
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:
- 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:
- 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:
- 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:
- 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:
- Return type:
- 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)