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 1362, Thu Aug 10 09:44:55 2006 UTC
# Line 2  Line 2 
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
 identical4 <- function(a,b,c,d) identical(a,b) && identical3(b,c,d)  
6    
7  ### Dense Matrices  ### Dense Matrices
8    
# Line 21  Line 20 
20  m[,2]     # 2nd column  m[,2]     # 2nd column
21  m[,1:2]   # sub matrix of first two columns  m[,1:2]   # sub matrix of first two columns
22  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
23    m[integer(0),] #-> 0 x 4 Matrix
24    m[2:4, numeric(0)] #-> 3 x 0 Matrix
25    
26  ## logical indexing  ## logical indexing
27  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]),
28            identical(m[2,], m[(1:nrow(m)) == 2, ]),            identical(m[2,], m[(1:nrow(m)) == 2, ]),
29            identical(m[,3:4], m[, (1:4) >= 3]))            identical(m[,3:4], m[, (1:4) >= 3]))
30    
31  ## dimnames index (TODO)  ## dimnames indexing:
32    mn <- m
33    dimnames(mn) <- list(paste("r",letters[1:nrow(mn)],sep=""),
34                         LETTERS[1:ncol(mn)])
35    mn["rd", "D"]
36    stopifnot(identical(mn["rc", "D"], mn[3,4]), mn[3,4] == 24,
37              identical(mn[, "A"], mn[,1]), mn[,1] == 1:7,
38              identical(mn[c("re", "rb"), "B"], mn[c(5,2), 2])
39              )
40    
41    mo <- m
42    m[2,3] <- 100
43    m[1:2, 4] <- 200
44    m[, 1] <- -1
45    m[1:3,]
46    
47    g10 <- m [ m > 10 ]
48    stopifnot(18 == length(g10))
49    ## needs R >= 2.3.0 [Buglet in R(<= 2.2.1)'s possibleExtends()]:
50    stopifnot(10 == length(m[ m <= 10 ]))
51    
 ## TODO: more --- particularly once we have "m > 10" working!  
52    
53    ### Sparse Matrices --------------------------------------
 ### Sparse Matrices  
