Introduction to Polynomials and Generating Functions

Polynomials and generating functions

Manipulating finite- and infinite-term polynomials is an important topic in OI mathematics, especially in the study of generating functions.

Polynomial algorithms based on the fast Fourier transform give algorithm-competition contestants the ability to manipulate generating functions directly.

Basic concepts

For the sum ∑anxn\sum a_nx^n, if it has finitely many terms, it is called a polynomial and denoted by f(x)=∑n=0manxnf(x)=\sum_{n=0}^m a_nx^n.

A sum with countably many terms is called a series. In the sum ∑n=0∞anxn\sum_{n=0}^\infty a_nx^n, every term is a constant coefficient times a nonnegative integer power of the variable; a series of this form is called a power series.

When studying polynomial arithmetic, we first consider the simpler polynomial case; the concept of a power series is used only for convenience. Mathematical analysis further studies the convergence and divergence of power series.

For general definitions of rings, fields, and their derived structures, see Basic concepts of abstract algebra.

For a general ring RR, define the polynomial ring over RR, denoted R[x]R[x].

Each element ff is called a polynomial over RR and can be written as

f=<f0,f1,f2,⋯ ,fn>(f0,f1,f2,⋯ ,fn∈R)f=\left<f_0,f_1,f_2,\cdots,f_n\right>\quad(f_0,f_1,f_2,\cdots,f_n\in R)

In other words, we define a polynomial directly as a sequence of coefficients. It can also be written as

f(x)=f0+f1x+f2x2+⋯+fnxnf(x)=f_0+f_1x+f_2x^2+\cdots+f_nx^n

Here, xx is regarded merely as a formal symbol, an identifier for the positions of the coefficients.

If we also allow infinitely many terms, namely

f(x)=f0+f1x+f2x2+⋯f(x)=f_0+f_1x+f_2x^2+\cdots

we obtain the formal power series ring R[[x]]R[[x]]. Each element ff is called a formal power series, abbreviated to power series below.

Degree of a polynomial

For a polynomial f(x)f(x), the degree of its highest-degree term is called the degree of the polynomial, denoted by deg⁡f\operatorname{deg}{f}.

Polynomial multiplication

The most fundamental operation is multiplying two polynomials. Given polynomials f(x)f(x) and g(x)g(x):

f(x)=a0+a1x+⋯+anxn(1)g(x)=b0+b1x+⋯+bmxm(2)\begin{alignedat}{3} f(x)&=a_0+a_1x+\dots+a_nx^n\quad \quad &(1)\\ g(x)&=b_0+b_1x+\dots+b_mx^m\quad \quad &(2) \end{alignedat}

To compute the polynomial Q(x)=f(x)⋅g(x)Q(x)=f(x)\cdot g(x):

Q(x)=∑i=0n∑j=0maibjxi+j=c0+c1x+⋯+cn+mxn+m\boxed {Q(x) = \sum \limits_ {i = 0} ^ n \sum \limits_ {j = 0 } ^ m a_i b_j x ^ {i + j}} = c_0 + c_1 x + \dots + c_ {n + m} x ^ {n + m}

Multiplication of polynomials or power series is associative and distributes over addition. If RR is a commutative ring or a ring with identity, multiplication also has commutativity and an identity element, respectively.

If RR contains a 2n2^n-th root of unity, the fast Fourier transform allows us to compute, in O(n2n)O(n2^n) time instead of O(22n)O(2^{2n}) time, the product of two degree-2n2^n polynomials.

Composition

In R[[x]]R[[x]], define the powers of an element ff by

f1=f,fk=fk−1×ff^1=f,f^k=f^{k-1}\times f

Based on this, for elements of R[[x]]R[[x]] denoted f,gf,g, define their composition by

(f∘g)(x)=f(g(x))=f0+∑k=1+∞fkgk(x)(f\circ g)(x)=f(g(x))=f_0+\sum_{k=1}^{+\infty}f_kg^k(x)

We stipulate that f∘gf\circ g exists if and only if ff has finitely many terms or g0=0g_0=0, so no limits over RR are involved.

