Skip to content

feat(matrix): add QR decomposition algorithm using Gram-Schmidt process#7427

Open
MD-Mushfiqur123 wants to merge 2 commits into
TheAlgorithms:masterfrom
MD-Mushfiqur123:add-qr-decomposition
Open

feat(matrix): add QR decomposition algorithm using Gram-Schmidt process#7427
MD-Mushfiqur123 wants to merge 2 commits into
TheAlgorithms:masterfrom
MD-Mushfiqur123:add-qr-decomposition

Conversation

@MD-Mushfiqur123
Copy link
Copy Markdown

Describe your change:

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests? -- Note: Please avoid changing both code and tests in a single pull request.
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Java files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Java naming conventions.
  • All new algorithms have a URL in its comments that points to Wikipedia or another similar explanation.
  • The build runs successfully with 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 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(mn^2) | Space: O(mn + n^2)

Reference

https://en.wikipedia.org/wiki/QR_decomposition

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-commenter
Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 87.09677% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 79.65%. Comparing base (4b8099c) to head (efecad4).

Files with missing lines Patch % Lines
...java/com/thealgorithms/matrix/QRDecomposition.java 87.09% 4 Missing and 4 partials ⚠️
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants