SCM

SCM Repository

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

Diff of /pkg/tests/indexing.R

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1269, Thu May 18 06:43:29 2006 UTC revision 1665, Fri Nov 3 23:18:07 2006 UTC
# Line 20  Line 20 
20  m[,2]     # 2nd column  m[,2]     # 2nd column
21  m[,1:2]   # sub matrix of first two columns  m[,1:2]   # sub matrix of first two columns
22  m[-(1:6),, drop=FALSE] # not the first 6 rows, i.e. only the 7th  m[-(1:6),, drop=FALSE] # not the first 6 rows, i.e. only the 7th
23    m[integer(0),] #-> 0 x 4 Matrix
24    m[2:4, numeric(0)] #-> 3 x 0 Matrix
25    
26  ## logical indexing  ## logical indexing
27  stopifnot(identical(m[2,3], m[(1:nrow(m)) == 2, (1:ncol(m)) == 3]),  stopifnot(identical(m[2,3], m[(1:nrow(m)) == 2, (1:ncol(m)) == 3]),
# Line 42  Line 44 
44  m[, 1] <- -1  m[, 1] <- -1
45  m[1:3,]  m[1:3,]
46    
47    ## testing operations on logical Matrices rather more than indexing:
48    m. <- as.matrix(m)
49  g10 <- m [ m > 10 ]  g10 <- m [ m > 10 ]
50  stopifnot(18 == length(g10))  stopifnot(18 == length(g10))
 if(paste(R.version$major, R.version$minor, sep=".") >= "2.3")  
     ## Buglet in R(<= 2.2.1)'s possibleExtends()  
51      stopifnot(10 == length(m[ m <= 10 ]))      stopifnot(10 == length(m[ m <= 10 ]))
52    sel <- (20 <  m) & (m <  150)
53    sel.<- (20 <  m.)& (m.<  150)
54    nsel <-(20 >= m) | (m >= 150)
55    (ssel <- as(sel, "sparseMatrix"))
56    stopifnot(is(sel, "lMatrix"), is(ssel, "lsparseMatrix"),
57              identical3(as.mat(sel.), as.mat(sel), as.mat(ssel)),
58              identical3(!sel, !ssel, nsel), # !<sparse> is typically dense
59              identical3(m[ sel],  m[ ssel], as.matrix(m)[as.matrix( ssel)]),
60              identical3(m[!sel],  m[!ssel], as.matrix(m)[as.matrix(!ssel)])
61              )
62    
63    ## more sparse Matrices --------------------------------------
 ### Sparse Matrices --------------------------------------  
64    
65  m <- 1:800  m <- 1:800
66  set.seed(101) ; m[sample(800, 600)] <- 0  set.seed(101) ; m[sample(800, 600)] <- 0
# Line 68  Line 79 
79  stopifnot(all.equal(mC[,3],   mm[,3]))  stopifnot(all.equal(mC[,3],   mm[,3]))
80  assert.EQ.mat(mC[7, , drop=FALSE], mm[7, , drop=FALSE])  assert.EQ.mat(mC[7, , drop=FALSE], mm[7, , drop=FALSE])
81    
82  stopifnot(identical(mC[7,  drop = FALSE],  stopifnot(dim(mC[numeric(0), ]) == c(0,20), # used to give warnings
83              dim(mC[, integer(0)]) == c(40,0),
84              identical(mC[, integer(0)], mC[, FALSE]),
85              identical(mC[7,  drop = FALSE],
86                      mC[7,, drop = FALSE]))                      mC[7,, drop = FALSE]))
87  validObject(print(mT[,c(2,4)]))  validObject(print(mT[,c(2,4)]))
88  stopifnot(all.equal(mT[2,], mm[2,]),  stopifnot(all.equal(mT[2,], mm[2,]),
# Line 82  Line 96 
96            identical3(mm[3,], mC[3,], mT[3,]),            identical3(mm[3,], mC[3,], mT[3,]),
97            identical3(mT[2,3], mC[2,3], 0),            identical3(mT[2,3], mC[2,3], 0),
98            identical(mT[], mT),            identical(mT[], mT),
99            ## TODO: identical4() with  m[c(3,7), 2:4] - fail because of 'dimnames'            identical4(       mm[c(3,7), 2:4],  as.mat( m[c(3,7), 2:4]),
100            identical3(as(mC[c(3,7), 2:4],"matrix"), mm[c(3,7), 2:4],                       as.mat(mT[c(3,7), 2:4]), as.mat(mC[c(3,7), 2:4]))
101                       as(mT[c(3,7), 2:4],"matrix")))            )
102    
103    x.x <- crossprod(mC)
104    stopifnot(class(x.x) == "dsCMatrix",
105              class(x.x. <- round(x.x / 10000)) == "dsCMatrix")
106    head(x.x.) # Note the *non*-structural 0's printed as "0"
107    tail(x.x., -3) # all but the first three lines
108    
109    lx.x <- as(x.x, "lsCMatrix") # FALSE only for "structural" 0
110    (l10 <- lx.x[1:10, 1:10])# "lsC"
111    (l3 <-  lx.x[1:3, ])
112    m.x <- as(x.x, "matrix")
113    stopifnot(class(l10) == "lsCMatrix", # symmetric indexing -> symmetric !
114              identical(as.mat(lx.x), m.x != 0),
115              identical(as.logical(lx.x), as.logical(m.x)),
116              identical(as.mat(l10), m.x[1:10, 1:10] != 0),
117              identical(as.mat(l3 ), m.x[1:3, ] != 0)
118              )
119    
120    
121  ## --- negative indices ----------  ## --- negative indices ----------
122  mc <- mC[1:5, 1:7]  mc <- mC[1:5, 1:7]
# Line 130  Line 162 
162  mt[1:5, 2:6]  mt[1:5, 2:6]
163  as((mt0 - mt)[1:5,], "dsparseMatrix")# [1,5] and lines 2:3  as((mt0 - mt)[1:5,], "dsparseMatrix")# [1,5] and lines 2:3
164    
165  mt[c(2,4), ] <- 0  mt[c(2,4), ] <- 0; stopifnot(as(mt[c(2,4), ],"matrix") == 0)
166  mt[2:3, 4:7] <- 33  mt[2:3, 4:7] <- 33
167  validObject(mt)  validObject(mt)
168  mt  mt
169    
170  mc[1,4] <- -99  mc[1,4] <- -99 ; stopifnot(mc[1,4] == -99)
171    mc[1,4] <-  00 ; stopifnot(mc[1,4] ==  00)
172    mc[1,4] <- -99 ; stopifnot(mc[1,4] == -99)
173    mc[1:2,4:3] <- 4:1; stopifnot(as.matrix(mc[1:2,4:3]) == 4:1)
174    
175  mc[-1, 3] <- -2:1 # 0 should not be entered; 'value' recycled  mc[-1, 3] <- -2:1 # 0 should not be entered; 'value' recycled
176  mt[-1, 3] <- -2:1  mt[-1, 3] <- -2:1
177  stopifnot(mc@x != 0, mt@x != 0)  stopifnot(mc@x != 0, mt@x != 0,
178  mc[1:5 %% 2 == 0, 3] <- 0            mc[-1,3] == -2:1, mt[-1,3] == -2:1) ##--> BUG -- fixed
179    
180    ev <- 1:5 %% 2 == 0
181    mc[ev, 3] <- 0:1
182    if(FALSE)## FIXME
183     stopifnot(mc[ev, 3] == 0:1) ##-> BUG  {very peculiar; the 2nd time it works ...}
184  validObject(mc)  validObject(mc)
185  mc  mc # now shows a non-structural zeros
186  mc[ii, jj] <- 1:6  mc[ii, jj] <- 1:6
187  mc[c(2,5), c(3,5)] <- 3.2  mc[c(2,5), c(3,5)] <- 3.2
188  validObject(mc)  validObject(mc)
189    m. <- mc
190    mc[4,] <- 0
191  mc  mc
192    
193    H <- Hilbert(9)
194    Hc <- as(round(H, 3), "dsCMatrix")# a sparse matrix with no 0 ...
195    (trH <- tril(Hc[1:5, 1:5]))
196    stopifnot(is(trH, "triangularMatrix"), trH@uplo == "L")
197    
198    i <- c(1:2, 4, 6:7); j <- c(2:4,6)
199    H[i,j] <- 0
200    (H. <- round(as(H, "sparseMatrix"), 3)[ , 2:7])
201    Hc. <- Hc
202    Hc.[i,j] <- 0 ## now "works", but setting "non-structural" 0s
203    stopifnot(as.matrix(Hc.[i,j]) == 0)
204    Hc.[, 1:6]
205    
206  cat('Time elapsed: ', proc.time(),'\n') # for ``statistical reasons''  cat('Time elapsed: ', proc.time(),'\n') # for ``statistical reasons''

Legend:
Removed from v.1269  
changed lines
  Added in v.1665

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