.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/combinatorics/designs/plot_01_latin_squares.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_api_gallery_combinatorics_designs_plot_01_latin_squares.py: Euler's officers: orthogonal Latin squares ================================================ Builds a pair of orthogonal Latin squares of order 5 and superimposes them so that every (rank, regiment) pair appears exactly once, the arrangement Euler's 36-officers problem asks for. It then checks by brute force that no such pair exists for order 2. .. GENERATED FROM PYTHON SOURCE LINES 12-19 .. code-block:: Python from itertools import permutations import matplotlib.pyplot as plt import numpy as np from mathematicskit.combinatorics import are_orthogonal, is_latin_square, orthogonal_latin_square_pair .. GENERATED FROM PYTHON SOURCE LINES 20-22 An orthogonal pair of order 5 ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 22-37 .. code-block:: Python ranks, regiments = orthogonal_latin_square_pair(5) print("ranks:\n", ranks) print("regiments:\n", regiments) print(f"orthogonal: {are_orthogonal(ranks, regiments)}") fig, axes = plt.subplots(1, 2, figsize=(8, 4)) for ax, square, title in zip(axes, (ranks, regiments), ("rank", "regiment")): ax.imshow(square, cmap="tab10") for (i, j), v in np.ndenumerate(square): ax.text(j, i, str(v), ha="center", va="center", color="w") ax.set_title(title) ax.axis("off") fig.suptitle("Every (rank, regiment) pair appears exactly once") .. image-sg:: /api/gallery/combinatorics/designs/images/sphx_glr_plot_01_latin_squares_001.png :alt: Every (rank, regiment) pair appears exactly once, rank, regiment :srcset: /api/gallery/combinatorics/designs/images/sphx_glr_plot_01_latin_squares_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none ranks: [[0 1 2 3 4] [1 2 3 4 0] [2 3 4 0 1] [3 4 0 1 2] [4 0 1 2 3]] regiments: [[0 1 2 3 4] [2 3 4 0 1] [4 0 1 2 3] [1 2 3 4 0] [3 4 0 1 2]] orthogonal: True Text(0.5, 0.98, 'Every (rank, regiment) pair appears exactly once') .. GENERATED FROM PYTHON SOURCE LINES 38-40 No orthogonal pair of order 2 ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 40-45 .. code-block:: Python squares = [np.array(rows) for rows in permutations(permutations(range(2)), 2)] latin = [s for s in squares if is_latin_square(s)] found = any(are_orthogonal(a, b) for a in latin for b in latin) print(f"order 2: {len(latin)} Latin squares, orthogonal pair exists: {found}") .. rst-class:: sphx-glr-script-out .. code-block:: none order 2: 2 Latin squares, orthogonal pair exists: False .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.028 seconds) .. _sphx_glr_download_api_gallery_combinatorics_designs_plot_01_latin_squares.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_latin_squares.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_latin_squares.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_latin_squares.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_