문제

I understand that sometimes in some float number computations, R makes some approximations leading to errors like 9.143243e-16.

But why can I get a determinant (with det command) like 9.143243e-16 when I only work with 3x3 matrix having only integers numbers?

Where do the 9.143243e-16 come from, in such a situation where I only had integers numebrs? Determinants involving integers numbers only should give an integer number, right ?

Thank you in advance.

PS : here is an example (in 4x4, but I could find one in 3x3 as well) (on R software x86 version) :

C=matrix(c(9,3,12,6,-3,-1,-4,-2,2,-2,4,0,5,-1,8,2),4,4,byrow=TRUE)
det(C)
[1] -1.923137e-30
도움이 되었습니까?

해결책

As a general rule, you should never expect exact answers from floating point calculations. You are correct that in principle, the determinant of an integer matrix, which involves only addition, subtraction and multiplication, should give an integer result. And this would (probably) be the case if R used the naive algorithm to compute the determinant.

However, the naive algorithm involves a summation over all N! permutations, which is enormously inefficient, so instead R uses some other method. I don't know what this method is -- you can look at the C source if you want, but my guess is that they do a matrix decomposition X=LU and then return det(X)=det(L)det(U), where the determinant of L and U involves simply multiplying the diagonals.

Some more info can be found by looking at the function det, shown below. As you can see, what it actually does is computes the log of the determinant, and then exponentiates it, so there is no reason to believe the result is going to be an integer.

> det
function (x, ...) 
{
    z <- determinant(x, logarithm = TRUE, ...)
    c(z$sign * exp(z$modulus))
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top