rmatrixmatrix-inversedeterminants

Solve function in R doesn't match inverse calculation


There seem to be a ton of questions about inverse functions, but they're not what I'm after (neither are many of the results in the matrix-inverse tag). I'm trying to use a function that gets the inverse of a matrix. As an example, this is the matrix I am using:

#### Create Matrix ####
mat <- matrix(
  c(4,2,7,6),
  nrow=2
)
mat

Shown below:

     [,1] [,2]
[1,]    4    7
[2,]    2    6

When I hand calculate the determinant and use the det function, I get the same answer for each solution to the inverse:

1/((4*6)-(7*2))*mat # 1/det
(1/det(mat))*mat # same

Shown below:

    [,1] [,2]
[1,]  0.6 -0.7
[2,] -0.2  0.4

However, if I use the supposed function for this, solve(mat) gives me this solution:

     [,1] [,2]
[1,]  0.4  0.7
[2,]  0.2  0.6

Why are the results different? Do I need to use an alternative function?

Edit

I restarted R and ran the same code with no change. Here is my session info:

R version 4.2.1 (2022-06-23 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 22621)

Matrix products: default

locale:
[1] LC_COLLATE=Chinese (Simplified)_China.utf8 
[2] LC_CTYPE=Chinese (Simplified)_China.utf8   
[3] LC_MONETARY=Chinese (Simplified)_China.utf8
[4] LC_NUMERIC=C                               
[5] LC_TIME=Chinese (Simplified)_China.utf8    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
[1] compiler_4.2.1  tools_4.2.1     rstudioapi_0.14

For some reason it's saying I have 4.2.1 installed but I'm pretty sure I'm using 4.2.2, so not sure if that matters or not. Running solve(mat) %*% mat gives me this matrix:

     [,1]         [,2]
[1,]    1 8.881784e-16
[2,]    0 1.000000e+00

Solution

  • At least one of us is confused.

    mat <- matrix(c(4,2,7,6),2)
    

    correct answers

    inv1 <- solve(mat)
    ## or
    inv2 <- matrix(c(mat[4],-mat[2], -mat[3], mat[1]), 2)/det(mat)
    all.equal(inv1, inv2)  ## TRUE
    all.equal(inv1 %*% mat, diag(2)) ## TRUE
    
    inv1
         [,1] [,2]
    [1,]  0.6 -0.7
    [2,] -0.2  0.4
    
    zapsmall(mat %*% inv1)
         [,1] [,2]
    [1,]    1    0
    [2,]    0    1
    

    incorrect answer

    inv3 <- mat/det(mat)
    all.equal(mat %*% inv3, diag(2))
    ## [1] "Mean relative difference: 0.8823529"
    
     inv3
         [,1] [,2]
    [1,]  0.4  0.7
    [2,]  0.2  0.6
    
    mat %*% inv3
         [,1] [,2]
    [1,]    3    7
    [2,]    2    5
    

    session info

    R Under development (unstable) (2023-02-25 r83903)
    Platform: x86_64-pc-linux-gnu (64-bit)
    Running under: Pop!_OS 22.04 LTS
    
    Matrix products: default
    BLAS:   /usr/local/lib/R/lib/libRblas.so 
    LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/liblapack.so.3;  LAPACK version 3.10.0
    
    locale:
     [1] LC_CTYPE=en_CA.UTF-8       LC_NUMERIC=C              
     [3] LC_TIME=en_CA.UTF-8        LC_COLLATE=en_CA.UTF-8    
     [5] LC_MONETARY=en_CA.UTF-8    LC_MESSAGES=en_CA.UTF-8   
     [7] LC_PAPER=en_CA.UTF-8       LC_NAME=C                 
     [9] LC_ADDRESS=C               LC_TELEPHONE=C            
    [11] LC_MEASUREMENT=en_CA.UTF-8 LC_IDENTIFICATION=C       
    
    time zone: America/Toronto
    tzcode source: system (glibc)
    
    attached base packages:
    [1] stats     graphics  grDevices datasets  utils     methods   base     
    
    loaded via a namespace (and not attached):
    [1] compiler_4.3.0 bspm_0.3.10