math::combinatorics(n)
โจ math::combinatorics โจ
okay bestie, sit down, hydrate, because we are about to get into the Tcl Math Library's combinatorics page and it's genuinely so much. this is the math::combinatorics manual page, and it's lowkey one of the most useful things in the whole math package. no cap. ๐
๐ธ SYNOPSIS ๐ธ
the whole vibe in one block, verbatim, don't @ me:
package require Tcl 8.5 9
package require math ?1.2.6?
package require Tcl 8.6 9
package require TclOO
package require math::combinatorics ?2.1?
::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::list-permutations n
::math::combinatorics::list-variations n k
::math::combinatorics::list-combinations n k
::math::combinatorics::list-derangements n
::math::combinatorics::list-powerset 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 ๐ฆ
so the math package is out here holding implementations of several functions that are genuinely useful in combinatorial problems. and math::combinatorics extends the collections based on features that showed up in Tcl 8.6. glow-up energy. โจ
heads up bestie: the meaning of thepartitionPfunction, and of Catalan and Stirling numbers, is explained over on the MathWorld website athttp://mathworld.wolfram.com. iykyk ๐
๐ COMMANDS ๐
๐งฎ the classic math-y ones
::math::ln_Gamma zโ hands you the natural logarithm of the Gamma function for the argumentz.
real talk, the Gamma function is defined as the improper integral from zero to positive infinity of:
t**(x-1)*exp(-t) dt
the approximation used in the Tcl Math Library comes 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 โ like, when you compute the resulting value of Gamma via
exp( ln_Gamma( x) )
it's expected to be precise to better than nine significant figures. iconic precision, honestly.
::math::factorial xโ returns the factorial of the argumentx. here's the breakdown, and it's giving very specific, so listen up:- for integer
x,0 <= x <= 12, an exact integer result is returned. - for integer
x,13 <= x <= 21, an exact floating-point 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 computingGamma(x+1)using the::math::ln_Gammafunction, and the result is expected to be precise to better than nine significant figures.
- for integer
โ ๏ธ it is an error to presentx <= -1orx > 170, or a value ofxthat is not numeric. do not do this. it will not slay.
::math::choose n kโ returns the binomial coefficientC(n, k)
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. integer results are exact up to at least n = 34. floating point results are precise to better than nine significant figures. ๐
::math::Beta z wโ returns the Beta function of the parameterszandw.
Beta(z,w) = Beta(w,z) = Gamma(z) * Gamma(w) / Gamma(z+w)
results come back as a floating point number precise to better than nine significant digits, provided that w and z are both at least 1.
๐ the counting ones
::math::combinatorics::permutations nโ returns the number of permutations ofnitems. and here's the flex: the returned number is always an integer, and it is not limited by the range of 32-or 64-bits integers, because it uses the arbitrary precision integers available in Tcl 8.5 and later. main character energy fr fr.int nโ the number of items to be permuted.
::math::combinatorics::variations n kโ returns the number of variations ofkitems selected from the total ofnitems. 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โ returns the number of combinations ofkitems selected from the total ofnitems. 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โ returns the number of derangements ofnitems. a derangement is a permutation where each item is displaced from the original position. chaotic but make it mathematical. ๐int nโ the number of items to be rearranged.
::math::combinatorics::catalan nโ returns then'th Catalan number.nis expected to be1or larger. these numbers occur in various combinatorial problems.int nโ the index of the Catalan number.
::math::combinatorics::firstStirling n mโ calculates a Stirling number of the first kind (signed version,mcycles in a permutation ofnitems).int nโ number of items.int mโ number of cycles.
::math::combinatorics::secondStirling n mโ calculates a Stirling number of the second kind (mnon-empty subsets fromnitems).int nโ number of items.int mโ number of subsets.
::math::combinatorics::partitionP nโ calculates the number of ways an integerncan be written as the sum of positive integers.int nโ number in question.
๐ the ones that hand you whole lists
::math::combinatorics::list-permutations nโ returns the list of permutations of the numbers0, ..., n-1.int nโ the number of items to be permuted.
::math::combinatorics::list-variations n kโ returns the list of variations ofknumbers selected from the numbers0, ..., n-1. 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::list-combinations n kโ returns the list of combinations ofknumbers selected from the numbers0, ..., n-1. 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::list-derangements nโ returns the list of derangements of the numbers0, ..., n-1.int nโ the number of items to be rearranged.
::math::combinatorics::list-powerset nโ returns the list of all subsets of the numbers0, ..., n-1.int nโ the number of items to be rearranged.
๐ the TclOO objects (the one-by-one girlies)
::math::combinatorics::permutationObj new/create NAME nโ creates 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.
$perm nextโ returns the next permutation ofnobjects.$perm resetโ resets the object, so that the commandnextreturns the complete list again.$perm setElements elementsโ registers a list of items to be permuted, using thenextElementscommand.list elementsโ the list ofnitems that will be permuted.
$perm setElementsโ returns the next permulation of the registered items. (yes bestie, that's how the page spells it โ keeping it exactly as written, we do not edit the source material ๐)::math::combinatorics::combinationObj new/create NAME n kโ creates 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.
$combin nextโ returns the next combination ofnobjects.$combin resetโ resets the object, so that the commandnextreturns the complete list again.$combin setElements elementsโ registers a list of items to be permuted, using thenextElementscommand.list elementsโ the list ofnitems that will be permuted.
$combin setElementsโ returns the next combination of the registered items.
๐ BUGS, IDEAS, FEEDBACK ๐
ngl, this document โ and the package it describes โ will undoubtedly contain bugs and other problems. it's giving humble but also real. please report such in the category math of the Tcllib Trackers at http://core.tcl.tk/tcllib/reportlist. ๐ซถ
please also report any ideas for enhancements you may have for either the package and/or the documentation. we want the glow-up. โจ
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 left-most button in the secondary navigation bar. do it right and it'll slay. ๐
โญ CATEGORY โญ
Mathematics
and that's the whole page, bestie! this was math::combinatorics from tcllib, version 2.1 โ so go forth and permute, derange, and generally count things with arbitrary precision. periodt. ๐๐๐ฆ๐ธ๐
๐ฆ where this page comes from
- fedora 43: tcllib 2.0-3.fc43 ยท TCL
- fedora 44: tcllib 2.0-4.fc44 ยท TCL
- nixos 26.05: tcllib 2.0
- nixpkgs unstable: tcllib 2.0
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms