I want to invert a matrix which is a "band" diagonal matrix. The structure of the matrix is
The blue strip represents the elements that are non zero.All other element in white area are of zero value.
I write down the code in java to represent non zero elements.
import org.ujmp.core.Matrix;
public class Omegatest {
public static void main(String args[])throws Exception{
Matrix omega= Matrix.Factory.zeros(2000,2000);
Matrix Bigomega=Matrix.Factory.zeros(2000,2000);
for(int k1=0;k1<1999;k1++){
omega.setAsDouble(1, k1, k1);
omega.setAsDouble(-1, k1, k1 + 1);
omega.setAsDouble(-1, k1 + 1, k1);
omega.setAsDouble(1, k1 + 1, k1 + 1);
Bigomega = Bigomega.plus(omega);
omega.clear();
}
System.out.println(Bigomega.inv());
}
}
Now whenever I try to invert this matrix, it gives me an error that the matrix is singular.
If I slightly change the code and take two Sparse Matrix instead of normal matrix it gives me a big value like 1902.003 1903.005 in each cells.
I know there is a definite problem in the code.
But first I want to know the mathematical explanation of this two conditions.
Thank you.

The sum of the elements in each row (or column) is zero, hence the matrix is not invertible. The matrix looks like this $$ \pmatrix{ 1 & -1 \\ -1 & 2 & -1 \\ &\ddots& \ddots&\ddots\\ &&\ddots& \ddots&\ddots\\ &&&-1 & 2 & -1 \\ &&&&1 & -1 } $$
(It seems to be some sort of stiffness matrix for a differential equation with Neumann boundary conditions, where constant functions are in the null space.)