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 873 - (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 :     ## dimnames index (TODO)
31 :    
32 :     ## TODO: more --- particularly once we have "m > 10" working!
33 :    
34 :    
35 : maechler 687 ### Sparse Matrices
36 :    
37 :     m <- 1:800
38 :     set.seed(101) ; m[sample(800, 600)] <- 0
39 :     m <- Matrix(m, nrow = 40)
40 :     mm <- as(m, "matrix")
41 :     dimnames(mm) <- NULL ## << workaround: as(<sparse>, "matrix") has NULL dimnames
42 :     str(mC <- as(m, "dgCMatrix"))
43 :     str(mT <- as(m, "dgTMatrix"))
44 :     stopifnot(identical(mT, as(mC, "dgTMatrix")),
45 :     identical(mC, as(mT, "dgCMatrix")))
46 :    
47 :     mC[,1]
48 :     mC[1:2,]
49 :     mC[7, drop = FALSE]
50 :    
51 :     mT[,c(2,4)]
52 :     mT[1,]
53 :     mT[4, drop = FALSE]
54 :     stopifnot(identical3(mm[,1], mC[,1], mT[,1]),
55 :     identical3(mm[3,], mC[3,], mT[3,]),
56 :     identical3(mT[2,3], mC[2,3], 0),
57 :     identical(mT[], mT),
58 :     ## TODO: identical4() with m[c(3,7), 2:4]
59 :     identical3(as(mC[c(3,7), 2:4],"matrix"), mm[c(3,7), 2:4],
60 :     as(mT[c(3,7), 2:4],"matrix")))
61 :    
62 :    

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