Stirling numbers
#include <flint/arith.h>Set to , or , for any . These functions compute an isolated value, with algorithms chosen according to and (binary splitting, power sums, power series and multimodular methods).
Set row to the values for of , or . A full row has ; entries with are set to zero.
void arith_stirling_matrix_1u(fmpz_mat_t mat)sourcevoid arith_stirling_matrix_1(fmpz_mat_t mat)sourcevoid arith_stirling_matrix_2(fmpz_mat_t mat)sourceSet the entry of the matrix mat to , or , for and ; the entries above the diagonal are zero. The square matrices of and are inverses of each other. Wrappers of gr_mat_stirling, which fills the rows with the triangular recurrences.
The Stirling numbers of the first kind connect the falling and rising factorials with the powers of , and those of the second kind the powers with the falling factorials:
The unsigned numbers are the coefficients of the rising factorial and count the permutations of elements with cycles; counts the partitions of an -set into blocks [Comtet1974]. All of them are zero for , and , for .
The module offers three interfaces, which use different algorithms:
arith_stirling_number_1u,arith_stirling_number_1andarith_stirling_number_2compute one value;- the
_vecfunctions compute a row for fixed ; - the
matrixfunctions fill a whole table of rows .
To compute many values, use the row or matrix functions: they cost about as much as a few isolated values.
AlgorithmStirling and Bell numbersIsolated Stirling numbers by binary splitting of rising factorials, power sums or power series, chosen by k/n; rows by convolution; Bell numbers by Dobiński's formula, a multimodular sum, the Bell triangle or generating functions modulo primes.Isolated values#
The values with near 0 or near are cheap: , , . In general, the functions choose by and the ratio :
- is a coefficient of the rising factorial, which
arith_stirling_number_1uexpands by binary splitting truncated to the needed length: as to length for , and as to length otherwise, with only the wanted coefficient extracted from the last product. For and it uses instead the exponential generating function . is computed by
arith_stirling_number_2from the triangular recurrence in one or two words for (on 64-bit machines), and otherwise from one ofthe power sum exactly for small (below about , and at most 0.8), the power series for above about , and the power sum modulo word-size primes with Chinese remaindering in between.
The signed version arith_stirling_number_1 is the unsigned one with the sign .
Rows#
A full row of is the coefficient list of the rising factorial, which arith_stirling_number_1u_vec computes by binary splitting with fast polynomial multiplication, truncated to klen coefficients. The row of comes from the triangular recurrence for ; for larger , it is the convolution
a polynomial product computed exactly over (with a common denominator) for and modulo word-size primes, followed by Chinese remaindering, otherwise. klen may exceed ; the extra entries are zero.
Matrices#
arith_stirling_matrix_1u, arith_stirling_matrix_1 and arith_stirling_matrix_2 are wrappers of gr_mat_stirling, which fills row from row with the recurrences
The matrix may have any shape; its entries above the diagonal are zero. By the defining identities, the square matrices of and are inverses of each other.
Example#
#include <flint/flint.h>
#include <flint/fmpz.h>
#include <flint/fmpz_vec.h>
#include <flint/fmpz_mat.h>
#include <flint/arith.h>
int main(void)
{
fmpz_t s;
fmpz * row;
fmpz_mat_t A, B, C;
slong k;
fmpz_init(s);
arith_stirling_number_1(s, 10, 3);
flint_printf("s(10,3) = ");
fmpz_print(s);
arith_stirling_number_2(s, 10, 3);
flint_printf(", S(10,3) = ");
fmpz_print(s);
flint_printf("\n");
row = _fmpz_vec_init(7);
arith_stirling_number_2_vec(row, 6, 7);
flint_printf("S(6,k):");
for (k = 0; k < 7; k++)
{
flint_printf(" ");
fmpz_print(row + k);
}
flint_printf("\n");
arith_stirling_number_2(s, 2000, 1000);
flint_printf("S(2000,1000) has %wu bits\n", fmpz_bits(s));
fmpz_mat_init(A, 8, 8);
fmpz_mat_init(B, 8, 8);
fmpz_mat_init(C, 8, 8);
arith_stirling_matrix_1(A);
arith_stirling_matrix_2(B);
fmpz_mat_mul(C, A, B);
flint_printf("s S = I: %d\n", fmpz_mat_is_one(C));
fmpz_mat_clear(A);
fmpz_mat_clear(B);
fmpz_mat_clear(C);
_fmpz_vec_clear(row, 7);
fmpz_clear(s);
return 0;
}s(10,3) = -1172700, S(10,3) = 9330 S(6,k): 0 1 31 90 65 15 1 S(2000,1000) has 11144 bits s S = I: 1
References#
- [Comtet1974] L. Comtet, Advanced Combinatorics: The Art of Finite and Infinite Expansions, D. Reidel, 1974.
The FLINT team, “Stirling numbers (arith.h),” FLINT: Fast Library for Number Theory, documentation, version 3.7.0-dev, 2026. https://flintlib.org/doc/arith/stirling.html (generated 2026-11-02).
@misc{flint-doc-arith-stirling,
author = {{The FLINT team}},
title = {{Stirling numbers (arith.h)}},
howpublished = {FLINT: Fast Library for Number Theory, documentation, version 3.7.0-dev},
year = {2026},
url = {https://flintlib.org/doc/arith/stirling.html},
note = {Generated 2026-11-02}
}