Computes solve(A + tol*I) with an automatically chosen tolerance, matching SPM25's spm_inv. Includes a fast path for diagonal Matrix objects.

dcm_inv(A, TOL = NULL)

Arguments

A

Square numeric matrix (dense or Matrix).

TOL

Optional tolerance. If NULL, set automatically.

Value

Inverse matrix.

Examples

A <- matrix(c(2, 1, 1, 2), 2, 2)
dcm_inv(A)
#>            [,1]       [,2]
#> [1,]  0.6666667 -0.3333333
#> [2,] -0.3333333  0.6666667

# Unlike solve(), a singular matrix is regularised rather than an error
singular <- matrix(1, 2, 2)
dcm_inv(singular)
#>               [,1]          [,2]
#> [1,]  3.950526e+13 -3.950526e+13
#> [2,] -3.950526e+13  3.950526e+13