Skip to content

Commit 1a47243

Browse files
committed
Progress to page 23
1 parent d436c1d commit 1a47243

2 files changed

Lines changed: 71 additions & 24 deletions

File tree

mathics/packages/DiscreteMath/CombinatoricaV0.9.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,7 @@
766766
inverse
767767
]
768768

769+
(* Section 1.2.3 The Equivalence Relation and Classesn, Page 18-19 *)
769770
EquivalenceRelationQ[r_?SquareMatrixQ] :=
770771
ReflexiveQ[r] && SymmetricQ[r] && TransitiveQ[r]
771772
EquivalenceRelationQ[g_Graph] := EquivalenceRelationQ[ Edges[g] ]
@@ -797,6 +798,7 @@
797798
]
798799
] /; perms != {}
799800

801+
(* 1.2.4 The Cycle Structure of Permutations; Pages 20-21 *)
800802
ToCycles[p1_?PermutationQ] :=
801803
Module[{p=p1,m,n,cycle,i},
802804
Select[
@@ -825,6 +827,7 @@
825827
p
826828
]
827829

830+
(* 1.2.4 The Cycle Structure of Permutations, Hiding Cycles; Page 22 *)
828831
HideCycles[c_List] :=
829832
Flatten[
830833
Sort[
@@ -845,6 +848,7 @@
845848
Append[cycles,Take[p,{start,end-1}]]
846849
]
847850

851+
(* 1.2.4 The Cycle Structure of Permutations, Counting Cycles; Page 23 *)
848852
NumberOfPermutationsByCycles[n_Integer,m_Integer] := (-1)^(n-m) StirlingS1[n,m]
849853

850854
StirlingFirst[n_Integer,m_Integer] := StirlingFirst1[n,m]

test/test_combinatorica.py

Lines changed: 67 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ def test_combinatorica_permutations_1_1():
3737
(
3838
"LexicographicPermutations[{a,b,c,d}]",
3939
"{{a, b, c, d}, {a, b, d, c}, {a, c, b, d}, "
40-
"{a, c, d, b}, {a, d, b, c}, {a, d, c, b}, "
41-
"{b, a, c, d}, {b, a, d, c}, {b, c, a, d}, "
42-
"{b, c, d, a}, {b, d, a, c}, {b, d, c, a}, "
43-
"{c, a, b, d}, {c, a, d, b}, {c, b, a, d}, "
44-
"{c, b, d, a}, {c, d, a, b}, {c, d, b, a}, "
45-
"{d, a, b, c}, {d, a, c, b}, {d, b, a, c}, "
46-
"{d, b, c, a}, {d, c, a, b}, {d, c, b, a}}",
40+
" {a, c, d, b}, {a, d, b, c}, {a, d, c, b}, "
41+
" {b, a, c, d}, {b, a, d, c}, {b, c, a, d}, "
42+
" {b, c, d, a}, {b, d, a, c}, {b, d, c, a}, "
43+
" {c, a, b, d}, {c, a, d, b}, {c, b, a, d}, "
44+
" {c, b, d, a}, {c, d, a, b}, {c, d, b, a}, "
45+
" {d, a, b, c}, {d, a, c, b}, {d, b, a, c}, "
46+
" {d, b, c, a}, {d, c, a, b}, {d, c, b, a}}",
4747
"LexicographicPermuations, 1.1.1 Page 4",
4848
),
4949
# NthPermutation does not work
@@ -75,7 +75,7 @@ def test_combinatorica_permutations_1_1():
7575
(
7676
"DistinctPermutations[{1,1,2,2}]",
7777
"{{1, 1, 2, 2}, {1, 2, 1, 2}, {1, 2, 2, 1}, "
78-
"{2, 1, 1, 2}, {2, 1, 2, 1}, {2, 2, 1, 1}}",
78+
" {2, 1, 1, 2}, {2, 1, 2, 1}, {2, 2, 1, 1}}",
7979
"DisctinctPermutations of multiset Binomial[6,3] permutations, 1.1.5, Page 14",
8080
),
8181
("Multinomial[3,3]", "20", "The built-in function Multinomial, Page 14"),
@@ -96,8 +96,10 @@ def test_combinatorica_permutations_1_1():
9696
),
9797
(
9898
"Sort[ Subsets [Range[4]],(Apply[Plus, #1]<=Apply[Plus,#2])& ]",
99-
"{{}, {1}, {2}, {3}, {1, 2}, {4}, {1, 3}, {1, 4}, {2, 3}, {2, 4}, "
100-
"{1, 2, 3}, {3, 4}, {1, 2, 4}, {1, 3, 4}, {2, 3, 4}, {1, 2, 3, 4}}",
99+
"{{}, {1}, {2}, {3}, {1, 2}, {4}, "
100+
" {1, 3}, {1, 4}, {2, 3}, {2, 4}, "
101+
" {1, 2, 3}, {3, 4}, {1, 2, 4}, {1, 3, 4}, {2, 3, 4}, "
102+
" {1, 2, 3, 4}}",
101103
"Sort to total order subsets, Page 15",
102104
),
103105
):
@@ -110,11 +112,11 @@ def test_combinatorica_permutations_1_2():
110112
(
111113
"MultiplicationTable[Permutations[Range[3]], Permute ]",
112114
"{{1, 2, 3, 4, 5, 6}, "
113-
"{2, 1, 5, 6, 3, 4}, "
114-
"{3, 4, 1, 2, 6, 5}, "
115-
"{4, 3, 6, 5, 1, 2}, "
116-
"{5, 6, 2, 1, 4, 3}, "
117-
"{6, 5, 4, 3, 2, 1}}",
115+
" {2, 1, 5, 6, 3, 4}, "
116+
" {3, 4, 1, 2, 6, 5}, "
117+
" {4, 3, 6, 5, 1, 2}, "
118+
" {5, 6, 2, 1, 4, 3}, "
119+
" {6, 5, 4, 3, 2, 1}}",
118120
"Symmetric group S_n. S_n is not commutative. 1.2 Page 17"
119121
),
120122
(
@@ -135,17 +137,63 @@ def test_combinatorica_permutations_1_2():
135137
(
136138
"relation = SamenessRelation[star]",
137139
"{{1, 1, 1, 1, 0}, "
138-
"{1, 1, 1, 1, 0}, "
139-
"{1, 1, 1, 1, 0}, "
140-
"{1, 1, 1, 1, 0}, "
141-
"{0, 0, 0, 0, 1}}",
140+
" {1, 1, 1, 1, 0}, "
141+
" {1, 1, 1, 1, 0}, "
142+
" {1, 1, 1, 1, 0}, "
143+
" {0, 0, 0, 0, 1}}",
142144
"Sameness, 1.2.3 Page 19",
143145
),
146+
(
147+
"EquivalenceClasses[relation]",
148+
"{{1, 2, 3, 4}, {5}}",
149+
"EquivalenceClasses, 1.2.3, Page 19"
150+
),
144151
# (
145152
# "PermutationGroupQ[Range[4], {4, 2, 3, 1}]",
146153
# "True",
147154
# "PermutationGroupQ, 1.2.3 Page 20",
148155
# ),
156+
(
157+
"ToCycles[Range[10]]",
158+
"{{1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}}",
159+
"ToCycles, 1.2.4, Page 21"
160+
),
161+
(
162+
"Select[ Permutations[Range[4]], (Length[ToCycles[#]] == 1)&]",
163+
"{{2, 3, 4, 1}, {2, 4, 1, 3}, {3, 1, 4, 2}, "
164+
" {3, 4, 2, 1}, {4, 1, 2, 3}, {4, 3, 1, 2}}",
165+
"ToCycles, 1.2.4, Page 21"
166+
),
167+
(
168+
"ToCycles[ Reverse[Range[10]] ]",
169+
"{{10, 1}, {9, 2}, {8, 3}, {7, 4}, {6, 5}}",
170+
"Reverse ToCycles, 1.2.4, Page 21"
171+
),
172+
(
173+
"Permute[ Reverse[Range[10]], Reverse[Range[10]] ]",
174+
"Range[10]",
175+
"Pemute as involution, 1.2.4, Page 21"
176+
),
177+
(
178+
"Apply[ And, List[p=RandomPermutation[8]; p===FromCycles[ToCycles[p]]] ]",
179+
"True",
180+
"Convert to-and-from cycle structure is identity, 1.2.4, Page 22"
181+
),
182+
(
183+
"Apply[ And, List[p=RandomPermutation[8]; p===FromCycles[ToCycles[p]]] ]",
184+
"True",
185+
"Convert to-and-from cycle structure is identity, 1.2.4, Page 22"
186+
),
187+
(
188+
"ToCycles[{6,2,1,5,4,3} ]",
189+
"{{6, 3, 1}, {2}, {5, 4}}",
190+
"Three permutations, one of each size, 1.2.4, Page 22"
191+
),
192+
(
193+
"HideCycles[ToCycles[{6,2,1,5,4,3}]]",
194+
"{4, 5, 2, 1, 6, 3}",
195+
"Permutations is not what we started with, 1.2.4, Page 23"
196+
),
149197
):
150198
check_evaluation(str_expr, str_expected, message)
151199

@@ -198,11 +246,6 @@ def test_combinatorica_rest():
198246
"2",
199247
"BinarySearch - find where key is a list",
200248
),
201-
(
202-
"InversePermutation[{4,8,5,2,1,3,7,6}]",
203-
"{5, 4, 6, 1, 3, 8, 7, 2}",
204-
"InversePermutation: 7 is fixed point. Page 18",
205-
),
206249
# (
207250
# "SetPartitions[3]",
208251
# "{{{1, 2, 3}}, {{1}, {2, 3}}, {{1, 2}, {3}}, {{1, 3}, {2}}, {{1}, {2}, {3}}}",

0 commit comments

Comments
 (0)