| invert_matrix | R Documentation |
Inverts a square matrix A using a dispatch rule that balances speed
and numerical stability:
If q < 100: try Cholesky (via
invert_spd_cpp()), falling through to LU
(invert_general_cpp()) and finally SVD pseudo-inverse if
the matrix is not positive-definite.
If q >= 100: route through the SVD pseudo-inverse path.
By default this is the dense LAPACK split-merge variant
(svd_pseudo_inverse(), paper Algorithm 2). When the user
opts in (see the method argument below), the iterative
truncated SVD from RSpectra::svds() is used instead –
faster when the matrix has effective rank much smaller than
q, slower otherwise.
The Cholesky fast path is roughly 2-3x faster than the original
kappa(A) + solve(A) R path on VCMM K matrices. See the Day 17
and Day 18 validation scripts in inst/validation/ for the
bit-equivalence and timing details.
invert_matrix(
A,
q = NULL,
verbose = FALSE,
use_cpp = TRUE,
method = c("auto", "lapack", "rspectra")
)
A |
Numeric square matrix to invert. |
q |
Optional integer. Routing dimension used to pick the inversion
strategy (defaults to |
verbose |
Logical. If |
use_cpp |
Logical. If |
method |
Character string, one of |
If you need to reproduce the original R-only path (e.g. for a
bit-equivalence test), pass use_cpp = FALSE.
A numeric matrix with the same dimensions as A.
Jalili, L. and Lin, L.-H. (2025). Scalable and Communication-Efficient Varying Coefficient Mixed-Effects Models.
set.seed(1)
A <- crossprod(matrix(rnorm(50), 10, 5)) + diag(5)
A_inv <- invert_matrix(A)
max(abs(A %*% A_inv - diag(5))) # ~ machine epsilon
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.