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 <[email protected]>
  • Loading branch information
nstarman committed Jan 30, 2025
1 parent d90350f commit cc730a7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
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)

0 comments on commit cc730a7

Please sign in to comment.