sparse_arr-class: Sparse Vectors and Matrices

sparse_arr-classR Documentation

Sparse Vectors and Matrices

Description

The sparse_mat class implements sparse matrices, potentially stored out-of-memory. Both compressed-sparse-column (CSC) and compressed-sparse-row (CSR) formats are supported. Sparse vectors are also supported through the sparse_vec class.

Usage

## Instance creation
sparse_mat(data, index, type = "double",
    nrow = NA_integer_, ncol = NA_integer_, dimnames = NULL,
    pointers = NULL, domain = NULL, offset = 0L, rowMaj = FALSE,
    tolerance = c(abs=0), sampler = "none", ...)

sparse_vec(data, index, type = "double",
    length = NA_integer_, names = NULL,
    domain = NULL, offset = 0L, rowMaj = FALSE,
    tolerance = c(abs=0), sampler = "none", ...)

# Check if an object is a sparse matrix
is.sparse(x)

# Coerce an object to a sparse matrix
as.sparse(x, ...)

## Additional methods documented below

Arguments

data

Either the non-zero values of the sparse array, or (if index is missing) a numeric vector or matrix from which to create the sparse array. For a sparse_vec, these should be a numeric vector. For a sparse_mat these can be a numeric vector if pointers is supplied, or a list of numeric vectors if pointers is NULL.

index

For sparse_vec, the indices of the non-zero items. For sparse_mat, either the row-indices or column-indices of the non-zero items, depending on the value of rowMaj.

type

A 'character' vector giving the storage mode of the data in virtual memory such. See ?"matter-types" for possible values.

nrow, ncol, length

The number of rows and columns, or the length of the array.

dimnames

The names of the sparse matrix dimensions.

names

The names of the sparse vector elements.

pointers

The (zero-indexed) pointers to the start of either the rows or columns (depending on the value of rowMaj) in data and index when they are numeric vectors rather than lists.

domain

Either NULL or a vector with length equal to the number of rows (for CSC matrices) or the number of columns (for CSR matrices). If NULL, then index is assumed to be row or column indices. If a vector, then they define the how the non-zero elements are matched to rows or columns. The index value of each non-zero element is matched against this domain using binary search. Must be numeric.

offset

If domain is NULL (i.e., index represents the actual row/column indices), then this is the index of the first row/column. The default of 0 means that index is indexed from 0.

rowMaj

Whether the data should be stored using compressed-sparse-row (CSR) representation (as opposed to compressed-sparse-column (CSC) representation). Defaults to 'FALSE', for efficient access to columns. Set to 'TRUE' for more efficient access to rows instead.

tolerance

For non-NULL domain, the tolerance used for floating-point equality when matching index to the domain. The vector should be named. Use 'absolute' to use absolute differences, and 'relative' to use relative differences.

sampler

For non-zero tolerances, how the data values should be combined when there are multiple index values within the tolerance. Must be of 'none', 'sum', 'mean', 'max', 'min', 'area', 'linear', 'cubic', 'gaussian', or 'lanczos'. Note that 'none' means nearest-neighbor interpolation.

x

An object to check if it is a sparse matrix or coerce to a sparse matrix.

...

Additional arguments to be passed to constructor.

Value

An object of class sparse_mat.

Slots

data:

The non-zero data values. Can be a numeric vector or a list of numeric vectors.

type:

The storage mode of the accessed data when read into R. This is a 'factor' with levels 'raw', 'logical', 'integer', 'numeric', or 'character'.

dim:

Either NULL for vectors, or an integer vector of length one of more giving the maximal indices in each dimension for matrices and arrays.

names:

The names of the data elements for vectors.

dimnames:

Either NULL or the names for the dimensions. If not NULL, then this should be a list of character vectors of the length given by 'dim' for each dimension. This is always NULL for vectors.

index:

The indices of the non-zero items. Can be a numeric vector or a list of numeric vectors.

pointers:

The pointers to the beginning of the rows or columns if index and data use vector storage rather than list storage.

domain:

Either NULL or a vector with length equal to the number of rows (for CSC matrices) or the number of columns (for CSR matrices). If NULL, then index is assumed to be row or column indices. If a vector, then they define the how the non-zero elements are matched to rows or columns. The index value of each non-zero element is matched against this domain using binary search. Must be numeric.

offset:

If domain is NULL (i.e., index represents the actual row/column indices), then this is the index of the first row/column. The default of 0 means that index is indexed from 0.

tolerance:

For non-NULL domain, the tolerance used for floating-point equality when matching index to the domain. The vector should be named. Use 'absolute' to use absolute differences, and 'relative' to use relative differences.

sampler:

The type of summarization or interpolation performed when there are multiple index values within the tolerance of the requested domain value(s).

ops:

Deferred arithmetic operations.

transpose

Indicates whether the data is stored in row-major order (TRUE) or column-major order (FALSE). For a matrix, switching the order that the data is read is equivalent to transposing the matrix (without changing any data).

Extends

matter

Creating Objects

sparse_mat and sparse_vec instances can be created through sparse_mat() and sparse_vec(), respectively.

Methods

Class-specific methods:

atomdata(x):

Access the 'data' slot.

adata(x):

An alias for atomdata(x).

atomindex(x):

Access the 'index' slot.

aindex(x):

An alias for atomindex(x).

pointers(x):

Access the 'pointers' slot.

domain(x):

Access the 'domain' slot.

tolerance(x), tolerance(x) <- value:

Get or set resampling 'tolerance'.

sampler(x), sampler(x) <- value:

Get or set the 'sampler' method.

fetch(x, ...):

Pull data into shared memory.

flash(x, ...):

Push data to a temporary file.

Standard generic methods:

dim(x):

Get 'dim'.

dimnames(x), dimnames(x) <- value:

Get or set 'dimnames'.

x[i, j, ..., drop], x[i, j] <- value:

Get or set the elements of the sparse matrix. Use drop = NULL to return a subset of the same class as the object.

cbind(x, ...), rbind(x, ...):

Combine sparse matrices by row or column.

t(x):

Transpose a matrix. This is a quick operation which only changes metadata and does not touch the data representation.

rowMaj(x):

Check the data orientation.

Author(s)

Kylie A. Bemis

See Also

matter

Examples

x <- matrix(rbinom(100, 1, 0.2), nrow=10, ncol=10)

y <- sparse_mat(x)
y[]

kuwisdelu/matter documentation built on Dec. 8, 2024, 8:09 p.m.