| spam solve {spam} | R Documentation |
backsolve and forwardsolve solve a system
of linear equations where the coefficient matrix
is upper or lower triangular.
solve solves a linear system or computes the inverse
of a matrix if the right-hand-side is missing.
solve(a, b, ...) backsolve(r, x, ...) forwardsolve(l, x, ...)
a |
symmetric positive definite matrix of class spam. |
l,r |
object of class spam or spam.chol.method returned by the function
chol. |
x,b |
vector or regular matrix of right-hand-side(s) of a system of linear equations. |
... |
further arguments passed to or from other methods, see Details below. |
We can solve A %*% x = b by first computing the Cholesky decomposition A =
t(R)%*%R), then solving t(R)%*%y = b for y, and
finally solving R%*%x = y for x.
solve combines chol, a Cholesky decomposition of a
symmetric positive definite sparse matrix, with forwardsolve and
then backsolve.
forwardsolve and backsolve solve a system of linear
equations where the coefficient matrix is lower (forwardsolve) or
upper (backsolve) triangular. Usually, the triangular matrix is
result from a chol call and it is not required to transpose it
for forwardsolve. Note that arguments of the default
methods k, upper.tri and transpose do not have any
effects here.
If the right-hand-side in solve is missing it will compute
the inverse of a matrix. For details about the specific Cholsesky
decomposition, see chol.
Recall that the Cholesky factors are from ordered matrices.
There is intentionally no S3 distinction between the classes
spam and spam.chol.method.
Reinhard Furrer, based on Ng and Peyton (1993) Fortran routines
See references in chol.
# Generate multivariate form a covariance inverse:
# (usefull for GRMF)
set.seed(13)
n <- 25 # dimension
N <- 1000 # sample size
Sigmainv <- .25^abs(outer(1:n,1:n,"-"))
Sigmainv <- as.spam( Sigmainv, eps=1e-4)
Sigma <- solve( Sigmainv) # for verification
iidsample <- array(rnorm(N*n),c(n,N))
mvsample <- backsolve( chol(Sigmainv), iidsample)
norm( var(t(mvsample)) - Sigma, type="HS")
# compare with:
mvsample <- backsolve( chol(as.matrix( Sigmainv)), iidsample)
norm( var(t(mvsample)) - Sigma, type="HS")
# 'solve' step by step:
b <- rnorm( n)
R <- chol(Sigmainv)
norm( backsolve( R, forwardsolve( R, b))-
solve( Sigmainv, b),type="HS")
norm( backsolve( R, forwardsolve( R, diag(n)))- Sigma,type="HS")