Skip to contents

SparseArray derivatives support operations from the Arith group, with some restrictions. See ?S4groupGeneric in the methods package for more information about the Arith group generic.

IMPORTANT NOTES:

Details

Two forms of 'Arith' operations are supported:

  1. Between an SVT_SparseArray object svt and an atomic vector y:

        svt op y
        y op svt

    The Arith operations that support this form are: *, /, ^, %%,%/%. Note that, except for * (for which both svt * y and y * svt are supported), atomic vector y must be on the right e.g. svt ^ 3.

  2. Between two SVT_SparseArray objects svt1 and svt2 of same dimensions (a.k.a. conformable arrays):

        svt1 op svt2

    The Arith operations that support this form are: +, -, *.

Value

A SparseArray derivative of the same dimensions as the input object(s).

See also

Examples

## ---------------------------------------------------------------------
## Basic examples
## ---------------------------------------------------------------------

svt1 <- SVT_SparseArray(dim=c(15, 6), type="integer")
svt1[cbind(1:15, 2)] <- 100:114
svt1[cbind(1:15, 5)] <- -(114:100)
svt1
#> <15 x 6 SparseMatrix> of type "integer" [nzcount=30 (33%)]:
#>       [,1] [,2] [,3] [,4] [,5] [,6]
#>  [1,]    0  100    0    0 -114    0
#>  [2,]    0  101    0    0 -113    0
#>  [3,]    0  102    0    0 -112    0
#>  [4,]    0  103    0    0 -111    0
#>  [5,]    0  104    0    0 -110    0
#>   ...    .    .    .    .    .    .
#> [11,]    0  110    0    0 -104    0
#> [12,]    0  111    0    0 -103    0
#> [13,]    0  112    0    0 -102    0
#> [14,]    0  113    0    0 -101    0
#> [15,]    0  114    0    0 -100    0

svt1 * -0.01
#> <15 x 6 SparseMatrix> of type "double" [nzcount=30 (33%)]:
#>        [,1]  [,2]  [,3]  [,4]  [,5]  [,6]
#>  [1,]  0.00 -1.00  0.00  0.00  1.14  0.00
#>  [2,]  0.00 -1.01  0.00  0.00  1.13  0.00
#>  [3,]  0.00 -1.02  0.00  0.00  1.12  0.00
#>  [4,]  0.00 -1.03  0.00  0.00  1.11  0.00
#>  [5,]  0.00 -1.04  0.00  0.00  1.10  0.00
#>   ...     .     .     .     .     .     .
#> [11,]  0.00 -1.10  0.00  0.00  1.04  0.00
#> [12,]  0.00 -1.11  0.00  0.00  1.03  0.00
#> [13,]  0.00 -1.12  0.00  0.00  1.02  0.00
#> [14,]  0.00 -1.13  0.00  0.00  1.01  0.00
#> [15,]  0.00 -1.14  0.00  0.00  1.00  0.00

svt1 * 10   # result is of type "double"
#> <15 x 6 SparseMatrix> of type "double" [nzcount=30 (33%)]:
#>        [,1]  [,2]  [,3]  [,4]  [,5]  [,6]
#>  [1,]     0  1000     0     0 -1140     0
#>  [2,]     0  1010     0     0 -1130     0
#>  [3,]     0  1020     0     0 -1120     0
#>  [4,]     0  1030     0     0 -1110     0
#>  [5,]     0  1040     0     0 -1100     0
#>   ...     .     .     .     .     .     .
#> [11,]     0  1100     0     0 -1040     0
#> [12,]     0  1110     0     0 -1030     0
#> [13,]     0  1120     0     0 -1020     0
#> [14,]     0  1130     0     0 -1010     0
#> [15,]     0  1140     0     0 -1000     0
svt1 * 10L  # result is of type "integer"
#> <15 x 6 SparseMatrix> of type "integer" [nzcount=30 (33%)]:
#>        [,1]  [,2]  [,3]  [,4]  [,5]  [,6]
#>  [1,]     0  1000     0     0 -1140     0
#>  [2,]     0  1010     0     0 -1130     0
#>  [3,]     0  1020     0     0 -1120     0
#>  [4,]     0  1030     0     0 -1110     0
#>  [5,]     0  1040     0     0 -1100     0
#>   ...     .     .     .     .     .     .
#> [11,]     0  1100     0     0 -1040     0
#> [12,]     0  1110     0     0 -1030     0
#> [13,]     0  1120     0     0 -1020     0
#> [14,]     0  1130     0     0 -1010     0
#> [15,]     0  1140     0     0 -1000     0

