Click4Ai

47.

Hard

Implement QR decomposition of a matrix A into an orthogonal matrix Q and upper triangular matrix R, such that A = Q*R. Use the Gram-Schmidt process.

Algorithm:

1. Apply Gram-Schmidt to columns of A to get orthonormal columns → Q

2. Compute R = Q^T * A

Example:

Input: A = [[1, 1], [0, 1]]

Output: Q ≈ [[1, 0], [0, 1]], R ≈ [[1, 1], [0, 1]]

Constraints:

  • A is an m×n matrix where m >= n
  • Q is m×n orthogonal, R is n×n upper triangular
  • Return (Q, R) as numpy arrays
  • Test Cases

    Test Case 1
    Input: [[1, 1], [0, 1]]
    Expected: A = Q * R verified
    Test Case 2
    Input: [[1, 0], [0, 1]]
    Expected: A = Q * R verified
    Test Case 3
    Input: [[12, -51], [6, 167]]
    Expected: A = Q * R verified
    + 2 hidden test cases