Kronecker product {tensorEVD} | R Documentation |
Kronecker product
Description
Computes the direct Kronecker product between two matrices
Usage
Kronecker(A, B, rows = NULL, cols = NULL,
make.dimnames = FALSE, drop = TRUE,
inplace = FALSE)
Arguments
A |
(numeric) Left numeric matrix |
B |
(numeric) Right numeric matrix |
rows |
(integer) Index which rows of the Kronecker are to be returned. They must range from 1 to |
cols |
(integer) Index which columns of the Kronecker are to be returned. They must range from 1 to |
drop |
Either |
make.dimnames |
|
inplace |
|
Details
For any two matrices A={aij} of dimensions m × n and B={bij} of dimensions p × q, the direct Kronecker product between them is a matrix defined as the block matrix
A⊗B = {aijB}
which is of dimensions mp × nq.
A sub-matrix formed by selecting specific rows and columns from the Kronecker can be obtained by pre- and post- multiplication with incidence matrices
R (A⊗B) C'
where R is an incidence matrix mapping from rows of the resulting sub-matrix to rows of the Kronecker product, and C is an incidence matrix mapping from columns of the resulting sub-matrix to columns of the Kronecker product. This sub-matrix of the Kronecker can be obtained by matrix indexing as
Kronecker(A,B)[rows,cols]
where rows
and cols
are integer vectors whose entries are, respectively, the row and column number of the Kronecker that are mapped at each row of R and C.
The function computes this sub-matrix of the Kronecker product directly from A and B without forming the whole Kronecker product. This is very useful if a relatively small number of row/columns are to be selected.
Value
Returns the Kronecker product matrix. It can be a sub-matrix of it as per the rows
and cols
arguments.
Examples
require(tensorEVD)
# (a) Kronecker product of 2 vectors
A = rnorm(3)
B = rnorm(2)
(K1 = Kronecker(A, B))
# it must equal when using from the R-base package:
(K2 = kronecker(A, B))
# (b) Kronecker product of 2 matrices
A = matrix(rnorm(12), ncol=3)
B = matrix(rnorm(4), ncol=2)
K1 = Kronecker(A, B)
# (it must equal (but faster) to:)
K2 = kronecker(A, B)
all.equal(K1,K2)
# (c) Subsetting rows/columns from the Kronecker
A = matrix(rnorm(100*150), ncol=150)
B = matrix(rnorm(100*120), ncol=120)
rows = c(1,3,5,7)
cols = c(10,20,30,50)
K1 = Kronecker(A, B, rows=rows, cols=cols)
# (it must equal (but faster) to:)
K2 = Kronecker(A, B)[rows,cols]
all.equal(K1,K2)
# (d) Inplace calculation
# overwrite the output at the same address as the input:
K1 = A[] # copy of A to be used as input
add = pryr::address(K1) # address of K on entry
K1 = Kronecker(K1, B=0.5)
pryr::address(K1) == add # on exit, K was moved to a different address
K2 = A[]
add = pryr::address(K2)
K2 = Kronecker(K2, B=0.5, inplace=TRUE)
pryr::address(K2) == add # on exit, K remains at the same address
all.equal(K1,K2)