54    
55  m <- 1:800  m <- 1:800
56  set.seed(101) ; m[sample(800, 600)] <- 0  set.seed(101) ; m[sample(800, 600)] <- 0
# Line 47  Line 65 
65  mC[,1]  mC[,1]
66  mC[1:2,]  mC[1:2,]
67  mC[7, drop = FALSE]  mC[7, drop = FALSE]
68    assert.EQ.mat(mC[1:2,], mm[1:2,])
69  mT[,c(2,4)]  stopifnot(all.equal(mC[,3],   mm[,3]))
70  mT[1,]  assert.EQ.mat(mC[7, , drop=FALSE], mm[7, , drop=FALSE])
71  mT[4, drop = FALSE]  
72    stopifnot(dim(mC[numeric(0), ]) == c(0,20), # used to give warnings
73              dim(mC[, integer(0)]) == c(40,0),
74              identical(mC[, integer(0)], mC[, FALSE]),
75              identical(mC[7,  drop = FALSE],
76                        mC[7,, drop = FALSE]))
77    validObject(print(mT[,c(2,4)]))
78    stopifnot(all.equal(mT[2,], mm[2,]),
79              ## row or column indexing in combination with t() :
80              identical(mT[2,], t(mT)[,2]),
81              identical(mT[-2,], t(t(mT)[,-2])),
82              identical(mT[c(2,5),], t(t(mT)[,c(2,5)]))
83              )
84    assert.EQ.mat(mT[4,, drop = FALSE], mm[4,, drop = FALSE])
85  stopifnot(identical3(mm[,1], mC[,1], mT[,1]),  stopifnot(identical3(mm[,1], mC[,1], mT[,1]),
86            identical3(mm[3,], mC[3,], mT[3,]),            identical3(mm[3,], mC[3,], mT[3,]),
87            identical3(mT[2,3], mC[2,3], 0),            identical3(mT[2,3], mC[2,3], 0),
88            identical(mT[], mT),            identical(mT[], mT),
89            ## TODO: identical4() with  m[c(3,7), 2:4]            ## TODO: identical4() with  m[c(3,7), 2:4] - fail because of 'dimnames'
90            identical3(as(mC[c(3,7), 2:4],"matrix"), mm[c(3,7), 2:4],            identical3(as(mC[c(3,7), 2:4],"matrix"), mm[c(3,7), 2:4],
91                       as(mT[c(3,7), 2:4],"matrix")))                       as(mT[c(3,7), 2:4],"matrix")))
92    
93    x.x <- crossprod(mC)
94    stopifnot(class(x.x) == "dsCMatrix",
95              class(x.x. <- round(x.x / 10000)) == "dsCMatrix")
96    head(x.x.) # Note the *non*-structural 0's printed as "0"
97    ## FIXME (once we require 2.4.x or higher):
98    ##  tail(x.x., -2) # the last two lines
99    
100    lx.x <- as(x.x, "lsCMatrix") # FALSE only for "structural" 0
101    if(FALSE) { ## FIXME: needs coercion  "lsCMatrix" to "lgTMatrix"
102        lx.x[1:10, 1:10]
103        lx.x[1:3, ]
104    }
105    
106    ## --- negative indices ----------
107    mc <- mC[1:5, 1:7]
108    mt <- mT[1:5, 1:7]
109    ## sub matrix
110    assert.EQ.mat(mC[1:2, 0:3], mm[1:2, 0:3]) # test 0-index
111    stopifnot(identical(mc[-(3:5), 0:2], mC[1:2, 0:2]),
112              identical(mt[-(3:5), 0:2], mT[1:2, 0:2]),
113              identical(mC[2:3, 4],      mm[2:3, 4]))
114    assert.EQ.mat(mC[1:2,], mm[1:2,])
115    ## sub vector
116    stopifnot(identical4(mc[-(1:4), ], mC[5, 1:7],
117                         mt[-(1:4), ], mT[5, 1:7]))
118    stopifnot(identical4(mc[-(1:4), -(2:4)], mC[5, c(1,5:7)],
119                         mt[-(1:4), -(2:4)], mT[5, c(1,5:7)]))
120    
121    ## mixing of negative and positive must give error
122    assertError(mT[-1:1,])
123    
124    ## Sub *Assignment* ---- now works (partially):
125    mt0 <- mt
126    mt[1, 4] <- -99
127    mt[2:3, 1:6] <- 0
128    mt
129    m2 <- mt+mt
130    m2[1,4] <- -200
131    m2[c(1,3), c(5:6,2)] <- 1:6
132    stopifnot(m2[1,4] == -200,
133              as.vector(m2[c(1,3), c(5:6,2)]) == 1:6)
134    mt[,3] <- 30
135    mt[2:3,] <- 250
136    mt[1:5 %% 2 == 1, 3] <- 0
137    mt[3:1, 1:7 > 5] <- 0
138    mt
139    
140    tt <- as(mt,"matrix")
141    ii <- c(0,2,5)
142    jj <- c(2:3,5)
143    tt[ii, jj] <- 1:6 # 0 is just "dropped"
144    mt[ii, jj] <- 1:6
145    assert.EQ.mat(mt, tt)
146    
147    mt[1:5, 2:6]
148    as((mt0 - mt)[1:5,], "dsparseMatrix")# [1,5] and lines 2:3
149    
150    mt[c(2,4), ] <- 0; stopifnot(as(mt[c(2,4), ],"matrix") == 0)
151    mt[2:3, 4:7] <- 33
152    validObject(mt)
153    mt
154    
155    mc[1,4] <- -99 ; stopifnot(mc[1,4] == -99)
156    mc[1,4] <-  00 ; stopifnot(mc[1,4] ==  00)
157    mc[1,4] <- -99 ; stopifnot(mc[1,4] == -99)
158    mc[1:2,4:3] <- 4:1; stopifnot(as.matrix(mc[1:2,4:3]) == 4:1)
159    
160    ## Debugging:  R bug --   debug(Matrix:::replCmat)  has no effect
161    
162    mc[-1, 3] <- -2:1 # 0 should not be entered; 'value' recycled
163    mt[-1, 3] <- -2:1
164    stopifnot(mc@x != 0, mt@x != 0,
165              mc[-1,3] == -2:1, mt[-1,3] == -2:1) ##--> BUG -- fixed
166    ## source("~/R/Pkgs/Matrix/R/Tsparse.R")
167    ## Matrix_expand_pointers <- Matrix:::Matrix_expand_pointers
168    ## -> open ../R/dgCMatrix.R  --> replCmat  .. now eval-line by line ..
169    
170    ev <- 1:5 %% 2 == 0
171    mc[ev, 3] <- 0:1
172    ##FIXME stopifnot(mc[ev, 3] == 0:1) ##-> BUG  {very peculiar; the 2nd time it works ...}
173    validObject(mc)
174    mc # now shows a non-structural zeros
175    mc[ii, jj] <- 1:6
176    mc[c(2,5), c(3,5)] <- 3.2
177    validObject(mc)
178    (m. <- mc)
179    ## FIXME: mc[4,] <- 0 # -> error -- another Bug
180    
181    H <- Hilbert(9)
182    Hc <- as(round(H, 3), "dsCMatrix")
183    tril(Hc[1:5, 1:5])
184    
185    H[c(1:2, 4, 6:7), c(2:4,6)] <- 0
186    (H. <- round(as(H, "sparseMatrix"), 3)[ , 2:7])
187    Hc. <- Hc
188    Hc.[c(1:2, 4, 6:7), c(2:4,6)] <- 0
189    Hc.[, 1:6]
190    
191    cat('Time elapsed: ', proc.time(),'\n') # for ``statistical reasons''

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

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