Function Repository Resource:

KhatriRaoProduct

Source Notebook

Evaluate the Khatri-Rao product of matrices

Contributed by: Jan Mangaldan

ResourceFunction["KhatriRaoProduct"][m1,m2,]

constructs the Khatri-Rao product of the matrices mi.

Details

The Khatri-Rao product is a columnwise Kronecker product of matrices; that is, each column of the Khatri-Rao product is the Kronecker product of the corresponding columns of the mi.
The matrices mi must all have the same number of columns.

Examples

Basic Examples (1) 

Khatri-Rao product of two matrices:

In[1]:=
am = Array[Subscript[a, ##] &, {2, 2}];
bm = Array[Subscript[b, ##] &, {2, 2}];
In[2]:=
ResourceFunction["KhatriRaoProduct"][am, bm] // MatrixForm
Out[2]=

Scope (2) 

a and b are matrices with exact entries:

In[3]:=
a = {{0, 1}, {-1, 0}};
b = {{1, 2}, {3, 4}};

Use exact arithmetic to compute the Khatri-Rao product:

In[4]:=
ResourceFunction["KhatriRaoProduct"][a, b] // MatrixForm
Out[4]=

Use machine arithmetic:

In[5]:=
ResourceFunction["KhatriRaoProduct"][N[a], N[b]] // MatrixForm
Out[5]=

Use 20-digit precision arithmetic:

In[6]:=
ResourceFunction["KhatriRaoProduct"][N[a, 20], N[b, 20]] // MatrixForm
Out[6]=

Evaluate the Khatri-Rao product of three matrices:

In[7]:=
ResourceFunction["KhatriRaoProduct"][RandomReal[1, {2, 2}], RandomReal[1, {3, 2}], RandomReal[1, {2, 2}]]
Out[7]=

Properties and Relations (3) 

The Khatri-Rao product is multi-linear (linear in each argument):

In[8]:=
m1 = Array[\[FormalX], {4, 3}];
m2 = Array[\[FormalY], {4, 3}];
m3 = Array[\[FormalZ], {4, 3}];
In[9]:=
ResourceFunction["KhatriRaoProduct"][C[1] m1 + C[2] m2, m3] ==
  C[1]  ResourceFunction["KhatriRaoProduct"][m1, m3] + C[2]  ResourceFunction["KhatriRaoProduct"][m2, m3] // Simplify
Out[9]=
In[10]:=
ResourceFunction["KhatriRaoProduct"][m1, C[1] m2 + C[2] m3] ==
  C[1]  ResourceFunction["KhatriRaoProduct"][m1, m2] + C[2]  ResourceFunction["KhatriRaoProduct"][m1, m3] // Simplify
Out[10]=

The Khatri-Rao product is associative:

In[11]:=
ResourceFunction["KhatriRaoProduct"][m1, ResourceFunction["KhatriRaoProduct"][m2, m3]] === ResourceFunction["KhatriRaoProduct"][
  ResourceFunction["KhatriRaoProduct"][m1, m2], m3] === ResourceFunction["KhatriRaoProduct"][m1, m2, m3]
Out[11]=

The Khatri-Rao product is not commutative:

In[12]:=
ResourceFunction["KhatriRaoProduct"][m1, m2] === ResourceFunction["KhatriRaoProduct"][m2, m1]
Out[12]=

Version History

  • 1.0.0 – 11 April 2022

Source Metadata

Related Resources

Author Notes

The implementation here makes no attempt to exploit sparsity, structure or symmetry of the input matrices.

License Information