The most common way to represent a polynomial is writing it as a linear combination of monomials, i.e., powers of the variable. For example, the polynomial \$p(x) = x^3 + 2x^2 + x + 1\$ is a linear combination of the monomials \$x^3\$, \$x^2\$, \$x\$, and \$1\$, with coefficients \$1\$, \$2\$, \$1\$, and \$1\$ respectively.
However, the monomial basis is not the only possible basis for polynomials. Another interesting basis, useful in combinatorics and some other areas of mathematics, is the falling factorial basis. The falling factorial of degree \$k\$ is defined as:
$$(x)_k = x (x - 1) (x - 2) \ldots (x - k + 1) = \prod_{i=0}^{k-1} (x - i).$$
In particular, \$(x)_0 = 1\$.
Any polynomial \$p(x)\$ of degree \$n\$ can be uniquely expressed as a linear combination of falling factorials \$(x)_0, (x)_1, \ldots, (x)_n\$. For example, the polynomial \$p(x) = x^3 + x^2 + x + 1\$ can be expressed in the falling factorial basis as:
$$p(x) = (x)_3 + 4 (x)_2 + 3 (x)_1 + (x)_0 = x (x - 1) (x - 2) + 4 x (x - 1) + 3 x + 1.$$
A possible way to find the coefficients in the falling factorial basis is using the Stirling numbers of the second kind. In fact,
$$x^n = \sum_{k=0}^{n} S(n, k) (x)_k,$$
where \$S(n, k)\$ is the Stirling number of the second kind, which counts the number of ways to partition a set of \$n\$ items into \$k\$ non-empty subsets.
Task
In this challenge, you are given a polynomial \$p(x)\$, and you need to find its representation in the falling factorial basis. Specifically, you need to find the coefficients \$c_0, c_1, \ldots, c_n\$ such that:
$$p(x) = c_n (x)_n + c_{n-1} (x)_{n-1} + \ldots + c_1 (x)_1 + c_0 (x)_0.$$
You may take the input polynomial \$p(x)\$ in any reasonable format. For example, the polynomial \$x^4 - 4x^3 + 5x^2 - 2x\$ may be represented as:
- a list of coefficients, in descending order:
[1,-4,5,-2,0]; - a list of coefficients, in ascending order:
[0,-2,5,-4,1]; - a string representation of the polynomial, with a chosen variable, e.g.,
"x^4-4*x^3+5*x^2-2*x"; - a built-in polynomial object, e.g.,
x^4-4*x^3+5*x^2-2*xin PARI/GP.
You may take the degree \$n\$ or the number of coefficients \$n + 1\$ as an additional input.
You may output the list of coefficients \$[c_n, c_{n-1}, \ldots, c_1, c_0]\$ in either ascending or descending order. You may also take an additional input \$k\$, and output the \$k\$-th coefficient \$c_k\$ only.
This is code-golf, so the shortest code in bytes wins.
Test cases
Here the input polynomials are represented as lists of coefficients in descending order. The output are also in descending order.
[1] -> [1] # 1 => (x)_0
[1, 1] -> [1, 1] # x + 1 => (x)_1 + (x)_0
[1, 0, 0] -> [1, 1, 0] # x^2 => (x)_2 + (x)_1
[1, 2, 1] -> [1, 3, 1] # x^2 + 2 * x + 1 => (x)_2 + 3 * (x)_1 + (x)_0
[1, 1, 1, 1] -> [1, 4, 3, 1] # x^3 + x^2 + x + 1 => (x)_3 + 4 * (x)_2 + 3 * (x)_1 + (x)_0
[1, 2, 3, 4] -> [1, 5, 6, 4] # x^3 + 2 * x^2 + 3 * x + 4 => (x)_3 + 5 * (x)_2 + 6 * (x)_1 + 4 * (x)_0
[1, -1, 1, -1, 1] -> [1, 5, 5, 0, 1] # x^4 - x^3 + x^2 - x + 1 => (x)_4 + 5 * (x)_3 + 5 * (x)_2 + (x)_0
[1, 0, -2, 0, 1] -> [1, 6, 5, -1, 1] # x^4 - 2 * x^2 + 1 => (x)_4 + 6 * (x)_3 + 5 * (x)_2 - (x)_1 + (x)_0
[5, 4, 3, 2, 1] -> [5, 34, 50, 14, 1] # 5 * x^4 + 4 * x^3 + 3 * x^2 + 2 * x + 1 => 5 * (x)_4 + 34 * (x)_3 + 50 * (x)_2 + 14 * (x)_1 + (x)_0
[1, 0, -2, 0, 1] -> [1, 6, 5, -1, 1]\$\endgroup\$