Skip to content

Instantly share code, notes, and snippets.

@Terminus-IMRC
Last active April 24, 2019 12:46
Show Gist options
  • Save Terminus-IMRC/98f2603f5d1d821c971e7e34d74321ae to your computer and use it in GitHub Desktop.
Save Terminus-IMRC/98f2603f5d1d821c971e7e34d74321ae to your computer and use it in GitHub Desktop.
Isomorphism of MSQ

n = 3

$ time ./isomorph_msq.py
(0, 1, 2, 3, 4, 5, 6, 7, 8)
(0, 3, 6, 1, 4, 7, 2, 5, 8)
(2, 1, 0, 5, 4, 3, 8, 7, 6)
(2, 5, 8, 1, 4, 7, 0, 3, 6)
(6, 3, 0, 7, 4, 1, 8, 5, 2)
(6, 7, 8, 3, 4, 5, 0, 1, 2)
(8, 5, 2, 7, 4, 1, 6, 3, 0)
(8, 7, 6, 5, 4, 3, 2, 1, 0)
cnt = 8

real    5m11.675s
user    5m11.904s
sys     0m0.861s

As for MSQ3 (magic squares of order 3, or 3×3 magic squares), it is widely known that there are 8 isomorph transformations: 4 patterns by rotation and 2 patterns by reflection. The number "8" matches to the one the program calculated, so we have proved that the 8 patterns are the all isomorph transformations for MSQ3!

#!/usr/bin/env python3
import numpy as np
import sympy
import itertools
n = 3
m = np.zeros((2*n + 2, n*n), dtype = np.int)
for i in range(n):
m[0 * n + i, i * n + 0 : i * n + n] = 1
m[1 * n + i, i : n*n - (n-i) + 1 : n] = 1
m[2 * n + 0, 0 : n*n : n + 1] = 1
m[2 * n + 1, n - 1 : n*n - n + 1 : n - 1] = 1
M_orig = sympy.Matrix(m)
rech_orig = M_orig.rref()[0]
cnt = 0
for idxs in itertools.permutations(range(n*n)):
M = M_orig[:, idxs]
if M.rref()[0] == rech_orig:
print(idxs)
#sympy.pprint(M); print('-' * (3*n*n))
cnt += 1
print('cnt =', cnt)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment