|
595 | 595 | PermutationQ[p_List] := (Sort[p] == Range[Length[p]]) |
596 | 596 |
|
597 | 597 | 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 *) |
598 | 601 |
|
599 | 602 | LexicographicPermutations[{l_}] := {{l}} |
600 | 603 |
|
|
616 | 619 | ] |
617 | 620 | ] |
618 | 621 |
|
| 622 | +(* Section 1.1.2 Ranking and Unranking Permutations, Pages 5-6 *) |
| 623 | + |
619 | 624 | RankPermutation[{1}] = 0 |
620 | 625 |
|
621 | 626 | RankPermutation[p_?PermutationQ] := (p[[1]]-1) (Length[Rest[p]]!) + |
|
635 | 640 | NextPermutation[p_?PermutationQ] := |
636 | 641 | NthPermutation[ RankPermutation[p]+1, Sort[p] ] |
637 | 642 |
|
| 643 | +(* Section 1.1.3 RandomPermutations, Pages 6-7 *) |
| 644 | + |
638 | 645 | RandomPermutation1[n_Integer?Positive] := |
639 | 646 | Map[ Last, Sort[ Map[({RandomInteger[],#})&,Range[n]] ] ] |
640 | 647 |
|
|
650 | 657 |
|
651 | 658 | RandomPermutation[n_Integer?Positive] := RandomPermutation1[n] |
652 | 659 |
|
| 660 | +(* Section 1.1.4 Permutation from Transpostions, Page 11 *) |
653 | 661 | MinimumChangePermutations[l_List] := |
654 | 662 | Module[{i=1,c,p=l,n=Length[l],k}, |
655 | 663 | c = Table[1,{n}]; |
|
667 | 675 | ] |
668 | 676 | ] |
669 | 677 |
|
| 678 | +(* Section 1.1.5 Backtracking and Distict Permutations, Page 12-13 *) |
670 | 679 | Backtrack[space_List,partialQ_,solutionQ_,flag_:One] := |
671 | 680 | Module[{n=Length[space],all={},done,index,v=2,solution}, |
672 | 681 | index=Prepend[ Table[0,{n-1}],1]; |
|
708 | 717 | ] |
709 | 718 | ] |
710 | 719 |
|
| 720 | +(* Section 1.1.6 Sorting and Searching, Page 14-16 *) |
| 721 | + |
711 | 722 | MinOp[l_List,f_] := |
712 | 723 | Module[{min=First[l]}, |
713 | 724 | Scan[ (If[ Apply[f,{#,min}], min = #])&, l]; |
|
738 | 749 | ] |
739 | 750 | ] |
740 | 751 |
|
| 752 | +(* Section 1.2.1 Multiplying Permutations, Page 17 *) |
741 | 753 | MultiplicationTable[elems_List,op_] := |
742 | 754 | Module[{i,j,n=Length[elems],p}, |
743 | 755 | Table[ |
|
747 | 759 | ] |
748 | 760 | ] |
749 | 761 |
|
| 762 | +(* Section 1.2.2 The Inverse of a Permutation, Page 18 *) |
750 | 763 | InversePermutation[p_?PermutationQ] := |
751 | 764 | Module[{inverse=p, i}, |
752 | 765 | Do[ inverse[[ p[[i]] ]] = i, {i,Length[p]} ]; |
753 | 766 | inverse |
754 | 767 | ] |
755 | 768 |
|
| 769 | +(* Section 1.2.3 The Equivalence Relation and Classesn, Page 18-19 *) |
756 | 770 | EquivalenceRelationQ[r_?SquareMatrixQ] := |
757 | 771 | ReflexiveQ[r] && SymmetricQ[r] && TransitiveQ[r] |
758 | 772 | EquivalenceRelationQ[g_Graph] := EquivalenceRelationQ[ Edges[g] ] |
|
784 | 798 | ] |
785 | 799 | ] /; perms != {} |
786 | 800 |
|
| 801 | +(* 1.2.4 The Cycle Structure of Permutations; Pages 20-21 *) |
787 | 802 | ToCycles[p1_?PermutationQ] := |
788 | 803 | Module[{p=p1,m,n,cycle,i}, |
789 | 804 | Select[ |
|
812 | 827 | p |
813 | 828 | ] |
814 | 829 |
|
| 830 | +(* 1.2.4 The Cycle Structure of Permutations, Hiding Cycles; Page 22 *) |
815 | 831 | HideCycles[c_List] := |
816 | 832 | Flatten[ |
817 | 833 | Sort[ |
|
832 | 848 | Append[cycles,Take[p,{start,end-1}]] |
833 | 849 | ] |
834 | 850 |
|
| 851 | +(* 1.2.4 The Cycle Structure of Permutations, Counting Cycles; Page 23 *) |
835 | 852 | NumberOfPermutationsByCycles[n_Integer,m_Integer] := (-1)^(n-m) StirlingS1[n,m] |
836 | 853 |
|
837 | 854 | StirlingFirst[n_Integer,m_Integer] := StirlingFirst1[n,m] |
|
1000 | 1017 | Join[ prev, Map[(Append[#,First[l]])&,Reverse[prev]] ] |
1001 | 1018 | ] |
1002 | 1019 |
|
| 1020 | +(* We have a builtin that does this. |
| 1021 | +GrayCode doesn't work? |
1003 | 1022 | Subsets[l_List] := GrayCode[l] |
1004 | 1023 | Subsets[n_Integer] := GrayCode[Range[n]] |
| 1024 | +*) |
1005 | 1025 |
|
1006 | 1026 | LexicographicSubsets[l_List] := LexicographicSubsets[l,{{}}] |
1007 | 1027 |
|
|
3131 | 3151 | (aj < Max[b]) |
3132 | 3152 | ] |
3133 | 3153 |
|
| 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 | + |
3134 | 3186 | End[] |
3135 | 3187 |
|
3136 | 3188 | Protect[ |
|
0 commit comments