Hi,

I wanted to raise a few minor issues with permutations in Sage, which have 
been annoying me (and presumably others) for a long time now. I propose a 
number of fixes below.

(1) It is a pain to enter permutations in cycle notation.

sage: Permutation("(1,2,3)(4,5)")

hurts my eyes (it just took me MUCH longer than it should have to type this 
line)

and

sage: Permutation(((1,2,3),(4,5)))

is pretty bad, too.

(2) Permutations display as lists by default (so the above displays as [2, 
3, 1, 5, 4]). Of course this is desirable in certain contexts, but awkward 
in others.

(3) The .parent() method returns class 
'sage.combinat.permutation.StandardPermutations_all_with_category' ; again, 
sometimes this makes a lot of sense, but sometimes you'd like the answer to 
be SymmetricGroup(5). Recall that

sage: polygen(QQ).parent()

Univariate Polynomial Ring in x over Rational Field


The answer is not bla.Polynomials_all_with_category.



*** Suggestion 1:

--change the Permutation constructor so it accepts Permutation( (1,2,3), 
(4,5) ). Already a bit better.

--use a global flag, if it doesn't already exist, so that the permutations 
are displayed in cycle notation.

--ignore problem 3.



*** Suggestion 2:

Just use:


sage: def perm(*args): return SymmetricGroup(max(flatten(args)))( args )

sage: perm( (1,2,3), (4,5) )

(1,2,3)(4,5)

sage: _.parent()

Symmetric group of order 5! as a permutation group


Then come up with a better name than "perm".



*** Suggestion 3:


I have this in my init.sage:



class Perm:                                                                 
                                                               

    def __init__(self, *args):                                             
                                                                

        self.string= str(args) if len(args) > 1 else "()"                   
                                                               

        self.max= max(args) if len(args) > 1 else args[0]                   
                                                               

                                                                            
                                                               

    def __call__(self, *args):                                             
                                                                

        if len(args) > 1 :                                                 
                                                                

            self.string+= str(args)                                         
                                                               

            self.max= max( self.max, max(args) )                           
                                                                

            return self                                                     
                                                               

        elif len(args) == 1:                                               
                                                                

            return SymmetricGroup(args[0])(self.string)                     
                                                               

        elif len(args) == 0:                                               
                                                                

            return SymmetricGroup(self.max)(self.string)                   
                                                                

                                                                            
                                                               

    def __str__(self):                                                     
                                                                

        return "Object is not a valid permutation. Example syntax: Perm(1, 
2, 3)(4, 5)(). Did you forget the () in the end?"               

                                                                            
                                                               

    def __repr__(self):                                                     
                                                               

        return str(self)   


And then:


sage: Perm(1,2,3)(4,5)()

(1,2,3)(4,5)

sage: Perm(1,2,3)(4,5)(10)

(1,2,3)(4,5)

sage: _.parent()

Symmetric group of order 10! as a permutation group
sage: Perm(1,2,3)(4,5)
Object is not a valid permutation. Example syntax: Perm(1, 2, 3)(4, 5)(). 
Did you forget the () in the end?

I've been told that it's not very Pythonic. I agree ! However in Sympy you 
can use Permutation(1,2,3)(4,5), which is similar.

A variant would be to make this class iterable, call it Cycles, and use 
something like

Permutation( Cycles(1,2,3)(4,5) )
SymmetricGroup(5)( Cycles(1,2,3)(4,5) )

... as the case may be. (I'm assuming that the constructors can take 
anything that can be iterated, right?) In terms of having less to type, I'm 
not sure if the variant is such a great idea.


I'd like to hear what everyone thinks. I'm happy to implement something 
myself (I'm now the proud owner of a trac account !)

cheers
Pierre
































-- 
You received this message because you are subscribed to the Google Groups 
"sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-devel+unsubscr...@googlegroups.com.
To post to this group, send email to sage-devel@googlegroups.com.
Visit this group at http://groups.google.com/group/sage-devel.
For more options, visit https://groups.google.com/d/optout.

Reply via email to