Repository navigation
Commit abe79e4
perf(base): speed up trnorm and tr2adjoint (#214)
## Summary
- `trnorm` (Gram-Schmidt re-orthonormalization used by e.g.
`SE3.__matmul__`/`@`) and `tr2adjoint` (adjoint matrix, used by
`SE3.Ad()`/`Twist3.Ad()`) both built small, fixed-size results
(3x3/4x4/6x6) via generic NumPy helpers -
`np.cross`/`unitvec`/`np.stack` in `trnorm`, `np.block` in `tr2adjoint`
- whose dispatch overhead (built for arbitrary shapes/broadcasting)
dominates cost at this size. Same pattern as the `isR`/`ishom` speedup
in #213.
- `trnorm`: replaced with explicit scalar cross-product/normalization
arithmetic and direct in-place array construction.
- `tr2adjoint`: replaced `np.block([[...]])` with a pre-allocated
`np.zeros((6,6), dtype=T.dtype)` and direct slice assignment.
`dtype=T.dtype` is preserved specifically so the function's documented
`:SymPy: supported` contract still holds for symbolic (`dtype=object`)
input.
- Net effect (isolated `timeit`, `trnorm`/`tr2adjoint` called directly):
`trnorm` ~12x faster (27.4us -> 2.3us), `tr2adjoint` ~2.7x faster (8.8us
-> 3.3us). End to end, `SE3 @ SE3` (which normalizes via `trnorm`) drops
from ~30us to ~4us. `SE3.Ad()`/`base.tr2adjoint()` calls on an
already-known SE3/rotation matrix land around ~3.3us.
- Note: `Twist3.Ad()` itself is not meaningfully faster from this change
- its cost is dominated by the separate `self.SE3()` -> `trexp()`
conversion step (matrix exponential), not by `tr2adjoint`. That's a
different, inherent cost (same class as `Twist3 * Twist3`'s "product of
exponentials"), not addressed here.
## Test plan
- [x] `pytest tests/` - 343 passed
- [x] Verified bit-for-bit numeric equivalence against the prior
implementation over 200 random SO(3)/SE(3) trials (both `trnorm` and
`tr2adjoint`, both 3x3 and 4x4 forms)
- [x] Verified symbolic (SymPy, `dtype=object`) equivalence for
`tr2adjoint` specifically, since it's the one with a documented `:SymPy:
supported` contract
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>1 parent 2829ced commit abe79e4
1 file changed
Lines changed: 36 additions & 16 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1606 | 1606 | | |
1607 | 1607 | | |
1608 | 1608 | | |
1609 | | - | |
1610 | | - | |
| 1609 | + | |
| 1610 | + | |
| 1611 | + | |
| 1612 | + | |
| 1613 | + | |
1611 | 1614 | | |
1612 | | - | |
1613 | | - | |
1614 | | - | |
| 1615 | + | |
| 1616 | + | |
| 1617 | + | |
| 1618 | + | |
1615 | 1619 | | |
1616 | | - | |
1617 | | - | |
1618 | | - | |
1619 | | - | |
| 1620 | + | |
| 1621 | + | |
| 1622 | + | |
| 1623 | + | |
| 1624 | + | |
| 1625 | + | |
| 1626 | + | |
| 1627 | + | |
| 1628 | + | |
| 1629 | + | |
| 1630 | + | |
| 1631 | + | |
| 1632 | + | |
| 1633 | + | |
| 1634 | + | |
| 1635 | + | |
| 1636 | + | |
| 1637 | + | |
| 1638 | + | |
1620 | 1639 | | |
1621 | 1640 | | |
1622 | 1641 | | |
| |||
2733 | 2752 | | |
2734 | 2753 | | |
2735 | 2754 | | |
2736 | | - | |
2737 | 2755 | | |
2738 | 2756 | | |
2739 | 2757 | | |
2740 | 2758 | | |
2741 | 2759 | | |
2742 | 2760 | | |
2743 | 2761 | | |
2744 | | - | |
2745 | | - | |
2746 | | - | |
2747 | | - | |
2748 | | - | |
2749 | | - | |
| 2762 | + | |
| 2763 | + | |
| 2764 | + | |
| 2765 | + | |
| 2766 | + | |
| 2767 | + | |
| 2768 | + | |
| 2769 | + | |
2750 | 2770 | | |
2751 | 2771 | | |
2752 | 2772 | | |
| |||
0 commit comments