R can solve underdetermined linear systems:
A = matrix((1:12)^2,3,4,T)
B = 1:3
qr(A)$rank # 3
qr.solve(A, B) # solutions will have one zero, not necessarily the same one
# 0.1875 -0.5000 0.3125 0.0000
solve(qr(A, LAPACK = TRUE), B)
# 0.08333333 -0.18750000 0.00000000 0.10416667
(It gives one solution among the infinity of solutions).
However, if the rank (here 2) is lower than the number of rows (here 3), it won't work:
A = matrix(c((1:8)^2,0,0,0,0),3,4,T)
B = c(1,2,0)
A
# [,1] [,2] [,3] [,4]
# [1,] 1 4 9 16
# [2,] 25 36 49 64
# [3,] 0 0 0 0
qr.solve(A, B) # Error in qr.solve(A, B) : singular matrix
solve(qr(A, LAPACK = TRUE), B) # Error in qr.coef(a, b) : error code 3
but this system does have a solution!
I know that the general solution is to use SVD or generalized/pseudo inverse of A (see this question and its answers), but:
Is there a mean with solve
or qr.solve
to automatically reduce the system AX=B to an equivalent system CX=D of only rank(A) rows, for which qr.solve(C, D)
would simply work out-of-the-box?
Example:
C = matrix(c((1:8)^2),2,4,T)
D = c(1,2)
qr.solve(C, D)
# -0.437500 0.359375 0.000000 0.000000