Skip to content

perf(base): speed up isR/ishom/ishom2 orthogonality checks - #213

Open
petercorke wants to merge 1 commit into
rai-opensource:masterfrom
petercorke:perf/speed-up-isR-checks
Open

perf(base): speed up isR/ishom/ishom2 orthogonality checks#213
petercorke wants to merge 1 commit into
rai-opensource:masterfrom
petercorke:perf/speed-up-isR-checks

Conversation

@petercorke

Copy link
Copy Markdown
Collaborator

Summary

  • isR (SO(2)/SO(3) orthogonality test used by SE3/SO3/SE2/SO2 construction with check=True) previously went through np.linalg.det/np.linalg.norm/np.eye, whose generic dispatch overhead dominates cost for such small (2x2/3x3) matrices. Added explicit fast paths for the 3x3 and 2x2 cases using a hand-written cofactor-expansion determinant and a squared-residual orthogonality check, falling back to the original generic implementation for any other size.
  • ishom/ishom2 bottom-row validity checks replaced all(T[-1,:] == np.array([...])) (array allocation + Python-level all() over a NumPy bool array) with direct scalar comparisons.
  • Net effect: isR is roughly 2x faster standalone, and SE3(T, check=True) construction is roughly 30-40% faster end to end.

Test plan

  • pytest tests/ — 343 passed
  • Verified pre-existing behaviour for symbolic (SymPy dtype=object) matrices with check=True is unchanged (was already unsupported before this change, for unrelated reasons - np.linalg.det/norm don't support dtype=object either)

🤖 Generated with Claude Code

np.linalg.det/norm and np.eye carry large dispatch overhead relative
to the actual work for 2x2/3x3 matrices, dominating SE3/SO3
constructor cost when check=True. Replace with explicit cofactor-
expansion determinants and a squared Frobenius residual for the 2x2
and 3x3 cases (falling back to the generic path otherwise), and
replace the bottom-row `all(... == np.array(...))` checks in
ishom/ishom2 with direct scalar comparisons.

~2x faster isR, ~30-40% faster SE3(T, check=True) construction.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@codecov-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 94.73684% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
spatialmath/base/transformsNd.py 94.73% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants