# tensorly.tenalg.khatri_rao

khatri_rao(matrices, weights=None, skip_matrix=None, reverse=False, mask=None)[source]

Khatri-Rao product of a list of matrices

This can be seen as a column-wise kronecker product. (see [R31] for more details).

If one matrix only is given, that matrix is directly returned.

Parameters

matrices : 2D-array list

list of matrices with the same number of columns, i.e.:

for i in len(matrices):
matrices[i].shape = (n_i, m)


weights : 1D-array

array of weights for each rank, of length m, the number of column of the factors (i.e. m == factor[i].shape for any factor)

skip_matrix : None or int, optional, default is None

if not None, index of a matrix to skip

reverse : bool, optional

if True, the order of the matrices is reversed

Returns

khatri_rao_product: matrix of shape (prod(n_i), m)

where prod(n_i) = prod([m.shape for m in matrices]) i.e. the product of the number of rows of all the matrices in the product.

Notes

Mathematically: A more intuitive but slower implementation is:

kr_product = np.zeros((n_rows, n_columns))
for i in range(n_columns):
cum_prod = matrices[:, i]  # Accumulates the khatri-rao product of the i-th columns
for matrix in matrices[1:]:
cum_prod = np.einsum('i,j->ij', cum_prod, matrix[:, i]).ravel()
# the i-th column corresponds to the kronecker product of all the i-th columns of all matrices:
kr_product[:, i] = cum_prod

return kr_product


References

R31(1,2)

T.G.Kolda and B.W.Bader, “Tensor Decompositions and Applications”, SIAM REVIEW, vol. 51, n. 3, pp. 455-500, 2009.