
R version 3.0.2 (2013-09-25) -- "Frisbee Sailing"
Copyright (C) 2013 The R Foundation for Statistical Computing
Platform: x86_64-unknown-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> #
> # Test out the Cholesky, special case of a null block corner 
> # In this case there is no advantage to a bdsmatrix as it consists of only
> #  the ordinary matrix part.  This case arises in coxme with an (x1+x2 | 1)
> #  term, however, so it is nice to have it work instead of coding lots of
> #  if/else logic in that code base.
> #
> library(bdsmatrix)

Attaching package: 'bdsmatrix'

The following object is masked from 'package:base':

    backsolve

> aeq <- function(x,y) all.equal(as.vector(x), as.vector(y))
> 
> tmat <- bdsmatrix(c(3,2,2,4), 
+ 	      c(22,1,2,21,3,20,19,4,18,17,5,16,15,6,7, 8,14,9,10,13,11,12),
+ 	      matrix(c(1,0,1,1,0,0,1,1,0,1,0,10,0,
+                        0,1,1,0,1,1,0,1,1,0,1,0,10), ncol=2))
> dimnames(tmat) <- list(NULL, letters[1:13])
> smat <- as.matrix(tmat)
> tmat <- bdsmatrix(integer(0), numeric(0), rmat=smat)  
> yy <- c(30,35,42,56,34,45,32,37,78,56,40,52,39)
> 
> aeq(diag(tmat), diag(smat))
[1] TRUE
> zz <- seq(1,13,2)
> aeq(as.matrix(tmat[zz,zz]), smat[zz,zz])
[1] TRUE
> 
> ch0 <- chol(smat)
> ch1 <- gchol(smat)
> ch2 <- gchol(tmat)
> # The gchol routines use the composition LDL', where L is lower triangular
> #  with a diagonal of 1's, and D is diagonal.  chol() uses U'U where U is
> #  upper trangular.  
> # The as.matrix function returns L and the diag function returns D.
> #  Convert and compare
> aeq(diag(ch1), diag(ch2))
[1] TRUE
> temp <- as.matrix(ch2)
> aeq(temp, as.matrix(ch1))
[1] TRUE
> temp3 <- temp %*% diag(sqrt(diag(ch2))) 
> aeq(temp3, t(ch0))
[1] TRUE
> 
> zz0 <- solve(smat, yy)
> zz1 <- solve(ch1, yy)
> zz2 <- solve(tmat, yy)
> aeq(zz1, zz2)
[1] TRUE
> aeq(zz0, zz1)
[1] TRUE
> 
> inv1 <- solve(smat)
> inv2 <- as.matrix(solve(tmat)) # the result is a full, non-sparse matrix
> aeq(inv1, inv2)
[1] TRUE
> 
> inv3 <- solve(gchol(tmat))
> aeq(inv1, as.matrix(inv3))
[1] TRUE
> 
> gmat <- gchol(tmat)
> g2 <- as.matrix(gmat) %*% diag(sqrt(diag(gmat)))
> aeq(1:13 %*% g2, 1:13 %*% gmat)  #vectors first
[1] TRUE
> aeq(g2 %*% 1:13, gmat %*% 1:13)
[1] TRUE
> temp <- matrix(runif(39), nrow=3)
> aeq(temp %*% g2, temp %*% gmat)
[1] TRUE
> aeq(g2 %*% t(temp), gmat %*% t(temp))
[1] TRUE
> 
> proc.time()
   user  system elapsed 
  0.849   0.044   0.917 
