Try to make a dihedral group with Python

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 $.

What is a dihedral group?

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:

Screenshot_20191125_181417.png

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.

Let's implement

I want to make it a simple shape

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 . ( r \ cdot r ^ {3} = e $, so $ r ^ {-1} = r ^ {3} $) Considering $ t ^ 2 = e $, $ r ^ 4 = e $, we can count the number of r and t and take the remainder divided by 4 and the remainder divided by 2.

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).

Think about the product

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.

Think of the inverse element

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.

code

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)

Try

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.

Summary

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

Try to make a dihedral group with Python
Try to make a command standby tool with python
Try to draw a life curve with python
I want to make a game with Python
Make a fortune with Python
Try to make a capture software with as high accuracy as possible with python (2)
Try to make a Python module in C language
Let's make a GUI with python.
Try to operate Facebook with Python
Make a recommender system with python
WEB scraping with python and try to make a word cloud from reviews
Let's make a graph with python! !!
Experiment to make a self-catering PDF for Kindle with Python
Try to bring up a subwindow with PyQt5 and Python
Try to reproduce color film with Python
Try logging in to qiita with Python
Let's make a shiritori game with Python
[Python] How to make a class iterable
Try to make a kernel of Jupyter
Let's create a free group with Python
Fractal to make and play with Python
Let's make a voice slowly with Python
Try HTML scraping with a Python library
Let's make a web framework with Python! (1)
Try drawing a map with python + cartopy 0.18.0
Make a desktop app with Python with Electron
Let's make a Twitter Bot with Python!
Let's make a web framework with Python! (2)
[3rd] I tried to make a certain authenticator-like tool with python
Try to create a python environment with Visual Studio Code & WSL
How to make a surveillance camera (Security Camera) with Opencv and Python
Try to make a web service-like guy with 3D markup language
I tried to make a periodical process with Selenium and Python
I tried to make a 2channel post notification application with Python
[Introduction] I want to make a Mastodon Bot with Python! 【Beginners】
Try to make capture software with as high accuracy as possible with python (1)
I tried to make a todo application using bottle with python
[4th] I tried to make a certain authenticator-like tool with python
[1st] I tried to make a certain authenticator-like tool with python
Try adding a wall to your IFC file with IfcOpenShell python
Try to make your own AWS-SDK with bash
[TCP / IP] After studying, try to make an HTTP client-like with Python
How to read a CSV file with Python 2/3
Try scraping with Python.
Python: I tried to make a flat / flat_map just right with a generator
Send a message to LINE with Python (LINE Notify)
[Cloudian # 3] Try to create a new object storage bucket with Python (boto3)
A memorandum to make WebDAV only with nginx
Make a Twitter trend bot with heroku + Python
[Python] Make a game with Pyxel-Use an editor-
Python beginners decided to make a LINE bot with Flask (Flask rough commentary)
Try to solve the man-machine chart with Python
How to make a dictionary with a hierarchical structure.
Try to make foldl and foldr with Python: lambda. Also time measurement
Try to automatically generate Python documents with Sphinx
I tried to make a traffic light-like with Raspberry Pi 4 (Python edition)
[Python] Make a simple maze game with Pyxel
Decide to assign a laboratory with Python (fiction)
Steps to create a Twitter bot with python
Let's replace UWSC with Python (5) Let's make a Robot
[Python] Try to make a sort program by yourself. (Selection sort, insertion sort, bubble sort)