Learn R Programming

npreg (version 1.1.0)

psolve: Pseudo-Solve a System of Equations

Description

This generic function solves the equation a %*% x = b for x, where b can be either a vector or a matrix. This implementation is similar to solve, but uses a pseudo-inverse if the system is computationally singular.

Usage

psolve(a, b, tol)

Value

If b is missing, returns the (pseudo-)inverse of a. Otherwise returns psolve(a) %*% b.

Arguments

a

a rectangular numeric matrix containing the coefficients of the linear system.

b

a numeric vector or matrix giving the right-hand side(s) of the linear system. If missing, b is taken to be an identity matrix and solve will return the (pseudo-)inverse of a.

tol

the tolerance for detecting linear dependencies in the columns of a. The default is .Machine$double.eps.

Author

Nathaniel E. Helwig <helwig@umn.edu>

Details

If a is a symmetric matrix, eigen is used to compute the (pseudo-)inverse. This assumes that a is a positive semi-definite matrix. Otherwise svd is used to compute the (pseudo-)inverse for rectangular matrices.

References

Moore, E. H. (1920). On the reciprocal of the general algebraic matrix. Bulletin of the American Mathematical Society, 26, 394-395. tools:::Rd_expr_doi("10.1090/S0002-9904-1920-03322-7")

Penrose, R. (1955). A generalized inverse for matrices. Mathematical Proceedings of the Cambridge Philosophical Society, 51(3), 406-413. tools:::Rd_expr_doi("10.1017/S0305004100030401")

See Also

msqrt

Examples

Run this code
# generate X
set.seed(0)
X <- matrix(rnorm(100), 20, 5)
X <- cbind(X, rowSums(X))

# pseudo-inverse of X  (dim = 6 by 20)
Xinv <- psolve(X)

# pseudo-inverse of crossprod(X)  (dim = 6 by 6)
XtXinv <- psolve(crossprod(X))

Run the code above in your browser using DataLab