Skip to content

Commit 9f1a49d

Browse files
petercorketaughz
authored andcommitted
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 6e1edea commit 9f1a49d

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
@@ -1360,6 +1360,20 @@ def __imul__(left, right): # noqa
13601360
"""
13611361
return left.__mul__(right)
13621362

1363+
def __imatmul__(left, right): # noqa
1364+
"""
1365+
Overloaded ``@=`` operator (superclass method)
1366+
1367+
:return: Product of two operands with normalization
1368+
:rtype: Pose instance or NumPy array
1369+
:raises ValueError: for incompatible arguments
1370+
1371+
- ``X @= Y`` compounds the poses ``X`` and ``Y`` and places the normalized result in ``X``
1372+
1373+
:seealso: ``__imul__`` :meth:`__matmul__`
1374+
"""
1375+
return left.__matmul__(right)
1376+
13631377
def __truediv__(left, right): # pylint: disable=no-self-argument
13641378
"""
13651379
Overloaded ``/`` operator (superclass method)

‎spatialmath/quaternion.py‎

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

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

0 commit comments

Comments
 (0)