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 2120, Tue Mar 4 21:44:41 2008 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    options(verbose = TRUE)# to show message()s
8    
9  ### Dense Matrices  ### Dense Matrices
10    
11  m <- Matrix(1:28, nrow = 7)  m <- Matrix(1:28 +0, nrow = 7)
12  validObject(m) ; m@x <- as.double(m@x) ; validObject(m)  validObject(m)
13  stopifnot(identical(m, m[]),  stopifnot(identical(m, m[]),
14            identical(m[2, 3],  16), # simple number            identical(m[2, 3],  16), # simple number
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 44  Line 46 
46  m[, 1] <- -1  m[, 1] <- -1
47  m[1:3,]  m[1:3,]
48    
49    m. <- as.matrix(m)
50    
51    ## m[ cbind(i,j) ] indexing:
52    ij <- cbind(1:6, 2:3)
53    stopifnot(identical(m[ij], m.[ij]))
54    
55    ## testing operations on logical Matrices rather more than indexing:
56  g10 <- m [ m > 10 ]  g10 <- m [ m > 10 ]
57  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()  
58      stopifnot(10 == length(m[ m <= 10 ]))      stopifnot(10 == length(m[ m <= 10 ]))
59    sel <- (20 <  m) & (m <  150)
60    sel.<- (20 <  m.)& (m.<  150)
61    nsel <-(20 >= m) | (m >= 150)
62    (ssel <- as(sel, "sparseMatrix"))
63    stopifnot(is(sel, "lMatrix"), is(ssel, "lsparseMatrix"),
64              identical3(as.mat(sel.), as.mat(sel), as.mat(ssel)),
65              identical3(!sel, !ssel, nsel), # !<sparse> is typically dense
66              identical3(m[ sel],  m[ ssel], as.matrix(m)[as.matrix( ssel)]),
67              identical3(m[!sel],  m[!ssel], as.matrix(m)[as.matrix(!ssel)])
68              )
69    
70    ## more sparse Matrices --------------------------------------
 ### Sparse Matrices --------------------------------------  
71    
72  m <- 1:800  m <- 1:800
73  set.seed(101) ; m[sample(800, 600)] <- 0  set.seed(101) ; m[sample(800, 600)] <- 0
# Line 67  Line 83 
83  mC[1:2,]  mC[1:2,]
84  mC[7,  drop = FALSE]  mC[7,  drop = FALSE]
85  assert.EQ.mat(mC[1:2,], mm[1:2,])  assert.EQ.mat(mC[1:2,], mm[1:2,])
86  stopifnot(all.equal(mC[,3],   mm[,3]))  
87    ## *repeated* (aka 'duplicated') indices - did not work at all ...
88    i <- rep(8:10,2)
89    j <- c(2:4, 4:3)
90    assert.EQ.mat(mC[i,], mm[i,])
91    assert.EQ.mat(mC[,j], mm[,j])
92    assert.EQ.mat(mC[i, 2:1], mm[i, 2:1])
93    assert.EQ.mat(mC[c(4,1,2:1), j], mm[c(4,1,2:1), j])
94    assert.EQ.mat(mC[i,j], mm[i,j])
95    set.seed(7)
96    for(n in 1:50) {
97        i <- sample(sample(nrow(mC), 7), 20, replace = TRUE)
98        j <- sample(sample(ncol(mC), 6), 17, replace = TRUE)
99        assert.EQ.mat(mC[i,j], mm[i,j])
100    }
101    
102    ##---- Symmetric indexing of symmetric Matrix ----------
103    m. <- mC; m.[, c(2, 7:12)] <- 0
104    validObject(S <- crossprod(add.simpleDimnames(m.) %% 100))
105    ss <- as(S, "matrix")
106    T <- as(S, "TsparseMatrix")
107    ## non-repeated indices:
108    i <- c(7:5, 2:4);assert.EQ.mat(T[i,i], ss[i,i])
109    N <- nrow(T)
110    set.seed(11)
111    for(n in 1:50) {
112        i <- sample(N, max(2, sample(N,1)), replace = FALSE)
113        validObject(Tii <- T[i,i])
114        stopifnot(is(Tii, "dsTMatrix"), # remained symmetric Tsparse
115                  identical(t(Tii), t(T)[i,i]))
116        assert.EQ.mat(Tii, ss[i,i])
117    }
118    
119    ## repeated ones ``the challenge'' (to do smartly):
120    j <- c(4, 4, 9, 12, 9, 4, 17, 3, 18, 4, 12, 18, 4, 9)
121    assert.EQ.mat(T[j,j], ss[j,j])
122    ## and another two sets  (a, A) &  (a., A.) :
123    a <- matrix(0, 6,6)
124    a[upper.tri(a)] <- (utr <- c(2, 0,-1, 0,0,5, 7,0,0,0, 0,0,-2,0,8))
125    ta <- t(a); ta[upper.tri(a)] <- utr; a <- t(ta)
126    diag(a) <- c(0,3,0,4,6,0)
127    A <- as(Matrix(a), "TsparseMatrix")
128    A. <- A
129    diag(A.) <- 10 * (1:6)
130    a. <- as(A., "matrix")
131    ## More testing {this was not working for a long time..}
132    set.seed(1)
133    for(n in 1:100) {
134        i <- sample(1:nrow(A), 3+2*rpois(1, lam=3), replace=TRUE)
135        Aii  <- A[i,i]
136        A.ii <- A.[i,i]
137        stopifnot(class(Aii) == class(A),
138                  class(A.ii) == class(A.))
139        assert.EQ.mat(Aii , a [i,i])
140        assert.EQ.mat(A.ii, a.[i,i])
141        assert.EQ.mat(T[i,i], ss[i,i])
142    }
143    
144    
145    stopifnot(all.equal(mC[,3], mm[,3]),
146              identical(mC[ij], mm[ij]))
147  assert.EQ.mat(mC[7, , drop=FALSE], mm[7, , drop=FALSE])  assert.EQ.mat(mC[7, , drop=FALSE], mm[7, , drop=FALSE])
148    
149  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 163 
163            identical3(mm[3,], mC[3,], mT[3,]),            identical3(mm[3,], mC[3,], mT[3,]),
164            identical3(mT[2,3], mC[2,3], 0),            identical3(mT[2,3], mC[2,3], 0),
165            identical(mT[], mT),            identical(mT[], mT),
166            ## 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]),
167            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]))
168                       as(mT[c(3,7), 2:4],"matrix")))            )
169    
170  x.x <- crossprod(mC)  x.x <- crossprod(mC)
171  stopifnot(class(x.x) == "dsCMatrix",  stopifnot(class(x.x) == "dsCMatrix",
172            class(x.x. <- round(x.x / 10000)) == "dsCMatrix")            class(x.x. <- round(x.x / 10000)) == "dsCMatrix",
173              identical(x.x[cbind(2:6, 2:6)],
174                        diag(x.x [2:6, 2:6])))
175  head(x.x.) # Note the *non*-structural 0's printed as "0"  head(x.x.) # Note the *non*-structural 0's printed as "0"
176  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  
177    
178  lx.x <- as(x.x, "lsCMatrix") # FALSE only for "structural" 0  lx.x <- as(x.x, "lsCMatrix") # FALSE only for "structural" 0
179  if(FALSE) { ## FIXME: needs coercion  "lsCMatrix" to "lgTMatrix"  (l10 <- lx.x[1:10, 1:10])# "lsC"
180      lx.x[1:10, 1:10]  (l3 <-  lx.x[1:3, ])
181      lx.x[1:3, ]  m.x <- as.mat(x.x) # as.mat() *drops* (NULL,NULL) dimnames
182  }  stopifnot(class(l10) == "lsCMatrix", # symmetric indexing -> symmetric !
183              identical(as.mat(lx.x), m.x != 0),
184              identical(as.logical(lx.x), as.logical(m.x)),
185              identical(as.mat(l10), m.x[1:10, 1:10] != 0),
186              identical(as.mat(l3 ), m.x[1:3, ] != 0)
187              )
188    
189    ##-- Sub*assignment* with repeated / duplicated index:
190    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. <- 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)),
196                        local({a <- matrix(0,4,3); a[c(1,2,1), 2] <-  1 ; a})),
197              identical(unname(as.matrix(B)),
198                        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
203    n <- 5 ## or much larger
204    sm <- new("dsTMatrix", i=as.integer(1),j=as.integer(1),
205              Dim=as.integer(c(n,n)), x = 1)
206    (cm <- as(sm, "CsparseMatrix"))
207    sm[2,]
208    stopifnot(sm[2,] == c(0:1, rep.int(0,ncol(sm)-2)),
209              sm[2,] == cm[2,],
210              sm[,3] == sm[3,],
211              all(sm[,-(1:3)] == t(sm[-(1:3),])), # all(<lge.>)
212              all(sm[,-(1:3)] == 0)
213              )
214    
215    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
220    (m2 <- as(m0, "CsparseMatrix")) # dtCMatrix (with an irrelevant warning)
221    m1g <- as(m1, "generalMatrix")
222    stopifnot(is(m1g, "dgTMatrix"))
223    assert.EQ.mat(m2[1:3,],    diag(5)[1:3,])
224    assert.EQ.mat(m2[,c(4,1)], diag(5)[,c(4,1)])
225    stopifnot(identical(m2[1:3,], as(m1[1:3,], "CsparseMatrix")),
226              identical(Matrix:::uniqTsparse(m1[, c(4,2)]),
227                        Matrix:::uniqTsparse(as(m2[, c(4,2)], "TsparseMatrix")))
228              )## 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
235    stopifnot(identical(M, Diagonal(x=c(0, rep(1,4)))))
236    M <- m0; M[,3] <- 3 ; M ; stopifnot(is(M, "sparseMatrix"), M[,3] == 3)
237    validObject(M)
238    M <- m0; M[1:3, 3] <- 0 ;M
239    T <- m0; T[1:3, 3] <- 10
240    stopifnot(identical(M, Diagonal(x=c(1,1, 0, 1,1))),
241              is(T, "triangularMatrix"), identical(T[,3], c(10,10,10,0,0)))
242    
243    M <- m1; M[1,] <- 0 ; M ; assert.EQ.mat(M, diag(c(0,rep(1,4))), tol=0)
244    M <- m1; M[,3] <- 3 ; stopifnot(is(M,"sparseMatrix"), M[,3] == 3)
245    validObject(M)
246    M <- m1; M[1:3, 3] <- 0 ;M
247    assert.EQ.mat(M, diag(c(1,1, 0, 1,1)), tol=0)
248    T <- m1; T[1:3, 3] <- 10; validObject(T)
249    stopifnot(is(T, "dtTMatrix"), identical(T[,3], c(10,10,10,0,0)))
250    
251    M <- m2; M[1,] <- 0 ; M ; assert.EQ.mat(M, diag(c(0,rep(1,4))), tol=0)
252    M <- m2; M[,3] <- 3 ; stopifnot(is(M,"sparseMatrix"), M[,3] == 3)
253    validObject(M)
254    M <- m2; M[1:3, 3] <- 0 ;M
255    assert.EQ.mat(M, diag(c(1,1, 0, 1,1)), tol=0)
256    T <- m2; T[1:3, 3] <- 10; validObject(T)
257    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]
# Line 158  Line 349 
349  mc[1,4] <- -99 ; stopifnot(mc[1,4] == -99)  mc[1,4] <- -99 ; stopifnot(mc[1,4] == -99)
350  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)
351    
 ## Debugging:  R bug --   debug(Matrix:::replCmat)  has no effect  
   
