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