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 1829, Thu Apr 26 06:35:20 2007 UTC
# Line 1  Line 1 
1  #### For both 'Extract' ("[") and 'Replace' ("[<-") Method testing  ## For both 'Extract' ("[") and 'Replace' ("[<-") Method testing
2    
3  library(Matrix)  library(Matrix)
4    
# Line 6  Line 6 
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 44  Line 44 
44  m[, 1] <- -1  m[, 1] <- -1
45  m[1:3,]  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 ]  g10 <- m [ m > 10 ]
55  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()  
56      stopifnot(10 == length(m[ m <= 10 ]))      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    ## 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 67  Line 81 
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,])  assert.EQ.mat(mC[1:2,], mm[1:2,])
84  stopifnot(all.equal(mC[,3],   mm[,3]))  
85    ## *repeated* indices - did not work at all ...
86    i <- rep(8:10,2)
87    j <- c(2:4, 4:3)
88    assert.EQ.mat(mC[i,], mm[i,])
89    assert.EQ.mat(mC[,j], mm[,j])
90    assert.EQ.mat(mC[i, 2:1], mm[i, 2:1])
91    assert.EQ.mat(mC[c(4,1), j], mm[c(4,1), j])
92    assert.EQ.mat(mC[i,j], mm[i,j])
93    set.seed(7)
94    for(n in 1:50) {
95        i <- sample(sample(nrow(mC), 7), 20, replace = TRUE)
96        j <- sample(sample(ncol(mC), 6), 17, replace = TRUE)
97        assert.EQ.mat(mC[i,j], mm[i,j])
98    }
99    ## symmetric index of symmetric matrix 000 not yet  ok
100    m. <- mC; m.[, c(2, 7:12)] <- 0
101    validObject(S <- crossprod(m.) %% 100)
102    ss <- as(S, "matrix")
103    T <- as(S, "TsparseMatrix")
104    i <- c(4:2,7) ;  assert.EQ.mat(T[i,i], ss[i,i])
105    i <- c(7:5, 2:4);assert.EQ.mat(T[i,i], ss[i,i])
106    if(FALSE) ## FIXME
107    assert.EQ.mat(T[j,j], ss[j,j])
108    
109    
110    stopifnot(all.equal(mC[,3], mm[,3]),
111              identical(mC[ij], mm[ij]))
112  assert.EQ.mat(mC[7, , drop=FALSE], mm[7, , drop=FALSE])  assert.EQ.mat(mC[7, , drop=FALSE], mm[7, , drop=FALSE])
113    
114  stopifnot(dim(mC[numeric(0), ]) == c(0,20), # used to give warnings  stopifnot(dim(mC[numeric(0), ]) == c(0,20), # used to give warnings
# Line 87  Line 128 
128            identical3(mm[3,], mC[3,], mT[3,]),            identical3(mm[3,], mC[3,], mT[3,]),
129            identical3(mT[2,3], mC[2,3], 0),            identical3(mT[2,3], mC[2,3], 0),
130            identical(mT[], mT),            identical(mT[], mT),
131            ## 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]),
132            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]))
133                       as(mT[c(3,7), 2:4],"matrix")))            )
134    
135  x.x <- crossprod(mC)  x.x <- crossprod(mC)
136  stopifnot(class(x.x) == "dsCMatrix",  stopifnot(class(x.x) == "dsCMatrix",
137            class(x.x. <- round(x.x / 10000)) == "dsCMatrix")            class(x.x. <- round(x.x / 10000)) == "dsCMatrix",
138              identical(x.x[cbind(2:6, 2:6)],
139                        diag(x.x [2:6, 2:6])))
140  head(x.x.) # Note the *non*-structural 0's printed as "0"  head(x.x.) # Note the *non*-structural 0's printed as "0"
141  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  
142    
143  lx.x <- as(x.x, "lsCMatrix") # FALSE only for "structural" 0  lx.x <- as(x.x, "lsCMatrix") # FALSE only for "structural" 0
144  if(FALSE) { ## FIXME: needs coercion  "lsCMatrix" to "lgTMatrix"  (l10 <- lx.x[1:10, 1:10])# "lsC"
145      lx.x[1:10, 1:10]  (l3 <-  lx.x[1:3, ])
146      lx.x[1:3, ]  m.x <- as(x.x, "matrix")
147  }  stopifnot(class(l10) == "lsCMatrix", # symmetric indexing -> symmetric !
148              identical(as.mat(lx.x), m.x != 0),
149              identical(as.logical(lx.x), as.logical(m.x)),
150              identical(as.mat(l10), m.x[1:10, 1:10] != 0),
151              identical(as.mat(l3 ), m.x[1:3, ] != 0)
152              )
153    
154    ## used to fail
155    n <- 5 ## or much larger
156    sm <- new("dsTMatrix", i=as.integer(1),j=as.integer(1),
157              Dim=as.integer(c(n,n)), x = 1)
158    (cm <- as(sm, "CsparseMatrix"))
159    sm[2,]
160    stopifnot(sm[2,] == c(0:1, rep.int(0,ncol(sm)-2)),
161              sm[2,] == cm[2,],
162              sm[,3] == sm[3,],
163              all(sm[,-(1:3)] == t(sm[-(1:3),])), # all(<lge.>)
164              all(sm[,-(1:3)] == 0)
165              )
166    
167    ### Diagonal -- Sparse:
168    m0 <- Diagonal(5)
169    (m1 <- as(m0, "sparseMatrix"))  # dtTMatrix
170    (m2 <- as(m0, "CsparseMatrix")) # dtCMatrix (with an irrelevant warning)
171    m1g <- as(m1, "generalMatrix")
172    stopifnot(is(m1g, "dgTMatrix"))
173    assert.EQ.mat(m2[1:3,],    diag(5)[1:3,])
174    assert.EQ.mat(m2[,c(4,1)], diag(5)[,c(4,1)])
175    stopifnot(identical(m2[1:3,], as(m1[1:3,], "CsparseMatrix")),
176              identical(Matrix:::uniqTsparse(m1[, c(4,2)]),
177                        Matrix:::uniqTsparse(as(m2[, c(4,2)], "TsparseMatrix")))
178              )## failed in 0.9975-11
179    
180    M <- m0; M[1,] <- 0
181    stopifnot(identical(M, Diagonal(x=c(0, rep(1,4)))))
182    M <- m0; M[,3] <- 3 ; M ; stopifnot(is(M, "sparseMatrix"), M[,3] == 3)
183    validObject(M)
184    M <- m0; M[1:3, 3] <- 0 ;M
185    T <- m0; T[1:3, 3] <- 10
186    stopifnot(identical(M, Diagonal(x=c(1,1, 0, 1,1))),
187              is(T, "triangularMatrix"), identical(T[,3], c(10,10,10,0,0)))
188    
189    M <- m1; M[1,] <- 0 ; M ; assert.EQ.mat(M, diag(c(0,rep(1,4))), tol=0)
190    M <- m1; M[,3] <- 3 ; stopifnot(is(M,"sparseMatrix"), M[,3] == 3)
191    validObject(M)
192    M <- m1; M[1:3, 3] <- 0 ;M
193    assert.EQ.mat(M, diag(c(1,1, 0, 1,1)), tol=0)
194    T <- m1; T[1:3, 3] <- 10; validObject(T)
195    stopifnot(is(T, "dtTMatrix"), identical(T[,3], c(10,10,10,0,0)))
196    
197    M <- m2; M[1,] <- 0 ; M ; assert.EQ.mat(M, diag(c(0,rep(1,4))), tol=0)
198    M <- m2; M[,3] <- 3 ; stopifnot(is(M,"sparseMatrix"), M[,3] == 3)
199    validObject(M)
200    M <- m2; M[1:3, 3] <- 0 ;M
201    assert.EQ.mat(M, diag(c(1,1, 0, 1,1)), tol=0)
202    T <- m2; T[1:3, 3] <- 10; validObject(T)
203    stopifnot(is(T, "dtCMatrix"), identical(T[,3], c(10,10,10,0,0)))
204    
205    
206  ## --- negative indices ----------  ## --- negative indices ----------
207  mc <- mC[1:5, 1:7]  mc <- mC[1:5, 1:7]
# Line 158  Line 257 
257  mc[1,4] <- -99 ; stopifnot(mc[1,4] == -99)  mc[1,4] <- -99 ; stopifnot(mc[1,4] == -99)
258  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)
259    
 ## Debugging:  R bug --   debug(Matrix:::replCmat)  has no effect  
   
