Skip to content

Commit

Permalink
docs: sub
Browse files Browse the repository at this point in the history
Signed-off-by: nstarman <nstarman@users.noreply.github.com>
  • Loading branch information
nstarman committed Aug 4, 2024
1 parent e886240 commit 87a3fb5
Showing 1 changed file with 87 additions and 2 deletions.
89 changes: 87 additions & 2 deletions src/unxt/_quantity/register_primitives.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
from plum.parametric import type_unparametrized as type_np
from quax import register as register_

from quaxed import lax as qlax

from .base import AbstractQuantity, can_convert_unit
from .base_parametric import AbstractParametricQuantity
from .core import Quantity
Expand Down Expand Up @@ -3409,17 +3411,100 @@ def _stop_gradient_p(x: AbstractQuantity) -> AbstractQuantity:

@register(lax.sub_p)
def _sub_p_qq(x: AbstractQuantity, y: AbstractQuantity) -> AbstractQuantity:
"""Subtract two quantities.
Examples
--------
>>> import quaxed.array_api as xp
>>> from unxt import UncheckedQuantity
>>> q1 = UncheckedQuantity(1.0, "km")
>>> q2 = UncheckedQuantity(500.0, "m")
>>> xp.subtract(q1, q2)
UncheckedQuantity(Array(0.5, dtype=float32, ...), unit='km')
>>> q1 - q2
UncheckedQuantity(Array(0.5, dtype=float32, ...), unit='km')
>>> from unxt import Quantity
>>> q1 = Quantity(1.0, "km")
>>> q2 = Quantity(500.0, "m")
>>> xp.subtract(q1, q2)
Quantity['length'](Array(0.5, dtype=float32, ...), unit='km')
>>> q1 - q2
Quantity['length'](Array(0.5, dtype=float32, ...), unit='km')
>>> from unxt import Distance
>>> d1 = Distance(1.0, "km")
>>> d2 = Distance(500.0, "m")
>>> xp.subtract(d1, d2)
Distance(Array(0.5, dtype=float32, ...), unit='km')
>>> from unxt import Parallax
>>> p1 = Parallax(1.0, "mas")
>>> p2 = Parallax(500.0, "uas")
>>> xp.subtract(p1, p2)
Parallax(Array(0.5, dtype=float32, ...), unit='mas')
>>> from unxt import DistanceModulus
>>> dm1 = DistanceModulus(1.0, "mag")
>>> dm2 = DistanceModulus(500.0, "mag")
>>> xp.subtract(dm1, dm2)
DistanceModulus(Array(-499., dtype=float32, ...), unit='mag')
"""
return replace(x, value=lax.sub(x.to_units_value(x.unit), y.to_units_value(x.unit)))


