Ask Your Question
0

apply Permutation cycles to a letters list

asked 2024-11-19 16:44:05 +0100

ortollj gravatar image

updated 2024-11-22 09:02:16 +0100

Hi

Sorry for this very basic question, but how to apply the Permutation function to a list of letters ? I think it should exist a simplest way than the the cooking below. I searched the web, without success !

def applyGen(ge,dic) :   
    return [ge[dic.get(e)] for e in dic.keys() ]

def decrement_dic(dic): 
    return {k - 1: v - 1 for k, v in dic.items()} 

lettersL=['a','b','c','d','e','f','g','h']

test=Permutation((1,5,8,4),(2,6,7,3))
dicD=decrement_dic(test.dict())
applyGen(lettersL,dicD)
edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted
2

answered 2024-11-19 17:17:37 +0100

FrédéricC gravatar image

updated 2024-11-19 18:28:45 +0100

Like this

sage: L = ['a','b','c','d','e','f','g','h']
sage: test = Permutation((1,5,8,4),(2,6,7,3))
sage: [L[test(i + 1) - 1] for i in range(len(L))]
['e', 'b', 'c', 'a', 'h', 'f', 'g', 'd']

Note that Python numbering starts from 0.

edit flag offensive delete link more

Comments

ok thank you @FrédéricC

ortollj gravatar imageortollj ( 2024-11-19 18:26:40 +0100 )edit

Btw, one can replace test(i + 1) with test[i].

Max Alekseyev gravatar imageMax Alekseyev ( 2024-11-20 03:27:17 +0100 )edit

@Max Alekseyev What do you mean ?

ortollj gravatar imageortollj ( 2024-11-22 09:04:50 +0100 )edit
1

I mean that [L[test[i] - 1] for i in range(len(L))] will work equally well.

Max Alekseyev gravatar imageMax Alekseyev ( 2024-11-22 14:30:58 +0100 )edit
ortollj gravatar imageortollj ( 2024-11-22 15:17:25 +0100 )edit
0

answered 2024-11-22 08:34:23 +0100

ortollj gravatar image

updated 2024-11-26 05:59:46 +0100

I propose to improve @FrédéricC's solution as below to avoid errors in certain cases :

tuplesL=[ ((1,5,8,4),(2,6,7,3)), ((4,5)),((1,2,3)),((1,2,3),(4,5)),((1,2,3),(4,5),(7,8))]
for t in tuplesL :
    print('permutation tuples= ',t)
    try :
        test=Permutation(t)
        print('FredericC solution OK : ',[L[test(i + 1) - 1] for i in range(len(L))])
    except :
        print('error from FredericC solution')
    P=Permutation(t)
    Pletters= [L[P(i + 1) - 1] for i in range(len(P.dict()))] +[L[i] for i in range(len(P.dict()),len(L))]
    print(Pletters)

But unfortunately this solution does not allow singletons in list of tuples ! like Wolfram alpha do

see Max Alekseyev solution but unfortunately it does not accept element which appears more than once in the input like Wolfram does

As this post has become a bit messy, final correct answer from Max Alekseyev to the question below:

import string
import sage.combinat.permutation as permutation
nl=12 # letters number
L=list(string.ascii_lowercase[0:nl])
tuplesL=[ ((1,5,8,4),(2,6,7,3)), ((4,5),),((1,2,3),),((1,2,3),(4,5),),((1,2,3),(4,5),(7,8)),((1, 2 ,6),(3, 5),),
((1, 2, 3, 4),(1, 3)(2, 4),)]
for t in tuplesL :
    print('permutation tuples= ',t)
    try :
        #test=permutation.from_cycles(len(L),t)
        test = prod(map(lambda x: permutation.from_cycles(len(L),[x]),t))
        print('Max Alekseyev solution OK : ',[L[test[i] - 1] for i in range(len(L))])
    except :
        print('error ')
edit flag offensive delete link more

Comments

But that doesn't accept singletons is a minor problem

ortollj gravatar imageortollj ( 2024-11-22 08:58:48 +0100 )edit
1

First, to specify tuples with single cycles, you need to have an extra "," - like ((4,5),) and ((1,2,3),). Second, to construct permutations with implicut presence of singletons you can replace test=Permutation(t) with

import sage.combinat.permutation as permutation
test = permutation.from_cycles(len(L),t)
Max Alekseyev gravatar imageMax Alekseyev ( 2024-11-22 14:41:32 +0100 )edit

Thank you @Max Alekseyev

import string
import sage.combinat.permutation as permutation
nl=12 # letters number
L=list(string.ascii_lowercase[0:nl])
tuplesL=[ ((1,5,8,4),(2,6,7,3)), ((4,5),),((1,2,3),),((1,2,3),(4,5),),((1,2,3),(4,5),(7,8)),((1, 2 ,6),(3, 5),)]
for t in tuplesL :
    print('permutation tuples= ',t)
    try :
        test=permutation.from_cycles(len(L),t)
        print('Max Alekseyev solution OK : ',[L[test[i] - 1] for i in range(len(L))])
    except :
        print('error from Max Alekseyev solution solution')

is it possible to transform Max Alekseyev solution as THE solution of this question ?

ortollj gravatar imageortollj ( 2024-11-22 15:43:52 +0100 )edit

Just edit your own answer.

Max Alekseyev gravatar imageMax Alekseyev ( 2024-11-22 16:42:28 +0100 )edit

I wanted to copy Max Alekseyev's solution into the incomplete solution I gave.So first, I tried to transform my answer into a comment using the menu --> more --> repost as a question comment, but nothing happens.

ortollj gravatar imageortollj ( 2024-11-22 17:07:25 +0100 )edit

If you want to allow product of non-disjoint cycles, then use

test = prod(map(lambda x: permutation.from_cycles(len(L),[x]),t))
Max Alekseyev gravatar imageMax Alekseyev ( 2024-11-24 16:57:26 +0100 )edit

Thank you @Max Alekseyev

ortollj gravatar imageortollj ( 2024-11-25 08:53:27 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2024-11-19 16:44:05 +0100

Seen: 201 times

Last updated: Nov 26