fix: reject matrices with fewer rows than columns in the LU and QR decompositions - #218
Closed
tayal-sarthak wants to merge 6 commits into
Closed
fix: reject matrices with fewer rows than columns in the LU and QR decompositions#218tayal-sarthak wants to merge 6 commits into
tayal-sarthak wants to merge 6 commits into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #218 +/- ##
==========================================
+ Coverage 68.88% 69.88% +0.99%
==========================================
Files 49 49
Lines 5904 5910 +6
Branches 1060 1084 +24
==========================================
+ Hits 4067 4130 +63
+ Misses 1824 1767 -57
Partials 13 13 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
Closes #203.
Summary
The issue asks whether the SVD is broken for matrices carrying more columns than rows. It is not, the decomposition is an economy one and it reconstructs its input exactly. What the report gets right is that this is undocumented, and looking into it turned up two neighbouring decompositions that genuinely fall over on the same input shape.
The SVD is correct
For the 2x3 matrix
[[1, 2, 3], [4, 5, 6]], both withautoTransposeon and off,U * diag(s) * Vᵀreturns the input to machine precision. The economy form reportsmin(m, n)singular values, so the vectors it hands back do not span the null space of a wide matrix. That is a property of the economy form, not a defect. This PR documents the shape contract along with the effect ofautoTransposein the typings and the README.LuDecomposition and QrDecomposition genuinely break
Both are ports of routines defined for a matrix with at least as many rows as columns. Neither checked. Feeding them a 2x3 matrix on
main:new LuDecomposition(wide).isSingular()TypeError: Cannot read properties of undefined (reading '2')new LuDecomposition(wide).solve(b)TypeErrornew QrDecomposition(wide).orthogonalMatrixTypeError: Cannot set properties of undefined (setting '2')new QrDecomposition(wide).householderVectorsTypeError: Cannot read properties of undefinedThe accessors that avoid the crash are worse, since they hand back a triangular factor of the wrong shape with no indication anything went wrong.
Both constructors now reject that input:
solveroutes a non square left hand side toQrDecomposition, sosolve(wideMatrix, b)reports the shape now rather than theMatrix is rank deficientit produced before. Callers with a wide system wantpseudoInverse, along withsolve(a, b, true)to go through the SVD.Nothing that worked before stops working. Every one of these calls already failed, either loudly with an internal error or quietly with a meaningless factor.
Notes
The
Matrix must be squareerror ondeterminantis untouched. A 3x2 matrix still reaches it, a 2x3 matrix is now stopped one step earlier by the constructor.