@register(lax.sub_p)
def _sub_p_vq(x: ArrayLike, y: AbstractQuantity) -> AbstractQuantity:
return replace(y, value=lax.sub(x, y.value))
"""Subtract a quantity from an array.
Examples
--------
>>> import quaxed.array_api as xp
>>> from unxt import UncheckedQuantity
>>> x = 1_000
>>> q = UncheckedQuantity(500.0, "")
>>> xp.subtract(x, q)

Check failure on line 3470 in src/unxt/_quantity/register_primitives.py

View workflow job for this annotation

GitHub Actions / Check Python 3.12 on ubuntu-latest

register_primitives.py line=3469 column=1

Check failure on line 3470 in src/unxt/_quantity/register_primitives.py

View workflow job for this annotation

GitHub Actions / Check Python 3.10 on ubuntu-latest

register_primitives.py line=3469 column=1

Check failure on line 3470 in src/unxt/_quantity/register_primitives.py

View workflow job for this annotation

GitHub Actions / Check Python 3.10 on macos-latest

register_primitives.py line=3469 column=1

Check failure on line 3470 in src/unxt/_quantity/register_primitives.py

View workflow job for this annotation

GitHub Actions / Check Python 3.12 on ubuntu-latest

register_primitives.py line=3469 column=1

Check failure on line 3470 in src/unxt/_quantity/register_primitives.py

View workflow job for this annotation

GitHub Actions / Check Python 3.12 on macos-latest

register_primitives.py line=3469 column=1
>>> x - q

Check failure on line 3472 in src/unxt/_quantity/register_primitives.py

View workflow job for this annotation

GitHub Actions / Check Python 3.12 on ubuntu-latest

register_primitives.py line=3471 column=1

Check failure on line 3472 in src/unxt/_quantity/register_primitives.py

View workflow job for this annotation

GitHub Actions / Check Python 3.10 on ubuntu-latest

register_primitives.py line=3471 column=1

Check failure on line 3472 in src/unxt/_quantity/register_primitives.py

View workflow job for this annotation

GitHub Actions / Check Python 3.10 on macos-latest

register_primitives.py line=3471 column=1

Check failure on line 3472 in src/unxt/_quantity/register_primitives.py

View workflow job for this annotation

GitHub Actions / Check Python 3.12 on ubuntu-latest

register_primitives.py line=3471 column=1

Check failure on line 3472 in src/unxt/_quantity/register_primitives.py

View workflow job for this annotation

GitHub Actions / Check Python 3.12 on macos-latest

register_primitives.py line=3471 column=1
>>> from unxt import Quantity
>>> q = Quantity(500.0, "")
>>> xp.subtract(x, q)

Check failure on line 3476 in src/unxt/_quantity/register_primitives.py

View workflow job for this annotation

GitHub Actions / Check Python 3.12 on ubuntu-latest

register_primitives.py line=3475 column=1

Check failure on line 3476 in src/unxt/_quantity/register_primitives.py

View workflow job for this annotation

GitHub Actions / Check Python 3.10 on ubuntu-latest

register_primitives.py line=3475 column=1

Check failure on line 3476 in src/unxt/_quantity/register_primitives.py

View workflow job for this annotation

GitHub Actions / Check Python 3.10 on macos-latest

register_primitives.py line=3475 column=1

Check failure on line 3476 in src/unxt/_quantity/register_primitives.py

View workflow job for this annotation

GitHub Actions / Check Python 3.12 on ubuntu-latest

register_primitives.py line=3475 column=1

Check failure on line 3476 in src/unxt/_quantity/register_primitives.py

View workflow job for this annotation

GitHub Actions / Check Python 3.12 on macos-latest

register_primitives.py line=3475 column=1
>>> x - q

Check failure on line 3478 in src/unxt/_quantity/register_primitives.py

View workflow job for this annotation

GitHub Actions / Check Python 3.12 on ubuntu-latest

register_primitives.py line=3477 column=1

Check failure on line 3478 in src/unxt/_quantity/register_primitives.py

View workflow job for this annotation

GitHub Actions / Check Python 3.10 on ubuntu-latest

register_primitives.py line=3477 column=1

Check failure on line 3478 in src/unxt/_quantity/register_primitives.py

View workflow job for this annotation

GitHub Actions / Check Python 3.10 on macos-latest

register_primitives.py line=3477 column=1

Check failure on line 3478 in src/unxt/_quantity/register_primitives.py

View workflow job for this annotation

GitHub Actions / Check Python 3.12 on ubuntu-latest

register_primitives.py line=3477 column=1

Check failure on line 3478 in src/unxt/_quantity/register_primitives.py

View workflow job for this annotation

GitHub Actions / Check Python 3.12 on macos-latest

register_primitives.py line=3477 column=1
"""
y = y.to_units(one)
return replace(y, value=qlax.sub(x, y.value))

Check warning on line 3481 in src/unxt/_quantity/register_primitives.py

View check run for this annotation

Codecov / codecov/patch

src/unxt/_quantity/register_primitives.py#L3480-L3481

Added lines #L3480 - L3481 were not covered by tests