260  mc[-1, 3] <- -2:1 # 0 should not be entered; 'value' recycled  mc[-1, 3] <- -2:1 # 0 should not be entered; 'value' recycled
261  mt[-1, 3] <- -2:1  mt[-1, 3] <- -2:1
262  stopifnot(mc@x != 0, mt@x != 0,  stopifnot(mc@x != 0, mt@x != 0,
263            mc[-1,3] == -2:1, mt[-1,3] == -2:1) ##--> BUG -- fixed            mc[-1,3] == -2:1, mt[-1,3] == -2:1) ## failed earlier
264  ## source("~/R/Pkgs/Matrix/R/Tsparse.R")  
265  ## Matrix_expand_pointers <- Matrix:::Matrix_expand_pointers  mc0 <- mc
266  ## -> open ../R/dgCMatrix.R  --> replCmat  .. now eval-line by line ..  mt0 <- as(mc0, "TsparseMatrix")
267    m0  <- as(mc0, "matrix")
268  ev <- 1:5 %% 2 == 0  set.seed(1)
269  mc[ev, 3] <- 0:1  for(i in 1:50) {
270  ##FIXME stopifnot(mc[ev, 3] == 0:1) ##-> BUG  {very peculiar; the 2nd time it works ...}      mc <- mc0; mt <- mt0 ; m <- m0
271  validObject(mc)      ev <- 1:5 %% 2 == round(runif(1))# 0 or 1
272  mc # now shows a non-structural zeros      j <- sample(ncol(mc), 1 + round(runif(1)))
273        nv <- rpois(sum(ev) * length(j), lambda = 1)
274        mc[ev, j] <- nv
275         m[ev, j] <- nv
276        mt[ev, j] <- nv
277        if(i %% 10 == 1) print(mc[ev,j, drop = FALSE])
278        stopifnot(as.vector(mc[ev, j]) == nv, ## failed earlier...
279                  as.vector(mt[ev, j]) == nv)
280        validObject(mc) ; assert.EQ.mat(mc, m)
281        validObject(mt) ; assert.EQ.mat(mt, m)
282    }
283    
284    mc # no longer has non-structural zeros
285  mc[ii, jj] <- 1:6  mc[ii, jj] <- 1:6
286  mc[c(2,5), c(3,5)] <- 3.2  mc[c(2,5), c(3,5)] <- 3.2
287  validObject(mc)  validObject(mc)
288  (m. <- mc)  m. <- mc
289  ## FIXME: mc[4,] <- 0 # -> error -- another Bug  mc[4,] <- 0
290    mc
291    
292  H <- Hilbert(9)  H <- Hilbert(9)
293  Hc <- as(round(H, 3), "dsCMatrix")  Hc <- as(round(H, 3), "dsCMatrix")# a sparse matrix with no 0 ...
294  tril(Hc[1:5, 1:5])  (trH <- tril(Hc[1:5, 1:5]))
295    stopifnot(is(trH, "triangularMatrix"), trH@uplo == "L")
296    
297  H[c(1:2, 4, 6:7), c(2:4,6)] <- 0  i <- c(1:2, 4, 6:7); j <- c(2:4,6)
298    H[i,j] <- 0
299  (H. <- round(as(H, "sparseMatrix"), 3)[ , 2:7])  (H. <- round(as(H, "sparseMatrix"), 3)[ , 2:7])
300  Hc. <- Hc  Hc. <- Hc
301  Hc.[c(1:2, 4, 6:7), c(2:4,6)] <- 0  Hc.[i,j] <- 0 ## now "works", but setting "non-structural" 0s
302    stopifnot(as.matrix(Hc.[i,j]) == 0)
303  Hc.[, 1:6]  Hc.[, 1:6]
304    
305    ## an example that failed for a long time
306    sy3 <- new("dsyMatrix", Dim = as.integer(c(2, 2)), x = c(14, -1, 2, -7))
307    validObject(dm <- kronecker(Diagonal(2), sy3))# now sparse with new kronecker
308    dm <- Matrix(as.matrix(dm))# -> "dsyMatrix"
309    (s2 <- as(dm, "sparseMatrix"))
310    validObject(st <- as(s2, "TsparseMatrix"))
311    stopifnot(is(s2, "symmetricMatrix"),
312              is(st, "symmetricMatrix"))
313    validObject(s.32  <- st[1:3,1:2]) ## 3 x 2 - and *not* dsTMatrix
314    validObject(s2.32 <- s2[1:3,1:2])
315    I <- c(1,4:3)
316    stopifnot(is(s2.32, "generalMatrix"),
317              is(s.32,  "generalMatrix"),
318              identical(as.mat(s.32), as.mat(s2.32)),
319              identical3(dm[1:3,-1], asD(s2[1:3,-1]), asD(st[1:3,-1])),
320              identical4(2, dm[4,3], s2[4,3], st[4,3]),
321              identical3(diag(dm), diag(s2), diag(st)),
322              is((cI <- s2[I,I]), "dsCMatrix"),
323              is((tI <- st[I,I]), "dsTMatrix"),
324              identical4(as.mat(dm)[I,I], as.mat(dm[I,I]), as.mat(tI), as.mat(cI))
325              )
326    
327    ## now sub-assign  and check for consistency
328    ## symmetric subassign should keep symmetry
329    st[I,I] <- 0; validObject(st); stopifnot(is(st,"symmetricMatrix"))
330    s2[I,I] <- 0; validObject(s2); stopifnot(is(s2,"symmetricMatrix"))
331    ##
332    m <- as.mat(st)
333     m[2:1,2:1] <- 4:1
334    st[2:1,2:1] <- 4:1
335    s2[2:1,2:1] <- 4:1
336    stopifnot(identical(m, as.mat(st)),
337              1:4 == as.vector(s2[1:2,1:2]),
338              identical(m, as.mat(s2)))
339    
340    ## now a slightly different situation for 's2' (had bug)
341    s2 <- as(dm, "sparseMatrix")
342    s2[I,I] <- 0; diag(s2)[2:3] <- -(1:2)
343    stopifnot(is(s2,"symmetricMatrix"), diag(s2) == c(0:-2,0))
344    t2 <- as(s2, "TsparseMatrix")
345    m <- as.mat(s2)
346    s2[2:1,2:1] <- 4:1
347    t2[2:1,2:1] <- 4:1
348     m[2:1,2:1] <- 4:1
349    assert.EQ.mat(t2, m)
350    assert.EQ.mat(s2, m)
351    ## and the same (for a different s2 !)
352    s2[2:1,2:1] <- 4:1
353    t2[2:1,2:1] <- 4:1
354    assert.EQ.mat(t2, m)# ok
355    assert.EQ.mat(s2, m)# failed in 0.9975-8
356    
357    
358    ## m[cbind(i,j)] <- value:
359    m.[ cbind(3:5, 1:3) ] <- 1:3
360    stopifnot(m.[3,1] == 1, m.[4,2] == 2)
361    x.x[ cbind(2:6, 2:6)] <- 12:16
362    validObject(x.x)
363    stopifnot(class(x.x) == "dsCMatrix",
364              12:16 == as.mat(x.x)[cbind(2:6, 2:6)])
365    (ne1 <- (mc - m.) != 0)
366    stopifnot(identical(ne1, 0 != abs(mc - m.)))
367    (ge <- m. >= mc) # contains "=" -> result is dense
368    ne. <- mc != m.  # was wrong (+ warning)
369    stopifnot(identical(!(m. < mc), m. >= mc),
370              identical(m. < mc, as(!ge, "sparseMatrix")),
371              identical(ne., Matrix:::drop0(ne1)))
372    
373    (M3 <- Matrix(upper.tri(matrix(, 3, 3)))) # ltC; indexing used to fail
374    T3 <- as(M3, "TsparseMatrix")
375    stopifnot(identical(drop(M3), M3),
376              identical4(drop(M3[,2, drop = FALSE]), M3[,2, drop = TRUE],
377                         drop(T3[,2, drop = FALSE]), T3[,2, drop = TRUE]),
378              is(T3, "triangularMatrix"),
379              !is(T3[,2, drop=FALSE], "triangularMatrix")
380              )
381    
382  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.1829

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