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 1825, Thu Apr 12 13:21:51 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    if(FALSE) ## FIXME
91    assert.EQ.mat(mC[i,j], mm[i,j])
92    
93    stopifnot(all.equal(mC[,3], mm[,3]),
94              identical(mC[ij], mm[ij]))
95  assert.EQ.mat(mC[7, , drop=FALSE], mm[7, , drop=FALSE])  assert.EQ.mat(mC[7, , drop=FALSE], mm[7, , drop=FALSE])
96    
97  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 111 
111            identical3(mm[3,], mC[3,], mT[3,]),            identical3(mm[3,], mC[3,], mT[3,]),
112            identical3(mT[2,3], mC[2,3], 0),            identical3(mT[2,3], mC[2,3], 0),
113            identical(mT[], mT),            identical(mT[], mT),
114            ## 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]),
115            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]))
116                       as(mT[c(3,7), 2:4],"matrix")))            )
117    
118  x.x <- crossprod(mC)  x.x <- crossprod(mC)
119  stopifnot(class(x.x) == "dsCMatrix",  stopifnot(class(x.x) == "dsCMatrix",
120            class(x.x. <- round(x.x / 10000)) == "dsCMatrix")            class(x.x. <- round(x.x / 10000)) == "dsCMatrix",
121              identical(x.x[cbind(2:6, 2:6)],
122                        diag(x.x [2:6, 2:6])))
123  head(x.x.) # Note the *non*-structural 0's printed as "0"  head(x.x.) # Note the *non*-structural 0's printed as "0"
124  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  
125    
126  lx.x <- as(x.x, "lsCMatrix") # FALSE only for "structural" 0  lx.x <- as(x.x, "lsCMatrix") # FALSE only for "structural" 0
127  if(FALSE) { ## FIXME: needs coercion  "lsCMatrix" to "lgTMatrix"  (l10 <- lx.x[1:10, 1:10])# "lsC"
128      lx.x[1:10, 1:10]  (l3 <-  lx.x[1:3, ])
129      lx.x[1:3, ]  m.x <- as(x.x, "matrix")
130  }  stopifnot(class(l10) == "lsCMatrix", # symmetric indexing -> symmetric !
131              identical(as.mat(lx.x), m.x != 0),
132              identical(as.logical(lx.x), as.logical(m.x)),
133              identical(as.mat(l10), m.x[1:10, 1:10] != 0),
134              identical(as.mat(l3 ), m.x[1:3, ] != 0)
135              )
136    
137    ## used to fail
138    n <- 5 ## or much larger
139    sm <- new("dsTMatrix", i=as.integer(1),j=as.integer(1),
140              Dim=as.integer(c(n,n)), x = 1)
141    (cm <- as(sm, "CsparseMatrix"))
142    sm[2,]
143    stopifnot(sm[2,] == c(0:1, rep.int(0,ncol(sm)-2)),
144              sm[2,] == cm[2,],
145              sm[,3] == sm[3,],
146              all(sm[,-(1:3)] == t(sm[-(1:3),])), # all(<lge.>)
147              all(sm[,-(1:3)] == 0)
148              )
149    
150    ### Diagonal -- Sparse:
151    m0 <- Diagonal(5)
152    (m1 <- as(m0, "sparseMatrix"))  # dtTMatrix
153    (m2 <- as(m0, "CsparseMatrix")) # dtCMatrix (with an irrelevant warning)
154    m1g <- as(m1, "generalMatrix")
155    stopifnot(is(m1g, "dgTMatrix"))
156    assert.EQ.mat(m2[1:3,],    diag(5)[1:3,])
157    assert.EQ.mat(m2[,c(4,1)], diag(5)[,c(4,1)])
158    stopifnot(identical(m2[1:3,], as(m1[1:3,], "CsparseMatrix")),
159              identical(Matrix:::uniqTsparse(m1[, c(4,2)]),
160                        Matrix:::uniqTsparse(as(m2[, c(4,2)], "TsparseMatrix")))
161              )## failed in 0.9975-11
162    
163    M <- m0; M[1,] <- 0
164    stopifnot(identical(M, Diagonal(x=c(0, rep(1,4)))))
165    M <- m0; M[,3] <- 3 ; M ; stopifnot(is(M, "sparseMatrix"), M[,3] == 3)
166    validObject(M)
167    M <- m0; M[1:3, 3] <- 0 ;M
168    T <- m0; T[1:3, 3] <- 10
169    stopifnot(identical(M, Diagonal(x=c(1,1, 0, 1,1))),
170              is(T, "triangularMatrix"), identical(T[,3], c(10,10,10,0,0)))
171    
172    M <- m1; M[1,] <- 0 ; M ; assert.EQ.mat(M, diag(c(0,rep(1,4))), tol=0)
173    M <- m1; M[,3] <- 3 ; stopifnot(is(M,"sparseMatrix"), M[,3] == 3)
174    validObject(M)
175    M <- m1; M[1:3, 3] <- 0 ;M
176    assert.EQ.mat(M, diag(c(1,1, 0, 1,1)), tol=0)
177    T <- m1; T[1:3, 3] <- 10; validObject(T)
178    stopifnot(is(T, "dtTMatrix"), identical(T[,3], c(10,10,10,0,0)))
179    
180    M <- m2; M[1,] <- 0 ; M ; assert.EQ.mat(M, diag(c(0,rep(1,4))), tol=0)
181    M <- m2; M[,3] <- 3 ; stopifnot(is(M,"sparseMatrix"), M[,3] == 3)
182    validObject(M)
183    M <- m2; M[1:3, 3] <- 0 ;M
184    assert.EQ.mat(M, diag(c(1,1, 0, 1,1)), tol=0)
185    T <- m2; T[1:3, 3] <- 10; validObject(T)
186    stopifnot(is(T, "dtCMatrix"), identical(T[,3], c(10,10,10,0,0)))
187    
188    
189  ## --- negative indices ----------  ## --- negative indices ----------
190  mc <- mC[1:5, 1:7]  mc <- mC[1:5, 1:7]
# Line 158  Line 240 
240  mc[1,4] <- -99 ; stopifnot(mc[1,4] == -99)  mc[1,4] <- -99 ; stopifnot(mc[1,4] == -99)
241  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)
242    
 ## Debugging:  R bug --   debug(Matrix:::replCmat)  has no effect  
   
