Repository navigation
Commit 111ee8e
perf(base): speed up qqmul and qvmul (#215)
## Summary
- `qqmul` (Hamilton product) and `qvmul` (vector rotation) both used
`np.dot`/`np.cross` on 3-vectors, whose generic dispatch overhead -
built for arbitrary shapes/broadcasting - dominates cost at this size.
Same pattern as `isR`/`trnorm`/`tr2adjoint` in #213/#214.
- `qqmul`: replaced with explicit scalar Hamilton-product arithmetic.
Bit-identical to the prior result (verified to ~1 ULP over 2000 random
trials).
- `qvmul`: replaced the `q * pure(v) * conj(q)` sandwich (two full
Hamilton products through `qqmul`/`qpure`/`qconj`, each with its own
`getvector` re-validation, and each Hamilton product wasting work on a
zero scalar part) with the closed-form rotation identity `v' = v + 2s(w
x v) + 2 w x (w x v)` for `q = (s, w)`. This is a genuinely different
(though mathematically equivalent) formula, not just a re-expression of
the same one - so it is **not** bit-identical, verified instead to
~1e-14 absolute over 2000 random trials (with `v` scaled to magnitude
~10), well within floating-point noise.
- Net effect: `qqmul` ~10x faster (15.0us -> 1.4us isolated), `qvmul`
~24x faster (33.4us -> 1.4us isolated). End to end: `Q1 * v` drops from
~33us to ~4us (~9x). `Q1 * Q2` drops from ~19us to ~10us (~2x) - `qqmul`
itself is no longer the bottleneck there; the remainder is
`UnitQuaternion` construction overhead (in particular `qunit()`'s
`np.linalg.norm`/`np.r_` calls), a separate, un-addressed candidate for
a possible future fix in the same spirit, not attempted here.
## Test plan
- [x] `pytest tests/` - 343 passed
- [x] `qqmul` verified against the prior implementation over 2000 random
quaternion pairs (max diff ~1 ULP, i.e. bit-identical modulo
floating-point operation ordering)
- [x] `qvmul` verified against the prior implementation over 2000 random
(quaternion, vector) pairs, vector magnitude ~10 (max abs diff ~1.4e-14)
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>1 parent abe79e4 commit 111ee8e
1 file changed
Lines changed: 30 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
326 | 326 | | |
327 | 327 | | |
328 | 328 | | |
329 | | - | |
330 | | - | |
331 | | - | |
332 | | - | |
| 329 | + | |
| 330 | + | |
333 | 331 | | |
334 | | - | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
335 | 342 | | |
336 | 343 | | |
337 | 344 | | |
| |||
398 | 405 | | |
399 | 406 | | |
400 | 407 | | |
401 | | - | |
402 | | - | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
403 | 426 | | |
404 | 427 | | |
405 | 428 | | |
| |||
0 commit comments