[ Main Table Of Contents  Table Of Contents  Keyword Index  Categories  Modules  Applications ]
NAME
math::combinatorics  Combinatorial functions in the Tcl Math Library
Table Of Contents
SYNOPSIS
package require Tcl 8.2
package require math ?1.2.3?
package require Tcl 8.6
package require TclOO
package require math::combinatorics ?2.0?
::math::ln_Gamma z
::math::factorial x
::math::choose n k
::math::Beta z w
::math::combinatorics::permutations n
::math::combinatorics::variations n k
::math::combinatorics::combinations n k
::math::combinatorics::derangements n
::math::combinatorics::catalan n
::math::combinatorics::firstStirling n m
::math::combinatorics::secondStirling n m
::math::combinatorics::partitionP n
::math::combinatorics::listpermutations n
::math::combinatorics::listvariations n k
::math::combinatorics::listcombinations n k
::math::combinatorics::listderangements n
::math::combinatorics::listpowerset n
::math::combinatorics::permutationObj new/create NAME n
$perm next
$perm reset
$perm setElements elements
$perm setElements
::math::combinatorics::combinationObj new/create NAME n k
$combin next
$combin reset
$combin setElements elements
$combin setElements
DESCRIPTION
The math package contains implementations of several functions useful in combinatorial problems. The 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 MathWorld website
COMMANDS

Returns the natural logarithm of the Gamma function for the argument z.
The Gamma function is defined as the improper integral from zero to positive infinity of
t**(x1)*exp(t) dt
The approximation used in the Tcl Math Library is from Lanczos, ISIAM J. Numerical Analysis, series B, volume 1, p. 86. For "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
exp( ln_Gamma( x) )
is computed is expected to be precise to better than nine significant figures.

Returns the factorial of the argument x.
For integer x, 0 <= x <= 12, an exact integer result is returned.
For integer x, 13 <= x <= 21, an exact floatingpoint result is returned on machines with IEEE floating point.
For integer x, 22 <= x <= 170, the result is exact to 1 ULP.
For real x, x >= 0, the result is approximated by computing Gamma(x+1) using the ::math::ln_Gamma function, and the result is expected to be precise to better than nine significant figures.
It is an error to present x <= 1 or x > 170, or a value of x that is not numeric.

Returns the binomial coefficient C(n, k)
C(n,k) = n! / k! (nk)!
If both parameters are integers and the result fits in 32 bits, the result is rounded to an integer.
Integer results are exact up to at least n = 34. Floating point results are precise to better than nine significant figures.

Returns the Beta function of the parameters z and w.
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 w and z are both at least 1.
::math::combinatorics::permutations n
Return the number of permutations of n items. The returned number is always an integer, it is not limited by the range of 32or 64bits integers using the arbitrary precision integers available in Tcl 8.5 and later.
int n
The number of items to be permuted.
::math::combinatorics::variations n k
Return the number of variations k items selected from the total of n items. The order of the items is taken into account.
int n
The number of items to be selected from.
int k
The number of items to be selected in each variation.
::math::combinatorics::combinations n k
Return the number of combinations of k items selected from the total of n items. The order of the items is not important.
int n
The number of items to be selected from.
int k
The number of items to be selected in each combination.
::math::combinatorics::derangements n
Return the number of derangements of n items. A derangement is a permutation where each item is displaced from the original position.
int n
The number of items to be rearranged.
::math::combinatorics::catalan n
Return the n'th Catalan number. The number n is expected to be 1 or larger. These numbers occur in various combinatorial problems.
int n
The index of the Catalan number
::math::combinatorics::firstStirling n m
Calculate a Stirling number of the first kind (signed version, m cycles in a permutation of n items)
int n
Number of items
int m
Number of cycles
::math::combinatorics::secondStirling n m
Calculate a Stirling number of the second kind (m nonempty subsets from n items)
int n
Number of items
int m
Number of subsets
::math::combinatorics::partitionP n
Calculate the number of ways an integer n can be written as the sum of positive integers.
int n
Number in question
::math::combinatorics::listpermutations n
Return the list of permutations of the numbers 0, ..., n1.
int n
The number of items to be permuted.
::math::combinatorics::listvariations n k
Return the list of variations of k numbers selected from the numbers 0, ..., n1. The order of the items is taken into account.
int n
The number of items to be selected from.
int k
The number of items to be selected in each variation.
::math::combinatorics::listcombinations n k
Return the list of combinations of k numbers selected from the numbers 0, ..., n1. The order of the items is ignored.
int n
The number of items to be selected from.
int k
The number of items to be selected in each combination.
::math::combinatorics::listderangements n
Return the list of derangements of the numbers 0, ..., n1.
int n
The number of items to be rearranged.
::math::combinatorics::listpowerset n
Return the list of all subsets of the numbers 0, ..., n1.
int n
The number of items to be rearranged.
::math::combinatorics::permutationObj new/create NAME n
Create a TclOO object for returning permutations one by one. If the last permutation has been reached an empty list is returned.
int n
The number of items to be rearranged.

Return the next permutation of n objects.

Reset the object, so that the command next returns the complete list again.

Register a list of items to be permuted, using the nextElements command.
list elements
The list of n items that will be permuted.

Return the next permulation of the registered items.
::math::combinatorics::combinationObj new/create NAME n k
Create a TclOO object for returning combinations one by one. If the last combination has been reached an empty list is returned.
int n
The number of items to be rearranged.

Return the next combination of n objects.

Reset the object, so that the command next returns the complete list again.

Register a list of items to be permuted, using the nextElements command.
list elements
The list of n items that will be permuted.

Return the next combination of the registered items.
Bugs, Ideas, Feedback
This document, and the package it describes, will undoubtedly contain bugs and other problems. Please report such in the category math of the Tcllib Trackers. Please also report any ideas for enhancements you may have for either package and/or documentation.
When proposing code changes, please provide unified diffs, i.e the output of diff u.
Note further that attachments are strongly preferred over inlined patches. Attachments can be made by going to the Edit form of the ticket immediately after its creation, and then using the leftmost button in the secondary navigation bar.
CATEGORY
Mathematics