∘\circ is associative (whenever both (f∘g)∘h(f\circ g)\circ h and f∘(g∘h)f\circ (g\circ h) exist), but not commutative. When RR is a ring with identity, ∘\circ has the identity element 1×x1\times x.

Polynomial composition (and compositional inversion) can be performed in Θ(nlog⁡2n)\Theta(n\log^2 n) time. The method was proposed by Yasunori Kinoshita and Li Baitian in 2024; see Formal power series composition | compositional inversion.

Derivative

Although a general ring may not even have limits,
we can still define the formal derivative of a formal power series as

(∑k=0+∞fkxk)′=∑k=1+∞kfkxk−1\left(\sum_{k=0}^{+\infty}f_kx^k\right)'=\sum_{k=1}^{+\infty}kf_kx^{k-1}

where

kfk=fk+fk+⋯+fk⏟k个fkkf_k=\underbrace{f_k+f_k+\cdots+f_k}_{k \text{个} f_k}

The basic differentiation rules—the sum rule, product rule, and chain rule (when composition is allowed)—remain valid.

If division is allowed in RR, we can similarly define the formal indefinite integral of a formal power series.

Multiplicative inverse

From the example

11−x=1+x+x2+⋯\dfrac{1}{1-x}=1+x+x^2+\cdots

we see that the reciprocal of a polynomial can be expanded as an infinite series. A reciprocal exists if and only if the constant term is not 00, and its reciprocal also has a constant term not equal to 00.

Thus, for a formal power series ff with f0≠0f_0\not=0, define its multiplicative inverse f−1f^{-1} to be another formal power series satisfying

f×f−1=f−1×f=1f\times f^{-1}=f^{-1}\times f=1

Expanding this equation using formal power series multiplication gives the recurrence for the coefficients of f−1f^{-1}:

f0−1=1f0,fn−1=−1f0∑k=0n−1fk−1fn−kf^{-1}_0=\dfrac{1}{f_0},f^{-1}_n=\dfrac{-1}{f_0}\sum_{k=0}^{n-1}f^{-1}_kf_{n-k}

Computing the first nn terms directly from the recurrence takes O(n2)O(n^2) time; using the FFT gives an O(nlog⁡n)O(n\log n) algorithm.

Note

It is easy to see that the reciprocal of f(x)f(x) is the infinite Maclaurin expansion of 1f(x)\frac{1}{f(x)} (the Taylor expansion at x=0x=0).

Common power-series expansions

In mathematical analysis, a univariate function that is differentiable to some order at a point or on an interval can be expanded into a polynomial over the corresponding range. This is generally called a Taylor expansion; an expansion at 00 is also called a Maclaurin expansion.

If a function is infinitely differentiable, it can be expanded as a power series. The most common expansion point is still 00.

In complex analysis, some functions cannot be Taylor-expanded at a singularity but can be expanded in a Laurent series.

The following equalities hold only when the power series converge; they do not hold when the series diverge. We give only the expansions and do not discuss their domains of convergence.

The two basic expansions are those of the exponential and power functions:

ex=1+x+12!x2+…+1n!xn+…\mathrm{e}^x=1+x+\frac{1}{2!}x^2+\ldots+\frac{1}{n!}x^n+\ldots (1+x)a=1+ax+a(a−1)2!x2+…+a(a−1)…(a−n+1)n!xn+…(1+x)^a=1+ax+\frac{a(a-1)}{2!}x^2+\ldots+\frac{a(a-1)\ldots(a-n+1)}{n!}x^n+\ldots

More expansions can often be derived by transforming these two. For example, the trigonometric functions are obtained by substituting complex numbers into the exponential function:

cos⁡x=1−12!x2+14!x4+…+(−1)n(2n)!x2n+…\cos x=1-\frac{1}{2!}x^2+\frac{1}{4!}x^4+\ldots+\frac{(-1)^n}{(2n)!}x^{2n}+\ldots sin⁡x=x−13!x3+15!x5+…+(−1)n(2n+1)!x2n+1+…\sin x=x-\frac{1}{3!}x^3+\frac{1}{5!}x^5+\ldots+\frac{(-1)^n}{(2n+1)!}x^{2n+1}+\ldots

