The R Project SVN R-packages

Rev

Rev 4052 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4052 Rev 4207
Line 1... Line 1...
1
#### Testing  cbind() & rbind()
1
#### Testing  cBind() & rBind()
2
 
2
 
3
library(Matrix)
3
library(Matrix)
4
 
4
 
5
source(system.file("test-tools.R", package = "Matrix"))# identical3() etc
5
source(system.file("test-tools.R", package = "Matrix"))# identical3() etc
6
 
6
 
Line 9... Line 9...
9
m1 <- m2 <- m <- Matrix(1:12, 3,4)
9
m1 <- m2 <- m <- Matrix(1:12, 3,4)
10
dimnames(m2) <- list(LETTERS[1:3],
10
dimnames(m2) <- list(LETTERS[1:3],
11
                     letters[1:4])
11
                     letters[1:4])
12
dimnames(m1) <- list(NULL,letters[1:4])
12
dimnames(m1) <- list(NULL,letters[1:4])
13
 
13
 
14
stopifnot(identical(cbind ( m, 10*m) -> R,
14
stopifnot(identical(cBind ( m, 10*m) -> R,
15
                    cbind2( m, 10*m))); R
15
                    cbind2( m, 10*m))); R
16
stopifnot(identical(cbind (m1,100+m1) -> R,
16
stopifnot(identical(cBind (m1,100+m1) -> R,
17
                    cbind2(m1,100+m1))); R
17
                    cbind2(m1,100+m1))); R
18
stopifnot(identical(cbind (m1, 10*m2) -> R,
18
stopifnot(identical(cBind (m1, 10*m2) -> R,
19
                    cbind2(m1, 10*m2))); R
19
                    cbind2(m1, 10*m2))); R
20
stopifnot(identical(cbind (m2, m1+m2) -> R,
20
stopifnot(identical(cBind (m2, m1+m2) -> R,
21
                    cbind2(m2, m1+m2))); R
21
                    cbind2(m2, m1+m2))); R
22
 
22
 
23
cbind(m1, MM = -1)
23
cBind(m1, MM = -1)
24
rbind(R1 = 10:11, m1)
24
rBind(R1 = 10:11, m1)
25
cbind(0, Matrix(0+0:1, 1,2), 3:2)# FIXME? should warn - as with matrix()
25
cBind(0, Matrix(0+0:1, 1,2), 3:2)# FIXME? should warn - as with matrix()
26
 
26
 
27
as(rbind(0, Matrix(0+0:1, 1,2), 3:2),
27
as(rBind(0, Matrix(0+0:1, 1,2), 3:2),
28
   "sparseMatrix")
28
   "sparseMatrix")
29
cbind(m2, 10*m2[nrow(m2):1 ,])# keeps the rownames from the first
29
cBind(m2, 10*m2[nrow(m2):1 ,])# keeps the rownames from the first
30
 
30
 
31
(im <- cbind(I = 100, m))
31
(im <- cBind(I = 100, m))
32
str(im)
32
str(im)
33
(mi <- cbind(m2, I = 1000))
33
(mi <- cBind(m2, I = 1000))
34
str(mi)
34
str(mi)
35
(m1m <- cbind(m,I=100,m2))
35
(m1m <- cBind(m,I=100,m2))
36
 
36
 
37
### --- Diagonal / Sparse - had bugs
37
### --- Diagonal / Sparse - had bugs
38
 
38
 
39
D4 <- Diagonal(4)
39
D4 <- Diagonal(4)
40
(D4T <- as(D4, "sparseMatrix"))
40
(D4T <- as(D4, "sparseMatrix"))
41
D4C <- as(D4T, "CsparseMatrix")
41
D4C <- as(D4T, "CsparseMatrix")
42
c1 <- Matrix(0+0:3, 4, sparse=TRUE) ; r1 <- t(c1); r1
42
c1 <- Matrix(0+0:3, 4, sparse=TRUE) ; r1 <- t(c1); r1
43
 
43
 
44
d4 <- rbind(Diagonal(4), 0:3)
44
d4 <- rBind(Diagonal(4), 0:3)
45
m4 <- cbind(Diagonal(x=-1:2), 0:3)
45
m4 <- cBind(Diagonal(x=-1:2), 0:3)
46
c4. <- cbind(Diagonal(4), c1)
46
c4. <- cBind(Diagonal(4), c1)
47
c.4 <- cbind(c1, Diagonal(4))
47
c.4 <- cBind(c1, Diagonal(4))
48
r4. <- rbind(Diagonal(4), r1)
48
r4. <- rBind(Diagonal(4), r1)
49
r.4 <- rbind(r1, Diagonal(4))
49
r.4 <- rBind(r1, Diagonal(4))
50
assert.EQ.mat(d4, rbind(diag(4),    0:3))
50
assert.EQ.mat(d4, rBind(diag(4),    0:3))
51
assert.EQ.mat(m4, cbind(diag(-1:2), 0:3))
51
assert.EQ.mat(m4, cBind(diag(-1:2), 0:3))
52
stopifnot(is(d4, "sparseMatrix"), is(m4, "sparseMatrix"),
52
stopifnot(is(d4, "sparseMatrix"), is(m4, "sparseMatrix"),
53
	  identical(t(d4), cbind(Diagonal(4),     0:3)),
53
	  identical(t(d4), cBind(Diagonal(4),     0:3)),
54
	  identical(t(m4), rbind(Diagonal(x=-1:2), 0:3)))
54
	  identical(t(m4), rBind(Diagonal(x=-1:2), 0:3)))
