Skip to main content

Combinatorics

The 12 definitions of the combinatorics library, each with its Epsil spelling, its MathJSON name, its signature and its full description.

Definitions​

Each definition is listed under its Epsil spelling (the MathJSON name when it has none), with its signature in the engine's type syntax. The Standard Library page is the one-page index of every category.

bellNumber​

(n: broadcastable<integer>, x: broadcastable<number>?) -> number

  • n — The number of elements of the set; BellNumber(0) is 1, and a negative integer stays unevaluated. Sample: 5.
  • x — The variable of the Bell polynomial; without it the result is the Bell number. Sample: 2.

Compute the Bell number B(n), the number of partitions of a set of n elements. With a second operand, bellNumber(n, x) is the Bell (Touchard) polynomial B_n(x), with B_n(1) = bellNumber(n).

bellNumber(5)
// ➔ 52
bellNumber(3, x)
// ➔ x^3 + 3x^2 + x

The Bell numbers follow the recurrence

B(n) = \sum_{k=0}^{n-1} \binom{n-1}{k} B(k), \quad B(0) = 1

For example, the 5 partitions of {1, 2, 3} are {{1, 2, 3}}, {{1, 2}, {3}}, {{1, 3}, {2}}, {{2, 3}, {1}} and {{1}, {2}, {3}}.

[bellNumber(3), bellNumber(4), bellNumber(5), bellNumber(0)]
// ➔ [5,15,52,1]

The Bell (Touchard) polynomial bellNumber(n, x) is \sum_k S(n, k)\, x^k, where S(n, k) is the number of partitions of a set of n elements into k parts:

bellNumber(4, x)
// ➔ x^4 + 6x^3 + 7x^2 + x

See also: Bell number.

MathJSON: BellNumber

["BellNumber", n], ["BellNumber", n, x].

["BellNumber", 3]
// ➔ 5

binomial​

(n: broadcastable<complex | infinity>, k: broadcastable<complex | infinity>) -> number

  • n — The size of the pool; a negative or non-integer value extends the coefficient through the Gamma function. Sample: 10.
  • k — The number of items chosen; with a non-negative integer n, the result is 0 for a negative integer or an integer greater than n. Sample: 3.

Compute the binomial coefficient C(n, k) = n! / (k! (n-k)!). Agrees with Choose for all defined values.

binomial(10, 3)
// ➔ 120
binomial(n, 2)
// ➔ (n * (n - 1)) / 2
\binom{n}{k} = \frac{n!}{k!\,(n-k)!}

It is 0 when k is below 0 or above n, and 1 when k is 0 or n. For a symbolic n, binomial(n, n) is 1 and binomial(n, n - 1) is n.

[binomial(6, 2), binomial(6, 7), binomial(6, -1), binomial(6, 6)]
// ➔ [15,0,0,1]
binomial(6, 2) == 6! / (2! * 4!)
// ➔ true
[binomial(n, n), binomial(n, n - 1)]
// ➔ [1,n]

See also: Binomial coefficient.

MathJSON: Binomial • LaTeX: \binom

["Binomial", n, k]. In LaTeX: \binom{n}{k}.

["Binomial", 6, 2]
// ➔ 15

cartesianProduct​

(set<any>+) -> set

  • arguments 1, … — A factor of the product; each element of the result is a tuple with one component from each factor. Sample: Set(1, 2).

Return the Cartesian product of input sets.

count(cartesianProduct(set(1, 2), set("a", "b", "c")))
// ➔ 6

The Cartesian product A \times B is the set of the ordered tuples (a, b) with a in A and b in B; with more sets, a tuple has one component for each set. Each operand must be a set: a list or a range is a type error.

The product is a lazy collection: it stays as written, and its elements are computed when they are read. count and in answer without listing the elements, and the spread ... lists them:

[...cartesianProduct(set(1, 2), set(a, b))]
// ➔ [(1, a),(1, b),(2, a),(2, b)]
[count(cartesianProduct(set(1, 2), set(3, 4), set(5, 6, 7)))]
// ➔ [12]
(1, 3) in cartesianProduct(set(1, 2), set(3, 4))
// ➔ true
cartesianProduct([1, 2], [3, 4])
// ➔ error: Type error: expected `set`, got `vector<integer^2>` at `[1, 2]` in `cartesianProduct([1, 2], [3, 4])`

See also: Cartesian product.

MathJSON: CartesianProduct

["CartesianProduct", set1, set2, ...]. In LaTeX, A \times B is read as a Multiply, not as a Cartesian product; CartesianProduct is written \mathrm{CartesianProduct}(A, B).

["Count", ["CartesianProduct", ["Set", 1, 2], ["Set", "a", "b"]]]
// ➔ 4

choose​

(n: broadcastable<complex | infinity>, m: broadcastable<complex | infinity>) -> number

  • n — The size of the pool; a negative or non-integer value extends the coefficient through the Gamma function. Sample: 5.
  • m — The number of items chosen; with a non-negative integer n, the result is 0 for a negative integer or an integer greater than n. Sample: 2.

Binomial coefficient: number of ways to choose k items from n. Agrees with Binomial for all defined values.

choose(5, 2)
// ➔ 10
choose(-3, 2)
// ➔ 6

The binomial coefficient "n choose m" is the number of ways to select m elements from a set of n elements, without regard to order:

\binom{n}{m} = \frac{n!}{m!\,(n-m)!}
choose(5, 2)
// ➔ 10

choose is the generalized binomial coefficient. For a non-negative integer n, an m below 0 or above n gives 0. A negative n follows the falling-factorial extension, a symbolic n gives a polynomial in n, and a floating-point n gives a floating-point number:

[choose(5, 7), choose(5, -1), choose(-1, 0), choose(-3, 2)]
// ➔ [0,0,1,6]
choose(n, 2)
// ➔ (n * (n - 1)) / 2
choose(5.5, 2)
// ➔ 12.375

See also: Binomial coefficient.

MathJSON: Choose

["Choose", n, m]. In LaTeX, \binom{n}{m} is read as Binomial, which has the same values; Choose is written \mathrm{Choose}(n, m).

["Choose", 5, 2]
// ➔ 10

combinations​

((xs: S, k: integer) -> list<string> where S: string) & ((xs: collection, k: integer) -> list<list>)

  • xs — The collection to choose from, in its order; the elements of a string are its characters, and each combination is a string. Sample: [1, 2, 3, 4].
  • k — The number of elements of each combination; 0 gives one empty combination, and a number past the size of the collection none. Sample: 2.

Return all k-element combinations of a collection.

combinations([1, 2, 3, 4], 2)
// ➔ [[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]]
combinations("abc", 2)
// ➔ ["ab","ac","bc"]

A combination is a subset in which order does not matter. The elements of each combination keep their order in the collection. A k above the length gives no combination, and a k of 0 gives one empty combination. The combinations of a string are strings.

combinations([1, 2, 3], 2)
// ➔ [[1,2],[1,3],[2,3]]
combinations("abcd", 2)
// ➔ ["ab","ac","ad","bc","bd","cd"]
[combinations([1, 2, 3], 4), combinations([1, 2, 3], 0)]
// ➔ [[],[[]]]

The result is a lazy collection: count and an index do not list the combinations.

count(combinations(1..52, 5))
// ➔ 2598960
[combinations(1..10, 3)[1], combinations(1..10, 3)[120]]
// ➔ [[1,2,3],[8,9,10]]

See also: Combination.

MathJSON: Combinations

["Combinations", collection, k].

["Combinations", ["List", 1, 2, 3], 2]

fibonacci​

(n: broadcastable<integer>, x: broadcastable<number>?) -> number

  • n — The index; Fibonacci(0) is 0, and a negative index follows F_{-n} = (-1)^{n+1} F_n. Sample: 10.
  • x — The variable of the Fibonacci polynomial; without it the result is the Fibonacci number. Sample: 2.

Compute the nth Fibonacci number. With a second operand, fibonacci(n, x) is the Fibonacci polynomial F_n(x), with F_n(1) = fibonacci(n).

fibonacci(10)
// ➔ 55
fibonacci(5, x)
// ➔ x^4 + 3x^2 + 1

The Fibonacci numbers start with F_0 = 0 and F_1 = 1, and each next number is the sum of the two before it: 0, 1, 1, 2, 3, 5, 8, 13, …

[fibonacci(7), fibonacci(10), fibonacci(100)]
// ➔ [13,55,354224848179261915075]

A negative index follows the standard extension to the negative integers, F_{-n} = (-1)^{n+1} F_n:

[fibonacci(-7), fibonacci(-8)]
// ➔ [13,-21]

The Fibonacci polynomials of fibonacci(n, x) follow the recurrence F_n(x) = x F_{n-1}(x) + F_{n-2}(x), with F_0 = 0 and F_1 = 1. A polynomial of an index above 1000 stays unevaluated:

