# SCM Repository

[matrix] Diff of /pkg/tests/indexing.R
 [matrix] / pkg / tests / indexing.R

# Diff of /pkg/tests/indexing.R

revision 1225, Mon Mar 13 14:06:17 2006 UTC revision 1226, Mon Mar 13 14:07:58 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)]  mT[,c(2,4)]
74  mT[1,]  stopifnot(all.equal(mT[1,], mm[1,]))
75  mT[4, drop = FALSE]  assert.EQ.mat(mT[4,, drop = FALSE], mm[4,, drop = FALSE])
76  stopifnot(identical3(mm[,1], mC[,1], mT[,1]),  stopifnot(identical3(mm[,1], mC[,1], mT[,1]),
77            identical3(mm[3,], mC[3,], mT[3,]),            identical3(mm[3,], mC[3,], mT[3,]),
78            identical3(mT[2,3], mC[2,3], 0),            identical3(mT[2,3], mC[2,3], 0),
# Line 77  Line 85
85  mc <- mC[1:5, 1:7]  mc <- mC[1:5, 1:7]
86  mt <- mT[1:5, 1:7]  mt <- mT[1:5, 1:7]
87  ## sub matrix  ## sub matrix
88    assert.EQ.mat(mC[1:2, 0:3], mm[1:2, 0:3]) # test 0-index
89  stopifnot(identical(mc[-(3:5), 0:2], mC[1:2, 0:2]),  stopifnot(identical(mc[-(3:5), 0:2], mC[1:2, 0:2]),
90            identical(mt[-(3:5), 0:2], mT[1:2, 0:2]))            identical(mt[-(3:5), 0:2], mT[1:2, 0:2]),
91              identical(mC[2:3, 4],      mm[2:3, 4]))
92    assert.EQ.mat(mC[1:2,], mm[1:2,])
93  ## sub vector  ## sub vector
94  stopifnot(identical4(mc[-(1:4), ], mC[5, 1:7],  stopifnot(identical4(mc[-(1:4), ], mC[5, 1:7],
95                       mt[-(1:4), ], mT[5, 1:7]))                       mt[-(1:4), ], mT[5, 1:7]))
# Line 88  Line 99
99  ## mixing of negative and positive must give error  ## mixing of negative and positive must give error
100  assertError(mT[-1:1,])  assertError(mT[-1:1,])
101
102    ## Sub *Assignment* ---- now works (partially):
103    mt0 <- mt
104    mt[1, 4] <- -99
105    mt[2:3, 1:6] <- 0
106    mt
107    m2 <- mt+mt
108    m2[1,4] <- -200
109    m2[c(1,3), c(5:6,2)] <- 1:6
110    stopifnot(m2[1,4] == -200,
111              as.vector(m2[c(1,3), c(5:6,2)]) == 1:6)
112    mt[,3] <- 30
113    mt[2:3,] <- 250
114    mt[1:5 %% 2 == 1, 3] <- 0
115    mt[3:1, 1:7 > 5] <- 0
116    mt
117
118    tt <- as(mt,"matrix")
119    ii <- c(0,2,5)
120    jj <- c(2:3,5)
121    tt[ii, jj] <- 1:6 # 0 is just "dropped"
122    mt[ii, jj] <- 1:6
123    assert.EQ.mat(mt, tt)
124
125    mt[1:5, 2:6]
126    as((mt0 - mt)[1:5,], "dsparseMatrix")# [1,5] and lines 2:3
127
128    mt[c(2,4), ] <- 0
129    mt[2:3, 4:7] <- 33
130    validObject(mt)
131    mt
132
133    mc[1,4] <- -99
134    mc[-1, 3] <- -2:1 # 0 should not be entered; 'value' recycled
135    mt[-1, 3] <- -2:1
136    stopifnot(mc@x != 0, mt@x != 0)
137    mc[1:5 %% 2 == 0, 3] <- 0
138    validObject(mc)
139    mc
140    mc[ii, jj] <- 1:6
141    mc[c(2,5), c(3,5)] <- 3.2
142    validObject(mc)
143    mc
144
145  # 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.1225 changed lines Added in v.1226