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 1724, Sat Jan 13 21:06:51 2007 UTC revision 2323, Wed Jan 21 20:50:12 2009 UTC
# Line 4  Line 4 
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 +0, nrow = 7)  m <- Matrix(1:28 +0, nrow = 7)
14  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
# Line 32  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 47  Line 53 
53  m. <- as.matrix(m)  m. <- as.matrix(m)
54    
55  ## m[ cbind(i,j) ] indexing:  ## m[ cbind(i,j) ] indexing:
56  ij <- cbind(1:6, 2:3)  iN <- ij <- cbind(1:6, 2:3)
57  stopifnot(identical(m[ij], m.[ij]))  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:  ## testing operations on logical Matrices rather more than indexing:
62  g10 <- m [ m > 10 ]  g10 <- m [ m > 10 ]
# Line 81  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    
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]),  stopifnot(all.equal(mC[,3], mm[,3]),
166            identical(mC[ij], mm[ij]))            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(dim(mC[numeric(0), ]) == c(0,20), # used to give warnings  stopifnot(dim(mC[numeric(0), ]) == c(0,20), # used to give warnings
# Line 117  Line 200 
200  lx.x <- as(x.x, "lsCMatrix") # FALSE only for "structural" 0  lx.x <- as(x.x, "lsCMatrix") # FALSE only for "structural" 0
201  (l10 <- lx.x[1:10, 1:10])# "lsC"  (l10 <- lx.x[1:10, 1:10])# "lsC"
202  (l3 <-  lx.x[1:3, ])  (l3 <-  lx.x[1:3, ])
203  m.x <- as(x.x, "matrix")  m.x <- as.mat(x.x) # as.mat() *drops* (NULL,NULL) dimnames
204  stopifnot(class(l10) == "lsCMatrix", # symmetric indexing -> symmetric !  stopifnot(class(l10) == "lsCMatrix", # symmetric indexing -> symmetric !
205            identical(as.mat(lx.x), m.x != 0),            identical(as.mat(lx.x), m.x != 0),
206            identical(as.logical(lx.x), as.logical(m.x)),            identical(as.logical(lx.x), as.logical(m.x)),
# Line 125  Line 208 
208            identical(as.mat(l3 ), m.x[1:3, ] != 0)            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  ## used to fail
225  n <- 5 ## or much larger  n <- 5 ## or much larger
226  sm <- new("dsTMatrix", i=as.integer(1),j=as.integer(1),  sm <- new("dsTMatrix", i=as.integer(1),j=as.integer(1),
# Line 138  Line 234 
234            all(sm[,-(1:3)] == 0)            all(sm[,-(1:3)] == 0)
235            )            )
236    
 ### Diagonal -- Sparse:  
237  m0 <- Diagonal(5)  m0 <- Diagonal(5)
238  (m1 <- as(m0, "sparseMatrix"))  # dtTMatrix  stopifnot(identical(m0[2,], m0[,2]),
239  (m2 <- as(m0, "CsparseMatrix")) # dtCMatrix (with an irrelevant warning)            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  M <- m0; M[1,] <- 0
257  stopifnot(identical(M, Diagonal(x=c(0, rep(1,4)))))  stopifnot(identical(M, Diagonal(x=c(0, rep(1,4)))))
258  M <- m0; M[,3] <- 3 ; M ; stopifnot(is(M, "sparseMatrix"), M[,3] == 3)  M <- m0; M[,3] <- 3 ; M ; stopifnot(is(M, "sparseMatrix"), M[,3] == 3)
259  validObject(M)  checkMatrix(M)
260  M <- m0; M[1:3, 3] <- 0 ;M  M <- m0; M[1:3, 3] <- 0 ;M
261  T <- m0; T[1:3, 3] <- 10  T <- m0; T[1:3, 3] <- 10
262  stopifnot(identical(M, Diagonal(x=c(1,1, 0, 1,1))),  stopifnot(identical(M, Diagonal(x=c(1,1, 0, 1,1))),
# Line 154  Line 264 
264    
265  M <- m1; M[1,] <- 0 ; M ; assert.EQ.mat(M, diag(c(0,rep(1,4))), tol=0)  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)  M <- m1; M[,3] <- 3 ; stopifnot(is(M,"sparseMatrix"), M[,3] == 3)
267  validObject(M)  checkMatrix(M)
268  M <- m1; M[1:3, 3] <- 0 ;M  M <- m1; M[1:3, 3] <- 0 ;M
269  assert.EQ.mat(M, diag(c(1,1, 0, 1,1)), tol=0)  assert.EQ.mat(M, diag(c(1,1, 0, 1,1)), tol=0)
270  T <- m1; T[1:3, 3] <- 10; validObject(T)  T <- m1; T[1:3, 3] <- 10; checkMatrix(T)
271  stopifnot(is(T, "dtTMatrix"), identical(T[,3], c(10,10,10,0,0)))  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)  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)  M <- m2; M[,3] <- 3 ; stopifnot(is(M,"sparseMatrix"), M[,3] == 3)
275  validObject(M)  checkMatrix(M)
276  M <- m2; M[1:3, 3] <- 0 ;M  M <- m2; M[1:3, 3] <- 0 ;M
277  assert.EQ.mat(M, diag(c(1,1, 0, 1,1)), tol=0)  assert.EQ.mat(M, diag(c(1,1, 0, 1,1)), tol=0)
278  T <- m2; T[1:3, 3] <- 10; validObject(T)  T <- m2; T[1:3, 3] <- 10; checkMatrix(T)
279  stopifnot(is(T, "dtCMatrix"), identical(T[,3], c(10,10,10,0,0)))  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]
322  mt <- mT[1:5, 1:7]  mt <- mT[1:5, 1:7]
# Line 215  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 250  Line 398 
398  mc # no longer has non-structural zeros  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  mc[4,] <- 0  mc[4,] <- 0
404  mc  mc
405    
406    S <- as(Diagonal(5),"TsparseMatrix")
407  H <- Hilbert(9)  H <- Hilbert(9)
408  Hc <- as(round(H, 3), "dsCMatrix")# a sparse matrix with no 0 ...  Hc <- as(round(H, 3), "dsCMatrix")# a sparse matrix with no 0 ...
409  (trH <- tril(Hc[1:5, 1:5]))  (trH <- tril(Hc[1:5, 1:5]))
410  stopifnot(is(trH, "triangularMatrix"), trH@uplo == "L")  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)  i <- c(1:2, 4, 6:7); j <- c(2:4,6)
439  H[i,j] <- 0  H[i,j] <- 0
# Line 270  Line 445 
445    
446  ## an example that failed for a long time  ## an example that failed for a long time
447  sy3 <- new("dsyMatrix", Dim = as.integer(c(2, 2)), x = c(14, -1, 2, -7))  sy3 <- new("dsyMatrix", Dim = as.integer(c(2, 2)), x = c(14, -1, 2, -7))
448  validObject(dm <- kronecker(Diagonal(2), sy3))  checkMatrix(dm <- kronecker(Diagonal(2), sy3))# now sparse with new kronecker
449    dm <- Matrix(as.matrix(dm))# -> "dsyMatrix"
450  (s2 <- as(dm, "sparseMatrix"))  (s2 <- as(dm, "sparseMatrix"))
451  validObject(st <- as(s2, "TsparseMatrix"))  checkMatrix(st <- as(s2, "TsparseMatrix"))
452  validObject(s.32  <- st[1:3,1:2]) ## 3 x 2 - and *not* dsTMatrix  stopifnot(is(s2, "symmetricMatrix"),
453  validObject(s2.32 <- s2[1:3,1:2])            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)  I <- c(1,4:3)
457  stopifnot(is(s2.32, "generalMatrix"),  stopifnot(is(s2.32, "generalMatrix"),
458            is(s.32,  "generalMatrix"),            is(s.32,  "generalMatrix"),
# Line 289  Line 467 
467    
468  ## now sub-assign  and check for consistency  ## now sub-assign  and check for consistency
469  ## symmetric subassign should keep symmetry  ## symmetric subassign should keep symmetry
470  st[I,I] <- 0; validObject(st); stopifnot(is(st,"symmetricMatrix"))  st[I,I] <- 0; checkMatrix(st); stopifnot(is(st,"symmetricMatrix"))
471  s2[I,I] <- 0; validObject(s2); stopifnot(is(s2,"symmetricMatrix"))  s2[I,I] <- 0; checkMatrix(s2); stopifnot(is(s2,"symmetricMatrix"))
472  ##  ##
473  m <- as.mat(st)  m <- as.mat(st)
474   m[2:1,2:1] <- 4:1   m[2:1,2:1] <- 4:1
# Line 322  Line 500 
500  m.[ cbind(3:5, 1:3) ] <- 1:3  m.[ cbind(3:5, 1:3) ] <- 1:3
501  stopifnot(m.[3,1] == 1, m.[4,2] == 2)  stopifnot(m.[3,1] == 1, m.[4,2] == 2)
502  x.x[ cbind(2:6, 2:6)] <- 12:16  x.x[ cbind(2:6, 2:6)] <- 12:16
503  validObject(x.x)  stopifnot(isValid(x.x, "dsCMatrix"),
 stopifnot(class(x.x) == "dsCMatrix",  
504            12:16 == as.mat(x.x)[cbind(2:6, 2:6)])            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.1724  
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