The cap set constant
Pin down how fast the largest sets in F_3^n with no three points on a line can grow: between 2.2203^n and 2.756^n.

| 1 | """A cap set of 20 points in F_3^4: the 81 points drawn as a 9 x 9 grid of 3 x 3 blocks, point (a, b, c, d) in block |
| 2 | row a, block column b, row c, column d. Found by depth-first search from 0 and the four unit vectors (any cap that spans |
| 3 | the space can be moved there by an affine map); checked here: no three distinct points x, y, z with x + y + z = 0.""" |
| 4 | import itertools |
| 5 | from pathlib import Path |
| 6 | |
| 7 | import kit |
| 8 | |
| 9 | P = list(itertools.product(range(3), repeat=4)) |
| 10 | idx = {p: i for i, p in enumerate(P)} |
| 11 | third = [[idx[tuple(-(a + b) % 3 for a, b in zip(x, y))] for y in P] for x in P] |
| 12 | |
| 13 | |
| 14 | def search(cap, blocked, start, want): |
| 15 | if len(cap) == want: |
| 16 | return cap |
| 17 | free = [i for i in range(start, 81) if not blocked >> i & 1] |
| 18 | if len(cap) + len(free) < want: |
| 19 | return None |
| 20 | for k, i in enumerate(free): |
| 21 | if len(cap) + len(free) - k < want: |
| 22 | return None |
| 23 | b = blocked | 1 << i |
| 24 | for j in cap: |
| 25 | b |= 1 << third[i][j] |
| 26 | found = search(cap + [i], b, i + 1, want) |
| 27 | if found: |
| 28 | return found |
| 29 | return None |
| 30 | |
| 31 | |
| 32 | seed = [idx[p] for p in ((0, 0, 0, 0), (1, 0, 0, 0), (0, 1, 0, 0), (0, 0, 1, 0), (0, 0, 0, 1))] |
| 33 | blocked = 0 |
| 34 | for k, i in enumerate(seed): |
| 35 | blocked |= 1 << i |
| 36 | for j in seed[:k]: |
| 37 | blocked |= 1 << third[i][j] |
| 38 | cap = search(seed, blocked, 0, 20) |
| 39 | assert cap and len(set(cap)) == 20 |
| 40 | |
| 41 | |
| 42 | def spread(pts): |
| 43 | """How unevenly the points fall over the 9 blocks, the 9 rows and the 9 columns of the drawing (0 is even).""" |
| 44 | def var(keys): |
| 45 | n = [0] * 9 |
| 46 | for k in keys: |
| 47 | n[k] += 1 |
| 48 | return sum((c - len(pts) / 9) ** 2 for c in n) |
| 49 | return var(3 * a + b for a, b, c, d in pts) + var(3 * a + c for a, b, c, d in pts) + var(3 * b + d for a, b, c, d in pts) |
| 50 | |
| 51 | |
| 52 | # the same cap moved by a coordinate permutation, sign changes and a translation (affine maps keep lines), so that it |
| 53 | # spreads evenly over the drawing; ties go to the first image found, so the result is deterministic |
| 54 | best = None |
| 55 | for perm in itertools.permutations(range(4)): |
| 56 | for sign in itertools.product((1, 2), repeat=4): |
| 57 | for t in P: |
| 58 | img = [tuple((sign[k] * P[i][perm[k]] + t[k]) % 3 for k in range(4)) for i in cap] |
| 59 | key = spread(img) |
| 60 | if best is None or key < best[0] - 1e-9: |
| 61 | best = (key, img) |
| 62 | cap = [idx[p] for p in best[1]] |
| 63 | assert len(set(cap)) == 20 |
| 64 | assert all(third[x][y] not in cap for x, y in itertools.combinations(cap, 2)) |
| 65 | |
| 66 | s, g = 25, 11 # cell and the gap between blocks |
| 67 | side = 9 * s + 2 * g |
| 68 | x0, y0 = (kit.W - side) / 2, 14 |
| 69 | |
| 70 | |
| 71 | def at(p): |
| 72 | a, b, c, d = p |
| 73 | return x0 + b * (3 * s + g) + d * s + s / 2, y0 + a * (3 * s + g) + c * s + s / 2 |
| 74 | |
| 75 | |
| 76 | inside = set(cap) |
| 77 | body = [kit.circle(*at(p), 2.6, 'faint') for i, p in enumerate(P) if i not in inside] |
| 78 | body += [kit.dot(*at(P[i]), 8.5, 'f') for i in cap] |
| 79 | kit.write(__file__, 'mathematics', ''.join(body), label='20 points of F₃⁴, no three on a line') |