math::combinatorics(3tcl)

πŸ“¦
version 2.0 Β· 3 versions
, in debian trixie, ubuntu questing, ubuntu resolute, ubuntu stonking

πŸ’– math::combinatorics β€” the Tcl Math Library's combinatorial era πŸ’–

okay bestie, gather round, because this is math::combinatorics(3tcl) from the Tcl Math Library and it is genuinely the moment. it's the part of the math package that does all your counting-things-ways stuff, and yes it's giving main character energy. ✨


🌸 NAME 🌸

math::combinatorics β€” Combinatorial functions in the Tcl Math Library πŸ’…


πŸŽ€ SYNOPSIS πŸŽ€

so first you gotta require the things, and here's literally exactly how, no cap:

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?

and then the actual functions, the whole roster, icons only:

::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 has implementations of several functions that are useful in combinatorial problems. and math::combinatorics extends the collections based on features that live in Tcl 8.6. πŸ¦‹

πŸ“Œ heads up, bestie: the meaning of the partitionP function, and of Catalan and Stirling numbers, is explained over on the MathWorld website at [http://mathworld.wolfram.com]

πŸ‘‘ COMMANDS πŸ‘‘

okay this is the big one, let's go one by one, no skipping, we eat the whole plate πŸ“

  • ::math::ln_Gamma z β€” gives you back the natural logarithm of the Gamma function for the argument z.

so 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. and for "x > 1", the absolute error of the result is claimed to be smaller than 5.5*10**-10 β€” meaning that the resulting value of Gamma when

exp( ln_Gamma( x) )

gets computed is expected to be precise to better than nine significant figures. πŸ’―

  • ::math::factorial x β€” returns the factorial of the argument x.
    • for integer x, 0 <= x <= 12, you get an exact integer result back.
    • for integer x, 13 <= x <= 21, you get an exact floating-point result back 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. so don't. πŸ™ˆ
  • ::math::choose n k β€” returns the binomial coefficient C(n, k)
C(n,k) = n! / k! (n-k)!

if both parameters are integers and the result fits in 32 bits, the result gets 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. slay.

  • ::math::Beta z w β€” returns the Beta function of the parameters z and w.
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. πŸ’œ

  • ::math::combinatorics::permutations n β€” returns the number of permutations of n items. the returned number is always an integer, and it's not limited by the range of 32- or 64-bit integers, thanks to the arbitrary precision integers available in Tcl 8.5 and later. big numbers, no fear. πŸ”₯
    • int n β€” the number of items to be permuted.
  • ::math::combinatorics::variations n k β€” returns the number of variations of 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 β€” returns 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 β€” returns the number of derangements of n items. a derangement is a permutation where each item is displaced from the original position. literally nobody ends up where they started, iconic behavior. 🫢
    • int n β€” the number of items to be rearranged.
  • ::math::combinatorics::catalan n β€” returns 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 β€” calculates 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 β€” calculates a Stirling number of the second kind (m non-empty subsets from n items).
    • int n β€” number of items.
    • int m β€” number of subsets.
  • ::math::combinatorics::partitionP n β€” calculates the number of ways an integer n can be written as the sum of positive integers.
    • int n β€” number in question.
  • ::math::combinatorics::list-permutations n β€” returns the list of permutations of the numbers 0, ..., n-1.
    • int n β€” the number of items to be permuted.
  • ::math::combinatorics::list-variations n k β€” returns the list of variations of k numbers selected from the numbers 0, ..., 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 of k numbers selected from the numbers 0, ..., 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 numbers 0, ..., n-1.
    • int n β€” the number of items to be rearranged.
  • ::math::combinatorics::list-powerset n β€” returns the list of all subsets of the numbers 0, ..., n-1.
    • int n β€” the number of items to be rearranged.
  • ::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 β€” return the next permutation of n objects.
  • $perm reset β€” reset the object, so that the command next returns the complete list again. the way that it just... starts over. πŸ”
  • $perm setElements elements β€” register a list of items to be permuted, using the nextElements command.
    • list elements β€” the list of n items that will be permuted.
  • $perm setElements β€” return the next permulation of the registered items. (yes bestie, we're typing it exactly as the docs do, don't @ me πŸ’€)
  • ::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 β€” return the next combination of n objects.
  • $combin reset β€” reset the object, so that the command next returns the complete list again.
  • $combin setElements elements β€” register a list of items to be permuted, using the nextElements command.
    • list elements β€” the list of n items that will be permuted.
  • $combin setElements β€” return 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. 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 feedback, fr fr. 🫢

when proposing code changes, please provide unified diffs, i.e. the output of diff -u.

also 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. ✨


πŸ¦‹ CATEGORY πŸ¦‹

Mathematics


and that's the whole page, bestie. πŸ’– this one ships as part of tcllib, version 2.1, and it's the math::combinatorics(3tcl) manual β€” go forth and count things, periodt. πŸ‘‘

πŸ“¦ where this page comes from