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

✨ feat(quantity): add promotion rule #382

Merged
merged 1 commit into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
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
11 changes: 10 additions & 1 deletion src/unxt/_src/quantity/register_primitives.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,16 @@ def add_p_aqaq(x: AbstractQuantity, y: AbstractQuantity) -> AbstractQuantity:
>>> q1 + q2
Quantity['length'](Array(1.5, dtype=float32, ...), unit='km')

>>> q1 = UncheckedQuantity(1, "km")
>>> q2 = Quantity(500.0, "m")
>>> jnp.add(q1, q2)
Quantity['length'](Array(1.5, dtype=float32, weak_type=True), unit='km')
>>> q1 + q2
Quantity['length'](Array(1.5, dtype=float32, weak_type=True), unit='km')

"""
x, y = promote(x, y)

# Strip the units to compare the values.
xv = ustrip(x)
yv = ustrip(x.unit, y) # this can change the dtype
Expand Down Expand Up @@ -2845,7 +2854,7 @@ def mul_p_qq(x: AbstractQuantity, y: AbstractQuantity) -> AbstractQuantity:
>>> q1 = UncheckedQuantity(2, "m")
>>> q2 = Quantity(3, "m")
>>> jnp.multiply(q1, q2)
UncheckedQuantity(Array(6, dtype=int32, ...), unit='m2')
Quantity['area'](Array(6, dtype=int32, weak_type=True), unit='m2')

"""
# Promote to a common type
Expand Down
9 changes: 7 additions & 2 deletions src/unxt/_src/quantity/unchecked.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@

import equinox as eqx
from jaxtyping import Array, Shaped
from plum import add_promotion_rule

from .base import AbstractQuantity
from .quantity import Quantity
from .value import convert_to_quantity_value
from unxt._src.units import AstropyUnits, unit as parse_unit
from unxt.units import unit as parse_unit
Expand All @@ -27,8 +29,8 @@ class UncheckedQuantity(AbstractQuantity):
"""The unit associated with this value."""

def __class_getitem__(
cls: type["UncheckedQuantity"], item: Any
) -> type["UncheckedQuantity"]:
cls: "type[UncheckedQuantity]", item: Any
) -> "type[UncheckedQuantity]":
"""No-op support for `UncheckedQuantity[...]` syntax.

This method is called when the class is subscripted, e.g.:
Expand All @@ -39,3 +41,6 @@ def __class_getitem__(

"""
return cls


add_promotion_rule(UncheckedQuantity, Quantity, Quantity)