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 1367, Sat Aug 12 17:33: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 44  Line 46 
46    
47  g10 <- m [ m > 10 ]  g10 <- m [ m > 10 ]
48  stopifnot(18 == length(g10))  stopifnot(18 == length(g10))
49  if(paste(R.version$major, R.version$minor, sep=".") >= "2.3")  ## needs R >= 2.3.0 [Buglet in R(<= 2.2.1)'s possibleExtends()]:
     ## Buglet in R(<= 2.2.1)'s possibleExtends()  
50      stopifnot(10 == length(m[ m <= 10 ]))      stopifnot(10 == length(m[ m <= 10 ]))
51    
52    
# Line 68  Line 69 
69  stopifnot(all.equal(mC[,3],   mm[,3]))  stopifnot(all.equal(mC[,3],   mm[,3]))
70  assert.EQ.mat(mC[7, , drop=FALSE], mm[7, , drop=FALSE])  assert.EQ.mat(mC[7, , drop=FALSE], mm[7, , drop=FALSE])
71    
72  stopifnot(identical(mC[7,  drop = FALSE],  stopifnot(dim(mC[numeric(0), ]) == c(0,20), # used to give warnings
73              dim(mC[, integer(0)]) == c(40,0),
74              identical(mC[, integer(0)], mC[, FALSE]),
75              identical(mC[7,  drop = FALSE],
76                      mC[7,, drop = FALSE]))                      mC[7,, drop = FALSE]))
77  mT[,c(2,4)]  validObject(print(mT[,c(2,4)]))
78  stopifnot(all.equal(mT[1,], mm[1,]))  stopifnot(all.equal(mT[2,], mm[2,]),
79              ## row or column indexing in combination with t() :
80              identical(mT[2,], t(mT)[,2]),
81              identical(mT[-2,], t(t(mT)[,-2])),
82              identical(mT[c(2,5),], t(t(mT)[,c(2,5)]))
83              )
84  assert.EQ.mat(mT[4,, drop = FALSE], mm[4,, drop = FALSE])  assert.EQ.mat(mT[4,, drop = FALSE], mm[4,, drop = FALSE])
85  stopifnot(identical3(mm[,1], mC[,1], mT[,1]),  stopifnot(identical3(mm[,1], mC[,1], mT[,1]),
86            identical3(mm[3,], mC[3,], mT[3,]),            identical3(mm[3,], mC[3,], mT[3,]),
87            identical3(mT[2,3], mC[2,3], 0),            identical3(mT[2,3], mC[2,3], 0),
88            identical(mT[], mT),            identical(mT[], mT),
89            ## 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'
90            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"),
91                       as(mT[c(3,7), 2:4],"matrix")))            ##       fails because of 'dimnames'
92              identical(mm[c(3,7), 2:4], as(mT[c(3,7), 2:4],"matrix"))
93              )
94    
95    x.x <- crossprod(mC)
96    stopifnot(class(x.x) == "dsCMatrix",
97              class(x.x. <- round(x.x / 10000)) == "dsCMatrix")
98    head(x.x.) # Note the *non*-structural 0's printed as "0"
99    ## FIXME (once we require 2.4.x or higher):
100    ##  tail(x.x., -2) # the last two lines
101    
102    lx.x <- as(x.x, "lsCMatrix") # FALSE only for "structural" 0
103    if(FALSE) { ## FIXME: needs coercion  "lsCMatrix" to "lgTMatrix"
104        lx.x[1:10, 1:10]
105        lx.x[1:3, ]
106    }
107    
108  ## --- negative indices ----------  ## --- negative indices ----------
109  mc <- mC[1:5, 1:7]  mc <- mC[1:5, 1:7]
# Line 125  Line 149 
149  mt[1:5, 2:6]  mt[1:5, 2:6]
150  as((mt0 - mt)[1:5,], "dsparseMatrix")# [1,5] and lines 2:3  as((mt0 - mt)[1:5,], "dsparseMatrix")# [1,5] and lines 2:3
151    
152  mt[c(2,4), ] <- 0  mt[c(2,4), ] <- 0; stopifnot(as(mt[c(2,4), ],"matrix") == 0)
153  mt[2:3, 4:7] <- 33  mt[2:3, 4:7] <- 33
154  validObject(mt)  validObject(mt)
155  mt  mt
156    
157  mc[1,4] <- -99  mc[1,4] <- -99 ; stopifnot(mc[1,4] == -99)
158    mc[1,4] <-  00 ; stopifnot(mc[1,4] ==  00)
159    mc[1,4] <- -99 ; stopifnot(mc[1,4] == -99)
160    mc[1:2,4:3] <- 4:1; stopifnot(as.matrix(mc[1:2,4:3]) == 4:1)
161    
162    ## Debugging:  R bug --   debug(Matrix:::replCmat)  has no effect
163    
164  mc[-1, 3] <- -2:1 # 0 should not be entered; 'value' recycled  mc[-1, 3] <- -2:1 # 0 should not be entered; 'value' recycled
165  mt[-1, 3] <- -2:1  mt[-1, 3] <- -2:1
166  stopifnot(mc@x != 0, mt@x != 0)  stopifnot(mc@x != 0, mt@x != 0,
167  mc[1:5 %% 2 == 0, 3] <- 0            mc[-1,3] == -2:1, mt[-1,3] == -2:1) ##--> BUG -- fixed
168    ## source("~/R/Pkgs/Matrix/R/Tsparse.R")
169    ## Matrix_expand_pointers <- Matrix:::Matrix_expand_pointers
170    ## -> open ../R/dgCMatrix.R  --> replCmat  .. now eval-line by line ..
171    
172    ev <- 1:5 %% 2 == 0
173    mc[ev, 3] <- 0:1
174    ##FIXME stopifnot(mc[ev, 3] == 0:1) ##-> BUG  {very peculiar; the 2nd time it works ...}
175  validObject(mc)  validObject(mc)
176  mc  mc # now shows a non-structural zeros
177  mc[ii, jj] <- 1:6  mc[ii, jj] <- 1:6
178  mc[c(2,5), c(3,5)] <- 3.2  mc[c(2,5), c(3,5)] <- 3.2
179  validObject(mc)  validObject(mc)
180  mc  (m. <- mc)
181    ## FIXME: mc[4,] <- 0 # -> error -- another Bug
182    
183    H <- Hilbert(9)
184    Hc <- as(round(H, 3), "dsCMatrix")
185    tril(Hc[1:5, 1:5])
186    
187    H[c(1:2, 4, 6:7), c(2:4,6)] <- 0
188    (H. <- round(as(H, "sparseMatrix"), 3)[ , 2:7])
189    Hc. <- Hc
190    ## This assignment does not achieve the correct effect.  replCmat should be changed.
191    Hc.[c(1:2, 4, 6:7), c(2:4,6)] <- 0
192    Hc.[, 1:6]
193    
194  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.1367

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