File: combinatorics.man

package info (click to toggle)
tcllib 2.0%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 83,560 kB
  • sloc: tcl: 306,798; ansic: 14,272; sh: 3,035; xml: 1,766; yacc: 1,157; pascal: 881; makefile: 124; perl: 84; f90: 84; python: 33; ruby: 13; php: 11
file content (275 lines) | stat: -rw-r--r-- 8,188 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
[comment {-*- tcl -*- doctools manpage}]
[manpage_begin math::combinatorics n 2.1]
[moddesc   {Tcl Math Library}]
[titledesc {Combinatorial functions in the Tcl Math Library}]
[category  Mathematics]
[require Tcl "8.5 9"]
[require math [opt 1.2.6]]
[require Tcl "8.6 9"]
[require TclOO]
[require math::combinatorics [opt 2.1]]
[description]
[para]

The [package math] package contains implementations of several
functions useful in combinatorial problems. The [package math::combinatorics]
extends the collections based on features in Tcl 8.6.

Note: the meaning of the partitionP function, Catalan and Stirling numbers is explained on the
[uri http://mathworld.wolfram.com {MathWorld website}]


[section COMMANDS]
[list_begin definitions]

[call [cmd ::math::ln_Gamma] [arg z]]

Returns the natural logarithm of the Gamma function for the argument
[arg z].

[para]

The Gamma function is defined as the improper integral from zero to
positive infinity of

[example {
  t**(x-1)*exp(-t) dt
}]

[para]

The approximation used in the Tcl Math Library is from Lanczos,
[emph {ISIAM J. Numerical Analysis, series B,}] volume 1, p. 86.
For "[var x] > 1", the absolute error of the result is claimed to be
smaller than 5.5*10**-10 -- that is, the resulting value of Gamma when

[example {
  exp( ln_Gamma( x) )
}]

is computed is expected to be precise to better than nine significant
figures.

[call [cmd ::math::factorial] [arg x]]

Returns the factorial of the argument [arg x].

[para]

For integer [arg x], 0 <= [arg x] <= 12, an exact integer result is
returned.

[para]

For integer [arg x], 13 <= [arg x] <= 21, an exact floating-point
result is returned on machines with IEEE floating point.

[para]

For integer [arg x], 22 <= [arg x] <= 170, the result is exact to 1
ULP.

[para]

For real [arg x], [arg x] >= 0, the result is approximated by
computing [term Gamma(x+1)] using the [cmd ::math::ln_Gamma]
function, and the result is expected to be precise to better than nine
significant figures.

[para]

It is an error to present [arg x] <= -1 or [arg x] > 170, or a value
of [arg x] that is not numeric.

[call [cmd ::math::choose] [arg {n k}]]

Returns the binomial coefficient [term {C(n, k)}]

[example {
   C(n,k) = n! / k! (n-k)!
}]

If both parameters are integers and the result fits in 32 bits, the
result is rounded to an integer.

[para]

Integer results are exact up to at least [arg n] = 34.  Floating point
results are precise to better than nine significant figures.

[call [cmd ::math::Beta] [arg {z w}]]

Returns the Beta function of the parameters [arg z] and [arg w].

[example {
   Beta(z,w) = Beta(w,z) = Gamma(z) * Gamma(w) / Gamma(z+w)
}]

Results are returned as a floating point number precise to better than
nine significant digits provided that [arg w] and [arg z] are both at
least 1.


[call [cmd ::math::combinatorics::permutations] [arg n]]
Return the number of permutations of n items. The returned number
is always an integer, it is not limited by the range of 32-or 64-bits
integers using the arbitrary precision integers available in Tcl 8.5 and later.

[list_begin arguments]
[arg_def int n] The number of items to be permuted.
[list_end]

[call [cmd ::math::combinatorics::variations] [arg n] [arg k]]
Return the number of variations k items selected from the total of n items.
The order of the items is taken into account.

[list_begin arguments]
[arg_def int n] The number of items to be selected from.
[arg_def int k] The number of items to be selected in each variation.
[list_end]

[call [cmd ::math::combinatorics::combinations] [arg n] [arg k]]
Return the number of combinations of k items selected from the total of n items.
The order of the items is not important.

[list_begin arguments]
[arg_def int n] The number of items to be selected from.
[arg_def int k] The number of items to be selected in each combination.
[list_end]

[call [cmd ::math::combinatorics::derangements] [arg n]]
Return the number of derangements of n items. A derangement is a permutation
where each item is displaced from the original position.

[list_begin arguments]
[arg_def int n] The number of items to be rearranged.
[list_end]

[call [cmd ::math::combinatorics::catalan] [arg n]]
Return the n'th Catalan number. The number n is expected to be 1 or larger.
These numbers occur in various combinatorial problems.

[list_begin arguments]
[arg_def int n] The index of the Catalan number
[list_end]

[call [cmd ::math::combinatorics::firstStirling] [arg n] [arg m]]
Calculate a Stirling number of the first kind
(signed version, m cycles in a permutation of n items)

[list_begin arguments]
[arg_def int n] Number of items
[arg_def int m] Number of cycles
[list_end]

[call [cmd ::math::combinatorics::secondStirling] [arg n] [arg m]]
Calculate a Stirling number of the second kind
(m non-empty subsets from n items)

[list_begin arguments]
[arg_def int n] Number of items
[arg_def int m] Number of subsets
[list_end]

[call [cmd ::math::combinatorics::partitionP] [arg n]]
Calculate the number of ways an integer n can be written as the sum of positive integers.

[list_begin arguments]
[arg_def int n] Number in question
[list_end]


[call [cmd ::math::combinatorics::list-permutations] [arg n]]
Return the list of permutations of the numbers 0, ..., n-1.

[list_begin arguments]
[arg_def int n] The number of items to be permuted.
[list_end]

[call [cmd ::math::combinatorics::list-variations] [arg n] [arg k]]
Return the list of variations of k numbers selected from the numbers 0, ..., n-1.
The order of the items is taken into account.

[list_begin arguments]
[arg_def int n] The number of items to be selected from.
[arg_def int k] The number of items to be selected in each variation.
[list_end]

[call [cmd ::math::combinatorics::list-combinations] [arg n] [arg k]]
Return the list of combinations of k numbers selected from the numbers 0, ..., n-1.
The order of the items is ignored.

[list_begin arguments]
[arg_def int n] The number of items to be selected from.
[arg_def int k] The number of items to be selected in each combination.
[list_end]

[call [cmd ::math::combinatorics::list-derangements] [arg n]]
Return the list of derangements of the numbers 0, ..., n-1.

[list_begin arguments]
[arg_def int n] The number of items to be rearranged.
[list_end]

[call [cmd ::math::combinatorics::list-powerset] [arg n]]
Return the list of all subsets of the numbers 0, ..., n-1.

[list_begin arguments]
[arg_def int n] The number of items to be rearranged.
[list_end]


[call [cmd ::math::combinatorics::permutationObj] new/create NAME [arg n]]
Create a TclOO object for returning permutations one by one. If the last permutation
has been reached an empty list is returned.

[list_begin arguments]
[arg_def int n] The number of items to be rearranged.
[list_end]

[call [cmd \$perm] next]
Return the next permutation of n objects.

[call [cmd \$perm] reset]
Reset the object, so that the command [term next] returns the complete list again.

[call [cmd \$perm] setElements [arg elements]]
Register a list of items to be permuted, using the [term nextElements] command.

[list_begin arguments]
[arg_def list elements] The list of n items that will be permuted.
[list_end]

[call [cmd \$perm] setElements]
Return the next permulation of the registered items.

[call [cmd ::math::combinatorics::combinationObj] new/create NAME [arg n] [arg k]]
Create a TclOO object for returning combinations one by one. If the last combination
has been reached an empty list is returned.

[list_begin arguments]
[arg_def int n] The number of items to be rearranged.
[list_end]

[call [cmd \$combin] next]
Return the next combination of n objects.

[call [cmd \$combin] reset]
Reset the object, so that the command [term next] returns the complete list again.

[call [cmd \$combin] setElements [arg elements]]
Register a list of items to be permuted, using the [term nextElements] command.

[list_begin arguments]
[arg_def list elements] The list of n items that will be permuted.
[list_end]

[call [cmd \$combin] setElements]
Return the next combination of the registered items.


[list_end]

[vset CATEGORY math]
[include ../common-text/feedback.inc]
[manpage_end]