Changeset 7447:cef8fe4d42f1


Ignore:
Timestamp:
11/26/07 02:14:48 (5 years ago)
Author:
Mike Hansen <mhansen@…>
Branch:
default
Message:

Fixed #1280

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sage/combinat/permutation.py

    r7279 r7447  
    3434import sage.rings.integer 
    3535from sage.groups.perm_gps.permgroup_element import PermutationGroupElement 
    36 from random import randint 
     36from random import randint, sample 
    3737from sage.interfaces.all import gap 
    3838from sage.graphs.graph import DiGraph 
     
    17901790          ['t', 'a', 'c']] 
    17911791    """ 
    1792     return Arrangements_msetk(mset, k) 
     1792    mset = list(mset) 
     1793    if map(mset.index, mset) == range(len(mset)): 
     1794        return Arrangements_setk(mset, k) 
     1795    else: 
     1796        return Arrangements_msetk(mset, k) 
    17931797 
    17941798 
     
    18471851            return 0 
    18481852 
     1853    def random(self): 
     1854        """ 
     1855        EXAMPLES: 
     1856            sage: Permutations(3,2).random() #random 
     1857            [1, 3] 
     1858        """ 
     1859        return sample(range(self.n), self.k) 
     1860 
    18491861class Permutations_mset(CombinatorialClass): 
    18501862    def __init__(self, mset): 
    18511863        """ 
    18521864        TESTS: 
    1853             sage: S = Permutations(['c','a','t']) 
     1865            sage: S = Permutations(['c','a','c']) 
    18541866            sage: S == loads(dumps(S)) 
    18551867            True 
     
    18601872        """ 
    18611873        TESTS: 
    1862             sage: repr(Permutations(['c','a','t'])) 
    1863             "Permutations of the (multi-)set ['c', 'a', 't']" 
    1864         """ 
    1865         return "Permutations of the (multi-)set %s"%self.mset 
     1874            sage: repr(Permutations(['c','a','c'])) 
     1875            "Permutations of the multi-set ['c', 'a', 'c']" 
     1876        """ 
     1877        return "Permutations of the multi-set %s"%self.mset 
    18661878     
    18671879    def iterator(self): 
     
    18711883 
    18721884        EXAMPLES: 
    1873             sage: [ p for p in Permutations(['c','a','t'])] 
    1874             [['c', 'a', 't'], 
    1875              ['c', 't', 'a'], 
    1876              ['a', 'c', 't'], 
    1877              ['a', 't', 'c'], 
    1878              ['t', 'c', 'a'], 
    1879              ['t', 'a', 'c']] 
    18801885            sage: [ p for p in Permutations(['c','t','t'])] 
    18811886            [['c', 't', 't'], ['t', 'c', 't'], ['t', 't', 'c']] 
     
    19351940            """ 
    19361941            EXAMPLES: 
    1937                 sage: Permutations([1,2,3]).count() 
    1938                 6 
    19391942                sage: Permutations([1,2,2]).count() 
    19401943                3 
     
    19531956            return c 
    19541957 
    1955  
    1956                  
     1958class Permutations_set(CombinatorialClass): 
     1959    def __init__(self, set): 
     1960        """ 
     1961        TESTS: 
     1962            sage: S = Permutations(['c','a','t']) 
     1963            sage: S == loads(dumps(S)) 
     1964            True 
     1965        """ 
     1966        self.set = set 
     1967 
     1968    def __repr__(self): 
     1969        """ 
     1970        TESTS: 
     1971            sage: repr(Permutations(['c','a','t'])) 
     1972            "Permutations of the set ['c', 'a', 't']" 
     1973        """ 
     1974        return "Permutations of the set %s"%self.set 
     1975     
     1976    def iterator(self): 
     1977        """ 
     1978        Algorithm based on: 
     1979        http://marknelson.us/2002/03/01/next-permutation/ 
     1980 
     1981        EXAMPLES: 
     1982            sage: [ p for p in Permutations(['c','a','t'])] 
     1983            [['c', 'a', 't'], 
     1984             ['c', 't', 'a'], 
     1985             ['a', 'c', 't'], 
     1986             ['a', 't', 'c'], 
     1987             ['t', 'c', 'a'], 
     1988             ['t', 'a', 'c']] 
     1989        """ 
     1990        set = self.set 
     1991        n = len(self.set) 
     1992        lset = __builtin__.list(set) 
     1993        set_list = map(lambda x: lset.index(x), lset) 
     1994        set_list.sort() 
     1995         
     1996        def label(x): 
     1997            return lset[x] 
     1998 
     1999        yield map(label, set_list) 
     2000 
     2001        if n == 1: 
     2002            return 
     2003 
     2004        while True: 
     2005            one = n - 2 
     2006            two = n - 1 
     2007            j   = n - 1 
     2008 
     2009            #starting from the end, find the first o such that 
     2010            #set_list[o] < set_list[o+1] 
     2011            while two > 0 and set_list[one] >= set_list[two]: 
     2012                one -= 1 
     2013                two -= 1     
     2014 
     2015            if two == 0: 
     2016                return 
     2017 
     2018            #starting from the end, find the first j such that 
     2019            #set_list[j] > set_list[one] 
     2020            while set_list[j] <= set_list[one]: 
     2021                j -= 1 
     2022 
     2023            #Swap positions one and j 
     2024            t = set_list[one] 
     2025            set_list[one] = set_list[j] 
     2026            set_list[j] = t 
     2027 
     2028 
     2029            #Reverse the list between two and last 
     2030            i = int((n - two)/2)-1 
     2031            #set_list = set_list[:two] + [x for x in reversed(set_list[two:])] 
     2032            while i >= 0: 
     2033                t = set_list[ i + two ] 
     2034                set_list[ i + two ] = set_list[n-1 - i] 
     2035                set_list[n-1 - i] = t 
     2036                i -= 1 
     2037 
     2038            #Yield the permutation 
     2039            yield map(label, set_list) 
     2040 
     2041    def count(self): 
     2042        """ 
     2043        EXAMPLES: 
     2044        sage: Permutations([1,2,3]).count() 
     2045        6 
     2046        """ 
     2047        return factorial(len(self.set)) 
     2048     
     2049    def random(self): 
     2050        """ 
     2051        EXAMPLES: 
     2052        sage: Permutations([1,2,3]).random() #random 
     2053        [2, 3] 
     2054        """ 
     2055        return sample(self.set, len(self.set)) 
    19572056 
    19582057class Permutations_msetk(CombinatorialClass): 
     
    19712070        TESTS: 
    19722071            sage: repr(Permutations([1,2,2],2)) 
    1973             'Permutations of the (multi-)set [1, 2, 2] of length 2' 
    1974         """ 
    1975         return "Permutations of the (multi-)set %s of length %s"%(self.mset,self.k) 
     2072            'Permutations of the multi-set [1, 2, 2] of length 2' 
     2073        """ 
     2074        return "Permutations of the multi-set %s of length %s"%(self.mset,self.k) 
    19762075 
    19772076    def list(self): 
     
    19822081            sage: Permutations([1,2,2],2).list() 
    19832082            [[1, 2], [2, 1], [2, 2]] 
    1984          """ 
     2083        """ 
     2084         
    19852085        mset = self.mset 
    19862086        n = len(self.mset) 
     
    19952095 
    19962096 
     2097class Permutations_setk(CombinatorialClass): 
     2098    def __init__(self, set, k): 
     2099        """ 
     2100        TESTS: 
     2101            sage: P = Permutations([1,2,2],2) 
     2102            sage: P == loads(dumps(P)) 
     2103            True 
     2104        """ 
     2105        self.set = set 
     2106        self.k = k 
     2107 
     2108    def __repr__(self): 
     2109        """ 
     2110        TESTS: 
     2111            sage: repr(Permutations([1,2,3],2)) 
     2112            'Permutations of the set [1, 2, 3] of length 2' 
     2113        """ 
     2114        return "Permutations of the set %s of length %s"%(self.set,self.k) 
     2115 
     2116    def _label(self, perm): 
     2117        """ 
     2118        Given a permutation of indices, return the corresponding 
     2119        permutation of self.set. 
     2120        """ 
     2121        return map(lambda x: self.set[x], perm) 
     2122     
     2123    def list(self): 
     2124        """ 
     2125        EXAMPLES: 
     2126            sage: Permutations([1,2,3],2).list() 
     2127            [[1, 2], [1, 3], [2, 1], [2, 3], [3, 1], [3, 2]] 
     2128         """ 
     2129 
     2130 
     2131        return map(self._label, permutation_nk.list(len(self.set), self.k)) 
     2132 
     2133    def iterator(self): 
     2134        """ 
     2135        EXAMPLES: 
     2136            sage: [i for i in Permutations([1,2,3],2)] 
     2137            [[1, 2], [1, 3], [2, 1], [2, 3], [3, 1], [3, 2]] 
     2138        """ 
     2139        for perm in permutation_nk.iterator(len(self.set), self.k): 
     2140            yield self._label(perm) 
     2141 
     2142    def random(self): 
     2143        """ 
     2144        EXAMPLES: 
     2145            sage: Permutations([1,2,3],2).random() #random 
     2146            [1, 3] 
     2147        """ 
     2148        return sample(self.set, self.k) 
     2149     
     2150 
    19972151class Arrangements_msetk(Permutations_msetk): 
    19982152    def __repr__(self): 
    19992153        """ 
    20002154        TESTS: 
     2155            sage: repr(Arrangements([1,2,2],2)) 
     2156            'Arrangements of the multi-set [1, 2, 2] of length 2' 
     2157        """ 
     2158        return "Arrangements of the multi-set %s of length %s"%(self.mset,self.k) 
     2159 
     2160class Arrangements_setk(Permutations_setk): 
     2161    def __repr__(self): 
     2162        """ 
     2163        TESTS: 
    20012164            sage: repr(Arrangements([1,2,3],2)) 
    2002             'Arrangements of the (multi-)set [1, 2, 3] of length 2' 
    2003         """ 
    2004         return "Arrangements of the (multi-)set %s of length %s"%(self.mset,self.k) 
     2165            'Arrangements of the set [1, 2, 3] of length 2' 
     2166        """ 
     2167        return "Arrangements of the set %s of length %s"%(self.set,self.k) 
    20052168 
    20062169 
     
    21062269            [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]] 
    21072270        """ 
    2108         for p in Permutations_mset(range(1,self.n+1)): 
     2271        for p in Permutations_set(range(1,self.n+1)): 
    21092272            yield Permutation_class(p) 
    21102273  
     
    34443607 
    34453608        sage: p = Permutations(['c', 'a', 't']); p 
    3446         Permutations of the (multi-)set ['c', 'a', 't'] 
     3609        Permutations of the set ['c', 'a', 't'] 
    34473610        sage: p.list() 
    34483611        [['c', 'a', 't'], 
     
    34543617 
    34553618        sage: p = Permutations(['c', 'a', 't'], 2); p 
    3456         Permutations of the (multi-)set ['c', 'a', 't'] of length 2 
     3619        Permutations of the set ['c', 'a', 't'] of length 2 
    34573620        sage: p.list() 
    34583621        [['c', 'a'], ['c', 't'], ['a', 'c'], ['a', 't'], ['t', 'c'], ['t', 'a']] 
     
    34603623 
    34613624        sage: p = Permutations([1,1,2]); p 
    3462         Permutations of the (multi-)set [1, 1, 2] 
     3625        Permutations of the multi-set [1, 1, 2] 
    34633626        sage: p.list() 
    34643627        [[1, 1, 2], [1, 2, 1], [2, 1, 1]] 
     
    34663629 
    34673630        sage: p = Permutations([1,1,2], 2); p 
    3468         Permutations of the (multi-)set [1, 1, 2] of length 2 
     3631        Permutations of the multi-set [1, 1, 2] of length 2 
    34693632        sage: p.list() 
    34703633        [[1, 1], [1, 2], [2, 1]] 
     
    35843747                return Permutations_nk(n,k) 
    35853748        else: 
    3586             if k is None: 
    3587                 return Permutations_mset(n) 
     3749            #In this case, we have that n is a list 
     3750            if map(n.index, n) == range(len(n)): 
     3751                if k is None: 
     3752                    return Permutations_set(n) 
     3753                else: 
     3754                    return Permutations_setk(n,k) 
    35883755            else: 
    3589                 return Permutations_msetk(n,k) 
     3756                if k is None: 
     3757                    return Permutations_mset(n) 
     3758                else: 
     3759                    return Permutations_msetk(n,k) 
    35903760    elif 'descents' in kwargs: 
    35913761        if isinstance(kwargs['descents'], tuple): 
Note: See TracChangeset for help on using the changeset viewer.