Ginv
returns an arbitrary generalized inverse of the matrix A
, using gaussianElimination
.
Usage
Ginv(A, tol = sqrt(.Machine$double.eps), verbose = FALSE, fractions = FALSE)
Arguments
- A
numerical matrix
- tol
tolerance for checking for 0 pivot
- verbose
logical; if
TRUE
, print intermediate steps- fractions
logical; if
TRUE
, try to express non-integers as rational numbers, using thefractions
function; if you require greater accuracy, you can set thecycles
(default 10) and/ormax.denominator
(default 2000) arguments tofractions
as a global option, e.g.,options(fractions=list(cycles=100, max.denominator=10^4))
.
Details
A generalized inverse is a matrix \(\mathbf{A}^-\) satisfying \(\mathbf{A A^- A} = \mathbf{A}\).
The purpose of this function is mainly to show how the generalized inverse can be computed using Gaussian elimination.
See also
ginv
for a more generally usable function
Examples
A <- matrix(c(1,2,3,4,5,6,7,8,10), 3, 3) # a nonsingular matrix
A
#> [,1] [,2] [,3]
#> [1,] 1 4 7
#> [2,] 2 5 8
#> [3,] 3 6 10
Ginv(A, fractions=TRUE) # a generalized inverse of A = inverse of A
#> [,1] [,2] [,3]
#> [1,] -2/3 -2/3 1
#> [2,] -4/3 11/3 -2
#> [3,] 1 -2 1
round(Ginv(A) %*% A, 6) # check
#> [,1] [,2] [,3]
#> [1,] 1 0 0
#> [2,] 0 1 0
#> [3,] 0 0 1
B <- matrix(1:9, 3, 3) # a singular matrix
B
#> [,1] [,2] [,3]
#> [1,] 1 4 7
#> [2,] 2 5 8
#> [3,] 3 6 9
Ginv(B, fractions=TRUE) # a generalized inverse of B
#> [,1] [,2] [,3]
#> [1,] -3/4 0 7/12
#> [2,] 0 0 0
#> [3,] 1/4 0 -1/12
B %*% Ginv(B) %*% B # check
#> [,1] [,2] [,3]
#> [1,] 1 4 7
#> [2,] 2 5 8
#> [3,] 3 6 9