scipy.linalg.

khatri_rao#

scipy.linalg.khatri_rao(a, b)[source]#

Khatri-Rao product of two matrices.

A column-wise Kronecker product of two matrices

The documentation is written assuming array arguments are of specified “core” shapes. However, array argument(s) of this function may have additional “batch” dimensions prepended to the core shape. In this case, the array is treated as a batch of lower-dimensional slices; see Batched Linear Operations for details. The NEP 5 signature of this function is (i,k),(j,k)->(i*j,k).

Parameters:
a(n, k) array_like

Input array

b(m, k) array_like

Input array

Returns:
c: (n*m, k) ndarray

Khatri-rao product of a and b.

Notes

The mathematical definition of the Khatri-Rao product is:

\[(A_{ij} \bigotimes B_{ij})_{ij}\]

which is the Kronecker product of every column of A and B, e.g.:

c = np.vstack([np.kron(a[:, k], b[:, k]) for k in range(b.shape[1])]).T
Array API Standard Support

khatri_rao has support for Python Array API Standard compatible backends in addition to NumPy. The following combinations of backend and device (or other capability) are supported.

Library

CPU

GPU

NumPy

✅

n/a

CuPy

n/a

✅

PyTorch

✅

✅

JAX

✅

✅

Dask

✅

n/a

See Support for the array API standard for more information.

Examples

>>> import numpy as np
>>> from scipy import linalg
>>> a = np.array([[1, 2, 3], [4, 5, 6]])
>>> b = np.array([[3, 4, 5], [6, 7, 8], [2, 3, 9]])
>>> linalg.khatri_rao(a, b)
array([[ 3,  8, 15],
       [ 6, 14, 24],
       [ 2,  6, 27],
       [12, 20, 30],
       [24, 35, 48],
       [ 8, 15, 54]])