svt1 * c(10, 5, 0.1)  # right vector recycled along 1st dimension
#> <15 x 6 SparseMatrix> of type "double" [nzcount=30 (33%)]:
#>          [,1]    [,2]    [,3]    [,4]    [,5]    [,6]
#>  [1,]     0.0  1000.0     0.0     0.0 -1140.0     0.0
#>  [2,]     0.0   505.0     0.0     0.0  -565.0     0.0
#>  [3,]     0.0    10.2     0.0     0.0   -11.2     0.0
#>  [4,]     0.0  1030.0     0.0     0.0 -1110.0     0.0
#>  [5,]     0.0   520.0     0.0     0.0  -550.0     0.0
#>   ...       .       .       .       .       .       .
#> [11,]     0.0   550.0     0.0     0.0  -520.0     0.0
#> [12,]     0.0    11.1     0.0     0.0   -10.3     0.0
#> [13,]     0.0  1120.0     0.0     0.0 -1020.0     0.0
#> [14,]     0.0   565.0     0.0     0.0  -505.0     0.0
#> [15,]     0.0    11.4     0.0     0.0   -10.0     0.0

svt1 / 10L
#> <15 x 6 SparseMatrix> of type "double" [nzcount=30 (33%)]:
#>        [,1]  [,2]  [,3]  [,4]  [,5]  [,6]
#>  [1,]   0.0  10.0   0.0   0.0 -11.4   0.0
#>  [2,]   0.0  10.1   0.0   0.0 -11.3   0.0
#>  [3,]   0.0  10.2   0.0   0.0 -11.2   0.0
#>  [4,]   0.0  10.3   0.0   0.0 -11.1   0.0
#>  [5,]   0.0  10.4   0.0   0.0 -11.0   0.0
#>   ...     .     .     .     .     .     .
#> [11,]   0.0  11.0   0.0   0.0 -10.4   0.0
#> [12,]   0.0  11.1   0.0   0.0 -10.3   0.0
#> [13,]   0.0  11.2   0.0   0.0 -10.2   0.0
#> [14,]   0.0  11.3   0.0   0.0 -10.1   0.0
#> [15,]   0.0  11.4   0.0   0.0 -10.0   0.0

svt1 ^ 2
#> <15 x 6 SparseMatrix> of type "double" [nzcount=30 (33%)]:
#>        [,1]  [,2]  [,3]  [,4]  [,5]  [,6]
#>  [1,]     0 10000     0     0 12996     0
#>  [2,]     0 10201     0     0 12769     0
#>  [3,]     0 10404     0     0 12544     0
#>  [4,]     0 10609     0     0 12321     0
#>  [5,]     0 10816     0     0 12100     0
#>   ...     .     .     .     .     .     .
#> [11,]     0 12100     0     0 10816     0
#> [12,]     0 12321     0     0 10609     0
#> [13,]     0 12544     0     0 10404     0
#> [14,]     0 12769     0     0 10201     0
#> [15,]     0 12996     0     0 10000     0
svt1 ^ (2:6)
#> <15 x 6 SparseMatrix> of type "double" [nzcount=30 (33%)]:
#>                [,1]          [,2]          [,3]          [,4]          [,5]
#>  [1,]             0         10000             0             0         12996
#>  [2,]             0       1030301             0             0      -1442897
#>  [3,]             0     108243216             0             0     157351936
#>  [4,]             0   11592740743             0             0  -16850581551
#>  [5,]             0 1265319018496             0             0 1771561000000
#>   ...             .             .             .             .             .
#> [11,]             0         12100             0             0         10816
#> [12,]             0       1367631             0             0      -1092727
#> [13,]             0     157351936             0             0     108243216
#> [14,]             0   18424351793             0             0  -10510100501
#> [15,]             0 2194972623936             0             0 1000000000000
#>                [,6]
#>  [1,]             0
#>  [2,]             0
#>  [3,]             0
#>  [4,]             0
#>  [5,]             0
#>   ...             .
#> [11,]             0
#> [12,]             0
#> [13,]             0
#> [14,]             0
#> [15,]             0

