Skip to content
Prev 360287 / 398503 Next

Solving sparse, singular systems of equations

Your code is a mess. 

A singular square system of linear equations has an infinity of solutions if a solution exists at all.
How that works you can find here: https://en.wikipedia.org/wiki/System_of_linear_equations
in the section "Matrix solutions".

For your simple example you can do it like this:

library(MASS)
Ag <- ginv(A)	# pseudoinverse

xb <- Ag %*% b # minimum norm solution

Aw <- diag(nrow=nrow(Ag)) - Ag %*% A  # see the Wikipedia page
w <- runif(3)
z <- xb + Aw %*% w
A %*% z - b

N <- Null(t(A))	 # null space of A;  see the help for Null in package MASS
A %*% N
A %*% (xb + 2 * N) - b

For sparse systems you will have to approach this differently; I have no experience with that.

Berend