Skip to content

Commit 1cf96f6

Browse files
committed
refactor: step 1
Signed-off-by: nstarman <[email protected]>
1 parent 2bf9337 commit 1cf96f6

File tree

23 files changed

+461
-2154
lines changed

23 files changed

+461
-2154
lines changed

docs/guides/operators.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@ CartesianPos3D(
5050
)
5151
```
5252

53-
### GalileanRotation
53+
### Rotate
5454

5555
Rotates vectors in space:
5656

5757
```{code-block} python
58-
>>> rot = cxo.GalileanRotation.from_euler("z", u.Quantity(90, "deg"))
58+
>>> rot = cxo.Rotate.from_euler("z", u.Quantity(90, "deg"))
5959
>>> rot(q).round(2)
6060
CartesianPos3D(
6161
x=Quantity(-2., unit='kpc'),
@@ -73,7 +73,7 @@ Operators can be composed using the {class}`~coordinax.ops.Pipe` class or the
7373

7474
```{code-block} python
7575
>>> op1 = cxo.GalileanSpatialTranslation.from_([1, 0, 0], "kpc")
76-
>>> op2 = cxo.GalileanRotation.from_euler("z", u.Quantity(90, "deg"))
76+
>>> op2 = cxo.Rotate.from_euler("z", u.Quantity(90, "deg"))
7777
>>> pipe = cxo.Pipe([op1, op2])
7878
>>> pipe(q).round(2)
7979
CartesianPos3D(
@@ -106,7 +106,7 @@ CartesianPos3D(
106106

107107
- {class}`~coordinax.ops.simplify_op`: Simplifies composed operators when
108108
possible.
109-
- {class}`~coordinax.ops.convert_to_pipe_operators`: Utility to convert a list
109+
- {class}`~coordinax.ops.convert_to_operator_tuple`: Utility to convert a list
110110
of operators into a {class}`~coordinax.ops.Pipe`.
111111

112112
---

src/coordinax/_coordinax_space_frames/frame_transforms.py

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Astronomy reference frames."""
22
# ruff:noqa: N806
33

4-
__all__: list[str] = []
4+
__all__: tuple[str, ...] = ()
55

66

77
from typing import TypeAlias
@@ -16,14 +16,7 @@
1616
from .galactocentric import Galactocentric
1717
from .icrs import ICRS
1818
from coordinax._src.distances import Distance
19-
from coordinax._src.operators import (
20-
GalileanRotation,
21-
GalileanSpatialTranslation,
22-
Identity,
23-
Pipe,
24-
VelocityBoost,
25-
simplify_op,
26-
)
19+
from coordinax._src.operators import Add, Identity, Pipe, Rotate, simplify_op
2720

2821
ScalarAngle: TypeAlias = Shaped[u.Quantity["angle"] | u.Angle, ""]
2922
RotationMatrix: TypeAlias = Shaped[Array, "3 3"]
@@ -54,7 +47,7 @@ def frame_transform_op(
5447
5548
>>> @dispatch
5649
... def frame_transform_op(from_frame: MySpaceFrame, to_frame: ICRS, /) -> cx.ops.AbstractOperator:
57-
... return cx.ops.GalileanRotation.from_euler("z", u.Quantity(10, "deg"))
50+
... return cx.ops.Rotate.from_euler("z", u.Quantity(10, "deg"))
5851
5952
We can transform from `MySpaceFrame` to a Galacocentric frame, even though
6053
we don't have a direct transformation defined:
@@ -65,7 +58,7 @@ def frame_transform_op(
6558
>>> op = cx.frames.frame_transform_op(my_frame, gcf_frame)
6659
>>> op
6760
Pipe((
68-
GalileanRotation(rotation=f32[3,3]),
61+
Rotate(rotation=f32[3,3]),
6962
...
7063
))
7164
@@ -116,14 +109,14 @@ def frame_transform_op(from_frame: Galactocentric, to_frame: Galactocentric, /)
116109
>>> frame_op2 = cxf.frame_transform_op(gcf_frame, gcf_frame2)
117110
>>> frame_op2
118111
Pipe((
119-
VelocityBoost(CartesianVel3D( ... )),
120-
GalileanRotation(rotation=f32[3,3]),
121-
GalileanSpatialTranslation(CartesianPos3D( ... )),
122-
GalileanRotation(rotation=f32[3,3]),
123-
GalileanRotation(rotation=f32[3,3]),
124-
GalileanSpatialTranslation(CartesianPos3D( ... )),
125-
GalileanRotation(rotation=f32[3,3]),
126-
VelocityBoost(CartesianVel3D( ... ))
112+
Add(CartesianVel3D( ... )),
113+
Rotate(rotation=f32[3,3]),
114+
Add(CartesianPos3D( ... )),
115+
Rotate(rotation=f32[3,3]),
116+
Rotate(rotation=f32[3,3]),
117+
Add(CartesianPos3D( ... )),
118+
Rotate(rotation=f32[3,3]),
119+
Add(CartesianVel3D( ... ))
127120
))
128121
129122
"""
@@ -193,10 +186,10 @@ def frame_transform_op(from_frame: ICRS, to_frame: Galactocentric, /) -> Pipe:
193186
>>> frame_op = cx.frames.frame_transform_op(icrs_frame, gcf_frame)
194187
>>> frame_op
195188
Pipe((
196-
GalileanRotation(rotation=f32[3,3]),
197-
GalileanSpatialTranslation(CartesianPos3D( ... )),
198-
GalileanRotation(rotation=f32[3,3]),
199-
VelocityBoost(CartesianVel3D( ... ))
189+
Rotate(rotation=f32[3,3]),
190+
Add(CartesianPos3D( ... )),
191+
Rotate(rotation=f32[3,3]),
192+
Add(CartesianVel3D( ... ))
200193
))
201194
202195
Apply the transformation:
@@ -238,25 +231,23 @@ def frame_transform_op(from_frame: ICRS, to_frame: Galactocentric, /) -> Pipe:
238231
239232
""" # noqa: E501
240233
# rotation matrix to align x(ICRS) with the vector to the Galactic center
241-
rot_lat = GalileanRotation.from_euler("y", to_frame.galcen.lat)
242-
rot_lon = GalileanRotation.from_euler("z", -to_frame.galcen.lon)
234+
rot_lat = Rotate.from_euler("y", to_frame.galcen.lat)
235+
rot_lon = Rotate.from_euler("z", -to_frame.galcen.lon)
243236
# extra roll away from the Galactic x-z plane
244-
roll = GalileanRotation.from_euler("x", to_frame.roll - to_frame.roll0)
237+
roll = Rotate.from_euler("x", to_frame.roll - to_frame.roll0)
245238
# construct transformation matrix
246239
R = (roll @ rot_lat @ rot_lon).simplify()
247240

248241
# Translation by Sun-Galactic center distance around x' and rotate about y'
249242
# to account for tilt due to Sun's height above the plane
250243
z_d = u.ustrip("", to_frame.z_sun / to_frame.galcen.distance) # [radian]
251-
H = GalileanRotation.from_euler("y", u.Quantity(jnp.asin(z_d), "rad"))
244+
H = Rotate.from_euler("y", u.Quantity(jnp.asin(z_d), "rad"))
252245

253246
# Post-rotation spatial offset to Galactic center.
254-
offset_q = GalileanSpatialTranslation(
255-
-to_frame.galcen.distance * jnp.asarray([1, 0, 0])
256-
)
247+
offset_q = Add(-to_frame.galcen.distance * jnp.asarray([1, 0, 0]))
257248

258249
# Post-rotation velocity offset
259-
offset_v = VelocityBoost(to_frame.galcen_v_sun)
250+
offset_v = Add(to_frame.galcen_v_sun)
260251

261252
# Total Operator
262253
return R | offset_q | H | offset_v

src/coordinax/_src/frames/example.py

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,7 @@
1111

1212
from .base import AbstractReferenceFrame
1313
from coordinax._src.frames import api
14-
from coordinax._src.operators import (
15-
GalileanBoost,
16-
GalileanRotation,
17-
GalileanSpatialTranslation,
18-
Identity,
19-
Pipe,
20-
)
14+
from coordinax._src.operators import Add, Identity, Pipe, Rotate
2115

2216

2317
@final
@@ -34,9 +28,9 @@ class Alice(AbstractReferenceFrame):
3428
>>> op = cxf.frame_transform_op(alice, bob)
3529
>>> print(op)
3630
Pipe((
37-
GalileanSpatialTranslation(<CartesianPos3D: (x, y, z) [km]
31+
Add(<CartesianPos3D: (x, y, z) [km]
3832
[100000 10000 0]>),
39-
GalileanBoost(<CartesianVel3D: (x, y, z) [m / s]
33+
Add(<CartesianVel3D: (x, y, z) [m / s]
4034
[2.698e+08 0.000e+00 0.000e+00]>)
4135
))
4236
@@ -62,9 +56,9 @@ class Bob(AbstractReferenceFrame):
6256
>>> op = cxf.frame_transform_op(alice, bob)
6357
>>> print(op)
6458
Pipe((
65-
GalileanSpatialTranslation(<CartesianPos3D: (x, y, z) [km]
59+
Add(<CartesianPos3D: (x, y, z) [km]
6660
[100000 10000 0]>),
67-
GalileanBoost(<CartesianVel3D: (x, y, z) [m / s]
61+
Add(<CartesianVel3D: (x, y, z) [m / s]
6862
[2.698e+08 0.000e+00 0.000e+00]>)
6963
))
7064
@@ -117,9 +111,9 @@ def frame_transform_op(from_frame: Alice, to_frame: FriendOfAlice, /) -> Pipe:
117111
>>> op = cx.frames.frame_transform_op(alice, friend)
118112
>>> print(op)
119113
Pipe((
120-
GalileanSpatialTranslation(<CartesianPos3D: (x, y, z) [m]
114+
Add(<CartesianPos3D: (x, y, z) [m]
121115
[10 0 0]>),
122-
GalileanRotation([[ 0. -0.99999994 0. ]
116+
Rotate([[ 0. -0.99999994 0. ]
123117
[ 0.99999994 0. 0. ]
124118
[ 0. 0. 0.99999994]])
125119
))
@@ -133,8 +127,8 @@ def frame_transform_op(from_frame: Alice, to_frame: FriendOfAlice, /) -> Pipe:
133127
))
134128
135129
"""
136-
shift = GalileanSpatialTranslation.from_([10, 0, 0], "m")
137-
rotation = GalileanRotation.from_euler("Z", u.Quantity(90, "deg"))
130+
shift = Add.from_([10, 0, 0], "m")
131+
rotation = Rotate.from_euler("Z", u.Quantity(90, "deg"))
138132
return shift | rotation
139133

140134

@@ -154,9 +148,9 @@ def frame_transform_op(from_frame: Alice, to_frame: Bob, /) -> Pipe:
154148
>>> op = cxf.frame_transform_op(alice, bob)
155149
>>> print(op)
156150
Pipe((
157-
GalileanSpatialTranslation(<CartesianPos3D: (x, y, z) [km]
151+
Add(<CartesianPos3D: (x, y, z) [km]
158152
[100000 10000 0]>),
159-
GalileanBoost(<CartesianVel3D: (x, y, z) [m / s]
153+
Add(<CartesianVel3D: (x, y, z) [m / s]
160154
[2.698e+08 0.000e+00 0.000e+00]>)
161155
))
162156
@@ -175,8 +169,8 @@ def frame_transform_op(from_frame: Alice, to_frame: Bob, /) -> Pipe:
175169
))
176170
177171
"""
178-
shift = GalileanSpatialTranslation.from_([100_000, 10_000, 0], "km")
179-
boost = GalileanBoost.from_([269_813_212.2, 0, 0], "m/s")
172+
shift = Add.from_([100_000, 10_000, 0], "km")
173+
boost = Add.from_([269_813_212.2, 0, 0], "m/s")
180174
return shift | boost
181175

182176

@@ -200,10 +194,10 @@ def frame_transform_op(
200194
>>> op = cx.frames.frame_transform_op(friend, alice)
201195
>>> print(op)
202196
Pipe((
203-
GalileanRotation([[ 0. 0.99999994 0. ]
197+
Rotate([[ 0. 0.99999994 0. ]
204198
[-0.99999994 0. 0. ]
205199
[ 0. 0. 0.99999994]]),
206-
GalileanSpatialTranslation(<CartesianPos3D: (x, y, z) [m]
200+
Add(<CartesianPos3D: (x, y, z) [m]
207201
[-10 0 0]>)
208202
))
209203

src/coordinax/_src/frames/register_ops.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
"""Register for `coordinax.ops`."""
22

3-
__all__: list[str] = []
3+
__all__: tuple[str, ...] = ()
44

55

6+
from plum import dispatch
7+
68
from dataclassish import replace
79

810
from .coordinate import Coordinate
911
from coordinax._src.operators import AbstractOperator
1012

1113

12-
@AbstractOperator.__call__.dispatch # type: ignore[misc]
13-
def call(self: AbstractOperator, x: Coordinate, /) -> Coordinate:
14+
@dispatch
15+
def operate(self: AbstractOperator, obj: Coordinate, /) -> Coordinate:
1416
"""Apply the operator to a coordinate.
1517
1618
Examples
@@ -34,4 +36,4 @@ def call(self: AbstractOperator, x: Coordinate, /) -> Coordinate:
3436
3537
"""
3638
# TODO: take the frame into account
37-
return replace(x, data=self(x.data))
39+
return replace(obj, data=self(obj.data))

src/coordinax/_src/frames/xfm.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ class TransformedReferenceFrame(AbstractReferenceFrame, Generic[FrameT]):
3636
>>> import coordinax as cx
3737
>>> import coordinax.frames as cxf
3838
39-
>>> R = cx.ops.GalileanRotation([[0., -1, 0], [1, 0, 0], [0, 0, 1]])
39+
>>> R = cx.ops.Rotate([[0., -1, 0], [1, 0, 0], [0, 0, 1]])
4040
>>> frame = cxf.TransformedReferenceFrame(cxf.ICRS(), R)
4141
>>> frame
4242
TransformedReferenceFrame(
43-
base_frame=ICRS(), xop=GalileanRotation(rotation=f32[3,3])
43+
base_frame=ICRS(), xop=Rotate(rotation=f32[3,3])
4444
)
4545
4646
Let's transform a position from the base frame to the transformed frame:
@@ -93,11 +93,11 @@ def frame_transform_op(
9393
>>> import coordinax as cx
9494
>>> import coordinax.frames as cxf
9595
96-
>>> R = cx.ops.GalileanRotation([[0., -1, 0], [1, 0, 0], [0, 0, 1]])
96+
>>> R = cx.ops.Rotate([[0., -1, 0], [1, 0, 0], [0, 0, 1]])
9797
>>> frame = cxf.TransformedReferenceFrame(cxf.ICRS(), R)
9898
>>> frame
9999
TransformedReferenceFrame(
100-
base_frame=ICRS(), xop=GalileanRotation(rotation=f32[3,3])
100+
base_frame=ICRS(), xop=Rotate(rotation=f32[3,3])
101101
)
102102
103103
Let's transform a position from the base frame to the transformed frame:
@@ -126,11 +126,11 @@ def frame_transform_op(
126126
>>> import coordinax as cx
127127
>>> import coordinax.frames as cxf
128128
129-
>>> R = cx.ops.GalileanRotation([[0., -1, 0], [1, 0, 0], [0, 0, 1]])
129+
>>> R = cx.ops.Rotate([[0., -1, 0], [1, 0, 0], [0, 0, 1]])
130130
>>> frame = cxf.TransformedReferenceFrame(cxf.ICRS(), R)
131131
>>> frame
132132
TransformedReferenceFrame(
133-
base_frame=ICRS(), xop=GalileanRotation(rotation=f32[3,3])
133+
base_frame=ICRS(), xop=Rotate(rotation=f32[3,3])
134134
)
135135
136136
Let's transform a position from the base frame to the transformed frame:
@@ -159,7 +159,7 @@ def frame_transform_op(
159159
>>> import coordinax as cx
160160
>>> import coordinax.frames as cxf
161161
162-
>>> R = cx.ops.GalileanRotation([[0., -1, 0], [1, 0, 0], [0, 0, 1]])
162+
>>> R = cx.ops.Rotate([[0., -1, 0], [1, 0, 0], [0, 0, 1]])
163163
>>> frame1 = cxf.TransformedReferenceFrame(cxf.ICRS(), R)
164164
165165
>>> shift = cx.ops.GalileanSpatialTranslation.from_([1, 0, 0], "kpc")
Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,29 @@
11
"""Coordinax Operator package."""
2-
# ruff: noqa: F401
32

43
__all__ = [
4+
# Functional API
5+
"operate",
56
"simplify_op",
67
# Classes
78
"AbstractOperator",
8-
"Identity",
99
"AbstractCompositeOperator",
1010
"Pipe",
11-
# Galilean
12-
"AbstractGalileanOperator",
13-
"GalileanBoost",
14-
"GalileanOperator",
15-
"GalileanRotation",
16-
"GalileanSpatialTranslation",
17-
"GalileanTranslation",
18-
# Misc
19-
"VelocityBoost",
20-
# Utils
21-
"convert_to_pipe_operators",
11+
"Identity",
12+
"Rotate",
13+
"Add",
14+
"GalileanOp",
2215
]
2316

24-
from .api import simplify_op
17+
from .api import operate, simplify_op
2518
from .base import AbstractOperator
26-
from .boost import VelocityBoost
2719
from .composite import AbstractCompositeOperator
28-
from .galilean.base import AbstractGalileanOperator
29-
from .galilean.boost import GalileanBoost
30-
from .galilean.composite import GalileanOperator
31-
from .galilean.rotation import GalileanRotation
32-
from .galilean.spatial_translation import GalileanSpatialTranslation
33-
from .galilean.translation import GalileanTranslation
20+
from .galilean import GalileanOp
3421
from .identity import Identity
35-
from .pipe import Pipe, convert_to_pipe_operators
22+
from .pipe import Pipe
23+
from .rotate import Rotate
24+
from .translate import Add
3625

37-
# isort: split
38-
from . import compat, register_simplify
26+
# # isort: split
27+
# from . import compat, register_simplify
3928

40-
del register_simplify, compat
29+
# del register_simplify, compat

0 commit comments

Comments
 (0)