Skip to content

Commit

Permalink
✨ feat(quantity): add promotion rule
Browse files Browse the repository at this point in the history
Signed-off-by: Nathaniel Starkman <nstarman@users.noreply.github.com>
  • Loading branch information
nstarman committed Jan 25, 2025
1 parent e558b58 commit 66d7827
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
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
5 changes: 5 additions & 0 deletions src/unxt/_src/quantity/unchecked.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
import equinox as eqx
import jax
from jaxtyping import Array, Shaped
from plum import add_promotion_rule

from .base import AbstractQuantity
from .quantity import Quantity
from unxt._src.units import unit as parse_unit
from unxt._src.units.api import AbstractUnits

Expand Down Expand Up @@ -39,3 +41,6 @@ def __class_getitem__(
"""
return cls


add_promotion_rule(UncheckedQuantity, Quantity, Quantity)

0 comments on commit 66d7827

Please sign in to comment.