Skip to contents

Like ordinary matrices in base R, SparseMatrix derivatives can be multiplied with the %*% operator. They also support crossprod() and tcrossprod().

Value

The %*%, crossprod() and tcrossprod() methods for SparseMatrix objects always return an ordinary matrix of type() "double".

Note

Matrix multiplication and cross-product of SparseMatrix derivatives are multithreaded. See set_SparseArray_nthread for how to control the number of threads.

See also

  • %*% and crossprod in base R.

  • SparseMatrix objects.

  • S4Arrays::type in the S4Arrays package to get the type of the elements of an array-like object.

  • Ordinary matrix objects in base R.

Examples

m1 <- matrix(0L, nrow=15, ncol=6)
m1[c(2, 6, 12:17, 22:33, 55, 59:62, 90)] <- 101:126
svt1 <- as(m1, "SVT_SparseMatrix")

set.seed(333)
svt2 <- poissonSparseMatrix(nrow=6, ncol=7, density=0.2)

svt1 %*% svt2
#>       [,1] [,2] [,3] [,4] [,5] [,6] [,7]
#>  [1,]  118    0    0    0    0    0    0
#>  [2,]  119    0  101    0    0    0  101
#>  [3,]  120    0    0    0    0    0    0
#>  [4,]    0    0    0    0    0    0    0
#>  [5,]    0    0    0    0    0    0    0
#>  [6,]    0    0  102    0    0    0  102
#>  [7,]    0    0    0    0    0    0    0
#>  [8,]    0    0    0    0    0    0    0
#>  [9,]    0    0    0    0    0    0    0
#> [10,]    0    0  121    0    0    0    0
#> [11,]    0    0    0    0    0    0    0
#> [12,]    0    0  103    0    0    0  103
#> [13,]    0    0  104    0    0    0  104
#> [14,]    0    0  227    0    0    0  105
#> [15,]    0    0  355    0  126    0  232
m1   %*% svt2
#>       [,1] [,2] [,3] [,4] [,5] [,6] [,7]
#>  [1,]  118    0    0    0    0    0    0
#>  [2,]  119    0  101    0    0    0  101
#>  [3,]  120    0    0    0    0    0    0
#>  [4,]    0    0    0    0    0    0    0
#>  [5,]    0    0    0    0    0    0    0
#>  [6,]    0    0  102    0    0    0  102
#>  [7,]    0    0    0    0    0    0    0
#>  [8,]    0    0    0    0    0    0    0
#>  [9,]    0    0    0    0    0    0    0
#> [10,]    0    0  121    0    0    0    0
#> [11,]    0    0    0    0    0    0    0
#> [12,]    0    0  103    0    0    0  103
#> [13,]    0    0  104    0    0    0  104
#> [14,]    0    0  227    0    0    0  105
#> [15,]    0    0  355    0  126    0  232

## Unary crossprod() and tcrossprod():
crossprod(svt1)               # same as t(svt1) %*% svt1
#>       [,1]   [,2]  [,3]  [,4]  [,5]  [,6]
#> [1,] 64291  59192 12019 25848 12625 13356
#> [2,] 59192 138094 25478 42095 26768 14742
#> [3,] 12019  25478 42485     0 29507     0
#> [4,] 25848  42095     0 44654     0 15498
#> [5,] 12625  26768 29507     0 31001     0
#> [6,] 13356  14742     0 15498     0 15876
tcrossprod(svt1)              # same as svt1 %*% t(svt1)
#>        [,1]  [,2]  [,3] [,4] [,5]  [,6]  [,7]  [,8]  [,9] [,10] [,11] [,12]
#>  [1,] 40749 41098 14160    0    0     0 11663 11770 11877 11984 12091 12198
#>  [2,] 41098 51651 14280    0    0 10302 11772 11880 11988 12096 12204 22715
#>  [3,] 14160 14280 14400    0    0     0     0     0     0     0     0     0
#>  [4,]     0     0     0    0    0     0     0     0     0     0     0     0
#>  [5,]     0     0     0    0    0     0     0     0     0     0     0     0
#>  [6,]     0 10302     0    0    0 10404     0     0     0     0     0 10506
#>  [7,] 11663 11772     0    0    0     0 11881 11990 12099 12208 12317 12426
#>  [8,] 11770 11880     0    0    0     0 11990 12100 12210 12320 12430 12540
#>  [9,] 11877 11988     0    0    0     0 12099 12210 12321 12432 12543 12654
#> [10,] 11984 12096     0    0    0     0 12208 12320 12432 27185 12656 12768
#> [11,] 12091 12204     0    0    0     0 12317 12430 12543 12656 12769 12882
#> [12,] 12198 22715     0    0    0 10506 12426 12540 12654 12768 12882 23605
#> [13,] 12305 22924     0    0    0 10608 12535 12650 12765 12880 12995 23822
#> [14,] 12412 23133     0    0    0 10710 12644 12760 12876 27754 13108 24039
#> [15,] 12519 23342     0    0    0 10812 12753 12870 12987 27987 13221 24256
#>       [,13] [,14] [,15]
#>  [1,] 12305 12412 12519
#>  [2,] 22924 23133 23342
#>  [3,]     0     0     0
#>  [4,]     0     0     0
#>  [5,]     0     0     0
#>  [6,] 10608 10710 10812
#>  [7,] 12535 12644 12753
#>  [8,] 12650 12760 12870
#>  [9,] 12765 12876 12987
#> [10,] 12880 27754 27987
#> [11,] 12995 13108 13221
#> [12,] 23822 24039 24256
#> [13,] 24041 24260 24479
#> [14,] 24260 39365 39708
#> [15,] 24479 39708 55930

## Binary crossprod() and tcrossprod():
crossprod(svt1[1:6, ], svt2)  # same as t(svt1[1:6, ]) %*% svt2
#>      [,1] [,2] [,3] [,4] [,5] [,6] [,7]
#> [1,]    0    0  102    0  102    0  102
#> [2,]    0    0  107    0    0    0  107
#> [3,]  120    0  118    0    0    0  118
#> [4,]    0    0    0    0    0    0    0
#> [5,]    0    0  124    0    0    0  124
#> [6,]    0    0    0    0    0    0    0
tcrossprod(svt1, t(svt2))     # same as svt1 %*% svt2
#>       [,1] [,2] [,3] [,4] [,5] [,6] [,7]
#>  [1,]  118    0    0    0    0    0    0
#>  [2,]  119    0  101    0    0    0  101
#>  [3,]  120    0    0    0    0    0    0
#>  [4,]    0    0    0    0    0    0    0
#>  [5,]    0    0    0    0    0    0    0
#>  [6,]    0    0  102    0    0    0  102
#>  [7,]    0    0    0    0    0    0    0
#>  [8,]    0    0    0    0    0    0    0
#>  [9,]    0    0    0    0    0    0    0
#> [10,]    0    0  121    0    0    0    0
#> [11,]    0    0    0    0    0    0    0
#> [12,]    0    0  103    0    0    0  103
#> [13,]    0    0  104    0    0    0  104
#> [14,]    0    0  227    0    0    0  105
#> [15,]    0    0  355    0  126    0  232

## Sanity checks:
m12 <- m1 %*% as.matrix(svt2)
stopifnot(
  identical(svt1 %*% svt2, m12),
  identical(m1 %*% svt2, m12),
  identical(crossprod(svt1), t(svt1) %*% svt1),
  identical(tcrossprod(svt1), svt1 %*% t(svt1)),
  identical(crossprod(svt1[1:6, ], svt2), t(svt1[1:6, ]) %*% svt2),
  identical(tcrossprod(svt1, t(svt2)), m12)
)