Skip to content

Commit 8239336

Browse files
committed
Use V0.9 in testing. It seems more complete
1 parent 3373ec9 commit 8239336

3 files changed

Lines changed: 54 additions & 6 deletions

File tree

mathics/packages/DiscreteMath/CombinatoricaLite.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,8 @@
284284
]
285285
*)
286286

287+
SetPartitions::usage = "SetPartitions[set] returns the list of set partitions of set. SetPartitions[n] returns the list of set partitions of {1, 2, ..., n}. If all set partitions with a fixed number of subsets are needed use KSetPartitions."
288+
287289
SetPartitions[{}] := {{}}
288290
SetPartitions[s_List] := Flatten[Table[KSetPartitions[s, i], {i, Length[s]}], 1]
289291

mathics/packages/DiscreteMath/CombinatoricaV0.9.m

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,7 @@
595595
PermutationQ[p_List] := (Sort[p] == Range[Length[p]])
596596

597597
Permute[l_List,p_?PermutationQ] := l [[ p ]]
598+
Permute[l_List,p_List] := Map[ (Permute[l,#])&, p] /; (Apply[And, Map[PermutationQ, p]])
598599

599600
LexicographicPermutations[{l_}] := {{l}}
600601

@@ -1000,8 +1001,11 @@
10001001
Join[ prev, Map[(Append[#,First[l]])&,Reverse[prev]] ]
10011002
]
10021003

1004+
(* We have a builtin that does this.
1005+
GrayCode doesn't work?
10031006
Subsets[l_List] := GrayCode[l]
10041007
Subsets[n_Integer] := GrayCode[Range[n]]
1008+
*)
10051009

10061010
LexicographicSubsets[l_List] := LexicographicSubsets[l,{{}}]
10071011

@@ -3131,6 +3135,38 @@
31313135
(aj < Max[b])
31323136
]
31333137

3138+
KSetPartitions::usage = "KSetPartitions[set, k] returns the list of set partitions of set with k blocks. KSetPartitions[n, k] returns the list of set partitions of {1, 2, ..., n} with k blocks. If all set partitions of a set are needed, use the function SetPartitions."
3139+
KSetPartitions[{}, 0] := {{}}
3140+
KSetPartitions[s_List, 0] := {}
3141+
KSetPartitions[s_List, k_Integer] := {} /; (k > Length[s])
3142+
KSetPartitions[s_List, k_Integer] := {Map[{#} &, s]} /; (k === Length[s])
3143+
KSetPartitions[s_List, k_Integer] :=
3144+
Block[{$RecursionLimit = Infinity},
3145+
Join[Map[Prepend[#, {First[s]}] &, KSetPartitions[Rest[s], k - 1]],
3146+
Flatten[
3147+
Map[Table[Prepend[Delete[#, j], Prepend[#[[j]], s[[1]]]],
3148+
{j, Length[#]}
3149+
]&,
3150+
KSetPartitions[Rest[s], k]
3151+
], 1
3152+
]
3153+
]
3154+
] /; (k > 0) && (k < Length[s])
3155+
3156+
KSetPartitions[0, 0] := {{}}
3157+
KSetPartitions[0, k_Integer?Positive] := {}
3158+
KSetPartitions[n_Integer?Positive, 0] := {}
3159+
KSetPartitions[n_Integer?Positive, k_Integer?Positive] := KSetPartitions[Range[n], k]
3160+
3161+
SetPartitions::usage = "SetPartitions[set] returns the list of set partitions of set. SetPartitions[n] returns the list of set partitions of {1, 2, ..., n}. If all set partitions with a fixed number of subsets are needed use KSetPartitions."
3162+
3163+
SetPartitions[{}] := {{}}
3164+
SetPartitions[s_List] := Flatten[Table[KSetPartitions[s, i], {i, Length[s]}], 1]
3165+
3166+
SetPartitions[0] := {{}}
3167+
SetPartitions[n_Integer?Positive] := SetPartitions[Range[n]]
3168+
3169+
31343170
End[]
31353171

31363172
Protect[

test/test_combinatorica.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
def test_combinatorica():
1212
session.evaluate(
1313
"""
14-
Needs["DiscreteMath`CombinatoricaLite`"]
14+
Needs["DiscreteMath`CombinatoricaV0.9`"]
1515
"""
1616
)
1717

@@ -168,6 +168,16 @@ def test_combinatorica():
168168
"{5, 4, 6, 1, 3, 8, 7, 2}",
169169
"InversePermutation: 7 is fixed point. Page 18",
170170
),
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+
),
171181
(
172182
"KSubsets[Range[5], 3]",
173183
"{{1, 2, 3}, {1, 2, 4}, {1, 2, 5}, {1, 3, 4}, {1, 3, 5}, {1, 4, 5}, "
@@ -182,11 +192,11 @@ def test_combinatorica():
182192
"{4, 0, 1}, {4, 1, 0}, {5, 0, 0}}",
183193
"Compositions",
184194
),
185-
(
186-
"SetPartitions[3]",
187-
"{{{1, 2, 3}}, {{1}, {2, 3}}, {{1, 2}, {3}}, {{1, 3}, {2}}, {{1}, {2}, {3}}}",
188-
"SetPartitions"
189-
),
195+
# (
196+
# "SetPartitions[3]",
197+
# "{{{1, 2, 3}}, {{1}, {2, 3}}, {{1, 2}, {3}}, {{1, 3}, {2}}, {{1}, {2}, {3}}}",
198+
# "SetPartitions"
199+
# ),
190200
(
191201
"TransposePartition[{8, 6, 4, 4, 3, 1}]",
192202
"{6, 5, 5, 4, 2, 2, 1, 1}",

0 commit comments

Comments
 (0)