I have an algorithm which involves generating a random square matrix and then inverting it. To avoid the program interrupted by numerically singular matrix, I wish to do something like the pseudo-code below;
# generate A
while(A is numerically singular){
# generate A again
}
B = inv(A)
However, I do not know how to write the condition A is singular.
One possible solution is calling the matrixcalc::is.singular.matrix() function, but matlib::inv determines singularity by pivoting and is.singular.matrix() determines it by determinant, so this would not be a valid solution as some matrices can have reasonable determinant but ill-conditioned pivot. For instance, in the following code:
library(matlib)
library(matrixcalc)
A <- matrix(c(10^9, 10^9-1, 1, 1), nrow=2, ncol=2)
check <- is.singular.matrix(A)
B <- matlib::inv(A)
While check will be FALSE, inv will return an error:
Error in Inverse(X, tol = sqrt(.Machine$double.eps), ...) : X is numerically singular