File: bind.Rout.save

package info (click to toggle)
rmatrix 0.999375-43-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 8,068 kB
  • ctags: 2,395
  • sloc: ansic: 37,941; makefile: 216; sh: 128
file content (335 lines) | stat: -rw-r--r-- 10,010 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335

R version 2.11.0 Patched (2010-05-17 r52025)
Copyright (C) 2010 The R Foundation for Statistical Computing
ISBN 3-900051-07-0

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information anda
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> #### Testing  cBind() & rBind()
> 
> library(Matrix)
Loading required package: lattice

Attaching package: 'Matrix'

The following object(s) are masked from 'package:base':

    det

> 
> source(system.file("test-tools.R", package = "Matrix"))# identical3() etc
> 
> ### --- Dense Matrices ---
> 
> m1 <- m2 <- m <- Matrix(1:12, 3,4)
> dimnames(m2) <- list(LETTERS[1:3],
+                      letters[1:4])
> dimnames(m1) <- list(NULL,letters[1:4])
> 
> stopifnot(identical(cBind ( m, 10*m) -> R,
+                     cbind2( m, 10*m))); R
3 x 8 Matrix of class "dgeMatrix"
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]    1    4    7   10   10   40   70  100
[2,]    2    5    8   11   20   50   80  110
[3,]    3    6    9   12   30   60   90  120
> stopifnot(identical(cBind (m1,100+m1) -> R,
+                     cbind2(m1,100+m1))); R
3 x 8 Matrix of class "dgeMatrix"
     a b c  d   a   b   c   d
[1,] 1 4 7 10 101 104 107 110
[2,] 2 5 8 11 102 105 108 111
[3,] 3 6 9 12 103 106 109 112
> stopifnot(identical(cBind (m1, 10*m2) -> R,
+                     cbind2(m1, 10*m2))); R
3 x 8 Matrix of class "dgeMatrix"
  a b c  d  a  b  c   d
A 1 4 7 10 10 40 70 100
B 2 5 8 11 20 50 80 110
C 3 6 9 12 30 60 90 120
> stopifnot(identical(cBind (m2, m1+m2) -> R,
+                     cbind2(m2, m1+m2))); R
3 x 8 Matrix of class "dgeMatrix"
  a b c  d a  b  c  d
A 1 4 7 10 2  8 14 20
B 2 5 8 11 4 10 16 22
C 3 6 9 12 6 12 18 24
> 
> cBind(m1, MM = -1)
3 x 5 Matrix of class "dgeMatrix"
     a b c  d MM
[1,] 1 4 7 10 -1
[2,] 2 5 8 11 -1
[3,] 3 6 9 12 -1
> rBind(R1 = 10:11, m1)
4 x 4 Matrix of class "dgeMatrix"
    a  b  c  d
R1 10 11 10 11
    1  4  7 10
    2  5  8 11
    3  6  9 12
> cBind(0, Matrix(0+0:1, 1,2), 3:2)# FIXME? should warn - as with matrix()
1 x 4 Matrix of class "dgeMatrix"
     [,1] [,2] [,3] [,4]
[1,]    0    0    1    3
> 
> as(rBind(0, Matrix(0+0:1, 1,2), 3:2),
+    "sparseMatrix")
3 x 2 sparse Matrix of class "dgCMatrix"
        
[1,] . .
[2,] . 1
[3,] 3 2
> cBind(m2, 10*m2[nrow(m2):1 ,])# keeps the rownames from the first
3 x 8 Matrix of class "dgeMatrix"
  a b c  d  a  b  c   d
A 1 4 7 10 30 60 90 120
B 2 5 8 11 20 50 80 110
C 3 6 9 12 10 40 70 100
> 
> (im <- cBind(I = 100, m))
3 x 5 Matrix of class "dgeMatrix"
       I         
[1,] 100 1 4 7 10
[2,] 100 2 5 8 11
[3,] 100 3 6 9 12
> str(im)
Formal class 'dgeMatrix' [package "Matrix"] with 4 slots
  ..@ x       : num [1:15] 100 100 100 1 2 3 4 5 6 7 ...
  ..@ Dim     : int [1:2] 3 5
  ..@ Dimnames:List of 2
  .. ..$ : NULL
  .. ..$ : chr [1:5] "I" "" "" "" ...
  ..@ factors : list()
> (mi <- cBind(m2, I = 1000))
3 x 5 Matrix of class "dgeMatrix"
  a b c  d    I
A 1 4 7 10 1000
B 2 5 8 11 1000
C 3 6 9 12 1000
> str(mi)
Formal class 'dgeMatrix' [package "Matrix"] with 4 slots
  ..@ x       : num [1:15] 1 2 3 4 5 6 7 8 9 10 ...
  ..@ Dim     : int [1:2] 3 5
  ..@ Dimnames:List of 2
  .. ..$ : chr [1:3] "A" "B" "C"
  .. ..$ : chr [1:5] "a" "b" "c" "d" ...
  ..@ factors : list()
> (m1m <- cBind(m,I=100,m2))
3 x 9 Matrix of class "dgeMatrix"
             I a b c  d