fibonacci(5, x)
// ➔ x^4 + 3x^2 + 1
fibonacci(1001, x)
// ➔ Fibonacci(1001, x)

See also: Fibonacci number.

MathJSON: Fibonacci

["Fibonacci", n], ["Fibonacci", n, x]. In LaTeX: \mathrm{Fibonacci}(n).

["Fibonacci", 7]
// ➔ 13
["Fibonacci", 5, "x"]
// ➔ ["Add", ["Power", "x", 4], ["Multiply", 3, ["Power", "x", 2]], 1]

integerPartitions​

(n: integer, k: any?, parts: list<integer>?, count: integer?) -> list<list<integer>>

  • n — The integer to partition; a negative one has no partition, and 0 has one, the empty list. Sample: 4.
  • k — The number of parts: k keeps the partitions of at most k parts, [k] exactly k, [kmin, kmax] or [kmin, kmax, step] a range, and All sets no limit. Sample: 2.
  • parts — The positive integers that the parts are drawn from. Sample: [1, 5, 10].
  • count — The number of partitions kept, the first ones. Sample: 3.

Return the partitions of n, each a list of its positive parts in non-increasing order, in reverse lexicographic order. The second operand limits the number of parts: integerPartitions(n, k) keeps those of at most k parts, a list [k] exactly k parts, and [kmin, kmax] or [kmin, kmax, step] a range of numbers of parts (kmax may be Infinity, and all is no limit). IntegerPartitions(n, kspec, [s1, s2, ...]) draws the parts from the given positive integers, and a fourth operand m keeps the first m partitions. There is no partition of a negative n, and the only one of 0 is the empty list.

integerPartitions(4)
// ➔ [[4],[3,1],[2,2],[2,1,1],[1,1,1,1]]
integerPartitions(8, [3])
// ➔ [[6,1,1],[5,2,1],[4,3,1],[4,2,2],[3,3,2]]
count(integerPartitions(100, 100, [1, 5, 10, 25, 50]))
// ➔ 292

A bound on the number of parts, or a part size, that is not an integer leaves the call unevaluated:

integerPartitions(6, 2.5)
// ➔ IntegerPartitions(6, 2.5)
integerPartitions(6, oo, [1, 2], 2)
// ➔ [[2,2,2],[2,2,1,1]]

The result is a lazy collection. count of the partitions of n is nPartition(n), computed without listing the partitions, and an index reads a position directly:

[count(integerPartitions(100)), nPartition(100)]
// ➔ [190569292,190569292]
integerPartitions(100)[1000]
// ➔ [83,8,3,3,3]

See also: Integer partition, nPartition.

MathJSON: IntegerPartitions

["IntegerPartitions", n], ["IntegerPartitions", n, kspec], ["IntegerPartitions", n, kspec, parts], ["IntegerPartitions", n, kspec, parts, m]. In kspec, PositiveInfinity or All is no limit.

["IntegerPartitions", 6, ["List", 2]]

multinomial​

(integer+) -> integer

  • arguments 1, … — The size of one group; the result is (k1 + k2 + ...)! / (k1! k2! ...), and it stays unevaluated when a size is negative. Sample: 2.

Compute the multinomial coefficient for multiple integers.

multinomial(2, 1, 1)
// ➔ 12

The multinomial coefficient of the group sizes k_1, k_2, \ldots is the number of ways to divide a set of n = k_1 + k_2 + \cdots elements into groups of these sizes. It extends the binomial coefficient to more than two groups.

\binom{k_1 + \cdots + k_m}{k_1, \ldots, k_m} = \frac{(k_1 + \cdots + k_m)!}{k_1! \cdots k_m!}
[multinomial(2, 1), multinomial(2, 2, 1), multinomial(1, 1, 1)]
// ➔ [3,30,6]
multinomial(2, 2, 1) == 5! / (2! * 2! * 1!)
// ➔ true

See also: Multinomial theorem.

MathJSON: Multinomial

["Multinomial", k1, k2, ...].

["Multinomial", 2, 1]
// ➔ 3

permutations​

((xs: S, k: integer?) -> list<string> where S: string) & ((n: integer<0..>, k: integer?) -> list<list<integer>>) & ((xs: collection, k: integer?) -> list<list>)

  • xs — The collection to arrange; the elements of a string are its characters, and each arrangement is a string. Sample: [1, 2, 3].
  • k — The length of each arrangement, the whole length by default; 0 gives one empty arrangement, and a length past the size of the collection none. Sample: 2.
  • n — A non-negative integer that stands for the collection Range(1, n). Sample: 3.

