R: Mathematical operators on sparse matrices and sparse vectors

operators {MatrixExtra} | R Documentation

## Mathematical operators on sparse matrices and sparse vectors

### Description

Implements some mathematical operators between sparse-sparse and sparse-dense matrices and vectors, such as 'CSR + CSR', 'CSR + COO', 'CSR * vector', 'CSR * dense', among others, which typically work natively in the storage order of the inputs without data duplication.

### Usage

    ## S4 method for signature 'RsparseMatrix,sparseMatrix'
    e1 * e2

    ## S4 method for signature 'ngRMatrix,sparseMatrix'
    e1 * e2

    ## S4 method for signature 'lgRMatrix,sparseMatrix'
    e1 * e2

    ## S4 method for signature 'sparseMatrix,RsparseMatrix'
    e1 * e2

    ## S4 method for signature 'sparseMatrix,ngRMatrix'
    e1 * e2

    ## S4 method for signature 'sparseMatrix,lgRMatrix'
    e1 * e2

    ## S4 method for signature 'CsparseMatrix,TsparseMatrix'
    e1 * e2

    ## S4 method for signature 'TsparseMatrix,CsparseMatrix'
    e1 * e2

    ## S4 method for signature 'RsparseMatrix,sparseMatrix'
    e1 & e2

    ## S4 method for signature 'ngRMatrix,sparseMatrix'
    e1 & e2

    ## S4 method for signature 'lgRMatrix,sparseMatrix'
    e1 & e2

    ## S4 method for signature 'sparseMatrix,RsparseMatrix'
    e1 & e2

    ## S4 method for signature 'sparseMatrix,ngRMatrix'
    e1 & e2

    ## S4 method for signature 'sparseMatrix,lgRMatrix'
    e1 & e2

    ## S4 method for signature 'CsparseMatrix,TsparseMatrix'
    e1 & e2

    ## S4 method for signature 'TsparseMatrix,CsparseMatrix'
    e1 & e2

    ## S4 method for signature 'RsparseMatrix,matrix'
    e1 * e2

    ## S4 method for signature 'ngRMatrix,matrix'
    e1 * e2

    ## S4 method for signature 'lgRMatrix,matrix'
    e1 * e2

    ## S4 method for signature 'RsparseMatrix,float32'
    e1 * e2

    ## S4 method for signature 'ngRMatrix,float32'
    e1 * e2

    ## S4 method for signature 'lgRMatrix,float32'
    e1 * e2

    ## S4 method for signature 'matrix,RsparseMatrix'
    e1 * e2

    ## S4 method for signature 'matrix,ngRMatrix'
    e1 * e2

    ## S4 method for signature 'matrix,lgRMatrix'
    e1 * e2

    ## S4 method for signature 'float32,RsparseMatrix'
    e1 * e2

    ## S4 method for signature 'float32,ngRMatrix'
    e1 * e2

    ## S4 method for signature 'float32,lgRMatrix'
    e1 * e2

    ## S4 method for signature 'RsparseMatrix,matrix'
    e1 & e2

    ## S4 method for signature 'ngRMatrix,matrix'
    e1 & e2

    ## S4 method for signature 'lgRMatrix,matrix'
    e1 & e2

    ## S4 method for signature 'matrix,RsparseMatrix'
    e1 & e2

    ## S4 method for signature 'matrix,ngRMatrix'
    e1 & e2

    ## S4 method for signature 'matrix,lgRMatrix'
    e1 & e2

    ## S4 method for signature 'TsparseMatrix,matrix'
    e1 * e2

    ## S4 method for signature 'TsparseMatrix,float32'
    e1 * e2

    ## S4 method for signature 'ngTMatrix,matrix'
    e1 * e2

    ## S4 method for signature 'lgTMatrix,matrix'
    e1 * e2

    ## S4 method for signature 'ngTMatrix,float32'
    e1 * e2

    ## S4 method for signature 'lgTMatrix,float32'
    e1 * e2

    ## S4 method for signature 'matrix,TsparseMatrix'
    e1 * e2

    ## S4 method for signature 'float32,TsparseMatrix'
    e1 * e2

    ## S4 method for signature 'matrix,ngTMatrix'
    e1 * e2

    ## S4 method for signature 'matrix,lgTMatrix'
    e1 * e2

    ## S4 method for signature 'float32,ngTMatrix'
    e1 * e2

    ## S4 method for signature 'float32,lgTMatrix'
    e1 * e2

    ## S4 method for signature 'TsparseMatrix,matrix'
    e1 & e2

    ## S4 method for signature 'ngTMatrix,matrix'
    e1 & e2

    ## S4 method for signature 'lgTMatrix,matrix'
    e1 & e2

    ## S4 method for signature 'matrix,TsparseMatrix'
    e1 & e2

    ## S4 method for signature 'matrix,ngTMatrix'
    e1 & e2

    ## S4 method for signature 'matrix,lgTMatrix'
    e1 & e2

    ## S4 method for signature 'CsparseMatrix,matrix'
    e1 * e2

    ## S4 method for signature 'CsparseMatrix,float32'
    e1 * e2

    ## S4 method for signature 'matrix,CsparseMatrix'
    e1 * e2

    ## S4 method for signature 'float32,CsparseMatrix'
    e1 * e2

    ## S4 method for signature 'CsparseMatrix,matrix'
    e1 & e2

    ## S4 method for signature 'CsparseMatrix,float32'
    e1 & e2

    ## S4 method for signature 'matrix,CsparseMatrix'
    e1 & e2

    ## S4 method for signature 'float32,CsparseMatrix'
    e1 & e2

    ## S4 method for signature 'RsparseMatrix,sparseMatrix'
    e1 + e2

    ## S4 method for signature 'ngRMatrix,sparseMatrix'
    e1 + e2

    ## S4 method for signature 'lgRMatrix,sparseMatrix'
    e1 + e2

    ## S4 method for signature 'sparseMatrix,RsparseMatrix'
    e1 + e2

    ## S4 method for signature 'sparseMatrix,ngRMatrix'
    e1 + e2

    ## S4 method for signature 'sparseMatrix,lgRMatrix'
    e1 + e2

    ## S4 method for signature 'CsparseMatrix,TsparseMatrix'
    e1 + e2

    ## S4 method for signature 'TsparseMatrix,CsparseMatrix'
    e1 + e2

    ## S4 method for signature 'RsparseMatrix,sparseMatrix'
    e1 - e2

    ## S4 method for signature 'ngRMatrix,sparseMatrix'
    e1 - e2

    ## S4 method for signature 'lgRMatrix,sparseMatrix'
    e1 - e2

    ## S4 method for signature 'sparseMatrix,RsparseMatrix'
    e1 - e2

    ## S4 method for signature 'sparseMatrix,ngRMatrix'
    e1 - e2

    ## S4 method for signature 'sparseMatrix,lgRMatrix'
    e1 - e2

    ## S4 method for signature 'CsparseMatrix,TsparseMatrix'
    e1 - e2

    ## S4 method for signature 'TsparseMatrix,CsparseMatrix'
    e1 - e2

    ## S4 method for signature 'RsparseMatrix,sparseMatrix'
    e1 | e2

    ## S4 method for signature 'ngRMatrix,sparseMatrix'
    e1 | e2

    ## S4 method for signature 'lgRMatrix,sparseMatrix'
    e1 | e2

    ## S4 method for signature 'sparseMatrix,RsparseMatrix'
    e1 | e2

    ## S4 method for signature 'sparseMatrix,ngRMatrix'
    e1 | e2

    ## S4 method for signature 'sparseMatrix,lgRMatrix'
    e1 | e2

    ## S4 method for signature 'CsparseMatrix,TsparseMatrix'
    e1 | e2

    ## S4 method for signature 'TsparseMatrix,CsparseMatrix'
    e1 | e2

    ## S4 method for signature 'RsparseMatrix,integer'
    e1 * e2

    ## S4 method for signature 'RsparseMatrix,numeric'
    e1 * e2

    ## S4 method for signature 'RsparseMatrix,logical'
    e1 * e2

    ## S4 method for signature 'integer,RsparseMatrix'
    e1 * e2

    ## S4 method for signature 'numeric,RsparseMatrix'
    e1 * e2

    ## S4 method for signature 'logical,RsparseMatrix'
    e1 * e2

    ## S4 method for signature 'RsparseMatrix,integer'
    e1 & e2

    ## S4 method for signature 'RsparseMatrix,numeric'
    e1 & e2

    ## S4 method for signature 'RsparseMatrix,logical'
    e1 & e2

    ## S4 method for signature 'integer,RsparseMatrix'
    e1 & e2

    ## S4 method for signature 'numeric,RsparseMatrix'
    e1 & e2

    ## S4 method for signature 'logical,RsparseMatrix'
    e1 & e2

    ## S4 method for signature 'RsparseMatrix,integer'
    e1 / e2

    ## S4 method for signature 'RsparseMatrix,numeric'
    e1 / e2

    ## S4 method for signature 'RsparseMatrix,logical'
    e1 / e2

    ## S4 method for signature 'RsparseMatrix,matrix'
    e1 / e2

    ## S4 method for signature 'integer,RsparseMatrix'
    e1 / e2

    ## S4 method for signature 'numeric,RsparseMatrix'
    e1 / e2

    ## S4 method for signature 'logical,RsparseMatrix'
    e1 / e2

    ## S4 method for signature 'matrix,RsparseMatrix'
    e1 / e2

    ## S4 method for signature 'RsparseMatrix,integer'
    e1 %% e2

    ## S4 method for signature 'RsparseMatrix,numeric'
    e1 %% e2

    ## S4 method for signature 'RsparseMatrix,logical'
    e1 %% e2

    ## S4 method for signature 'RsparseMatrix,matrix'
    e1 %% e2

    ## S4 method for signature 'integer,RsparseMatrix'
    e1 %% e2

    ## S4 method for signature 'numeric,RsparseMatrix'
    e1 %% e2

    ## S4 method for signature 'logical,RsparseMatrix'
    e1 %% e2

    ## S4 method for signature 'matrix,RsparseMatrix'
    e1 %% e2

    ## S4 method for signature 'RsparseMatrix,integer'
    e1 %/% e2

    ## S4 method for signature 'RsparseMatrix,numeric'
    e1 %/% e2

    ## S4 method for signature 'RsparseMatrix,logical'
    e1 %/% e2

    ## S4 method for signature 'RsparseMatrix,matrix'
    e1 %/% e2

    ## S4 method for signature 'integer,RsparseMatrix'
    e1 %/% e2

    ## S4 method for signature 'numeric,RsparseMatrix'
    e1 %/% e2

    ## S4 method for signature 'logical,RsparseMatrix'
    e1 %/% e2

    ## S4 method for signature 'matrix,RsparseMatrix'
    e1 %/% e2

    ## S4 method for signature 'RsparseMatrix,integer'
    e1 ^ e2

    ## S4 method for signature 'RsparseMatrix,numeric'
    e1 ^ e2

    ## S4 method for signature 'RsparseMatrix,logical'
    e1 ^ e2

    ## S4 method for signature 'RsparseMatrix,matrix'
    e1 ^ e2

    ## S4 method for signature 'integer,RsparseMatrix'
    e1 ^ e2

    ## S4 method for signature 'numeric,RsparseMatrix'
    e1 ^ e2

    ## S4 method for signature 'logical,RsparseMatrix'
    e1 ^ e2

    ## S4 method for signature 'matrix,RsparseMatrix'
    e1 ^ e2

    ## S4 method for signature 'TsparseMatrix,integer'
    e1 * e2

    ## S4 method for signature 'TsparseMatrix,numeric'
    e1 * e2

    ## S4 method for signature 'TsparseMatrix,logical'
    e1 * e2

    ## S4 method for signature 'integer,TsparseMatrix'
    e1 * e2

    ## S4 method for signature 'numeric,TsparseMatrix'
    e1 * e2

    ## S4 method for signature 'logical,TsparseMatrix'
    e1 * e2

    ## S4 method for signature 'TsparseMatrix,integer'
    e1 & e2

    ## S4 method for signature 'TsparseMatrix,numeric'
    e1 & e2

    ## S4 method for signature 'TsparseMatrix,logical'
    e1 & e2

    ## S4 method for signature 'integer,TsparseMatrix'
    e1 & e2

    ## S4 method for signature 'numeric,TsparseMatrix'
    e1 & e2

    ## S4 method for signature 'logical,TsparseMatrix'
    e1 & e2

    ## S4 method for signature 'TsparseMatrix,integer'
    e1 / e2

    ## S4 method for signature 'TsparseMatrix,numeric'
    e1 / e2

    ## S4 method for signature 'TsparseMatrix,logical'
    e1 / e2

    ## S4 method for signature 'TsparseMatrix,matrix'
    e1 / e2

    ## S4 method for signature 'integer,TsparseMatrix'
    e1 / e2

    ## S4 method for signature 'numeric,TsparseMatrix'
    e1 / e2

    ## S4 method for signature 'logical,TsparseMatrix'
    e1 / e2

    ## S4 method for signature 'matrix,TsparseMatrix'
    e1 / e2

    ## S4 method for signature 'TsparseMatrix,integer'
    e1 %% e2

    ## S4 method for signature 'TsparseMatrix,numeric'
    e1 %% e2

    ## S4 method for signature 'TsparseMatrix,logical'
    e1 %% e2

    ## S4 method for signature 'TsparseMatrix,matrix'
    e1 %% e2

    ## S4 method for signature 'integer,TsparseMatrix'
    e1 %% e2

    ## S4 method for signature 'numeric,TsparseMatrix'
    e1 %% e2

    ## S4 method for signature 'logical,TsparseMatrix'
    e1 %% e2

    ## S4 method for signature 'matrix,TsparseMatrix'
    e1 %% e2

    ## S4 method for signature 'TsparseMatrix,integer'
    e1 %/% e2

    ## S4 method for signature 'TsparseMatrix,numeric'
    e1 %/% e2

    ## S4 method for signature 'TsparseMatrix,logical'
    e1 %/% e2

    ## S4 method for signature 'TsparseMatrix,matrix'
    e1 %/% e2

    ## S4 method for signature 'integer,TsparseMatrix'
    e1 %/% e2

    ## S4 method for signature 'numeric,TsparseMatrix'
    e1 %/% e2

    ## S4 method for signature 'logical,TsparseMatrix'
    e1 %/% e2

    ## S4 method for signature 'matrix,TsparseMatrix'
    e1 %/% e2

    ## S4 method for signature 'TsparseMatrix,integer'
    e1 ^ e2

    ## S4 method for signature 'TsparseMatrix,numeric'
    e1 ^ e2

    ## S4 method for signature 'TsparseMatrix,logical'
    e1 ^ e2

    ## S4 method for signature 'TsparseMatrix,matrix'
    e1 ^ e2

    ## S4 method for signature 'integer,TsparseMatrix'
    e1 ^ e2

    ## S4 method for signature 'numeric,TsparseMatrix'
    e1 ^ e2

    ## S4 method for signature 'logical,TsparseMatrix'
    e1 ^ e2

    ## S4 method for signature 'matrix,TsparseMatrix'
    e1 ^ e2

    ## S4 method for signature 'RsparseMatrix,sparseVector'
    e1 * e2

    ## S4 method for signature 'sparseVector,RsparseMatrix'
    e1 * e2

    ## S4 method for signature 'matrix,sparseVector'
    e1 * e2

    ## S4 method for signature 'sparseVector,matrix'
    e1 * e2

    ## S4 method for signature 'float32,sparseVector'
    e1 * e2

    ## S4 method for signature 'sparseVector,float32'
    e1 * e2

