# SCM Repository

[matrix] View of /pkg/tests/indexing.Rout.save
 [matrix] / pkg / tests / indexing.Rout.save

# View of /pkg/tests/indexing.Rout.save

Mon Nov 6 20:54:26 2006 UTC (12 years, 11 months ago) by maechler
File size: 17623 byte(s)
`host of changes; mostly indexing(sub-set & -assign) for symmetric and [ cbind(i,j) ]`
```R version 2.4.0 Patched (2006-11-03 r39791)
Copyright (C) 2006 The R Foundation for Statistical Computing
ISBN 3-900051-07-0

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.
'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.

> #### For both 'Extract' ("[") and 'Replace' ("[<-") Method testing
>
> library(Matrix)
>
> source(system.file("test-tools.R", package = "Matrix"))# identical3() etc
>
> ### Dense Matrices
>
> m <- Matrix(1:28 +0, nrow = 7)
> validObject(m)
[1] TRUE
> stopifnot(identical(m, m[]),
+           identical(m[2, 3],  16), # simple number
+           identical(m[2, 3:4], c(16,23))) # simple numeric of length 2
>
> m[2, 3:4, drop=FALSE] # sub matrix of class 'dgeMatrix'
1 x 2 Matrix of class "dgeMatrix"
[,1] [,2]
[1,]   16   23
> m[-(4:7), 3:4]        # dito; the upper right corner of 'm'
3 x 2 Matrix of class "dgeMatrix"
[,1] [,2]
[1,]   15   22
[2,]   16   23
[3,]   17   24
>
> ## rows or columns only:
> m[1,]     # first row, as simple numeric vector
[1]  1  8 15 22
> m[,2]     # 2nd column
[1]  8  9 10 11 12 13 14
> m[,1:2]   # sub matrix of first two columns
7 x 2 Matrix of class "dgeMatrix"
[,1] [,2]
[1,]    1    8
[2,]    2    9
[3,]    3   10
[4,]    4   11
[5,]    5   12
[6,]    6   13
[7,]    7   14
> m[-(1:6),, drop=FALSE] # not the first 6 rows, i.e. only the 7th
1 x 4 Matrix of class "dgeMatrix"
[,1] [,2] [,3] [,4]
[1,]    7   14   21   28
> m[integer(0),] #-> 0 x 4 Matrix
0 x 4 Matrix of class "dgeMatrix"
[,1] [,2] [,3] [,4]
> m[2:4, numeric(0)] #-> 3 x 0 Matrix
3 x 0 Matrix of class "dgeMatrix"

[1,]
[2,]
[3,]
>
> ## logical indexing
> stopifnot(identical(m[2,3], m[(1:nrow(m)) == 2, (1:ncol(m)) == 3]),
+           identical(m[2,], m[(1:nrow(m)) == 2, ]),
+           identical(m[,3:4], m[, (1:4) >= 3]))
>
> ## dimnames indexing:
> mn <- m
> dimnames(mn) <- list(paste("r",letters[1:nrow(mn)],sep=""),
+                      LETTERS[1:ncol(mn)])
> mn["rd", "D"]
[1] 25
> stopifnot(identical(mn["rc", "D"], mn[3,4]), mn[3,4] == 24,
+           identical(mn[, "A"], mn[,1]), mn[,1] == 1:7,
+           identical(mn[c("re", "rb"), "B"], mn[c(5,2), 2])
+           )
>
> mo <- m
> m[2,3] <- 100
> m[1:2, 4] <- 200
> m[, 1] <- -1
> m[1:3,]
3 x 4 Matrix of class "dgeMatrix"
[,1] [,2] [,3] [,4]
[1,]   -1    8   15  200
[2,]   -1    9  100  200
[3,]   -1   10   17   24
>
> m. <- as.matrix(m)
>
> ## m[ cbind(i,j) ] indexing:
> ij <- cbind(1:6, 2:3)
> stopifnot(identical(m[ij], m.[ij]))
>
> ## testing operations on logical Matrices rather more than indexing:
> g10 <- m [ m > 10 ]
> stopifnot(18 == length(g10))
> stopifnot(10 == length(m[ m <= 10 ]))
> sel <- (20 <  m) & (m <  150)
> sel.<- (20 <  m.)& (m.<  150)
> nsel <-(20 >= m) | (m >= 150)
> (ssel <- as(sel, "sparseMatrix"))
7 x 4 sparse Matrix of class "lgCMatrix"

[1,] . . . .
[2,] . . | .
[3,] . . . |
[4,] . . . |
[5,] . . . |
[6,] . . . |
[7,] . . | |
> stopifnot(is(sel, "lMatrix"), is(ssel, "lsparseMatrix"),
+ 	  identical3(as.mat(sel.), as.mat(sel), as.mat(ssel)),
+ 	  identical3(!sel, !ssel, nsel), # !<sparse> is typically dense
+ 	  identical3(m[ sel],  m[ ssel], as.matrix(m)[as.matrix( ssel)]),
+ 	  identical3(m[!sel],  m[!ssel], as.matrix(m)[as.matrix(!ssel)])
+ 	  )
>
> ## more sparse Matrices --------------------------------------
>
> m <- 1:800
> set.seed(101) ; m[sample(800, 600)] <- 0
> m <- Matrix(m, nrow = 40)
> mm <- as(m, "matrix")
> dimnames(mm) <- NULL ## << workaround: as(<sparse>, "matrix") has NULL dimnames
> str(mC <- as(m, "dgCMatrix"))
Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
..@ i       : int [1:200] 2 6 11 21 24 29 37 38 1 4 ...
..@ p       : int [1:21] 0 8 22 28 37 41 50 63 71 81 ...
..@ Dim     : int [1:2] 40 20
..@ Dimnames:List of 2
.. ..\$ : NULL
.. ..\$ : NULL
..@ x       : num [1:200] 3 7 12 22 25 30 38 39 42 45 ...
..@ factors : list()
> str(mT <- as(m, "dgTMatrix"))
Formal class 'dgTMatrix' [package "Matrix"] with 6 slots
..@ i       : int [1:200] 2 6 11 21 24 29 37 38 1 4 ...
..@ j       : int [1:200] 0 0 0 0 0 0 0 0 1 1 ...
..@ Dim     : int [1:2] 40 20
..@ Dimnames:List of 2
.. ..\$ : NULL
.. ..\$ : NULL
..@ x       : num [1:200] 3 7 12 22 25 30 38 39 42 45 ...
..@ factors : list()
> stopifnot(identical(mT, as(mC, "dgTMatrix")),
+ 	  identical(mC, as(mT, "dgCMatrix")))
>
> mC[,1]
[1]  0  0  3  0  0  0  7  0  0  0  0 12  0  0  0  0  0  0  0  0  0 22  0  0 25
[26]  0  0  0  0 30  0  0  0  0  0  0  0 38 39  0
> mC[1:2,]
2 x 20 sparse Matrix of class "dgCMatrix"

[1,] . .  . 121 . .   241 . . . . 441 .   .   561 . 641 . .   .
[2,] . 42 . .   . 202 .   . . . . .   482 522 .   . .   . 722 .
> mC[7,  drop = FALSE]
1 x 20 sparse Matrix of class "dgCMatrix"

[1,] 7 . . . . . . 287 . . 407 . 487 527 . . . . 727 .
> assert.EQ.mat(mC[1:2,], mm[1:2,])
> stopifnot(all.equal(mC[,3], mm[,3]),
+ 	  identical(mC[ij], mm[ij]))
> assert.EQ.mat(mC[7, , drop=FALSE], mm[7, , drop=FALSE])
>
> stopifnot(dim(mC[numeric(0), ]) == c(0,20), # used to give warnings
+           dim(mC[, integer(0)]) == c(40,0),
+           identical(mC[, integer(0)], mC[, FALSE]),
+           identical(mC[7,  drop = FALSE],
+                     mC[7,, drop = FALSE]))
> validObject(print(mT[,c(2,4)]))
40 x 2 sparse Matrix of class "dgTMatrix"

[1,] .  121
[2,] 42 .
[3,] .  .
[4,] .  .
[5,] 45 .
[6,] .  .
[7,] .  .
[8,] .  128
[9,] .  129
[10,] 50 .
[11,] .  .
[12,] 52 132
[13,] .  133
[14,] .  .
[15,] 55 .
[16,] .  .
[17,] .  .
[18,] .  138
[19,] .  .
[20,] .  .
[21,] .  141
[22,] .  142
[23,] 63 .
[24,] .  .
[25,] 65 .
[26,] .  .
[27,] 67 .
[28,] 68 .
[29,] .  .
[30,] .  .
[31,] 71 .
[32,] 72 .
[33,] .  .
[34,] 74 .
[35,] .  .
[36,] 76 .
[37,] .  .
[38,] .  .
[39,] .  159
[40,] 80 .
[1] TRUE
> stopifnot(all.equal(mT[2,], mm[2,]),
+           ## row or column indexing in combination with t() :
+           identical(mT[2,], t(mT)[,2]),
+           identical(mT[-2,], t(t(mT)[,-2])),
+           identical(mT[c(2,5),], t(t(mT)[,c(2,5)]))
+           )
> assert.EQ.mat(mT[4,, drop = FALSE], mm[4,, drop = FALSE])
> stopifnot(identical3(mm[,1], mC[,1], mT[,1]),
+ 	  identical3(mm[3,], mC[3,], mT[3,]),
+ 	  identical3(mT[2,3], mC[2,3], 0),
+ 	  identical(mT[], mT),
+           identical4(       mm[c(3,7), 2:4],  as.mat( m[c(3,7), 2:4]),
+                      as.mat(mT[c(3,7), 2:4]), as.mat(mC[c(3,7), 2:4]))
+           )
>
> x.x <- crossprod(mC)
> stopifnot(class(x.x) == "dsCMatrix",
+           class(x.x. <- round(x.x / 10000)) == "dsCMatrix",
+           identical(x.x[cbind(2:6, 2:6)],
+                     diag(x.x [2:6, 2:6])))
> head(x.x.) # Note the *non*-structural 0's printed as "0"
6 x 20 sparse Matrix of class "dgCMatrix"

[1,] 1 0 .  1 .   1  1  3 .   3  2  1  6  1 .   2  4  6  5  1
[2,] 0 6 2  1  3  5  7  5 12 14 14  9 11 16 12 13 17 19 19 10
[3,] . 2 6 .   4  2  5  3  8 12  5 16  9 11 23 .  .   6  7  7
[4,] 1 1 . 17 .   8 10 13  8  6 18 18 29 35 14  8 25 10 19 21
[5,] . 3 4 .  14  4 10 .  .  29  8  9 19 11 11 .  .  26 26 16
[6,] 1 5 2  8  4 42  5 19 14  9  8 10 42 56 50 27 29 32 64 16
> tail(x.x., -3) # all but the first three lines
17 x 20 sparse Matrix of class "dgCMatrix"

[4,]  1  1 .  17 .   8  10 13   8   6  18  18  29  35  14   8  25  10  19  21
[5,]  .  3  4 .  14  4  10 .  .    29   8   9  19  11  11 .   .    26  26  16
[6,]  1  5  2  8  4 42   5 19  14   9   8  10  42  56  50  27  29  32  64  16
[7,]  1  7  5 10 10  5  87 14   9  31  77  47  79  43  28  17  67 110  36 121
[8,]  3  5  3 13 .  19  14 70  10  24  37  13  59  62  34  19  58  21  64  44
[9,]  . 12  8  8 .  14   9 10 116  41  58  33  33  72  78  43  69  72  75  25
[10,] 3 14 12  6 29  9  31 24  41 167  69  56  99  44  70  24 105  82  85  32
[11,] 2 14  5 18  8  8  77 37  58  69 267  80  86 139  49 105 194 119 122 129
[12,] 1  9 16 18  9 10  47 13  33  56  80 194  70  77  81 .    90  32 .   106
[13,] 6 11  9 29 19 42  79 59  33  99  86  70 324 157  55 .    69 142 144 155
[14,] 1 16 11 35 11 56  43 62  72  44 139  77 157 375 123 102 145  39 196  81
[15,] . 12 23 14 11 50  28 34  78  70  49  81  55 123 368  71 112  41  41  86
[16,] 2 13 .   8 .  27  17 19  43  24 105 .   .   102  71 233 124  44 139 .
[17,] 4 17 .  25 .  29  67 58  69 105 194  90  69 145 112 124 523 141 245 100
[18,] 6 19  6 10 26 32 110 21  72  82 119  32 142  39  41  44 141 497 104 111
[19,] 5 19  7 19 26 64  36 64  75  85 122 .   144 196  41 139 245 104 542  55
[20,] 1 10  7 21 16 16 121 44  25  32 129 106 155  81  86 .   100 111  55 541
>
> lx.x <- as(x.x, "lsCMatrix") # FALSE only for "structural" 0
> (l10 <- lx.x[1:10, 1:10])# "lsC"
10 x 10 sparse Matrix of class "lsCMatrix"

[1,] | | . | . | | | . |
[2,] | | | | | | | | | |
[3,] . | | . | | | | | |
[4,] | | . | . | | | | |
[5,] . | | . | | | . . |
[6,] | | | | | | | | | |
[7,] | | | | | | | | | |
[8,] | | | | . | | | | |
[9,] . | | | . | | | | |
[10,] | | | | | | | | | |
> (l3 <-  lx.x[1:3, ])
3 x 20 sparse Matrix of class "lgCMatrix"

[1,] | | . | . | | | . | | | | | . | | | | |
[2,] | | | | | | | | | | | | | | | | | | | |
[3,] . | | . | | | | | | | | | | | . . | | |
> m.x <- as(x.x, "matrix")
> stopifnot(class(l10) == "lsCMatrix", # symmetric indexing -> symmetric !
+           identical(as.mat(lx.x), m.x != 0),
+           identical(as.logical(lx.x), as.logical(m.x)),
+           identical(as.mat(l10), m.x[1:10, 1:10] != 0),
+           identical(as.mat(l3 ), m.x[1:3, ] != 0)
+           )
>
>
> ## --- negative indices ----------
> mc <- mC[1:5, 1:7]
> mt <- mT[1:5, 1:7]
> ## sub matrix
> assert.EQ.mat(mC[1:2, 0:3], mm[1:2, 0:3]) # test 0-index
> stopifnot(identical(mc[-(3:5), 0:2], mC[1:2, 0:2]),
+           identical(mt[-(3:5), 0:2], mT[1:2, 0:2]),
+           identical(mC[2:3, 4],      mm[2:3, 4]))
> assert.EQ.mat(mC[1:2,], mm[1:2,])
> ## sub vector
> stopifnot(identical4(mc[-(1:4), ], mC[5, 1:7],
+                      mt[-(1:4), ], mT[5, 1:7]))
> stopifnot(identical4(mc[-(1:4), -(2:4)], mC[5, c(1,5:7)],
+                      mt[-(1:4), -(2:4)], mT[5, c(1,5:7)]))
>
> ## mixing of negative and positive must give error
> assertError(mT[-1:1,])
>
> ## Sub *Assignment* ---- now works (partially):
> mt0 <- mt
> mt[1, 4] <- -99
> mt[2:3, 1:6] <- 0
> mt
5 x 7 sparse Matrix of class "dgTMatrix"

[1,] . .  . -99 . . 241
[2,] . .  . .   . . .
[3,] . .  . .   . . 243
[4,] . .  . .   . . .
[5,] . 45 . .   . . .
> m2 <- mt+mt
> m2[1,4] <- -200
> m2[c(1,3), c(5:6,2)] <- 1:6
> stopifnot(m2[1,4] == -200,
+           as.vector(m2[c(1,3), c(5:6,2)]) == 1:6)
> mt[,3] <- 30
> mt[2:3,] <- 250
> mt[1:5 %% 2 == 1, 3] <- 0
> mt[3:1, 1:7 > 5] <- 0
> mt
5 x 7 sparse Matrix of class "dgTMatrix"

[1,] .   .   .   -99 .   . .
[2,] 250 250 250 250 250 . .
[3,] 250 250 .   250 250 . .
[4,] .   .    30 .   .   . .
[5,] .    45 .   .   .   . .
>
> tt <- as(mt,"matrix")
> ii <- c(0,2,5)
> jj <- c(2:3,5)
> tt[ii, jj] <- 1:6 # 0 is just "dropped"
> mt[ii, jj] <- 1:6
> assert.EQ.mat(mt, tt)
>
> mt[1:5, 2:6]
5 x 5 sparse Matrix of class "dgTMatrix"

[1,] .   .  -99 .   .
[2,]   1  3 250   5 .
[3,] 250 .  250 250 .
[4,] .   30 .   .   .
[5,]   2  4 .     6 .
> as((mt0 - mt)[1:5,], "dsparseMatrix")# [1,5] and lines 2:3
5 x 7 sparse Matrix of class "dgCMatrix"

[1,] .    .    .    220 .    .   241
[2,] -250   41  -3 -250   -5 202 .
[3,] -247 -250 .   -250 -250 .   243
[4,] .    .    -30 .    .    .   .
[5,] .      43  -4 .      -6 .   .
Warning message:
Ambiguous method selection for "-", target "dgTMatrix#dgTMatrix" (the first of the signatures shown will be used)
sparseMatrix#sparseMatrix
dMatrix#dMatrix
in: .findInheritedMethods(classes, fdef, mtable)
>
> mt[c(2,4), ] <- 0; stopifnot(as(mt[c(2,4), ],"matrix") == 0)
> mt[2:3, 4:7] <- 33
> validObject(mt)
[1] TRUE
> mt
5 x 7 sparse Matrix of class "dgTMatrix"

[1,] .   .   . -99 .  .  .
[2,] .   .   .  33 33 33 33
[3,] 250 250 .  33 33 33 33
[4,] .   .   . .   .  .  .
[5,] .     2 4 .    6 .  .
>
> mc[1,4] <- -99 ; stopifnot(mc[1,4] == -99)
> mc[1,4] <-  00 ; stopifnot(mc[1,4] ==  00)
> mc[1,4] <- -99 ; stopifnot(mc[1,4] == -99)
> mc[1:2,4:3] <- 4:1; stopifnot(as.matrix(mc[1:2,4:3]) == 4:1)
>
> mc[-1, 3] <- -2:1 # 0 should not be entered; 'value' recycled
> mt[-1, 3] <- -2:1
> stopifnot(mc@x != 0, mt@x != 0,
+           mc[-1,3] == -2:1, mt[-1,3] == -2:1) ##--> BUG -- fixed
>
> ev <- 1:5 %% 2 == 0
> mc[ev, 3] <- 0:1
> if(FALSE)## FIXME
+  stopifnot(mc[ev, 3] == 0:1) ##-> BUG  {very peculiar; the 2nd time it works ...}
> validObject(mc)
[1] TRUE
> mc # now shows a non-structural zeros
5 x 7 sparse Matrix of class "dgCMatrix"

[1,] . .   2 4 . .   241
[2,] . 42 -2 3 . 202 .
[3,] 3 .  -1 . . .   243
[4,] . .   1 . . .   .
[5,] . 45  1 . . .   .
> mc[ii, jj] <- 1:6
> mc[c(2,5), c(3,5)] <- 3.2
> validObject(mc)
[1] TRUE
> m. <- mc
> mc[4,] <- 0
> mc
5 x 7 sparse Matrix of class "dgCMatrix"

[1,] . .  2.0 4 .   .   241
[2,] . 1  3.2 3 3.2 202 .
[3,] 3 . -1.0 . .   .   243
[4,] . . .    . .   .   .
[5,] . 2  3.2 . 3.2 .   .
>
> H <- Hilbert(9)
> Hc <- as(round(H, 3), "dsCMatrix")# a sparse matrix with no 0 ...
> (trH <- tril(Hc[1:5, 1:5]))
5 x 5 sparse Matrix of class "dtCMatrix"

[1,] 1.000 .     .     .     .
[2,] 0.500 0.333 .     .     .
[3,] 0.333 0.250 0.200 .     .
[4,] 0.250 0.200 0.167 0.143 .
[5,] 0.200 0.167 0.143 0.125 0.111
> stopifnot(is(trH, "triangularMatrix"), trH@uplo == "L")
>
> i <- c(1:2, 4, 6:7); j <- c(2:4,6)
> H[i,j] <- 0
> (H. <- round(as(H, "sparseMatrix"), 3)[ , 2:7])
9 x 6 sparse Matrix of class "dgCMatrix"

[1,] .     .     .     0.200 .     0.143
[2,] .     .     .     0.167 .     0.125
[3,] 0.250 0.200 0.167 0.143 0.125 0.111
[4,] .     .     .     0.125 .     0.100
[5,] 0.167 0.143 0.125 0.111 0.100 0.091
[6,] .     .     .     0.100 .     0.083
[7,] .     .     .     0.091 .     0.077
[8,] 0.111 0.100 0.091 0.083 0.077 0.071
[9,] 0.100 0.091 0.083 0.077 0.071 0.067
> Hc. <- Hc
> Hc.[i,j] <- 0 ## now "works", but setting "non-structural" 0s
> stopifnot(as.matrix(Hc.[i,j]) == 0)
> Hc.[, 1:6]
9 x 6 sparse Matrix of class "dgCMatrix"

[1,] 1.000 0.000 0.000 0.000 0.200 0.000
[2,] 0.500 0.000 0.000 0.000 0.167 0.000
[3,] 0.333 0.250 0.200 0.167 0.143 0.125
[4,] 0.250 0.000 0.000 0.000 0.125 0.000
[5,] 0.200 0.167 0.143 0.125 0.111 0.100
[6,] 0.167 0.000 0.000 0.000 0.100 0.000
[7,] 0.143 0.000 0.000 0.000 0.091 0.000
[8,] 0.125 0.111 0.100 0.091 0.083 0.077
[9,] 0.111 0.100 0.091 0.083 0.077 0.071
>
> ## an example that failed long
> sy3 <- new("dsyMatrix", Dim = as.integer(c(2, 2)), x = c(14, -1, 2, -7))
> validObject(dm <- kronecker(Diagonal(2), sy3))
Warning message:
Ambiguous method selection for "kronecker", target "ddiMatrix#dsyMatrix" (the first of the signatures shown will be used)
Matrix#ANY
ANY#Matrix
in: .findInheritedMethods(classes, fdef, mtable)
[1] TRUE
> (s2 <- as(dm, "sparseMatrix"))
4 x 4 sparse Matrix of class "dsCMatrix"

[1,] 14  2 .  .
[2,]  2 -7 .  .
[3,] .  .  14  2
[4,] .  .   2 -7
> validObject(st <- as(s2, "TsparseMatrix"))
[1] TRUE
> validObject(s.32  <- st[1:3,1:2]) ## 3 x 2 - and *not* dsTMatrix
[1] TRUE
> validObject(s2.32 <- s2[1:3,1:2])
[1] TRUE
> I <- c(1,4:3)
> stopifnot(is(s2.32, "generalMatrix"),
+           is(s.32,  "generalMatrix"),
+           identical(as.mat(s.32), as.mat(s2.32)),
+           identical3(dm[1:3,-1], asD(s2[1:3,-1]), asD(st[1:3,-1])),
+           identical4(2, dm[4,3], s2[4,3], st[4,3]),
+           identical3(diag(dm), diag(s2), diag(st)),
+           is((cI <- s2[I,I]), "dsCMatrix"),
+           is((tI <- st[I,I]), "dsTMatrix"),
+           identical4(as.mat(dm)[I,I], as.mat(dm[I,I]), as.mat(tI), as.mat(cI))
+           )
>
> ## now sub-assign  and check for consistency
> ## symmetric subassign should keep symmetry
> st[I,I] <- 0; validObject(st); stopifnot(is(st,"symmetricMatrix"))
[1] TRUE
> s2[I,I] <- 0; validObject(s2); stopifnot(is(s2,"symmetricMatrix"))
[1] TRUE
>
> m <- as.mat(st)
>  m[2:1,2:1] <- 4:1
> st[2:1,2:1] <- 4:1
> s2[2:1,2:1] <- 4:1
> stopifnot(identical(m, as.mat(st)),
+ 	  1:4 == as.vector(s2[1:2,1:2]),
+ 	  identical(m, as.mat(s2)))
>
> ## m[cbind(i,j)] <- value:
> m.[ cbind(3:5, 1:3) ] <- 1:3
> stopifnot(m.[3,1] == 1, m.[4,2] == 2)
> x.x[ cbind(2:6, 2:6)] <- 12:16
> validObject(x.x)
[1] TRUE
> stopifnot(class(x.x) == "dsCMatrix",
+ 	  12:16 == as.mat(x.x)[cbind(2:6, 2:6)])
>
> cat('Time elapsed: ', proc.time(),'\n') # for ``statistical reasons''
Time elapsed:  3.316 0.124 3.84 0 0
>
```