Skip to content

Commit

Permalink
✨ feat(vecs): new function vector for constructing vectors (#320)
Browse files Browse the repository at this point in the history
* 🎨 style(vecs): improve d1 from_ dispatches
* 🎨 style(vecs): consolidate nd from_
* ♻️ refactor(vecs): consolidate vecs from quantity
* ✨ feat(vecs): new function `vector` for constructing vectors
* 📝 docs(vecs): add docstrings


Signed-off-by: Nathaniel Starkman <nstarman@users.noreply.github.com>
  • Loading branch information
nstarman authored Dec 29, 2024
1 parent 14611e2 commit 3a1e799
Show file tree
Hide file tree
Showing 18 changed files with 547 additions and 549 deletions.
2 changes: 2 additions & 0 deletions src/coordinax/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
SphericalPos,
SphericalVel,
vconvert,
vector,
)

# isort: split
Expand All @@ -37,6 +38,7 @@
# common distance objects
"Distance",
# common vecs objects
"vector",
"vconvert",
"CartesianPos3D",
"CartesianVel3D",
Expand Down
78 changes: 39 additions & 39 deletions src/coordinax/_interop/coordinax_interop_astropy/constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@

import astropy.coordinates as apyc
import astropy.units as apyu
from plum import convert
from plum import convert, dispatch

import unxt as u

import coordinax as cx


