Function Repository Resource:

CommutationMatrix

Source Notebook

Generate a commutation matrix

Contributed by: Jan Mangaldan

ResourceFunction["CommutationMatrix"][m,n]

returns the commutation matrix .

Details and Options

The commutation matrix is also known as the vec-permutation matrix or the swap matrix.
The commutation matrix is a special permutation matrix of size that relates the Kronecker products and . Specifically, if a is a matrix of size p×q and b is a matrix of size r×s, then the two Kronecker products satisfy the relationship .
By default, ResourceFunction["CommutationMatrix"] produces a PermutationMatrix and takes the same options as PermutationMatrix, including TargetStructure.

Examples

Basic Examples (2) 

A commutation matrix:

In[1]:=
cm = ResourceFunction["CommutationMatrix"][5, 3]
Out[1]=

Visualize the commutation matrix:

In[2]:=
ArrayPlot[cm]
Out[2]=

Options (5) 

TargetStructure (3) 

Return the commutation matrix as a dense matrix:

In[3]:=
ResourceFunction["CommutationMatrix"][2, 3, TargetStructure -> "Dense"]
Out[3]=

Return the commutation matrix as a structured array:

In[4]:=
ResourceFunction["CommutationMatrix"][2, 3, TargetStructure -> "Structured"]
Out[4]=

Return the commutation matrix as a sparse array:

In[5]:=
ResourceFunction["CommutationMatrix"][2, 3, TargetStructure -> "Sparse"]
Out[5]=

WorkingPrecision (2) 

Specify the working precision for the commutation matrix:

In[6]:=
ResourceFunction["CommutationMatrix"][2, 3, WorkingPrecision -> MachinePrecision]
Out[6]=

The normal representation returns a full matrix with finite precision elements:

In[7]:=
Normal[%]
Out[7]=

Properties and Relations (5) 

The commutation matrix is orthogonal, i.e. the inverse is equal to the transpose:

In[8]:=
p = 4; q = 3;
cm = ResourceFunction["CommutationMatrix"][p, q];
Inverse[cm] === Transpose[cm]
Out[10]=

The inverse of CommutationMatrix[p,q] is given by CommutationMatrix[q,p]:

In[11]:=
p = 4; q = 3;
ResourceFunction["CommutationMatrix"][p, q] . ResourceFunction["CommutationMatrix"][q, p] == IdentityMatrix[p q]
Out[12]=

Define the vec operator, which stacks the columns of a matrix into a single vector:

In[13]:=
vec[m_?MatrixQ] := Flatten[m, {{2, 1}}]

The commutation matrix relates the result of applying the vec operator to a matrix and its transpose:

In[14]:=
p = 4; q = 3;
m1 = Array[\[FormalX], {p, q}];
vec[Transpose[m1]] == ResourceFunction["CommutationMatrix"][p, q] . vec[m1]
Out[15]=

The commutation matrix can be used to express the relationship between the Kronecker product of two given matrices and the Kronecker product of the same matrices in reverse order:

In[16]:=
p = 4; q = 3;
m1 = Array[\[FormalX], {p, q}];
r = 4; s = 3;
m2 = Array[\[FormalY], {r, s}];
KroneckerProduct[m1, m2] . ResourceFunction["CommutationMatrix"][q, s] == ResourceFunction["CommutationMatrix"][p, r] . KroneckerProduct[m2, m1]
Out[17]=

The commutation matrix can be expressed as a sum of Kronecker products of an identity matrix with unit vectors:

In[18]:=
p = 4; q = 3;
ResourceFunction["CommutationMatrix"][p, q] == \!\(
\*UnderoverscriptBox[\(\[Sum]\), \(k = 1\), \(p\)]\(KroneckerProduct[
   UnitVector[p, k], IdentityMatrix[q], UnitVector[p, k]]\)\)
Out[19]=

Requirements

Wolfram Language 13.1 (June 2022) or above

Version History

  • 1.0.0 – 16 August 2023

Source Metadata

Related Resources

License Information