# SCM Repository

[matrix] Diff of /pkg/tests/indexing.Rout.save
 [matrix] / pkg / tests / indexing.Rout.save

# Diff of /pkg/tests/indexing.Rout.save

revision 1672, Mon Nov 6 20:25:10 2006 UTC revision 1673, Mon Nov 6 20:54:26 2006 UTC
# Line 1  Line 1
1
2  R version 2.4.0 Patched (2006-11-03 r39777)  R version 2.4.0 Patched (2006-11-03 r39791)
3  Copyright (C) 2006 The R Foundation for Statistical Computing  Copyright (C) 2006 The R Foundation for Statistical Computing
4  ISBN 3-900051-07-0  ISBN 3-900051-07-0
5
# Line 24  Line 24
24  >  >
25  > ### Dense Matrices  > ### Dense Matrices
26  >  >
27  > m <- Matrix(1:28, nrow = 7)  > m <- Matrix(1:28 +0, nrow = 7)
28  Warning message:  > validObject(m)
integer matrices not yet implemented in 'Matrix'; using 'double' ones' in: Matrix(1:28, nrow = 7)
> validObject(m) ; m@x <- as.double(m@x) ; validObject(m)
[1] TRUE
29  [1] TRUE  [1] TRUE
30  > stopifnot(identical(m, m[]),  > stopifnot(identical(m, m[]),
31  +           identical(m[2, 3],  16), # simple number  +           identical(m[2, 3],  16), # simple number
# Line 101  Line 98
98  [2,]   -1    9  100  200  [2,]   -1    9  100  200
99  [3,]   -1   10   17   24  [3,]   -1   10   17   24
100  >  >
> ## testing operations on logical Matrices rather more than indexing:
101  > m. <- as.matrix(m)  > m. <- as.matrix(m)
102    >
103    > ## m[ cbind(i,j) ] indexing:
104    > ij <- cbind(1:6, 2:3)
105    > stopifnot(identical(m[ij], m.[ij]))
106    >
107    > ## testing operations on logical Matrices rather more than indexing:
108  > g10 <- m [ m > 10 ]  > g10 <- m [ m > 10 ]
109  > stopifnot(18 == length(g10))  > stopifnot(18 == length(g10))
110  > stopifnot(10 == length(m[ m <= 10 ]))  > stopifnot(10 == length(m[ m <= 10 ]))
# Line 169  Line 171
171
172  [1,] 7 . . . . . . 287 . . 407 . 487 527 . . . . 727 .  [1,] 7 . . . . . . 287 . . 407 . 487 527 . . . . 727 .
173  > assert.EQ.mat(mC[1:2,], mm[1:2,])  > assert.EQ.mat(mC[1:2,], mm[1:2,])
174  > stopifnot(all.equal(mC[,3],   mm[,3]))  > stopifnot(all.equal(mC[,3], mm[,3]),
175    +         identical(mC[ij], mm[ij]))
176  > assert.EQ.mat(mC[7, , drop=FALSE], mm[7, , drop=FALSE])  > assert.EQ.mat(mC[7, , drop=FALSE], mm[7, , drop=FALSE])
177  >  >
178  > stopifnot(dim(mC[numeric(0), ]) == c(0,20), # used to give warnings  > stopifnot(dim(mC[numeric(0), ]) == c(0,20), # used to give warnings
# Line 238  Line 241
241  >  >
242  > x.x <- crossprod(mC)  > x.x <- crossprod(mC)
243  > stopifnot(class(x.x) == "dsCMatrix",  > stopifnot(class(x.x) == "dsCMatrix",
244  +           class(x.x. <- round(x.x / 10000)) == "dsCMatrix")  +           class(x.x. <- round(x.x / 10000)) == "dsCMatrix",
245    +           identical(x.x[cbind(2:6, 2:6)],
246    +                     diag(x.x [2:6, 2:6])))
247  > head(x.x.) # Note the *non*-structural 0's printed as "0"  > head(x.x.) # Note the *non*-structural 0's printed as "0"
248  6 x 20 sparse Matrix of class "dgCMatrix"  6 x 20 sparse Matrix of class "dgCMatrix"
249
# Line 469  Line 474
474   [8,] 0.125 0.111 0.100 0.091 0.083 0.077   [8,] 0.125 0.111 0.100 0.091 0.083 0.077
475   [9,] 0.111 0.100 0.091 0.083 0.077 0.071   [9,] 0.111 0.100 0.091 0.083 0.077 0.071
476  >  >
477    > ## an example that failed long
478    > sy3 <- new("dsyMatrix", Dim = as.integer(c(2, 2)), x = c(14, -1, 2, -7))
479    > validObject(dm <- kronecker(Diagonal(2), sy3))
480    Warning message:
481    Ambiguous method selection for "kronecker", target "ddiMatrix#dsyMatrix" (the first of the signatures shown will be used)
482        Matrix#ANY
483        ANY#Matrix
484     in: .findInheritedMethods(classes, fdef, mtable)
485    [1] TRUE
486    > (s2 <- as(dm, "sparseMatrix"))
487    4 x 4 sparse Matrix of class "dsCMatrix"
488
489    [1,] 14  2 .  .
490    [2,]  2 -7 .  .
491    [3,] .  .  14  2
492    [4,] .  .   2 -7
493    > validObject(st <- as(s2, "TsparseMatrix"))
494    [1] TRUE
495    > validObject(s.32  <- st[1:3,1:2]) ## 3 x 2 - and *not* dsTMatrix
496    [1] TRUE
497    > validObject(s2.32 <- s2[1:3,1:2])
498    [1] TRUE
499    > I <- c(1,4:3)
500    > stopifnot(is(s2.32, "generalMatrix"),
501    +           is(s.32,  "generalMatrix"),
502    +           identical(as.mat(s.32), as.mat(s2.32)),
503    +           identical3(dm[1:3,-1], asD(s2[1:3,-1]), asD(st[1:3,-1])),
504    +           identical4(2, dm[4,3], s2[4,3], st[4,3]),
505    +           identical3(diag(dm), diag(s2), diag(st)),
506    +           is((cI <- s2[I,I]), "dsCMatrix"),
507    +           is((tI <- st[I,I]), "dsTMatrix"),
508    +           identical4(as.mat(dm)[I,I], as.mat(dm[I,I]), as.mat(tI), as.mat(cI))
509    +           )
510    >
511    > ## now sub-assign  and check for consistency
512    > ## symmetric subassign should keep symmetry
513    > st[I,I] <- 0; validObject(st); stopifnot(is(st,"symmetricMatrix"))
514    [1] TRUE
515    > s2[I,I] <- 0; validObject(s2); stopifnot(is(s2,"symmetricMatrix"))
516    [1] TRUE
517    >
518    > m <- as.mat(st)
519    >  m[2:1,2:1] <- 4:1
520    > st[2:1,2:1] <- 4:1
521    > s2[2:1,2:1] <- 4:1
522    > stopifnot(identical(m, as.mat(st)),
523    +         1:4 == as.vector(s2[1:2,1:2]),
524    +         identical(m, as.mat(s2)))
525    >
526    > ## m[cbind(i,j)] <- value:
527    > m.[ cbind(3:5, 1:3) ] <- 1:3
528    > stopifnot(m.[3,1] == 1, m.[4,2] == 2)
529    > x.x[ cbind(2:6, 2:6)] <- 12:16
530    > validObject(x.x)
531    [1] TRUE
532    > stopifnot(class(x.x) == "dsCMatrix",
533    +         12:16 == as.mat(x.x)[cbind(2:6, 2:6)])
534    >
535  > cat('Time elapsed: ', proc.time(),'\n') # for ``statistical reasons''  > cat('Time elapsed: ', proc.time(),'\n') # for ``statistical reasons''
536  Time elapsed:  2.636 0.108 3.177 0 0  Time elapsed:  3.316 0.124 3.84 0 0
537  >  >

Legend:
 Removed from v.1672 changed lines Added in v.1673