@cx.vecs.AbstractVector.from_.dispatch(precedence=-1)
def from_(
@dispatch(precedence=-1)
def vector(
cls: type[cx.vecs.AbstractPos3D], obj: apyc.CartesianRepresentation, /
) -> cx.CartesianPos3D:
"""Construct from a :class:`astropy.coordinates.CartesianRepresentation`.
Expand All @@ -34,8 +34,8 @@ def from_(
return cx.CartesianPos3D.from_(obj)


@cx.vecs.AbstractVector.from_.dispatch(precedence=-1)
def from_(
@dispatch(precedence=-1)
def vector(
cls: type[cx.vecs.AbstractPos3D], obj: apyc.CylindricalRepresentation, /
) -> cx.vecs.CylindricalPos:
"""Construct from a :class:`astropy.coordinates.CylindricalRepresentation`.
Expand All @@ -58,8 +58,8 @@ def from_(
return cx.vecs.CylindricalPos.from_(obj)


@cx.vecs.AbstractVector.from_.dispatch(precedence=-1)
def from_(
@dispatch(precedence=-1)
def vector(
cls: type[cx.vecs.AbstractPos3D], obj: apyc.PhysicsSphericalRepresentation, /
) -> cx.SphericalPos:
"""Construct from a :class:`astropy.coordinates.PhysicsSphericalRepresentation`.
Expand All @@ -82,8 +82,8 @@ def from_(
return cx.SphericalPos.from_(obj)


@cx.vecs.AbstractVector.from_.dispatch(precedence=-1)
def from_(
@dispatch(precedence=-1)
def vector(
cls: type[cx.vecs.AbstractPos3D], obj: apyc.SphericalRepresentation, /
) -> cx.vecs.LonLatSphericalPos:
"""Construct from a :class:`astropy.coordinates.SphericalRepresentation`.
Expand All @@ -109,8 +109,8 @@ def from_(
# -------------------------------------------------------------------


@cx.vecs.AbstractVector.from_.dispatch
def from_(
@dispatch
def vector(
cls: type[cx.CartesianPos3D], obj: apyc.BaseRepresentation, /
) -> cx.CartesianPos3D:
"""Construct from a :class:`astropy.coordinates.BaseRepresentation`.
Expand All @@ -130,8 +130,8 @@ def from_(
return cls(x=obj.x, y=obj.y, z=obj.z)


@cx.vecs.AbstractVector.from_.dispatch
def from_(
@dispatch
def vector(
cls: type[cx.vecs.CylindricalPos], obj: apyc.BaseRepresentation, /
) -> cx.vecs.CylindricalPos:
"""Construct from a :class:`astropy.coordinates.BaseRepresentation`.
Expand All @@ -153,8 +153,8 @@ def from_(
return cls(rho=obj.rho, phi=obj.phi, z=obj.z)


@cx.vecs.AbstractVector.from_.dispatch
def from_(
@dispatch
def vector(
cls: type[cx.SphericalPos], obj: apyc.BaseRepresentation, /
) -> cx.SphericalPos:
"""Construct from a :class:`astropy.coordinates.BaseRepresentation`.
Expand All @@ -176,8 +176,8 @@ def from_(
return cls(r=obj.r, theta=obj.theta, phi=obj.phi)


@cx.vecs.AbstractVector.from_.dispatch
def from_(
@dispatch
def vector(
cls: type[cx.vecs.LonLatSphericalPos], obj: apyc.BaseRepresentation, /
) -> cx.vecs.LonLatSphericalPos:
"""Construct from a :class:`astropy.coordinates.BaseRepresentation`.
Expand All @@ -202,8 +202,8 @@ def from_(
#####################################################################


@cx.vecs.AbstractVector.from_.dispatch
def from_(
@dispatch
def vector(
cls: type[cx.vecs.AbstractVel3D], obj: apyc.CartesianDifferential, /
) -> cx.CartesianVel3D:
"""Construct from a :class:`astropy.coordinates.CartesianDifferential`.
Expand All @@ -225,8 +225,8 @@ def from_(
return cx.CartesianVel3D.from_(obj)


@cx.vecs.AbstractVector.from_.dispatch
def from_(
@dispatch
def vector(
cls: type[cx.vecs.AbstractVel3D], obj: apyc.CylindricalDifferential, /
) -> cx.vecs.CylindricalVel:
"""Construct from a :class:`astropy.coordinates.CylindricalDifferential`.
Expand All @@ -249,8 +249,8 @@ def from_(
return cx.vecs.CylindricalVel.from_(obj)


@cx.vecs.AbstractVector.from_.dispatch
def from_(
@dispatch
def vector(
cls: type[cx.vecs.AbstractVel3D], obj: apyc.PhysicsSphericalDifferential, /
) -> cx.SphericalVel:
"""Construct from a :class:`astropy.coordinates.PhysicsSphericalDifferential`.
Expand All @@ -273,8 +273,8 @@ def from_(
return cx.SphericalVel.from_(obj)


@cx.vecs.AbstractVector.from_.dispatch
def from_(
@dispatch
def vector(
cls: type[cx.vecs.AbstractVel3D], obj: apyc.SphericalDifferential, /
) -> cx.vecs.LonLatSphericalVel:
"""Construct from a :class:`astropy.coordinates.SphericalDifferential`.
Expand All @@ -298,8 +298,8 @@ def from_(
return cx.vecs.LonLatSphericalVel.from_(obj)


@cx.vecs.AbstractVector.from_.dispatch
def from_(
@dispatch
def vector(
cls: type[cx.vecs.AbstractVel3D], obj: apyc.SphericalCosLatDifferential, /
) -> cx.vecs.LonCosLatSphericalVel:
"""Construct from a :class:`astropy.coordinates.SphericalCosLatDifferential`.
Expand Down Expand Up @@ -332,8 +332,8 @@ def from_(
# -------------------------------------------------------------------


@cx.vecs.AbstractVector.from_.dispatch
def from_(
@dispatch
def vector(
cls: type[cx.CartesianVel3D], obj: apyc.CartesianDifferential, /
) -> cx.CartesianVel3D:
"""Construct from a :class:`astropy.coordinates.CartesianDifferential`.
Expand All @@ -353,8 +353,8 @@ def from_(
return cls(d_x=obj.d_x, d_y=obj.d_y, d_z=obj.d_z)


@cx.vecs.AbstractVector.from_.dispatch
def from_(
@dispatch
def vector(
cls: type[cx.vecs.CylindricalVel], obj: apyc.CylindricalDifferential, /
) -> cx.vecs.CylindricalVel:
"""Construct from a :class:`astropy.coordinates.CylindricalVel`.
Expand All @@ -375,8 +375,8 @@ def from_(
return cls(d_rho=obj.d_rho, d_phi=obj.d_phi, d_z=obj.d_z)


@cx.vecs.AbstractVector.from_.dispatch
def from_(
@dispatch
def vector(
cls: type[cx.SphericalVel], obj: apyc.PhysicsSphericalDifferential, /
) -> cx.SphericalVel:
"""Construct from a :class:`astropy.coordinates.PhysicsSphericalDifferential`.
Expand All @@ -397,8 +397,8 @@ def from_(
return cls(d_r=obj.d_r, d_phi=obj.d_phi, d_theta=obj.d_theta)


@cx.vecs.AbstractVector.from_.dispatch
def from_(
@dispatch
def vector(
cls: type[cx.vecs.LonLatSphericalVel], obj: apyc.SphericalDifferential, /
) -> cx.vecs.LonLatSphericalVel:
"""Construct from a :class:`astropy.coordinates.SphericalVel`.
Expand All @@ -420,8 +420,8 @@ def from_(
return cls(d_distance=obj.d_distance, d_lon=obj.d_lon, d_lat=obj.d_lat)


@cx.vecs.AbstractVector.from_.dispatch
def from_(
@dispatch
def vector(
cls: type[cx.vecs.LonCosLatSphericalVel], obj: apyc.SphericalCosLatDifferential, /
) -> cx.vecs.LonCosLatSphericalVel:
"""Construct from a :class:`astropy.coordinates.SphericalCosLatDifferential`.
Expand Down Expand Up @@ -454,8 +454,8 @@ def from_(
#####################################################################


@cx.vecs.AbstractVector.from_.dispatch
def from_(
@dispatch
def vector(
cls: type[cx.vecs.AbstractVector], obj: apyu.Quantity, /
) -> cx.vecs.AbstractVector:
"""Construct a vector from an Astropy Quantity array.
Expand Down
8 changes: 8 additions & 0 deletions src/coordinax/_src/frames/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ class AbstractCoordinate(AbstractVector):
#: `coordinax.frames.AbstractReferenceFrame` object.
frame: eqx.AbstractVar[AbstractReferenceFrame]

@classmethod
@dispatch # type: ignore[misc]
def from_(
cls: "type[AbstractCoordinate]", *args: Any, **kwargs: Any
) -> "AbstractCoordinate":
"""Construct a coordinate from other data."""
return super().from_(*args, **kwargs)

# ===============================================================
# Coordinate API

Expand Down
7 changes: 7 additions & 0 deletions src/coordinax/_src/vectors/api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Copyright (c) 2023 coordinax maintainers. All rights reserved."""

__all__ = [
"vector",
"vconvert",
"normalize_vector",
]
Expand All @@ -24,3 +25,9 @@ def vconvert(target: type[Any], /, *args: Any, **kwargs: Any) -> Any:
def normalize_vector(x: Any, /) -> Any:
"""Return the unit vector."""
raise NotImplementedError


@dispatch.abstract # type: ignore[misc]
def vector(*args: Any, **kwargs: Any) -> Any:
"""Create a vector."""
raise NotImplementedError # pragma: no cover
Loading

0 comments on commit 3a1e799

Please sign in to comment.