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 2496, Sat Nov 14 17:24:42 2009 UTC revision 2517, Wed Feb 3 11:33:18 2010 UTC
# Line 9  Line 9 
9      options(error = recover, warn = 1)      options(error = recover, warn = 1)
10  } else if(FALSE) { ## MM @ testing  } else if(FALSE) { ## MM @ testing
11      options(error = recover, Matrix.verbose = TRUE, warn = 1)      options(error = recover, Matrix.verbose = TRUE, warn = 1)
12  } else options(Matrix.verbose = TRUE, warn = 1)  } else
13    options(Matrix.verbose = TRUE, warn = 1)
14    
15    
16  ### Dense Matrices  ### Dense Matrices
# Line 129  Line 130 
130  assert.EQ.mat(ds[NA,NA], ss[NA,NA])  assert.EQ.mat(ds[NA,NA], ss[NA,NA])
131  assert.EQ.mat(ds[NA,  ], ss[NA,])  assert.EQ.mat(ds[NA,  ], ss[NA,])
132  assert.EQ.mat(ds[  ,NA], ss[,NA])  assert.EQ.mat(ds[  ,NA], ss[,NA])
 stopifnot(identical(ds[2 ,NA], ss[2,NA]),  
           identical(ds[NA, 1], ss[NA, 1]))  
133  T <- as(S, "TsparseMatrix")  T <- as(S, "TsparseMatrix")
134    stopifnot(identical(ds[2 ,NA], ss[2,NA]),
135              identical(ds[NA, 1], ss[NA, 1]),
136              identical(S, as(T, "CsparseMatrix")) )
137    
138  ## non-repeated indices:  ## non-repeated indices:
139  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])
140  ## NA in indices  -- check that we get a helpful error message:  ## NA in indices  -- check that we get a helpful error message:
# Line 143  Line 146 
146  set.seed(11)  set.seed(11)
147  for(n in 1:50) {  for(n in 1:50) {
148      i <- sample(N, max(2, sample(N,1)), replace = FALSE)      i <- sample(N, max(2, sample(N,1)), replace = FALSE)
149      validObject(Tii <- T[i,i])      validObject(Tii <- T[i,i]) ; tTi <- t(T)[i,i]
150      stopifnot(is(Tii, "dsTMatrix"), # remained symmetric Tsparse      stopifnot(is(Tii, "dsTMatrix"), # remained symmetric Tsparse
151                identical(t(Tii), t(T)[i,i]))                is(tTi, "dsTMatrix"), # may not be identical when *sorted* differently
152                  identical(as(t(Tii),"CsparseMatrix"), as(tTi,"CsparseMatrix")))
153      assert.EQ.mat(Tii, ss[i,i])      assert.EQ.mat(Tii, ss[i,i])
154  }  }
155    
156    b <- diag(1:2)[,c(1,1,2,2)]
157    cb <- crossprod(b)
158    cB <- crossprod(Matrix(b, sparse=TRUE))
159    a <- matrix(0, 6, 6)
160    a[1:4, 1:4] <- cb
161    A1 <- A2 <- Matrix(0, 6, 6)#-> sparse
162    A1[1:4, 1:4] <- cb
163    A2[1:4, 1:4] <- cB
164    assert.EQ.mat(A1, a)# indeed
165    stopifnot(identical(A1, A2), is(A1, "dsCMatrix"))
166    
167  ## repeated ones ``the challenge'' (to do smartly):  ## repeated ones ``the challenge'' (to do smartly):
168  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)
169  assert.EQ.mat(T[j,j], ss[j,j])  assert.EQ.mat(T[j,j], ss[j,j])
# Line 188  Line 203 
203  validObject(print(mT[,c(2,4)]))  validObject(print(mT[,c(2,4)]))
204  stopifnot(all.equal(mT[2,], mm[2,]),  stopifnot(all.equal(mT[2,], mm[2,]),
205            ## row or column indexing in combination with t() :            ## row or column indexing in combination with t() :
206            identical(mT[2,], t(mT)[,2]),            Q.C.identical(mT[2,], t(mT)[,2]),
207            identical(mT[-2,], t(t(mT)[,-2])),            Q.C.identical(mT[-2,], t(t(mT)[,-2])),
208            identical(mT[c(2,5),], t(t(mT)[,c(2,5)]))            Q.C.identical(mT[c(2,5),], t(t(mT)[,c(2,5)])) )
           )  
209  assert.EQ.mat(mT[4,, drop = FALSE], mm[4,, drop = FALSE])  assert.EQ.mat(mT[4,, drop = FALSE], mm[4,, drop = FALSE])
210  stopifnot(identical3(mm[,1], mC[,1], mT[,1]),  stopifnot(identical3(mm[,1], mC[,1], mT[,1]),
211            identical3(mm[3,], mC[3,], mT[3,]),            identical3(mm[3,], mC[3,], mT[3,]),
# Line 272  Line 286 
286  M <- m0; M[1:3, 3] <- 0 ;M  M <- m0; M[1:3, 3] <- 0 ;M
287  T <- m0; T[1:3, 3] <- 10  T <- m0; T[1:3, 3] <- 10
288  stopifnot(identical(M, Diagonal(x=c(1,1, 0, 1,1))),  stopifnot(identical(M, Diagonal(x=c(1,1, 0, 1,1))),
289            is(T, "triangularMatrix"), identical(T[,3], c(10,10,10,0,0)))            isValid(T, "triangularMatrix"), identical(T[,3], c(10,10,10,0,0)))
290    
291  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)
292  M <- m1; M[,3] <- 3 ; stopifnot(is(M,"sparseMatrix"), M[,3] == 3)  M <- m1; M[,3] <- 3 ; stopifnot(is(M,"sparseMatrix"), M[,3] == 3)
# Line 280  Line 294 
294  M <- m1; M[1:3, 3] <- 0 ;M  M <- m1; M[1:3, 3] <- 0 ;M
295  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)
296  T <- m1; T[1:3, 3] <- 10; checkMatrix(T)  T <- m1; T[1:3, 3] <- 10; checkMatrix(T)
297  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)))
298    
299  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)
300  M <- m2; M[,3] <- 3 ; stopifnot(is(M,"sparseMatrix"), M[,3] == 3)  M <- m2; M[,3] <- 3 ; stopifnot(is(M,"sparseMatrix"), M[,3] == 3)
# Line 367  Line 381 
381    
382  ## Sub *Assignment* ---- now works (partially):  ## Sub *Assignment* ---- now works (partially):
383  mt0 <- mt  mt0 <- mt
384    nt <- as(mt, "nMatrix")
385  mt[1, 4] <- -99  mt[1, 4] <- -99
386  mt[2:3, 1:6] <- 0  mt[2:3, 1:6] <- 0
387  mt  mt
# Line 526  Line 541 
541  assert.EQ.mat(s2, m)# failed in 0.9975-8  assert.EQ.mat(s2, m)# failed in 0.9975-8
542    
543    
544  ## m[cbind(i,j)] <- value:  ## m[cbind(i,j)] <- value: (2-column matrix subassignment):
545  m.[ cbind(3:5, 1:3) ] <- 1:3  m.[ cbind(3:5, 1:3) ] <- 1:3
546  stopifnot(m.[3,1] == 1, m.[4,2] == 2)  stopifnot(m.[3,1] == 1, m.[4,2] == 2)
547    nt. <- nt ; nt[rbind(2:3, 3:4, c(3,3))] <- FALSE
548    s. <- m. ; m.[cbind(3:4,2:3)] <- 0 ## assigning 0 where there *is* 0 ..
549    stopifnot(identical(nt.,nt),       ## should not have changed
550              identical(s., m.))
551  x.x[ cbind(2:6, 2:6)] <- 12:16  x.x[ cbind(2:6, 2:6)] <- 12:16
552  stopifnot(isValid(x.x, "dsCMatrix"),  stopifnot(isValid(x.x, "dsCMatrix"),
553            12:16 == as.mat(x.x)[cbind(2:6, 2:6)])            12:16 == as.mat(x.x)[cbind(2:6, 2:6)])

Legend:
Removed from v.2496  
changed lines
  Added in v.2517

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