The logarithm, arctangent, and arcsine functions are obtained by integration:

11+x=1−x+x2+…+(−1)nxn+…\frac{1}{1+x}=1-x+x^2+\ldots+{(-1)}^n x^n+\ldots ln⁡(1+x)=x−12x2+13x3+…+(−1)n−1nxn+…\ln(1+x)=x-\frac{1}{2}x^2+\frac{1}{3}x^3+\ldots+\frac{{(-1)}^{n-1}}{n}x^n+\ldots 11+x2=1−x2+x4+…+(−1)nx2n+…\frac{1}{1+x^2}=1-x^2+x^4+\ldots+{(-1)}^n x^{2n}+\ldots arctan⁡x=x−13x3+15x5+…+(−1)n2n+1x2n+1+…\arctan x=x-\frac{1}{3}x^3+\frac{1}{5}x^5+\ldots+\frac{{(-1)}^{n}}{2n+1}x^{2n+1}+\ldots 11+x=1−12x+38x2+…+(−1)n(2n)!(n!)24nxn+…\frac{1}{\sqrt{1+x}}=1-\frac{1}{2}x+\frac{3}{8}x^2+\ldots+{(-1)}^n\frac{(2n)!}{{(n!)}^2 4^n} x^{n}+\ldots 11−x2=1+12x2+38x4+…+(2n)!(n!)24nx2n+…\frac{1}{\sqrt{1-x^2}}=1+\frac{1}{2}x^2+\frac{3}{8}x^4+\ldots+\frac{(2n)!}{{(n!)}^2 4^n} x^{2n}+\ldots arcsin⁡x=x+16x3+340x5+…+(2n)!(n!)2(2n+1)4nx2n+1+…\arcsin x=x+\frac{1}{6}x^3+\frac{3}{40}x^5+\ldots+\frac{(2n)!}{{(n!)}^2(2n+1)4^n} x^{2n+1}+\ldots

Compositional inverse

Compositional inversion is the extension of the concept of an inverse function to the formal power series ring.

For a formal power series with f0=0f_0=0 and f1≠0f_1\not=0, denoted ff, its compositional inverse is the formal power series satisfying g(f(x))=f(g(x))=xg(f(x))=f(g(x))=x, denoted gg. Lagrange inversion gives, for any integers n,kn,k,

n[xn]fk=k[xn−k](xg)nn[x^n]f^k=k[x^{n-k}]\left(\dfrac{x}{g}\right)^n

Here, [xk]f(x)[x^k]f(x) denotes the coefficient of f(x)f(x) at xkx^k.

Polynomial divisibility

For polynomials f(x)f(x) and g(x)g(x), if there exists a polynomial h(x)h(x) such that

f(x)=g(x)h(x)f(x)=g(x)h(x)

then g(x)g(x) divides f(x)f(x).

It is clear that g(x)g(x) divides f(x)f(x) if and only if every root of g(x)g(x) is a root of f(x)f(x), with multiplicity in g(x)g(x) no greater than the corresponding multiplicity in f(x)f(x).

Polynomial remainders and quotients

For polynomials f(x),g(x)f(x), g(x), there are unique polynomials Q(x),R(x)Q(x), R(x) satisfying

f(x)=Q(x)g(x)+R(x)deg⁡R<deg⁡g\begin{aligned} f(x) &= Q(x) g(x) + R(x) \\ \operatorname{deg}{R} &< \operatorname{deg}{g} \end{aligned}

When deg⁡f≥deg⁡g\operatorname{deg}{f} \ge \operatorname{deg}{g}, we have deg⁡Q=deg⁡f−deg⁡g\operatorname{deg}{Q} = \operatorname{deg}{f} - \operatorname{deg}{g}; otherwise, Q(x)=0Q(x) = 0. We call Q(x)Q(x) the quotient, with g(x)g(x) as the divisor and f(x)f(x) as the dividend, and call R(x)R(x) the remainder, with g(x)g(x) as the divisor and f(x)f(x) as the dividend.

Polynomial congruences

Polynomial congruence is the subring of a polynomial ring obtained by quotienting the polynomial ring by the equivalence relation of congruence.

