feat(pose,quaternion): add __imatmul__ (@=) operator#199
Open
petercorke wants to merge 2 commits into
Open
Conversation
`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).
Quaternion.__imatmul__'s own docstring claimed `q1 @= q2` sets `q1 := qnorm(q1 * q2)`, but the implementation just delegated to __mul__ - identical to plain *=, no normalization at all, contradicting both the docstring and the entire point of adding a separate @= operator. UnitQuaternion.__matmul__ (pre-existing, unchanged) already does this correctly via smb.qunit(smb.qqmul(x, y)) - qunit being the normalizer; qnorm just returns the scalar magnitude, so it was never actually the right function despite the docstring's wording. Fixed by having __imatmul__ delegate to `left @ right` instead of left.__mul__(right). Deliberately not left.__matmul__(right): plain Quaternion has no __matmul__ (only UnitQuaternion defines one, with normalization), and calling the dunder directly as a plain attribute bypasses Python's normal operator fallback, raising a confusing AttributeError instead of the same TypeError `q1 @ q2` already raises for plain Quaternion. `left @ right` matches @'s behaviour exactly in both cases: normalizes for UnitQuaternion, raises consistently for Quaternion. Also fixed the docstring's `-> bool` return type (should be `-> Quaternion`) and its example, which used Quaternion.Eul() - a method that only exists on UnitQuaternion. Tests: added @= coverage for UnitQuaternion (must match @, not *) and for plain Quaternion (must raise TypeError, matching @, not silently degrade to *=).
|
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
X @= Ynow works as the augmented-assignment form of the existingX @ Y(
__matmul__, which composes with normalization) — previously onlyX *= Yexisted, which composes without normalization. Useful when a pose or unit
quaternion is updated incrementally over many cycles and you want the
normalized form without writing
X = X @ Yby hand.Added to
BasePoseMatrix(coversSO2/SE2/SO3/SE3) andQuaternion(covers
Quaternion/UnitQuaternion).Note on semantics: like the existing
__imul__, this doesn't mutate theobject in place —
__imatmul__returns a new (normalized) object and Pythonrebinds the name, same as
X = X @ Y. That matches the existing*=pattern in this codebase exactly, just saves writing
X = X @ Y.A bug found while adding test coverage
This started as a cherry-pick of old, never-merged WIP work. While writing
tests I found
Quaternion.__imatmul__'s docstring claimedq1 @= q2setsq1 := qnorm(q1 * q2), but the implementation just delegated to__mul__— identical to plain
*=, no normalization at all, contradicting both thedocstring and the entire point of adding
@=.UnitQuaternion.__matmul__(pre-existing, unchanged) already normalizescorrectly via
smb.qunit(smb.qqmul(x, y))—qunitbeing the actualnormalizer;
qnormjust returns the scalar magnitude, so it was neverreally the right function despite the docstring's wording.
Fixed by having
__imatmul__delegate toleft @ rightinstead ofleft.__mul__(right). Deliberately notleft.__matmul__(right)either:plain
Quaternionhas no__matmul__(onlyUnitQuaterniondefines one),and calling the dunder directly as a plain attribute bypasses Python's
normal operator fallback, raising a confusing
AttributeErrorinstead ofthe
TypeErrorthatq1 @ q2already raises consistently for plainQuaternion.left @ rightmatches@'s behaviour exactly in both cases.Also fixed the docstring's
-> boolreturn type annotation (should be-> Quaternion) and its example, which calledQuaternion.Eul()— amethod that only exists on
UnitQuaternion.Testing
@=coverage forSO3/SE3(must match@) alongside theexisting
*=tests intest_pose3d.py.@=coverage forUnitQuaternion(must match@, not*) and forplain
Quaternion(must raiseTypeError, matching@, not silentlydegrade to
*=) intest_quaternion.py.black --checkclean at the pinned 23.10.0.