UNPKG

4.29 kBMarkdownView Raw
1[![build status](https://secure.travis-ci.org/dankogai/js-combinatorics.png)](http://travis-ci.org/dankogai/js-combinatorics)
2
3combinatorics.js
4===============
5
6Simple combinatorics like power set, combination, and permutation in JavaScript
7
8SYNOPSIS
9--------
10
11### In Browser
12````
13<script src="combinatorics.js"></script>
14````
15### node.js
16````
17var Combinatorics = require('./combinatorics.js').Combinatorics;
18````
19
20#### power set
21````
22var cmb, a;
23cmb = Combinatorics.power(['a','b','c']);
24cmb.each(function(a){ console.log(a) });
25// []
26// ["a"]
27// ["b"]
28// ["a", "b"]
29// ["c"]
30// ["a", "c"]
31// ["b", "c"]
32// ["a", "b", "c"]
33````
34#### combination
35````
36cmb = Combinatorics.combination(['a','b','c','d'], 2);
37while(a = cmb.next()) console.log(a);
38// ["a", "b"]
39// ["a", "c"]
40// ["a", "d"]
41// ["b", "c"]
42// ["b", "d"]
43// ["c", "d"]
44````
45#### permutation
46````
47cmb = Combinatorics.permutation(['a','b','c','d']); // assumes 4
48console.log(cmb.toArray());
49// [
50 ["a","b","c","d"],["a","b","d","c"],["a","c","b","d"],["a","c","d","b"],
51 ["a","d","b","c"],["a","d","c","b"],["b","a","c","d"],["b","a","d","c"],
52 ["b","c","a","d"],["b","c","d","a"],["b","d","a","c"],["b","d","c","a"],
53 ["c","a","b","d"],["c","a","d","b"],["c","b","a","d"],["c","b","d","a"],
54 ["c","d","a","b"],["c","d","b","a"],["d","a","b","c"],["d","a","c","b"],
55 ["d","b","a","c"],["d","b","c","a"],["d","c","a","b"],["d","c","b","a"]
56]
57````
58
59#### cartesian product
60````
61cp = Combinatorics.cartesianProduct([0, 1, 2], [0, 10, 20], [0, 100, 200]);
62console.log(cp.toArray());
63// [
64 [0, 0, 0], [1, 0, 0], [2, 0, 0],
65 [0, 10, 0], [1, 10, 0], [2, 10, 0],
66 [0, 20, 0], [1, 20, 0], [2, 20, 0],
67 [0, 0, 100], [1, 0, 100], [2, 0, 100],
68 [0, 10, 100],[1, 10, 100],[2, 10, 100],
69 [0, 20, 100],[1, 20, 100],[2, 20, 100],
70 [0, 0, 200], [1, 0, 200], [2, 0, 200],
71 [0, 10, 200],[1, 10, 200],[2, 10, 200],
72 [0, 20, 200],[1, 20, 200],[2, 20, 200]
73]
74````
75
76
77#### Arithmetic Functions
78
79+ .`P(m, n)`
80 calculates m P n
81+ .`C(m, n)`
82 calculates m C n
83+ .`factorial(n)`
84 calculates `n!`
85+ .`factoradic(n)`
86 returns the factoradic representation of n in array, *LSB ORDER*. See
87 http://en.wikipedia.org/wiki/Factorial_number_system
88
89
90DESCRIPTION
91-----------
92
93All methods create _generators_. Instead of creating all elements at once, each element is created on demand. So it is memory efficient even when you need to iterate through millions of elements.
94
95#### Combinatorics.power( _ary_ )
96
97Creates a generator which generates the power set of _ary_
98
99#### Combinatorics.combination( _ary_ , _nelem_ )
100
101Creates a generator which generates the combination of _ary_ with _nelem_ elements.
102When _nelem_ is ommited, _ary_.length is used.
103
104#### Combinatorics.permutation( _ary_, _nelem_ )
105
106Creates a generator which generates the permutation of _ary_ with _nelem_ elements.
107When _nelem_ is ommited, _ary_.length is used.
108
109#### Combinatorics.cartesianProduct( _ary0_, ...)
110
111Creates a generator which generates the cartesian product of the arrays. All arguments must be arrays with more than one element.
112
113### Generator Methods
114
115All generators have following methods:
116
117#### .next()
118
119Returns the element or `undefined` if no more element is available.
120
121#### .forEach(function(a){ ... });
122
123Applies the callback function for each element.
124
125#### .toArray()
126
127All elements at once.
128
129#### .map(function(a){ ... })
130
131All elements at once with function f applied to each element.
132
133#### .filter(function(a){ ... })
134
135Returns an array with elements that passes the filter function.
136For example, you can redefine combination as follows:
137
138````
139myCombination = function(ary, n) {
140 return Combinatorics.power(ary).filter(function (a) {
141 return a.length === n;
142 });
143};
144````
145
146#### .length
147
148Returns the number of elements to be generated
149Which equals to _generator_`.toArray().length` but it is precalculated without actually generating elements.
150Handy when you prepare for large iteraiton.
151
152#### 0 + _generator_
153
154Same as _generator_`.length`
155
156#### .nth(n)
157
158Available for `power` and `cartesianProduct` generator which returns the *n*th element.
159
160#### .get(x0, ...)
161
162Available for `cartesianProduct` generator. Arguments are coordinates in integer.
163Arguments can be out of bounds but it returns `undefined` in such cases.
164
\No newline at end of file