### Arguments

`e1` | A sparse or dense matrix or vector/
`e2` | Another sparse or dense matrix or vector.

### Details

By default, when doing elementwise multiplication ('*') between a sparse and a dense matrix or vice-versa, if the dense matrix has missing values ('NA' / 'NaN') at some coordinate in which the sparse matrix has no present entry, the resulting output will not have an entry there either, which differs from the behavior of 'Matrix' and base R, but makes the operation much faster. The same applies to division by zero and exponentiation to zero.

If such missing values (or infinites and ones) are to be preserved, this behavior can be changed through the package options (i.e. 'options("MatrixExtra.ignore_na" = FALSE)' - see MatrixExtra-options).

The indices of the matrices might be sorted in-place for some operations (see sort_sparse_indices).

### Value

A CSR or COO matrix depending on the input type and operation. Some operations (blocked by default) will produce dense matrices as outputs.

### Examples

    library(Matrix)
    library(MatrixExtra)
    set.seed(1)
    X <- rsparsematrix(4, 3, .5, repr="R")
    options("MatrixExtra.quick_show" = FALSE)
    X + X
    X * X
    X * as.coo.matrix(X)
    X * 2
    X * 1:4
    X ^ 2
    X ^ (1:4)

    ### Beware
    set_new_matrix_behavior()
    print(suppressWarnings(X / 0))
    restore_old_matrix_behavior()
    print(suppressWarnings(X / 0))

[Package MatrixExtra version 0.1.9-1 Index]