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 1215, Wed Feb 8 17:54:57 2006 UTC revision 1315, Mon Jul 10 16:08:05 2006 UTC
# Line 31  Line 31 
31  dimnames(mn) <- list(paste("r",letters[1:nrow(mn)],sep=""),  dimnames(mn) <- list(paste("r",letters[1:nrow(mn)],sep=""),
32                       LETTERS[1:ncol(mn)])                       LETTERS[1:ncol(mn)])
33  mn["rd", "D"]  mn["rd", "D"]
34  stopifnot(identical(mn["rc", "D"], mn[3,4]),  stopifnot(identical(mn["rc", "D"], mn[3,4]), mn[3,4] == 24,
35            identical(mn[, "A"], mn[,1]),            identical(mn[, "A"], mn[,1]), mn[,1] == 1:7,
36            identical(mn[c("re", "rb"), "B"], mn[c(5,2), 2])            identical(mn[c("re", "rb"), "B"], mn[c(5,2), 2])
37            )            )
38    
# Line 42  Line 42 
42  m[, 1] <- -1  m[, 1] <- -1
43  m[1:3,]  m[1:3,]
44    
45  ## TODO: more --- particularly once we have "m > 10" working!  g10 <- m [ m > 10 ]
46    stopifnot(18 == length(g10))
47    if(paste(R.version$major, R.version$minor, sep=".") >= "2.3")
48        ## Buglet in R(<= 2.2.1)'s possibleExtends()
49        stopifnot(10 == length(m[ m <= 10 ]))
50    
51    
52  ### Sparse Matrices  ### Sparse Matrices --------------------------------------
53    
54  m <- 1:800  m <- 1:800
55  set.seed(101) ; m[sample(800, 600)] <- 0  set.seed(101) ; m[sample(800, 600)] <- 0
# Line 60  Line 64 
64  mC[,1]  mC[,1]
65  mC[1:2,]  mC[1:2,]
66  mC[7,  drop = FALSE]  mC[7,  drop = FALSE]
67    assert.EQ.mat(mC[1:2,], mm[1:2,])
68    stopifnot(all.equal(mC[,3],   mm[,3]))
69    assert.EQ.mat(mC[7, , drop=FALSE], mm[7, , drop=FALSE])
70    
71  stopifnot(identical(mC[7,  drop = FALSE],  stopifnot(identical(mC[7,  drop = FALSE],
72                      mC[7,, drop = FALSE]))                      mC[7,, drop = FALSE]))
73  mT[,c(2,4)]  validObject(print(mT[,c(2,4)]))
74  mT[1,]  stopifnot(all.equal(mT[2,], mm[2,]),
75  mT[4, drop = FALSE]            ## row or column indexing in combination with t() :
76              identical(mT[2,], t(mT)[,2]),
77              identical(mT[-2,], t(t(mT)[,-2])),
78              identical(mT[c(2,5),], t(t(mT)[,c(2,5)]))
79              )
80    assert.EQ.mat(mT[4,, drop = FALSE], mm[4,, drop = FALSE])
81  stopifnot(identical3(mm[,1], mC[,1], mT[,1]),  stopifnot(identical3(mm[,1], mC[,1], mT[,1]),
82            identical3(mm[3,], mC[3,], mT[3,]),            identical3(mm[3,], mC[3,], mT[3,]),
83            identical3(mT[2,3], mC[2,3], 0),            identical3(mT[2,3], mC[2,3], 0),
# Line 77  Line 90 
90  mc <- mC[1:5, 1:7]  mc <- mC[1:5, 1:7]
91  mt <- mT[1:5, 1:7]  mt <- mT[1:5, 1:7]
92  ## sub matrix  ## sub matrix
93    assert.EQ.mat(mC[1:2, 0:3], mm[1:2, 0:3]) # test 0-index
94  stopifnot(identical(mc[-(3:5), 0:2], mC[1:2, 0:2]),  stopifnot(identical(mc[-(3:5), 0:2], mC[1:2, 0:2]),
95            identical(mt[-(3:5), 0:2], mT[1:2, 0:2]))            identical(mt[-(3:5), 0:2], mT[1:2, 0:2]),
96              identical(mC[2:3, 4],      mm[2:3, 4]))
97    assert.EQ.mat(mC[1:2,], mm[1:2,])
98  ## sub vector  ## sub vector
99  stopifnot(identical4(mc[-(1:4), ], mC[5, 1:7],  stopifnot(identical4(mc[-(1:4), ], mC[5, 1:7],
100                       mt[-(1:4), ], mT[5, 1:7]))                       mt[-(1:4), ], mT[5, 1:7]))
# Line 88  Line 104 
104  ## mixing of negative and positive must give error  ## mixing of negative and positive must give error
105  assertError(mT[-1:1,])  assertError(mT[-1:1,])
106    
107    ## Sub *Assignment* ---- now works (partially):
108    mt0 <- mt
109    mt[1, 4] <- -99
110    mt[2:3, 1:6] <- 0
111    mt
112    m2 <- mt+mt
113    m2[1,4] <- -200
114    m2[c(1,3), c(5:6,2)] <- 1:6
115    stopifnot(m2[1,4] == -200,
116              as.vector(m2[c(1,3), c(5:6,2)]) == 1:6)
117    mt[,3] <- 30
118    mt[2:3,] <- 250
119    mt[1:5 %% 2 == 1, 3] <- 0
120    mt[3:1, 1:7 > 5] <- 0
121    mt
122    
123    tt <- as(mt,"matrix")
124    ii <- c(0,2,5)
125    jj <- c(2:3,5)
126    tt[ii, jj] <- 1:6 # 0 is just "dropped"
127    mt[ii, jj] <- 1:6
128    assert.EQ.mat(mt, tt)
129    
130    mt[1:5, 2:6]
131    as((mt0 - mt)[1:5,], "dsparseMatrix")# [1,5] and lines 2:3
132    
133    mt[c(2,4), ] <- 0; stopifnot(as(mt[c(2,4), ],"matrix") == 0)
134    mt[2:3, 4:7] <- 33
135    validObject(mt)
136    mt
137    
138    mc[1,4] <- -99 ; stopifnot(mc[1,4] == -99)
139    mc[1,4] <-  00 ; stopifnot(mc[1,4] ==  00)
140    mc[1,4] <- -99 ; stopifnot(mc[1,4] == -99)
141    mc[1:2,4:3] <- 4:1; stopifnot(as.matrix(mc[1:2,4:3]) == 4:1)
142    
143    ## Debugging:  R bug --   debug(Matrix:::replCmat)  has no effect
144    
145    mc[-1, 3] <- -2:1 # 0 should not be entered; 'value' recycled
146    mt[-1, 3] <- -2:1
147    stopifnot(mc@x != 0, mt@x != 0,
148              mc[-1,3] == -2:1, mt[-1,3] == -2:1) ##--> BUG -- fixed
149    ## source("~/R/Pkgs/Matrix/R/Tsparse.R")
150    ## Matrix_expand_pointers <- Matrix:::Matrix_expand_pointers
151    ## -> open ../R/dgCMatrix.R  --> replCmat  .. now eval-line by line ..
152    
153    ev <- 1:5 %% 2 == 0
154    mc[ev, 3] <- 0:1
155    ##FIXME stopifnot(mc[ev, 3] == 0:1) ##-> BUG  {very peculiar; the 2nd time it works ...}
156    validObject(mc)
157    mc # now shows a non-structural zeros
158    mc[ii, jj] <- 1:6
159    mc[c(2,5), c(3,5)] <- 3.2
160    validObject(mc)
161    (m. <- mc)
162    ## FIXME: mc[4,] <- 0 # -> error -- another Bug
163    
164  # now works  cat('Time elapsed: ', proc.time(),'\n') # for ``statistical reasons''
 mT[1, 4] <- -99  
 mT[1:2, 1:7]  
 ## At least these now give a nicely understandable error:  
 try(mT[2:3, ] <- 0)  
   

Legend:
Removed from v.1215  
changed lines
  Added in v.1315

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