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 2113, Mon Feb 18 08:27:41 2008 UTC revision 2506, Fri Dec 11 12:54:28 2009 UTC
# Line 2  Line 2 
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"), keep.source = FALSE)
6    ##-> identical3() etc
7    
8    if(interactive()) {
9        options(error = recover, warn = 1)
10    } else if(FALSE) { ## MM @ testing
11        options(error = recover, Matrix.verbose = TRUE, warn = 1)
12    } else options(Matrix.verbose = TRUE, warn = 1)
13    
 options(verbose = TRUE)# to show message()s  
14    
15  ### Dense Matrices  ### Dense Matrices
16    
# Line 12  Line 18 
18  validObject(m)  validObject(m)
19  stopifnot(identical(m, m[]),  stopifnot(identical(m, m[]),
20            identical(m[2, 3],  16), # simple number            identical(m[2, 3],  16), # simple number
21            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
22              identical(m[NA,NA], as(Matrix(NA, 7,4), "dMatrix")))
23    
24  m[2, 3:4, drop=FALSE] # sub matrix of class 'dgeMatrix'  m[2, 3:4, drop=FALSE] # sub matrix of class 'dgeMatrix'
25  m[-(4:7), 3:4]        # ditto; the upper right corner of 'm'  m[-(4:7), 3:4]        # ditto; the upper right corner of 'm'
# Line 34  Line 41 
41  mn <- m  mn <- m
42  dimnames(mn) <- list(paste("r",letters[1:nrow(mn)],sep=""),  dimnames(mn) <- list(paste("r",letters[1:nrow(mn)],sep=""),
43                       LETTERS[1:ncol(mn)])                       LETTERS[1:ncol(mn)])
44    checkMatrix(mn)
45  mn["rd", "D"]  mn["rd", "D"]
46    ## Printing sparse colnames:
47    ms <- as(mn,"sparseMatrix")
48    ms[sample(28, 20)] <- 0
49    ms <- t(rbind2(ms, 3*ms))
50    cnam1 <- capture.output(show(ms))[2] ; op <- options("sparse.colnames" = "abb3")
51    cnam2 <- capture.output(show(ms))[2] ; options(op) # revert
52  stopifnot(identical(mn["rc", "D"], mn[3,4]), mn[3,4] == 24,  stopifnot(identical(mn["rc", "D"], mn[3,4]), mn[3,4] == 24,
53            identical(mn[, "A"], mn[,1]), mn[,1] == 1:7,            identical(mn[, "A"], mn[,1]), mn[,1] == 1:7,
54            identical(mn[c("re", "rb"), "B"], mn[c(5,2), 2])            identical(mn[c("re", "rb"), "B"], mn[c(5,2), 2]),
55            )            ## sparse printing
56              grep("^ +$", cnam1) == 1, # cnam1 is empty
57              identical(cnam2,
58                        paste(" ", paste(rep(rownames(mn), 2), collapse=" "))))
59    
60  mo <- m  mo <- m
61  m[2,3] <- 100  m[2,3] <- 100
# Line 49  Line 66 
66  m. <- as.matrix(m)  m. <- as.matrix(m)
67    
68  ## m[ cbind(i,j) ] indexing:  ## m[ cbind(i,j) ] indexing:
69  ij <- cbind(1:6, 2:3)  iN <- ij <- cbind(1:6, 2:3)
70  stopifnot(identical(m[ij], m.[ij]))  iN[2:3,] <- iN[5,2] <- NA
71    stopifnot(identical(m[ij], m.[ij]),
72              identical(m[iN], m.[iN]))
73    
74  ## testing operations on logical Matrices rather more than indexing:  ## testing operations on logical Matrices rather more than indexing:
75  g10 <- m [ m > 10 ]  g10 <- m [ m > 10 ]
# Line 89  Line 108 
108  j <- c(2:4, 4:3)  j <- c(2:4, 4:3)
109  assert.EQ.mat(mC[i,], mm[i,])  assert.EQ.mat(mC[i,], mm[i,])
110  assert.EQ.mat(mC[,j], mm[,j])  assert.EQ.mat(mC[,j], mm[,j])
111    ## FIXME? assert.EQ.mat(mC[,NA], mm[,NA]) -- mC[,NA] is all 0 "instead" of all NA
112    ## MM currently thinks we should  NOT  allow  <sparse>[ <NA> ]
113  assert.EQ.mat(mC[i, 2:1], mm[i, 2:1])  assert.EQ.mat(mC[i, 2:1], mm[i, 2:1])
114  assert.EQ.mat(mC[c(4,1,2:1), j], mm[c(4,1,2:1), j])  assert.EQ.mat(mC[c(4,1,2:1), j], mm[c(4,1,2:1), j])
115  assert.EQ.mat(mC[i,j], mm[i,j])  assert.EQ.mat(mC[i,j], mm[i,j])
# Line 103  Line 124 
124  m. <- mC; m.[, c(2, 7:12)] <- 0  m. <- mC; m.[, c(2, 7:12)] <- 0
125  validObject(S <- crossprod(add.simpleDimnames(m.) %% 100))  validObject(S <- crossprod(add.simpleDimnames(m.) %% 100))
126  ss <- as(S, "matrix")  ss <- as(S, "matrix")
127    ds <- as(S, "denseMatrix")
128    ## NA-indexing of *dense* Matrices: should work as traditionally
129    assert.EQ.mat(ds[NA,NA], ss[NA,NA])
130    assert.EQ.mat(ds[NA,  ], ss[NA,])
131    assert.EQ.mat(ds[  ,NA], ss[,NA])
132  T <- as(S, "TsparseMatrix")  T <- as(S, "TsparseMatrix")
133    stopifnot(identical(ds[2 ,NA], ss[2,NA]),
134              identical(ds[NA, 1], ss[NA, 1]),
135              identical(S, as(T, "CsparseMatrix")) )
136    
137  ## non-repeated indices:  ## non-repeated indices:
138  i <- c(7:5, 2:4);assert.EQ.mat(T[i,i], ss[i,i])  i <- c(7:5, 2:4);assert.EQ.mat(T[i,i], ss[i,i])
139    ## NA in indices  -- check that we get a helpful error message:
140    i[2] <- NA
141    er <- tryCatch(T[i,i], error = function(e)e)
142    stopifnot(as.logical(grep("indices.*sparse Matrices", er$message)))
143    
144  N <- nrow(T)  N <- nrow(T)
145  set.seed(11)  set.seed(11)
146  for(n in 1:50) {  for(n in 1:50) {
# Line 116  Line 151 
151      assert.EQ.mat(Tii, ss[i,i])      assert.EQ.mat(Tii, ss[i,i])
152  }  }
153    
154    b <- diag(1:2)[,c(1,1,2,2)]
155    cb <- crossprod(b)
156    cB <- crossprod(Matrix(b, sparse=TRUE))
157    a <- matrix(0, 6, 6)
158    a[1:4, 1:4] <- cb
159    A1 <- A2 <- Matrix(0, 6, 6)#-> sparse
160    A1[1:4, 1:4] <- cb
161    A2[1:4, 1:4] <- cB
162    assert.EQ.mat(A1, a)# indeed
163    stopifnot(identical(A1, A2), is(A1, "dsCMatrix"))
164    
165  ## repeated ones ``the challenge'' (to do smartly):  ## repeated ones ``the challenge'' (to do smartly):
166  j <- c(4, 4, 9, 12, 9, 4, 17, 3, 18, 4, 12, 18, 4, 9)  j <- c(4, 4, 9, 12, 9, 4, 17, 3, 18, 4, 12, 18, 4, 9)
167  assert.EQ.mat(T[j,j], ss[j,j])  assert.EQ.mat(T[j,j], ss[j,j])
# Line 143  Line 189 
189    
190    
191  stopifnot(all.equal(mC[,3], mm[,3]),  stopifnot(all.equal(mC[,3], mm[,3]),
192            identical(mC[ij], mm[ij]))            identical(mC[ij], mm[ij]),
193              identical(mC[iN], mm[iN]))
194    
195  assert.EQ.mat(mC[7, , drop=FALSE], mm[7, , drop=FALSE])  assert.EQ.mat(mC[7, , drop=FALSE], mm[7, , drop=FALSE])
196    identical    (mC[7,   drop=FALSE], mm[7,   drop=FALSE]) # *vector* indexing
197    
198  stopifnot(dim(mC[numeric(0), ]) == c(0,20), # used to give warnings  stopifnot(dim(mC[numeric(0), ]) == c(0,20), # used to give warnings
199            dim(mC[, integer(0)]) == c(40,0),            dim(mC[, integer(0)]) == c(40,0),
200            identical(mC[, integer(0)], mC[, FALSE]),            identical(mC[, integer(0)], mC[, FALSE]))
           identical(mC[7,  drop = FALSE],  
                     mC[7,, drop = FALSE]))  
