Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ 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>
nstarman committed Jan 25, 2025
1 parent e558b58 commit e1d2a8b
Showing 2 changed files with 14 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/unxt/_src/quantity/register_primitives.py
Original file line number Diff line number Diff line change
@@ -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(Array(1.5, dtype=float32, ...), unit='km')

Check failure on line 155 in src/unxt/_src/quantity/register_primitives.py

GitHub Actions / Check Interoperability (3.11, ubuntu-latest)

register_primitives.py line=154 column=1

Check failure on line 155 in src/unxt/_src/quantity/register_primitives.py

GitHub Actions / Check Python 3.13 on ubuntu-latest

register_primitives.py line=154 column=1

Check failure on line 155 in src/unxt/_src/quantity/register_primitives.py

GitHub Actions / Check Python 3.12 on ubuntu-latest

register_primitives.py line=154 column=1

Check failure on line 155 in src/unxt/_src/quantity/register_primitives.py

GitHub Actions / Check Python 3.11 on ubuntu-latest

register_primitives.py line=154 column=1

Check failure on line 155 in src/unxt/_src/quantity/register_primitives.py

GitHub Actions / Check Interoperability (3.13, ubuntu-latest)

register_primitives.py line=154 column=1

Check failure on line 155 in src/unxt/_src/quantity/register_primitives.py

GitHub Actions / Check Python 3.13 on macos-latest

register_primitives.py line=154 column=1

Check failure on line 155 in src/unxt/_src/quantity/register_primitives.py

GitHub Actions / Check Python 3.12 on macos-latest

register_primitives.py line=154 column=1

Check failure on line 155 in src/unxt/_src/quantity/register_primitives.py

GitHub Actions / Check Python 3.11 on macos-latest

register_primitives.py line=154 column=1

Check failure on line 155 in src/unxt/_src/quantity/register_primitives.py

GitHub Actions / Check Interoperability (3.12, ubuntu-latest)

register_primitives.py line=154 column=1
>>> q1 + q2
Quantity(Array(1.5, dtype=float32, ...), unit='km')

Check failure on line 157 in src/unxt/_src/quantity/register_primitives.py

GitHub Actions / Check Interoperability (3.11, ubuntu-latest)

register_primitives.py line=156 column=1

Check failure on line 157 in src/unxt/_src/quantity/register_primitives.py

GitHub Actions / Check Python 3.13 on ubuntu-latest

register_primitives.py line=156 column=1

Check failure on line 157 in src/unxt/_src/quantity/register_primitives.py

GitHub Actions / Check Python 3.12 on ubuntu-latest

register_primitives.py line=156 column=1

Check failure on line 157 in src/unxt/_src/quantity/register_primitives.py

GitHub Actions / Check Python 3.11 on ubuntu-latest

register_primitives.py line=156 column=1

Check failure on line 157 in src/unxt/_src/quantity/register_primitives.py

GitHub Actions / Check Interoperability (3.13, ubuntu-latest)

register_primitives.py line=156 column=1

Check failure on line 157 in src/unxt/_src/quantity/register_primitives.py

GitHub Actions / Check Python 3.13 on macos-latest

register_primitives.py line=156 column=1

Check failure on line 157 in src/unxt/_src/quantity/register_primitives.py

GitHub Actions / Check Python 3.12 on macos-latest

register_primitives.py line=156 column=1

Check failure on line 157 in src/unxt/_src/quantity/register_primitives.py

GitHub Actions / Check Python 3.11 on macos-latest

register_primitives.py line=156 column=1

Check failure on line 157 in src/unxt/_src/quantity/register_primitives.py

GitHub Actions / Check Interoperability (3.12, ubuntu-latest)

register_primitives.py line=156 column=1
"""
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
5 changes: 5 additions & 0 deletions src/unxt/_src/quantity/unchecked.py
Original file line number Diff line number Diff line change
@@ -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

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


add_promotion_rule(UncheckedQuantity, Quantity, Quantity)

0 comments on commit e1d2a8b

Please sign in to comment.