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 1331, Sat Jul 22 17:59:53 2006 UTC revision 1665, Fri Nov 3 23:18:07 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    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 87  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)  x.x <- crossprod(mC)
104  stopifnot(class(x.x) == "dsCMatrix",  stopifnot(class(x.x) == "dsCMatrix",
105            class(x.x. <- round(x.x / 10000)) == "dsCMatrix")            class(x.x. <- round(x.x / 10000)) == "dsCMatrix")
106  head(x.x.) # Note the *non*-structural 0's printed as "0"  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  lx.x <- as(x.x, "lsCMatrix") # FALSE only for "structural" 0
110  if(FALSE) { ## FIXME: needs coercion  "lsCMatrix" to "lgTMatrix"  (l10 <- lx.x[1:10, 1:10])# "lsC"
111      lx.x[1:10, 1:10]  (l3 <-  lx.x[1:3, ])
112      lx.x[1:3, ]  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 156  Line 172 
172  mc[1,4] <- -99 ; stopifnot(mc[1,4] == -99)  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)  mc[1:2,4:3] <- 4:1; stopifnot(as.matrix(mc[1:2,4:3]) == 4:1)
174    
 ## Debugging:  R bug --   debug(Matrix:::replCmat)  has no effect  
   
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,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 ..  
179    
180  ev <- 1:5 %% 2 == 0  ev <- 1:5 %% 2 == 0
181  mc[ev, 3] <- 0:1  mc[ev, 3] <- 0:1
182  ##FIXME stopifnot(mc[ev, 3] == 0:1) ##-> BUG  {very peculiar; the 2nd time it works ...}  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 # now shows a non-structural zeros  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)  m. <- mc
190  ## FIXME: mc[4,] <- 0 # -> error -- another Bug  mc[4,] <- 0
191    mc
192    
193  H <- Hilbert(9)  H <- Hilbert(9)
194  Hc <- as(round(H, 3), "dsCMatrix")  Hc <- as(round(H, 3), "dsCMatrix")# a sparse matrix with no 0 ...
195  tril(Hc[1:5, 1:5])  (trH <- tril(Hc[1:5, 1:5]))
196    stopifnot(is(trH, "triangularMatrix"), trH@uplo == "L")
197    
198  H[c(1:2, 4, 6:7), c(2:4,6)] <- 0  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])  (H. <- round(as(H, "sparseMatrix"), 3)[ , 2:7])
201  Hc. <- Hc  Hc. <- Hc
202  Hc.[c(1:2, 4, 6:7), c(2:4,6)] <- 0  Hc.[i,j] <- 0 ## now "works", but setting "non-structural" 0s
203    stopifnot(as.matrix(Hc.[i,j]) == 0)
204  Hc.[, 1:6]  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.1331  
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