feat(matrix): add QR decomposition algorithm using Gram-Schmidt process#7427
Open
MD-Mushfiqur123 wants to merge 2 commits into
Open
feat(matrix): add QR decomposition algorithm using Gram-Schmidt process#7427MD-Mushfiqur123 wants to merge 2 commits into
MD-Mushfiqur123 wants to merge 2 commits into
Conversation
Decomposes a matrix A into an orthogonal matrix Q and an upper triangular matrix R such that A = Q * R. ### What This Adds **QRDecomposition.java** - Main algorithm implementation: - `decompose()` - Performs QR factorization using the Gram-Schmidt process - Returns a QR object containing both Q (orthogonal) and R (upper triangular) matrices - Validates input matrix using MatrixUtil.validateInputMatrix() - Throws ArithmeticException for rank-deficient matrices **QRDecompositionTest.java** - Unit tests: - Tests for 2x2 and 3x3 matrix decomposition - Verifies Q * R reconstruction equals original matrix - Validates Q columns are orthonormal - Confirms R is upper triangular - Tests identity matrix decomposition - Tests rank-deficient matrix rejection - Tests null and empty matrix rejection ### Algorithm The Gram-Schmidt process orthogonalizes columns iteratively: - For each column j, subtract projections onto previous orthogonal vectors - Normalize to get j-th column of Q - Store coefficients in R[i][j] Time: O(m*n^2) | Space: O(m*n + n^2) ### Reference https://en.wikipedia.org/wiki/QR_decomposition
MatrixUtil is in com.thealgorithms.matrix.utils package which causes compilation errors when referenced from com.thealgorithms.matrix. Inlined the validation logic (validateInputMatrix, hasValidRows, isJaggedMatrix) directly into QRDecomposition to resolve the issue.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #7427 +/- ##
============================================
+ Coverage 79.63% 79.65% +0.01%
- Complexity 7241 7263 +22
============================================
Files 800 801 +1
Lines 23608 23670 +62
Branches 4646 4659 +13
============================================
+ Hits 18800 18854 +54
- Misses 4055 4059 +4
- Partials 753 757 +4 ☔ View full report in Codecov by Sentry. 🚀 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.
Describe your change:
Checklist:
mvn clean verify.QR Decomposition (Gram-Schmidt Process)
Decomposes a matrix A into an orthogonal matrix Q and an upper triangular matrix R such that A = Q * R.
What This Adds
QRDecomposition.java - Main algorithm implementation:
decompose()- Performs QR factorization using the Gram-Schmidt processQRDecompositionTest.java - Unit tests:
Algorithm
The Gram-Schmidt process orthogonalizes columns iteratively:
Time: O(mn^2) | Space: O(mn + n^2)
Reference
https://en.wikipedia.org/wiki/QR_decomposition