Return all permutations of length k (default full length) of a collection. permutations(n) for a non-negative integer n is the permutations of Range(1, n), as lists, and permutations(n, k) its arrangements of length k.

permutations([1, 2, 3])
// ➔ [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
permutations(3, 2)
// ➔ [[1,2],[1,3],[2,1],[2,3],[3,1],[3,2]]

A permutation is an ordered arrangement of elements. Without k, the permutations have the length of the collection. A k above that length gives no permutation, and a k of 0 gives one empty permutation. The permutations of a string are strings. Equal elements are not merged: each arrangement is listed once for each position the elements come from.

permutations([1, 2, 3])
// ➔ [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
permutations("abc")
// ➔ ["abc","acb","bac","bca","cab","cba"]
[permutations([1, 2, 3], 4), permutations([1, 2, 3], 0)]
// ➔ [[],[[]]]
permutations([1, 1, 2])
// ➔ [[1,1,2],[1,2,1],[1,1,2],[1,2,1],[2,1,1],[2,1,1]]

The result is a lazy collection: count, an index and indexOf compute their answer from the position, without listing the arrangements, so they work on collections with factorially many elements:

[count(permutations(1..20)), count(permutations(1..10, 3))]
// ➔ [2432902008176640000,720]
indexOf(permutations([1, 2, 3]), [2, 3, 1])
// ➔ 4

See also: Permutation.

MathJSON: Permutations

["Permutations", collection], ["Permutations", collection, k].

["Permutations", ["List", 1, 2, 3], 2]

pochhammer​

MathJSON Pochhammer · (x: broadcastable<complex | infinity>, n: broadcastable<complex | infinity>) -> number

  • x — The first factor of the product. Sample: 3.
  • n — The number of factors; 0 gives 1, and a negative or non-integer value extends the product through the Gamma function. Sample: 4.

Rising factorial (Pochhammer symbol) (x)_n = x(x+1)…(x+n-1).

pochhammer(3, 4)
// ➔ 360
pochhammer(x, 3)
// ➔ x * (x + 1) * (x + 2)

powerSet​

(set<any>) -> set

  • argument 1 — The set whose subsets are listed; the power set of the empty set holds the empty set alone. Sample: Set(1, 2, 3).

Return the power set of a set (set of all subsets).

count(powerSet(set(1, 2, 3)))
// ➔ 8

The power set of a set is the set of all its subsets, including the empty set and the set itself. A set of n elements has 2^n subsets.

The power set is a lazy collection: it stays as written, and count and in answer without listing the 2^n subsets. A subset is a member whatever the order of its elements:

[count(powerSet(set(1, 2, 3))), count(powerSet(set()))]
// ➔ [8,1]
[set(2, 1) in powerSet(set(1, 2, 3)), set(4) in powerSet(set(1, 2, 3))]
// ➔ [true,false]

See also: Power set.

MathJSON: PowerSet

["PowerSet", set]. The empty subset is the symbol EmptySet. In LaTeX: \mathrm{PowerSet}(A); \mathcal{P}(A) is not read as a power set.

["Count", ["PowerSet", ["Set", "a", "b"]]]
// ➔ 4

subfactorial​

(broadcastable<integer>) -> integer

  • argument 1 — The number of items; Subfactorial(0) is 1, and a negative integer stays unevaluated. Sample: 4.

Compute the number of derangements (subfactorial) of n items.

subfactorial(4)
// ➔ 9

A derangement is a permutation in which no element stays in its original position, as in the hat-check problem. The number of derangements of n elements is

!n = n! \sum_{k=0}^n \frac{(-1)^k}{k!}
[subfactorial(4), subfactorial(5), subfactorial(0), subfactorial(1)]
// ➔ [9,44,1,0]
subfactorial(5) == 5! * (1 - 1 + 1/2 - 1/6 + 1/24 - 1/120)
// ➔ true

A negative or symbolic n stays unevaluated:

[subfactorial(-1), subfactorial(n)]
// ➔ [Subfactorial(-1),Subfactorial(n)]

See also: Derangement.

MathJSON: Subfactorial

["Subfactorial", n]. The LaTeX !n is not read as a subfactorial.

["Subfactorial", 4]
// ➔ 9