Learn R Programming

lgspline (version 0.2.0)

invert: Matrix Inversion with Fallback Methods

Description

Attempts matrix inversion using multiple methods, falling back to more robust approaches if standard inversion fails.

Usage

invert(mat, include_warnings = FALSE)

Value

Inverted matrix or identity matrix if all methods fail

Arguments

mat

Square matrix to invert

include_warnings

Logical; default FALSE for current implementation.

Details

Tries methods in order:

1. Direct inversion using armaInv()

2. Generalized inverse using eigendecomposition

3. Returns identity matrix with warning if both fail

For eigendecomposition, uses a small ridge penalty (1e-16) for stability and zeroes eigenvalues below machine precision.

Examples

Run this code
## Well-conditioned matrix
A <- matrix(c(4,2,2,4), 2, 2)
invert(A) %**% A

## Singular matrix falls back to M.P. generalized inverse
B <- matrix(c(1,1,1,1), 2, 2)
invert(B) %**% B

Run the code above in your browser using DataLab