| solve {RandomFieldsUtils} | R Documentation |
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.
solvex(a, b=NULL, logdeterminant=FALSE)
a |
a square real-valued 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, |
logdeterminant |
logical. whether the logarithm of the determinant should also be returned |
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 eigen value decomposition is tried.
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.
Martin Schlather, schlather@math.uni-mannheim.de
See chol.spam of the package spam
chol.spam in the package spam
if (FALSE) {
## This examples shows that 'solvex' 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)
system.time(inv2 <- solve(M, b))
system.time(inv1 <- solvex(M, b)) ## much faster in this case
range(inv2 - inv1)
stopifnot(all(abs(inv2 - inv1) < 10^{-9}))
}