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 1315, Mon Jul 10 16:08:05 2006 UTC revision 2323, Wed Jan 21 20:50:12 2009 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    
5  source(system.file("test-tools.R", package = "Matrix"))# identical3() etc  source(system.file("test-tools.R", package = "Matrix"))# identical3() etc
6    
7    if(interactive()) {
8        options(error = recover, warn = 1)
9    } else options(verbose = TRUE, warn = 1)
10    
11  ### Dense Matrices  ### Dense Matrices
12    
13  m <- Matrix(1:28, nrow = 7)  m <- Matrix(1:28 +0, nrow = 7)
14  validObject(m) ; m@x <- as.double(m@x) ; validObject(m)  validObject(m)
15  stopifnot(identical(m, m[]),  stopifnot(identical(m, m[]),
16            identical(m[2, 3],  16), # simple number            identical(m[2, 3],  16), # simple number
17            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
18              identical(m[NA,NA], as(Matrix(NA, 7,4), "dMatrix")))
19    
20  m[2, 3:4, drop=FALSE] # sub matrix of class 'dgeMatrix'  m[2, 3:4, drop=FALSE] # sub matrix of class 'dgeMatrix'
21  m[-(4:7), 3:4]        # dito; the upper right corner of 'm'  m[-(4:7), 3:4]        # ditto; the upper right corner of 'm'
22    
23  ## rows or columns only:  ## rows or columns only:
24  m[1,]     # first row, as simple numeric vector  m[1,]     # first row, as simple numeric vector
25  m[,2]     # 2nd column  m[,2]     # 2nd column
26  m[,1:2]   # sub matrix of first two columns  m[,1:2]   # sub matrix of first two columns
27  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
28    m[integer(0),] #-> 0 x 4 Matrix
29    m[2:4, numeric(0)] #-> 3 x 0 Matrix
30    
31  ## logical indexing  ## logical indexing
32  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 30  Line 37 
37  mn <- m  mn <- m
38  dimnames(mn) <- list(paste("r",letters[1:nrow(mn)],sep=""),  dimnames(mn) <- list(paste("r",letters[1:nrow(mn)],sep=""),
39                       LETTERS[1:ncol(mn)])                       LETTERS[1:ncol(mn)])
40    checkMatrix(mn)
41  mn["rd", "D"]  mn["rd", "D"]
42  stopifnot(identical(mn["rc", "D"], mn[3,4]), mn[3,4] == 24,  stopifnot(identical(mn["rc", "D"], mn[3,4]), mn[3,4] == 24,
43            identical(mn[, "A"], mn[,1]), mn[,1] == 1:7,            identical(mn[, "A"], mn[,1]), mn[,1] == 1:7,
# Line 42  Line 50 
50  m[, 1] <- -1  m[, 1] <- -1
51  m[1:3,]  m[1:3,]
52    
53    m. <- as.matrix(m)
54    
55    ## m[ cbind(i,j) ] indexing:
56    iN <- ij <- cbind(1:6, 2:3)
57    iN[2:3,] <- iN[5,2] <- NA
58    stopifnot(identical(m[ij], m.[ij]),
59              identical(m[iN], m.[iN]))
60    
61    ## testing operations on logical Matrices rather more than indexing:
62  g10 <- m [ m > 10 ]  g10 <- m [ m > 10 ]
63  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()  
64      stopifnot(10 == length(m[ m <= 10 ]))      stopifnot(10 == length(m[ m <= 10 ]))
65    sel <- (20 <  m) & (m <  150)
66    sel.<- (20 <  m.)& (m.<  150)
67    nsel <-(20 >= m) | (m >= 150)
68    (ssel <- as(sel, "sparseMatrix"))
69    stopifnot(is(sel, "lMatrix"), is(ssel, "lsparseMatrix"),
70              identical3(as.mat(sel.), as.mat(sel), as.mat(ssel)),
71              identical3(!sel, !ssel, nsel), # !<sparse> is typically dense
72              identical3(m[ sel],  m[ ssel], as.matrix(m)[as.matrix( ssel)]),
73              identical3(m[!sel],  m[!ssel], as.matrix(m)[as.matrix(!ssel)])
74              )
75    
76    ## more sparse Matrices --------------------------------------
 ### Sparse Matrices --------------------------------------  
77    
78  m <- 1:800  m <- 1:800
79  set.seed(101) ; m[sample(800, 600)] <- 0  set.seed(101) ; m[sample(800, 600)] <- 0
# Line 65  Line 89 
89  mC[1:2,]  mC[1:2,]
90  mC[7,  drop = FALSE]  mC[7,  drop = FALSE]
91  assert.EQ.mat(mC[1:2,], mm[1:2,])  assert.EQ.mat(mC[1:2,], mm[1:2,])
92  stopifnot(all.equal(mC[,3],   mm[,3]))  
93    ## *repeated* (aka 'duplicated') indices - did not work at all ...
94    i <- rep(8:10,2)
95    j <- c(2:4, 4:3)
96    assert.EQ.mat(mC[i,], mm[i,])
97    assert.EQ.mat(mC[,j], mm[,j])
98    ## FIXME? assert.EQ.mat(mC[,NA], mm[,NA]) -- mC[,NA] is all 0 "instead" of all NA
99    ## MM currently thinks we should  NOT  allow  <sparse>[ <NA> ]
100    assert.EQ.mat(mC[i, 2:1], mm[i, 2:1])
101    assert.EQ.mat(mC[c(4,1,2:1), j], mm[c(4,1,2:1), j])
102    assert.EQ.mat(mC[i,j], mm[i,j])
103    set.seed(7)
104    for(n in 1:50) {
105        i <- sample(sample(nrow(mC), 7), 20, replace = TRUE)
106        j <- sample(sample(ncol(mC), 6), 17, replace = TRUE)
107        assert.EQ.mat(mC[i,j], mm[i,j])
108    }
109    
110    ##---- Symmetric indexing of symmetric Matrix ----------
111    m. <- mC; m.[, c(2, 7:12)] <- 0
112    validObject(S <- crossprod(add.simpleDimnames(m.) %% 100))
113    ss <- as(S, "matrix")
114    ds <- as(S, "denseMatrix")
115    ## NA-indexing of *dense* Matrices: should work as traditionally
116    assert.EQ.mat(ds[NA,NA], ss[NA,NA])
117    assert.EQ.mat(ds[NA,  ], ss[NA,])
118    assert.EQ.mat(ds[  ,NA], ss[,NA])
119    stopifnot(identical(ds[2 ,NA], ss[2,NA]),
120              identical(ds[NA, 1], ss[NA, 1]))
121    T <- as(S, "TsparseMatrix")
122    ## non-repeated indices:
123    i <- c(7:5, 2:4);assert.EQ.mat(T[i,i], ss[i,i])
124    ## NA in indices  -- check that we get a helpful error message:
125    i[2] <- NA
126    er <- tryCatch(T[i,i], error = function(e)e)
127    stopifnot(as.logical(grep("indices.*sparse Matrices", er$message)))
128    
129    N <- nrow(T)
130    set.seed(11)
131    for(n in 1:50) {
132        i <- sample(N, max(2, sample(N,1)), replace = FALSE)
133        validObject(Tii <- T[i,i])
134        stopifnot(is(Tii, "dsTMatrix"), # remained symmetric Tsparse
135                  identical(t(Tii), t(T)[i,i]))
136        assert.EQ.mat(Tii, ss[i,i])
137    }
138    
139    ## repeated ones ``the challenge'' (to do smartly):
140    j <- c(4, 4, 9, 12, 9, 4, 17, 3, 18, 4, 12, 18, 4, 9)
141    assert.EQ.mat(T[j,j], ss[j,j])
142    ## and another two sets  (a, A) &  (a., A.) :
143    a <- matrix(0, 6,6)
144    a[upper.tri(a)] <- (utr <- c(2, 0,-1, 0,0,5, 7,0,0,0, 0,0,-2,0,8))
145    ta <- t(a); ta[upper.tri(a)] <- utr; a <- t(ta)
146    diag(a) <- c(0,3,0,4,6,0)
147    A <- as(Matrix(a), "TsparseMatrix")
148    A. <- A
149    diag(A.) <- 10 * (1:6)
150    a. <- as(A., "matrix")
151    ## More testing {this was not working for a long time..}
152    set.seed(1)
153    for(n in 1:100) {
154        i <- sample(1:nrow(A), 3+2*rpois(1, lam=3), replace=TRUE)
155        Aii  <- A[i,i]
156        A.ii <- A.[i,i]
157        stopifnot(class(Aii) == class(A),
158                  class(A.ii) == class(A.))
159        assert.EQ.mat(Aii , a [i,i])
160        assert.EQ.mat(A.ii, a.[i,i])
161        assert.EQ.mat(T[i,i], ss[i,i])
162    }
163    
164    
165    stopifnot(all.equal(mC[,3], mm[,3]),
166              identical(mC[ij], mm[ij]),
167              identical(mC[iN], mm[iN]))
168    
169  assert.EQ.mat(mC[7, , drop=FALSE], mm[7, , drop=FALSE])  assert.EQ.mat(mC[7, , drop=FALSE], mm[7, , drop=FALSE])
170    
171  stopifnot(identical(mC[7,  drop = FALSE],  stopifnot(dim(mC[numeric(0), ]) == c(0,20), # used to give warnings
172              dim(mC[, integer(0)]) == c(40,0),
173              identical(mC[, integer(0)], mC[, FALSE]),
174              identical(mC[7,  drop = FALSE],
175                      mC[7,, drop = FALSE]))                      mC[7,, drop = FALSE]))
176  validObject(print(mT[,c(2,4)]))  validObject(print(mT[,c(2,4)]))
177  stopifnot(all.equal(mT[2,], mm[2,]),  stopifnot(all.equal(mT[2,], mm[2,]),
# Line 82  Line 185 
185            identical3(mm[3,], mC[3,], mT[3,]),            identical3(mm[3,], mC[3,], mT[3,]),
186            identical3(mT[2,3], mC[2,3], 0),            identical3(mT[2,3], mC[2,3], 0),
187            identical(mT[], mT),            identical(mT[], mT),
188            ## 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]),
189            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]))
190                       as(mT[c(3,7), 2:4],"matrix")))            )
191    
192    x.x <- crossprod(mC)
193    stopifnot(class(x.x) == "dsCMatrix",
194              class(x.x. <- round(x.x / 10000)) == "dsCMatrix",
195              identical(x.x[cbind(2:6, 2:6)],
196                        diag(x.x [2:6, 2:6])))
197    head(x.x.) # Note the *non*-structural 0's printed as "0"
198    tail(x.x., -3) # all but the first three lines
199    
200    lx.x <- as(x.x, "lsCMatrix") # FALSE only for "structural" 0
201    (l10 <- lx.x[1:10, 1:10])# "lsC"
202    (l3 <-  lx.x[1:3, ])
203    m.x <- as.mat(x.x) # as.mat() *drops* (NULL,NULL) dimnames
204    stopifnot(class(l10) == "lsCMatrix", # symmetric indexing -> symmetric !
205              identical(as.mat(lx.x), m.x != 0),
206              identical(as.logical(lx.x), as.logical(m.x)),
207              identical(as.mat(l10), m.x[1:10, 1:10] != 0),
208              identical(as.mat(l3 ), m.x[1:3, ] != 0)
209              )
210    
211    ##-- Sub*assignment* with repeated / duplicated index:
212    A <- Matrix(0,4,3) ; A[c(1,2,1), 2] <- 1 ; A
213    B <- A;              B[c(1,2,1), 2] <- 1:3; B; B. <- B
214    B.[3,] <- rbind(4:2)
215    diag(B.) <- 10 * diag(B.)
216    C <- B.; C[,2] <- C[,2];  C[1,] <- C[1,]; C[2:3,2:1] <- C[2:3,2:1]
217    stopifnot(identical(unname(as.matrix(A)),
218                        local({a <- matrix(0,4,3); a[c(1,2,1), 2] <-  1 ; a})),
219              identical(unname(as.matrix(B)),
220                        local({a <- matrix(0,4,3); a[c(1,2,1), 2] <- 1:3; a})),
221              identical(C, drop0(B.)))
222    
223    
224    ## used to fail
225    n <- 5 ## or much larger
226    sm <- new("dsTMatrix", i=as.integer(1),j=as.integer(1),
227              Dim=as.integer(c(n,n)), x = 1)
228    (cm <- as(sm, "CsparseMatrix"))
229    sm[2,]
230    stopifnot(sm[2,] == c(0:1, rep.int(0,ncol(sm)-2)),
231              sm[2,] == cm[2,],
232              sm[,3] == sm[3,],
233              all(sm[,-(1:3)] == t(sm[-(1:3),])), # all(<lge.>)
234              all(sm[,-(1:3)] == 0)
235              )
236    
237    m0 <- Diagonal(5)
238    stopifnot(identical(m0[2,], m0[,2]),
239              identical(m0[,1], c(1,0,0,0,0)))
240    ### Diagonal -- Sparse:
241    (m1 <- as(m0, "TsparseMatrix"))  # dtTMatrix
242    (m2 <- as(m0, "CsparseMatrix")) # dtCMatrix
243    m1g <- as(m1, "generalMatrix")
244    stopifnot(is(m1g, "dgTMatrix"))
245    assert.EQ.mat(m2[1:3,],    diag(5)[1:3,])
246    assert.EQ.mat(m2[,c(4,1)], diag(5)[,c(4,1)])
247    stopifnot(identical(m2[1:3,], as(m1[1:3,], "CsparseMatrix")),
248              identical(Matrix:::uniqTsparse(m1[, c(4,2)]),
249                        Matrix:::uniqTsparse(as(m2[, c(4,2)], "TsparseMatrix")))
250              )## failed in 0.9975-11
251    
252    (uTr <- new("dtTMatrix", Dim = c(3L,3L), diag="U"))
253    uTr[1,] <- 0
254    assert.EQ.mat(uTr, cbind(0, rbind(0,diag(2))))
255    
256    M <- m0; M[1,] <- 0
257    stopifnot(identical(M, Diagonal(x=c(0, rep(1,4)))))
258    M <- m0; M[,3] <- 3 ; M ; stopifnot(is(M, "sparseMatrix"), M[,3] == 3)
259    checkMatrix(M)
260    M <- m0; M[1:3, 3] <- 0 ;M
261    T <- m0; T[1:3, 3] <- 10
262    stopifnot(identical(M, Diagonal(x=c(1,1, 0, 1,1))),
263              is(T, "triangularMatrix"), identical(T[,3], c(10,10,10,0,0)))
264    
265    M <- m1; M[1,] <- 0 ; M ; assert.EQ.mat(M, diag(c(0,rep(1,4))), tol=0)
266    M <- m1; M[,3] <- 3 ; stopifnot(is(M,"sparseMatrix"), M[,3] == 3)
267    checkMatrix(M)
268    M <- m1; M[1:3, 3] <- 0 ;M
269    assert.EQ.mat(M, diag(c(1,1, 0, 1,1)), tol=0)
270    T <- m1; T[1:3, 3] <- 10; checkMatrix(T)
271    stopifnot(is(T, "dtTMatrix"), identical(T[,3], c(10,10,10,0,0)))
272    
273    M <- m2; M[1,] <- 0 ; M ; assert.EQ.mat(M, diag(c(0,rep(1,4))), tol=0)
274    M <- m2; M[,3] <- 3 ; stopifnot(is(M,"sparseMatrix"), M[,3] == 3)
275    checkMatrix(M)
276    M <- m2; M[1:3, 3] <- 0 ;M
277    assert.EQ.mat(M, diag(c(1,1, 0, 1,1)), tol=0)
278    T <- m2; T[1:3, 3] <- 10; checkMatrix(T)
279    stopifnot(is(T, "dtCMatrix"), identical(T[,3], c(10,10,10,0,0)))
280    
281    
282    ## "Vector indices" -------------------
283    D <- Diagonal(6)
284    M <- as(D,"dgeMatrix")
285    m <- as(D,"matrix")
286    s <- as(D,"TsparseMatrix")
287    S <- as(s,"CsparseMatrix")
288    i <- c(3,1,6); v <- c(10,15,20)
289    ## (logical,value) which both are recycled:
290    L <- c(TRUE, rep(FALSE,8)) ; z <- c(50,99)
291    
292    ## vector subassignment, both with integer & logical
293    ## these now work correctly {though not very efficiently; hence warnings}
294    m[i] <- v # the role model: only first column is affected
295    M[i] <- v; assert.EQ.mat(M,m) # dge
296    D[i] <- v; assert.EQ.mat(D,m) # ddi -> dtT -> dgT
297    s[i] <- v; assert.EQ.mat(s,m) # dtT -> dgT
298    S[i] <- v; assert.EQ.mat(S,m); S # dtC -> dtT -> dgT -> dgC
299    ## logical
300    m[L] <- z
301    M[L] <- z; assert.EQ.mat(M,m)
302    D[L] <- z; assert.EQ.mat(D,m)
303    s[L] <- z; assert.EQ.mat(s,m)
304    S[L] <- z; assert.EQ.mat(S,m) ; S
305    
306    ## indexing [i]  vs  [i,] --- now ok
307    stopifnot(identical4(m[i], M[i], D[i], s[i]), identical(s[i],S[i]))
308    stopifnot(identical4(m[L], M[L], D[L], s[L]), identical(s[L],S[L]))
309    assert.EQ.mat(D[i,], m[i,])
310    assert.EQ.mat(M[i,], m[i,])
311    assert.EQ.mat(s[i,], m[i,])
312    assert.EQ.mat(S[i,], m[i,])
313    
314    assert.EQ.mat(D[,i], m[,i])
315    assert.EQ.mat(M[,i], m[,i])
316    assert.EQ.mat(s[,i], m[,i])
317    assert.EQ.mat(S[,i], m[,i])
318    
319    
320  ## --- negative indices ----------  ## --- negative indices ----------
321  mc <- mC[1:5, 1:7]  mc <- mC[1:5, 1:7]
# Line 132  Line 363 
363    
364  mt[c(2,4), ] <- 0; stopifnot(as(mt[c(2,4), ],"matrix") == 0)  mt[c(2,4), ] <- 0; stopifnot(as(mt[c(2,4), ],"matrix") == 0)
365  mt[2:3, 4:7] <- 33  mt[2:3, 4:7] <- 33
366  validObject(mt)  checkMatrix(mt)
367  mt  mt
368    
369  mc[1,4] <- -99 ; stopifnot(mc[1,4] == -99)  mc[1,4] <- -99 ; stopifnot(mc[1,4] == -99)
# Line 140  Line 371 
371  mc[1,4] <- -99 ; stopifnot(mc[1,4] == -99)  mc[1,4] <- -99 ; stopifnot(mc[1,4] == -99)
372  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)
373    
 ## Debugging:  R bug --   debug(Matrix:::replCmat)  has no effect  
   