In the polynomial division with remainder discussed above, f(x)f(x) and its remainder R(x)R(x) are congruent modulo the polynomial g(x)g(x).

f(x)≡R(x)(modg(x))f(x) \equiv R(x) \pmod{g(x)}

This congruence also means that, for the polynomial g(x)g(x), substituting any root x0x_0 into f(x)f(x) and R(x)R(x) gives the same value:

f(x0)=R(x0)f(x_0)=R(x_0)

Moreover, if x0x_0 is a root of the polynomial g(x)g(x) with multiplicity kk, meaning that (x−x0)k(x-x_0)^k divides g(x)g(x), then for every integer at least 00 and less than kk, denoted by tt,

ft(x0)=Rt(x0)f^{t}(x_0)=R^{t}(x_0)

Here, the notation denotes the tt-th derivative.

Polynomial congruence can also be applied to power series. An infinite power series can be congruent to a finite polynomial modulo a specific polynomial. For example:

1+x+x2+x3+…≡1+x+…+xn−1(modxn)1+x+x^2+x^3+\ldots \equiv 1+x+\ldots+x^{n-1} \pmod{x^n}

Clearly, all remaining terms are divisible by xnx^n. Therefore, operating modulo xnx^n is equivalent to truncation: truncate the power series to its first nn terms, directly discarding all higher-order information.

In some specific cases, we can also work modulo other polynomials; the corresponding cases are explained below.

Multi-point evaluation and interpolation of polynomials

Multi-point evaluation of a polynomial means, given a polynomial f(x)f(x) and nn points x1,x2,…,xnx_{1}, x_{2}, \dots, x_{n}, computing

f(x1),f(x2),…,f(xn)f(x_{1}), f(x_{2}), \dots, f(x_{n})

Polynomial interpolation means, given n+1n+1 points

(x0,y0),(x1,y1),…,(xn,yn)(x_{0}, y_{0}), (x_{1}, y_{1}), \dots, (x_{n}, y_{n})

finding an nn-th degree polynomial f(x)f(x) such that all n+1n+1 points lie on f(x)f(x).

The essence of these two operations is converting a polynomial between its coefficient representation and point-value representation. Multi-point evaluation converts the coefficient representation into the point-value representation, while interpolation converts the point-value representation into the coefficient representation.

Note

From the viewpoint of power series, multi-point evaluation compresses the information in infinitely many terms into finitely many point values, and therefore loses some information; interpolation restores the coefficient representation up to the corresponding degree.

In common programming applications of evaluation and interpolation, such as the discrete Fourier transform and its inverse, the selected n+1n+1 points all have multiplicity 11, that is, they are pairwise distinct, avoiding the need for derivatives.

This kind of compression guarantees agreement only at the n+1n+1 selected points. According to the explanation of polynomial congruence above, if a power series f(x)f(x) is evaluated at points x0x_0 through xnx_n and then interpolated to obtain a polynomial R(x)R(x), define

g(x)=(x−x0)…(x−xn)g(x)=(x-x_0)\ldots(x-x_n)

Then

f(x)≡R(x)(modg(x))f(x) \equiv R(x) \pmod{g(x)}

Since the degree of R(x)R(x) is strictly less than that of g(x)g(x), the R(x)R(x) obtained through evaluation and interpolation is the remainder. Thus, in this situation, if the power series can be evaluated at the roots, it can be reduced modulo a polynomial. A counterexample is

11−x=1+x+x2+x3+…\frac{1}{1-x}=1+x+x^2+x^3+\ldots

It cannot be evaluated at x=1x=1, so the series 1+x+x2+x3+…1+x+x^2+x^3+\ldots cannot be reduced modulo the polynomial x−1x-1.

Since every derivative of a power series has a value at 00, reduction modulo xnx^n can always be computed, consistent with the meaning of truncation above. The discrete Fourier transform and its inverse correspond to reduction modulo the polynomial xn−1x^n-1.

Factorization and Euclidean algorithm

Many results from elementary number theory generalize to polynomials.

Over the complex numbers, the fundamental theorem of algebra implies that for an nn-th degree polynomial ff, the equation

