Previously, I made a free group $ F_2 $ in Python.
Create a free group with Python
This time, I will continue to create the dihedral group $ D_4 $.
Imagine a flat regular n-sided polygon. This time, we will make $ D_4 $, so think of a square.
About this square
--Operation r: Rotate the square clockwise 360 / n degrees --Operation t: Turn the square over --Operation e: Do nothing
Consider the three operations of. You can also think of these products (performing multiple operations in sequence) and the inverse element (the inverse element of r rotates counterclockwise, the inverse element of t is t). Also, the "do nothing" operation e corresponds to the identity element.
I'm skipping writing $ r ^ {-1} $, but the diagram looks like this:
It is similar to the free group $ F_2 $ in that it is generated from two elements r and t, but as a feature that the free group did not have. It has the properties of $ r ^ 4 = e $, $ t ^ 2 = e $, and even $ trt = r ^ {-1} $. (Did you notice the third property? It says that if you turn it over and rotate it clockwise, and then turn it over again, it will rotate counterclockwise.)
In fact, it may be worth knowing that any group can be created by adding these relations to the free group.
As with the free group, we keep it as a character string, but we want to keep it as simple as possible while considering the relational expression.
First, $ t ^ {-1} $ can be replaced with $ t $ and $ r ^ {-1} $ can be replaced with $ r ^ 3
Wouldn't it be easier? There was $ trt = r ^ {-1} $, but if you multiply it by $ t $ from the right, you get $ tr = r ^ {-1} t $ considering $ t ^ 2 = e $.
$ trrt = trttrt = r ^ {-1} r ^ {-1} $. If you repeat this, you will find $ tr ^ nt = tr ^ {-n} t $. As above, $ tr ^ n = r ^ {-n} t $. By repeating this, you can see that r ... rtr ... rtr ... rt is changed to r ... rt and t is shifted to the right.
As a result, we can see that any element can be written in regular expression in the form'r {, 3} t?'(0-3 r and 0 or 1 t).
Consider the case according to the presence or absence of t.
-When $ (r ^ n t) \ cdot (r ^ m t) $ ⇒ $ r ^ {n-m} $ -When $ (r ^ n t) \ cdot (r ^ m) $ ⇒ $ r ^ {n-m} t $ -When $ (r ^ n) \ cdot (r ^ m t) $ ⇒ $ r ^ {n + m} t $ -When $ (r ^ n) \ cdot (r ^ m) $ ⇒ $ r ^ {n + m} $
That is, if there is t on the left side, the number of r is n-m, otherwise it is n + m. Also, whether or not t is added at the end depends on whether the number of t is odd or even.
As with the free group, it would be nice to reverse the string and reverse everything, but considering the simple form, it can be divided into the following cases.
How, if t is attached, you will be the inverse element yourself. interesting.
Let's make the previously created FreeGroupBase
the parent class.
from collections import namedtuple
FreeGroupBase = namedtuple('FreeGroupBase', 's')
In addition, transcribe what we considered earlier into the code.
import re
class DihedralGroup(FreeGroupBase):
'''A group consisting of two elements, r and t, r^n = e, t^2 = e, trt = r^-Meet 1.
Here, n=4 (D_4)And said.
'''
check = re.compile('r{,3}t?')
def __init__(self, s: str):
if not self.check.fullmatch(s):
raise ValueError('Unexpected format')
def __mul__(self, other: 'DihedralGroup') -> 'DihedralGroup':
return DihedralGroup(self._reduction(self.s, other.s))
def __invert__(self) -> 'DihedralGroup':
# ~(r^n) = r^{-n}
# ~t = t
# ~(r^n t) = t r^{-n} = r^n t
if not self.s or self.s[-1] == 't':
return self
return DihedralGroup('r' * (4 - len(self.s)))
@staticmethod
def _reduction(lhs: str, rhs: str) -> str:
# r^4 = e, t^2 = t, trt = r^-There is a relational expression of 1.
# trt = r^-1 is tr= r^-It can be read as 1 t, so use this to move t to the right.
#Also, r^-k (k>0)If a shape like^{n-k}Replace with
#As a result, when written in regular expression, r{,4}t?It is summarized in the form of.
# lhs,rhs thinks this has already been done
def count(s):
'"r{,3}t?"Returns the number of r and the number of t in the string represented by'
if not s:
return 0, 0
if s[-1] == 't':
return len(s) - 1, 1
return len(s), 0
r1, t1 = count(lhs)
r2, t2 = count(rhs)
if t1:
r2 = -r2
return 'r' * ((r1 + r2) % 4) + 't' * ((t1 + t2) % 2)
def __repr__(self) -> str:
if not self.s:
return 'e'
return ' * '.join(self.s)
print(t * r * t)
print(t * r * t * r)
print(t * r * r * r * t)
print(e * e * t * r * e * r * e * e * e * r * t)
print(r * r * t * r)
print(~(r*r*r) * (r*r*r))
print(~(r*r*r*t) * (r*r*r*t))
r * r * r
e
r
r
r * t
e
e
I think I can do something.
This time, I made a dihedral group with Python. The dihedral group is a simple but useful group that is also used when considering molecular symmetry in chemical calculations.
Recommended Posts