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×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 (1) 

Generate Latin squares containing the numerals 1 through 4:

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

Scope (1) 

Generate Latin squares containing strings:

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

Options (1) 

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 (1) 

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

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

Publisher

Jon McLoone

Requirements

Wolfram Language 11.3 (March 2018) or above

Version History

  • 1.0.0 – 07 February 2019

License Information