The Art of R Programming

(WallPaper) #1
The componentmatof this class will store the matrix. As mentioned, to
save on storage space, only the diagonal and above-diagonal elements will
be stored, in column-major order. Storage for the matrix (9.1), for instance,
consists of the vector (1,5,6,12,9,2), and the componentmathas that value.
We will include a componentixin this class, to show where inmatthe
various columns begin. For the preceding case,ixisc(1,2,4), meaning that
column 1 begins atmat[1], column 2 begins atmat[2], and column 3 begins at
mat[4]. This allows for handy access to individual elements or columns of the
matrix.
The following is the code for our class.

1 # class "ut", compact storage of upper-triangular matrices
2
3 # utility function, returns 1+...+i
4 sum1toi <- function(i) return(i*(i+1)/2)
5
6 # create an object of class "ut" from the full matrix inmat (0s included)
7 ut <- function(inmat) {
8 n <- nrow(inmat)
9 rtrn <- list() # start to build the object
10 class(rtrn) <- "ut"
11 rtrn$mat <- vector(length=sum1toi(n))
12 rtrn$ix <- sum1toi(0:(n-1)) + 1
13 for (i in 1:n) {
14 # store column i
15 ixi <- rtrn$ix[i]
16 rtrn$mat[ixi:(ixi+i-1)] <- inmat[1:i,i]
17 }
18 return(rtrn)
19 }
20
21 # uncompress utmat to a full matrix
22 expandut <- function(utmat) {
23 n <- length(utmat$ix) # numbers of rows and cols of matrix
24 fullmat <- matrix(nrow=n,ncol=n)
25 for (j in 1:n) {
26 # fill jth column
27 start <- utmat$ix[j]
28 fin <- start+j-1
29 abovediagj <- utmat$mat[start:fin] # above-diag part of col j
30 fullmat[,j] <- c(abovediagj,rep(0,n-j))
31 }
32 return(fullmat)
33 }
34
35 # print matrix
36 print.ut <- function(utmat)
37 print(expandut(utmat))


Object-Oriented Programming 215
Free download pdf