I want to check if the determinant of a 22x22 matrix is a square of an integer or not.
So I have
> det(M)
[1] 1.849688e+29
> sqrt(det(M))
[1] 4.3008e+14
But now I can't see the decimal digits of this number.
So, if I run
> options(digits=22)
> sqrt(det(M))
[1] 430080000000001.1875000
I have this result, which means that it isn't square of an integer.
So, when I use the following function
check.integer <- function(N){
!length(grep("[^[:digit:]]", as.character(N)))
}
the result is that the number is an integer.
Your check.integer
function does not really check if something is an integer. It checks if whatever R's character representation of something happens to have a non-digit character in it. This is a pretty bad way of doing things. You want to know if something really is and integer and not just if it looks like one.
Besides, there is already the is.integer
function in R.
But this may not help in your case. R will only exactly represent integers up to a certain point, which is one of the many things stored in .Machine
. In particular:
.Machine$integer.max
If you want to check if a matrix determinant that may be larger than .Machine$integer.max
is a square in R you can consider going to arbitrary precision. The Rmpfr package should be able to do all this for you. It lets you have arbitrary precision matrices, has its own determinant
function, and has its own is.whole
function.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments