Skip to content

Commit 5a90661

Browse files
authored
Merge pull request #1049 from mathics/combinatorica-use-v9
Combinatorica use v9
2 parents e32dc23 + 0b7bcc4 commit 5a90661

4 files changed

Lines changed: 276 additions & 103 deletions

File tree

mathics/packages/DiscreteMath/CombinatoricaLite.m

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,30 @@
2525
authors, Wolfram Research, or Cambridge University Press, their licensees,
2626
distributors and dealers shall in no event be liable for any indirect,
2727
incidental, or consequential damages.
28+
*)
2829

30+
(* :History:
31+
Version 2.1 updated to Mathematica 6 by John M. Novak, 2006.
32+
Version 2.0 most code rewritten Sriram V. Pemmaraju, 2000-2002
33+
Too many changes to describe here. Read the book!
34+
Version 1.1 modification by ECM, March 1996.
35+
Replaced K with CompleteGraph because K is now the
36+
default generic name for the summation index in
37+
symbolic sum.
38+
Added CombinatorialFunctions.m and Permutations.m to
39+
BeginPackage, and commented out CatalanNumber,
40+
PermutationQ, ToCycles, FromCycles, and
41+
RandomPermutation so there would be no shadowing of
42+
symbols among the DiscreteMath packages.
43+
Replaced old BinarySearch with new code by Paul Abbott
44+
correctly implementing binary search.
45+
Version 1.0 by Steven S. Skiena, April 1995.
46+
Version .9 by Steven S. Skiena, February 1992.
47+
Version .8 by Steven S. Skiena, July 1991.
48+
Version .7 by Steven S. Skiena, January 1991.
49+
Version .6 by Steven S. Skiena, June 1990.
50+
*)
51+
(*
2952
And for the 0.6 version:
3053
Version 0.6 6/11/90 Beta Release
3154
Copyright (c) 1990 by Steven S. Skiena
@@ -284,6 +307,8 @@
284307
]
285308
*)
286309

310+
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."
311+
287312
SetPartitions[{}] := {{}}
288313
SetPartitions[s_List] := Flatten[Table[KSetPartitions[s, i], {i, Length[s]}], 1]
289314

mathics/packages/DiscreteMath/CombinatoricaV0.9.m

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,9 @@
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]])
599+
600+
(* Section 1.1.1 Lexicographically Ordered Permutions, Pages 3-4 *)
598601

599602
LexicographicPermutations[{l_}] := {{l}}
600603

@@ -616,6 +619,8 @@
616619
]
617620
]
618621

622+
(* Section 1.1.2 Ranking and Unranking Permutations, Pages 5-6 *)
623+
619624
RankPermutation[{1}] = 0
620625

621626
RankPermutation[p_?PermutationQ] := (p[[1]]-1) (Length[Rest[p]]!) +
@@ -635,6 +640,8 @@
635640
NextPermutation[p_?PermutationQ] :=
636641
NthPermutation[ RankPermutation[p]+1, Sort[p] ]
637642

643+
(* Section 1.1.3 RandomPermutations, Pages 6-7 *)
644+
638645
RandomPermutation1[n_Integer?Positive] :=
639646
Map[ Last, Sort[ Map[({RandomInteger[],#})&,Range[n]] ] ]
640647

@@ -650,6 +657,7 @@
650657

651658
RandomPermutation[n_Integer?Positive] := RandomPermutation1[n]
652659

660+
(* Section 1.1.4 Permutation from Transpostions, Page 11 *)
653661
MinimumChangePermutations[l_List] :=
654662
Module[{i=1,c,p=l,n=Length[l],k},
655663
c = Table[1,{n}];
@@ -667,6 +675,7 @@
667675
]
668676
]
669677

678+
(* Section 1.1.5 Backtracking and Distict Permutations, Page 12-13 *)
670679
Backtrack[space_List,partialQ_,solutionQ_,flag_:One] :=
671680
Module[{n=Length[space],all={},done,index,v=2,solution},
672681
index=Prepend[ Table[0,{n-1}],1];
@@ -708,6 +717,8 @@
708717
]
709718
]
710719

720+
(* Section 1.1.6 Sorting and Searching, Page 14-16 *)
721+
711722
MinOp[l_List,f_] :=
712723
Module[{min=First[l]},
713724
Scan[ (If[ Apply[f,{#,min}], min = #])&, l];
@@ -738,6 +749,7 @@
738749
]
739750
]
740751

752+
(* Section 1.2.1 Multiplying Permutations, Page 17 *)
741753
MultiplicationTable[elems_List,op_] :=
742754
Module[{i,j,n=Length[elems],p},
743755
Table[
@@ -747,12 +759,14 @@
747759
]
748760
]
749761

762+
(* Section 1.2.2 The Inverse of a Permutation, Page 18 *)
750763
InversePermutation[p_?PermutationQ] :=
751764
Module[{inverse=p, i},
752765
Do[ inverse[[ p[[i]] ]] = i, {i,Length[p]} ];
753766
inverse
754767
]
755768

769+
(* Section 1.2.3 The Equivalence Relation and Classesn, Page 18-19 *)
756770
EquivalenceRelationQ[r_?SquareMatrixQ] :=
757771
ReflexiveQ[r] && SymmetricQ[r] && TransitiveQ[r]
758772
EquivalenceRelationQ[g_Graph] := EquivalenceRelationQ[ Edges[g] ]
@@ -784,6 +798,7 @@
784798
]
785799
] /; perms != {}
786800

801+
(* 1.2.4 The Cycle Structure of Permutations; Pages 20-21 *)
787802
ToCycles[p1_?PermutationQ] :=
788803
Module[{p=p1,m,n,cycle,i},
789804
Select[
@@ -812,6 +827,7 @@
812827
p
813828
]
814829

830+
(* 1.2.4 The Cycle Structure of Permutations, Hiding Cycles; Page 22 *)
815831
HideCycles[c_List] :=
816832
Flatten[
817833
Sort[
@@ -832,6 +848,7 @@
832848
Append[cycles,Take[p,{start,end-1}]]
833849
]
834850

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

837854
StirlingFirst[n_Integer,m_Integer] := StirlingFirst1[n,m]
@@ -1000,8 +1017,11 @@
10001017
Join[ prev, Map[(Append[#,First[l]])&,Reverse[prev]] ]
10011018
]
10021019

1020+
(* We have a builtin that does this.
1021+
GrayCode doesn't work?
10031022
Subsets[l_List] := GrayCode[l]
10041023
Subsets[n_Integer] := GrayCode[Range[n]]
1024+
*)
10051025

10061026
LexicographicSubsets[l_List] := LexicographicSubsets[l,{{}}]
10071027

@@ -3131,6 +3151,38 @@
31313151
(aj < Max[b])
31323152
]
31333153

3154+
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."
3155+
KSetPartitions[{}, 0] := {{}}
3156+
KSetPartitions[s_List, 0] := {}
3157+
KSetPartitions[s_List, k_Integer] := {} /; (k > Length[s])
3158+
KSetPartitions[s_List, k_Integer] := {Map[{#} &, s]} /; (k === Length[s])
3159+
KSetPartitions[s_List, k_Integer] :=
3160+
Block[{$RecursionLimit = Infinity},
3161+
Join[Map[Prepend[#, {First[s]}] &, KSetPartitions[Rest[s], k - 1]],
3162+
Flatten[
3163+
Map[Table[Prepend[Delete[#, j], Prepend[#[[j]], s[[1]]]],
3164+
{j, Length[#]}
3165+
]&,
3166+
KSetPartitions[Rest[s], k]
3167+
], 1
3168+
]
3169+
]
3170+
] /; (k > 0) && (k < Length[s])
3171+
3172+
KSetPartitions[0, 0] := {{}}
3173+
KSetPartitions[0, k_Integer?Positive] := {}
3174+
KSetPartitions[n_Integer?Positive, 0] := {}
3175+
KSetPartitions[n_Integer?Positive, k_Integer?Positive] := KSetPartitions[Range[n], k]
3176+
3177+
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."
3178+
3179+
SetPartitions[{}] := {{}}
3180+
SetPartitions[s_List] := Flatten[Table[KSetPartitions[s, i], {i, Length[s]}], 1]
3181+
3182+
SetPartitions[0] := {{}}
3183+
SetPartitions[n_Integer?Positive] := SetPartitions[Range[n]]
3184+
3185+
31343186
End[]
31353187

31363188
Protect[

0 commit comments

Comments
 (0)