A 1 4 7 10 100 1 4 7 10
B 2 5 8 11 100 2 5 8 11
C 3 6 9 12 100 3 6 9 12
> 
> ### --- Diagonal / Sparse - had bugs
> 
> D4 <- Diagonal(4)
> (D4T <- as(D4, "TsparseMatrix"))
4 x 4 sparse Matrix of class "dtTMatrix" (unitriangular)
            
[1,] 1 . . .
[2,] . 1 . .
[3,] . . 1 .
[4,] . . . 1
> D4C <- as(D4T, "CsparseMatrix")
> c1 <- Matrix(0+0:3, 4, sparse=TRUE) ; r1 <- t(c1); r1
1 x 4 sparse Matrix of class "dgCMatrix"
            
[1,] . 1 2 3
> 
> d4 <- rBind(Diagonal(4), 0:3)
> m4 <- cBind(Diagonal(x=-1:2), 0:3)
> c4. <- cBind(Diagonal(4), c1)
> c.4 <- cBind(c1, Diagonal(4))
> r4. <- rBind(Diagonal(4), r1)
> r.4 <- rBind(r1, Diagonal(4))
> assert.EQ.mat(d4, rBind(diag(4),    0:3))
> assert.EQ.mat(m4, cBind(diag(-1:2), 0:3))
> stopifnot(identical(Matrix(cbind(diag(3),0)), cbind2(Diagonal(3),0)),
+ 	  is(d4, "sparseMatrix"), is(m4, "sparseMatrix"),
+ 	  identical(t(d4), cBind(Diagonal(4),     0:3)),
+ 	  identical(t(m4), rBind(Diagonal(x=-1:2), 0:3)))
> 
> ### --- Sparse Matrices ---
> 
> identical4(cBind(diag(4), diag(4)),
+            cBind(D4C, D4C),
+            cBind(D4T, D4C),
+            cBind(D4C, D4T))
[1] FALSE
> nr <- 4
> m. <- matrix(c(0, 2:-1),  nr ,6)
Warning message:
In matrix(c(0, 2:-1), nr, 6) :
  data length [5] is not a sub-multiple or multiple of the number of rows [4]
> M <- Matrix(m.)
> (mC <- as(M, "dgCMatrix"))
4 x 6 sparse Matrix of class "dgCMatrix"
                    
[1,] . -1  .  1  2 .
[2,] 2  . -1  .  1 2
[3,] 1  2  . -1  . 1
[4,] .  1  2  . -1 .
> (mT <- as(M, "dgTMatrix"))
4 x 6 sparse Matrix of class "dgTMatrix"
                    
[1,] . -1  .  1  2 .
[2,] 2  . -1  .  1 2
[3,] 1  2  . -1  . 1
[4,] .  1  2  . -1 .
> stopifnot(identical(mT, as(mC, "dgTMatrix")),
+           identical(mC, as(mT, "dgCMatrix")))
> 
> for(v in list(0, 2, 1:0))
+     for(fnam in c("cBind", "rBind")) {
+         cat(fnam,"(m, v=", deparse(v),"), class(m) :")
+         FUN <- get(fnam)
+         for(m in list(M, mC, mT)) {
+             cat("", class(m),"")
+             assert.EQ.mat(FUN(v, m), FUN(v, m.)) ; cat(",")
+             assert.EQ.mat(FUN(m, v), FUN(m., v)) ; cat(".")
+         }
+         cat("\n")
+     }
cBind (m, v= 0 ), class(m) : dgeMatrix ,. dgCMatrix ,. dgTMatrix ,.
rBind (m, v= 0 ), class(m) : dgeMatrix ,. dgCMatrix ,. dgTMatrix ,.
cBind (m, v= 2 ), class(m) : dgeMatrix ,. dgCMatrix ,. dgTMatrix ,.
rBind (m, v= 2 ), class(m) : dgeMatrix ,. dgCMatrix ,. dgTMatrix ,.
cBind (m, v= c(1L, 0L) ), class(m) : dgeMatrix ,. dgCMatrix ,. dgTMatrix ,.
rBind (m, v= c(1L, 0L) ), class(m) : dgeMatrix ,. dgCMatrix ,. dgTMatrix ,.
> 
> cBind(0, mC); cBind(mC, 0)
4 x 7 sparse Matrix of class "dgCMatrix"
                      
[1,] . . -1  .  1  2 .
[2,] . 2  . -1  .  1 2
[3,] . 1  2  . -1  . 1
[4,] . .  1  2  . -1 .
4 x 7 sparse Matrix of class "dgCMatrix"
                      
[1,] . -1  .  1  2 . .
[2,] 2  . -1  .  1 2 .
[3,] 1  2  . -1  . 1 .
[4,] .  1  2  . -1 . .
> cBind(0, mT); cBind(mT, 2)
4 x 7 sparse Matrix of class "dgCMatrix"
                      
[1,] . . -1  .  1  2 .
[2,] . 2  . -1  .  1 2
[3,] . 1  2  . -1  . 1
[4,] . .  1  2  . -1 .
4 x 7 sparse Matrix of class "dgCMatrix"
                      
