tensorSparse: Tensor Product of Sparse Vectors, Matrices or Arrays

tensorSparseR Documentation

Tensor Product of Sparse Vectors, Matrices or Arrays


Compute the tensor product of two vectors, matrices or arrays which may be sparse or non-sparse.


tensorSparse(A, B, alongA = integer(0), alongB = integer(0))



Vectors, matrices, three-dimensional arrays, or objects of class sparseVector, sparseMatrix or sparse3Darray.


Integer vector specifying the dimensions of A to be collapsed.


Integer vector specifying the dimensions of B to be collapsed.


This function is a generalisation, to sparse arrays, of the function tensor in the tensor package.

tensorSparse has the same syntax and interpretation as tensor. For example, if A and B are matrices, then tensor(A,B,2,1) is the matrix product A %*% B while tensor(A,B,2,2) is A %*% t(B).

This function tensorSparse handles sparse vectors (class "sparseVector" in the Matrix package), sparse matrices (class "sparseMatrix" in the Matrix package) and sparse three-dimensional arrays (class "sparse3Darray" in the spatstat.sparse package) in addition to the usual vectors, matrices and arrays.

The result is a sparse object if at least one of A and B is sparse. Otherwise, if neither A nor B is sparse, then the result is computed using tensor.

The main limitation is that the result cannot have more than 3 dimensions (because sparse arrays with more than 3 dimensions are not yet supported).


Either a scalar, a vector, a matrix, an array, a sparse vector (class "sparseVector" in the Matrix package), a sparse matrix (class "sparseMatrix" in the Matrix package) or a sparse three-dimensional array (class "sparse3Darray" in the spatstat.sparse package).




See Also

sparse3Darray, aperm.sparse3Darray


  M <- sparse3Darray(i=1:4, j=sample(1:4, replace=TRUE),
                     k=c(1,2,1,2), x=1:4, dims=c(5,5,2))
  A <- tensorSparse(M, M, 1:2, 2:1)

spatstat.sparse documentation built on April 18, 2022, 9:05 a.m.