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 1226, Mon Mar 13 14:07:58 2006 UTC revision 1655, Mon Oct 30 17:16:27 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  mT[,c(2,4)]  validObject(print(mT[,c(2,4)]))
88  stopifnot(all.equal(mT[1,], mm[1,]))  stopifnot(all.equal(mT[2,], mm[2,]),
89              ## row or column indexing in combination with t() :
90              identical(mT[2,], t(mT)[,2]),
91              identical(mT[-2,], t(t(mT)[,-2])),
92              identical(mT[c(2,5),], t(t(mT)[,c(2,5)]))
93              )
94  assert.EQ.mat(mT[4,, drop = FALSE], mm[4,, drop = FALSE])  assert.EQ.mat(mT[4,, drop = FALSE], mm[4,, drop = FALSE])
95  stopifnot(identical3(mm[,1], mC[,1], mT[,1]),  stopifnot(identical3(mm[,1], mC[,1], mT[,1]),
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'            ## TODO: identical4() with  m[c(3,7), 2:4] - fail because of 'dimnames'
100            identical3(as(mC[c(3,7), 2:4],"matrix"), mm[c(3,7), 2:4],            ## TODO: identical3() with as(mC[c(3,7), 2:4],"matrix"),
101                       as(mT[c(3,7), 2:4],"matrix")))            ##       fails because of 'dimnames'
102              identical(mm[c(3,7), 2:4], as(mT[c(3,7), 2:4],"matrix"))
103              )
104    
105    x.x <- crossprod(mC)
106    stopifnot(class(x.x) == "dsCMatrix",
107              class(x.x. <- round(x.x / 10000)) == "dsCMatrix")
108    head(x.x.) # Note the *non*-structural 0's printed as "0"
109    tail(x.x., -3) # all but the first three lines
110    
111    lx.x <- as(x.x, "lsCMatrix") # FALSE only for "structural" 0
112    if(FALSE) { ## FIXME: needs coercion  "lsCMatrix" to "lgTMatrix"
113        lx.x[1:10, 1:10]
114        lx.x[1:3, ]
115    }
116    
117  ## --- negative indices ----------  ## --- negative indices ----------
118  mc <- mC[1:5, 1:7]  mc <- mC[1:5, 1:7]
# Line 125  Line 158 
158  mt[1:5, 2:6]  mt[1:5, 2:6]
159  as((mt0 - mt)[1:5,], "dsparseMatrix")# [1,5] and lines 2:3  as((mt0 - mt)[1:5,], "dsparseMatrix")# [1,5] and lines 2:3
160    
161  mt[c(2,4), ] <- 0  mt[c(2,4), ] <- 0; stopifnot(as(mt[c(2,4), ],"matrix") == 0)
162  mt[2:3, 4:7] <- 33  mt[2:3, 4:7] <- 33
163  validObject(mt)  validObject(mt)
164  mt  mt
165    
166  mc[1,4] <- -99  mc[1,4] <- -99 ; stopifnot(mc[1,4] == -99)
167    mc[1,4] <-  00 ; stopifnot(mc[1,4] ==  00)
168    mc[1,4] <- -99 ; stopifnot(mc[1,4] == -99)
169    mc[1:2,4:3] <- 4:1; stopifnot(as.matrix(mc[1:2,4:3]) == 4:1)
170    
171  mc[-1, 3] <- -2:1 # 0 should not be entered; 'value' recycled  mc[-1, 3] <- -2:1 # 0 should not be entered; 'value' recycled
172  mt[-1, 3] <- -2:1  mt[-1, 3] <- -2:1
173  stopifnot(mc@x != 0, mt@x != 0)  stopifnot(mc@x != 0, mt@x != 0,
174  mc[1:5 %% 2 == 0, 3] <- 0            mc[-1,3] == -2:1, mt[-1,3] == -2:1) ##--> BUG -- fixed
175    
176    ev <- 1:5 %% 2 == 0
177    mc[ev, 3] <- 0:1
178    if(FALSE)## FIXME
179     stopifnot(mc[ev, 3] == 0:1) ##-> BUG  {very peculiar; the 2nd time it works ...}
180  validObject(mc)  validObject(mc)
181  mc  mc # now shows a non-structural zeros
182  mc[ii, jj] <- 1:6  mc[ii, jj] <- 1:6
183  mc[c(2,5), c(3,5)] <- 3.2  mc[c(2,5), c(3,5)] <- 3.2
184  validObject(mc)  validObject(mc)
185    m. <- mc
186    mc[4,] <- 0
187  mc  mc
188    
189    H <- Hilbert(9)
190    Hc <- as(round(H, 3), "dsCMatrix")# a sparse matrix with no 0 ...
191    (trH <- tril(Hc[1:5, 1:5]))
192    stopifnot(is(trH, "triangularMatrix"), trH@uplo == "L")
193    
194    i <- c(1:2, 4, 6:7); j <- c(2:4,6)
195    H[i,j] <- 0
196    (H. <- round(as(H, "sparseMatrix"), 3)[ , 2:7])
197    Hc. <- Hc
198    Hc.[i,j] <- 0 ## now "works", but setting "non-structural" 0s
199    stopifnot(as.matrix(Hc.[i,j]) == 0)
200    Hc.[, 1:6]
201    
202  cat('Time elapsed: ', proc.time(),'\n') # for ``statistical reasons''  cat('Time elapsed: ', proc.time(),'\n') # for ``statistical reasons''

Legend:
Removed from v.1226  
changed lines
  Added in v.1655

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