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 1347, Mon Aug 7 08:47:43 2006 UTC revision 1599, Fri Sep 29 09:39:50 2006 UTC
# Line 44  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  g10 <- m [ m > 10 ]  g10 <- m [ m > 10 ]
49  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()  
50      stopifnot(10 == length(m[ m <= 10 ]))      stopifnot(10 == length(m[ m <= 10 ]))
51    sel <- (20 <  m) & (m <  150)
52    nsel <-(20 >= m) | (m >= 150)
53    (ssel <- as(sel, "sparseMatrix"))
54    stopifnot(is(sel, "lMatrix"), is(ssel, "lsparseMatrix"),
55              identical3(!sel, !ssel, nsel), # !<sparse> is typically dense
56              identical3(m[ sel],  m[ ssel], as.matrix(m)[as.matrix( ssel)]),
57              identical3(m[!sel],  m[!ssel], as.matrix(m)[as.matrix(!ssel)])
58              )
59    
60    ## more sparse Matrices --------------------------------------
 ### Sparse Matrices --------------------------------------  
61    
62  m <- 1:800  m <- 1:800
63  set.seed(101) ; m[sample(800, 600)] <- 0  set.seed(101) ; m[sample(800, 600)] <- 0
# Line 88  Line 94 
94            identical3(mT[2,3], mC[2,3], 0),            identical3(mT[2,3], mC[2,3], 0),
95            identical(mT[], mT),            identical(mT[], mT),
96            ## 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'
97            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"),
98                       as(mT[c(3,7), 2:4],"matrix")))            ##       fails because of 'dimnames'
99              identical(mm[c(3,7), 2:4], as(mT[c(3,7), 2:4],"matrix"))
100              )
101    
102  x.x <- crossprod(mC)  x.x <- crossprod(mC)
103  stopifnot(class(x.x) == "dsCMatrix",  stopifnot(class(x.x) == "dsCMatrix",
104            class(x.x. <- round(x.x / 10000)) == "dsCMatrix")            class(x.x. <- round(x.x / 10000)) == "dsCMatrix")
105  head(x.x.) # Note the *non*-structural 0's printed as "0"  head(x.x.) # Note the *non*-structural 0's printed as "0"
106  if(paste(R.version$major, R.version$minor, sep=".") >= "2.4")  tail(x.x., -3) # all but the first three lines
     tail(x.x., -2) # the last two lines  
107    
108  lx.x <- as(x.x, "lsCMatrix") # FALSE only for "structural" 0  lx.x <- as(x.x, "lsCMatrix") # FALSE only for "structural" 0
109  if(FALSE) { ## FIXME: needs coercion  "lsCMatrix" to "lgTMatrix"  if(FALSE) { ## FIXME: needs coercion  "lsCMatrix" to "lgTMatrix"
# Line 158  Line 165 
165  mc[1,4] <- -99 ; stopifnot(mc[1,4] == -99)  mc[1,4] <- -99 ; stopifnot(mc[1,4] == -99)
166  mc[1:2,4:3] <- 4:1; stopifnot(as.matrix(mc[1:2,4:3]) == 4:1)  mc[1:2,4:3] <- 4:1; stopifnot(as.matrix(mc[1:2,4:3]) == 4:1)
167    
 ## Debugging:  R bug --   debug(Matrix:::replCmat)  has no effect  
   
168  mc[-1, 3] <- -2:1 # 0 should not be entered; 'value' recycled  mc[-1, 3] <- -2:1 # 0 should not be entered; 'value' recycled
169  mt[-1, 3] <- -2:1  mt[-1, 3] <- -2:1
170  stopifnot(mc@x != 0, mt@x != 0,  stopifnot(mc@x != 0, mt@x != 0,
171            mc[-1,3] == -2:1, mt[-1,3] == -2:1) ##--> BUG -- fixed            mc[-1,3] == -2:1, mt[-1,3] == -2:1) ##--> BUG -- fixed
 ## source("~/R/Pkgs/Matrix/R/Tsparse.R")  
 ## Matrix_expand_pointers <- Matrix:::Matrix_expand_pointers  
 ## -> open ../R/dgCMatrix.R  --> replCmat  .. now eval-line by line ..  
172    
173  ev <- 1:5 %% 2 == 0  ev <- 1:5 %% 2 == 0
174  mc[ev, 3] <- 0:1  mc[ev, 3] <- 0:1
175  ##FIXME stopifnot(mc[ev, 3] == 0:1) ##-> BUG  {very peculiar; the 2nd time it works ...}  if(FALSE)## FIXME
176     stopifnot(mc[ev, 3] == 0:1) ##-> BUG  {very peculiar; the 2nd time it works ...}
177  validObject(mc)  validObject(mc)
178  mc # now shows a non-structural zeros  mc # now shows a non-structural zeros
179  mc[ii, jj] <- 1:6  mc[ii, jj] <- 1:6
180  mc[c(2,5), c(3,5)] <- 3.2  mc[c(2,5), c(3,5)] <- 3.2
181  validObject(mc)  validObject(mc)
182  (m. <- mc)  (m. <- mc)
183  ## FIXME: mc[4,] <- 0 # -> error -- another Bug  if(FALSE)## FIXME:
184     mc[4,] <- 0 # -> error -- another Bug
185    
186  H <- Hilbert(9)  H <- Hilbert(9)
187  Hc <- as(round(H, 3), "dsCMatrix")  Hc <- as(round(H, 3), "dsCMatrix")# a sparse matrix with no 0 ...
188  tril(Hc[1:5, 1:5])  (trH <- tril(Hc[1:5, 1:5]))
189    stopifnot(is(trH, "triangularMatrix"), trH@uplo == "L")
190    
191  H[c(1:2, 4, 6:7), c(2:4,6)] <- 0  i <- c(1:2, 4, 6:7); j <- c(2:4,6)
192    H[i,j] <- 0
193  (H. <- round(as(H, "sparseMatrix"), 3)[ , 2:7])  (H. <- round(as(H, "sparseMatrix"), 3)[ , 2:7])
194  Hc. <- Hc  Hc. <- Hc
195  Hc.[c(1:2, 4, 6:7), c(2:4,6)] <- 0  Hc.[i,j] <- 0 ## now "works", but setting "non-structural" 0s
196    stopifnot(as.matrix(Hc.[i,j]) == 0)
197  Hc.[, 1:6]  Hc.[, 1:6]
198    
199  cat('Time elapsed: ', proc.time(),'\n') # for ``statistical reasons''  cat('Time elapsed: ', proc.time(),'\n') # for ``statistical reasons''

Legend:
Removed from v.1347  
changed lines
  Added in v.1599

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