374  mc[-1, 3] <- -2:1 # 0 should not be entered; 'value' recycled  mc[-1, 3] <- -2:1 # 0 should not be entered; 'value' recycled
375  mt[-1, 3] <- -2:1  mt[-1, 3] <- -2:1
376  stopifnot(mc@x != 0, mt@x != 0,  stopifnot(mc@x != 0, mt@x != 0,
377            mc[-1,3] == -2:1, mt[-1,3] == -2:1) ##--> BUG -- fixed            mc[-1,3] == -2:1, mt[-1,3] == -2:1) ## failed earlier
378  ## source("~/R/Pkgs/Matrix/R/Tsparse.R")  
379  ## Matrix_expand_pointers <- Matrix:::Matrix_expand_pointers  mc0 <- mc
380  ## -> open ../R/dgCMatrix.R  --> replCmat  .. now eval-line by line ..  mt0 <- as(mc0, "TsparseMatrix")
381    m0  <- as(mc0, "matrix")
382  ev <- 1:5 %% 2 == 0  set.seed(1)
383  mc[ev, 3] <- 0:1  for(i in 1:50) {
384  ##FIXME stopifnot(mc[ev, 3] == 0:1) ##-> BUG  {very peculiar; the 2nd time it works ...}      mc <- mc0; mt <- mt0 ; m <- m0
385  validObject(mc)      ev <- 1:5 %% 2 == round(runif(1))# 0 or 1
386  mc # now shows a non-structural zeros      j <- sample(ncol(mc), 1 + round(runif(1)))
387        nv <- rpois(sum(ev) * length(j), lambda = 1)
388        mc[ev, j] <- nv
389         m[ev, j] <- nv
390        mt[ev, j] <- nv
391        if(i %% 10 == 1) print(mc[ev,j, drop = FALSE])
392        stopifnot(as.vector(mc[ev, j]) == nv, ## failed earlier...
393                  as.vector(mt[ev, j]) == nv)
394        validObject(mc) ; assert.EQ.mat(mc, m)
395        validObject(mt) ; assert.EQ.mat(mt, m)
396    }
397    
398    mc # no longer has non-structural zeros
399  mc[ii, jj] <- 1:6  mc[ii, jj] <- 1:6
400  mc[c(2,5), c(3,5)] <- 3.2  mc[c(2,5), c(3,5)] <- 3.2
401  validObject(mc)  checkMatrix(mc)
402  (m. <- mc)  m. <- mc
403  ## FIXME: mc[4,] <- 0 # -> error -- another Bug  mc[4,] <- 0
404    mc
405    
406    S <- as(Diagonal(5),"TsparseMatrix")
407    H <- Hilbert(9)
408    Hc <- as(round(H, 3), "dsCMatrix")# a sparse matrix with no 0 ...
409    (trH <- tril(Hc[1:5, 1:5]))
410    stopifnot(is(trH, "triangularMatrix"), trH@uplo == "L",
411              is(S, "triangularMatrix"))
412    
413    ## triangular assignment
414    ## the slick (but inefficient in case of sparse!) way to assign sub-diagonals:
415    ## equivalent to tmp <- `diag<-`(S[,-1], -2:1); S[,-1] <- tmp
416    ## which dispatches to (x="TsparseMatrix", i="missing",j="index", value="replValue")
417    diag(S[,-1]) <- -2:1 # used to give a wrong warning
418    S <- as(S,"triangularMatrix")
419    assert.EQ.mat(S, local({s <- diag(5); diag(s[,-1]) <- -2:1; s}))
420    
421    trH[c(1:2,4), c(2:3,5)] <- 0 # gave an *error* upto Jan.2008
422    trH[ lower.tri(trH) ] <- 0   # ditto, because of callNextMethod()
423    
424    m <- Matrix(0+1:28, nrow = 4)
425    m[-3,c(2,4:5,7)] <- m[ 3, 1:4] <- m[1:3, 6] <- 0
426    mT <- as(m, "dgTMatrix")
427    stopifnot(identical(mT[lower.tri(mT)],
428                        m [lower.tri(m) ]))
429    lM <- upper.tri(mT, diag=TRUE)
430    mT[lM] <- 0
431     m[lM] <- 0
432    assert.EQ.mat(mT, as(m,"matrix"))
433    mT[lM] <- -1:0
434     m[lM] <- -1:0
435    assert.EQ.mat(mT, as(m,"matrix"))
436    (mT <- drop0(mT))
437    
438    i <- c(1:2, 4, 6:7); j <- c(2:4,6)
439    H[i,j] <- 0
440    (H. <- round(as(H, "sparseMatrix"), 3)[ , 2:7])
441    Hc. <- Hc
442    Hc.[i,j] <- 0 ## now "works", but setting "non-structural" 0s
443    stopifnot(as.matrix(Hc.[i,j]) == 0)
444    Hc.[, 1:6]
445    
446    ## an example that failed for a long time
447    sy3 <- new("dsyMatrix", Dim = as.integer(c(2, 2)), x = c(14, -1, 2, -7))
448    checkMatrix(dm <- kronecker(Diagonal(2), sy3))# now sparse with new kronecker
449    dm <- Matrix(as.matrix(dm))# -> "dsyMatrix"
450    (s2 <- as(dm, "sparseMatrix"))
451    checkMatrix(st <- as(s2, "TsparseMatrix"))
452    stopifnot(is(s2, "symmetricMatrix"),
453              is(st, "symmetricMatrix"))
454    checkMatrix(s.32  <- st[1:3,1:2]) ## 3 x 2 - and *not* dsTMatrix
455    checkMatrix(s2.32 <- s2[1:3,1:2])
456    I <- c(1,4:3)
457    stopifnot(is(s2.32, "generalMatrix"),
458              is(s.32,  "generalMatrix"),
459              identical(as.mat(s.32), as.mat(s2.32)),
460              identical3(dm[1:3,-1], asD(s2[1:3,-1]), asD(st[1:3,-1])),
461              identical4(2, dm[4,3], s2[4,3], st[4,3]),
462              identical3(diag(dm), diag(s2), diag(st)),
463              is((cI <- s2[I,I]), "dsCMatrix"),
464              is((tI <- st[I,I]), "dsTMatrix"),
465              identical4(as.mat(dm)[I,I], as.mat(dm[I,I]), as.mat(tI), as.mat(cI))
466              )
467    
468    ## now sub-assign  and check for consistency
469    ## symmetric subassign should keep symmetry
470    st[I,I] <- 0; checkMatrix(st); stopifnot(is(st,"symmetricMatrix"))
471    s2[I,I] <- 0; checkMatrix(s2); stopifnot(is(s2,"symmetricMatrix"))
472    ##
473    m <- as.mat(st)
474     m[2:1,2:1] <- 4:1
475    st[2:1,2:1] <- 4:1
476    s2[2:1,2:1] <- 4:1
477    stopifnot(identical(m, as.mat(st)),
478              1:4 == as.vector(s2[1:2,1:2]),
479              identical(m, as.mat(s2)))
480    
481    ## now a slightly different situation for 's2' (had bug)
482    s2 <- as(dm, "sparseMatrix")
483    s2[I,I] <- 0; diag(s2)[2:3] <- -(1:2)
484    stopifnot(is(s2,"symmetricMatrix"), diag(s2) == c(0:-2,0))
485    t2 <- as(s2, "TsparseMatrix")
486    m <- as.mat(s2)
487    s2[2:1,2:1] <- 4:1
488    t2[2:1,2:1] <- 4:1
489     m[2:1,2:1] <- 4:1
490    assert.EQ.mat(t2, m)
491    assert.EQ.mat(s2, m)
492    ## and the same (for a different s2 !)
493    s2[2:1,2:1] <- 4:1
494    t2[2:1,2:1] <- 4:1
495    assert.EQ.mat(t2, m)# ok
496    assert.EQ.mat(s2, m)# failed in 0.9975-8
497    
498    
499    ## m[cbind(i,j)] <- value:
500    m.[ cbind(3:5, 1:3) ] <- 1:3
501    stopifnot(m.[3,1] == 1, m.[4,2] == 2)
502    x.x[ cbind(2:6, 2:6)] <- 12:16
503    stopifnot(isValid(x.x, "dsCMatrix"),
504              12:16 == as.mat(x.x)[cbind(2:6, 2:6)])
505    (ne1 <- (mc - m.) != 0)
506    stopifnot(identical(ne1, 0 != abs(mc - m.)))
507    (ge <- m. >= mc) # contains "=" -> result is dense
508    ne. <- mc != m.  # was wrong (+ warning)
509    stopifnot(identical(!(m. < mc), m. >= mc),
510              identical(m. < mc, as(!ge, "sparseMatrix")),
511              identical(ne., drop0(ne1)))
512    
513    d6 <- Diagonal(6)
514    ii <- c(1:2, 4:5)
515    d6[cbind(ii,ii)] <- 7*ii
516    stopifnot(is(d6, "ddiMatrix"), identical(d6, Diagonal(x=c(7*1:2,1,7*4:5,1))))
517    
518    for(j in 3:6) { ## even and odd j used to behave differently
519        M <- Matrix(0, j,j); m <- matrix(0, j,j)
520        T  <- as(M, "TsparseMatrix")
521        TG <- as(T, "generalMatrix")
522        G <-  as(M, "generalMatrix")
523        id <- cbind(1:j,1:j)
524        i2 <- cbind(1:j,j:1)
525        m[id] <- 1:j
526        M[id] <- 1:j ; stopifnot(is(M,"symmetricMatrix"))
527        T[id] <- 1:j ; stopifnot(is(T,"symmetricMatrix"))
528        G[id] <- 1:j
529        TG[id]<- 1:j
530        m[i2] <- 10
531        M[i2] <- 10 ; stopifnot(is(M,"symmetricMatrix"))
532        T[i2] <- 10 ; stopifnot(is(T,"symmetricMatrix"))
533        G[i2] <- 10
534        TG[i2]<- 10
535        ##
536        assert.EQ.mat(M, m)
537        assert.EQ.mat(T, m)
538        assert.EQ.mat(G, m)
539        assert.EQ.mat(TG,m)
540    }
541    
542    
543    ## drop, triangular, ...
544    (M3 <- Matrix(upper.tri(matrix(, 3, 3)))) # ltC; indexing used to fail
545    T3 <- as(M3, "TsparseMatrix")
546    stopifnot(identical(drop(M3), M3),
547              identical4(drop(M3[,2, drop = FALSE]), M3[,2, drop = TRUE],
548                         drop(T3[,2, drop = FALSE]), T3[,2, drop = TRUE]),
549              is(T3, "triangularMatrix"),
550              !is(T3[,2, drop=FALSE], "triangularMatrix")
551              )
552    
553    (T6 <- as(as(kronecker(Matrix(c(0,0,1,0),2,2), t(T3)), "lMatrix"),
554              "triangularMatrix"))
555    T6[1:4, -(1:3)] # failed (trying to coerce back to ltTMatrix)
556    stopifnot(identical(T6[1:4, -(1:3)][2:3, -3],
557                        spMatrix(2,2, i=c(1,2,2), j=c(1,1,2), x=rep(TRUE,3))))
558    
559    M <- Diagonal(4); M[1,2] <- 2
560    M. <- as(M, "CsparseMatrix")
561    (R <- as(M., "RsparseMatrix"))
562    (Ms <- symmpart(M.))
563    Rs <- as(Ms, "RsparseMatrix")
564    stopifnot(isValid(M, "triangularMatrix"),
565              isValid(M.,"triangularMatrix"),
566              isValid(Ms, "dsCMatrix"),
567              isValid(R,  "dtRMatrix"),
568              isValid(Rs, "dsRMatrix") )
569    stopifnot(dim(M[2:3, FALSE]) == c(2,0),
570              dim(R[2:3, FALSE]) == c(2,0),
571              identical(M [2:3,TRUE], M [2:3,]),
572              identical(M.[2:3,TRUE], M.[2:3,]),
573              identical(R [2:3,TRUE], R [2:3,]),
574              dim(R[FALSE, FALSE]) == c(0,0))
575    
576    n <- 50000L
577    Lrg <- new("dgTMatrix", Dim = c(n,n))
578    diag(Lrg) <- 1:n
579    dLrg <- as(Lrg, "diagonalMatrix")
580    stopifnot(identical(Diagonal(x = 1:n), dLrg))
581    diag(dLrg) <- 1 + diag(dLrg)
582    Clrg <- as(Lrg,"CsparseMatrix")
583    Ctrg <- as(Clrg, "triangularMatrix")
584    diag(Ctrg) <- 1 + diag(Ctrg)
585    stopifnot(identical(Diagonal(x = 1+ 1:n), dLrg),
586              identical(Ctrg, as(dLrg,"CsparseMatrix")))
587    
588    cc <- capture.output(show(dLrg))# show(<diag>) used to error for large n
589    
590    cat('Time elapsed: ', (.pt <- proc.time()),'\n') # "stats"
591    ##
592    cat("checkMatrix() of all: \n---------\n")
593    Sys.setlocale("LC_COLLATE", "C")# to keep ls() reproducible
594    for(nm in ls()) if(is(.m <- get(nm), "Matrix")) {
595        cat(nm, "\n")
596        checkMatrix(.m, verbose = FALSE)
597    }
598    cat('Time elapsed: ', proc.time() - .pt,'\n') # "stats"
599    
600    if(!interactive()) warnings()
601    
 cat('Time elapsed: ', proc.time(),'\n') # for ``statistical reasons''  

Legend:
Removed from v.1315  
changed lines
  Added in v.2323

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