# [R] Block Triangular Matirx

Martin Maechler maechler at stat.math.ethz.ch
Tue Jan 26 11:12:18 CET 2016

```>>>>> David Winsemius <dwinsemius at comcast.net>
>>>>>     on Mon, 25 Jan 2016 18:13:59 -0800 writes:

>> On Jan 24, 2016, at 10:45 PM, Olivier Crouzet
>> <olivier.crouzet at univ-nantes.fr> wrote:
>>
>>
>> https://stat.ethz.ch/R-manual/R-devel/library/base/html/lower.tri.html

> I agree that might be useful. Also very useful if the OP
> wants further advice would be for the OP to post a
> follow-up with R code that creates a minimal example and
> show what a correct answer would look like.

> If each block were homogeneous, the answer might be
> simpler than if the values in the blocks could be
> arbitrary. It would be helpful to know what sort of
> specification for the dimensions of blocks might look
> like. Simpler would be square blocks.

Also, if the matrix is relatively large and there are relatively
many small blocks,  it may be quite advantageous to use the
Matrix package because of more efficient storage as
'sparseMatrix', and 'triangularMatrix'  typically also providing
more efficient *computation* with such matrices :

require("Matrix")

##' random (diagonal-)block-triangular matrices:
rblockTri <- function(nb, max.ni, lambda = 3) {
.bdiag(replicate(nb, {
n <- sample.int(max.ni, 1)
tril(Matrix(rpois(n*n, lambda=lambda), n,n)) }))
}

set.seed(77)
TT <- rblockTri(30, 40)
image(TT) # to visualize it
tt <- as.matrix(TT)
object.size(TT) #  141832 bytes
object.size(tt) # 3145232 bytes -- factor 22.2 larger:
c(object.size(tt) / object.size(TT))

system.time(crossprod(TT))
##  user  system elapsed
## 0.001   0.000   0.001
system.time(crossprod(tt)) # a factor of ~100 slower :
##  user  system elapsed
## 0.123   0.000   0.122

```