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 873, Sat Aug 27 21:26:23 2005 UTC revision 2186, Mon Apr 28 12:37:01 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  identical3 <- function(x,y,z)   identical(x,y) && identical (y,z)  source(system.file("test-tools.R", package = "Matrix"))# identical3() etc
6  identical4 <- function(a,b,c,d) identical(a,b) && identical3(b,c,d)  
7    if(interactive()) {
8        options(error = recover)
9    } else options(verbose = TRUE)# to show message()s
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    
19  m[2, 3:4, drop=FALSE] # sub matrix of class 'dgeMatrix'  m[2, 3:4, drop=FALSE] # sub matrix of class 'dgeMatrix'
20  m[-(4:7), 3:4]        # dito; the upper right corner of 'm'  m[-(4:7), 3:4]        # ditto; the upper right corner of 'm'
21    
22  ## rows or columns only:  ## rows or columns only:
23  m[1,]     # first row, as simple numeric vector  m[1,]     # first row, as simple numeric vector
24  m[,2]     # 2nd column  m[,2]     # 2nd column
25  m[,1:2]   # sub matrix of first two columns  m[,1:2]   # sub matrix of first two columns
26  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
27    m[integer(0),] #-> 0 x 4 Matrix
28    m[2:4, numeric(0)] #-> 3 x 0 Matrix
29    
30  ## logical indexing  ## logical indexing
31  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]),
32            identical(m[2,], m[(1:nrow(m)) == 2, ]),            identical(m[2,], m[(1:nrow(m)) == 2, ]),
33            identical(m[,3:4], m[, (1:4) >= 3]))            identical(m[,3:4], m[, (1:4) >= 3]))
34    
35  ## dimnames index (TODO)  ## dimnames indexing:
36    mn <- m
37  ## TODO: more --- particularly once we have "m > 10" working!  dimnames(mn) <- list(paste("r",letters[1:nrow(mn)],sep=""),
38                         LETTERS[1:ncol(mn)])
39    checkMatrix(mn)
40    mn["rd", "D"]
41    stopifnot(identical(mn["rc", "D"], mn[3,4]), mn[3,4] == 24,
42              identical(mn[, "A"], mn[,1]), mn[,1] == 1:7,
43              identical(mn[c("re", "rb"), "B"], mn[c(5,2), 2])
44              )
45    
46    mo <- m
47    m[2,3] <- 100
48    m[1:2, 4] <- 200
49    m[, 1] <- -1
50    m[1:3,]
51    
52    m. <- as.matrix(m)
53    
54    ## m[ cbind(i,j) ] indexing:
55    ij <- cbind(1:6, 2:3)
56    stopifnot(identical(m[ij], m.[ij]))
57    
58    ## testing operations on logical Matrices rather more than indexing:
59    g10 <- m [ m > 10 ]
60    stopifnot(18 == length(g10))
61    stopifnot(10 == length(m[ m <= 10 ]))
62    sel <- (20 <  m) & (m <  150)
63    sel.<- (20 <  m.)& (m.<  150)
64    nsel <-(20 >= m) | (m >= 150)
65    (ssel <- as(sel, "sparseMatrix"))
66    stopifnot(is(sel, "lMatrix"), is(ssel, "lsparseMatrix"),
67              identical3(as.mat(sel.), as.mat(sel), as.mat(ssel)),
68              identical3(!sel, !ssel, nsel), # !<sparse> is typically dense
69              identical3(m[ sel],  m[ ssel], as.matrix(m)[as.matrix( ssel)]),
70              identical3(m[!sel],  m[!ssel], as.matrix(m)[as.matrix(!ssel)])
71              )
72    
73  ### Sparse Matrices  ## more sparse Matrices --------------------------------------
74    
75  m <- 1:800  m <- 1:800
76  set.seed(101) ; m[sample(800, 600)] <- 0  set.seed(101) ; m[sample(800, 600)] <- 0
# Line 47  Line 85 
85  mC[,1]  mC[,1]
86  mC[1:2,]  mC[1:2,]
87  mC[7, drop = FALSE]  mC[7, drop = FALSE]
88    assert.EQ.mat(mC[1:2,], mm[1:2,])
89    
90  mT[,c(2,4)]  ## *repeated* (aka 'duplicated') indices - did not work at all ...
91  mT[1,]  i <- rep(8:10,2)
92  mT[4, drop = FALSE]  j <- c(2:4, 4:3)
93    assert.EQ.mat(mC[i,], mm[i,])
94    assert.EQ.mat(mC[,j], mm[,j])
95    assert.EQ.mat(mC[i, 2:1], mm[i, 2:1])
96    assert.EQ.mat(mC[c(4,1,2:1), j], mm[c(4,1,2:1), j])
97    assert.EQ.mat(mC[i,j], mm[i,j])
98    set.seed(7)
99    for(n in 1:50) {
100        i <- sample(sample(nrow(mC), 7), 20, replace = TRUE)
101        j <- sample(sample(ncol(mC), 6), 17, replace = TRUE)
102        assert.EQ.mat(mC[i,j], mm[i,j])
103    }
104    
105    ##---- Symmetric indexing of symmetric Matrix ----------
106    m. <- mC; m.[, c(2, 7:12)] <- 0
107    validObject(S <- crossprod(add.simpleDimnames(m.) %% 100))
108    ss <- as(S, "matrix")
109    T <- as(S, "TsparseMatrix")
110    ## non-repeated indices:
111    i <- c(7:5, 2:4);assert.EQ.mat(T[i,i], ss[i,i])
112    N <- nrow(T)
113    set.seed(11)
114    for(n in 1:50) {
115        i <- sample(N, max(2, sample(N,1)), replace = FALSE)
116        validObject(Tii <- T[i,i])
117        stopifnot(is(Tii, "dsTMatrix"), # remained symmetric Tsparse
118                  identical(t(Tii), t(T)[i,i]))
119        assert.EQ.mat(Tii, ss[i,i])
120    }
121    
122    ## repeated ones ``the challenge'' (to do smartly):
123    j <- c(4, 4, 9, 12, 9, 4, 17, 3, 18, 4, 12, 18, 4, 9)
124    assert.EQ.mat(T[j,j], ss[j,j])
125    ## and another two sets  (a, A) &  (a., A.) :
126    a <- matrix(0, 6,6)
127    a[upper.tri(a)] <- (utr <- c(2, 0,-1, 0,0,5, 7,0,0,0, 0,0,-2,0,8))
128    ta <- t(a); ta[upper.tri(a)] <- utr; a <- t(ta)
129    diag(a) <- c(0,3,0,4,6,0)
130    A <- as(Matrix(a), "TsparseMatrix")
131    A. <- A
132    diag(A.) <- 10 * (1:6)
133    a. <- as(A., "matrix")
134    ## More testing {this was not working for a long time..}
135    set.seed(1)
136    for(n in 1:100) {
137        i <- sample(1:nrow(A), 3+2*rpois(1, lam=3), replace=TRUE)
138        Aii  <- A[i,i]
139        A.ii <- A.[i,i]
140        stopifnot(class(Aii) == class(A),
141                  class(A.ii) == class(A.))
142        assert.EQ.mat(Aii , a [i,i])
143        assert.EQ.mat(A.ii, a.[i,i])
144        assert.EQ.mat(T[i,i], ss[i,i])
145    }
146    
147    
148    stopifnot(all.equal(mC[,3], mm[,3]),
149              identical(mC[ij], mm[ij]))
150    assert.EQ.mat(mC[7, , drop=FALSE], mm[7, , drop=FALSE])
151    
152    stopifnot(dim(mC[numeric(0), ]) == c(0,20), # used to give warnings
153              dim(mC[, integer(0)]) == c(40,0),
154              identical(mC[, integer(0)], mC[, FALSE]),
155              identical(mC[7,  drop = FALSE],
156                        mC[7,, drop = FALSE]))
157    validObject(print(mT[,c(2,4)]))
158    stopifnot(all.equal(mT[2,], mm[2,]),
159              ## row or column indexing in combination with t() :
160              identical(mT[2,], t(mT)[,2]),
161              identical(mT[-2,], t(t(mT)[,-2])),
162              identical(mT[c(2,5),], t(t(mT)[,c(2,5)]))
163              )
164    assert.EQ.mat(mT[4,, drop = FALSE], mm[4,, drop = FALSE])
165  stopifnot(identical3(mm[,1], mC[,1], mT[,1]),  stopifnot(identical3(mm[,1], mC[,1], mT[,1]),
166            identical3(mm[3,], mC[3,], mT[3,]),            identical3(mm[3,], mC[3,], mT[3,]),
167            identical3(mT[2,3], mC[2,3], 0),            identical3(mT[2,3], mC[2,3], 0),
168            identical(mT[], mT),            identical(mT[], mT),
169            ## TODO: identical4() with  m[c(3,7), 2:4]            identical4(       mm[c(3,7), 2:4],  as.mat( m[c(3,7), 2:4]),
170            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]))
171                       as(mT[c(3,7), 2:4],"matrix")))            )
172    
173    x.x <- crossprod(mC)
174    stopifnot(class(x.x) == "dsCMatrix",
175              class(x.x. <- round(x.x / 10000)) == "dsCMatrix",
176              identical(x.x[cbind(2:6, 2:6)],
177                        diag(x.x [2:6, 2:6])))
178    head(x.x.) # Note the *non*-structural 0's printed as "0"
179    tail(x.x., -3) # all but the first three lines
180    
181    lx.x <- as(x.x, "lsCMatrix") # FALSE only for "structural" 0
182    (l10 <- lx.x[1:10, 1:10])# "lsC"
183    (l3 <-  lx.x[1:3, ])
184    m.x <- as.mat(x.x) # as.mat() *drops* (NULL,NULL) dimnames
185    stopifnot(class(l10) == "lsCMatrix", # symmetric indexing -> symmetric !
186              identical(as.mat(lx.x), m.x != 0),
187              identical(as.logical(lx.x), as.logical(m.x)),
188              identical(as.mat(l10), m.x[1:10, 1:10] != 0),
189              identical(as.mat(l3 ), m.x[1:3, ] != 0)
190              )
191    
192    ##-- Sub*assignment* with repeated / duplicated index:
193    A <- Matrix(0,4,3) ; A[c(1,2,1), 2] <- 1 ; A
194    B <- A;              B[c(1,2,1), 2] <- 1:3; B; B. <- B
195    B.[3,] <- rbind(4:2)
196    diag(B.) <- 10 * diag(B.)
197    C <- B.; C[,2] <- C[,2];  C[1,] <- C[1,]; C[2:3,2:1] <- C[2:3,2:1]
198    stopifnot(identical(unname(as.matrix(A)),
199                        local({a <- matrix(0,4,3); a[c(1,2,1), 2] <-  1 ; a})),
200              identical(unname(as.matrix(B)),
201                        local({a <- matrix(0,4,3); a[c(1,2,1), 2] <- 1:3; a})),
202              identical(C, drop0(B.)))
203    
204    
205    ## used to fail
206    n <- 5 ## or much larger
207    sm <- new("dsTMatrix", i=as.integer(1),j=as.integer(1),
208              Dim=as.integer(c(n,n)), x = 1)
209    (cm <- as(sm, "CsparseMatrix"))
210    sm[2,]
211    stopifnot(sm[2,] == c(0:1, rep.int(0,ncol(sm)-2)),
212              sm[2,] == cm[2,],
213              sm[,3] == sm[3,],
214              all(sm[,-(1:3)] == t(sm[-(1:3),])), # all(<lge.>)
215              all(sm[,-(1:3)] == 0)
216              )
217    
218    m0 <- Diagonal(5)
219    stopifnot(identical(m0[2,], m0[,2]),
220              identical(m0[,1], c(1,0,0,0,0)))
221    ### Diagonal -- Sparse:
222    (m1 <- as(m0, "sparseMatrix"))  # dtTMatrix
223    (m2 <- as(m0, "CsparseMatrix")) # dtCMatrix (with an irrelevant warning)
224    m1g <- as(m1, "generalMatrix")
225    stopifnot(is(m1g, "dgTMatrix"))
226    assert.EQ.mat(m2[1:3,],    diag(5)[1:3,])
227    assert.EQ.mat(m2[,c(4,1)], diag(5)[,c(4,1)])
228    stopifnot(identical(m2[1:3,], as(m1[1:3,], "CsparseMatrix")),
229              identical(Matrix:::uniqTsparse(m1[, c(4,2)]),
230                        Matrix:::uniqTsparse(as(m2[, c(4,2)], "TsparseMatrix")))
231              )## failed in 0.9975-11
232    
233    (uTr <- new("dtTMatrix", Dim = c(3L,3L), diag="U"))
234    uTr[1,] <- 0
235    assert.EQ.mat(uTr, cbind(0, rbind(0,diag(2))))
236    
237    M <- m0; M[1,] <- 0
238    stopifnot(identical(M, Diagonal(x=c(0, rep(1,4)))))
239    M <- m0; M[,3] <- 3 ; M ; stopifnot(is(M, "sparseMatrix"), M[,3] == 3)
240    checkMatrix(M)
241    M <- m0; M[1:3, 3] <- 0 ;M
242    T <- m0; T[1:3, 3] <- 10
243    stopifnot(identical(M, Diagonal(x=c(1,1, 0, 1,1))),
244              is(T, "triangularMatrix"), identical(T[,3], c(10,10,10,0,0)))
245    
246    M <- m1; M[1,] <- 0 ; M ; assert.EQ.mat(M, diag(c(0,rep(1,4))), tol=0)
247    M <- m1; M[,3] <- 3 ; stopifnot(is(M,"sparseMatrix"), M[,3] == 3)
248    checkMatrix(M)
249    M <- m1; M[1:3, 3] <- 0 ;M
250    assert.EQ.mat(M, diag(c(1,1, 0, 1,1)), tol=0)
251    T <- m1; T[1:3, 3] <- 10; checkMatrix(T)
252    stopifnot(is(T, "dtTMatrix"), identical(T[,3], c(10,10,10,0,0)))
253    
254    M <- m2; M[1,] <- 0 ; M ; assert.EQ.mat(M, diag(c(0,rep(1,4))), tol=0)
255    M <- m2; M[,3] <- 3 ; stopifnot(is(M,"sparseMatrix"), M[,3] == 3)
256    checkMatrix(M)
257    M <- m2; M[1:3, 3] <- 0 ;M
258    assert.EQ.mat(M, diag(c(1,1, 0, 1,1)), tol=0)
259    T <- m2; T[1:3, 3] <- 10; checkMatrix(T)
260    stopifnot(is(T, "dtCMatrix"), identical(T[,3], c(10,10,10,0,0)))
261    
262    
263    ## "Vector indices" -------------------
264    D <- Diagonal(6)
265    M <- as(D,"dgeMatrix")
266    m <- as(D,"matrix")
267    s <- as(D,"TsparseMatrix")
268    S <- as(s,"CsparseMatrix")
269    i <- c(3,1,6); v <- c(10,15,20)
270    ## (logical,value) which both are recycled:
271    L <- c(TRUE, rep(FALSE,8)) ; z <- c(50,99)
272    
273    ## vector subassignment, both with integer & logical
274    ## these now work correctly {though not very efficiently; hence warnings}
275    m[i] <- v # the role model: only first column is affected
276    M[i] <- v; assert.EQ.mat(M,m) # dge
277    D[i] <- v; assert.EQ.mat(D,m) # ddi -> dtT -> dgT
278    s[i] <- v; assert.EQ.mat(s,m) # dtT -> dgT
279    S[i] <- v; assert.EQ.mat(S,m); S # dtC -> dtT -> dgT -> dgC
280    ## logical
281    m[L] <- z
282    M[L] <- z; assert.EQ.mat(M,m)
283    D[L] <- z; assert.EQ.mat(D,m)
284    s[L] <- z; assert.EQ.mat(s,m)
285    S[L] <- z; assert.EQ.mat(S,m) ; S
286    
287    ## indexing [i]  vs  [i,] --- now ok
288    stopifnot(identical4(m[i], M[i], D[i], s[i]), identical(s[i],S[i]))
289    stopifnot(identical4(m[L], M[L], D[L], s[L]), identical(s[L],S[L]))
290    assert.EQ.mat(D[i,], m[i,])
291    assert.EQ.mat(M[i,], m[i,])
292    assert.EQ.mat(s[i,], m[i,])
293    assert.EQ.mat(S[i,], m[i,])
294    
295    assert.EQ.mat(D[,i], m[,i])
296    assert.EQ.mat(M[,i], m[,i])
297    assert.EQ.mat(s[,i], m[,i])
298    assert.EQ.mat(S[,i], m[,i])
299    
300    
301    ## --- negative indices ----------
302    mc <- mC[1:5, 1:7]
303    mt <- mT[1:5, 1:7]
304    ## sub matrix
305    assert.EQ.mat(mC[1:2, 0:3], mm[1:2, 0:3]) # test 0-index
306    stopifnot(identical(mc[-(3:5), 0:2], mC[1:2, 0:2]),
307              identical(mt[-(3:5), 0:2], mT[1:2, 0:2]),
308              identical(mC[2:3, 4],      mm[2:3, 4]))
309    assert.EQ.mat(mC[1:2,], mm[1:2,])
310    ## sub vector
311    stopifnot(identical4(mc[-(1:4), ], mC[5, 1:7],
312                         mt[-(1:4), ], mT[5, 1:7]))
313    stopifnot(identical4(mc[-(1:4), -(2:4)], mC[5, c(1,5:7)],
314                         mt[-(1:4), -(2:4)], mT[5, c(1,5:7)]))
315    
316    ## mixing of negative and positive must give error
317    assertError(mT[-1:1,])
318    
319    ## Sub *Assignment* ---- now works (partially):
320    mt0 <- mt
321    mt[1, 4] <- -99
322    mt[2:3, 1:6] <- 0
323    mt
324    m2 <- mt+mt
325    m2[1,4] <- -200
326    m2[c(1,3), c(5:6,2)] <- 1:6
327    stopifnot(m2[1,4] == -200,
328              as.vector(m2[c(1,3), c(5:6,2)]) == 1:6)
329    mt[,3] <- 30
330    mt[2:3,] <- 250
331    mt[1:5 %% 2 == 1, 3] <- 0
332    mt[3:1, 1:7 > 5] <- 0
333    mt
334    
335    tt <- as(mt,"matrix")
336    ii <- c(0,2,5)
337    jj <- c(2:3,5)
338    tt[ii, jj] <- 1:6 # 0 is just "dropped"
339    mt[ii, jj] <- 1:6
340    assert.EQ.mat(mt, tt)
341    
342    mt[1:5, 2:6]
343    as((mt0 - mt)[1:5,], "dsparseMatrix")# [1,5] and lines 2:3
344    
345    mt[c(2,4), ] <- 0; stopifnot(as(mt[c(2,4), ],"matrix") == 0)
346    mt[2:3, 4:7] <- 33
347    checkMatrix(mt)
348    mt
349    
350    mc[1,4] <- -99 ; stopifnot(mc[1,4] == -99)
351    mc[1,4] <-  00 ; stopifnot(mc[1,4] ==  00)
352    mc[1,4] <- -99 ; stopifnot(mc[1,4] == -99)
353    mc[1:2,4:3] <- 4:1; stopifnot(as.matrix(mc[1:2,4:3]) == 4:1)
354    
355    mc[-1, 3] <- -2:1 # 0 should not be entered; 'value' recycled
356    mt[-1, 3] <- -2:1
357    stopifnot(mc@x != 0, mt@x != 0,
358              mc[-1,3] == -2:1, mt[-1,3] == -2:1) ## failed earlier
359    
360    mc0 <- mc
361    mt0 <- as(mc0, "TsparseMatrix")
362    m0  <- as(mc0, "matrix")
363    set.seed(1)
364    for(i in 1:50) {
365        mc <- mc0; mt <- mt0 ; m <- m0
366        ev <- 1:5 %% 2 == round(runif(1))# 0 or 1
367        j <- sample(ncol(mc), 1 + round(runif(1)))
368        nv <- rpois(sum(ev) * length(j), lambda = 1)
369        mc[ev, j] <- nv
370         m[ev, j] <- nv
371        mt[ev, j] <- nv
372        if(i %% 10 == 1) print(mc[ev,j, drop = FALSE])
373        stopifnot(as.vector(mc[ev, j]) == nv, ## failed earlier...
374                  as.vector(mt[ev, j]) == nv)
375        validObject(mc) ; assert.EQ.mat(mc, m)
376        validObject(mt) ; assert.EQ.mat(mt, m)
377    }
378    
379    mc # no longer has non-structural zeros
380    mc[ii, jj] <- 1:6
381    mc[c(2,5), c(3,5)] <- 3.2
382    checkMatrix(mc)
383    m. <- mc
384    mc[4,] <- 0
385    mc
386    
387    S <- as(Diagonal(5),"sparseMatrix")
388    H <- Hilbert(9)
389    Hc <- as(round(H, 3), "dsCMatrix")# a sparse matrix with no 0 ...
390    (trH <- tril(Hc[1:5, 1:5]))
391    stopifnot(is(trH, "triangularMatrix"), trH@uplo == "L",
392              is(S, "triangularMatrix"))
393    
394    ## triangular assignment
395    ## the slick (but inefficient in case of sparse!) way to assign sub-diagonals:
396    ## equivalent to tmp <- `diag<-`(S[,-1], -2:1); S[,-1] <- tmp
397    ## which dispatches to (x="TsparseMatrix", i="missing",j="index", value="replValue")
398    diag(S[,-1]) <- -2:1 # used to give a wrong warning
399    S <- as(S,"triangularMatrix")
400    assert.EQ.mat(S, local({s <- diag(5); diag(s[,-1]) <- -2:1; s}))
401    
402    trH[c(1:2,4), c(2:3,5)] <- 0 # gave an *error* upto Jan.2008
403    trH[ lower.tri(trH) ] <- 0   # ditto, because of callNextMethod()
404    
405    m <- Matrix(0+1:28, nrow = 4)
406    m[-3,c(2,4:5,7)] <- m[ 3, 1:4] <- m[1:3, 6] <- 0
407    mT <- as(m, "dgTMatrix")
408    stopifnot(identical(mT[lower.tri(mT)],
409                        m [lower.tri(m) ]))
410    lM <- upper.tri(mT, diag=TRUE)
411    mT[lM] <- 0
412     m[lM] <- 0
413    assert.EQ.mat(mT, as(m,"matrix"))
414    mT[lM] <- -1:0
415     m[lM] <- -1:0
416    assert.EQ.mat(mT, as(m,"matrix"))
417    (mT <- drop0(mT))
418    
419    i <- c(1:2, 4, 6:7); j <- c(2:4,6)
420    H[i,j] <- 0
421    (H. <- round(as(H, "sparseMatrix"), 3)[ , 2:7])
422    Hc. <- Hc
423    Hc.[i,j] <- 0 ## now "works", but setting "non-structural" 0s
424    stopifnot(as.matrix(Hc.[i,j]) == 0)
425    Hc.[, 1:6]
426    
427    ## an example that failed for a long time
428    sy3 <- new("dsyMatrix", Dim = as.integer(c(2, 2)), x = c(14, -1, 2, -7))
429    checkMatrix(dm <- kronecker(Diagonal(2), sy3))# now sparse with new kronecker
430    dm <- Matrix(as.matrix(dm))# -> "dsyMatrix"
431    (s2 <- as(dm, "sparseMatrix"))
432    checkMatrix(st <- as(s2, "TsparseMatrix"))
433    stopifnot(is(s2, "symmetricMatrix"),
434              is(st, "symmetricMatrix"))
435    checkMatrix(s.32  <- st[1:3,1:2]) ## 3 x 2 - and *not* dsTMatrix
436    checkMatrix(s2.32 <- s2[1:3,1:2])
437    I <- c(1,4:3)
438    stopifnot(is(s2.32, "generalMatrix"),
439              is(s.32,  "generalMatrix"),
440              identical(as.mat(s.32), as.mat(s2.32)),
441              identical3(dm[1:3,-1], asD(s2[1:3,-1]), asD(st[1:3,-1])),
442              identical4(2, dm[4,3], s2[4,3], st[4,3]),
443              identical3(diag(dm), diag(s2), diag(st)),
444              is((cI <- s2[I,I]), "dsCMatrix"),
445              is((tI <- st[I,I]), "dsTMatrix"),
446              identical4(as.mat(dm)[I,I], as.mat(dm[I,I]), as.mat(tI), as.mat(cI))
447              )
448    
449    ## now sub-assign  and check for consistency
450    ## symmetric subassign should keep symmetry
451    st[I,I] <- 0; checkMatrix(st); stopifnot(is(st,"symmetricMatrix"))
452    s2[I,I] <- 0; checkMatrix(s2); stopifnot(is(s2,"symmetricMatrix"))
453    ##
454    m <- as.mat(st)
455     m[2:1,2:1] <- 4:1
456    st[2:1,2:1] <- 4:1
457    s2[2:1,2:1] <- 4:1
458    stopifnot(identical(m, as.mat(st)),
459              1:4 == as.vector(s2[1:2,1:2]),
460              identical(m, as.mat(s2)))
461    
462    ## now a slightly different situation for 's2' (had bug)
463    s2 <- as(dm, "sparseMatrix")
464    s2[I,I] <- 0; diag(s2)[2:3] <- -(1:2)
465    stopifnot(is(s2,"symmetricMatrix"), diag(s2) == c(0:-2,0))
466    t2 <- as(s2, "TsparseMatrix")
467    m <- as.mat(s2)
468    s2[2:1,2:1] <- 4:1
469    t2[2:1,2:1] <- 4:1
470     m[2:1,2:1] <- 4:1
471    assert.EQ.mat(t2, m)
472    assert.EQ.mat(s2, m)
473    ## and the same (for a different s2 !)
474    s2[2:1,2:1] <- 4:1
475    t2[2:1,2:1] <- 4:1
476    assert.EQ.mat(t2, m)# ok
477    assert.EQ.mat(s2, m)# failed in 0.9975-8
478    
479    
480    ## m[cbind(i,j)] <- value:
481    m.[ cbind(3:5, 1:3) ] <- 1:3
482    stopifnot(m.[3,1] == 1, m.[4,2] == 2)
483    x.x[ cbind(2:6, 2:6)] <- 12:16
484    stopifnot(isValid(x.x, "dsCMatrix"),
485              12:16 == as.mat(x.x)[cbind(2:6, 2:6)])
486    (ne1 <- (mc - m.) != 0)
487    stopifnot(identical(ne1, 0 != abs(mc - m.)))
488    (ge <- m. >= mc) # contains "=" -> result is dense
489    ne. <- mc != m.  # was wrong (+ warning)
490    stopifnot(identical(!(m. < mc), m. >= mc),
491              identical(m. < mc, as(!ge, "sparseMatrix")),
492              identical(ne., drop0(ne1)))
493    
494    (M3 <- Matrix(upper.tri(matrix(, 3, 3)))) # ltC; indexing used to fail
495    T3 <- as(M3, "TsparseMatrix")
496    stopifnot(identical(drop(M3), M3),
497              identical4(drop(M3[,2, drop = FALSE]), M3[,2, drop = TRUE],
498                         drop(T3[,2, drop = FALSE]), T3[,2, drop = TRUE]),
499              is(T3, "triangularMatrix"),
500              !is(T3[,2, drop=FALSE], "triangularMatrix")
501              )
502    
503    (T6 <- as(as(kronecker(Matrix(c(0,0,1,0),2,2), t(T3)), "lMatrix"),
504              "triangularMatrix"))
505    T6[1:4, -(1:3)] # failed (trying to coerce back to ltTMatrix)
506    stopifnot(identical(T6[1:4, -(1:3)][2:3, -3],
507                        spMatrix(2,2, i=c(1,2,2), j=c(1,1,2), x=rep(TRUE,3))))
508    
509    M <- Diagonal(4); M[1,2] <- 2
510    M. <- as(M, "CsparseMatrix")
511    (R <- as(M., "RsparseMatrix"))
512    (Ms <- symmpart(M.))
513    Rs <- as(Ms, "RsparseMatrix")
514    stopifnot(isValid(M, "triangularMatrix"),
515              isValid(M.,"triangularMatrix"),
516              isValid(Ms, "dsCMatrix"),
517              isValid(R,  "dtRMatrix"),
518              isValid(Rs, "dsRMatrix") )
519    stopifnot(dim(M[2:3, FALSE]) == c(2,0),
520              dim(R[2:3, FALSE]) == c(2,0),
521              identical(M [2:3,TRUE], M [2:3,]),
522              identical(M.[2:3,TRUE], M.[2:3,]),
523              identical(R [2:3,TRUE], R [2:3,]),
524              dim(R[FALSE, FALSE]) == c(0,0))
525    
526    cat('Time elapsed: ', (.pt <- proc.time()),'\n') # "stats"
527    ##
528    cat("checkMatrix() of all: \n---------\n")
529    Sys.setlocale("LC_COLLATE", "C")# to keep ls() reproducible
530    for(nm in ls()) if(is(.m <- get(nm), "Matrix")) {
531        cat(nm, "\n")
532        checkMatrix(.m, verbose = FALSE)
533    }
534    cat('Time elapsed: ', proc.time() - .pt,'\n') # "stats"
535    
536    if(!interactive()) warnings()
537    

Legend:
Removed from v.873  
changed lines
  Added in v.2186

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