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 1835, Fri May 11 10:41:21 2007 UTC revision 2120, Tue Mar 4 21:44:41 2008 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    options(verbose = TRUE)# to show message()s
8    
9  ### Dense Matrices  ### Dense Matrices
10    
11  m <- Matrix(1:28 +0, nrow = 7)  m <- Matrix(1:28 +0, nrow = 7)
# Line 13  Line 15 
15            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
16    
17  m[2, 3:4, drop=FALSE] # sub matrix of class 'dgeMatrix'  m[2, 3:4, drop=FALSE] # sub matrix of class 'dgeMatrix'
18  m[-(4:7), 3:4]        # dito; the upper right corner of 'm'  m[-(4:7), 3:4]        # ditto; the upper right corner of 'm'
19    
20  ## rows or columns only:  ## rows or columns only:
21  m[1,]     # first row, as simple numeric vector  m[1,]     # first row, as simple numeric vector
# Line 176  Line 178 
178  lx.x <- as(x.x, "lsCMatrix") # FALSE only for "structural" 0  lx.x <- as(x.x, "lsCMatrix") # FALSE only for "structural" 0
179  (l10 <- lx.x[1:10, 1:10])# "lsC"  (l10 <- lx.x[1:10, 1:10])# "lsC"
180  (l3 <-  lx.x[1:3, ])  (l3 <-  lx.x[1:3, ])
181  m.x <- as(x.x, "matrix")  m.x <- as.mat(x.x) # as.mat() *drops* (NULL,NULL) dimnames
182  stopifnot(class(l10) == "lsCMatrix", # symmetric indexing -> symmetric !  stopifnot(class(l10) == "lsCMatrix", # symmetric indexing -> symmetric !
183            identical(as.mat(lx.x), m.x != 0),            identical(as.mat(lx.x), m.x != 0),
184            identical(as.logical(lx.x), as.logical(m.x)),            identical(as.logical(lx.x), as.logical(m.x)),
# Line 186  Line 188 
188    
189  ##-- Sub*assignment* with repeated / duplicated index:  ##-- Sub*assignment* with repeated / duplicated index:
190  A <- Matrix(0,4,3) ; A[c(1,2,1), 2] <- 1 ; A  A <- Matrix(0,4,3) ; A[c(1,2,1), 2] <- 1 ; A
191  B <- A;              B[c(1,2,1), 2] <- 1:3; B  B <- A;              B[c(1,2,1), 2] <- 1:3; B; B. <- B
192    B.[3,] <- rbind(4:2)
193    diag(B.) <- 10 * diag(B.)
194    C <- B.; C[,2] <- C[,2];  C[1,] <- C[1,]; C[2:3,2:1] <- C[2:3,2:1]
195  stopifnot(identical(unname(as.matrix(A)),  stopifnot(identical(unname(as.matrix(A)),
196                      local({a <- matrix(0,4,3); a[c(1,2,1), 2] <-  1 ; a})),                      local({a <- matrix(0,4,3); a[c(1,2,1), 2] <-  1 ; a})),
197            identical(unname(as.matrix(B)),            identical(unname(as.matrix(B)),
198                      local({a <- matrix(0,4,3); a[c(1,2,1), 2] <- 1:3; a})))                      local({a <- matrix(0,4,3); a[c(1,2,1), 2] <- 1:3; a})),
199              identical(C, drop0(B.)))
200    
201    
202  ## used to fail  ## used to fail
# Line 206  Line 212 
212            all(sm[,-(1:3)] == 0)            all(sm[,-(1:3)] == 0)
213            )            )
214    
 ### Diagonal -- Sparse:  