201  validObject(print(mT[,c(2,4)]))  validObject(print(mT[,c(2,4)]))
202  stopifnot(all.equal(mT[2,], mm[2,]),  stopifnot(all.equal(mT[2,], mm[2,]),
203            ## row or column indexing in combination with t() :            ## row or column indexing in combination with t() :
# Line 178  Line 225 
225  lx.x <- as(x.x, "lsCMatrix") # FALSE only for "structural" 0  lx.x <- as(x.x, "lsCMatrix") # FALSE only for "structural" 0
226  (l10 <- lx.x[1:10, 1:10])# "lsC"  (l10 <- lx.x[1:10, 1:10])# "lsC"
227  (l3 <-  lx.x[1:3, ])  (l3 <-  lx.x[1:3, ])
228  m.x <- as(x.x, "matrix")  m.x <- as.mat(x.x) # as.mat() *drops* (NULL,NULL) dimnames
229  stopifnot(class(l10) == "lsCMatrix", # symmetric indexing -> symmetric !  stopifnot(class(l10) == "lsCMatrix", # symmetric indexing -> symmetric !
230            identical(as.mat(lx.x), m.x != 0),            identical(as.mat(lx.x), m.x != 0),
231            identical(as.logical(lx.x), as.logical(m.x)),            identical(as.logical(lx.x), as.logical(m.x)),
# Line 212  Line 259 
259            all(sm[,-(1:3)] == 0)            all(sm[,-(1:3)] == 0)
260            )            )
261    
 ### Diagonal -- Sparse:  
262  m0 <- Diagonal(5)  m0 <- Diagonal(5)
263  (m1 <- as(m0, "sparseMatrix"))  # dtTMatrix  stopifnot(identical(m0[2,], m0[,2]),
264  (m2 <- as(m0, "CsparseMatrix")) # dtCMatrix (with an irrelevant warning)            identical(m0[,1], c(1,0,0,0,0)))
265    ### Diagonal -- Sparse:
266    (m1 <- as(m0, "TsparseMatrix"))  # dtTMatrix
267    (m2 <- as(m0, "CsparseMatrix")) # dtCMatrix
268  m1g <- as(m1, "generalMatrix")  m1g <- as(m1, "generalMatrix")
269  stopifnot(is(m1g, "dgTMatrix"))  stopifnot(is(m1g, "dgTMatrix"))
270  assert.EQ.mat(m2[1:3,],    diag(5)[1:3,])  assert.EQ.mat(m2[1:3,],    diag(5)[1:3,])
# Line 225  Line 274 
274                      Matrix:::uniqTsparse(as(m2[, c(4,2)], "TsparseMatrix")))                      Matrix:::uniqTsparse(as(m2[, c(4,2)], "TsparseMatrix")))
275            )## failed in 0.9975-11            )## failed in 0.9975-11
276    
277    (uTr <- new("dtTMatrix", Dim = c(3L,3L), diag="U"))
278    uTr[1,] <- 0
279    assert.EQ.mat(uTr, cbind(0, rbind(0,diag(2))))
280    
281  M <- m0; M[1,] <- 0  M <- m0; M[1,] <- 0
282  stopifnot(identical(M, Diagonal(x=c(0, rep(1,4)))))  stopifnot(identical(M, Diagonal(x=c(0, rep(1,4)))))
283  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)
284  validObject(M)  checkMatrix(M)
285  M <- m0; M[1:3, 3] <- 0 ;M  M <- m0; M[1:3, 3] <- 0 ;M
286  T <- m0; T[1:3, 3] <- 10  T <- m0; T[1:3, 3] <- 10
287  stopifnot(identical(M, Diagonal(x=c(1,1, 0, 1,1))),  stopifnot(identical(M, Diagonal(x=c(1,1, 0, 1,1))),
288            is(T, "triangularMatrix"), identical(T[,3], c(10,10,10,0,0)))            isValid(T, "triangularMatrix"), identical(T[,3], c(10,10,10,0,0)))
289    
290  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)
291  M <- m1; M[,3] <- 3 ; stopifnot(is(M,"sparseMatrix"), M[,3] == 3)  M <- m1; M[,3] <- 3 ; stopifnot(is(M,"sparseMatrix"), M[,3] == 3)
292  validObject(M)  checkMatrix(M)
293  M <- m1; M[1:3, 3] <- 0 ;M  M <- m1; M[1:3, 3] <- 0 ;M
294  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)
295  T <- m1; T[1:3, 3] <- 10; validObject(T)  T <- m1; T[1:3, 3] <- 10; checkMatrix(T)
296  stopifnot(is(T, "dtTMatrix"), identical(T[,3], c(10,10,10,0,0)))  stopifnot(isValid(T, "dtTMatrix"), identical(T[,3], c(10,10,10,0,0)))
297    
298  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)
299  M <- m2; M[,3] <- 3 ; stopifnot(is(M,"sparseMatrix"), M[,3] == 3)  M <- m2; M[,3] <- 3 ; stopifnot(is(M,"sparseMatrix"), M[,3] == 3)
300  validObject(M)  checkMatrix(M)
301  M <- m2; M[1:3, 3] <- 0 ;M  M <- m2; M[1:3, 3] <- 0 ;M
302  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)
303  T <- m2; T[1:3, 3] <- 10; validObject(T)  T <- m2; T[1:3, 3] <- 10; checkMatrix(T)
304  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)))
305    
306    
307  ## "Vector indices" -------------------  ## "Vector indices" -------------------
308    .iniDiag.example <- expression({
309  D <- Diagonal(6)  D <- Diagonal(6)
310  M <- as(D,"dgeMatrix")  M <- as(D,"dgeMatrix")
311  m <- as(D,"matrix")  m <- as(D,"matrix")
312  s <- as(D,"TsparseMatrix")  s <- as(D,"TsparseMatrix")
313  S <- as(s,"CsparseMatrix")  S <- as(s,"CsparseMatrix")
314    })
315    eval(.iniDiag.example)
316  i <- c(3,1,6); v <- c(10,15,20)  i <- c(3,1,6); v <- c(10,15,20)
317  ## (logical,value) which both are recycled:  ## (logical,value) which both are recycled:
318  L <- c(TRUE, rep(FALSE,8)) ; z <- c(50,99)  L <- c(TRUE, rep(FALSE,8)) ; z <- c(50,99)
# Line 268  Line 324 
324  D[i] <- v; assert.EQ.mat(D,m) # ddi -> dtT -> dgT  D[i] <- v; assert.EQ.mat(D,m) # ddi -> dtT -> dgT
325  s[i] <- v; assert.EQ.mat(s,m) # dtT -> dgT  s[i] <- v; assert.EQ.mat(s,m) # dtT -> dgT
326  S[i] <- v; assert.EQ.mat(S,m); S # dtC -> dtT -> dgT -> dgC  S[i] <- v; assert.EQ.mat(S,m); S # dtC -> dtT -> dgT -> dgC
327    stopifnot(identical(s,D))
328  ## logical  ## logical
329    eval(.iniDiag.example)
330  m[L] <- z  m[L] <- z
331  M[L] <- z; assert.EQ.mat(M,m)  M[L] <- z; assert.EQ.mat(M,m)
332  D[L] <- z; assert.EQ.mat(D,m)  D[L] <- z; assert.EQ.mat(D,m)
# Line 276  Line 334 
334  S[L] <- z; assert.EQ.mat(S,m) ; S  S[L] <- z; assert.EQ.mat(S,m) ; S
335    
336  ## indexing [i]  vs  [i,] --- now ok  ## indexing [i]  vs  [i,] --- now ok
337  stopifnot(identical4(m[i], M[i], D[i], s[i]), identical(s[i],S[i]))  eval(.iniDiag.example)
338  stopifnot(identical4(m[L], M[L], D[L], s[L]), identical(s[L],S[L]))  stopifnot(identical5(m[i], M[i], D[i], s[i], S[i]))
339    stopifnot(identical5(m[L], M[L], D[L], s[L], S[L]))
340    ## bordercase ' drop = .' *vector* indexing {failed till 2009-04-..)
341    stopifnot(identical5(m[i,drop=FALSE], M[i,drop=FALSE], D[i,drop=FALSE],
342                         s[i,drop=FALSE], S[i,drop=FALSE]))
343    stopifnot(identical5(m[L,drop=FALSE], M[L,drop=FALSE], D[L,drop=FALSE],
344                         s[L,drop=FALSE], S[L,drop=FALSE]))
345    ## using L for row-indexing should give an error
346    assertError(m[L,]); assertError(m[L,, drop=FALSE])
347    ## these did not signal an error, upto (including) 0.999375-30:
348    assertError(s[L,]); assertError(s[L,, drop=FALSE])
349    assertError(S[L,]); assertError(S[L,, drop=FALSE])
350    
351    ## row indexing:
352  assert.EQ.mat(D[i,], m[i,])  assert.EQ.mat(D[i,], m[i,])
353  assert.EQ.mat(M[i,], m[i,])  assert.EQ.mat(M[i,], m[i,])
354  assert.EQ.mat(s[i,], m[i,])  assert.EQ.mat(s[i,], m[i,])
355  assert.EQ.mat(S[i,], m[i,])  assert.EQ.mat(S[i,], m[i,])
356    ## column indexing:
357  assert.EQ.mat(D[,i], m[,i])  assert.EQ.mat(D[,i], m[,i])
358  assert.EQ.mat(M[,i], m[,i])  assert.EQ.mat(M[,i], m[,i])
359  assert.EQ.mat(s[,i], m[,i])  assert.EQ.mat(s[,i], m[,i])
# Line 335  Line 406 
406    
407  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)
408  mt[2:3, 4:7] <- 33  mt[2:3, 4:7] <- 33
409  validObject(mt)  checkMatrix(mt)
410  mt  mt
411    
412  mc[1,4] <- -99 ; stopifnot(mc[1,4] == -99)  mc[1,4] <- -99 ; stopifnot(mc[1,4] == -99)
# Line 370  Line 441 
441  mc # no longer has non-structural zeros  mc # no longer has non-structural zeros
442  mc[ii, jj] <- 1:6  mc[ii, jj] <- 1:6
443  mc[c(2,5), c(3,5)] <- 3.2  mc[c(2,5), c(3,5)] <- 3.2
444  validObject(mc)  checkMatrix(mc)
445  m. <- mc  m. <- mc
446  mc[4,] <- 0  mc[4,] <- 0
447  mc  mc
448    
449  S <- as(Diagonal(5),"sparseMatrix")  S <- as(Diagonal(5),"TsparseMatrix")
450  H <- Hilbert(9)  H <- Hilbert(9)
451  Hc <- as(round(H, 3), "dsCMatrix")# a sparse matrix with no 0 ...  Hc <- as(round(H, 3), "dsCMatrix")# a sparse matrix with no 0 ...
452  (trH <- tril(Hc[1:5, 1:5]))  (trH <- tril(Hc[1:5, 1:5]))
# Line 417  Line 488 
488    
489  ## an example that failed for a long time  ## an example that failed for a long time
490  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))
491  validObject(dm <- kronecker(Diagonal(2), sy3))# now sparse with new kronecker  checkMatrix(dm <- kronecker(Diagonal(2), sy3))# now sparse with new kronecker
492  dm <- Matrix(as.matrix(dm))# -> "dsyMatrix"  dm <- Matrix(as.matrix(dm))# -> "dsyMatrix"
493  (s2 <- as(dm, "sparseMatrix"))  (s2 <- as(dm, "sparseMatrix"))
494  validObject(st <- as(s2, "TsparseMatrix"))  checkMatrix(st <- as(s2, "TsparseMatrix"))
495  stopifnot(is(s2, "symmetricMatrix"),  stopifnot(is(s2, "symmetricMatrix"),
496            is(st, "symmetricMatrix"))            is(st, "symmetricMatrix"))
497  validObject(s.32  <- st[1:3,1:2]) ## 3 x 2 - and *not* dsTMatrix  checkMatrix(s.32  <- st[1:3,1:2]) ## 3 x 2 - and *not* dsTMatrix
498  validObject(s2.32 <- s2[1:3,1:2])  checkMatrix(s2.32 <- s2[1:3,1:2])
499  I <- c(1,4:3)  I <- c(1,4:3)
500  stopifnot(is(s2.32, "generalMatrix"),  stopifnot(is(s2.32, "generalMatrix"),
501            is(s.32,  "generalMatrix"),            is(s.32,  "generalMatrix"),
# Line 439  Line 510 
510    
511  ## now sub-assign  and check for consistency  ## now sub-assign  and check for consistency
512  ## symmetric subassign should keep symmetry  ## symmetric subassign should keep symmetry
513  st[I,I] <- 0; validObject(st); stopifnot(is(st,"symmetricMatrix"))  st[I,I] <- 0; checkMatrix(st); stopifnot(is(st,"symmetricMatrix"))
514  s2[I,I] <- 0; validObject(s2); stopifnot(is(s2,"symmetricMatrix"))  s2[I,I] <- 0; checkMatrix(s2); stopifnot(is(s2,"symmetricMatrix"))
515  ##  ##
516  m <- as.mat(st)  m <- as.mat(st)
517   m[2:1,2:1] <- 4:1   m[2:1,2:1] <- 4:1
# Line 472  Line 543 
543  m.[ cbind(3:5, 1:3) ] <- 1:3  m.[ cbind(3:5, 1:3) ] <- 1:3
544  stopifnot(m.[3,1] == 1, m.[4,2] == 2)  stopifnot(m.[3,1] == 1, m.[4,2] == 2)
545  x.x[ cbind(2:6, 2:6)] <- 12:16  x.x[ cbind(2:6, 2:6)] <- 12:16
546  validObject(x.x)  stopifnot(isValid(x.x, "dsCMatrix"),
 stopifnot(class(x.x) == "dsCMatrix",  
547            12:16 == as.mat(x.x)[cbind(2:6, 2:6)])            12:16 == as.mat(x.x)[cbind(2:6, 2:6)])
548  (ne1 <- (mc - m.) != 0)  (ne1 <- (mc - m.) != 0)
549  stopifnot(identical(ne1, 0 != abs(mc - m.)))  stopifnot(identical(ne1, 0 != abs(mc - m.)))
# Line 483  Line 553 
553            identical(m. < mc, as(!ge, "sparseMatrix")),            identical(m. < mc, as(!ge, "sparseMatrix")),
554            identical(ne., drop0(ne1)))            identical(ne., drop0(ne1)))
555    
556    d6 <- Diagonal(6)
557    ii <- c(1:2, 4:5)
558    d6[cbind(ii,ii)] <- 7*ii
559    stopifnot(is(d6, "ddiMatrix"), identical(d6, Diagonal(x=c(7*1:2,1,7*4:5,1))))
560    
561    for(j in 3:6) { ## even and odd j used to behave differently
562        M <- Matrix(0, j,j); m <- matrix(0, j,j)
563        T  <- as(M, "TsparseMatrix")
564        TG <- as(T, "generalMatrix")
565        G <-  as(M, "generalMatrix")
566        id <- cbind(1:j,1:j)
567        i2 <- cbind(1:j,j:1)
568        m[id] <- 1:j
569        M[id] <- 1:j ; stopifnot(is(M,"symmetricMatrix"))
570        T[id] <- 1:j ; stopifnot(is(T,"symmetricMatrix"))
571        G[id] <- 1:j
572        TG[id]<- 1:j
573        m[i2] <- 10
574        M[i2] <- 10 ; stopifnot(is(M,"symmetricMatrix"))
575        T[i2] <- 10 ; stopifnot(is(T,"symmetricMatrix"))
576        G[i2] <- 10
577        TG[i2]<- 10
578        ##
579        assert.EQ.mat(M, m)
580        assert.EQ.mat(T, m)
581        assert.EQ.mat(G, m)
582        assert.EQ.mat(TG,m)
583    }
584    
585    
586    ## drop, triangular, ...
587  (M3 <- Matrix(upper.tri(matrix(, 3, 3)))) # ltC; indexing used to fail  (M3 <- Matrix(upper.tri(matrix(, 3, 3)))) # ltC; indexing used to fail
588  T3 <- as(M3, "TsparseMatrix")  T3 <- as(M3, "TsparseMatrix")
589  stopifnot(identical(drop(M3), M3),  stopifnot(identical(drop(M3), M3),
# Line 492  Line 593 
593            !is(T3[,2, drop=FALSE], "triangularMatrix")            !is(T3[,2, drop=FALSE], "triangularMatrix")
594            )            )
595    
596    (T6 <- as(as(kronecker(Matrix(c(0,0,1,0),2,2), t(T3)), "lMatrix"),
597              "triangularMatrix"))
598    T6[1:4, -(1:3)] # failed (trying to coerce back to ltTMatrix)
599    stopifnot(identical(T6[1:4, -(1:3)][2:3, -3],
600                        spMatrix(2,2, i=c(1,2,2), j=c(1,1,2), x=rep(TRUE,3))))
601    
602  M <- Diagonal(4); M[1,2] <- 2  M <- Diagonal(4); M[1,2] <- 2
603  M. <- as(M, "CsparseMatrix")  M. <- as(M, "CsparseMatrix")
604  (R <- as(M., "RsparseMatrix"))  (R <- as(M., "RsparseMatrix"))
605  stopifnot(is(M, "triangularMatrix"),  (Ms <- symmpart(M.))
606            is(M.,"triangularMatrix"),  Rs <- as(Ms, "RsparseMatrix")
607            is(R, "triangularMatrix"))  stopifnot(isValid(M, "triangularMatrix"),
608              isValid(M.,"triangularMatrix"),
609              isValid(Ms, "dsCMatrix"),
610              isValid(R,  "dtRMatrix"),
611              isValid(Rs, "dsRMatrix") )
612  stopifnot(dim(M[2:3, FALSE]) == c(2,0),  stopifnot(dim(M[2:3, FALSE]) == c(2,0),
613            dim(R[2:3, FALSE]) == c(2,0),            dim(R[2:3, FALSE]) == c(2,0),
614            identical(M [2:3,TRUE], M [2:3,]),            identical(M [2:3,TRUE], M [2:3,]),
# Line 505  Line 616 
616            identical(R [2:3,TRUE], R [2:3,]),            identical(R [2:3,TRUE], R [2:3,]),
617            dim(R[FALSE, FALSE]) == c(0,0))            dim(R[FALSE, FALSE]) == c(0,0))
618    
619  cat('Time elapsed: ', proc.time(),'\n') # for ``statistical reasons''  n <- 50000L
620    Lrg <- new("dgTMatrix", Dim = c(n,n))
621    diag(Lrg) <- 1:n
622    dLrg <- as(Lrg, "diagonalMatrix")
623    stopifnot(identical(Diagonal(x = 1:n), dLrg))
624    diag(dLrg) <- 1 + diag(dLrg)
625    Clrg <- as(Lrg,"CsparseMatrix")
626    Ctrg <- as(Clrg, "triangularMatrix")
627    diag(Ctrg) <- 1 + diag(Ctrg)
628    stopifnot(identical(Diagonal(x = 1+ 1:n), dLrg),
629              identical(Ctrg, as(dLrg,"CsparseMatrix")))
630    
631    cc <- capture.output(show(dLrg))# show(<diag>) used to error for large n
632    
633    ## Large Matrix indexing / subassignment
634    ## ------------------------------------- (from ex. by Imran Rashid)
635    n <- 7000000
636    m <-  100000
637    nnz <- 20000
638    
639    set.seed(12)
640    f <- sparseMatrix(i = sample(n, size=nnz, replace=TRUE),
641                      j = sample(m, size=nnz, replace=TRUE))
642    str(f)
643    dim(f) # 6999863 x 99992
644    prod(dim(f)) # 699930301096 == 699'930'301'096  (~ 700'000 millions)
645    str(thisCol <-  f[,5000])# logi [~ 7 mio....]
646    sv <- as(thisCol, "sparseVector")
647    str(sv) ## "empty" !
648    validObject(spCol <- f[,5000, drop=FALSE])
649    ## ^^ FIXME slow Tsparse_to_Csparse from memory-hog
650    ## cholmod_sparse *CHOLMOD(triplet_to_sparse)
651    ## which has  "workspace: Iwork (max (nrow,ncol))"
652    ## in ../src/CHOLMOD/Core/cholmod_triplet.c  *and*
653    ## in ../src/CHOLMOD/Core/t_cholmod_triplet.c
654    ##
655    ## *not* identical(): as(spCol, "sparseVector")@length is "double"prec:
656    stopifnot(all.equal(as(spCol, "sparseVector"),
657                        as(sv,   "nsparseVector"), tol=0))
658    f[,5762] <- thisCol # now "fine" <<<<<<<<<< FIXME uses LARGE objects
659    ## is using  replCmat() in ../R/Csparse.R, then
660    ##           replTmat() in ../R/Tsparse.R
661    
662    fx <- sparseMatrix(i = sample(n, size=nnz, replace=TRUE),
663                       j = sample(m, size=nnz, replace=TRUE),
664                       x = round(10*rnorm(nnz)))
665    class(fx)## dgCMatrix
666    fx[,6000] <- (tC <- rep(thisCol, length=nrow(fx)))
667    thCol <- fx[,2000]
668    fx[,5762] <- thCol
669    stopifnot(is(f, "ngCMatrix"), is(fx, "dgCMatrix"),
670              identical(thisCol, f[,5762]),# perfect
671              identical(as.logical(fx[,6000]), tC),
672              identical(thCol,  fx[,5762]))
673    
674    cat('Time elapsed: ', (.pt <- proc.time()),'\n') # "stats"
675    ##
676    cat("checkMatrix() of all: \n---------\n")
677    Sys.setlocale("LC_COLLATE", "C")# to keep ls() reproducible
678    for(nm in ls()) if(is(.m <- get(nm), "Matrix")) {
679        cat(nm, "\n")
680        checkMatrix(.m, verbose = FALSE)
681    }
682    cat('Time elapsed: ', proc.time() - .pt,'\n') # "stats"
683    
684    if(!interactive()) warnings()
685    

Legend:
Removed from v.2113  
changed lines
  Added in v.2506

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