The Thomson problem
Find or prove the minimum-energy arrangements of N electrons on a sphere; optimality is proved only for a few small N.

| 1 | """Nine electrons on a sphere in their lowest-energy arrangement known, N = 9 being the smallest case with no proof. |
| 2 | |
| 3 | Computed here: Coulomb energy minimized by projected gradient descent from several seeded random starts (Python's |
| 4 | random, seed 1); every start reaches the same energy, 25.7599865313, which is asserted so that a change shows. Drawn with its convex hull, the edges in front solid and those behind dashed, the electrons |
| 5 | larger and darker the nearer they are. |
| 6 | """ |
| 7 | import math |
| 8 | import random |
| 9 | from itertools import combinations |
| 10 | from pathlib import Path |
| 11 | |
| 12 | import kit |
| 13 | |
| 14 | N = 9 |
| 15 | sub = lambda a, b: [x - y for x, y in zip(a, b)] |
| 16 | dot = lambda u, v: sum(x * y for x, y in zip(u, v)) |
| 17 | cross = lambda u, v: [u[1] * v[2] - u[2] * v[1], u[2] * v[0] - u[0] * v[2], u[0] * v[1] - u[1] * v[0]] |
| 18 | unit = lambda u: [c / math.hypot(*u) for c in u] |
| 19 | |
| 20 | |
| 21 | def energy(P): |
| 22 | return sum(1 / math.dist(p, q) for p, q in combinations(P, 2)) |
| 23 | |
| 24 | |
| 25 | def relax(P, steps=4000): |
| 26 | """Projected gradient descent with an adaptive step; returns the points, their energy and the tangential force.""" |
| 27 | E, h = energy(P), 0.05 |
| 28 | for _ in range(steps): |
| 29 | F = [[0.0] * 3 for _ in P] |
| 30 | for i, j in combinations(range(len(P)), 2): |
| 31 | d = sub(P[i], P[j]); r3 = math.hypot(*d) ** 3 |
| 32 | for k in range(3): |
| 33 | F[i][k] += d[k] / r3; F[j][k] -= d[k] / r3 |
| 34 | T = [sub(f, [dot(p, f) * c for c in p]) for p, f in zip(P, F)] |
| 35 | g = math.sqrt(sum(c * c for t in T for c in t)) |
| 36 | if g < 1e-11: |
| 37 | break |
| 38 | Q = [unit([a + h * b for a, b in zip(p, t)]) for p, t in zip(P, T)] |
| 39 | E2 = energy(Q) |
| 40 | if E2 < E: |
| 41 | P, E, h = Q, E2, h * 1.2 |
| 42 | else: |
| 43 | h *= 0.5 |
| 44 | return P, E, g |
| 45 | |
| 46 | |
| 47 | rnd = random.Random(1) |
| 48 | runs = [relax([unit([rnd.gauss(0, 1) for _ in range(3)]) for _ in range(N)]) for _ in range(4)] |
| 49 | P, E, g = min(runs, key=lambda r: r[1]) |
| 50 | assert all(abs(r[1] - E) < 1e-9 for r in runs) and g < 1e-6, [r[1] for r in runs] |
| 51 | assert f'{E:.10f}' == '25.7599865313', E |
| 52 | |
| 53 | # the convex hull, by brute force: a triple is a face when every other point lies on one side of its plane |
| 54 | faces = [] |
| 55 | for t in combinations(range(N), 3): |
| 56 | n = cross(sub(P[t[1]], P[t[0]]), sub(P[t[2]], P[t[0]])) |
| 57 | side = [dot(sub(P[m], P[t[0]]), n) for m in range(N) if m not in t] |
| 58 | if all(s < 0 for s in side) or all(s > 0 for s in side): |
| 59 | faces.append((t, n if all(s < 0 for s in side) else [-c for c in n])) # outward normal |
| 60 | assert len(faces) == 2 * N - 4 |
| 61 | |
| 62 | |
| 63 | def view(d, up=(0, 1, 0)): |
| 64 | d = unit(d); e1 = unit(cross(up, d)); e2 = cross(d, e1) |
| 65 | return lambda p: [dot(p, e) for e in (e1, e2, d)] |
| 66 | |
| 67 | |
| 68 | # look from the direction (on a 6-degree grid) that spreads the nine points farthest apart on the page |
| 69 | dirs = [(math.sin(a) * math.cos(b), math.sin(a) * math.sin(b), math.cos(a)) for a in [i * math.pi / 30 for i in range(1, 30)] |
| 70 | for b in [j * math.pi / 30 for j in range(60)]] |
| 71 | see = view(max(dirs, key=lambda d: min(math.dist(view(d)(p)[:2], view(d)(q)[:2]) for p, q in combinations(P, 2)))) |
| 72 | Q = [see(p) for p in P] |
| 73 | front = {} |
| 74 | for t, n in faces: |
| 75 | for a, b in combinations(sorted(t), 2): |
| 76 | front[a, b] = front.get((a, b), False) or see(n)[2] > 0 |
| 77 | assert len(front) == 3 * N - 6 |
| 78 | |
| 79 | R, CX, CY = 118, kit.W / 2, 134 |
| 80 | at = lambda q: (CX + q[0] * R, CY - q[1] * R) |
| 81 | body = ('<defs><radialGradient id="sphere" cx=".38" cy=".34" r=".8"><stop offset="0" style="stop-color:var(--f);stop-opacity:0"/>' |
| 82 | '<stop offset="1" style="stop-color:var(--f);stop-opacity:.22"/></radialGradient></defs>') |
| 83 | body += kit.circle(CX, CY, R, 'f', ' style="fill:url(#sphere)"') + kit.circle(CX, CY, R, 'faint-s', ' stroke-width="2"') |
| 84 | body += ''.join(kit.line(*at(Q[a]), *at(Q[b]), 'faint-s', 2, ' stroke-dasharray="4 5"') for (a, b), f in front.items() if not f) |
| 85 | body += ''.join(kit.line(*at(Q[a]), *at(Q[b]), 'ink2-s', 2.4) for (a, b), f in front.items() if f) |
| 86 | for q in sorted(Q, key=lambda q: q[2]): |
| 87 | near = (q[2] + 1) / 2 |
| 88 | r = 6 + 4.5 * near |
| 89 | body += kit.circle(*at(q), r, 'ink', ' style="fill:var(--bg)"') + kit.circle(*at(q), r, 'f ring', f' fill-opacity="{0.4 + 0.6 * near:.2f}"') |
| 90 | kit.write(__file__, 'mathematics', body, label='N = 9 electrons · lowest energy known, not proved') |