Python de symmetric group 2

Improvement of plusone () function

It seems that the method used in the previous tuple, which I should have seen somewhere, is not standard, so I will change it to list and further expand and improve it.

1 Last function

Previous function. The output is commented out. I changed the name to plusone_tuple ().

import numpy as np
from sympy.combinatorics import *
from sympy import *
init_printing(pretty_print=False)

n = 3

def plusone_tuple(x):
    X = tuple([0])
    for i in range(n):
        a = tuple([tuple(x)[i] + 1])
        X = X + a
    return Permutation(tuple(X))

print(plusone_tuple(Permutation(0,1,2)))

#(1 2 3)

It's a little cumbersome to combine, and you need to be careful about subscripts, but it's better to do it with list than with tuple. For the time being, I will write the element of the dihedral group multiplied by [0, 1, 2] using the inclusion notation.

A = PermutationGroup(SymmetricGroup(n)[0], SymmetricGroup(n)[1])._elements

print(A)
[list(A[i]) for i in range(len(A))]
'''
[Permutation(2), Permutation(0, 1, 2), Permutation(0, 2, 1), Permutation(1, 2), Permutation(2)(0, 1), Permutation(0, 2)]
[[0, 1, 2], [1, 2, 0], [2, 0, 1], [0, 2, 1], [1, 0, 2], [2, 1, 0]]
'''

2 This function

An improved function that can handle a list function and a list of multiple substitutions.

from sympy.combinatorics import *
from sympy import *
init_printing(pretty_print=False)

def plusone(x):
    add_one = [(i > 0) * (list(x)[i - 1] + 1) for i in range(1 + len(list(x)))]
    #(i > 0) is a step function.
    return Permutation(add_one)

n = 3
def plusone_list(x):
    return plusone(x)

def plusone_multi(list_of_permutations):
    return [plusone(list_of_permutations[i]) for i in range(len(list_of_permutations))]

print(A) 
AA = plusone_multi(A)
AA

'''
[Permutation(2), Permutation(0, 1, 2), Permutation(0, 2, 1), Permutation(1, 2), Permutation(2)(0, 1), Permutation(0, 2)]
[(3), (1 2 3), (1 3 2), (2 3), (3)(1 2), (1 3)]
'''

By the way, it is the same even if it is troublesome.

generate_Sym3_perm = PermutationGroup(plusone(SymmetricGroup(n)[0]), plusone(SymmetricGroup(n)[1]))._elements
generate_Sym3_perm
'''
[(3), (1 2 3), (1 3 2), (2 3), (3)(1 2), (1 3)]
'''

The following list_multi () converts a list of multiple permutations into a list. Now, list can handle the elements of symmetric groups in a unified manner without the intervention of tuples. This is simpler no matter how you think about it.

def list_multi(list_of_permutations):
    return [list(list_of_permutations[i]) for i in range(len(list_of_permutations))]
print(list_multi(A))
print(list_multi(AA))
'''
[[0, 1, 2], [1, 2, 0], [2, 0, 1], [0, 2, 1], [1, 0, 2], [2, 1, 0]]
[[0, 1, 2, 3], [0, 2, 3, 1], [0, 3, 1, 2], [0, 1, 3, 2], [0, 2, 1, 3], [0, 3, 2, 1]]
'''

3 Simple calculation of dihedral group of symmetry

Display the elements of the dihedral group of order.

Sym3_perm = SymmetricGroup(n)._elements
Sym3_perm
'''
[(2), (0 1 2), (0 2 1), (1 2), (2)(0 1), (0 2)]
'''

Let's change the display from 1 to the list (result of replacing [0,1,2,3]).

Sym3_list = list_multi(plusone_multi(Sym3_perm))
Sym3_list
'''
[[0, 1, 2, 3], [0, 2, 3, 1], [0, 3, 1, 2], [0, 1, 3, 2], [0, 2, 1, 3], [0, 3, 2, 1]]
'''

I got the result I wanted to get. Leave 0 as the replacement and list cannot be converted to each other. The program treats "fix 3 and replace 1, 2" first, and put "fix 2 and replace 1, 3" = (1 3) after. The original naming is likely to be an issue. Especially for a fourth-order symmetric group whose original number is 24.

Consideration

I noticed that np.array can do it, so I will measure the calculation time and compare it.

First of all, above.

%%timeit
Sym3_list = list_multi(plusone_multi(Sym3_perm))
Sym3_list
#129 µs ± 6.92 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
#132 µs ± 13.1 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
#132 µs ± 13.1 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

numpy.

%%timeit
Sym3_list_np = np.array(plusone_multi(Sym3_perm)).tolist()
Sym3_list_np
#139 µs ± 10.7 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
#135 µs ± 5.21 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
#143 µs ± 8.14 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

・ The first time is list_multi → np.array

・ The second time is np.array → list_multi

・ The third time I tried to execute in parallel.

It can be said that they are the same within the margin of error. However, I think that using np.array makes it a little heavier, or using list_multi () is faster and more stable. You don't even need to import numpy here.

Previous article: Python de symmetric group --Qiita

Next article: Python de Symmetric Group 3-Qiita

Recommended Posts

Python de symmetric group 3
Python de symmetric group 2
Python de symmetric group
Python de BDD (in Lettuce)