SCM

SCM Repository

[matrix] Annotation of /pkg/tests/indexing.R
ViewVC logotype

Annotation of /pkg/tests/indexing.R

Parent Directory Parent Directory | Revision Log Revision Log


Revision 886 - (view) (download)

1 : maechler 687 #### For both 'Extract' ("[") and 'Replace' ("[<-") Method testing
2 :    
3 :     library(Matrix)
4 :    
5 :     identical3 <- function(x,y,z) identical(x,y) && identical (y,z)
6 :     identical4 <- function(a,b,c,d) identical(a,b) && identical3(b,c,d)
7 :    
8 : maechler 873 ### Dense Matrices
9 :    
10 : maechler 687 m <- Matrix(1:28, nrow = 7)
11 : maechler 873 validObject(m) ; m@x <- as.double(m@x) ; validObject(m)
12 :     stopifnot(identical(m, m[]),
13 :     identical(m[2, 3], 16), # simple number
14 :     identical(m[2, 3:4], c(16,23))) # simple numeric of length 2
15 : maechler 687
16 : maechler 873 m[2, 3:4, drop=FALSE] # sub matrix of class 'dgeMatrix'
17 :     m[-(4:7), 3:4] # dito; the upper right corner of 'm'
18 : maechler 687
19 : maechler 873 ## rows or columns only:
20 :     m[1,] # first row, as simple numeric vector
21 :     m[,2] # 2nd column
22 :     m[,1:2] # sub matrix of first two columns
23 :     m[-(1:6),, drop=FALSE] # not the first 6 rows, i.e. only the 7th
24 : maechler 687
25 : maechler 873 ## logical indexing
26 :     stopifnot(identical(m[2,3], m[(1:nrow(m)) == 2, (1:ncol(m)) == 3]),
27 :     identical(m[2,], m[(1:nrow(m)) == 2, ]),
28 :     identical(m[,3:4], m[, (1:4) >= 3]))
29 :    
30 : maechler 886 ## dimnames indexing:
31 :     mn <- m
32 :     dimnames(mn) <- list(paste("r",letters[1:nrow(mn)],sep=""),
33 :     LETTERS[1:ncol(mn)])
34 :     mn["rd", "D"]
35 :     stopifnot(identical(mn["rc", "D"], mn[3,4]),
36 :     identical(mn[, "A"], mn[,1]),
37 :     identical(mn[c("re", "rb"), "B"], mn[c(5,2), 2])
38 :     )
39 : maechler 873
40 : maechler 886 mo <- m
41 :     m[2,3] <- 100
42 :     m[1:2, 4] <- 200
43 :     m[, 1] <- -1
44 :     m[1:3,]
45 :    
46 : maechler 873 ## TODO: more --- particularly once we have "m > 10" working!
47 :    
48 :    
49 : maechler 687 ### Sparse Matrices
50 :    
51 :     m <- 1:800
52 :     set.seed(101) ; m[sample(800, 600)] <- 0
53 :     m <- Matrix(m, nrow = 40)
54 :     mm <- as(m, "matrix")
55 :     dimnames(mm) <- NULL ## << workaround: as(<sparse>, "matrix") has NULL dimnames
56 :     str(mC <- as(m, "dgCMatrix"))
57 :     str(mT <- as(m, "dgTMatrix"))
58 :     stopifnot(identical(mT, as(mC, "dgTMatrix")),
59 :     identical(mC, as(mT, "dgCMatrix")))
60 :    
61 :     mC[,1]
62 :     mC[1:2,]
63 :     mC[7, drop = FALSE]
64 :    
65 :     mT[,c(2,4)]
66 :     mT[1,]
67 :     mT[4, drop = FALSE]
68 :     stopifnot(identical3(mm[,1], mC[,1], mT[,1]),
69 :     identical3(mm[3,], mC[3,], mT[3,]),
70 :     identical3(mT[2,3], mC[2,3], 0),
71 :     identical(mT[], mT),
72 :     ## TODO: identical4() with m[c(3,7), 2:4]
73 :     identical3(as(mC[c(3,7), 2:4],"matrix"), mm[c(3,7), 2:4],
74 :     as(mT[c(3,7), 2:4],"matrix")))
75 :    
76 : maechler 886 ## At least these now give a nicely understandable error:
77 :     try(mT[1, 4] <- -99)
78 :     try(mT[2:3, ] <- 0)

root@r-forge.r-project.org
ViewVC Help
Powered by ViewVC 1.0.0  
Thanks to:
Vienna University of Economics and Business Powered By FusionForge