feat: add column pivoting to the QR decomposition - #219
Closed
tayal-sarthak wants to merge 5 commits into
Closed
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #219 +/- ##
==========================================
+ Coverage 68.88% 69.59% +0.70%
==========================================
Files 49 49
Lines 5904 5976 +72
Branches 1060 1098 +38
==========================================
+ Hits 4067 4159 +92
+ Misses 1824 1804 -20
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 #125.
Summary
The issue asks for a QR decomposition with column pivoting, so that a least square problem whose matrix is not of full rank can be solved, the way LINEST does. The references gathered in the thread are the LAPACK guide on rank deficient least squares along with the
nalgebraimplementation.The reported symptom is that
QrDecomposition.solvegives up on a matrix that is not of full rank. What happens is worse than giving up. Take a 4x3 matrix whose third column is twice the second minus the first:isFullRankcompares the diagonal of R against exact zero, which rounding almost never produces, so the guard lets the matrix through and the caller receives values around 1e14 with no warning.What pivoting gives
At each step the column carrying the largest remaining norm is moved into place. The diagonal of R then comes out non increasing, which is what makes the rank readable: the negligible entries collect at the end rather than sitting anywhere along the diagonal.
rankcounts the diagonal entries of R abovemax(m, n) * max|Rdiag| * EPSILON.columnPermutationVectorgives the column of the input sitting at each position, so thatA[:, columnPermutationVector] = Q R.solvereturns the basic solution when the input is not of full rank, the one holding at mostranknon zero components with the rest pinned to zero. The rows are permuted back so they line up with the columns of the input.On the matrix above,
rankreports 2, and the solution satisfies the normal equations,Aᵀ(Ax - b)coming out at 1e-14 rather than the 1e14 seen before.Compatibility
pivotingis off by default and every existing path is untouched. Without it the permutation is the identity,solvestill refuses a matrix that is not of full rank with the same message, andisFullRankkeeps its exact zero comparison.