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 873, Sat Aug 27 21:26:23 2005 UTC revision 1705, Sat Dec 16 12:37:25 2006 UTC
# Line 2  Line 2 
2    
3  library(Matrix)  library(Matrix)
4    
5  identical3 <- function(x,y,z)   identical(x,y) && identical (y,z)  source(system.file("test-tools.R", package = "Matrix"))# identical3() etc
 identical4 <- function(a,b,c,d) identical(a,b) && identical3(b,c,d)  
6    
7  ### Dense Matrices  ### Dense Matrices
8    
9  m <- Matrix(1:28, nrow = 7)  m <- Matrix(1:28 +0, nrow = 7)
10  validObject(m) ; m@x <- as.double(m@x) ; validObject(m)  validObject(m)
11  stopifnot(identical(m, m[]),  stopifnot(identical(m, m[]),
12            identical(m[2, 3],  16), # simple number            identical(m[2, 3],  16), # simple number
13            identical(m[2, 3:4], c(16,23))) # simple numeric of length 2            identical(m[2, 3:4], c(16,23))) # simple numeric of length 2
# Line 21  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]),
28            identical(m[2,], m[(1:nrow(m)) == 2, ]),            identical(m[2,], m[(1:nrow(m)) == 2, ]),
29            identical(m[,3:4], m[, (1:4) >= 3]))            identical(m[,3:4], m[, (1:4) >= 3]))
30    
31  ## dimnames index (TODO)  ## dimnames indexing:
32    mn <- m
33    dimnames(mn) <- list(paste("r",letters[1:nrow(mn)],sep=""),
34                         LETTERS[1:ncol(mn)])
35    mn["rd", "D"]
36    stopifnot(identical(mn["rc", "D"], mn[3,4]), mn[3,4] == 24,
37              identical(mn[, "A"], mn[,1]), mn[,1] == 1:7,
38              identical(mn[c("re", "rb"), "B"], mn[c(5,2), 2])
39              )
40    
41    mo <- m
42    m[2,3] <- 100
43    m[1:2, 4] <- 200
44    m[, 1] <- -1
45    m[1:3,]
46    
47    m. <- as.matrix(m)
48    
49    ## m[ cbind(i,j) ] indexing:
50    ij <- cbind(1:6, 2:3)
51    stopifnot(identical(m[ij], m.[ij]))
52    
53    ## testing operations on logical Matrices rather more than indexing:
54    g10 <- m [ m > 10 ]
55    stopifnot(18 == length(g10))
56    stopifnot(10 == length(m[ m <= 10 ]))
57    sel <- (20 <  m) & (m <  150)
58    sel.<- (20 <  m.)& (m.<  150)
59    nsel <-(20 >= m) | (m >= 150)
60    (ssel <- as(sel, "sparseMatrix"))
61    stopifnot(is(sel, "lMatrix"), is(ssel, "lsparseMatrix"),
62              identical3(as.mat(sel.), as.mat(sel), as.mat(ssel)),
63              identical3(!sel, !ssel, nsel), # !<sparse> is typically dense
64              identical3(m[ sel],  m[ ssel], as.matrix(m)[as.matrix( ssel)]),
65              identical3(m[!sel],  m[!ssel], as.matrix(m)[as.matrix(!ssel)])
66              )
67    
68  ## TODO: more --- particularly once we have "m > 10" working!  ## more sparse Matrices --------------------------------------
   
   
 ### Sparse Matrices  
