Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 167 lines (131 sloc) 4.094 kb
4e46356 @postmodern Capitalize the title.
authored
1 # Combinatorics
effb8c2 @postmodern Populated the project.
authored
2
41e50c9 @postmodern https://github links.
authored
3 * [Source](https://github.com/postmodern/combinatorics)
4 * [Issues](https://github.com/postmodern/combinatorics/issues)
5e60668 @postmodern Updated the links in the README.
authored
5 * [Documentation](http://rubydoc.info/gems/combinatorics)
6 * [Email](mailto:postmodern.mod3 at gmail.com)
effb8c2 @postmodern Populated the project.
authored
7
8 ## Description
9
10 A collection of modules and methods for performing
11 [Combinatoric](http://en.wikipedia.org/wiki/Combinatoric) calculations.
15b8806 @duper Updated the gem description and added several new REPL samples in
duper authored
12 Methods are defined to compute power sets, cartesian products, permutations,
96ee57a @postmodern Wording.
authored
13 combinations, and derangements.
14
15 Note: this includes k-combinations and k-permutations, i.e. only generating
16 resultant sets of a given size. Each set theory operation method supports block
17 arguments to allow continuous code execution during the combinatoric set
18 generation process. Each combinatoric operation implementation also supports a
19 `cardinality` method which determines the total number of subsets that will be
20 created beforehand (to aid in the avoidance of starting an operation which will
21 never complete due to exponential computational complexity.)
effb8c2 @postmodern Populated the project.
authored
22
23 ## Features
24
25 * Adds Haskell/Python style list comprehensions via {Array#comprehension}.
ac45c5b @postmodern Updated the features section.
authored
26 * Provides reusable Combinatorics Mixins:
27 * {Combinatorics::CartesianProduct}
28 * {Combinatorics::Choose}
29 * {Combinatorics::Derange}
30 * {Combinatorics::Permute}
31 * {Combinatorics::PowerSet}
32 * Adds Combinatorics conveniance methods to {Array} and {Set}:
065febb @postmodern Reference the primary method, not it's aliases.
authored
33 * `cartesian_product`
ac45c5b @postmodern Updated the features section.
authored
34 * `choose`
35 * `derange`
36 * `permute`
37 * `powerset`
d616fdf @postmodern Updated the README.
authored
38 * Adds conveniance methods:
39 * {Range#&}
40 * {Range#upto}
41 * {Range#downto}
42 * {Math.sigma}
43 * {Math.pi}
44 * {Math.factorial}
45 * {Math.subfactorial}
effb8c2 @postmodern Populated the project.
authored
46
f14c63a @postmodern Added an Examples section.
authored
47 ## Examples
48
aa0ac7c @postmodern Removed old examples.
authored
49 Power-set:
f14c63a @postmodern Added an Examples section.
authored
50
51 Set['ab', 'cd', 'ef'].powerset
90b0bc3 @postmodern Formatting.
authored
52 # => [#<Set: {}>,
53 #<Set: {"ef"}>,
54 #<Set: {"cd"}>,
55 #<Set: {"cd", "ef"}>,
56 #<Set: {"ab"}>,
57 #<Set: {"ab", "ef"}>,
58 #<Set: {"ab", "cd"}>,
59 #<Set: {"ab", "cd", "ef"}>]
f14c63a @postmodern Added an Examples section.
authored
60
15b8806 @duper Updated the gem description and added several new REPL samples in
duper authored
61 Cartesian products:
62
d616fdf @postmodern Updated the README.
authored
63 require 'combinatorics/cartesian_product'
64
4a6bf8b @postmodern Use the primary methods in the examples.
authored
65 ['a', 'b', 'c'].cartesian_product([0, 1, 2]).to_a
d616fdf @postmodern Updated the README.
authored
66 # => [["a", 0],
67 ["b", 0],
68 ["c", 0],
69 ["a", 1],
70 ["b", 1],
71 ["c", 1],
72 ["a", 2],
73 ["b", 2],
74 ["c", 2]]
15b8806 @duper Updated the gem description and added several new REPL samples in
duper authored
75
76 k-combinations:
77
d616fdf @postmodern Updated the README.
authored
78 require 'combinatorics/choose'
79
80 ('a'..'f').to_a.choose(2).to_a
81 # => [["a", "b"],
82 ["a", "c"],
83 ["a", "d"],
84 ["a", "e"],
85 ["a", "f"],
86 ["b", "c"],
87 ["b", "d"],
88 ["b", "e"],
89 ["b", "f"],
90 ["c", "d"],
91 ["c", "e"],
92 ["c", "f"],
93 ["d", "e"],
94 ["d", "f"],
95 ["e", "f"]]
15b8806 @duper Updated the gem description and added several new REPL samples in
duper authored
96
97 Derangements:
98
d616fdf @postmodern Updated the README.
authored
99 require 'combinatorics/derange'
100
101 [:_, :q, :z, :x].derange.to_a
102 # => [[:q, :_, :x, :z],
103 [:q, :z, :x, :_],
104 [:q, :x, :_, :z],
105 [:z, :_, :x, :q],
106 [:z, :x, :_, :q],
107 [:z, :x, :q, :_],
108 [:x, :_, :q, :z],
109 [:x, :z, :_, :q],
110 [:x, :z, :q, :_]]
15b8806 @duper Updated the gem description and added several new REPL samples in
duper authored
111
112 Permutation cardinality:
113
d616fdf @postmodern Updated the README.
authored
114 require 'combinatorics/permutation'
115
24ce26a @postmodern Fixed an example.
authored
116 Combinatorics::Permute.cardinality(128)
d616fdf @postmodern Updated the README.
authored
117 # => 8256
15b8806 @duper Updated the gem description and added several new REPL samples in
duper authored
118
6d54329 @postmodern Organize the Examples.
authored
119 List comprehensions:
120
121 require 'combinatorics/list_comprehension'
122
123 [(0..10).step(2),('a'..'c')].comprehension.to_a
124 # => [[0, "a"],
125 [0, "b"],
126 [0, "c"],
127 [2, "a"],
128 [2, "b"],
129 [2, "c"],
130 [4, "a"],
131 [4, "b"],
132 [4, "c"],
133 [6, "a"],
134 [6, "b"],
135 [6, "c"],
136 [8, "a"],
137 [8, "b"],
138 [8, "c"],
139 [10, "a"],
140 [10, "b"],
141 [10, "c"]]
142
143 Find the intersecting sub-range between two ranges:
144
145 (1..50) & (20..100)
146 # => (20..50)
147
148 Enumerate over every sub-range between two ranges:
149
150 (1..5).upto(2..10).to_a
151 # => [1..5, 1..6, 1..7, 1..8, 1..9, 1..10,
152 2..5, 2..6, 2..7, 2..8, 2..9, 2..10]
153
1859e0a @postmodern Specify that ruby >= 1.8.7 is required, due to heavy Enumerator usage.
authored
154 ## Requirements
155
d81bbca @postmodern Removed indentation.
authored
156 * [ruby](http://www.ruby-lang.org/) >= 1.8.7
1859e0a @postmodern Specify that ruby >= 1.8.7 is required, due to heavy Enumerator usage.
authored
157
effb8c2 @postmodern Populated the project.
authored
158 ## Install
159
6c55020 @postmodern No longer recommend installing via sudo.
authored
160 $ gem install combinatorics
effb8c2 @postmodern Populated the project.
authored
161
8741ab2 @postmodern Renamed License to Copyright.
authored
162 ## Copyright
effb8c2 @postmodern Populated the project.
authored
163
9018401 @postmodern Updated the copyright dates.
authored
164 Copyright (c) 2010-2012 Hal Brodigan
effb8c2 @postmodern Populated the project.
authored
165
ef8e540 @postmodern spacing.
authored
166 See {file:LICENSE.txt} for license information.
Something went wrong with that request. Please try again.