mchol {mgcv} | R Documentation |
Sparse chol
function
Description
A wrapper for the Cholesky
function from the Matrix
package that produces the sparse pivoted Cholesky factor of a positive definite matrix, and returns the pivot vector for this as an attribute. The Matrix
package chol
function no longer retuns the pivots.
Usage
mchol(A)
Arguments
A |
A sparse matrix suitable for passing to |
Details
The Matrix
version of chol
performs sparse Cholesky decomposition with sparsity maintaining pivoting, but no longer returns the pivot vector, rendering the returned factor useless for many purposes. This wrapper function simply fixes this. It also ensures that there are no numerical zeroes below the leading diagonal (not the default behaviour of expand1
, which can put some numerical zeroes there, in place of structural ones, at least at version 1.6-5). Calls chol(A,pivot=TRUE)
if A
inherits from class matrix
.
Value
If A
is positive definite then the upper triangular Cholesky factor matrix, with "pivot"
attribute and "rank"
attribute which is the diminsion of A
. Otherwise -1
with "rank"
attribute -1
.
Author(s)
Simon N. Wood simon.wood@r-project.org
Examples
library(mgcv)
## A sparse +ve def matrix
u <- sample(1:100,10)*.001
x <- i <- j <- 1:10
ii <- sample(1:10,10,replace=TRUE);
jj <- sample(1:10,10,replace=TRUE)
x <- c(x,u,u);
i <- c(i,ii,jj)
j <- c(j,jj,ii)
A <- Matrix::sparseMatrix(i=i,j=j,x=x)
R <- mchol(A)
piv <- attr(R,"pivot")
range(crossprod(R)-A[piv,piv])
i <- sample(1:5,10,replace=TRUE)
j <- sample(1:10,10,replace=TRUE)
u <- sample(1:100,10)*.001
A <- crossprod(Matrix::sparseMatrix(i=i,j=j,x=u))
mchol(A)