All problems
QR Solve Small System
Problem
Solve the least squares problem A*x = b for a matrix A (n x 2) with linearly independent columns using the QR factorization A = Q*R.
Build Q from the columns of A with Gram-Schmidt, then solve R*x = Q^T*b by back substitution. Return [x0, x1].
Hint
For full column rank A, Q spans the same column space, so the normal equations become R*x = Q^T*b.
Test cases
case 1
in: [[[1,1],[1,2],[1,3]],[1,2,3]]
expected: [0,1]
case 2
in: [[[1,0],[0,1]],[3,4]]
expected: [3,4]
case 3
in: [[[1,1],[1,-1]],[2,0]]
expected: [1,1]
case 4
in: [[[1,0],[1,1],[1,2]],[1,2,2]]
expected: [1.1666666666666667,0.5]
Starter codePython
def qr_solve(A, b):
# Your code here
pass4 test cases · run your code against them in the editor