feat: add Matrix.mmulByTranspose()#207
Open
lpatiny wants to merge 2 commits into
Open
Conversation
Adds `mmulByTranspose()`, which returns the matrix product of a matrix by its own transpose (`this · thisᵀ`). The result is symmetric, so only the upper triangle is computed and mirrored, and the transpose is never materialized — about twice as fast as `this.mmul(this.transpose())`. This is the Gram-style product at the heart of the normal equations (e.g. the Gauss-Newton Hessian `J·Jᵀ` in Levenberg-Marquardt). Measured ~2.4x faster than `mmul(transpose())` on a 48x2000 matrix, bit-for-bit identical. Assisted-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #207 +/- ##
==========================================
+ Coverage 64.83% 65.03% +0.19%
==========================================
Files 47 47
Lines 5625 5657 +32
Branches 954 961 +7
==========================================
+ Hits 3647 3679 +32
Misses 1967 1967
Partials 11 11 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
`mmulByTranspose(scale)` now computes `this · diag(scale) · thisᵀ`, the weighted Gram matrix, with `scale` a per-column factor. Without `scale` it is unchanged (`this · thisᵀ`). This covers weighted normal equations (`J·W·Jᵀ`) in a single symmetric, transpose-free pass — ~2.4x faster than `mmul(transpose().scale(...))`. Assisted-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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
Adds
Matrix.prototype.mmulByTranspose(scale?):J.mmulByTranspose()→J · Jᵀ(anm×mresult from anm×nmatrix).J.mmulByTranspose(scale)→J · diag(scale) · Jᵀ, the weighted Gram matrix, withscalea per-column factor.The result is symmetric, so only the upper triangle is computed and mirrored, and the transpose is never materialized.
Why
This is the Gram-style product at the heart of the (weighted) normal equations — e.g. the Gauss-Newton Hessian
J·W·Jᵀin Levenberg–Marquardt. Computing it asJ.mmul(J.transpose().scale(...))allocates a full transpose, makes a separate scaling pass, and computes alln²entries; this method does none of that.Numbers (48×2000 matrix)
mmul(transpose())J·Jᵀ(unweighted)J·diag(w)·Jᵀ(weighted)Unweighted result is bit-for-bit identical to
mmul(transpose()); weighted matches to ~1e-13 (summation order).Notes
src/matrix.js(implementation),matrix.d.ts(types),src/__tests__/matrix/utility.test.js(tests).npm testpasses (251 tests, eslint, prettier).🤖 Generated with Claude Code