Function Repository Resource:

 RandomUnimodularMatrix

Source Notebook

Return a pseudorandom unimodular matrix

Contributed by: Jan Mangaldan

ResourceFunction["RandomUnimodularMatrix"][n]

gives a pseudorandom n×n unimodular matrix.

Details and Options

A unimodular matrix is a square integer matrix whose inverse also has integer entries.
Such a matrix has a determinant that is a unit (that is, 1 or -1 if not allowing Gaussian integers).
ResourceFunction["RandomUnimodularMatrix"] accepts the following options:
GaussianIntegersFalsewhether the matrix returned should have Gaussian integer entries
"MaxEntry"Automaticbound on the matrix elements
MaxIterationsAutomaticmaximum number of iterations to use
With "MaxEntry"→m, the absolute values of the entries of the matrix are guaranteed to be less than or equal to m.

Examples

Basic Examples (2) 

Generate a random 5×5 unimodular matrix:

In[1]:=
MatrixForm[mat = ResourceFunction["RandomUnimodularMatrix"][5]]
Out[1]=

Its inverse has integer entries:

In[2]:=
Inverse[mat] // MatrixForm
Out[2]=

Scope (1) 

A medium-sized unimodular matrix:

In[3]:=
ResourceFunction["RandomUnimodularMatrix"][20] // MatrixForm
Out[3]=

Options (4) 

GaussianIntegers (2) 

Generate a unimodular matrix with Gaussian integer entries:

In[4]:=
MatrixForm[
 mat = ResourceFunction["RandomUnimodularMatrix"][5, GaussianIntegers -> True]]
Out[4]=

Its inverse has Gaussian integer entries:

In[5]:=
Inverse[mat] // MatrixForm
Out[5]=

MaxEntry (1) 

Generate a unimodular matrix with elements between -2 and 2:

In[6]:=
ResourceFunction["RandomUnimodularMatrix"][5, "MaxEntry" -> 2] // MatrixForm
Out[6]=

MaxIterations (1) 

Change the number of iterations needed to generate the matrix:

In[7]:=
ResourceFunction["RandomUnimodularMatrix"][5, MaxIterations -> 10] // MatrixForm
Out[7]=

Applications (2) 

Generate an integer matrix with integer eigenvalues:

In[8]:=
n = 5;
u = ResourceFunction["RandomUnimodularMatrix"][n];
MatrixForm[
 mat = Inverse[u] . DiagonalMatrix[RandomInteger[{-9, 9}, n]] . u]
Out[8]=

Show its eigenvalues and eigenvectors:

In[9]:=
Eigensystem[%]
Out[9]=

Version History

  • 1.0.0 – 03 March 2021

Source Metadata

Related Resources

Author Notes

Hausen's original method only worked for integers. The method implemented here generalizes it to Gaussian integers.

License Information