svt1 %% 5L
#> <15 x 6 SparseMatrix> of type "integer" [nzcount=24 (27%)]:
#>       [,1] [,2] [,3] [,4] [,5] [,6]
#>  [1,]    0    0    0    0    1    0
#>  [2,]    0    1    0    0    2    0
#>  [3,]    0    2    0    0    3    0
#>  [4,]    0    3    0    0    4    0
#>  [5,]    0    4    0    0    0    0
#>   ...    .    .    .    .    .    .
#> [11,]    0    0    0    0    1    0
#> [12,]    0    1    0    0    2    0
#> [13,]    0    2    0    0    3    0
#> [14,]    0    3    0    0    4    0
#> [15,]    0    4    0    0    0    0
svt1 %/% 5L
#> <15 x 6 SparseMatrix> of type "integer" [nzcount=30 (33%)]:
#>       [,1] [,2] [,3] [,4] [,5] [,6]
#>  [1,]    0   20    0    0  -23    0
#>  [2,]    0   20    0    0  -23    0
#>  [3,]    0   20    0    0  -23    0
#>  [4,]    0   20    0    0  -23    0
#>  [5,]    0   20    0    0  -22    0
#>   ...    .    .    .    .    .    .
#> [11,]    0   22    0    0  -21    0
#> [12,]    0   22    0    0  -21    0
#> [13,]    0   22    0    0  -21    0
#> [14,]    0   22    0    0  -21    0
#> [15,]    0   22    0    0  -20    0

svt2 <- SVT_SparseArray(dim=dim(svt1), type="double")
svt2[c(2, 6, 12:17, 22:33, 55, 59:62, 90)] <- runif(26)
svt2
#> <15 x 6 SparseMatrix> of type "double" [nzcount=26 (29%)]:
#>            [,1]      [,2]      [,3]      [,4]      [,5]      [,6]
#>  [1,] 0.0000000 0.8143864 0.6549458 0.0000000 0.7607204 0.0000000
#>  [2,] 0.6247395 0.5103984 0.7155539 0.0000000 0.6053032 0.0000000
#>  [3,] 0.0000000 0.0000000 0.4122341 0.0000000 0.0000000 0.0000000
#>  [4,] 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
#>  [5,] 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
#>   ...         .         .         .         .         .         .
#> [11,] 0.0000000 0.1516054 0.0000000 0.0000000 0.0000000 0.0000000
#> [12,] 0.6009843 0.8188669 0.0000000 0.0000000 0.0000000 0.0000000
#> [13,] 0.4543698 0.1019313 0.0000000 0.0000000 0.0000000 0.0000000
#> [14,] 0.8167198 0.6939910 0.0000000 0.6505006 0.0000000 0.0000000
#> [15,] 0.5466602 0.1770074 0.0000000 0.3788358 0.0000000 0.6604255

svt1 + svt2
#> <15 x 6 SparseMatrix> of type "double" [nzcount=43 (48%)]:
#>               [,1]         [,2]         [,3]         [,4]         [,5]
#>  [1,]    0.0000000  100.8143864    0.6549458    0.0000000 -113.2392796
#>  [2,]    0.6247395  101.5103984    0.7155539    0.0000000 -112.3946968
#>  [3,]    0.0000000  102.0000000    0.4122341    0.0000000 -112.0000000
#>  [4,]    0.0000000  103.0000000    0.0000000    0.0000000 -111.0000000
#>  [5,]    0.0000000  104.0000000    0.0000000    0.0000000 -110.0000000
#>   ...            .            .            .            .            .
#> [11,]    0.0000000  110.1516054    0.0000000    0.0000000 -104.0000000
#> [12,]    0.6009843  111.8188669    0.0000000    0.0000000 -103.0000000
#> [13,]    0.4543698  112.1019313    0.0000000    0.0000000 -102.0000000
#> [14,]    0.8167198  113.6939910    0.0000000    0.6505006 -101.0000000
#> [15,]    0.5466602  114.1770074    0.0000000    0.3788358 -100.0000000
#>               [,6]
#>  [1,]    0.0000000
#>  [2,]    0.0000000
#>  [3,]    0.0000000
#>  [4,]    0.0000000
#>  [5,]    0.0000000
#>   ...            .
#> [11,]    0.0000000
#> [12,]    0.0000000
#> [13,]    0.0000000
#> [14,]    0.0000000
#> [15,]    0.6604255
svt1 - svt2
#> <15 x 6 SparseMatrix> of type "double" [nzcount=43 (48%)]:
#>               [,1]         [,2]         [,3]         [,4]         [,5]
#>  [1,]    0.0000000   99.1856136   -0.6549458    0.0000000 -114.7607204
#>  [2,]   -0.6247395  100.4896016   -0.7155539    0.0000000 -113.6053032
#>  [3,]    0.0000000  102.0000000   -0.4122341    0.0000000 -112.0000000
#>  [4,]    0.0000000  103.0000000    0.0000000    0.0000000 -111.0000000
#>  [5,]    0.0000000  104.0000000    0.0000000    0.0000000 -110.0000000
#>   ...            .            .            .            .            .
#> [11,]    0.0000000  109.8483946    0.0000000    0.0000000 -104.0000000
#> [12,]   -0.6009843  110.1811331    0.0000000    0.0000000 -103.0000000
#> [13,]   -0.4543698  111.8980687    0.0000000    0.0000000 -102.0000000
#> [14,]   -0.8167198  112.3060090    0.0000000   -0.6505006 -101.0000000
#> [15,]   -0.5466602  113.8229926    0.0000000   -0.3788358 -100.0000000
#>               [,6]
#>  [1,]    0.0000000
#>  [2,]    0.0000000
#>  [3,]    0.0000000
#>  [4,]    0.0000000
#>  [5,]    0.0000000
#>   ...            .
#> [11,]    0.0000000
#> [12,]    0.0000000
#> [13,]    0.0000000
#> [14,]    0.0000000
#> [15,]   -0.6604255
svt1 * svt2
#> <15 x 6 SparseMatrix> of type "double" [nzcount=13 (14%)]:
#>            [,1]      [,2]      [,3]      [,4]      [,5]      [,6]
#>  [1,]   0.00000  81.43864   0.00000   0.00000 -86.72212   0.00000
#>  [2,]   0.00000  51.55024   0.00000   0.00000 -68.39926   0.00000
#>  [3,]   0.00000   0.00000   0.00000   0.00000   0.00000   0.00000
#>  [4,]   0.00000   0.00000   0.00000   0.00000   0.00000   0.00000
#>  [5,]   0.00000   0.00000   0.00000   0.00000   0.00000   0.00000
#>   ...         .         .         .         .         .         .
#> [11,]   0.00000  16.67660   0.00000   0.00000   0.00000   0.00000
#> [12,]   0.00000  90.89423   0.00000   0.00000   0.00000   0.00000
#> [13,]   0.00000  11.41630   0.00000   0.00000   0.00000   0.00000
#> [14,]   0.00000  78.42099   0.00000   0.00000   0.00000   0.00000
#> [15,]   0.00000  20.17884   0.00000   0.00000   0.00000   0.00000

