]> gitweb.factorcode.org Git - factor.git/blob - basis/math/combinatorics/combinatorics-docs.factor
Delete empty unit tests files, remove 1- and 1+, reorder IN: lines in a lot of places...
[factor.git] / basis / math / combinatorics / combinatorics-docs.factor
1 USING: help.markup help.syntax kernel math math.order multiline sequences ;
2 IN: math.combinatorics
3
4 HELP: factorial
5 { $values { "n" "a non-negative integer" } { "n!" integer } }
6 { $description "Outputs the product of all positive integers less than or equal to " { $snippet "n" } "." }
7 { $examples 
8     { $example "USING: math.combinatorics prettyprint ;"
9         "4 factorial ." "24" }
10 } ;
11
12 HELP: nPk
13 { $values { "n" "a non-negative integer" } { "k" "a non-negative integer" } { "nPk" integer } }
14 { $description "Outputs the total number of unique permutations of size " { $snippet "k" } " (order does matter) that can be taken from a set of size " { $snippet "n" } "." }
15 { $examples
16     { $example "USING: math.combinatorics prettyprint ;"
17         "10 4 nPk ." "5040" }
18 } ;
19
20 HELP: nCk
21 { $values { "n" "a non-negative integer" } { "k" "a non-negative integer" } { "nCk" integer } }
22 { $description "Outputs the total number of unique combinations of size " { $snippet "k" } " (order does not matter) that can be taken from a set of size " { $snippet "n" } ". Commonly written as \"n choose k\"." }
23 { $examples
24     { $example "USING: math.combinatorics prettyprint ;"
25         "10 4 nCk ." "210" }
26 } ;
27
28 HELP: permutation
29 { $values { "n" "a non-negative integer" } { "seq" sequence } { "seq" sequence } }
30 { $description "Outputs the " { $snippet "nth" } " lexicographical permutation of " { $snippet "seq" } "." }
31 { $notes "Permutations are 0-based and a bounds error will be thrown if " { $snippet "n" } " is larger than " { $snippet "seq length factorial 1 -" } "." }
32 { $examples
33     { $example "USING: math.combinatorics prettyprint ;"
34         "1 3 permutation ." "{ 0 2 1 }" }
35     { $example "USING: math.combinatorics prettyprint ;"
36         "5 { \"apple\" \"banana\" \"orange\" } permutation ." "{ \"orange\" \"banana\" \"apple\" }" }
37 } ;
38
39 HELP: all-permutations
40 { $values { "seq" sequence } { "seq" sequence } }
41 { $description "Outputs a sequence containing all permutations of " { $snippet "seq" } " in lexicographical order." }
42 { $examples
43     { $example "USING: math.combinatorics prettyprint ;"
44         "3 all-permutations ." "{ { 0 1 2 } { 0 2 1 } { 1 0 2 } { 1 2 0 } { 2 0 1 } { 2 1 0 } }" }
45 } ;
46
47 HELP: each-permutation
48 { $values { "seq" sequence } { "quot" { $quotation "( seq -- )" } } }
49 { $description "Applies the quotation to each permuation of " { $snippet "seq" } " in order." } ;
50
51 HELP: inverse-permutation
52 { $values { "seq" sequence } { "permutation" sequence } }
53 { $description "Outputs a sequence of indices representing the lexicographical permutation of " { $snippet "seq" } "." }
54 { $notes "All items in " { $snippet "seq" } " must be comparable by " { $link <=> } "." }
55 { $examples
56     { $example "USING: math.combinatorics prettyprint ;"
57         "\"dcba\" inverse-permutation ." "{ 3 2 1 0 }" }
58     { $example "USING: math.combinatorics prettyprint ;"
59         "{ 12 56 34 78 } inverse-permutation ." "{ 0 2 1 3 }" }
60 } ;
61
62 HELP: combination
63 { $values { "m" "a non-negative integer" } { "seq" sequence } { "k" "a non-negative integer" } { "seq" sequence } }
64 { $description "Outputs the " { $snippet "mth" } " lexicographical combination of " { $snippet "seq" } " choosing " { $snippet "k" } " elements." }
65 { $notes "Combinations are 0-based and a bounds error will be thrown if " { $snippet "m" } " is larger than " { $snippet "seq length k nCk" } "." }
66 { $examples
67     { $example "USING: math.combinatorics sequences prettyprint ;"
68         "6 7 iota 4 combination ." "{ 0 1 3 6 }" }
69     { $example "USING: math.combinatorics prettyprint ;"
70         "0 { \"a\" \"b\" \"c\" \"d\" } 2 combination ." "{ \"a\" \"b\" }" }
71 } ;
72
73 HELP: all-combinations
74 { $values { "seq" sequence } { "k" "a non-negative integer" } { "seq" sequence } }
75 { $description "Outputs a sequence containing all combinations of " { $snippet "seq" } " choosing " { $snippet "k" } " elements, in lexicographical order." }
76 { $examples
77     { $example "USING: math.combinatorics prettyprint ;"
78         "{ \"a\" \"b\" \"c\" \"d\" } 2 all-combinations ."
79 <" {
80     { "a" "b" }
81     { "a" "c" }
82     { "a" "d" }
83     { "b" "c" }
84     { "b" "d" }
85     { "c" "d" }
86 }"> } } ;
87
88 HELP: each-combination
89 { $values { "seq" sequence } { "k" "a non-negative integer" } { "quot" { $quotation "( seq -- )" } } }
90 { $description "Applies the quotation to each combination of " { $snippet "seq" } " choosing " { $snippet "k" } " elements, in order." } ;
91
92
93 IN: math.combinatorics.private
94
95 HELP: factoradic
96 { $values { "n" integer } { "factoradic" sequence } }
97 { $description "Converts a positive integer " { $snippet "n" } " to factoradic form.  The factoradic of an integer is its representation based on a mixed radix numerical system that corresponds to the values of " { $snippet "n" } " factorial." }
98 { $examples { $example "USING: math.combinatorics.private  prettyprint ;" "859 factoradic ." "{ 1 1 0 3 0 1 0 }" } } ;
99
100 HELP: >permutation
101 { $values { "factoradic" sequence } { "permutation" sequence } }
102 { $description "Converts an integer represented in factoradic form into its corresponding unique permutation (0-based)." }
103 { $notes "For clarification, the following two statements are equivalent:" { $code "10 factoradic >permutation" "{ 1 2 0 0 } >permutation" } }
104 { $examples { $example "USING: math.combinatorics.private prettyprint ;" "{ 0 0 0 0 } >permutation ." "{ 0 1 2 3 }" } } ;
105