Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Symmetric Elements (Group Theory)
import numpy as np
class SymmetricElement:
def __init__(self, *args):
self.data = np.array(list(args), dtype=np.int64)
# Confirm that there is at least one mapping
assert len(self.data) > 0
# Check bijective
assert len(np.unique(self.data)) == len(self)
# Check that range is between 1-length
assert self.data.min() == 1
assert self.data.max() == len(self)
def inverse(self):
inverse = np.empty(len(self), dtype=np.int64)
for x, fx in zip(range(len(self)), self.data):
inverse[fx.item() - 1] = x + 1
return SymmetricElement(*list(inverse))
def __len__(self):
return len(self.data)
def __mul__(self, g):
assert len(self) == len(g)
codomain = np.empty(len(self), dtype=np.int64)
for x in range(len(self)):
codomain[x] = self.data[g.data[x] - 1]
return SymmetricElement(*list(codomain))
def __eq__(self, g):
return (self.data == g.data).all()
# Print the element in two row notation
def __repr__(self):
two_row = np.empty((2, len(self)))
two_row[0] = range(1, len(self) + 1)
two_row[1] = self.data
return two_row.__repr__()
class Cycle(SymmetricElement):
def __init__(self, group_order, *args):
self.cycle = np.array(list(args), dtype=np.int64)
# Check to make sure the cycle is well formed (no repeats)
assert len(self.cycle) <= group_order
assert len(np.unique(self.cycle)) == len(self.cycle)
codomain = np.array(list(range(1, group_order + 1)))
for index, element in enumerate(self.cycle):
next_element = self.cycle[index + 1] if index < len(self.cycle) - 1 else self.cycle[0]
codomain[element - 1] = next_element
super(Cycle, self).__init__(*list(codomain))
def inverse(self):
return Cycle(len(self.cycle), *list(np.flip(self.cycle)))
def __repr__(self):
return "(" + ",".join([str(x) for x in self.cycle]) + ")"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment