Skip to content

Commit

Permalink
Refactor neg (#150)
Browse files Browse the repository at this point in the history
* refactor: neg

Signed-off-by: nstarman <nstarman@users.noreply.github.com>
  • Loading branch information
nstarman authored Aug 12, 2024
1 parent 54c040b commit cec85cd
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 17 deletions.
25 changes: 13 additions & 12 deletions src/coordinax/_base_acc.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,19 @@ def __neg__(self) -> "Self":
>>> from unxt import Quantity
>>> import coordinax as cx
>>> dr = cx.RadialVelocity(Quantity(1, "m/s"))
>>> -dr
RadialVelocity( d_r=Quantity[...]( value=i32[], unit=Unit("m / s") ) )
>>> dp = cx.PolarVelocity(Quantity(1, "m/s"), Quantity(1, "mas/yr"))
>>> neg_dp = -dp
>>> neg_dp.d_r
Quantity['speed'](Array(-1., dtype=float32), unit='m / s')
>>> neg_dp.d_phi
Quantity['angular frequency'](Array(-1., dtype=float32), unit='mas / yr')
"""
>>> d2r = cx.RadialAcceleration.constructor([1], "m/s2")
>>> -d2r
RadialAcceleration(
d2_r=Quantity[PhysicalType('acceleration')](value=i32[], unit=Unit("m / s2")) )
>>> d2p = cx.PolarAcceleration(Quantity(1, "m/s2"), Quantity(1, "mas/yr2"))
>>> neg_d2p = -d2p
>>> neg_d2p.d2_r
Quantity['acceleration'](Array(-1., dtype=float32), unit='m / s2')
>>> neg_d2p.d2_phi
Quantity['angular acceleration'](Array(-1., dtype=float32), unit='mas / yr2')
""" # noqa: E501
return replace(self, **{k: -v for k, v in field_items(self)})

# ===============================================================
Expand Down
14 changes: 14 additions & 0 deletions src/coordinax/_base_pos.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,20 @@ def __neg__(self) -> "Self":
"""Negate the vector.
The default implementation is to go through Cartesian coordinates.
Examples
--------
>>> import coordinax as cx
>>> vec = cx.CartesianPosition3D.constructor([1, 2, 3], "m")
>>> -vec
CartesianPosition3D(
x=Quantity[PhysicalType('length')](value=f32[], unit=Unit("m")),
y=Quantity[PhysicalType('length')](value=f32[], unit=Unit("m")),
z=Quantity[PhysicalType('length')](value=f32[], unit=Unit("m"))
)
>>> (-vec).x
Quantity['length'](Array(-1., dtype=float32), unit='m')
"""
cart = self.represent_as(self._cartesian_cls)
return (-cart).represent_as(type(self))
Expand Down
3 changes: 2 additions & 1 deletion src/coordinax/_base_vel.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ def __neg__(self) -> "Self":
--------
>>> from unxt import Quantity
>>> import coordinax as cx
>>> dr = cx.RadialVelocity(Quantity(1, "m/s"))
>>> dr = cx.RadialVelocity.constructor([1], "m/s")
>>> -dr
RadialVelocity( d_r=Quantity[...]( value=i32[], unit=Unit("m / s") ) )
Expand Down
1 change: 0 additions & 1 deletion src/coordinax/_d1/cartesian.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ def __neg__(self) -> "Self":
Examples
--------
>>> from unxt import Quantity
>>> import coordinax as cx
>>> q = cx.CartesianPosition1D.constructor([1], "kpc")
>>> -q
Expand Down
1 change: 0 additions & 1 deletion src/coordinax/_d3/cartesian.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ def __neg__(self) -> "Self":
Examples
--------
>>> from unxt import Quantity
>>> import coordinax as cx
>>> q = cx.CartesianPosition3D.constructor([1, 2, 3], "kpc")
>>> (-q).x
Expand Down
4 changes: 2 additions & 2 deletions src/coordinax/_dn/cartesian.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ def __neg__(self) -> "Self":
A 3D vector:
>>> q = cx.CartesianPositionND(Quantity([1, 2, 3], "kpc"))
>>> (-q).q
>>> vec = cx.CartesianPositionND(Quantity([1, 2, 3], "kpc"))
>>> (-vec).q
Quantity['length'](Array([-1., -2., -3.], dtype=float32), unit='kpc')
"""
Expand Down

0 comments on commit cec85cd

Please sign in to comment.