Skip to contents

The COO_SparseArray class is a container for efficient in-memory representation of multidimensional sparse arrays. It uses the COO layout to represent the nonzero data internally.

A COO_SparseMatrix object is a COO_SparseArray object of 2 dimensions.

IMPORTANT NOTE: COO_SparseArray and COO_SparseMatrix objects are now superseded by the new and more efficient SVT_SparseArray and SVT_SparseMatrix objects.

Usage

## Constructor function:
COO_SparseArray(dim, nzcoo=NULL, nzdata=NULL, dimnames=NULL, check=TRUE)

## Getters (in addition to dim(), length(), and dimnames()):
nzcoo(x)
nzdata(x)

Arguments

dim

The dimensions (supplied as an integer vector) of the COO_SparseArray or COO_SparseMatrix object to construct.

nzcoo

A matrix containing the array coordinates of the nonzero elements.

This must be an integer matrix of array coordinates like one returned by base::arrayInd or S4Arrays::Lindex2Mindex, that is, a matrix with length(dim) columns and where each row is an n-tuple representing the coordinates of an array element.

nzdata

A vector (atomic or list) of length nrow(nzcoo) containing the nonzero elements.

dimnames

The dimnames of the object to construct. Must be NULL or a list of length the number of dimensions. Each list element must be either NULL or a character vector along the corresponding dimension.

check

Should the object be validated upon construction?

x

A COO_SparseArray or COO_SparseMatrix object.

Value

  • For COO_SparseArray(): A COO_SparseArray or COO_SparseMatrix object.

  • For nzcoo(): A matrix with one column per dimension containing the array coordinates of the nonzero elements.

  • For nzdata(): A vector parallel to nzcoo(x) (i.e. with one element per row in nzcoo(x)) containing the nonzero elements.

See also

  • The new SVT_SparseArray class for a replacement of of the COO_SparseArray class.

  • The SparseArray class for the virtual parent class of COO_SparseArray and SVT_SparseArray.

  • dgCMatrix-class and lgCMatrix-class in the Matrix package, for the de facto standard for sparse matrix representations in the R ecosystem.

  • base::arrayInd in the base package.

  • S4Arrays::Lindex2Mindex in the S4Arrays package for an improved (faster) version of base::arrayInd.

  • Ordinary array objects in base R.

Examples

## ---------------------------------------------------------------------
## EXAMPLE 1
## ---------------------------------------------------------------------
dim1 <- 5:3
nzcoo1 <- Lindex2Mindex(sample(60, 8), 5:3)
nzdata1 <- 11.11 * seq_len(nrow(nzcoo1))
coo1 <- COO_SparseArray(dim1, nzcoo1, nzdata1)
coo1
#> <5 x 4 x 3 SparseArray> of type "double" [nzcount=8 (13%)]:
#> ,,1
#>       [,1]  [,2]  [,3]  [,4]
#> [1,]  0.00  0.00  0.00  0.00
#> [2,]  0.00  0.00 33.33  0.00
#> [3,]  0.00  0.00  0.00  0.00
#> [4,]  0.00  0.00  0.00  0.00
#> [5,]  0.00  0.00  0.00  0.00
#> 
#> ,,2
#>       [,1]  [,2]  [,3]  [,4]
#> [1,]  0.00  0.00 88.88  0.00
#> [2,]  0.00  0.00  0.00 44.44
#> [3,] 22.22  0.00  0.00 55.55
#> [4,]  0.00  0.00  0.00  0.00
#> [5,]  0.00  0.00  0.00  0.00
#> 
#> ,,3
#>       [,1]  [,2]  [,3]  [,4]
#> [1,]  0.00  0.00  0.00  0.00
#> [2,]  0.00 66.66  0.00  0.00
#> [3,]  0.00  0.00  0.00  0.00
#> [4,]  0.00  0.00  0.00  0.00
#> [5,] 11.11  0.00 77.77  0.00
#> 

nzcoo(coo1)
#>      [,1] [,2] [,3]
#> [1,]    5    1    3
#> [2,]    3    1    2
#> [3,]    2    3    1
#> [4,]    2    4    2
#> [5,]    3    4    2
#> [6,]    2    2    3
#> [7,]    5    3    3
#> [8,]    1    3    2
nzdata(coo1)
#> [1] 11.11 22.22 33.33 44.44 55.55 66.66 77.77 88.88
type(coo1)
#> [1] "double"
sparsity(coo1)
#> [1] 0.8666667

as.array(coo1)  # back to a dense representation
#> , , 1
#> 
#>      [,1] [,2]  [,3] [,4]
#> [1,]    0    0  0.00    0
#> [2,]    0    0 33.33    0
#> [3,]    0    0  0.00    0
#> [4,]    0    0  0.00    0
#> [5,]    0    0  0.00    0
#> 
#> , , 2
#> 
#>       [,1] [,2]  [,3]  [,4]
#> [1,]  0.00    0 88.88  0.00
#> [2,]  0.00    0  0.00 44.44
#> [3,] 22.22    0  0.00 55.55
#> [4,]  0.00    0  0.00  0.00
#> [5,]  0.00    0  0.00  0.00
#> 
#> , , 3
#> 
#>       [,1]  [,2]  [,3] [,4]
#> [1,]  0.00  0.00  0.00    0
#> [2,]  0.00 66.66  0.00    0
#> [3,]  0.00  0.00  0.00    0
#> [4,]  0.00  0.00  0.00    0
#> [5,] 11.11  0.00 77.77    0
#> 

