7
$\begingroup$

I'm an art hobbyist working on a painting that has a grid of $7$ rows and columns with a painted rectangle (rounded corners) at each grid position. I wanted to use $7$ colours and not have a colour repeat in any row or column. Googling I found this is called a Latin Square. Then to my surprise I learnt that there are $16$ million odd order $7$ Latin Squares. Rather than just pick one of those $16$ million I thought it would be interesting to use one of the more unique versions that has some additional constraint imposed on it. Then I wondered whether there is such a thing as a maximally dispersed Latin Square? By this I mean that the average physical distance between each element and the other $6$ elements of the same symbol is greatest when averaged over all $49$ elements.

So my question is:

Does a method exist for construction an order 7 Latin Square that is maximally dispersed in the sense I've tried to describe above?

  • 0
    suggest you decide on the best 3 by 3 Latin square, see what happens. It is legitimate to take the top row 1,2,3 May be necessary to go to a 4 by 4 square to see much choice (once top row is fixed).2017-02-09
  • 0
    Thanks Will. I've already completed the sketch and am at the stage of allocating colours to the 49 rectangles so am locked into order 7 for this painting at any rate!2017-02-09
  • 0
    Right. I meant to experiment a little so as to get a more precise description of the property you might want, just scribbling numbers (not colors) onto graph paper https://www.printablepaper.net/category/graph2017-02-09
  • 0
    Here's my attempt at pseudo code for calculating the dispersion factor: 1. For each of the 49 grid positions 1.1. Draw a line from the current grid position to each of the other 6 grid positions having the same symbol (colour in my case) 1.2. Measure the length of the line 1.3. Add the length to a running total 2. Divide the total by 49 x 62017-02-09
  • 1
    This is an interesting question that might be of interest to professional research mathematicians. If you don't get a complete answer here, let me know.2017-02-09

1 Answers 1

5

This is not a method of construction, but a brute force exhaustive search. Not mathematically elegant, but may eventually give you a maximally dispersed Latin square.

Wikipedia says there are $16942080$ reduced Latin squares of order $7$. I suspect you want the first column to be permuted, which multiplies that by $6! = 720$, giving around 12 billion squares.

I wrote a computer program to exhaustively search the space of Latin squares with first row fixed. (There could possibly be more if rounding errors distorted the calculations, I only output when $\ge$ current best) the best $4$ each with a final score of $1199.824938649409432$ corresponding to average distance $4.081$ are:

A B C D E F G
D E F G A B C
F G A B C D E
B C D E F G A
E F G A B C D
G A B C D E F
C D E F G A B

A B C D E F G
D E F G C A B
G C A B F D E
B F D E A G C
E A G C D B F
C D B F G E A
F G E A B C D

A B C D E F G
E F G A B C D
C D E F G A B
G A B C D E F
D E F G A B C
B C D E F G A
F G A B C D E

A B C D E F G
F G E A B C D
C D B F G E A
E A G C D B F
B F D E A G C
G C A B F D E
D E F G C A B

The next nearest runner-up had score $1199.476574957478533$.

The Latin square with the least dispersion (score $1151.661909164740564$, corresponding to average distance $3.917$) was (two isomorphic variants of):

A B C D E F G
B C A E F G D
C A E B G D F
F E B G D A C
E D G F B C A
D G F A C B E
G F D C A E B

The nearest runner up had score $1152.143695276356539$.

Interestingly, using a slightly different measurement (summing squared distances), every single one of the 1 billion or so calculated so far has score $5488$.

Here is the source code for the search program:

// gcc -std=c99 -Wall -Wextra -pedantic -O3 -march=native -o latin7 latin7.c -lm

#include 
#include 

#define O 7

static char square[O][O];
static long long total = 0;
static double best = 0;

static inline double evaluate()
{
  double metric = 0;
  for (int i = 0; i < O; ++i)
    for (int j = 0; j < O; ++j)
    {
      char symbol = square[i][j];
      for (int p = 0; p < O; ++p)
        for (int q = 0; q < O; ++q)
          if (square[p][q] == symbol)
          {
            int x = i - p;
            int y = j - q;
            metric += sqrt(x * x + y * y);
          }
    }
  return metric;
}

static inline int prune(int i, int j)
{
  char symbol = square[i][j];
  for (int q = 0; q < j; ++q)
    if (symbol == square[i][q])
      return 1;
  for (int p = 0; p < i; ++p)
    if (symbol == square[p][j])
      return 1;
  return 0;
}

