-2

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
Shawn Hemelstrand
  • 2,676
  • 4
  • 17
  • 30
  • `solve()` gives me the first (correct) answer [((0.6, -0.7), (-0.2, 0.4)) in row-major order): `identical(zapsmall(solve(mat) %*% mat), diag(2))` is TRUE]. I suspect that you (or some package you've loaded) has redefined `solve()` in a wonky way. What is `find("solve")` ? Is this in a clean/vanilla R session? Results of `sessionInfo()` ? – Ben Bolker Mar 04 '23 at 05:07
  • I've edited my question with the `sessionInfo` included. – Shawn Hemelstrand Mar 04 '23 at 05:22
  • I'm confused. Your second result ((0.4, 0.7), (0.2, 0.6)) appears to be the result of the *incorrect* calculation `mat/(det(mat))` (if you were going to do this by hand you would need `matrix(c(mat[4],-mat[2], -mat[3], mat[1]), 2))/det(mat)` https://www.mathsisfun.com/algebra/matrix-inverse.html – Ben Bolker Mar 04 '23 at 05:23
  • This is precisely my point (that's actually the link where I was testing this from). The first result is correct, but every time I run `solve` it doesn't give me the correct answer. Also running `find("solve")` indicates that it is using the base R function. – Shawn Hemelstrand Mar 04 '23 at 05:24
  • what is `solve(mat) %*% mat` ? – Ben Bolker Mar 04 '23 at 05:31
  • Added that to the question. – Shawn Hemelstrand Mar 04 '23 at 05:34
  • The outcome of `solve(mat) %*% mat` is correct (if you use `zapsmall()` or `all.equal()` you'll see that it's effectively the identity matrix. I **strongly** suspect that you're just having a "thinko"/transposing results at some point. – Ben Bolker Mar 04 '23 at 05:36

1 Answers1

2

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   
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453