Invert a batch of symmetric positive-definite matrices
Description
Batched counterpart of invert_mat: inverts a (batch, n, n) stack of symmetric positive-definite matrices in a single Cholesky factorisation call rather than looping invert_mat() over each slice. SpaNorm’s own model never needs this (its per-gene fit shares a single coefficient across all genes, so it only ever inverts one matrix at a time); it is exposed for downstream packages (e.g. spiDE) that need a genuinely per-gene/per-feature covariance – a different n x n matrix for every slice of the batch, varying with a per-gene working weight – where looping invert_mat()’s R-level tryCatch/Cholesky call once per gene is the dominant cost.
Usage
invert_mat_batched(mat)
Arguments
mat
a (batch, n, n) torch tensor, or a base R array of the same shape (dim(mat) == c(batch, n, n)).
Details
Unlike invert_mat()’s per-matrix tryCatch, a failure here (e.g. one non-SPD slice) falls back to a general solve for the whole batch, not just the failing slice – correctness is unaffected (the fallback solves every slice correctly regardless of conditioning), it is simply not the exact same numerical routine applied per-slice in that (rare) case.
Value
the batch of matrix inverses, in the same representation as mat.