Skip to contents

Return the inverse of a square matrix when solve() cannot.

Usage

mat_inv(mat, safer_check = TRUE)

Arguments

mat

A square numeric matrix without NULL, NA, Inf or single case (dimension 1, 1) of 0.

safer_check

Single logical value. Perform some "safer" checks (see https://github.com/safer-r)? If TRUE, checkings are performed before main code running: 1) R classical operators (like "<-") not overwritten by another package because of the R scope and 2) required functions and related packages effectively present in local R lybraries. Set to FALSE if this fonction is used inside another "safer" function to avoid pointless multiple checkings.

Value

The inversed matrix.

See also

ginv, pinv which can calculates the Moore-Penrose generalized inverse of a matrix X.

Author

Gael Millot <gael.millot@pasteur.fr>

Yushi Han <yushi.han2000@gmail.com>

Haiding Wang <wanghaiding442@gmail.com>

Examples

mat1 = matrix(c(1,1,1,2,1,5,9,8,9), ncol = 3) ; mat_inv(mat = mat1) # use solve()
#>             [,1] [,2]       [,3]
#> [1,] -10.3333333    9  2.3333333
#> [2,]  -0.3333333    0  0.3333333
#> [3,]   1.3333333   -1 -0.3333333

mat1 = matrix(2, ncol = 1) ; mat_inv(mat = mat1)
#>      [,1]
#> [1,]  0.5