Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: promotion in sqrt #116

Merged
merged 1 commit into from
May 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion src/unxt/_quantity/register_primitives.py
Original file line number Diff line number Diff line change
Expand Up @@ -3307,10 +3307,51 @@ def _sort_p_one_operand(


@register(lax.sqrt_p)
def _sqrt_p(x: AbstractQuantity) -> AbstractQuantity:
def _sqrt_p_q(x: AbstractQuantity) -> AbstractQuantity:
"""Square root of a quantity.

Examples
--------
>>> import quaxed.numpy as qnp

>>> from unxt import UncheckedQuantity
>>> q = UncheckedQuantity(9, "m")
>>> qnp.sqrt(q)
UncheckedQuantity(Array(3., dtype=float32), unit='m(1/2)')

>>> from unxt import Quantity
>>> q = Quantity(9, "m")
>>> qnp.sqrt(q)
Quantity['m0.5'](Array(3., dtype=float32), unit='m(1/2)')

"""
# Apply sqrt to the value and adjust the unit
return type_np(x)(lax.sqrt(x.value), unit=x.unit ** (1 / 2))


@register(lax.sqrt_p)
def _sqrt_p_d(x: AbstractDistance) -> Quantity:
"""Square root of a quantity.

Examples
--------
>>> import quaxed.numpy as qnp

>>> from unxt import Distance
>>> q = Distance(9, "m")
>>> qnp.sqrt(q)
Quantity['m0.5'](Array(3., dtype=float32), unit='m(1/2)')

>>> from unxt import Parallax
>>> q = Parallax(9, "mas")
>>> qnp.sqrt(q)
Quantity['rad0.5'](Array(3., dtype=float32), unit='mas(1/2)')

"""
# Promote to something that supports sqrt units.
return Quantity(lax.sqrt(x.value), unit=x.unit ** (1 / 2))


# ==============================================================================


Expand Down
Loading