69    
70  m <- 1:800  m <- 1:800
71  set.seed(101) ; m[sample(800, 600)] <- 0  set.seed(101) ; m[sample(800, 600)] <- 0
# Line 47  Line 80 
80  mC[,1]  mC[,1]
81  mC[1:2,]  mC[1:2,]
82  mC[7, drop = FALSE]  mC[7, drop = FALSE]
83    assert.EQ.mat(mC[1:2,], mm[1:2,])
84  mT[,c(2,4)]  stopifnot(all.equal(mC[,3], mm[,3]),
85  mT[1,]            identical(mC[ij], mm[ij]))
86  mT[4, drop = FALSE]  assert.EQ.mat(mC[7, , drop=FALSE], mm[7, , drop=FALSE])
87    
88    stopifnot(dim(mC[numeric(0), ]) == c(0,20), # used to give warnings
89              dim(mC[, integer(0)]) == c(40,0),
90              identical(mC[, integer(0)], mC[, FALSE]),
91              identical(mC[7,  drop = FALSE],
92                        mC[7,, drop = FALSE]))
93    validObject(print(mT[,c(2,4)]))
94    stopifnot(all.equal(mT[2,], mm[2,]),
95              ## row or column indexing in combination with t() :
96              identical(mT[2,], t(mT)[,2]),
97              identical(mT[-2,], t(t(mT)[,-2])),
98              identical(mT[c(2,5),], t(t(mT)[,c(2,5)]))
99              )
100    assert.EQ.mat(mT[4,, drop = FALSE], mm[4,, drop = FALSE])
101  stopifnot(identical3(mm[,1], mC[,1], mT[,1]),  stopifnot(identical3(mm[,1], mC[,1], mT[,1]),
102            identical3(mm[3,], mC[3,], mT[3,]),            identical3(mm[3,], mC[3,], mT[3,]),
103            identical3(mT[2,3], mC[2,3], 0),            identical3(mT[2,3], mC[2,3], 0),
104            identical(mT[], mT),            identical(mT[], mT),
105            ## TODO: identical4() with  m[c(3,7), 2:4]            identical4(       mm[c(3,7), 2:4],  as.mat( m[c(3,7), 2:4]),
106            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]))
107                       as(mT[c(3,7), 2:4],"matrix")))            )
108    
109    x.x <- crossprod(mC)
110    stopifnot(class(x.x) == "dsCMatrix",
111              class(x.x. <- round(x.x / 10000)) == "dsCMatrix",
112              identical(x.x[cbind(2:6, 2:6)],
113                        diag(x.x [2:6, 2:6])))
114    head(x.x.) # Note the *non*-structural 0's printed as "0"
115    tail(x.x., -3) # all but the first three lines
116    
117    lx.x <- as(x.x, "lsCMatrix") # FALSE only for "structural" 0
118    (l10 <- lx.x[1:10, 1:10])# "lsC"
119    (l3 <-  lx.x[1:3, ])
120    m.x <- as(x.x, "matrix")
121    stopifnot(class(l10) == "lsCMatrix", # symmetric indexing -> symmetric !
122              identical(as.mat(lx.x), m.x != 0),
123              identical(as.logical(lx.x), as.logical(m.x)),
124              identical(as.mat(l10), m.x[1:10, 1:10] != 0),
125              identical(as.mat(l3 ), m.x[1:3, ] != 0)
126              )
127    
128    ## used to fail
129    n <- 5 ## or much larger
130    sm <- new("dsTMatrix", i=as.integer(1),j=as.integer(1),
131              Dim=as.integer(c(n,n)), x = 1)
132    (cm <- as(sm, "CsparseMatrix"))
133    sm[2,]
134    stopifnot(sm[2,] == c(0:1, rep.int(0,ncol(sm)-2)),
135              sm[2,] == cm[2,],
136              sm[,3] == sm[3,],
137              all(sm[,-(1:3)] == t(sm[-(1:3),])), # all(<lge.>)
138              all(sm[,-(1:3)] == 0)
139              )
140    
141    
142    ## --- negative indices ----------
143    mc <- mC[1:5, 1:7]
144    mt <- mT[1:5, 1:7]
145    ## sub matrix
146    assert.EQ.mat(mC[1:2, 0:3], mm[1:2, 0:3]) # test 0-index
147    stopifnot(identical(mc[-(3:5), 0:2], mC[1:2, 0:2]),
148              identical(mt[-(3:5), 0:2], mT[1:2, 0:2]),
149              identical(mC[2:3, 4],      mm[2:3, 4]))
150    assert.EQ.mat(mC[1:2,], mm[1:2,])
151    ## sub vector
152    stopifnot(identical4(mc[-(1:4), ], mC[5, 1:7],
153                         mt[-(1:4), ], mT[5, 1:7]))
154    stopifnot(identical4(mc[-(1:4), -(2:4)], mC[5, c(1,5:7)],
155                         mt[-(1:4), -(2:4)], mT[5, c(1,5:7)]))
156    
157    ## mixing of negative and positive must give error
158    assertError(mT[-1:1,])
159    
160    ## Sub *Assignment* ---- now works (partially):
161    mt0 <- mt
162    mt[1, 4] <- -99
163    mt[2:3, 1:6] <- 0
164    mt
165    m2 <- mt+mt
166    m2[1,4] <- -200
167    m2[c(1,3), c(5:6,2)] <- 1:6
168    stopifnot(m2[1,4] == -200,
169              as.vector(m2[c(1,3), c(5:6,2)]) == 1:6)
170    mt[,3] <- 30
171    mt[2:3,] <- 250
172    mt[1:5 %% 2 == 1, 3] <- 0
173    mt[3:1, 1:7 > 5] <- 0
174    mt
175    
176    tt <- as(mt,"matrix")
177    ii <- c(0,2,5)
178    jj <- c(2:3,5)
179    tt[ii, jj] <- 1:6 # 0 is just "dropped"
180    mt[ii, jj] <- 1:6
181    assert.EQ.mat(mt, tt)
182    
183    mt[1:5, 2:6]
184    as((mt0 - mt)[1:5,], "dsparseMatrix")# [1,5] and lines 2:3
185    
186    mt[c(2,4), ] <- 0; stopifnot(as(mt[c(2,4), ],"matrix") == 0)
187    mt[2:3, 4:7] <- 33
188    validObject(mt)
189    mt
190    
191    mc[1,4] <- -99 ; stopifnot(mc[1,4] == -99)
192    mc[1,4] <-  00 ; stopifnot(mc[1,4] ==  00)
193    mc[1,4] <- -99 ; stopifnot(mc[1,4] == -99)
194    mc[1:2,4:3] <- 4:1; stopifnot(as.matrix(mc[1:2,4:3]) == 4:1)
195    
196    mc[-1, 3] <- -2:1 # 0 should not be entered; 'value' recycled
197    mt[-1, 3] <- -2:1
198    stopifnot(mc@x != 0, mt@x != 0,
199              mc[-1,3] == -2:1, mt[-1,3] == -2:1) ##--> BUG -- fixed
200    
201    ev <- 1:5 %% 2 == 0
202    mc[ev, 3] <- 0:1
203    if(FALSE)## FIXME
204     stopifnot(mc[ev, 3] == 0:1) ##-> BUG  {very peculiar; the 2nd time it works ...}
205    validObject(mc)
206    mc # now shows a non-structural zeros
207    mc[ii, jj] <- 1:6
208    mc[c(2,5), c(3,5)] <- 3.2
209    validObject(mc)
210    m. <- mc
211    mc[4,] <- 0
212    mc
213    
214    H <- Hilbert(9)
215    Hc <- as(round(H, 3), "dsCMatrix")# a sparse matrix with no 0 ...
216    (trH <- tril(Hc[1:5, 1:5]))
217    stopifnot(is(trH, "triangularMatrix"), trH@uplo == "L")
218    
219    i <- c(1:2, 4, 6:7); j <- c(2:4,6)
220    H[i,j] <- 0
221    (H. <- round(as(H, "sparseMatrix"), 3)[ , 2:7])
222    Hc. <- Hc
223    Hc.[i,j] <- 0 ## now "works", but setting "non-structural" 0s
224    stopifnot(as.matrix(Hc.[i,j]) == 0)
225    Hc.[, 1:6]
226    
227    ## an example that failed long
228    sy3 <- new("dsyMatrix", Dim = as.integer(c(2, 2)), x = c(14, -1, 2, -7))
229    validObject(dm <- kronecker(Diagonal(2), sy3))
230    (s2 <- as(dm, "sparseMatrix"))
231    validObject(st <- as(s2, "TsparseMatrix"))
232    validObject(s.32  <- st[1:3,1:2]) ## 3 x 2 - and *not* dsTMatrix
233    validObject(s2.32 <- s2[1:3,1:2])
234    I <- c(1,4:3)
235    stopifnot(is(s2.32, "generalMatrix"),
236              is(s.32,  "generalMatrix"),
237              identical(as.mat(s.32), as.mat(s2.32)),
238              identical3(dm[1:3,-1], asD(s2[1:3,-1]), asD(st[1:3,-1])),
239              identical4(2, dm[4,3], s2[4,3], st[4,3]),
240              identical3(diag(dm), diag(s2), diag(st)),
241              is((cI <- s2[I,I]), "dsCMatrix"),
242              is((tI <- st[I,I]), "dsTMatrix"),
243              identical4(as.mat(dm)[I,I], as.mat(dm[I,I]), as.mat(tI), as.mat(cI))
244              )
245    
246    ## now sub-assign  and check for consistency
247    ## symmetric subassign should keep symmetry
248    st[I,I] <- 0; validObject(st); stopifnot(is(st,"symmetricMatrix"))
249    s2[I,I] <- 0; validObject(s2); stopifnot(is(s2,"symmetricMatrix"))
250    
251    m <- as.mat(st)
252     m[2:1,2:1] <- 4:1
253    st[2:1,2:1] <- 4:1
254    s2[2:1,2:1] <- 4:1
255    stopifnot(identical(m, as.mat(st)),
256              1:4 == as.vector(s2[1:2,1:2]),
257              identical(m, as.mat(s2)))
258    
259    ## m[cbind(i,j)] <- value:
260    m.[ cbind(3:5, 1:3) ] <- 1:3
261    stopifnot(m.[3,1] == 1, m.[4,2] == 2)
262    x.x[ cbind(2:6, 2:6)] <- 12:16
263    validObject(x.x)
264    stopifnot(class(x.x) == "dsCMatrix",
265              12:16 == as.mat(x.x)[cbind(2:6, 2:6)])
266    
267    cat('Time elapsed: ', proc.time(),'\n') # for ``statistical reasons''

Legend:
Removed from v.873  
changed lines
  Added in v.1705

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