243  mc[-1, 3] <- -2:1 # 0 should not be entered; 'value' recycled  mc[-1, 3] <- -2:1 # 0 should not be entered; 'value' recycled
244  mt[-1, 3] <- -2:1  mt[-1, 3] <- -2:1
245  stopifnot(mc@x != 0, mt@x != 0,  stopifnot(mc@x != 0, mt@x != 0,
246            mc[-1,3] == -2:1, mt[-1,3] == -2:1) ##--> BUG -- fixed            mc[-1,3] == -2:1, mt[-1,3] == -2:1) ## failed earlier
247  ## source("~/R/Pkgs/Matrix/R/Tsparse.R")  
248  ## Matrix_expand_pointers <- Matrix:::Matrix_expand_pointers  mc0 <- mc
249  ## -> open ../R/dgCMatrix.R  --> replCmat  .. now eval-line by line ..  mt0 <- as(mc0, "TsparseMatrix")
250    m0  <- as(mc0, "matrix")
251  ev <- 1:5 %% 2 == 0  set.seed(1)
252  mc[ev, 3] <- 0:1  for(i in 1:50) {
253  ##FIXME stopifnot(mc[ev, 3] == 0:1) ##-> BUG  {very peculiar; the 2nd time it works ...}      mc <- mc0; mt <- mt0 ; m <- m0
254  validObject(mc)      ev <- 1:5 %% 2 == round(runif(1))# 0 or 1
255  mc # now shows a non-structural zeros      j <- sample(ncol(mc), 1 + round(runif(1)))
256        nv <- rpois(sum(ev) * length(j), lambda = 1)
257        mc[ev, j] <- nv
258         m[ev, j] <- nv
259        mt[ev, j] <- nv
260        if(i %% 10 == 1) print(mc[ev,j, drop = FALSE])
261        stopifnot(as.vector(mc[ev, j]) == nv, ## failed earlier...
262                  as.vector(mt[ev, j]) == nv)
263        validObject(mc) ; assert.EQ.mat(mc, m)
264        validObject(mt) ; assert.EQ.mat(mt, m)
265    }
266    
267    mc # no longer has non-structural zeros
268  mc[ii, jj] <- 1:6  mc[ii, jj] <- 1:6
269  mc[c(2,5), c(3,5)] <- 3.2  mc[c(2,5), c(3,5)] <- 3.2
270  validObject(mc)  validObject(mc)
271  (m. <- mc)  m. <- mc
272  ## FIXME: mc[4,] <- 0 # -> error -- another Bug  mc[4,] <- 0
273    mc
274    
275  H <- Hilbert(9)  H <- Hilbert(9)
276  Hc <- as(round(H, 3), "dsCMatrix")  Hc <- as(round(H, 3), "dsCMatrix")# a sparse matrix with no 0 ...
277  tril(Hc[1:5, 1:5])  (trH <- tril(Hc[1:5, 1:5]))
278    stopifnot(is(trH, "triangularMatrix"), trH@uplo == "L")
279    
280  H[c(1:2, 4, 6:7), c(2:4,6)] <- 0  i <- c(1:2, 4, 6:7); j <- c(2:4,6)
281    H[i,j] <- 0
282  (H. <- round(as(H, "sparseMatrix"), 3)[ , 2:7])  (H. <- round(as(H, "sparseMatrix"), 3)[ , 2:7])
283  Hc. <- Hc  Hc. <- Hc
284  Hc.[c(1:2, 4, 6:7), c(2:4,6)] <- 0  Hc.[i,j] <- 0 ## now "works", but setting "non-structural" 0s
285    stopifnot(as.matrix(Hc.[i,j]) == 0)
286  Hc.[, 1:6]  Hc.[, 1:6]
287    
288    ## an example that failed for a long time
289    sy3 <- new("dsyMatrix", Dim = as.integer(c(2, 2)), x = c(14, -1, 2, -7))
290    validObject(dm <- kronecker(Diagonal(2), sy3))# now sparse with new kronecker
291    dm <- Matrix(as.matrix(dm))# -> "dsyMatrix"
292    (s2 <- as(dm, "sparseMatrix"))
293    validObject(st <- as(s2, "TsparseMatrix"))
294    stopifnot(is(s2, "symmetricMatrix"),
295              is(st, "symmetricMatrix"))
296    validObject(s.32  <- st[1:3,1:2]) ## 3 x 2 - and *not* dsTMatrix
297    validObject(s2.32 <- s2[1:3,1:2])
298    I <- c(1,4:3)
299    stopifnot(is(s2.32, "generalMatrix"),
300              is(s.32,  "generalMatrix"),
301              identical(as.mat(s.32), as.mat(s2.32)),
302              identical3(dm[1:3,-1], asD(s2[1:3,-1]), asD(st[1:3,-1])),
303              identical4(2, dm[4,3], s2[4,3], st[4,3]),
304              identical3(diag(dm), diag(s2), diag(st)),
305              is((cI <- s2[I,I]), "dsCMatrix"),
306              is((tI <- st[I,I]), "dsTMatrix"),
307              identical4(as.mat(dm)[I,I], as.mat(dm[I,I]), as.mat(tI), as.mat(cI))
308              )
309    
310    ## now sub-assign  and check for consistency
311    ## symmetric subassign should keep symmetry
312    st[I,I] <- 0; validObject(st); stopifnot(is(st,"symmetricMatrix"))
313    s2[I,I] <- 0; validObject(s2); stopifnot(is(s2,"symmetricMatrix"))
314    ##
315    m <- as.mat(st)
316     m[2:1,2:1] <- 4:1
317    st[2:1,2:1] <- 4:1
318    s2[2:1,2:1] <- 4:1
319    stopifnot(identical(m, as.mat(st)),
320              1:4 == as.vector(s2[1:2,1:2]),
321              identical(m, as.mat(s2)))
322    
323    ## now a slightly different situation for 's2' (had bug)
324    s2 <- as(dm, "sparseMatrix")
325    s2[I,I] <- 0; diag(s2)[2:3] <- -(1:2)
326    stopifnot(is(s2,"symmetricMatrix"), diag(s2) == c(0:-2,0))
327    t2 <- as(s2, "TsparseMatrix")
328    m <- as.mat(s2)
329    s2[2:1,2:1] <- 4:1
330    t2[2:1,2:1] <- 4:1
331     m[2:1,2:1] <- 4:1
332    assert.EQ.mat(t2, m)
333    assert.EQ.mat(s2, m)
334    ## and the same (for a different s2 !)
335    s2[2:1,2:1] <- 4:1
336    t2[2:1,2:1] <- 4:1
337    assert.EQ.mat(t2, m)# ok
338    assert.EQ.mat(s2, m)# failed in 0.9975-8
339    
340    
341    ## m[cbind(i,j)] <- value:
342    m.[ cbind(3:5, 1:3) ] <- 1:3
343    stopifnot(m.[3,1] == 1, m.[4,2] == 2)
344    x.x[ cbind(2:6, 2:6)] <- 12:16
345    validObject(x.x)
346    stopifnot(class(x.x) == "dsCMatrix",
347              12:16 == as.mat(x.x)[cbind(2:6, 2:6)])
348    (ne1 <- (mc - m.) != 0)
349    stopifnot(identical(ne1, 0 != abs(mc - m.)))
350    (ge <- m. >= mc) # contains "=" -> result is dense
351    ne. <- mc != m.  # was wrong (+ warning)
352    stopifnot(identical(!(m. < mc), m. >= mc),
353              identical(m. < mc, as(!ge, "sparseMatrix")),
354              identical(ne., Matrix:::drop0(ne1)))
355    
356    (M3 <- Matrix(upper.tri(matrix(, 3, 3)))) # ltC; indexing used to fail
357    T3 <- as(M3, "TsparseMatrix")
358    stopifnot(identical(drop(M3), M3),
359              identical4(drop(M3[,2, drop = FALSE]), M3[,2, drop = TRUE],
360                         drop(T3[,2, drop = FALSE]), T3[,2, drop = TRUE]),
361              is(T3, "triangularMatrix"),
362              !is(T3[,2, drop=FALSE], "triangularMatrix")
363              )
364    
365  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.1825

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