[1,] . -1  .  1  2 . 2
[2,] 2  . -1  .  1 2 2
[3,] 1  2  . -1  . 1 2
[4,] .  1  2  . -1 . 2
> cBind(diag(nr), mT)
4 x 10 sparse Matrix of class "dgCMatrix"
                            
[1,] 1 . . . . -1  .  1  2 .
[2,] . 1 . . 2  . -1  .  1 2
[3,] . . 1 . 1  2  . -1  . 1
[4,] . . . 1 .  1  2  . -1 .
> stopifnot(identical(t(cBind(diag(nr),   mT)),
+                       rBind(diag(nr), t(mT))))
> (cc <- cBind(mC, 0,7,0, diag(nr), 0))
4 x 14 sparse Matrix of class "dgCMatrix"
                                    
[1,] . -1  .  1  2 . . 7 . 1 . . . .
[2,] 2  . -1  .  1 2 . 7 . . 1 . . .
[3,] 1  2  . -1  . 1 . 7 . . . 1 . .
[4,] .  1  2  . -1 . . 7 . . . . 1 .
> stopifnot(identical3(cc, cBind(mT, 0,7,0, diag(nr), 0),
+                      as( cBind( M, 0,7,0, diag(nr), 0), "dgCMatrix")))
> 
> cBind(mC, 1, 100*mC, 0, 0:2)
4 x 15 sparse Matrix of class "dgCMatrix"
                                                      
[1,] . -1  .  1  2 . 1   . -100    .  100  200   . . .
[2,] 2  . -1  .  1 2 1 200    . -100    .  100 200 . 1
[3,] 1  2  . -1  . 1 1 100  200    . -100    . 100 . 2
[4,] .  1  2  . -1 . 1   .  100  200    . -100   . . .
> cBind(mT, 1, 0, mT+10*mT, 0, 0:2)
4 x 16 sparse Matrix of class "dgCMatrix"
                                                  
[1,] . -1  .  1  2 . 1 .  . -11   .  11  22  . . .
[2,] 2  . -1  .  1 2 1 . 22   . -11   .  11 22 . 1
[3,] 1  2  . -1  . 1 1 . 11  22   . -11   . 11 . 2
[4,] .  1  2  . -1 . 1 .  .  11  22   . -11  . . .
> 
> ## logical (sparse) - should remain logical :
> L5 <- Diagonal(n = 5, x = TRUE); v5 <- rep(x = c(FALSE,TRUE), length = ncol(L5))
> stopifnot(is(show(rBind(L5,v5)), "lsparseMatrix"),
+ 	  is(show(cBind(v5,L5)), "lsparseMatrix"),
+ 	  is(rBind(L5, 2* v5), "dsparseMatrix"),
+	  is(cBind(2* v5, L5), "dsparseMatrix"))
6 x 5 sparse Matrix of class "lgCMatrix"
            
   | . . . .
   . | . . .
   . . | . .
   . . . | .
   . . . . |
v5 . | . | .
5 x 6 sparse Matrix of class "lgCMatrix"
     v5          
[1,]  . | . . . .
[2,]  | . | . . .
[3,]  . . . | . .
[4,]  | . . . | .
[5,]  . . . . . |
> 
> ## print() / show() of  non-structural zeros:
> (m <- Matrix(c(0, 0, 2:0), 3, 5))
3 x 5 sparse Matrix of class "dgCMatrix"
              
[1,] . 1 . . 2
[2,] . . 2 . 1
[3,] 2 . 1 . .
> (m2 <- cBind(m,m))
3 x 10 sparse Matrix of class "dgCMatrix"
                        
[1,] . 1 . . 2 . 1 . . 2
[2,] . . 2 . 1 . . 2 . 1
[3,] 2 . 1 . . 2 . 1 . .
> (m4 <- rBind(m2,m2))
6 x 10 sparse Matrix of class "dgCMatrix"
                        
[1,] . 1 . . 2 . 1 . . 2
[2,] . . 2 . 1 . . 2 . 1
[3,] 2 . 1 . . 2 . 1 . .
[4,] . 1 . . 2 . 1 . . 2
[5,] . . 2 . 1 . . 2 . 1
[6,] 2 . 1 . . 2 . 1 . .
> diag(m4)
[1] 0 0 1 0 1 2
> for(i in 1:6) {
+     m4[i, i ] <- i
+     m4[i,i+1] <- 0
+ }
> m4 ## now show some non-structural zeros:
6 x 10 sparse Matrix of class "dgCMatrix"
                        
[1,] 1 . . . 2 . 1 . . 2
[2,] . 2 . . 1 . . 2 . 1
[3,] 2 . 3 . . 2 . 1 . .
[4,] . 1 . 4 . . 1 . . 2
[5,] . . 2 . 5 . . 2 . 1
[6,] 2 . 1 . . 6 . 1 . .
> 
> 
> cat('Time elapsed: ', proc.time(),'\n') # for ``statistical reasons''
Time elapsed:  2.454 0.072 2.626 0 0 
>