]> gitweb.factorcode.org Git - factor.git/blob - basis/math/combinatorics/combinatorics-docs.factor
Merge branch 'master' of git://factorcode.org/git/factor
[factor.git] / basis / math / combinatorics / combinatorics-docs.factor
1 USING: help.markup help.syntax kernel math math.order 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 { $example "USING: math.combinatorics prettyprint ;" "4 factorial ." "24" } } ;
8
9 HELP: nPk
10 { $values { "n" "a non-negative integer" } { "k" "a non-negative integer" } { "nPk" integer } }
11 { $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" } "." }
12 { $examples { $example "USING: math.combinatorics prettyprint ;" "10 4 nPk ." "5040" } } ;
13
14 HELP: nCk
15 { $values { "n" "a non-negative integer" } { "k" "a non-negative integer" } { "nCk" integer } }
16 { $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\"." }
17 { $examples { $example "USING: math.combinatorics prettyprint ;" "10 4 nCk ." "210" } } ;
18
19 HELP: permutation
20 { $values { "n" "a non-negative integer" } { "seq" sequence } { "seq" sequence } }
21 { $description "Outputs the " { $snippet "nth" } " lexicographical permutation of " { $snippet "seq" } "." }
22 { $notes "Permutations are 0-based and a bounds error will be thrown if " { $snippet "n" } " is larger than " { $snippet "seq length factorial 1-" } "." }
23 { $examples { $example "USING: math.combinatorics prettyprint ;" "1 3 permutation ." "{ 0 2 1 }" } { $example "USING: math.combinatorics prettyprint ;" "5 { \"apple\" \"banana\" \"orange\" } permutation ." "{ \"orange\" \"banana\" \"apple\" }" } } ;
24
25 HELP: all-permutations
26 { $values { "seq" sequence } { "seq" sequence } }
27 { $description "Outputs a sequence containing all permutations of " { $snippet "seq" } " in lexicographical order." }
28 { $examples { $example "USING: math.combinatorics prettyprint ;" "3 all-permutations ." "{ { 0 1 2 } { 0 2 1 } { 1 0 2 } { 1 2 0 } { 2 0 1 } { 2 1 0 } }" } } ;
29
30 HELP: inverse-permutation
31 { $values { "seq" sequence } { "permutation" sequence } }
32 { $description "Outputs a sequence of indices representing the lexicographical permutation of " { $snippet "seq" } "." }
33 { $notes "All items in " { $snippet "seq" } " must be comparable by " { $link <=> } "." }
34 { $examples { $example "USING: math.combinatorics prettyprint ;" "\"dcba\" inverse-permutation ." "{ 3 2 1 0 }" } { $example "USING: math.combinatorics prettyprint ;" "{ 12 56 34 78 } inverse-permutation ." "{ 0 2 1 3 }" } } ;
35
36
37 IN: math.combinatorics.private
38
39 HELP: factoradic
40 { $values { "n" integer } { "factoradic" sequence } }
41 { $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." }
42 { $examples { $example "USING: math.combinatorics.private  prettyprint ;" "859 factoradic ." "{ 1 1 0 3 0 1 0 }" } } ;
43
44 HELP: >permutation
45 { $values { "factoradic" sequence } { "permutation" sequence } }
46 { $description "Converts an integer represented in factoradic form into its corresponding unique permutation (0-based)." }
47 { $notes "For clarification, the following two statements are equivalent:" { $code "10 factoradic >permutation" "{ 1 2 0 0 } >permutation" } }
48 { $examples { $example "USING: math.combinatorics.private prettyprint ;" "{ 0 0 0 0 } >permutation ." "{ 0 1 2 3 }" } } ;
49