Learn R Programming

RandomFieldsUtils (version 0.1.0)

solve: Solve a System of Equations for Positive Definite Matrices

Description

This function solves the equality $a x = b$ for $x$ where $a$ is a positive definite matrix and $b$ is a vector or a matrix. It is slightly faster than the inversion by the cholesky decomposition and clearly faster than solve. It also returns the logarithm of the determinant at no additional computational costs.

Usage

solvePosDef(a, b=NULL, logdeterminant=FALSE)

Arguments

a
a square numeric or complex matrix containing the coefficients of the linear system. Logical matrices are coerced to numeric.
b
a numeric or complex vector or matrix giving the right-hand side(s) of the linear system. If missing, b is taken to be an identity matrix and solvePosDef will return the inverse of a.
logdeterminant
logical. whether the logarithm of the determinant should also be returned

Value

  • If logdeterminant=FALSE the function returns a vector or a matrix, depending on b which is the solution to the linear equation. Else the function returns a list containing both the solution to the linear equation and the logarithm of the determinant of a.

Details

If the matrix is diagonal direct calculations are performed.

Else if the matrix is sparse the package spam is used.

Else the Cholesky decomposition is tried. If it fails, the SVD is tried.

References

See chol.spam of the package spam

See Also

chol.spam in the package spam

Examples

Run this code
## This examples shows that 'solvePosDef' can be much faster
## than 'solve'

## creating a covariance matrix for a temporal process
covmatrix <- function(model, x) {
  x <- as.matrix(dist(x))
  return(eval(substitute(model)))
}

size <- 600
x <- runif(size, 0, size / 10)
M <- covmatrix((1 - x) * (x < 1) , x) ## Askey's model of covariance
b <- seq(0, 1, len=size)
unix.time(inv2 <- solve(M, b))
unix.time(inv1 <- solvePosDef(M, b)) ## much faster in this case
range(inv2 - inv1)

Run the code above in your browser using DataLab