https://github.com/cran/Matrix
Tip revision: 7de360e60bb82728d10895f2cc29646ec7006df4 authored by Douglas Bates on 15 September 2006, 00:00:00 UTC
version 0.995-19
version 0.995-19
Tip revision: 7de360e
indexing.Rout.save
R : Copyright 2006, The R Foundation for Statistical Computing
Version 2.3.1 Patched (2006-08-13 r38879)
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.
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.
> #### For both 'Extract' ("[") and 'Replace' ("[<-") Method testing
>
> library(Matrix)
Loading required package: lattice
>
> source(system.file("test-tools.R", package = "Matrix"))# identical3() etc
>
> ### Dense Matrices
>
> m <- Matrix(1:28, nrow = 7)
Warning message:
integer matrices not yet implemented in 'Matrix'; using 'double' ones' in: Matrix(1:28, nrow = 7)
> validObject(m) ; m@x <- as.double(m@x) ; validObject(m)
[1] TRUE
[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
>
> g10 <- m [ m > 10 ]
> stopifnot(18 == length(g10))
> ## needs R >= 2.3.0 [Buglet in R(<= 2.2.1)'s possibleExtends()]:
> stopifnot(10 == length(m[ m <= 10 ]))
>
>
> ### 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]))
> 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),
+ ## TODO: identical4() with m[c(3,7), 2:4] - fail because of 'dimnames'
+ ## TODO: identical3() with as(mC[c(3,7), 2:4],"matrix"),
+ ## fails because of 'dimnames'
+ identical(mm[c(3,7), 2:4], as(mT[c(3,7), 2:4],"matrix"))
+ )
>
> x.x <- crossprod(mC)
> stopifnot(class(x.x) == "dsCMatrix",
+ class(x.x. <- round(x.x / 10000)) == "dsCMatrix")
> 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
> ## FIXME (once we require 2.4.x or higher):
> ## tail(x.x., -2) # the last two lines
>
> lx.x <- as(x.x, "lsCMatrix") # FALSE only for "structural" 0
> if(FALSE) { ## FIXME: needs coercion "lsCMatrix" to "lgTMatrix"
+ lx.x[1:10, 1:10]
+ lx.x[1:3, ]
+ }
>
> ## --- 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 . .
>
> 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)
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,] . . 1.0 . . . .
[5,] . 2 3.2 . 3.2 . .
> if(FALSE)## FIXME:
+ mc[4,] <- 0 # -> error -- another Bug
>
> 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
>
> cat('Time elapsed: ', proc.time(),'\n') # for ``statistical reasons''
Time elapsed: 22.532 0.256 24.283 0 0
>