static inline void output(void)
{
  total++;
  if ((total & 0xFFFF) == 0)
    fprintf(stderr, "\r        %lld      ", total);
  double metric = evaluate();
  if (metric < best) return;
  best = metric;
  printf("%.18e", metric);
  for (int p = 0; p < O; ++p)
  {
    printf("\t");
    for (int q = 0; q < O; ++q)
      printf("%c", square[p][q]);
  }
  printf("\n");
}

static void generate(int i, int j)
{
  if (j == O)
  {
    i += 1;
    j = 0;
  }
  if (i == O)
  {
    output();
    return;
  }
  if (i == 0)
  {
    square[i][j] = 'A' + j;
    generate(i, j + 1);
  } 
  else
  {
    for (int k = 0; k < O; ++k)
    {
      square[i][j] = 'A' + k;
      if (prune(i, j))
        continue;
      generate(i, j + 1);
    }
  }
}

int main()
{
  generate(0, 0);
  return 0;
}
  • 0
    Many thanks Claude! My purposes are entirely practical? So brute force is fine for me. Will your program identify whether there is a single unique answer or if there are multiple, how many? If you have the spare processing capacity it would be interesting to see those 7 order Latin square(s) with the least dispersion.2017-02-09
  • 0
    "Interestingly, […] summing squared distances, *every single one* […]" - That is not surprising. It is very easy to show that this number is always $(n^5-n^3)/6$ where $n$ is the size of the square. This evaluates to $2744$ for $n=7$, so I think you counted each distance twice.2017-02-09
  • 0
    See also my follow-up: http://math.stackexchange.com/questions/2136287/sum-of-squares-of-distances-in-latin-square2017-02-09
  • 0
    @DavidKilpatrick updated with results - to be sure there are only 4 with the top score w.r.t. rounding errors the program could be modified to output all those above the runner up score; similarly for least dispersion the program could be modified with a > instead of a < and initialising best to a huge value like 1.0/0.0. It takes a long time to run, but uses only a single core.2017-02-10
  • 0
    @ThomasR nice! Thanks for the link.2017-02-10
  • 1
    Remarkably, in two of the four solutions, the rows are not shuffled, just shifted (cyclically). In the other two solutions, the same is true for the columns. Can this be used to construct solutions?2017-02-10
  • 1
    All the solutions are isomorphic in the sense that one can be obtained from the other by reflection and renaming of the entries. 1 mirrored by the main diagonal=2, 1 mirrored horizontally=3, 3 mirrored by the main diagonal=4. So there is really only one solution.2017-02-10
  • 0
    I really had no sense of what the answer would be. I'm finding it hard to get my head around the four forms versus one solution concept though. I take it @ThomasR is saying if I painted one of the four forms and then looked at the painting in a mirror placed correctly I could see each of the other 3 forms in turn, given I mentally remapped the assignment of colours to letters?2017-02-11
  • 0
    So if I was writing a blurb to accompany the painting I could say something like, "There are over 12 billion possibilities for a 7 by 7 Latin square with the order of the symbols fixed in the first row. This painting shows 1 of the 4 possibilities that maximises the average distance between like symbols." (Assuming I had first explained what a Latin square is)2017-02-11
  • 0
    My thinking now is to do a second painting giving an example of a least dispersion possibility and show them together. Would somebody consider running the code to find out how many solutions there are please?2017-02-11
  • 0
    Many thanks @Claude for writing and running the code. Despite an honours degree in Computing I couldn't quite work out the recursion aspect! I'd need to step through it in a debugger with a smaller order and print out statements I think, but don't have that setup on my PC. Painting much easier I feel :)2017-02-11
  • 0
    @DavidKilpatrick I'm running the code modified to calculate the least dispersion, will take a while. The recursion is a depth-first search with pruning, if that helps your comprehension. Your blurb could say "unique" instead of "1 of 4" because as ThomasR says the 4 are isomorphic. However the 12 billion figure doesn't take isomorphisms into account, I think you could better use the 7th element of https://oeis.org/A264603 which unfortunately doesn't seem to have been calculated yet.2017-02-11
  • 0
    @DavidKilpatrick results for least dispersion now added.2017-02-13
  • 0
    @Claude - Thanks again for all your help. It'll be a while but I will post images of the two completed paintings. I wonder whether there is always a unique maximally and minimally dispersed Latin square (excluding isomorphism) for any order. I'm surprised and glad it is so for order 7 since it makes a good back story for the paintings!2017-02-14
  • 0
    @DavidKilpatrick I calculated the missing 7th element of the sequence, see https://math.stackexchange.com/a/2143780/2092862017-02-14