Skip to contents

Mirrors the C# GaussJordanElimination::Solve(ref Matrix A, ref Matrix B): one full-pivot Gauss-Jordan reduction that produces a's inverse and, when b is supplied, the solution set of a %*% x = b. Unlike the C# ref, ref in-place API, a and b are never mutated in R – both results come back as new matrices.

Usage

gauss_jordan(a, b = NULL)

Arguments

a

a square numeric matrix, N x N.

b

a numeric matrix with N rows (the right-hand sides). Omit for the inverse alone, in which case solution is an N x 0 matrix.

Value

a list with elements inverse (a's inverse, N x N) and solution (N x ncol(b)).

Examples

a <- matrix(c(1, 3, 3, 1, 4, 3, 1, 3, 4), nrow = 3, byrow = TRUE)
gauss_jordan(a)$inverse
#>      [,1] [,2] [,3]
#> [1,]    7   -3   -3
#> [2,]   -1    1    0
#> [3,]   -1    0    1