Function Repository Resource:

# SymmetricSort

Symmetrically reorder the rows and columns of a square matrix

Contributed by: Edmund B Robinson
 ResourceFunction["SymmetricSort"][matrix,from,to] symmetrically reorders the elements of matrix from ordering from to ordering to. ResourceFunction["SymmetricSort"][matrix,index] symmetrically reorders the elements of matrix by the ordering represented by index.

## Details

ResourceFunction["SymmetricSort"] maintains matrix symmetry when permuting the matrix elements.
ResourceFunction["SymmetricSort"] takes any expression that satisfies SquareMatrixQ for matrix.
In ResourceFunction["SymmetricSort"][matrix,from,to], from and to can be any expression that satisfies VectorQ and DuplicateFreeQ, and together satisfy ContainsOnly. matrix, from and to must all have the same length.
In ResourceFunction["SymmetricSort"][matrix,index], index can be any ordering represented as a permutation list or as a disjoint cycle.

## Examples

### Basic Examples (5)

Generate a covariance matrix from example data:

 In[1]:=
 Out[1]=

The covariance matrix is symmetric and positive semidefinite:

 In[2]:=
 Out[2]=

A SymmetricSort using the variable names to change the order of the variables:

 In[3]:=
 Out[3]=

Symmetric and positive semidefinite properties are preserved:

 In[4]:=
 Out[4]=

SymmetricSort can also take an ordering of position indices:

 In[5]:=
 Out[5]=

### Scope (2)

SymmetricSort preserves the diagonal and the relative positions of the upper and lower off-diagonals:

 In[6]:=
 Out[6]=

Diagonal elements in the original matrix remain on the diagonal in the reordered matrix, and off-diagonal relative positions are comparable. For example, m34 is still mirrored with m43:

 In[7]:=
 Out[7]=

SymmetricSort accepts permutations expressed in cycle format:

 In[8]:=
 Out[8]=

### Applications (2)

Properties of Graph, like WeightedAdjacencyMatrix, are given in the order of VertexList:

 In[9]:=
 Out[9]=

SymmetricSort property elements in canonical order:

 In[10]:=
 Out[10]=

 In[11]:=
 Out[11]=

The permuted matrix is involutory, just like the original Hadamard matrix:

 In[12]:=
 Out[12]=

### Properties and Relations (1)

The eigenvalues of a symmetric matrix are invariant under SymmetricSort:

 In[13]:=
 Out[13]=
 In[14]:=
 Out[14]=
 In[15]:=
 Out[15]=

### Neat Examples (5)

An upper bidiagonal matrix:

 In[16]:=
 Out[16]=

Generate a symmetric block matrix with bidiagonal blocks:

 In[17]:=
 Out[17]=

Use the resource function OutShuffle with SymmetricSort to transform the matrix into a tridiagonal matrix:

 In[18]:=
 Out[18]=

Compute the singular values of a numerical upper bidiagonal matrix:

 In[19]:=
 Out[19]=

These are equivalent to the positive eigenvalues of the permuted block matrix:

 In[20]:=
 Out[20]=

## Publisher

Edmund B Robinson

## Version History

• 1.0.0 – 01 September 2021