矩阵A的逆A-1可以用函数solve(),例如:
> A=matrix(rnorm(9),nrow=3,ncol=3)
> A
[,1] [,2] [,3]
[1,] -0.2915845 0.2831544 0.94493154
[2,] -1.6494678 0.6999185 -0.06292334
[3,] -0.7224015 -0.3906971 0.44799963
> solve(A)
[,1] [,2] [,3]
[1,] 0.2359821 -0.4050650 -0.5546321
[2,] 0.6405592 0.4507583 -1.2877720
[3,] 0.9391490 -0.2600663 0.2147417
验证AA-1=1:
> A%*%solve(A)
[,1] [,2] [,3]
[1,] 1.000000e+00 8.433738e-17 -1.341700e-18
[2,] 1.216339e-17 1.000000e+00 -4.667152e-17
[3,] -2.203641e-17 4.283954e-17 1.000000e+00
用round函数可以更好的得到结果:
> round(A%*%solve(A))
[,1] [,2] [,3]
[1,] 1 0 0
[2,] 0 1 0
[3,] 0 0 1
solve()函数也可以用来求解方程组ax=b。