Changeset 7447:cef8fe4d42f1
- Timestamp:
- 11/26/07 02:14:48 (5 years ago)
- Branch:
- default
- File:
-
- 1 edited
-
sage/combinat/permutation.py (modified) (16 diffs)
Legend:
- Unmodified
- Added
- Removed
-
sage/combinat/permutation.py
r7279 r7447 34 34 import sage.rings.integer 35 35 from sage.groups.perm_gps.permgroup_element import PermutationGroupElement 36 from random import randint 36 from random import randint, sample 37 37 from sage.interfaces.all import gap 38 38 from sage.graphs.graph import DiGraph … … 1790 1790 ['t', 'a', 'c']] 1791 1791 """ 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) 1793 1797 1794 1798 … … 1847 1851 return 0 1848 1852 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 1849 1861 class Permutations_mset(CombinatorialClass): 1850 1862 def __init__(self, mset): 1851 1863 """ 1852 1864 TESTS: 1853 sage: S = Permutations(['c','a',' t'])1865 sage: S = Permutations(['c','a','c']) 1854 1866 sage: S == loads(dumps(S)) 1855 1867 True … … 1860 1872 """ 1861 1873 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.mset1874 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 1866 1878 1867 1879 def iterator(self): … … 1871 1883 1872 1884 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']]1880 1885 sage: [ p for p in Permutations(['c','t','t'])] 1881 1886 [['c', 't', 't'], ['t', 'c', 't'], ['t', 't', 'c']] … … 1935 1940 """ 1936 1941 EXAMPLES: 1937 sage: Permutations([1,2,3]).count()1938 61939 1942 sage: Permutations([1,2,2]).count() 1940 1943 3 … … 1953 1956 return c 1954 1957 1955 1956 1958 class 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)) 1957 2056 1958 2057 class Permutations_msetk(CombinatorialClass): … … 1971 2070 TESTS: 1972 2071 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) 1976 2075 1977 2076 def list(self): … … 1982 2081 sage: Permutations([1,2,2],2).list() 1983 2082 [[1, 2], [2, 1], [2, 2]] 1984 """ 2083 """ 2084 1985 2085 mset = self.mset 1986 2086 n = len(self.mset) … … 1995 2095 1996 2096 2097 class 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 1997 2151 class Arrangements_msetk(Permutations_msetk): 1998 2152 def __repr__(self): 1999 2153 """ 2000 2154 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 2160 class Arrangements_setk(Permutations_setk): 2161 def __repr__(self): 2162 """ 2163 TESTS: 2001 2164 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) 2005 2168 2006 2169 … … 2106 2269 [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]] 2107 2270 """ 2108 for p in Permutations_ mset(range(1,self.n+1)):2271 for p in Permutations_set(range(1,self.n+1)): 2109 2272 yield Permutation_class(p) 2110 2273 … … 3444 3607 3445 3608 sage: p = Permutations(['c', 'a', 't']); p 3446 Permutations of the (multi-)set ['c', 'a', 't']3609 Permutations of the set ['c', 'a', 't'] 3447 3610 sage: p.list() 3448 3611 [['c', 'a', 't'], … … 3454 3617 3455 3618 sage: p = Permutations(['c', 'a', 't'], 2); p 3456 Permutations of the (multi-)set ['c', 'a', 't'] of length 23619 Permutations of the set ['c', 'a', 't'] of length 2 3457 3620 sage: p.list() 3458 3621 [['c', 'a'], ['c', 't'], ['a', 'c'], ['a', 't'], ['t', 'c'], ['t', 'a']] … … 3460 3623 3461 3624 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] 3463 3626 sage: p.list() 3464 3627 [[1, 1, 2], [1, 2, 1], [2, 1, 1]] … … 3466 3629 3467 3630 sage: p = Permutations([1,1,2], 2); p 3468 Permutations of the (multi-)set [1, 1, 2] of length 23631 Permutations of the multi-set [1, 1, 2] of length 2 3469 3632 sage: p.list() 3470 3633 [[1, 1], [1, 2], [2, 1]] … … 3584 3747 return Permutations_nk(n,k) 3585 3748 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) 3588 3755 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) 3590 3760 elif 'descents' in kwargs: 3591 3761 if isinstance(kwargs['descents'], tuple):
Note: See TracChangeset
for help on using the changeset viewer.
