1math::combinatorics(n) Tcl Math Library math::combinatorics(n)
2
3
4
5______________________________________________________________________________
6
8 math::combinatorics - Combinatorial functions in the Tcl Math Library
9
11 package require Tcl 8.2
12
13 package require math ?1.2.3?
14
15 package require Tcl 8.6
16
17 package require TclOO
18
19 package require math::combinatorics ?2.0?
20
21 ::math::ln_Gamma z
22
23 ::math::factorial x
24
25 ::math::choose n k
26
27 ::math::Beta z w
28
29 ::math::combinatorics::permutations n
30
31 ::math::combinatorics::variations n k
32
33 ::math::combinatorics::combinations n k
34
35 ::math::combinatorics::derangements n
36
37 ::math::combinatorics::catalan n
38
39 ::math::combinatorics::firstStirling n m
40
41 ::math::combinatorics::secondStirling n m
42
43 ::math::combinatorics::partitionP n
44
45 ::math::combinatorics::list-permutations n
46
47 ::math::combinatorics::list-variations n k
48
49 ::math::combinatorics::list-combinations n k
50
51 ::math::combinatorics::list-derangements n
52
53 ::math::combinatorics::list-powerset n
54
55 ::math::combinatorics::permutationObj new/create NAME n
56
57 $perm next
58
59 $perm reset
60
61 $perm setElements elements
62
63 $perm setElements
64
65 ::math::combinatorics::combinationObj new/create NAME n k
66
67 $combin next
68
69 $combin reset
70
71 $combin setElements elements
72
73 $combin setElements
74
75______________________________________________________________________________
76
78 The math package contains implementations of several functions useful
79 in combinatorial problems. The math::combinatorics extends the collec‐
80 tions based on features in Tcl 8.6. Note: the meaning of the parti‐
81 tionP function, Catalan and Stirling numbers is explained on the Math‐
82 World website [http://mathworld.wolfram.com]
83
85 ::math::ln_Gamma z
86 Returns the natural logarithm of the Gamma function for the ar‐
87 gument z.
88
89 The Gamma function is defined as the improper integral from zero
90 to positive infinity of
91
92
93 t**(x-1)*exp(-t) dt
94
95
96 The approximation used in the Tcl Math Library is from Lanczos, ISIAM
97 J. Numerical Analysis, series B, volume 1, p. 86. For "x > 1", the ab‐
98 solute error of the result is claimed to be smaller than 5.5*10**-10 --
99 that is, the resulting value of Gamma when
100
101
102 exp( ln_Gamma( x) )
103
104
105 is computed is expected to be precise to better than nine sig‐
106 nificant figures.
107
108 ::math::factorial x
109 Returns the factorial of the argument x.
110
111 For integer x, 0 <= x <= 12, an exact integer result is re‐
112 turned.
113
114 For integer x, 13 <= x <= 21, an exact floating-point result is
115 returned on machines with IEEE floating point.
116
117 For integer x, 22 <= x <= 170, the result is exact to 1 ULP.
118
119 For real x, x >= 0, the result is approximated by computing
120 Gamma(x+1) using the ::math::ln_Gamma function, and the result
121 is expected to be precise to better than nine significant fig‐
122 ures.
123
124 It is an error to present x <= -1 or x > 170, or a value of x
125 that is not numeric.
126
127 ::math::choose n k
128 Returns the binomial coefficient C(n, k)
129
130
131 C(n,k) = n! / k! (n-k)!
132
133
134 If both parameters are integers and the result fits in 32 bits,
135 the result is rounded to an integer.
136
137 Integer results are exact up to at least n = 34. Floating point
138 results are precise to better than nine significant figures.
139
140 ::math::Beta z w
141 Returns the Beta function of the parameters z and w.
142
143
144 Beta(z,w) = Beta(w,z) = Gamma(z) * Gamma(w) / Gamma(z+w)
145
146
147 Results are returned as a floating point number precise to bet‐
148 ter than nine significant digits provided that w and z are both
149 at least 1.
150
151 ::math::combinatorics::permutations n
152 Return the number of permutations of n items. The returned num‐
153 ber is always an integer, it is not limited by the range of
154 32-or 64-bits integers using the arbitrary precision integers
155 available in Tcl 8.5 and later.
156
157 int n The number of items to be permuted.
158
159 ::math::combinatorics::variations n k
160 Return the number of variations k items selected from the total
161 of n items. The order of the items is taken into account.
162
163 int n The number of items to be selected from.
164
165 int k The number of items to be selected in each variation.
166
167 ::math::combinatorics::combinations n k
168 Return the number of combinations of k items selected from the
169 total of n items. The order of the items is not important.
170
171 int n The number of items to be selected from.
172
173 int k The number of items to be selected in each combination.
174
175 ::math::combinatorics::derangements n
176 Return the number of derangements of n items. A derangement is a
177 permutation where each item is displaced from the original posi‐
178 tion.
179
180 int n The number of items to be rearranged.
181
182 ::math::combinatorics::catalan n
183 Return the n'th Catalan number. The number n is expected to be 1
184 or larger. These numbers occur in various combinatorial prob‐
185 lems.
186
187 int n The index of the Catalan number
188
189 ::math::combinatorics::firstStirling n m
190 Calculate a Stirling number of the first kind (signed version, m
191 cycles in a permutation of n items)
192
193 int n Number of items
194
195 int m Number of cycles
196
197 ::math::combinatorics::secondStirling n m
198 Calculate a Stirling number of the second kind (m non-empty sub‐
199 sets from n items)
200
201 int n Number of items
202
203 int m Number of subsets
204
205 ::math::combinatorics::partitionP n
206 Calculate the number of ways an integer n can be written as the
207 sum of positive integers.
208
209 int n Number in question
210
211 ::math::combinatorics::list-permutations n
212 Return the list of permutations of the numbers 0, ..., n-1.
213
214 int n The number of items to be permuted.
215
216 ::math::combinatorics::list-variations n k
217 Return the list of variations of k numbers selected from the
218 numbers 0, ..., n-1. The order of the items is taken into ac‐
219 count.
220
221 int n The number of items to be selected from.
222
223 int k The number of items to be selected in each variation.
224
225 ::math::combinatorics::list-combinations n k
226 Return the list of combinations of k numbers selected from the
227 numbers 0, ..., n-1. The order of the items is ignored.
228
229 int n The number of items to be selected from.
230
231 int k The number of items to be selected in each combination.
232
233 ::math::combinatorics::list-derangements n
234 Return the list of derangements of the numbers 0, ..., n-1.
235
236 int n The number of items to be rearranged.
237
238 ::math::combinatorics::list-powerset n
239 Return the list of all subsets of the numbers 0, ..., n-1.
240
241 int n The number of items to be rearranged.
242
243 ::math::combinatorics::permutationObj new/create NAME n
244 Create a TclOO object for returning permutations one by one. If
245 the last permutation has been reached an empty list is returned.
246
247 int n The number of items to be rearranged.
248
249 $perm next
250 Return the next permutation of n objects.
251
252 $perm reset
253 Reset the object, so that the command next returns the complete
254 list again.
255
256 $perm setElements elements
257 Register a list of items to be permuted, using the nextElements
258 command.
259
260 list elements
261 The list of n items that will be permuted.
262
263 $perm setElements
264 Return the next permulation of the registered items.
265
266 ::math::combinatorics::combinationObj new/create NAME n k
267 Create a TclOO object for returning combinations one by one. If
268 the last combination has been reached an empty list is returned.
269
270 int n The number of items to be rearranged.
271
272 $combin next
273 Return the next combination of n objects.
274
275 $combin reset
276 Reset the object, so that the command next returns the complete
277 list again.
278
279 $combin setElements elements
280 Register a list of items to be permuted, using the nextElements
281 command.
282
283 list elements
284 The list of n items that will be permuted.
285
286 $combin setElements
287 Return the next combination of the registered items.
288
290 This document, and the package it describes, will undoubtedly contain
291 bugs and other problems. Please report such in the category math of
292 the Tcllib Trackers [http://core.tcl.tk/tcllib/reportlist]. Please
293 also report any ideas for enhancements you may have for either package
294 and/or documentation.
295
296 When proposing code changes, please provide unified diffs, i.e the out‐
297 put of diff -u.
298
299 Note further that attachments are strongly preferred over inlined
300 patches. Attachments can be made by going to the Edit form of the
301 ticket immediately after its creation, and then using the left-most
302 button in the secondary navigation bar.
303
305 Mathematics
306
307
308
309tcllib 2.0 math::combinatorics(n)