admin管理员组

文章数量:1323550

Below is a small part of what I'm trying to run, it gets stuck on the final line

k=2
elements = [i for i in np.arange(k)]
Sk = PermutationGroup([Permutation(list(perm)) for perm in itertools.permutations(elements)])
factorial = int(sp.special.factorial(k))
for i in np.arange(factorial):
    trace_list = np.zeros(Sk[i].cycles)
    ...

The problem is that the list index is out of range because Sk doesn't seem to include the identity element when you list all of the elements:

k=2
elements = [i for i in np.arange(k)]
Sk = PermutationGroup([Permutation(list(perm)) for perm in itertools.permutations(elements)])
print(Sk)

PermutationGroup([(0 1)])

I know that the identity exists as Sk.identity, but I just want to be able to simply loop through Sk without having to put special cases for every time the identity is needed. Is there a way to do this?

Below is a small part of what I'm trying to run, it gets stuck on the final line

k=2
elements = [i for i in np.arange(k)]
Sk = PermutationGroup([Permutation(list(perm)) for perm in itertools.permutations(elements)])
factorial = int(sp.special.factorial(k))
for i in np.arange(factorial):
    trace_list = np.zeros(Sk[i].cycles)
    ...

The problem is that the list index is out of range because Sk doesn't seem to include the identity element when you list all of the elements:

k=2
elements = [i for i in np.arange(k)]
Sk = PermutationGroup([Permutation(list(perm)) for perm in itertools.permutations(elements)])
print(Sk)

PermutationGroup([(0 1)])

I know that the identity exists as Sk.identity, but I just want to be able to simply loop through Sk without having to put special cases for every time the identity is needed. Is there a way to do this?

Share Improve this question asked Jan 12 at 4:31 roshokaroshoka 1711 silver badge9 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Key Adjustments:

  • Add Identity Permutation: Manually include the identity permutation in the list of permutations using all_permutations.append(identity_perm)

  • Iterate Smoothly: Ensure the iteration through Sk properly accounts for the identity permutation, eliminating any potential index errors.

By doing this, the identity permutation becomes a regular part of Sk, so you won’t need to handle it separately.

本文标签: pythonHow to loop through a permutation group in SymPy including the identityStack Overflow