#as.matrix(coo1)  # error!

## ---------------------------------------------------------------------
## EXAMPLE 2
## ---------------------------------------------------------------------
m2 <- matrix(c(5:-2, rep.int(c(0L, 99L), 11)), ncol=6)
coo2 <- as(m2, "COO_SparseArray")
class(coo2)
#> [1] "COO_SparseMatrix"
#> attr(,"package")
#> [1] "SparseArray"
dim(coo2)
#> [1] 5 6
length(coo2)
#> [1] 30
nzcoo(coo2)
#>       [,1] [,2]
#>  [1,]    1    1
#>  [2,]    2    1
#>  [3,]    3    1
#>  [4,]    4    1
#>  [5,]    5    1
#>  [6,]    2    2
#>  [7,]    3    2
#>  [8,]    5    2
#>  [9,]    2    3
#> [10,]    4    3
#> [11,]    1    4
#> [12,]    3    4
#> [13,]    5    4
#> [14,]    2    5
#> [15,]    4    5
#> [16,]    1    6
#> [17,]    3    6
#> [18,]    5    6
nzdata(coo2)
#>  [1]  5  4  3  2  1 -1 -2 99 99 99 99 99 99 99 99 99 99 99
type(coo2)
#> [1] "integer"
sparsity(coo2)
#> [1] 0.4

stopifnot(identical(as.matrix(coo2), m2))

t(coo2)
#> <6 x 5 SparseMatrix> of type "integer" [nzcount=18 (60%)]:
#>      [,1] [,2] [,3] [,4] [,5]
#> [1,]    5    4    3    2    1
#> [2,]    0   -1   -2    0   99
#> [3,]    0   99    0   99    0
#> [4,]   99    0   99    0   99
#> [5,]    0   99    0   99    0
#> [6,]   99    0   99    0   99
stopifnot(identical(as.matrix(t(coo2)), t(as.matrix(coo2))))

## ---------------------------------------------------------------------
## COERCION FROM/TO dg[C|R]Matrix OR lg[C|R]Matrix OBJECTS
## ---------------------------------------------------------------------
## dg[C|R]Matrix and lg[C|R]Matrix objects are defined in the Matrix
## package.

## dgCMatrix/dgRMatrix:

M2C <- as(coo2, "dgCMatrix")
stopifnot(identical(M2C, as(m2, "dgCMatrix")))

coo2C <- as(M2C, "COO_SparseArray")
## 'coo2C' is the same as 'coo2' except that 'nzdata(coo2C)' has
## type "double" instead of "integer":
stopifnot(all.equal(coo2, coo2C))
typeof(nzdata(coo2C))  # double
#> [1] "double"
typeof(nzdata(coo2))   # integer
#> [1] "integer"

M2R <- as(coo2, "dgRMatrix")
stopifnot(identical(M2R, as(m2, "dgRMatrix")))
coo2R <- as(M2R, "COO_SparseArray")
stopifnot(all.equal(as.matrix(coo2), as.matrix(coo2R)))

## lgCMatrix/lgRMatrix:

m3 <- m2 == 99  # logical matrix
coo3 <- as(m3, "COO_SparseArray")
class(coo3)
#> [1] "COO_SparseMatrix"
#> attr(,"package")
#> [1] "SparseArray"
type(coo3)
#> [1] "logical"

M3C <- as(coo3, "lgCMatrix")
stopifnot(identical(M3C, as(m3, "lgCMatrix")))
coo3C <- as(M3C, "COO_SparseArray")
identical(as.matrix(coo3), as.matrix(coo3C))
#> [1] TRUE

M3R <- as(coo3, "lgRMatrix")
#stopifnot(identical(M3R, as(m3, "lgRMatrix")))
coo3R <- as(M3R, "COO_SparseArray")
identical(as.matrix(coo3), as.matrix(coo3R))
#> [1] TRUE

## ---------------------------------------------------------------------
## A BIG COO_SparseArray OBJECT
## ---------------------------------------------------------------------
nzcoo4 <- cbind(sample(25000, 600000, replace=TRUE),
                sample(195000, 600000, replace=TRUE))
nzdata4 <- runif(600000)
coo4 <- COO_SparseArray(c(25000, 195000), nzcoo4, nzdata4)
coo4
#> <25000 x 195000 SparseMatrix> of type "double" [nzcount=599974 (0.012%)]:
#>               [,1]      [,2]      [,3] ... [,194999] [,195000]
#>     [1,]         0         0         0   .         0         0
#>     [2,]         0         0         0   .         0         0
#>     [3,]         0         0         0   .         0         0
#>     [4,]         0         0         0   .         0         0
#>     [5,]         0         0         0   .         0         0
#>      ...         .         .         .   .         .         .
#> [24996,]         0         0         0   .         0         0
#> [24997,]         0         0         0   .         0         0
#> [24998,]         0         0         0   .         0         0
#> [24999,]         0         0         0   .         0         0
#> [25000,]         0         0         0   .         0         0
sparsity(coo4)
#> [1] 0.9998769