55
 
55
 
56
### --- Sparse Matrices ---
56
### --- Sparse Matrices ---
57
 
57
 
58
identical4(cbind(diag(4), diag(4)),
58
identical4(cBind(diag(4), diag(4)),
59
           cbind(D4C, D4C),
59
           cBind(D4C, D4C),
60
           cbind(D4T, D4C),
60
           cBind(D4T, D4C),
61
           cbind(D4C, D4T))
61
           cBind(D4C, D4T))
62
nr <- 4
62
nr <- 4
63
m. <- matrix(c(0, 2:-1),  nr ,6)
63
m. <- matrix(c(0, 2:-1),  nr ,6)
64
M <- Matrix(m.)
64
M <- Matrix(m.)
65
(mC <- as(M, "dgCMatrix"))
65
(mC <- as(M, "dgCMatrix"))
66
(mT <- as(M, "dgTMatrix"))
66
(mT <- as(M, "dgTMatrix"))
67
stopifnot(identical(mT, as(mC, "dgTMatrix")),
67
stopifnot(identical(mT, as(mC, "dgTMatrix")),
68
          identical(mC, as(mT, "dgCMatrix")))
68
          identical(mC, as(mT, "dgCMatrix")))
69
 
69
 
70
for(v in list(0, 2, 1:0))
70
for(v in list(0, 2, 1:0))
71
    for(fnam in c("cbind", "rbind")) {
71
    for(fnam in c("cBind", "rBind")) {
72
        cat(fnam,"(m, v=", deparse(v),"), class(m) :")
72
        cat(fnam,"(m, v=", deparse(v),"), class(m) :")
73
        FUN <- get(fnam)
73
        FUN <- get(fnam)
74
        for(m in list(M, mC, mT)) {
74
        for(m in list(M, mC, mT)) {
75
            cat("", class(m),"")
75
            cat("", class(m),"")
76
            assert.EQ.mat(FUN(v, m), FUN(v, m.)) ; cat(",")
76
            assert.EQ.mat(FUN(v, m), FUN(v, m.)) ; cat(",")
77
            assert.EQ.mat(FUN(m, v), FUN(m., v)) ; cat(".")
77
            assert.EQ.mat(FUN(m, v), FUN(m., v)) ; cat(".")
78
        }
78
        }
79
        cat("\n")
79
        cat("\n")
80
    }
80
    }
81
 
81
 
82
cbind(0, mC); cbind(mC, 0)
82
cBind(0, mC); cBind(mC, 0)
83
cbind(0, mT); cbind(mT, 2)
83
cBind(0, mT); cBind(mT, 2)
84
cbind(diag(nr), mT)
84
cBind(diag(nr), mT)
85
stopifnot(identical(t(cbind(diag(nr),   mT)),
85
stopifnot(identical(t(cBind(diag(nr),   mT)),
86
                      rbind(diag(nr), t(mT))))
86
                      rBind(diag(nr), t(mT))))
87
(cc <- cbind(mC, 0,7,0, diag(nr), 0))
87
(cc <- cBind(mC, 0,7,0, diag(nr), 0))
88
stopifnot(identical3(cc, cbind(mT, 0,7,0, diag(nr), 0),
88
stopifnot(identical3(cc, cBind(mT, 0,7,0, diag(nr), 0),
89
                     as( cbind( M, 0,7,0, diag(nr), 0), "dgCMatrix")))
89
                     as( cBind( M, 0,7,0, diag(nr), 0), "dgCMatrix")))
90
 
90
 
91
cbind(mC, 1, 100*mC, 0, 0:2)
91
cBind(mC, 1, 100*mC, 0, 0:2)
92
cbind(mT, 1, 0, mT+10*mT, 0, 0:2)
92
cBind(mT, 1, 0, mT+10*mT, 0, 0:2)
93
 
93
 
94
## print() / show() of  non-structural zeros:
94
## print() / show() of  non-structural zeros:
95
(m <- Matrix(c(0, 0, 2:0), 3, 5))
95
(m <- Matrix(c(0, 0, 2:0), 3, 5))
96
(m2 <- cbind(m,m))
96
(m2 <- cBind(m,m))
97
(m4 <- rbind(m2,m2))
97
(m4 <- rBind(m2,m2))
98
diag(m4)
98
diag(m4)
99
for(i in 1:6) {
99
for(i in 1:6) {
100
    m4[i, i ] <- i
100
    m4[i, i ] <- i
101
    m4[i,i+1] <- 0
101
    m4[i,i+1] <- 0
102
}
102
}