Skip to content

Commit faa90dd

Browse files
committed
feat(pose,quaternion): add __imatmul__ (@=) operator
`X @= Y` now compounds poses/quaternions in place with automatic normalization, mirroring the existing `X @ Y` (__matmul__) behaviour but writing the normalized result back into the left operand instead of returning a new one. Useful when a pose is updated incrementally over many cycles and you don't want to pay for a fresh object each time. Added to BasePoseMatrix (covers SO2/SE2/SO3/SE3) and Quaternion (covers Quaternion/UnitQuaternion).
1 parent 8b83c1f commit faa90dd

2 files changed

Lines changed: 40 additions & 2 deletions

File tree

‎spatialmath/baseposematrix.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1350,6 +1350,20 @@ def __imul__(left, right): # noqa
13501350
"""
13511351
return left.__mul__(right)
13521352

1353+
def __imatmul__(left, right): # noqa
1354+
"""
1355+
Overloaded ``@=`` operator (superclass method)
1356+
1357+
:return: Product of two operands with normalization
1358+
:rtype: Pose instance or NumPy array
1359+
:raises ValueError: for incompatible arguments
1360+
1361+
- ``X @= Y`` compounds the poses ``X`` and ``Y`` and places the normalized result in ``X``
1362+
1363+
:seealso: ``__imul__`` :meth:`__matmul__`
1364+
"""
1365+
return left.__matmul__(right)
1366+
13531367
def __truediv__(left, right): # pylint: disable=no-self-argument
13541368
"""
13551369
Overloaded ``/`` operator (superclass method)

‎spatialmath/quaternion.py‎

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,32 @@ def __imul__(
666666
"""
667667
return left.__mul__(right)
668668

669+
def __imatmul__(
670+
left, right: Quaternion
671+
) -> bool: # lgtm[py/not-named-self] pylint: disable=no-self-argument
672+
"""
673+
Overloaded ``@=`` operator
674+
675+
:return: product
676+
:rtype: Quaternion
677+
:raises: ValueError
678+
679+
``q1 @= q2`` sets ``q1 := qnorm(q1 * q2`)`
680+
681+
Example:
682+
683+
.. runblock:: pycon
684+
685+
>>> from spatialmath import Quaternion
686+
>>> q = UnitQuaternion.Eul([0.1, 0.2, 0.3])
687+
>>> q @= Quaternion.Eul([0.3, 0.4, 0.5])
688+
>>> print(q)
689+
690+
691+
:seealso: :func:`__mul__`
692+
"""
693+
return left.__mul__(right)
694+
669695
def __pow__(self, n: int) -> Quaternion:
670696
"""
671697
Overloaded ``**`` operator
@@ -1883,8 +1909,6 @@ def __matmul__(
18831909
- ``q1 @ q2`` is the Hamilton product of ``q1`` and ``q2``, both unit
18841910
quaternions, followed by explicit normalization.
18851911
1886-
- `` q1 @= q2`` as above.
1887-
18881912
.. note:: This operator is functionally equivalent to ``*`` but is more
18891913
costly. It is useful for cases where a pose is incrementally update
18901914
over many cycles.

0 commit comments

Comments
 (0)