@register(lax.sub_p)
def _sub_p_qv(x: AbstractQuantity, y: ArrayLike) -> AbstractQuantity:
return replace(x, value=lax.sub(x.value, y))
"""Subtract an array from a quantity.
Examples
--------
>>> import quaxed.array_api as xp
>>> from unxt import UncheckedQuantity
>>> q = UncheckedQuantity(500.0, "")
>>> y = 1_000
>>> xp.subtract(q, y)

Check failure on line 3496 in src/unxt/_quantity/register_primitives.py

View workflow job for this annotation

GitHub Actions / Check Python 3.12 on ubuntu-latest

register_primitives.py line=3495 column=1

Check failure on line 3496 in src/unxt/_quantity/register_primitives.py

View workflow job for this annotation

GitHub Actions / Check Python 3.10 on ubuntu-latest

register_primitives.py line=3495 column=1

Check failure on line 3496 in src/unxt/_quantity/register_primitives.py

View workflow job for this annotation

GitHub Actions / Check Python 3.10 on macos-latest

register_primitives.py line=3495 column=1

Check failure on line 3496 in src/unxt/_quantity/register_primitives.py

View workflow job for this annotation

GitHub Actions / Check Python 3.12 on ubuntu-latest

register_primitives.py line=3495 column=1

Check failure on line 3496 in src/unxt/_quantity/register_primitives.py

View workflow job for this annotation

GitHub Actions / Check Python 3.12 on macos-latest

register_primitives.py line=3495 column=1
>>> q - y

Check failure on line 3498 in src/unxt/_quantity/register_primitives.py

View workflow job for this annotation

GitHub Actions / Check Python 3.12 on ubuntu-latest

register_primitives.py line=3497 column=1

Check failure on line 3498 in src/unxt/_quantity/register_primitives.py

View workflow job for this annotation

GitHub Actions / Check Python 3.10 on ubuntu-latest

register_primitives.py line=3497 column=1

Check failure on line 3498 in src/unxt/_quantity/register_primitives.py

View workflow job for this annotation

GitHub Actions / Check Python 3.10 on macos-latest

register_primitives.py line=3497 column=1

Check failure on line 3498 in src/unxt/_quantity/register_primitives.py

View workflow job for this annotation

GitHub Actions / Check Python 3.12 on ubuntu-latest

register_primitives.py line=3497 column=1

Check failure on line 3498 in src/unxt/_quantity/register_primitives.py

View workflow job for this annotation

GitHub Actions / Check Python 3.12 on macos-latest

register_primitives.py line=3497 column=1
>>> from unxt import Quantity
>>> q = Quantity(500.0, "")
>>> xp.subtract(q, y)

Check failure on line 3502 in src/unxt/_quantity/register_primitives.py

View workflow job for this annotation

GitHub Actions / Check Python 3.12 on ubuntu-latest

register_primitives.py line=3501 column=1

Check failure on line 3502 in src/unxt/_quantity/register_primitives.py

View workflow job for this annotation

GitHub Actions / Check Python 3.10 on ubuntu-latest

register_primitives.py line=3501 column=1

Check failure on line 3502 in src/unxt/_quantity/register_primitives.py

View workflow job for this annotation

GitHub Actions / Check Python 3.10 on macos-latest

register_primitives.py line=3501 column=1

Check failure on line 3502 in src/unxt/_quantity/register_primitives.py

View workflow job for this annotation

GitHub Actions / Check Python 3.12 on ubuntu-latest

register_primitives.py line=3501 column=1

Check failure on line 3502 in src/unxt/_quantity/register_primitives.py

View workflow job for this annotation

GitHub Actions / Check Python 3.12 on macos-latest

register_primitives.py line=3501 column=1
>>> q - y

Check failure on line 3504 in src/unxt/_quantity/register_primitives.py

View workflow job for this annotation

GitHub Actions / Check Python 3.12 on ubuntu-latest

register_primitives.py line=3503 column=1

Check failure on line 3504 in src/unxt/_quantity/register_primitives.py

View workflow job for this annotation

GitHub Actions / Check Python 3.10 on ubuntu-latest

register_primitives.py line=3503 column=1

Check failure on line 3504 in src/unxt/_quantity/register_primitives.py

View workflow job for this annotation

GitHub Actions / Check Python 3.10 on macos-latest

register_primitives.py line=3503 column=1

Check failure on line 3504 in src/unxt/_quantity/register_primitives.py

View workflow job for this annotation

GitHub Actions / Check Python 3.12 on ubuntu-latest

register_primitives.py line=3503 column=1

Check failure on line 3504 in src/unxt/_quantity/register_primitives.py

View workflow job for this annotation

GitHub Actions / Check Python 3.12 on macos-latest

register_primitives.py line=3503 column=1
"""
x = x.to_units(one)
return replace(x, value=qlax.sub(x.value, y))


# ==============================================================================
Expand Down

0 comments on commit 87a3fb5

Please sign in to comment.