import numpy as np
from math import log, ceil
from misc.bin2svg import bin2svg # https://pastebin.com/y8rY5Vj4
from octahedral_permutations import * # https://pastebin.com/JBx0TSsC
from store_svg import * # https://pastebin.com/FaBF6k7E
def number_to_reverse_binary_list(n, length):
bin_str = "{0:b}".format(n).zfill(length)[::-1]
return [int(digit) for digit in bin_str]
def perm_to_bin_mat(perm):
long = len(perm)
high = ceil(log(max(perm), 2))
mat = np.zeros([high, long], dtype=bool)
for i, e in enumerate(perm):
mat[:, i] = number_to_reverse_binary_list(e, high)
return mat
def perm_to_svg_path(perm):
mat = perm_to_bin_mat(perm)
return bin2svg(mat)
conjugacy_class_to_color = {
'A': 'white', 'B': 'white', 'C': 'white', 'D': 'green', 'E': 'orange',
'a': 'yellow', 'b': 'yellow', 'c': 'yellow', 'd': 'blue', 'e': 'red'
}
gray_cube_coordinates = [(0, 3), (0, 1), (1, 2), (1, 0), (2, 3), (2, 1), (3, 2), (3, 0)]
arrow_cube_coordinates = [(0, 3), (0, 1), (.6, 2.2), (.6, .2), (2, 3), (2, 1), (2.6, 2.2), (2.6, .2)]
for filenum in range(6):
footer_perm_formulas = ''
footer_perm_matrix = ''
footer_invperm_formulas = ''
footer_invperm_matrix = ''
filename = 'test %s.svg' % filenum
rectangles_svg = ''
gray_matrix_text_svg = ''
for rectnum in range(8):
permdict = octahedral_permutations[(rectnum, filenum)]
invpermdict = octahedral_permutations[permdict['inverse']]
perm, invperm = permdict['perm'], invpermdict['perm']
# background and cube colors
rectbeige = 'f6e6d2' if permdict['parity'] else 'fcf9f0' # light if parity 0
(bottomgray, topgray) = ('ddd', '888') if rectnum in [0, 3, 5, 6] else ('888', 'ddd')
if rectnum == 0:
(bottombeige, topbeige) = ('f6e6d2', 'fcf9f0') if permdict['parity'] else ('fcf9f0', 'f6e6d2')
# conjugacy class colors
conjug_letter = permdict['conjug']
conjug_color_name = conjugacy_class_to_color[conjug_letter]
conjug_color = {
'white': 'ffffff', 'green': '33d42a', 'orange': 'ffa200',
'yellow': 'ffff7f', 'blue': '3375ff', 'red': 'ef2500'
}[conjug_color_name]
# permutation IDs (0..23, 0'..23')
int24 = permdict['int'] % 24
apostrophe = permdict['int'] >= 24
bold = int24 in [0, 7, 16, 23]
perm_id = str(int24) + apostrophe*"'"
perm_id_circle_svg = '' \
'' \
'%s' \
% ('3' if bold else '1.5', conjug_color, 'bold' if bold else 'normal', perm_id)
# arrows
arrows_svg = ''
for i, p in enumerate(perm):
if i != p:
c1 = arrow_cube_coordinates[i]
c2 = arrow_cube_coordinates[p]
diff = np.array(c2) - np.array(c1)
diff_length = np.sqrt(diff[0] ** 2 + diff[1] ** 2)
diff_short = diff * .32 / diff_length # .32 is the approximate length of the arrow end before scaling by 30
if perm[p] == i: # if this is a transposition, make one double arrow
if i < p:
c1_short = c1 + diff_short
c2_short = c2 - diff_short
coordinates = (
round(c1_short[0], 3), round(c1_short[1], 3), round(c2_short[0], 3), round(c2_short[1], 3)
)
arrows_svg += '' \
'' \
'' % coordinates
else: # if no transposition
c2_short = c2 - diff_short
coordinates = (c1[0], c1[1], round(c2_short[0], 3), round(c2_short[1], 3))
arrows_svg += '' \
'' \
'' % coordinates
# permuted numbers
perm_vector = '%s%s' \
'%s%s' \
'%s%s' \
'%s%s' % perm
invperm_vector = '%s%s' \
'%s%s' \
'%s%s' \
'%s%s' % invperm
invperm_cube = '%s%s' \
'%s%s' \
'%s%s' \
'%s%s' % invperm
# letters
perm_letters = '' \
'' \
'' % permdict['letters']
invperm_letters = '' \
'' \
'' % invpermdict['letters']
# negators
perm_negators = ''
for i, d in enumerate(permdict['negators']):
if d:
perm_negators += '' % (101 + i * 28)
invperm_negators = ''
for i, d in enumerate(invpermdict['negators']):
if d:
invperm_negators += '' % (101 + i * 28)
# formulas (letters with negators)
perm_formula = formula_svg_store.format(letters=perm_letters, negators=perm_negators)
invperm_formula = formula_svg_store.format(letters=invperm_letters, negators=invperm_negators)
# SVG matrices
perm_mat = perm_to_svg_path(perm)
invperm_mat = perm_to_svg_path(invperm)
# where to move the rectangle
(rectx, recty) = (gray_cube_coordinates[rectnum][0] * 200, gray_cube_coordinates[rectnum][1] * 200)
# append to ``rectangles_svg``
rectangles_svg += rectangle_svg_store.format(rectx=rectx, recty=recty,
idcircle=perm_id_circle_svg,
pmat=perm_mat, ipmat=invperm_mat,
pform=perm_formula, ipform=invperm_formula,
arrows=arrows_svg,
cubenums=invperm_cube, pvect=perm_vector, ipvect=invperm_vector,
bottomgray=bottomgray, topgray=topgray, beige=rectbeige)
# append to footer variables
footer_perm_formulas += '{f}'.format(y=rectnum*24, f=perm_formula)
footer_perm_matrix += '{v}'.format(y=rectnum*24, v=perm_vector)
footer_invperm_formulas += '{f}'.format(y=rectnum*24, f=invperm_formula)
footer_invperm_matrix += '{v}'.format(y=rectnum*24, v=invperm_vector)
# create SVG
svg_string = svg_string_store.format(
rectangles=rectangles_svg,
pf=footer_perm_formulas,
pm=footer_perm_matrix,
ipf=footer_invperm_formulas,
ipm=footer_invperm_matrix,
bottombeige=bottombeige,
topbeige=topbeige
)
svg_file = open(filename, 'w')
svg_file.write(svg_string)