352  mc[-1, 3] <- -2:1 # 0 should not be entered; 'value' recycled  mc[-1, 3] <- -2:1 # 0 should not be entered; 'value' recycled
353  mt[-1, 3] <- -2:1  mt[-1, 3] <- -2:1
354  stopifnot(mc@x != 0, mt@x != 0,  stopifnot(mc@x != 0, mt@x != 0,
355            mc[-1,3] == -2:1, mt[-1,3] == -2:1) ##--> BUG -- fixed            mc[-1,3] == -2:1, mt[-1,3] == -2:1) ## failed earlier
356  ## source("~/R/Pkgs/Matrix/R/Tsparse.R")  
357  ## Matrix_expand_pointers <- Matrix:::Matrix_expand_pointers  mc0 <- mc
358  ## -> open ../R/dgCMatrix.R  --> replCmat  .. now eval-line by line ..  mt0 <- as(mc0, "TsparseMatrix")
359    m0  <- as(mc0, "matrix")
360  ev <- 1:5 %% 2 == 0  set.seed(1)
361  mc[ev, 3] <- 0:1  for(i in 1:50) {
362  ##FIXME stopifnot(mc[ev, 3] == 0:1) ##-> BUG  {very peculiar; the 2nd time it works ...}      mc <- mc0; mt <- mt0 ; m <- m0
363  validObject(mc)      ev <- 1:5 %% 2 == round(runif(1))# 0 or 1
364  mc # now shows a non-structural zeros      j <- sample(ncol(mc), 1 + round(runif(1)))
365        nv <- rpois(sum(ev) * length(j), lambda = 1)
366        mc[ev, j] <- nv
367         m[ev, j] <- nv
368        mt[ev, j] <- nv
369        if(i %% 10 == 1) print(mc[ev,j, drop = FALSE])
370        stopifnot(as.vector(mc[ev, j]) == nv, ## failed earlier...
371                  as.vector(mt[ev, j]) == nv)
372        validObject(mc) ; assert.EQ.mat(mc, m)
373        validObject(mt) ; assert.EQ.mat(mt, m)
374    }
375    
376    mc # no longer has non-structural zeros
377  mc[ii, jj] <- 1:6  mc[ii, jj] <- 1:6
378  mc[c(2,5), c(3,5)] <- 3.2  mc[c(2,5), c(3,5)] <- 3.2
379  validObject(mc)  validObject(mc)
380  (m. <- mc)  m. <- mc
381  ## FIXME: mc[4,] <- 0 # -> error -- another Bug  mc[4,] <- 0
382    mc
383    
384    S <- as(Diagonal(5),"sparseMatrix")
385  H <- Hilbert(9)  H <- Hilbert(9)
386  Hc <- as(round(H, 3), "dsCMatrix")  Hc <- as(round(H, 3), "dsCMatrix")# a sparse matrix with no 0 ...
387  tril(Hc[1:5, 1:5])  (trH <- tril(Hc[1:5, 1:5]))
388    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  H[c(1:2, 4, 6:7), c(2:4,6)] <- 0  i <- c(1:2, 4, 6:7); j <- c(2:4,6)
417    H[i,j] <- 0
418  (H. <- round(as(H, "sparseMatrix"), 3)[ , 2:7])  (H. <- round(as(H, "sparseMatrix"), 3)[ , 2:7])
419  Hc. <- Hc  Hc. <- Hc
420  Hc.[c(1:2, 4, 6:7), c(2:4,6)] <- 0  Hc.[i,j] <- 0 ## now "works", but setting "non-structural" 0s
421    stopifnot(as.matrix(Hc.[i,j]) == 0)
422  Hc.[, 1:6]  Hc.[, 1:6]
423    
424    ## an example that failed for a long time
425    sy3 <- new("dsyMatrix", Dim = as.integer(c(2, 2)), x = c(14, -1, 2, -7))
426    validObject(dm <- kronecker(Diagonal(2), sy3))# now sparse with new kronecker
427    dm <- Matrix(as.matrix(dm))# -> "dsyMatrix"
428    (s2 <- as(dm, "sparseMatrix"))
429    validObject(st <- as(s2, "TsparseMatrix"))
430    stopifnot(is(s2, "symmetricMatrix"),
431              is(st, "symmetricMatrix"))
432    validObject(s.32  <- st[1:3,1:2]) ## 3 x 2 - and *not* dsTMatrix
433    validObject(s2.32 <- s2[1:3,1:2])
434    I <- c(1,4:3)
435    stopifnot(is(s2.32, "generalMatrix"),
436              is(s.32,  "generalMatrix"),
437              identical(as.mat(s.32), as.mat(s2.32)),
438              identical3(dm[1:3,-1], asD(s2[1:3,-1]), asD(st[1:3,-1])),
439              identical4(2, dm[4,3], s2[4,3], st[4,3]),
440              identical3(diag(dm), diag(s2), diag(st)),
441              is((cI <- s2[I,I]), "dsCMatrix"),
442              is((tI <- st[I,I]), "dsTMatrix"),
443              identical4(as.mat(dm)[I,I], as.mat(dm[I,I]), as.mat(tI), as.mat(cI))
444              )
445    
446    ## now sub-assign  and check for consistency
447    ## symmetric subassign should keep symmetry
448    st[I,I] <- 0; validObject(st); stopifnot(is(st,"symmetricMatrix"))
449    s2[I,I] <- 0; validObject(s2); stopifnot(is(s2,"symmetricMatrix"))
450    ##
451    m <- as.mat(st)
452     m[2:1,2:1] <- 4:1
453    st[2:1,2:1] <- 4:1
454    s2[2:1,2:1] <- 4:1
455    stopifnot(identical(m, as.mat(st)),
456              1:4 == as.vector(s2[1:2,1:2]),
457              identical(m, as.mat(s2)))
458    
459    ## now a slightly different situation for 's2' (had bug)
460    s2 <- as(dm, "sparseMatrix")
461    s2[I,I] <- 0; diag(s2)[2:3] <- -(1:2)
462    stopifnot(is(s2,"symmetricMatrix"), diag(s2) == c(0:-2,0))
463    t2 <- as(s2, "TsparseMatrix")
464    m <- as.mat(s2)
465    s2[2:1,2:1] <- 4:1
466    t2[2:1,2:1] <- 4:1
467     m[2:1,2:1] <- 4:1
468    assert.EQ.mat(t2, m)
469    assert.EQ.mat(s2, m)
470    ## and the same (for a different s2 !)
471    s2[2:1,2:1] <- 4:1
472    t2[2:1,2:1] <- 4:1
473    assert.EQ.mat(t2, m)# ok
474    assert.EQ.mat(s2, m)# failed in 0.9975-8
475    
476    
477    ## m[cbind(i,j)] <- value:
478    m.[ cbind(3:5, 1:3) ] <- 1:3
479    stopifnot(m.[3,1] == 1, m.[4,2] == 2)
480    x.x[ cbind(2:6, 2:6)] <- 12:16
481    validObject(x.x)
482    stopifnot(class(x.x) == "dsCMatrix",
483              12:16 == as.mat(x.x)[cbind(2:6, 2:6)])
484    (ne1 <- (mc - m.) != 0)
485    stopifnot(identical(ne1, 0 != abs(mc - m.)))
486    (ge <- m. >= mc) # contains "=" -> result is dense
487    ne. <- mc != m.  # was wrong (+ warning)
488    stopifnot(identical(!(m. < mc), m. >= mc),
489              identical(m. < mc, as(!ge, "sparseMatrix")),
490              identical(ne., drop0(ne1)))
491    
492    (M3 <- Matrix(upper.tri(matrix(, 3, 3)))) # ltC; indexing used to fail
493    T3 <- as(M3, "TsparseMatrix")
494    stopifnot(identical(drop(M3), M3),
495              identical4(drop(M3[,2, drop = FALSE]), M3[,2, drop = TRUE],
496                         drop(T3[,2, drop = FALSE]), T3[,2, drop = TRUE]),
497              is(T3, "triangularMatrix"),
498              !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.1347  
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