f(x)=0f(x)=0

has exactly nn solutions, counting repeated roots according to their multiplicity.

Therefore, over the complex numbers, f(x)f(x) has the unique factorization

a(x−x1)c1(x−x2)c2⋯(x−xm)cma(x-x_1)^{c_1}(x-x_2)^{c_2}\cdots(x-x_m)^{c_m} c1+c2+⋯+cm=n,x1,x2,⋯ ,xm 互不相同c_1+c_2+\cdots+c_m=n,x_1,x_2,\cdots,x_m \text{ 互不相同}

Analogous to the greatest common divisor of positive integers, this gives the polynomial greatest common divisor (gcd). It can be computed with the Euclidean algorithm:

gcd⁡(f,0)=f,gcd⁡(f,g)=gcd⁡(g,f mod g)\gcd(f,0)=f,\gcd(f,g)=\gcd(g,f\bmod g)

This property generalizes to a more general setting:

For any field PP, the polynomial ring P[x]P[x] satisfies:
every polynomial has a unique factorization, and the greatest common divisor can be computed with the Euclidean algorithm.
Note that this conclusion need not hold for polynomials over a general ring.

When the Euclidean algorithm applies, the extended Euclidean algorithm gives the following Diophantine equation

f(x)P(x)+g(x)Q(x)=gcd⁡(f(x),g(x))f(x)P(x)+g(x)Q(x)=\gcd(f(x),g(x))

A particular solution is (P(x),Q(x))(P(x),Q(x)), and Bézout's identity can be used to determine whether the Diophantine equation

f(x)P(x)+g(x)Q(x)=h(x)f(x)P(x)+g(x)Q(x)=h(x)

is solvable.

HALF-GCD allows polynomial Euclidean algorithms to be computed in O(nlog⁡2n)O(n\log^2 n) time.

Multiplicative inverse modulo a polynomial

Modulo a polynomial h(x)h(x), a power series f(x)f(x) sometimes has an inverse. The inverse is the remainder obtained by reducing the reciprocal of f(x)f(x) modulo h(x)h(x).

Equivalently, for a polynomial f(x)f(x), if there exists a g(x)g(x) satisfying

f(x)g(x)≡1(modh(x))\begin{aligned} f(x) g(x) & \equiv 1 \pmod{h(x)} \end{aligned}

then g(x)g(x) is called the inverse element of f(x)f(x) modulo h(x)h(x). When the polynomial Euclidean algorithm applies, the inverse exists if and only if gcd⁡(f,g)=1\gcd(f,g)=1.

The inverse modulo a polynomial h(x)h(x) is always unique. If the degree of f(x)f(x) is also less than that of h(x)h(x), then the resulting g(x)g(x) and f(x)f(x) are mutual inverses.

In terms of truncation, the inverse modulo xnx^n is generally denoted by f−1(x)f^{-1}(x), and this is also the default meaning of inverse below. Unless otherwise specified, the modulus of an inverse is xnx^n.

Note

One might ask whether various interpolation transforms can be used to solve for the inverse directly, for example by computing:

IDFT(DFT(1)DFT(f(x)))IDFT\left(\frac{DFT(1)}{DFT(f(x))}\right)

The answer is no. According to the explanation above, the inverse obtained directly using the discrete Fourier transform and its inverse here is the inverse modulo the polynomial xn−1x^n-1, not the usual inverse modulo xnx^n. Moreover, because the original polynomial may be 00 at some point values, this computation may not be possible.

Generating functions

A generating function, also called a mother function, is a formal power series whose coefficients can provide information about a sequence.

Generating functions come in many kinds, but most can be written in the unified form

F(x)=∑nankn(x)F(x)=\sum_n a_nk_n(x)

where kn(x)k_n(x) is called the kernel function. Different kernel functions lead to different generating functions with different properties. For example:

  1. Ordinary generating function: kn(x)=xnk_n(x)=x^n.
  2. Exponential generating function: kn(x)=xnn!k_n(x)=\dfrac{x^n}{n!}.
  3. Dirichlet generating function: kn(x)=1nxk_n(x)=\dfrac{1}{n^x}.

References and further reading