215  m0 <- Diagonal(5)  m0 <- Diagonal(5)
216    stopifnot(identical(m0[2,], m0[,2]),
217              identical(m0[,1], c(1,0,0,0,0)))
218    ### Diagonal -- Sparse:
219  (m1 <- as(m0, "sparseMatrix"))  # dtTMatrix  (m1 <- as(m0, "sparseMatrix"))  # dtTMatrix
220  (m2 <- as(m0, "CsparseMatrix")) # dtCMatrix (with an irrelevant warning)  (m2 <- as(m0, "CsparseMatrix")) # dtCMatrix (with an irrelevant warning)
221  m1g <- as(m1, "generalMatrix")  m1g <- as(m1, "generalMatrix")
# Line 219  Line 227 
227                      Matrix:::uniqTsparse(as(m2[, c(4,2)], "TsparseMatrix")))                      Matrix:::uniqTsparse(as(m2[, c(4,2)], "TsparseMatrix")))
228            )## failed in 0.9975-11            )## failed in 0.9975-11
229    
230    (uTr <- new("dtTMatrix", Dim = c(3L,3L), diag="U"))
231    uTr[1,] <- 0
232    assert.EQ.mat(uTr, cbind(0, rbind(0,diag(2))))
233    
234  M <- m0; M[1,] <- 0  M <- m0; M[1,] <- 0
235  stopifnot(identical(M, Diagonal(x=c(0, rep(1,4)))))  stopifnot(identical(M, Diagonal(x=c(0, rep(1,4)))))
236  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)
# Line 245  Line 257 
257  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)))
258    
259    
260    ## "Vector indices" -------------------
261    D <- Diagonal(6)
262    M <- as(D,"dgeMatrix")
263    m <- as(D,"matrix")
264    s <- as(D,"TsparseMatrix")
265    S <- as(s,"CsparseMatrix")
266    i <- c(3,1,6); v <- c(10,15,20)
267    ## (logical,value) which both are recycled:
268    L <- c(TRUE, rep(FALSE,8)) ; z <- c(50,99)
269    
270    ## vector subassignment, both with integer & logical
271    ## these now work correctly {though not very efficiently; hence warnings}
272    m[i] <- v # the role model: only first column is affected
273    M[i] <- v; assert.EQ.mat(M,m) # dge
274    D[i] <- v; assert.EQ.mat(D,m) # ddi -> dtT -> dgT
275    s[i] <- v; assert.EQ.mat(s,m) # dtT -> dgT
276    S[i] <- v; assert.EQ.mat(S,m); S # dtC -> dtT -> dgT -> dgC
277    ## logical
278    m[L] <- z
279    M[L] <- z; assert.EQ.mat(M,m)
280    D[L] <- z; assert.EQ.mat(D,m)
281    s[L] <- z; assert.EQ.mat(s,m)
282    S[L] <- z; assert.EQ.mat(S,m) ; S
283    
284    ## indexing [i]  vs  [i,] --- now ok
285    stopifnot(identical4(m[i], M[i], D[i], s[i]), identical(s[i],S[i]))
286    stopifnot(identical4(m[L], M[L], D[L], s[L]), identical(s[L],S[L]))
287    assert.EQ.mat(D[i,], m[i,])
288    assert.EQ.mat(M[i,], m[i,])
289    assert.EQ.mat(s[i,], m[i,])
290    assert.EQ.mat(S[i,], m[i,])
291    
292    assert.EQ.mat(D[,i], m[,i])
293    assert.EQ.mat(M[,i], m[,i])
294    assert.EQ.mat(s[,i], m[,i])
295    assert.EQ.mat(S[,i], m[,i])
296    
297    
298  ## --- negative indices ----------  ## --- negative indices ----------
299  mc <- mC[1:5, 1:7]  mc <- mC[1:5, 1:7]
300  mt <- mT[1:5, 1:7]  mt <- mT[1:5, 1:7]
# Line 331  Line 381 
381  mc[4,] <- 0  mc[4,] <- 0
382  mc  mc
383    
384    S <- as(Diagonal(5),"sparseMatrix")
385  H <- Hilbert(9)  H <- Hilbert(9)
386  Hc <- as(round(H, 3), "dsCMatrix")# a sparse matrix with no 0 ...  Hc <- as(round(H, 3), "dsCMatrix")# a sparse matrix with no 0 ...
387  (trH <- tril(Hc[1:5, 1:5]))  (trH <- tril(Hc[1:5, 1:5]))
388  stopifnot(is(trH, "triangularMatrix"), trH@uplo == "L")  stopifnot(is(trH, "triangularMatrix"), trH@uplo == "L",
389              is(S, "triangularMatrix"))
390    
391    ## triangular assignment
392    ## the slick (but inefficient in case of sparse!) way to assign sub-diagonals:
393    ## equivalent to tmp <- `diag<-`(S[,-1], -2:1); S[,-1] <- tmp
394    ## which dispatches to (x="TsparseMatrix", i="missing",j="index", value="replValue")
395    diag(S[,-1]) <- -2:1 # used to give a wrong warning
396    S <- as(S,"triangularMatrix")
397    assert.EQ.mat(S, local({s <- diag(5); diag(s[,-1]) <- -2:1; s}))
398    
399    trH[c(1:2,4), c(2:3,5)] <- 0 # gave an *error* upto Jan.2008
400    trH[ lower.tri(trH) ] <- 0   # ditto, because of callNextMethod()
401    
402    m <- Matrix(0+1:28, nrow = 4)
403    m[-3,c(2,4:5,7)] <- m[ 3, 1:4] <- m[1:3, 6] <- 0
404    mT <- as(m, "dgTMatrix")
405    stopifnot(identical(mT[lower.tri(mT)],
406                        m [lower.tri(m) ]))
407    lM <- upper.tri(mT, diag=TRUE)
408    mT[lM] <- 0
409     m[lM] <- 0
410    assert.EQ.mat(mT, as(m,"matrix"))
411    mT[lM] <- -1:0
412     m[lM] <- -1:0
413    assert.EQ.mat(mT, as(m,"matrix"))
414    (mT <- drop0(mT))
415    
416  i <- c(1:2, 4, 6:7); j <- c(2:4,6)  i <- c(1:2, 4, 6:7); j <- c(2:4,6)
417  H[i,j] <- 0  H[i,j] <- 0
# Line 410  Line 487 
487  ne. <- mc != m.  # was wrong (+ warning)  ne. <- mc != m.  # was wrong (+ warning)
488  stopifnot(identical(!(m. < mc), m. >= mc),  stopifnot(identical(!(m. < mc), m. >= mc),
489            identical(m. < mc, as(!ge, "sparseMatrix")),            identical(m. < mc, as(!ge, "sparseMatrix")),
490            identical(ne., Matrix:::drop0(ne1)))            identical(ne., drop0(ne1)))
491    
492  (M3 <- Matrix(upper.tri(matrix(, 3, 3)))) # ltC; indexing used to fail  (M3 <- Matrix(upper.tri(matrix(, 3, 3)))) # ltC; indexing used to fail
493  T3 <- as(M3, "TsparseMatrix")  T3 <- as(M3, "TsparseMatrix")
# Line 421  Line 498 
498            !is(T3[,2, drop=FALSE], "triangularMatrix")            !is(T3[,2, drop=FALSE], "triangularMatrix")
499            )            )
500    
501    M <- Diagonal(4); M[1,2] <- 2
502    M. <- as(M, "CsparseMatrix")
503    (R <- as(M., "RsparseMatrix"))
504    stopifnot(is(M, "triangularMatrix"),
505              is(M.,"triangularMatrix"),
506              is(R, "triangularMatrix"))
507    stopifnot(dim(M[2:3, FALSE]) == c(2,0),
508              dim(R[2:3, FALSE]) == c(2,0),
509              identical(M [2:3,TRUE], M [2:3,]),
510              identical(M.[2:3,TRUE], M.[2:3,]),
511              identical(R [2:3,TRUE], R [2:3,]),
512              dim(R[FALSE, FALSE]) == c(0,0))
513    
514  cat('Time elapsed: ', proc.time(),'\n') # for ``statistical reasons''  cat('Time elapsed: ', proc.time(),'\n') # for ``statistical reasons''

Legend:
Removed from v.1835  
changed lines
  Added in v.2120

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