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 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
|
R version 4.3.1 Patched (2023-08-08 r84910) -- "Beagle Scouts"
Copyright (C) 2023 The R Foundation for Statistical Computing
Platform: aarch64-apple-darwin22.5.0 (64-bit)
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 and
'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() -- based on cbind2() & rbind2()
> ## (where using 'cBind()' and 'rBind()' in Matrix)
>
> ## for R_DEFAULT_PACKAGES=NULL :
> library(utils)
>
> library(Matrix)
>
> source(system.file("test-tools.R", package = "Matrix"))# identical3() etc
Loading required package: tools
>
> ### --- 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])
> M <- Matrix(m)
> M1 <- Matrix(m1)
> M2 <- Matrix(m2)
>
> stopifnot(
+ identical3(cbind ( M, 10*M),
+ show(cbind2( M, 10*M)),
+ Matrix(cbind ( m, 10*m)))
+ ,
+ identical3(cbind (M1, 100+M1),
+ show(cbind2(M1, 100+M1)),
+ Matrix(cbind (m1, 100+m1)))
+ ,
+ identical3(cbind (M1, 10*M2),
+ show(cbind2(M1, 10*M2)),
+ Matrix(cbind (m1, 10*m2)))
+ ,
+ identical3(cbind (M2, M1+M2),
+ show(cbind2(M2, M1+M2)),
+ Matrix(cbind (m2, m1+m2)))
+ ,
+ identical(colnames(show(cbind(M1, MM = -1))),
+ c(colnames(M1), "MM"))
+ ,
+ identical3(rbind ( M, 10*M),
+ show(rbind2( M, 10*M)),
+ Matrix(rbind ( m, 10*m)))
+ ,
+ identical3(rbind (M2, M1+M2),
+ show(rbind2(M2, M1+M2)),
+ Matrix(rbind (m2, m1+m2)))
+ ,
+ Qidentical(show (rbind(R1 = 10:11, M1)),
+ Matrix(rbind(R1 = 10:11, m1)), strictClass=FALSE)
+ , TRUE)
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
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
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
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
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
6 x 4 Matrix of class "dgeMatrix"
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
[4,] 10 40 70 100
[5,] 20 50 80 110
[6,] 30 60 90 120
6 x 4 Matrix of class "dgeMatrix"
a b c d
A 1 4 7 10
B 2 5 8 11
C 3 6 9 12
2 8 14 20
4 10 16 22
6 12 18 24
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
>
> identical.or.eq <- function(x,y, tol=0, ...) {
+ if(identical(x,y, ...))
+ TRUE
+ else if(isTRUE(aeq <- all.equal(x,y, tolerance = tol)))
+ structure(TRUE, comment = "not identical")
+ else aeq
+ }
> identicalShow <- function(x,y, ...)
+ if(!isTRUE(id <- identical.or.eq(x, y, ...))) cat(id,"\n")
>
> ## Checking deparse.level { <==> example at end of ?cbind }:
> checkRN <- function(dd, B = rbind) {
+ FN <- function(deparse.level)
+ rownames(B(1:4, c=2,"a+"=10, dd, deparse.level=deparse.level))
+ rn <- c("1:4", "c", "a+", "dd", "")
+ isMatr <- (length(dim(dd)) == 2)
+ id <- if(isMatr) 5 else 4
+ identicalShow(rn[c(5,2:3, 5)], FN(deparse.level= 0)) # middle two names
+ identicalShow(rn[c(5,2:3,id)], FN(deparse.level= 1)) # last shown if vector
+ identicalShow(rn[c(1,2:3,id)], FN(deparse.level= 2)) # first shown; (last if vec.)
+ }
> checkRN(10) # <==> ?cbind's ex
> checkRN(1:4)
> checkRN( rbind(c(0:1,0,0)))
> checkRN(Matrix(rbind(c(0:1,0,0)))) ## in R <= 3.4.1, from methods:::rbind bug :
> ## Modes: character, NULL Lengths: 4, 0 target is character, current is NULL
> checkRN(10 , rbind)
> checkRN(1:4, rbind)
> checkRN( rbind(c(0:1,0,0)), rbind)
> checkRN(Matrix(rbind(c(0:1,0,0))), rbind)
>
> 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
Warning message:
In cbind.Matrix(x, y, deparse.level = 0L) :
number of rows of result is not a multiple of vector length
> 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
..@ Dim : int [1:2] 3 5
..@ Dimnames:List of 2
.. ..$ : NULL
.. ..$ : chr [1:5] "I" "" "" "" ...
..@ x : num [1:15] 100 100 100 1 2 3 4 5 6 7 ...
..@ 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
..@ Dim : int [1:2] 3 5
..@ Dimnames:List of 2
.. ..$ : chr [1:3] "A" "B" "C"
.. ..$ : chr [1:5] "a" "b" "c" "d" ...
..@ x : num [1:15] 1 2 3 4 5 6 7 8 9 10 ...
..@ 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
> showProc.time()
Time (user system elapsed): 0.051 0.006 0.056
>
> ## lgeMatrix -- rbind2() had bug (in C code):
> is.lge <- function(M) isValid(M, "lgeMatrix")
> stopifnot(exprs = {
+ is.lge(rbind(M2 > 0, M2 < 0)) # had Error in rbind2():
+ ## REAL() can only be applied to a 'numeric', not a 'logical'
+ is.lge(rbind(M2 < 0, M2 > 0)) # ditto
+ is.lge(rbind(Matrix(1:6 %% 3 != 0, 2,3), FALSE))
+ is.lge(L <- rbind(Matrix(TRUE, 2,3), TRUE))
+ all(L)
+ is.lge(rbind(Matrix(TRUE, 2,3), FALSE))
+ })
>
> ### --- Diagonal / Sparse - had bugs
>
> D4 <- Diagonal(4)
> (D4T <- as(D4, "TsparseMatrix"))
4 x 4 sparse Matrix of class "dtTMatrix" (unitriangular)
[1,] I . . .
[2,] . I . .
[3,] . . I .
[4,] . . . I
> 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(.tCRT(d4), cbind(Diagonal(4), 0:3)),
+ identical(.tCRT(m4), rbind(Diagonal(x=-1:2), 0:3)))
> showProc.time()
Time (user system elapsed): 0.019 0 0.02
>
> ### --- Sparse Matrices ---
>
> identical4(cbind(diag(4), diag(4)),
+ cbind(D4C, D4C),
+ cbind(D4T, D4C),
+ cbind(D4C, D4T))
[1] FALSE
> nr <- 4
> nc <- 6
> m. <- matrix(rep_len(c(0, 2:-1), nr * nc), nr, nc)
> M <- Matrix(m.)
> (mC <- as(M, "CsparseMatrix"))
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, "TsparseMatrix"))
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, "TsparseMatrix")),
+ identical(mC, as(mT, "CsparseMatrix")))
>
> 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= 1:0 ), class(m) : dgeMatrix ,. dgCMatrix ,. dgTMatrix ,.
rbind (m, v= 1:0 ), class(m) : dgeMatrix ,. dgCMatrix ,. dgTMatrix ,.
> showProc.time()
Time (user system elapsed): 0.018 0.001 0.019
>
> 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 "dgTMatrix"
[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 "dgTMatrix"
[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 "dgTMatrix"
[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,
+ as(cbind(mT, 0, 7, 0, diag(nr), 0), "CsparseMatrix"),
+ as(cbind( M, 0, 7, 0, diag(nr), 0), "CsparseMatrix")))
>
> 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 . . .
> one <- 1
> zero <- 0
> dimnames(mC) <- dimnames(mT) <- list(LETTERS[1:4], letters[1:6])
> op <- options(sparse.colnames = TRUE)# show colnames in print :
> cbind(mC, one, 100*mC, zero, 0:2)
4 x 15 sparse Matrix of class "dgCMatrix"
a b c d e f one a b c d e f zero
A . -1 . 1 2 . 1 . -100 . 100 200 . . .
B 2 . -1 . 1 2 1 200 . -100 . 100 200 . 1
C 1 2 . -1 . 1 1 100 200 . -100 . 100 . 2
D . 1 2 . -1 . 1 . 100 200 . -100 . . .
> cbind(mC, one, 100*mC, zero, 0:2, deparse.level=0)# no "zero", "one"
4 x 15 sparse Matrix of class "dgCMatrix"
a b c d e f a b c d e f
A . -1 . 1 2 . 1 . -100 . 100 200 . . .
B 2 . -1 . 1 2 1 200 . -100 . 100 200 . 1
C 1 2 . -1 . 1 1 100 200 . -100 . 100 . 2
D . 1 2 . -1 . 1 . 100 200 . -100 . . .
> cbind(mC, one, 100*mC, zero, 0:2, deparse.level=2)# even "0:2"
4 x 15 sparse Matrix of class "dgCMatrix"
a b c d e f one a b c d e f zero 0:2
A . -1 . 1 2 . 1 . -100 . 100 200 . . .
B 2 . -1 . 1 2 1 200 . -100 . 100 200 . 1
C 1 2 . -1 . 1 1 100 200 . -100 . 100 . 2
D . 1 2 . -1 . 1 . 100 200 . -100 . . .
> cbind(mT, one, zero, mT+10*mT, zero, 0:2)
4 x 16 sparse Matrix of class "dgCMatrix"
a b c d e f one zero a b c d e f zero
A . -1 . 1 2 . 1 . . -11 . 11 22 . . .
B 2 . -1 . 1 2 1 . 22 . -11 . 11 22 . 1
C 1 2 . -1 . 1 1 . 11 22 . -11 . 11 . 2
D . 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.out = 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 "lgRMatrix"
[,1] [,2] [,3] [,4] [,5]
| . . . .
. | . . .
. . | . .
. . . | .
. . . . |
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] [,2] [,3] [,4] [,5]
[1,] . 1 . . 2
[2,] . . 2 . 1
[3,] 2 . 1 . .
> (m2 <- cbind(m,m))
3 x 10 sparse Matrix of class "dgCMatrix"
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[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] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[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] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[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 . .
>
> ## Mixture of dense and sparse/diagonal -- used to fail, even in 1.0-0
> D5 <- Diagonal(x = 10*(1:5))
> (D5.1 <- cbind2(D5, 1))
5 x 6 sparse Matrix of class "dgCMatrix"
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 10 . . . . 1
[2,] . 20 . . . 1
[3,] . . 30 . . 1
[4,] . . . 40 . 1
[5,] . . . . 50 1
> ## "FIXME" in newer versions of R, do not need Matrix() here:
> s42 <- Matrix(z42 <- cbind2(rep(0:1,4), rep(1:0,4)),
+ sparse=TRUE)
> (C86 <- rbind(1, 0, D5.1, 0))
8 x 6 sparse Matrix of class "dgCMatrix"
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 1 1 1 1 1
[2,] . . . . . .
[3,] 10 . . . . 1
[4,] . 20 . . . 1
[5,] . . 30 . . 1
[6,] . . . 40 . 1
[7,] . . . . 50 1
[8,] . . . . . .
> stopifnotValid(D5.1, "dgCMatrix")
> stopifnotValid(print(rbind2(Matrix(1:10, 2,5), D5)), "dgRMatrix")
7 x 5 sparse Matrix of class "dgRMatrix"
[,1] [,2] [,3] [,4] [,5]
[1,] 1 3 5 7 9
[2,] 2 4 6 8 10
[3,] 10 . . . .
[4,] . 20 . . .
[5,] . . 30 . .
[6,] . . . 40 .
[7,] . . . . 50
> stopifnotValid(print(cbind2(Matrix(10:1, 5,2), D5.1)), "dgCMatrix")
5 x 8 sparse Matrix of class "dgCMatrix"
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,] 10 5 10 . . . . 1
[2,] 9 4 . 20 . . . 1
[3,] 8 3 . . 30 . . 1
[4,] 7 2 . . . 40 . 1
[5,] 6 1 . . . . 50 1
> stopifnotValid(zz <- cbind2(z42, C86), "dgCMatrix")
> stopifnot(identical(zz, cbind2(s42, C86)))
>
> ## Using "nMatrix"
> (m1 <- sparseMatrix(1:3, 1:3)) # ngCMatrix
3 x 3 sparse Matrix of class "ngCMatrix"
[,1] [,2] [,3]
[1,] | . .
[2,] . | .
[3,] . . |
> m2 <- sparseMatrix(1:3, 1:3, x = 1:3)
> stopifnotValid(c12 <- cbind(m1,m2), "dgCMatrix") # was "ngC.." because of cholmod_horzcat !
> stopifnotValid(c21 <- cbind(m2,m1), "dgCMatrix") # ditto
> stopifnotValid(r12 <- rbind(m1,m2), "dgCMatrix") # was "ngC.." because of cholmod_vertcat !
> stopifnotValid(r21 <- rbind(m2,m1), "dgCMatrix") # ditto
> d1 <- as(m1, "denseMatrix")
> d2 <- as(m2, "denseMatrix")
> stopifnotValid(cbind2(d2,d1), "dgeMatrix")
> stopifnotValid(cbind2(d1,d2), "dgeMatrix")## gave an error in Matrix 1.1-5
> stopifnotValid(rbind2(d2,d1), "dgeMatrix")
> stopifnotValid(rbind2(d1,d2), "dgeMatrix")## gave an error in Matrix 1.1-5
>
> ## rbind2() / cbind2() mixing sparse/dense: used to "fail",
> ## ------------------- then (in 'devel', ~ 2015-03): completely wrong
> S <- .sparseDiagonal(2)
> s <- diag(2)
> S9 <- rbind(S,0,0,S,0,NaN,0,0,0,2)## r/cbind2() failed to determine 'sparse' in Matrix <= 1.2-2
> s9 <- rbind(s,0,0,s,0,NaN,0,0,0,2)
> assert.EQ.mat(S9, s9)
> D <- Matrix(1:6, 3,2); d <- as(D, "matrix")
> T9 <- t(S9); t9 <- t(s9); T <- t(D); t <- t(d)
> stopifnot(identical(rbind (s9,d), rbind2(s9,d)),
+ identical(rbind2(D,S9), t(cbind2(T,T9))),
+ identical(rbind2(S9,D), t(cbind2(T9,T))))
> assert.EQ.mat(rbind2(S9,D), rbind2(s9,d))
> assert.EQ.mat(rbind2(D,S9), rbind2(d,s9))
> ## now with cbind2() -- no problem!
> stopifnot(identical(cbind (t9,t), cbind2(t9,t)))
> assert.EQ.mat(cbind2(T9,T), cbind2(t9,t))
> assert.EQ.mat(cbind2(T,T9), cbind2(t,t9))
>
>
>
> options(op)
> showProc.time()
Time (user system elapsed): 0.06 0.003 0.063
>
> proc.time()
user system elapsed
0.609 0.039 0.646
|