Learn R Programming

Cardinal (version 1.4.0)

Hashmat-class: Sparse Matrix Class Using Lists as Hash Tables

Description

The Hashmat class implements compressed sparse column (CSC) style matrices using R list objects as the columns. The implementation is unique in that it allows re-assignment of the keys describing the rows, allowing for arbitrary re-ordering of rows and row-wise elements. This is useful for storing sparse signals, such as processed spectra.

Usage

## Instance creation Hashmat(data = NA, nrow = 1, ncol = 1, byrow=FALSE, dimnames = NULL, ...)
## Additional methods documented below

Arguments

data
A matrix or a vector. If data is a matrix, then a sparse matrix is construced from matrix directly and other arguments (except for dimnames) are ignored. If data is a vector, then the behavior is the same as for ordinary matrix construction.
nrow
The number of rows in the sparse matrix.
ncol
The number of columns in the sparse matrix.
byrow
If 'FALSE', the matrix is filled by columns. If 'TRUE', it is filled by rows.
dimnames
The 'dimnames' giving the dimension names for the matrix, analogous to the 'dimnames' attribute of an ordinary R matrix. This must be a list of length 2 or NULL.
...
Additional arguments passed to the constructor.

Slots

data:
A list with vectors corresponding columns of the sparse matrix, whose elements are its non-zero elements.
keys:
A character vector providing the keys that determine the rows of the non-zero elements of the matrix.
dim:
A length 2 integer vector analogous to the 'dim' attribute of an ordinary R matrix.
dimnames:
A length 2 list analogous to the 'dimnames' attribute of an ordinary R matrix.
.__classVersion__:
A Versions object describing the version of the class used to created the instance. Intended for developer use.

Extends

Versioned

Creating Objects

Hashmat instances are usually created through Hashmat().

Methods

Class-specific methods:
pData(object), pData(object)<-:
Access or set the list of numeric vectors storing the column-vectors of the sparse matrix directly.
keys(object), keys(object)<-:
Access of set the keys for the row elements. If this is a character, it sets the keys slot directly, and hence the 'dim' is also changed. If this is a list, then the list should have length equal to the number of rows, and each element should be an integer vector of length equal to the number of non-zero row elements for the respective column. The vectors are used to index the keys slot and set the key names of the vectors, and hence change or reorder the row elements.
Standard generic methods:
combine(x, y, ...):
Combines two Hashmat objects. See the combine method for matrices for details of how the Hashmat sparse matrices are combined. The behavior is identical, except when filling in missing elements in non-shared rows and columns, the resulting Hashmat object will have zeroes instead of NAs.
dim(x), dim(x) <- value:
Return or set the dimensions of the sparse matrix.
dimnames(x), dimnames(x) <- value:
Return or set the 'dimnames' of the sparse matrix.
colnames(x), colnames(x) <- value:
Return or set the column names of the sparse matrix.
rownames(x), rownames(x) <- value:
Return or set the row names of the sparse matrix.
ncol:
Return the number of columns in the sparse matrix.
nrow:
Return the number of columns in the sparse matrix.
cbind:
Combine sparse matrices by columns. The keys used to resolve the rows must match between matrices.
rbind:
Not allowed for sparse matrices. (Always returns an error.)
Hashmat[i, j, ..., drop], Hashmat[i, j, ...] <- value:
Access and assign elements in the sparse matrix. A Hashmat sparse matrix can be indexed like an ordinary R matrix. Note however that linear indexing is not supported. Use drop = NA to return a subset of the same class as the object.

See Also

matrix, Binmat, SImageSet

Examples

Run this code
## Create an Hashmat object
Hashmat()

## Using a list of elements and keys
dmat1 <- diag(3)
smat1 <- Hashmat(dmat1)
all.equal(smat1[], dmat1, check.attr=FALSE)

## Filling an empty sparse matrix
smat2 <- Hashmat(nrow=1000, ncol=1000)
smat2[500,] <- rep(1, 1000)

dmat2 <- matrix(nrow=1000, ncol=1000)
dmat2[500,] <- rep(1, 1000)

print(object.size(dmat2), units="Mb")
print(object.size(smat2), units="Mb") # Much smaller

all.equal(dmat2[500,], smat2[500,], , check.attr=FALSE)

Run the code above in your browser using DataLab