#### nonSingular.R Dick Lane, 2014-06-19
## create n by n integer matrix with its inverse also being an integer matrix
#### usage:
## a <- nonSingular(5)
## b <- solve(a)
## a %*% b ## rounds to 5 by 5 identity matrix
## round( b %*% a ) ## 5 by 5 identity matrix
Low <- function( n = 2L ){
#### input: n is integer >= 2
#### output: lower triangular matrix of rank n, +/-1 on diagonal
## n by n matrix, diagonal contains random sample of 1 and -1
M <- diag( sample( c(-1,1), size=n, replace=T ) )
## elements below diagonal are independent random samples from -9:9
for (i in 2:n){
for (j in 1:(i-1))
M[i,j] = sample(-9:9, 1)
}
## non-singular lower triangular matrix
## det(M), determinant of M is product of its diagonal elements
M
}
nonSingular <- function( n = 2L ){
#### input: n
#### output: n by n integer matrix whose inverse is also an integer matrix
## coerce n to be integer, 2 or larger
n <- max(2L, round(abs(c(n))) )
## product of unitary lower and upper triangular integer matrices
Low(n) %*% t(Low(n))
}