Learn R Programming

qlcMatrix (version 0.9.2)

rKhatriRao: `reduced' Khatri-Rao product (sparse matrices)

Description

This function performs a Khatri-Rao product (`column-wise Kronecker product', see KhatriRao for more info) on two sparse matrices. However, the result of such a product on sparse matrices normally results in very many empty rows. This function removes those empty rows, and, most importantly, it produces row names only for the remaining rows. For large sparse matrices this is much more efficient than first producing all rownames, and then removing the one with the empty rows.

Usage

rKhatriRao(X, Y, 
	rownamesX = rownames(X), rownamesY = rownames(Y), 
	simplify = FALSE, binder = ":", FUN = "*")

Arguments

Value

By default, the result is a list of two items:Mresulting sparse product matrix with empty rows removedrownamesa vector with the resulting row names for the non-empty rowsWhen simplify=T, then the matrix is return with the row names included.

Details

Up to 1e6 row names to be produced goes reasonably quick with the basic KhatriRao function. However, larger amounts of pasting of row names becomes very slow, and the row names take an enormous amount of RAM. This function solves that problem by only producing row names for the non-empty rows.

See Also

KhatriRao

Examples

Run this code
# two sparse matrices with row names

X <- rSparseMatrix(1e4, 1e3, 1e4)
Y <- rSparseMatrix(1e4, 1e3, 1e4)

rownames(X) <- 1:nrow(X)
rownames(Y) <- 1:nrow(Y)

# the basic KhatriRao product is very fast
# but almost all rows are empty

system.time(M <- KhatriRao(X, Y))
sum(rowSums(M)==0)/nrow(M) # 99.9\% empty rows

# To produce all row names takes a long time
# with 1e8 row names it took half an hour on my laptop
# so: don't try the following, except on a very large machine!
system.time(M <- KhatriRao(X, Y, make.dimnames = TRUE))

# Using the current special version works just fine and is very quick
system.time(M <- rKhatriRao(X, Y))

Run the code above in your browser using DataLab