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 1825, Thu Apr 12 13:21:51 2007 UTC revision 1835, Fri May 11 10:41:21 2007 UTC
# Line 82  Line 82 
82  mC[7,  drop = FALSE]  mC[7,  drop = FALSE]
83  assert.EQ.mat(mC[1:2,], mm[1:2,])  assert.EQ.mat(mC[1:2,], mm[1:2,])
84    
85  ## *repeated* indices - did not work at all ...  ## *repeated* (aka 'duplicated') indices - did not work at all ...
86  i <- rep(8:10,2)  i <- rep(8:10,2)
87  j <- c(2:4, 4:3)  j <- c(2:4, 4:3)
88  assert.EQ.mat(mC[i,], mm[i,])  assert.EQ.mat(mC[i,], mm[i,])
89  assert.EQ.mat(mC[,j], mm[,j])  assert.EQ.mat(mC[,j], mm[,j])
90  if(FALSE) ## FIXME  assert.EQ.mat(mC[i, 2:1], mm[i, 2:1])
91    assert.EQ.mat(mC[c(4,1,2:1), j], mm[c(4,1,2:1), j])
92  assert.EQ.mat(mC[i,j], mm[i,j])  assert.EQ.mat(mC[i,j], mm[i,j])
93    set.seed(7)
94    for(n in 1:50) {
95        i <- sample(sample(nrow(mC), 7), 20, replace = TRUE)
96        j <- sample(sample(ncol(mC), 6), 17, replace = TRUE)
97        assert.EQ.mat(mC[i,j], mm[i,j])
98    }
99    
100    ##---- Symmetric indexing of symmetric Matrix ----------
101    m. <- mC; m.[, c(2, 7:12)] <- 0
102    validObject(S <- crossprod(add.simpleDimnames(m.) %% 100))
103    ss <- as(S, "matrix")
104    T <- as(S, "TsparseMatrix")
105    ## non-repeated indices:
106    i <- c(7:5, 2:4);assert.EQ.mat(T[i,i], ss[i,i])
107    N <- nrow(T)
108    set.seed(11)
109    for(n in 1:50) {
110        i <- sample(N, max(2, sample(N,1)), replace = FALSE)
111        validObject(Tii <- T[i,i])
112        stopifnot(is(Tii, "dsTMatrix"), # remained symmetric Tsparse
113                  identical(t(Tii), t(T)[i,i]))
114        assert.EQ.mat(Tii, ss[i,i])
115    }
116    
117    ## repeated ones ``the challenge'' (to do smartly):
118    j <- c(4, 4, 9, 12, 9, 4, 17, 3, 18, 4, 12, 18, 4, 9)
119    assert.EQ.mat(T[j,j], ss[j,j])
120    ## and another two sets  (a, A) &  (a., A.) :
121    a <- matrix(0, 6,6)
122    a[upper.tri(a)] <- (utr <- c(2, 0,-1, 0,0,5, 7,0,0,0, 0,0,-2,0,8))
123    ta <- t(a); ta[upper.tri(a)] <- utr; a <- t(ta)
124    diag(a) <- c(0,3,0,4,6,0)
125    A <- as(Matrix(a), "TsparseMatrix")
126    A. <- A
127    diag(A.) <- 10 * (1:6)
128    a. <- as(A., "matrix")
129    ## More testing {this was not working for a long time..}
130    set.seed(1)
131    for(n in 1:100) {
132        i <- sample(1:nrow(A), 3+2*rpois(1, lam=3), replace=TRUE)
133        Aii  <- A[i,i]
134        A.ii <- A.[i,i]
135        stopifnot(class(Aii) == class(A),
136                  class(A.ii) == class(A.))
137        assert.EQ.mat(Aii , a [i,i])
138        assert.EQ.mat(A.ii, a.[i,i])
139        assert.EQ.mat(T[i,i], ss[i,i])
140    }
141    
142    
143  stopifnot(all.equal(mC[,3], mm[,3]),  stopifnot(all.equal(mC[,3], mm[,3]),
144            identical(mC[ij], mm[ij]))            identical(mC[ij], mm[ij]))
# Line 134  Line 184 
184            identical(as.mat(l3 ), m.x[1:3, ] != 0)            identical(as.mat(l3 ), m.x[1:3, ] != 0)
185            )            )
186    
187    ##-- Sub*assignment* with repeated / duplicated index:
188    A <- Matrix(0,4,3) ; A[c(1,2,1), 2] <- 1 ; A
189    B <- A;              B[c(1,2,1), 2] <- 1:3; B
190    stopifnot(identical(unname(as.matrix(A)),
191                        local({a <- matrix(0,4,3); a[c(1,2,1), 2] <-  1 ; a})),
192              identical(unname(as.matrix(B)),
193                        local({a <- matrix(0,4,3); a[c(1,2,1), 2] <- 1:3; a})))
194    
195    
196  ## used to fail  ## used to fail
197  n <- 5 ## or much larger  n <- 5 ## or much larger
198  sm <- new("dsTMatrix", i=as.integer(1),j=as.integer(1),  sm <- new("dsTMatrix", i=as.integer(1),j=as.integer(1),

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

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