svt2 * (0.1 * svt1 - svt2 ^ 2) + svt1 / sum(svt2)
#> <15 x 6 SparseMatrix> of type "double" [nzcount=43 (48%)]:
#>               [,1]         [,2]         [,3]         [,4]         [,5]
#>  [1,]   0.00000000  14.93088724  -0.28094156   0.00000000 -17.46538319
#>  [2,]  -0.24383548  12.42247816  -0.36637600   0.00000000 -15.34137798
#>  [3,]   0.00000000   7.47368787  -0.07005381   0.00000000  -8.20640237
#>  [4,]   0.00000000   7.54695932   0.00000000   0.00000000  -8.13313092
#>  [5,]   0.00000000   7.62023077   0.00000000   0.00000000  -8.05985947
#>   ...            .            .            .            .            .
#> [11,]   0.00000000   9.72403482   0.00000000   0.00000000  -7.62023077
#> [12,]  -0.21706475  16.67346812   0.00000000   0.00000000  -7.54695932
#> [13,]  -0.09380551   9.34697342   0.00000000   0.00000000  -7.47368787
#> [14,]  -0.54477754  15.78753011   0.00000000  -0.27525999  -7.40041642
#> [15,]  -0.16336246  10.36528315   0.00000000  -0.05436923  -7.32714497
#>               [,6]
#>  [1,]   0.00000000
#>  [2,]   0.00000000
#>  [3,]   0.00000000
#>  [4,]   0.00000000
#>  [5,]   0.00000000
#>   ...            .
#> [11,]   0.00000000
#> [12,]   0.00000000
#> [13,]   0.00000000
#> [14,]   0.00000000
#> [15,]  -0.28805240

## Sanity checks:
m1 <- as.matrix(svt1)
m2 <- as.matrix(svt2)
stopifnot(
  identical(as.matrix(svt1 * -0.01), m1 * -0.01),
  identical(as.matrix(svt1 * 10), m1 * 10),
  identical(as.matrix(svt1 * 10L), m1 * 10L),
  identical(as.matrix(svt1 / 10L), m1 / 10L),
  identical(as.matrix(svt1 ^ 3.5), m1 ^ 3.5),
  identical(as.matrix(svt1 %% 5L), m1 %% 5L),
  identical(as.matrix(svt1 %/% 5L), m1 %/% 5L),
  identical(as.matrix(svt1 + svt2), m1 + m2),
  identical(as.matrix(svt1 - svt2), m1 - m2),
  identical(as.matrix(svt1 * svt2), m1 * m2),
  all.equal(as.matrix(svt2 * (0.1 * svt1 - svt2 ^ 2) + svt1 / sum(svt2)),
            m2 * (0.1 * m1 - m2 ^ 2) + m1 / sum(m2))
)

## ---------------------------------------------------------------------
## An example combining operations from the 'Arith', 'Compare',
## and 'Logic' groups
## ---------------------------------------------------------------------

m3 <- matrix(0L, nrow=15, ncol=6)
m3[c(2, 6, 12:17, 22:33, 55, 59:62, 90)] <- 101:126
svt3 <- SparseArray(m3)

## Can be 5x or 10x faster than with a dgCMatrix object on a big
## SVT_SparseMatrix object!
svt4 <- (svt3^1.5 + svt3) %% 100 - 0.2 * svt3 > 0
svt4
#> <15 x 6 SparseMatrix> of type "logical" [nzcount=20 (22%)]:
#>        [,1]  [,2]  [,3]  [,4]  [,5]  [,6]
#>  [1,] FALSE FALSE  TRUE FALSE FALSE FALSE
#>  [2,] FALSE  TRUE FALSE FALSE FALSE FALSE
#>  [3,] FALSE FALSE  TRUE FALSE FALSE FALSE
#>  [4,] FALSE FALSE FALSE FALSE FALSE FALSE
#>  [5,] FALSE FALSE FALSE FALSE FALSE FALSE
#>   ...     .     .     .     .     .     .
#> [11,] FALSE FALSE FALSE FALSE FALSE FALSE
#> [12,]  TRUE  TRUE FALSE FALSE FALSE FALSE
#> [13,]  TRUE  TRUE FALSE FALSE FALSE FALSE
#> [14,]  TRUE  TRUE FALSE  TRUE FALSE FALSE
#> [15,]  TRUE  TRUE FALSE  TRUE FALSE  TRUE

## Sanity check:
m4 <- (m3^1.5 + m3) %% 100 - 0.2 * m3 > 0
stopifnot(identical(as.matrix(svt4), m4))