Wolfram Research

Function Repository Resource:

LatinSquare

Source Notebook

Generate a matrix from a list such that no row or column contains the same element twice

Contributed by: Jon McLoone

ResourceFunction["LatinSquare"][n]

generates an n x n matrix containing the numbers 1 to n such that no row or column contains the same element twice.

ResourceFunction["LatinSquare"][list]

generates a Latin square using the elements of list.

Details and Options

A provided list must not contain duplicate elements.
The option “ReducedForm” can be used to generated reduced-form Latin squares.
Typically, there are multiple ways to arrange items into a valid Latin square. ResourceFunction["LatinSquare"] randomly chooses one of these to return.

Examples

Basic Examples

Generate Latin squares containing the numerals 1 through 4:

In[1]:=
ResourceFunction["LatinSquare"][4] // Grid
Out[1]=

Scope

Generate Latin squares containing strings:

In[2]:=
ResourceFunction["LatinSquare"][{"a", "b", "c", "d"}] // Grid
Out[2]=

Options

Reduced-form Latin squares are Latin squares where the first row and first column are sorted in order:

In[3]:=
ResourceFunction["LatinSquare"][4, "ReducedForm" -> True] // Grid
Out[3]=

Possible Issues

The basis set for a Latin square cannot contain duplicate elements:

In[4]:=
ResourceFunction["LatinSquare"][{1, 2, 3, 3}]
Out[4]=

Requirements

Wolfram Language 11.3 (March 2018) or above

Resource History

License Information