# Generating unimodular matrices (determinant = 1 or -1) browsing

• Maple has a deprecated function linalg[randmatrix] which has an option to make the matrix unimodular. The function generates an upper triangular matrix with all the diagonal elements equal to 1 (so the determinant is always equal to 1), and other non-zero elements randomized.

To create an equivalent matrix without using the deprecated function, you can generate a random upper triangular matrix using LinearAlgebra[RandomMatrix] and then manually set all diagonal elements to 1. Below is an example of a function that produces a 3x3 unimodular matrix.

\$RandomUnimod3=maple("
proc()
U:=LinearAlgebra[RandomMatrix](3,3,generator=rand(-4..4),shape=triangular[upper]):
for i from 1 to 3 do
U[i,i]:=1:
end do:
U
end proc:
");


• This can be generalized so the procedure takes the size, random generator and whether to produce an upper or lower triangular matrix as arguments.

genUniModular := proc(size, {gen := rand(10), type := upper})
local i, U:=LinearAlgebra[RandomMatrix](size,size,generator=gen,shape=triangular[type]):
for i from 1 to size do U[i,i]:=1:end do:
return U:
end proc:


The only required argument is the size of the matrix, the others are optional.

Usage:

genUniModular(4, gen = rand(-10..10), type = lower)


to generate a 4x4 lower triangular unimodular matrix with non-diagonal elements between -10 and 10