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

Refactor str repr #71

Merged
merged 4 commits into from
Mar 20, 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
8 changes: 4 additions & 4 deletions src/coordinax/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -695,12 +695,12 @@ def __str__(self) -> str:

>>> vec = cx.Cartesian3DVector.constructor(Quantity([1, 2, 3], "m"))
>>> str(vec)
'<Cartesian3DVector (x, y, z) in (m, m, m)\n [1. 2. 3.]>'
'<Cartesian3DVector (x[m], y[m], z[m])\n [1. 2. 3.]>'

"""
cls_name = type(self).__name__
comps = ", ".join(self.components)
units = ", ".join(map(str, self.units.values()))
units = self.units
comps = ", ".join(f"{c}[{units[c]}]" for c in self.components)
vs = np.array2string(
xp.stack(
tuple(v.value for v in xp.broadcast_arrays(*dataclass_values(self))),
Expand All @@ -709,7 +709,7 @@ def __str__(self) -> str:
precision=3,
prefix=" ",
)
return f"<{cls_name} ({comps}) in ({units})\n {vs}>"
return f"<{cls_name} ({comps})\n {vs}>"


# -----------------------------------------------
Expand Down
48 changes: 43 additions & 5 deletions src/coordinax/_d3/operate.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,40 @@


@op_call_dispatch
def call(self: AbstractOperator, x: Q3, /) -> Q3:
"""Dispatch to the operator's `__call__` method."""
return self(Cartesian3DVector.constructor(x))
def call(self: AbstractOperator, q: Q3, /) -> Q3:
r"""Operate on a 3D Quantity.

`q` is the position vector. This is interpreted as a 3D CartesianVector.
See :class:`coordinax.Cartesian3DVector` for more details.

Returns
-------
x' : Quantity['length', '*#batch 3']
The operated-upon position vector.

Examples
--------
>>> from unxt import Quantity
>>> import coordinax as cx
>>> import coordinax.operators as cxo

>>> shift = Quantity([1.0, 2.0, 3.0], "kpc")
>>> op = cxo.GalileanSpatialTranslationOperator(shift)

>>> q = Quantity([0.0, 0, 0], "kpc")
>>> op(q)
Quantity['length'](Array([1., 2., 3.], dtype=float32), unit='kpc')

Since :meth:`AbstractOperator.__call__` uses multiple dispatch, we
explicitly call this registered method.

>>> op.__call__._f.resolve_method((op, q))[0](op, q)
Quantity['length'](Array([1., 2., 3.], dtype=float32), unit='kpc')

"""
return convert(
self(Cartesian3DVector.constructor(q)).represent_as(Cartesian3DVector), Quantity
)


@op_call_dispatch
Expand All @@ -42,10 +73,17 @@ def call(

We can then apply the operator to a position:

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

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

Since :meth:`AbstractOperator.__call__` uses multiple dispatch, we
explicitly call this registered method.

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

Expand Down
61 changes: 59 additions & 2 deletions src/coordinax/_d3/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,34 @@ def represent_as(
def represent_as(
current: Abstract3DVector, target: type[Abstract3DVector], /, **kwargs: Any
) -> Abstract3DVector:
"""Self transform."""
"""Self transforms for 3D vectors.

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

Cartesian to Cartesian:

>>> vec = cx.Cartesian3DVector.constructor(Quantity([1, 2, 3], "kpc"))
>>> cx.represent_as(vec, cx.Cartesian3DVector) is vec
True

Spherical to Spherical:

>>> vec = cx.SphericalVector(r=Quantity(1, "kpc"), theta=Quantity(2, "deg"),
... phi=Quantity(3, "deg"))
>>> cx.represent_as(vec, cx.SphericalVector) is vec
True

Cylindrical to Cylindrical:

>>> vec = cx.CylindricalVector(rho=Quantity(1, "kpc"), phi=Quantity(2, "deg"),
... z=Quantity(3, "kpc"))
>>> cx.represent_as(vec, cx.CylindricalVector) is vec
True

"""
return current


Expand All @@ -55,7 +82,37 @@ def represent_as(
/,
**kwargs: Any,
) -> Abstract3DVectorDifferential:
"""Self transform of 3D Differentials."""
"""Self transforms for 3D differentials.

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

For these transformations the position does not matter since the
self-transform returns the differential unchanged.

>>> vec = cx.Cartesian3DVector.constructor(Quantity([1, 2, 3], "kpc"))

Cartesian to Cartesian Differential:

>>> dif = cx.CartesianDifferential3D.constructor(Quantity([1, 2, 3], "km/s"))
>>> cx.represent_as(dif, cx.CartesianDifferential3D, vec) is dif
True

>>> dif = cx.SphericalDifferential(d_r=Quantity(1, "km/s"),
... d_theta=Quantity(2, "mas/yr"),
... d_phi=Quantity(3, "mas/yr"))
>>> cx.represent_as(dif, cx.SphericalDifferential, vec) is dif
True

>>> dif = cx.CylindricalDifferential(d_rho=Quantity(1, "km/s"),
... d_phi=Quantity(2, "mas/yr"),
... d_z=Quantity(3, "km/s"))
>>> cx.represent_as(dif, cx.CylindricalDifferential, vec) is dif
True

"""
return current


Expand Down
2 changes: 1 addition & 1 deletion tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ def test_represent_as(self, difntl, target, vector):

This just tests that the machiner works.
"""
# TODO: have all the convertsions
# TODO: have all the conversions
if (
(
isinstance(difntl, Abstract1DVectorDifferential)
Expand Down
Loading