77from mathics .core .evaluation import Evaluation
88import pytest
99
10-
11- def test_combinatorica ():
12- session .evaluate (
13- """
14- Needs["DiscreteMath`CombinatoricaV0.9`"]
10+ session .evaluate (
11+ """
12+ Needs["DiscreteMath`CombinatoricaV0.9`"]
1513 """
16- )
14+ )
1715
18- # A number of examples from Implementing Discrete Mathematics by
19- # Steven Skiena and
20- # A number of examples from Computation Discrete Mathematics by
21- # Sriram Pemmaraju and Steven Skiena.
16+ # A number of examples from Implementing Discrete Mathematics by
17+ # Steven Skiena and
18+ # A number of examples from Computation Discrete Mathematics by
19+ # Sriram Pemmaraju and Steven Skiena.
2220
23- # Page numbers below come from the first book
21+ # Page numbers below come from the first book
2422
2523
26- # Permutation[3] doesn't work
27- permutations3 = (
28- r"{{1, 2, 3}, {1, 3, 2}, {2, 1, 3}, {2, 3, 1}, {3, 1, 2}, {3, 2, 1}}"
29- )
24+ def test_combinatorica_permutations_1_1 ():
25+
3026 for str_expr , str_expected , message in (
3127 (
3228 "Permute[{a, b, c, d}, Range[4]]" ,
3329 "{a, b, c, d}" ,
34- "Permute list with simple list" ,
30+ "Permute list with simple list; 1.1 Page 3 " ,
3531 ),
3632 (
3733 "Permute[{a, b, c, d}, {1,2,2,4}]" ,
3834 "Permute[{a, b, c, d}, {1,2,2,4}]" ,
39- "Incorrect permute: index 2 duplicated" ,
40- ),
41- (
42- "Permute[{A, B, C, D}, %s]" % permutations3 ,
43- "{{A, B, C}, {A, C, B}, {B, A, C}, {B, C, A}, {C, A, B}, {C, B, A}}" ,
44- "Permute" ,
35+ "Incorrect permute: index 2 duplicated; 1.1 Page 3" ,
4536 ),
4637 (
4738 "LexicographicPermutations[{a,b,c,d}]" ,
@@ -53,74 +44,143 @@ def test_combinatorica():
5344 "{c, b, d, a}, {c, d, a, b}, {c, d, b, a}, "
5445 "{d, a, b, c}, {d, a, c, b}, {d, b, a, c}, "
5546 "{d, b, c, a}, {d, c, a, b}, {d, c, b, a}}" ,
56- "LexicographicPermuations, Page 4"
47+ "LexicographicPermuations, 1.1.1 Page 4" ,
48+ ),
49+ # NthPermutation does not work
50+ (
51+ "Map[RankPermutation, Permutations[Range[4]]]" ,
52+ "Range[0, 23]" ,
53+ "Permutations uses lexographic order; 1.1.2, Page 6" ,
5754 ),
58-
59- ("Map[RankPermutation, Permutations[Range[4]]]" ,
60- "Range[0, 23]" ,
61- "Permutations uses lexographic order"
62- ),
63-
64- ("RandomPermutation1[20] === RandomPermutation2[20]" ,
65- "False" ,
66- "Not likey two of 20! permutations will be the same (different routines), Page 7"
67- ),
68- ("RandomPermutation1[20] === RandomPermutation1[20]" ,
69- "False" ,
70- "Not likley two of 20! permutations will be the same (same routine)"
71- ),
72- ("RankPermutation[{8, 9, 7, 1, 6, 4, 5, 3, 2}]" , "321953" , "RankPermutation" ),
7355 (
74- "Permute[{5,2,4,3,1}, InversePermutation[{5,2,4,3,1}]]" ,
75- "{1, 2, 3, 4, 5}" ,
76- "InversePermute" ,
56+ "RandomPermutation1[20] === RandomPermutation2[20]" ,
57+ "False" ,
58+ "Not likey two of the 20! permutations will be the same, 1.1.3, Page 7" ,
59+ ),
60+ (
61+ "RandomPermutation1[20] === RandomPermutation1[20]" ,
62+ "False" ,
63+ "Not likley two of 20! permutations will be the same (same routine)" ,
7764 ),
7865 (
7966 "MinimumChangePermutations[{a,b,c}]" ,
8067 "{{a, b, c}, {b, a, c}, {c, a, b}, {a, c, b}, {b, c, a}, {c, b, a}}" ,
81- "MinimumChangePermuations, Page 11" ,
68+ "MinimumChangePermuations; 1.1.4 , Page 11" ,
8269 ),
8370 (
8471 "Union[Permutations[{a,a,a,a,a}]]" ,
8572 "{{a, a, a, a, a}}" ,
86- "simple but wasteful Permutation duplication elimination, Page 12"
73+ "simple but wasteful Permutation duplication elimination, 1.1.5, Page 12" ,
8774 ),
8875 (
8976 "DistinctPermutations[{1,1,2,2}]" ,
9077 "{{1, 1, 2, 2}, {1, 2, 1, 2}, {1, 2, 2, 1}, "
9178 "{2, 1, 1, 2}, {2, 1, 2, 1}, {2, 2, 1, 1}}" ,
92- "DisctinctPermutations of multiset Binomial[6,3] permutations, Page 14"
79+ "DisctinctPermutations of multiset Binomial[6,3] permutations, 1.1.5, Page 14" ,
9380 ),
81+ ("Multinomial[3,3]" , "20" , "The built-in function Multinomial, Page 14" ),
9482 (
95- "Multinomial[3,3]" ,
83+ "DistinctPermutations[{A,B,C}]" ,
84+ "{{A, B, C}, {A, C, B}, {B, A, C}, {B, C, A}, {C, A, B}, {C, B, A}}" ,
85+ "DisctinctPermutations all n! permutations, Page 14" ,
86+ ),
87+ (
88+ "BinarySearch[Table[2i,{i, 30}],40]" ,
9689 "20" ,
97- "The built-in function Multinomial , Page 14"
90+ "BinarySearch: 40 is one of the first 30 even numbers; 1.1.6 , Page 16" ,
9891 ),
9992 (
100- "DistinctPermutations[{A,B,C} ]" ,
101- "{{A, B, C}, {A, C, B}, {B, A, C}, {B, C, A}, {C, A, B}, {C, B, A}} " ,
102- "DisctinctPermutations all n! permutations , Page 14"
93+ "BinarySearch[Table[2i,{i, 30}],41 ]" ,
94+ "41/2 " ,
95+ "BinarySearch: BinarySearch: 41 is not even; 1.1.6 , Page 16" ,
10396 ),
10497 (
10598 "Sort[ Subsets [Range[4]],(Apply[Plus, #1]<=Apply[Plus,#2])& ]" ,
10699 "{{}, {1}, {2}, {3}, {1, 2}, {4}, {1, 3}, {1, 4}, {2, 3}, {2, 4}, "
107100 "{1, 2, 3}, {3, 4}, {1, 2, 4}, {1, 3, 4}, {2, 3, 4}, {1, 2, 3, 4}}" ,
108- "Sort to total order subsets, Page 15"
101+ "Sort to total order subsets, Page 15" ,
109102 ),
110103 (
111- "Subsets[Range[3] ]" ,
112- "{{}, {1}, {2}, {3}, { 1, 2}, {1, 3}, {2, 3}, {1, 2, 3} }" ,
113- "Subsets " ,
104+ "InversePermutation[{4,8,5,2,1,3,7,6} ]" ,
105+ "{5, 4, 6, 1, 3, 8, 7, 2 }" ,
106+ "InversePermutation: 7 is fixed point. Page 18 " ,
114107 ),
115108 (
116- "BinarySearch[Table[2i,{i, 30}],40]" ,
117- "20" ,
118- "BinarySearch: 40 is one of the first 30 even numbers, Page 16"
109+ "star = Automorphisms[Star[5]]" ,
110+ "{{1, 2, 3, 4, 5}, {1, 2, 4, 3, 5}, {1, 3, 2, 4, 5}, {1, 3, 4, 2, 5}, "
111+ "{1, 4, 2, 3, 5}, {1, 4, 3, 2, 5}, {2, 1, 3, 4, 5}, {2, 1, 4, 3, 5}, "
112+ "{2, 3, 1, 4, 5}, {2, 3, 4, 1, 5}, {2, 4, 1, 3, 5}, {2, 4, 3, 1, 5}, "
113+ "{3, 1, 2, 4, 5}, {3, 1, 4, 2, 5}, {3, 2, 1, 4, 5}, {3, 2, 4, 1, 5}, "
114+ "{3, 4, 1, 2, 5}, {3, 4, 2, 1, 5}, {4, 1, 2, 3, 5}, {4, 1, 3, 2, 5}, "
115+ "{4, 2, 1, 3, 5}, {4, 2, 3, 1, 5}, {4, 3, 1, 2, 5}, {4, 3, 2, 1, 5}}" ,
116+ "Automorphisms, Page 19" ,
119117 ),
118+ ):
119+ check_evaluation (str_expr , str_expected , message )
120+
121+
122+ def test_combinatorica_permutations_1_2 ():
123+
124+ for str_expr , str_expected , message in (
120125 (
121- "BinarySearch[Table[2i,{i, 30}],41]" ,
122- "41/2" ,
123- "BinarySearch: BinarySearch: 41 is not even"
126+ "MultiplicationTable[Permutations[Range[3]], Permute ]" ,
127+ "{{1, 2, 3, 4, 5, 6}, "
128+ "{2, 1, 5, 6, 3, 4}, "
129+ "{3, 4, 1, 2, 6, 5}, "
130+ "{4, 3, 6, 5, 1, 2}, "
131+ "{5, 6, 2, 1, 4, 3}, "
132+ "{6, 5, 4, 3, 2, 1}}" ,
133+ "Symmetric group S_n. S_n is not commutative. 1.2 Page 17"
134+ ),
135+ (
136+ "InversePermutation[{4,8,5,2,1,3,7,6}]" ,
137+ "{5, 4, 6, 1, 3, 8, 7, 2}" ,
138+ "InversePermutation: 7 is fixed point. 1.2 Page 18" ,
139+ ),
140+ (
141+ "star = Automorphisms[Star[5]]" ,
142+ "{{1, 2, 3, 4, 5}, {1, 2, 4, 3, 5}, {1, 3, 2, 4, 5}, {1, 3, 4, 2, 5}, "
143+ "{1, 4, 2, 3, 5}, {1, 4, 3, 2, 5}, {2, 1, 3, 4, 5}, {2, 1, 4, 3, 5}, "
144+ "{2, 3, 1, 4, 5}, {2, 3, 4, 1, 5}, {2, 4, 1, 3, 5}, {2, 4, 3, 1, 5}, "
145+ "{3, 1, 2, 4, 5}, {3, 1, 4, 2, 5}, {3, 2, 1, 4, 5}, {3, 2, 4, 1, 5}, "
146+ "{3, 4, 1, 2, 5}, {3, 4, 2, 1, 5}, {4, 1, 2, 3, 5}, {4, 1, 3, 2, 5}, "
147+ "{4, 2, 1, 3, 5}, {4, 2, 3, 1, 5}, {4, 3, 1, 2, 5}, {4, 3, 2, 1, 5}}" ,
148+ "Automorphisms, 1.2.3 Page 19" ,
149+ ),
150+ (
151+ "relation = SamenessRelation[star]" ,
152+ "{{1, 1, 1, 1, 0}, "
153+ "{1, 1, 1, 1, 0}, "
154+ "{1, 1, 1, 1, 0}, "
155+ "{1, 1, 1, 1, 0}, "
156+ "{0, 0, 0, 0, 1}}" ,
157+ "Sameness, 1.2.3 Page 19" ,
158+ ),
159+ # (
160+ # "PermutationGroupQ[Range[4], {4, 2, 3, 1}]",
161+ # "True",
162+ # "PermutationGroupQ, 1.2.3 Page 20",
163+ # ),
164+ ):
165+ check_evaluation (str_expr , str_expected , message )
166+
167+
168+ def test_combinatorica_rest ():
169+
170+ # Permutation[3] doesn't work
171+ permutations3 = (
172+ r"{{1, 2, 3}, {1, 3, 2}, {2, 1, 3}, {2, 3, 1}, {3, 1, 2}, {3, 2, 1}}"
173+ )
174+ for str_expr , str_expected , message in (
175+ (
176+ "Permute[{A, B, C, D}, %s]" % permutations3 ,
177+ "{{A, B, C}, {A, C, B}, {B, A, C}, {B, C, A}, {C, A, B}, {C, B, A}}" ,
178+ "Permute" ,
179+ ),
180+ (
181+ "Subsets[Range[3]]" ,
182+ "{{}, {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}}" ,
183+ "Subsets" ,
124184 ),
125185 (
126186 "BinarySearch[{2, 3, 9}, 7] // N" ,
@@ -153,45 +213,11 @@ def test_combinatorica():
153213 "2" ,
154214 "BinarySearch - find where key is a list" ,
155215 ),
156- # (
157- # "TableForm[ MultiplicationTable[Permutations[Range[3]], Permute ] ]",
158- # "1 2 3 4 5 6\n"
159- # "2 1 5 6 3 4\n"
160- # "3 4 1 2 6 5\n"
161- # "4 3 6 5 1 2\n"
162- # "5 6 2 1 4 3\n"
163- # "6 5 4 3 2 1\n",
164- # "Symmetric group S_n. S_n is not commutative. Page 17"
165- # ),
166216 (
167217 "InversePermutation[{4,8,5,2,1,3,7,6}]" ,
168218 "{5, 4, 6, 1, 3, 8, 7, 2}" ,
169219 "InversePermutation: 7 is fixed point. Page 18" ,
170220 ),
171- (
172- "star = Automorphisms[Star[5]]" ,
173- "{{1, 2, 3, 4, 5}, {1, 2, 4, 3, 5}, {1, 3, 2, 4, 5}, {1, 3, 4, 2, 5}, "
174- "{1, 4, 2, 3, 5}, {1, 4, 3, 2, 5}, {2, 1, 3, 4, 5}, {2, 1, 4, 3, 5}, "
175- "{2, 3, 1, 4, 5}, {2, 3, 4, 1, 5}, {2, 4, 1, 3, 5}, {2, 4, 3, 1, 5}, "
176- "{3, 1, 2, 4, 5}, {3, 1, 4, 2, 5}, {3, 2, 1, 4, 5}, {3, 2, 4, 1, 5}, "
177- "{3, 4, 1, 2, 5}, {3, 4, 2, 1, 5}, {4, 1, 2, 3, 5}, {4, 1, 3, 2, 5}, "
178- "{4, 2, 1, 3, 5}, {4, 2, 3, 1, 5}, {4, 3, 1, 2, 5}, {4, 3, 2, 1, 5}}" ,
179- "Automorphisms, Page 19"
180- ),
181- (
182- "KSubsets[Range[5], 3]" ,
183- "{{1, 2, 3}, {1, 2, 4}, {1, 2, 5}, {1, 3, 4}, {1, 3, 5}, {1, 4, 5}, "
184- "{2, 3, 4}, {2, 3, 5}, {2, 4, 5}, {3, 4, 5}}" ,
185- "Ksubsets" ,
186- ),
187- (
188- "Compositions[5,3]" ,
189- "{{0, 0, 5}, {0, 1, 4}, {0, 2, 3}, {0, 3, 2}, {0, 4, 1}, {0, 5, 0}, "
190- "{1, 0, 4}, {1, 1, 3}, {1, 2, 2}, {1, 3, 1}, {1, 4, 0}, {2, 0, 3}, "
191- "{2, 1, 2}, {2, 2, 1}, {2, 3, 0}, {3, 0, 2}, {3, 1, 1}, {3, 2, 0}, "
192- "{4, 0, 1}, {4, 1, 0}, {5, 0, 0}}" ,
193- "Compositions" ,
194- ),
195221 # (
196222 # "SetPartitions[3]",
197223 # "{{{1, 2, 3}}, {{1}, {2, 3}}, {{1, 2}, {3}}, {{1, 3}, {2}}, {{1}, {2}, {3}}}",
@@ -200,7 +226,7 @@ def test_combinatorica():
200226 (
201227 "TransposePartition[{8, 6, 4, 4, 3, 1}]" ,
202228 "{6, 5, 5, 4, 2, 2, 1, 1}" ,
203- "TransposePartition"
229+ "TransposePartition" ,
204230 ),
205231 ):
206232 check_evaluation (str_expr , str_expected , message )
0 commit comments