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

docs: add to registered call methods #56

Merged
merged 3 commits into from
Mar 7, 2024
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
24 changes: 23 additions & 1 deletion src/coordinax/_d3/operate.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,28 @@ def call(self: AbstractOperator, x: Q3, /) -> Q3:
def call(
self: AbstractOperator, x: Q3, t: TimeBatchOrScalar, /
) -> tuple[Q3, TimeBatchOrScalar]:
"""Dispatch to the operator's `__call__` method."""
"""Dispatch to the operator's `__call__` method.

Examples
--------
>>> from jax_quantity import Quantity
>>> import coordinax as cx

We can then create a spatial translation operator:

>>> op = cx.operators.GalileanSpatialTranslationOperator(Quantity([1, 2, 3], "kpc"))
>>> op
GalileanSpatialTranslationOperator( translation=Cartesian3DVector( ... ) )

We can then apply the operator to a position:

>>> pos = Quantity([1.0, 2.0, 3.0], "kpc")
>>> t = Quantity(0.0, "Gyr")

>>> op(pos, t)
(Quantity['length'](Array([2., 4., 6.], dtype=float32), unit='kpc'),
Quantity['time'](Array(0., dtype=float32, ...), unit='Gyr'))

"""
vec, t = self(Cartesian3DVector.constructor(x), t)
return convert(vec, Quantity), t
56 changes: 52 additions & 4 deletions src/coordinax/_d4/operate.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,63 @@


@op_call_dispatch
def call(self: AbstractOperator, x: FourVector, /) -> FourVector:
"""Dispatch to the operator's `__call__` method."""
q, t = self(x.q, x.t)
def call(self: AbstractOperator, v4: FourVector, /) -> FourVector:
"""Dispatch to the operator's `__call__` method.

Examples
--------
>>> from jax_quantity import Quantity
>>> import coordinax as cx

We can then create a spatial translation operator:

>>> op = cx.operators.GalileanSpatialTranslationOperator(Quantity([1, 2, 3], "kpc"))
>>> op
GalileanSpatialTranslationOperator( translation=Cartesian3DVector( ... ) )

We can then apply the operator to a position:

>>> pos = cx.FourVector.constructor(Quantity([0, 1.0, 2.0, 3.0], "kpc"))
>>> pos
FourVector( t=Quantity[PhysicalType('time')](...), q=Cartesian3DVector( ... ) )

>>> newpos = op(pos)
>>> newpos
FourVector( t=Quantity[PhysicalType('time')](...), q=Cartesian3DVector( ... ) )
>>> newpos.q.x
Quantity['length'](Array(2., dtype=float32), unit='kpc')

"""
q, t = self(v4.q, v4.t)
return FourVector(t=t, q=q)


@op_call_dispatch
def call(
self: AbstractOperator, x: Shaped[Quantity["length"], "*batch 4"], /
) -> Shaped[Quantity["length"], "*batch 4"]:
"""Dispatch to the operator's `__call__` method."""
"""Dispatch to the operator's `__call__` method.

Examples
--------
>>> from jax_quantity import Quantity
>>> import coordinax as cx

We can then create a spatial translation operator:

>>> op = cx.operators.GalileanSpatialTranslationOperator(Quantity([1, 2, 3], "kpc"))
>>> op
GalileanSpatialTranslationOperator( translation=Cartesian3DVector( ... ) )

We can then apply the operator to a position:

>>> pos = Quantity([0, 1.0, 2.0, 3.0], "kpc")
>>> pos
Quantity['length'](Array([0., 1., 2., 3.], dtype=float32), unit='kpc')

>>> newpos = op(pos)
>>> newpos
Quantity['length'](Array([0., 2., 4., 6.], dtype=float32), unit='kpc')

"""
return convert(self(FourVector.constructor(x)), Quantity)
12 changes: 7 additions & 5 deletions src/coordinax/operators/_galilean/rotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,13 @@ class GalileanRotationOperator(AbstractGalileanOperator):

def __check_init__(self) -> None:
# Check that the rotation matrix is orthogonal.
if not jnp.allclose(
self.rotation @ self.rotation.T, jnp.eye(3), **self.check_tol
):
msg = "The rotation matrix must be orthogonal."
raise ValueError(msg)
_ = eqx.error_if(
self.rotation,
not jnp.allclose(
self.rotation @ self.rotation.T, jnp.eye(3), **self.check_tol
),
"The rotation matrix must be orthogonal.",
)

# -----------------------------------------------------

Expand Down
8 changes: 8 additions & 0 deletions src/coordinax/operators/_galilean/translation.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
__all__ = ["GalileanSpatialTranslationOperator", "GalileanTranslationOperator"]


from dataclasses import replace
from typing import Any, Literal, final

import equinox as eqx
Expand Down Expand Up @@ -276,6 +277,13 @@ def __call__(
"""
return q + self.translation, t

@op_call_dispatch(precedence=1)
def __call__(
self: "GalileanSpatialTranslationOperator", v4: FourVector, /
) -> AbstractVector:
"""Apply the translation to the coordinates.""" # TODO: docstring
return replace(v4, q=v4.q + self.translation)


@simplify_op.register
def _simplify_op_spatialtranslation(
Expand Down
Loading