← 学习库 Discrete Mathematics (Levin) · 中英对照 目录

5.1 Generating Functions 生成函数

本页译自 LibreTexts · Discrete Mathematics (Levin) 第 5.1 章。公式经 MathJax 渲染,自定义宏已注入。

There is an extremely powerful tool in discrete mathematics used to manipulate sequences called the generating function. The idea is this: instead of an infinite sequence (for example: $2, 3, 5, 8, 12, \ldots$) we look at a single function which encodes the sequence. But not a function which gives the $n$th term as output. Instead, a function whose power series (like from calculus) "displays" the terms of the sequence. So for example, we would look at the power series $2 + 3x + 5x^2 + 8x^3 + 12x^4 + \cdots$ which displays the sequence $2, 3, 5, 8, 12, \ldots$ as coefficients.

离散数学中有一件极其有力的工具,用来处理数列,称为生成函数(generating function)。其思路是:不去直接看一个无穷数列(例如 $2, 3, 5, 8, 12, \ldots$),而是考察一个把该数列编码进自身的函数。不过,这个函数并不以输出第 $n$ 项为目的;相反,它是一个幂级数(与微积分中的幂级数类似),把数列的各项「展示」出来。举例来说,我们会去考察幂级数 $2 + 3x + 5x^2 + 8x^3 + 12x^4 + \cdots$,它把数列 $2, 3, 5, 8, 12, \ldots$ 的各项当作系数展示出来。

An infinite power series is simply an infinite sum of terms of the form $c_nx^n$ were $c_n$ is some constant. So we might write a power series like this:

无穷幂级数不过是关于 $c_nx^n$ 这类项的一个无穷和,其中 $c_n$ 为某常数。因此我们可以把一个幂级数写成如下形式:

\begin{equation*} \sum_{k=0}^\infty c_k x^k. \end{equation*}

即 $\sum_{k=0}^\infty c_k x^k$,其中 $k$ 从 $0$ 取到无穷,各项系数为 $c_k$。

or expanded like this

或者按各项展开写成:

\begin{equation*} c_0 + c_1x + c_2x^2 + c_3x^3 + c_4x^4 + c_5x^5 + \cdots. \end{equation*}

也就是 $c_0 + c_1x + c_2x^2 + c_3x^3 + c_4x^4 + c_5x^5 + \cdots$。

When viewed in the context of generating functions, we call such a power series a generating series. The generating series generates the sequence

在生成函数的语境下,我们把这样的幂级数称为生成级数(generating series)。该生成级数所生成的数列为

\begin{equation*} c_0, c_1, c_2, c_3, c_4, c_5, \ldots. \end{equation*}

即 $c_0, c_1, c_2, c_3, c_4, c_5, \ldots$。

In other words, the sequence generated by a generating series is simply the sequence of coefficients of the infinite polynomial.

换言之,一个生成级数所生成的数列,就是这个无穷多项式的系数(coefficients)数列。

Example 1

示例 1

What sequence is represented by the generating series $3 + 8x^2 + x^3 + \frac{x^5}{7} + 100x^6 + \cdots\text{?}$

生成级数 $3 + 8x^2 + x^3 + \frac{x^5}{7} + 100x^6 + \cdots$ 表示哪个数列?

Solution

解答

We just read off the coefficients of each $x^n$ term. So $a_0 = 3$ since the coefficient of $x^0$ is 3 ($x^0 = 1$ so this is the constant term). What is $a_1\text{?}$ It is NOT 8, since 8 is the coefficient of $x^2\text{,}$ so 8 is the term $a_2$ of the sequence. To find $a_1$ we need to look for the coefficient of $x^1$ which in this case is 0. So $a_1 = 0\text{.}$ Continuing, we have $a_2 = 8\text{,}$ $a_3 = 1\text{,}$ $a_4 = 0\text{,}$ and $a_5 = \frac{1}{7}\text{.}$ So we have the sequence

我们只需读出每个 $x^n$ 项前面的系数。由于 $x^0$ 的系数是 3(而 $x^0 = 1$,故这就是常数项),所以 $a_0 = 3$。那么 $a_1$ 是多少?它不是 8,因为 8 是 $x^2$ 的系数,即它是数列中的第 $a_2$ 项。要找 $a_1$,我们得看 $x^1$ 的系数,而此处它为 0,故 $a_1 = 0$。继续下去,有 $a_2 = 8\text{、}$ $a_3 = 1\text{、}$ $a_4 = 0\text{、}$ $a_5 = \frac{1}{7}$。于是我们得到数列

\begin{equation*} 3, 0, 8, 1, \frac{1}{7}, 100, \ldots \end{equation*}

$3, 0, 8, 1, \frac{1}{7}, 100, \ldots$(注意 $x^4$ 项缺失,故其系数为 0,而 $x^6$ 的系数为 100)。

Note that when discussing generating functions, we always start our sequence with $a_0\text{.}$

注意,在讨论生成函数时,我们的数列总是从 $a_0$ 开始。

Now you might very naturally ask why we would do such a thing. One reason is that encoding a sequence with a power series helps us keep track of which term is which in the sequence. For example, if we write the sequence $1, 3, 4, 6, 9, \ldots, 24, 41,\ldots$ it is impossible to determine which term $24$ is (even if we agreed that the first term was supposed to be $a_0$. However, if we wrote the generating series instead, we would have $1 + 3x + 4x^2 + 6x^3 + 9x^4 + \cdots + 24 x^{17} + 41 x^{18} + \cdots\text{.}$ Now it is clear that 24 is the 17th term of the sequence (that is, $a_{17} = 24$. Of course to get this benefit we could have displayed our sequence in any number of ways, perhaps $\fbox{1}_0 \fbox{3}_1 \fbox{4}_2 \fbox{6}_3 \fbox{9}_4 \cdots \fbox{24}_{17}\fbox{41}_{18}\cdots\text{,}$ but we do not do this. The reason is that the generating series looks like an ordinary power series (although we are interpreting it differently) so we can do things with it that we ordinarily do with power series such as write down what it converges to.

现在你大概会自然地发问:我们为何要这么做?原因之一在于,用幂级数对数列编码,能帮我们弄清数列中每一项究竟是第几项。例如,若我们把数列写成 $1, 3, 4, 6, 9, \ldots, 24, 41,\ldots$,则根本无从判断 24 是第几项(即便我们约定首项应为 $a_0$)。然而,如果我们改写成生成级数,就会得到 $1 + 3x + 4x^2 + 6x^3 + 9x^4 + \cdots + 24 x^{17} + 41 x^{18} + \cdots$。这样便一目了然:24 是数列的第 17 项(即 $a_{17} = 24$)。当然,要得到这种好处,我们本也可以用各种方式标注数列,比如 $\fbox{1}_0 \fbox{3}_1 \fbox{4}_2 \fbox{6}_3 \fbox{9}_4 \cdots \fbox{24}_{17}\fbox{41}_{18}\cdots$,但我们不这么做。原因在于,生成级数看起来就像一个普通的幂级数(尽管我们赋予了它不同的解释),因此我们可以像对待普通幂级数那样对它操作,例如写出它的收敛对象。

For example, from calculus we know that the power series $1 + x + \frac{x^2}{2} + \frac{x^3}{6} + \frac{x^4}{24} + \cdots + \frac{x^n}{n!} + \cdots$ converges to the function $e^x\text{.}$ So we can use $e^x$ as a way of talking about the sequence of coefficients of the power series for $e^x\text{.}$ When we write down a nice compact function which has an infinite power series that we view as a generating series, then we call that function a generating function. In this example, we would say

例如,由微积分可知,幂级数 $1 + x + \frac{x^2}{2} + \frac{x^3}{6} + \frac{x^4}{24} + \cdots + \frac{x^n}{n!} + \cdots$ 收敛到函数 $e^x$。因此我们可以用 $e^x$ 来指代 $e^x$ 的幂级数系数的数列。当我们写下一个简洁紧凑的函数,它带有一个被我们视为生成级数的无穷幂级数时,就把这个函数称为生成函数(generating function)。在本例中,我们会说

\begin{equation*} 1, 1, \frac{1}{2}, \frac{1}{6}, \frac{1}{24}, \ldots, \frac{1}{n!}, \ldots \mbox{ has generating function } e^x \end{equation*}

数列 $1, 1, \frac{1}{2}, \frac{1}{6}, \frac{1}{24}, \ldots, \frac{1}{n!}, \ldots$ 的生成函数是 $e^x$。

Building Generating Functions 构造生成函数

The $e^x$ example is very specific. We have a rather odd sequence, and the only reason we know its generating function is because we happen to know the Taylor series for $e^x\text{.}$ Our goal now is to gather some tools to build the generating function of a particular given sequence.

$e^x$ 这个例子非常特殊。我们得到的数列相当古怪,而之所以知道它的生成函数,只是因为我们恰好知道 $e^x$ 的泰勒级数。现在我们的目标是积累一些工具,以便为给定的某个具体数列构造出它的生成函数。

Let's see what the generating functions are for some very simple sequences. The simplest of all: 1, 1, 1, 1, 1, …. What does the generating series look like? It is simply $1 + x + x^2 + x^3 + x^4 + \cdots\text{.}$ Now, can we find a closed form for this power series? Yes! This particular series is really just a geometric series with common ratio $x\text{.}$ So if we use our "multiply, shift and subtract" technique from Section 2.2, we have

先来看看几个极简单数列的生成函数。最简单的一个是:1, 1, 1, 1, 1, …。它的生成级数长什么样?其实就是 $1 + x + x^2 + x^3 + x^4 + \cdots$。那么,我们能否为这个幂级数找到一个闭式(closed form)?能!这个级数实质上就是一个公比为 $x$ 的等比级数。所以,如果我们运用第 2.2 节的「乘、移、减」技巧,就有

\begin{align*} S &= 1 + x + x^2 + x^3 + \cdots\\ \underline{- xS} &\underline{= \quad\quad x + x^2 + x^3 + x^4 + \cdots}\\ (1-x)S &= 1 \end{align*}

设 $S = 1 + x + x^2 + x^3 + \cdots$,将两式错位相减(第二式即 $-xS$)得到 $(1-x)S = 1$。

Therefore we see that

于是我们得到

\begin{equation*} 1 + x + x^2 + x^3 \cdots = \dfrac{1}{1-x} \end{equation*}

$1 + x + x^2 + x^3 + \cdots = \dfrac{1}{1-x}$。

You might remember from calculus that this is only true on the interval of convergence for the power series, in this case when $\|x\| < 1\text{.}$ That is true for us, but we don't care. We are never going to plug anything in for $x\text{,}$ so as long as there is some value of $x$ for which the generating function and generating series agree, we are happy. And in this case we are happy.

你或许还记得微积分里讲过,这只在幂级数的收敛区间内成立,此即 $\|x\| < 1$ 时。对我们而言这固然没错,但我们并不在意。我们永远不会对 $x$ 代入任何具体数值;只要存在某个 $x$ 值使得生成函数与生成级数一致,我们就满意了。而在本例中,我们确实满意。

$1,1,1,\ldots$

数列 $1, 1, 1, \ldots$(各项全为 1)。

The generating function for $1,1,1,1,1,1,\ldots$ is $\dfrac{1}{1-x}$

数列 $1,1,1,1,1,1,\ldots$ 的生成函数是 $\dfrac{1}{1-x}$。

Let's use this basic generating function to find generating functions for more sequences. What if we replace $x$ by $-x\text{.}$ We get

我们拿这个基本的生成函数,去求更多数列的生成函数。如果把 $x$ 换成 $-x$ 会怎样?我们得到

\begin{equation*} \frac{1}{1+x} = 1 - x + x^2 - x^3 + \cdots \mbox{ which generates } 1, -1, 1, -1, \ldots \end{equation*}

$\frac{1}{1+x} = 1 - x + x^2 - x^3 + \cdots$,它生成的数列是 $1, -1, 1, -1, \ldots$。

If we replace $x$ by $3x$ we get

如果把 $x$ 换成 $3x$,则得到

\begin{equation*} \frac{1}{1-3x} = 1 + 3x + 9x^2 + 27x^3 + \cdots \mbox{ which generates } 1, 3, 9, 27, \ldots \end{equation*}

$\frac{1}{1-3x} = 1 + 3x + 9x^2 + 27x^3 + \cdots$,它生成的数列是 $1, 3, 9, 27, \ldots$。

By replacing the $x$ in $\frac{1}{1-x}$ we can get generating functions for a variety of sequences, but not all. For example, you cannot plug in anything for $x$ to get the generating function for $2,2,2,2, \ldots\text{.}$ However, we are not lost yet. Notice that each term of $2, 2, 2, 2, \ldots$ is the result of multiplying the terms of $1, 1, 1, 1, \ldots$ by the constant 2. So multiply the generating function by 2 as well.

通过替换 $\frac{1}{1-x}$ 中的 $x$,我们能为许多数列求得生成函数,但并非全部。例如,无论给 $x$ 代入什么,都得不到 $2,2,2,2, \ldots$ 的生成函数。不过我们还没束手无策。注意到 $2, 2, 2, 2, \ldots$ 的每一项,都是把数列 $1, 1, 1, 1, \ldots$ 的对应项乘以常数 2 得到的。因此,我们只需把生成函数也乘以 2。

\begin{equation*} \frac{2}{1-x} = 2 + 2x + 2x^2 + 2x^3 + \cdots \mbox{ which generates } 2, 2, 2, 2, \ldots \end{equation*}

$\frac{2}{1-x} = 2 + 2x + 2x^2 + 2x^3 + \cdots$,它生成的数列是 $2, 2, 2, 2, \ldots$。

Similarly, to find the generating function for the sequence $3, 9, 27, 81, \ldots\text{,}$ we note that this sequence is the result of multiplying each term of $1, 3, 9, 27, \ldots$ by 3. Since we have the generating function for $1, 3, 9, 27, \ldots$ we can say

类似地,要求数列 $3, 9, 27, 81, \ldots$ 的生成函数,我们注意到该数列的每一项都是把 $1, 3, 9, 27, \ldots$ 的对应项乘以 3 得到的。既然我们已经有了 $1, 3, 9, 27, \ldots$ 的生成函数,便可以说

\begin{equation*} \frac{3}{1-3x} = 3\cdot 1 + 3\cdot 3x + 3\cdot 9x^2 + 3\cdot 27x^3 + \cdots \mbox{ which generates } 3, 9, 27, 81, \ldots \end{equation*}

$\frac{3}{1-3x} = 3\cdot 1 + 3\cdot 3x + 3\cdot 9x^2 + 3\cdot 27x^3 + \cdots$,它生成的数列是 $3, 9, 27, 81, \ldots$。

What about the sequence $2, 4, 10, 28, 82, \ldots\text{?}$ Here the terms are always 1 more than powers of 3. That is, we have added the sequences $1,1,1,1,\ldots$ and $1,3,9, 27,\ldots$ term by term. Therefore we can get a generating function by adding the respective generating functions:

那么数列 $2, 4, 10, 28, 82, \ldots$ 呢?这里的每一项都比 3 的某个幂次多 1。也就是说,它是把数列 $1,1,1,1,\ldots$ 与 $1,3,9, 27,\ldots$ 逐项相加得到的。因此,我们可以把各自的生成函数相加,从而得到所求的生成函数:

\begin{align*} 2 + 4x + 10x^2 + 28x^3 + \cdots &= (1 + 1) + (1 + 3)x + (1 + 9)x^2 + (1 + 27)x^3 + \cdots\\ &= 1 + x + x^2 + x^3 + \cdots + 1 + 3x + 9x^2 + 27x^3 + \cdots\\ &= \frac{1}{1-x} + \frac{1}{1-3x} \end{align*}

把两个生成级数逐项相加,正好对应系数 $2=1+1,\;4=1+3,\;10=1+9,\;28=1+27,\ldots$,因此生成函数为 $\frac{1}{1-x} + \frac{1}{1-3x}$。

The fun does not stop there: if we replace $x$ in our original generating function by $x^2$ we get

乐趣还不止于此:如果把原来生成函数里的 $x$ 换成 $x^2$,我们得到

\begin{equation*} \frac{1}{1-x^2} = 1 + x^2 + x^4 + x^6\cdots \mbox{ which generates } 1, 0, 1, 0, 1, 0, \ldots. \end{equation*}

$\frac{1}{1-x^2} = 1 + x^2 + x^4 + x^6 + \cdots$,它生成的数列是 $1, 0, 1, 0, 1, 0, \ldots$。

How could we get $0,1,0,1,0,1,\ldots\text{?}$ Start with the previous sequence and shift it over by 1. But how do you do this? To see how shifting works, let's first try to get the generating function for the sequence $0, 1, 3, 9, 27, \ldots\text{.}$ We know that $\frac{1}{1-3x} = 1 + 3x + 9x^2 + 27x^3 + \cdots\text{.}$ To get the zero out front, we need the generating series to look like $x + 3x^2 + 9x^3 + 27x^4+ \cdots$ (so there is no constant term). Multiplying by $x$ has this effect. So the generating function for $0, 1, 3, 9, 27, \ldots$ is $\frac{x}{1-3x}\text{.}$ This will also work to get the generating function for $0,1,0,1,0,1,\ldots\text{:}$

我们怎样得到 $0,1,0,1,0,1,\ldots$ 呢?从上一数列出发,把它整体平移(shift)1 位。但如何做到?为了弄清平移的原理,先试着求数列 $0, 1, 3, 9, 27, \ldots$ 的生成函数。已知 $\frac{1}{1-3x} = 1 + 3x + 9x^2 + 27x^3 + \cdots$。要让最前面出现 0,生成级数就得形如 $x + 3x^2 + 9x^3 + 27x^4 + \cdots$(即没有常数项)。乘以 $x$ 正好能起到这个作用。因此数列 $0, 1, 3, 9, 27, \ldots$ 的生成函数是 $\frac{x}{1-3x}$。同理,也能借此得到 $0,1,0,1,0,1,\ldots$ 的生成函数:

\begin{equation*} \frac{x}{1-x^2} = x + x^3 + x^5 + \cdots \mbox{ which generates } 0, 1, 0, 1, 0 , 1 \ldots \end{equation*}

$\frac{x}{1-x^2} = x + x^3 + x^5 + \cdots$,它生成的数列是 $0, 1, 0, 1, 0, 1, \ldots$。

What if we add the sequences $1,0,1,0,1,0,\ldots$ and $0,1,0,1,0,1,\ldots$ term by term? We should get $1,1,1,1,1,1\ldots\text{.}$ What happens when we add the generating functions? It works (try it)!

如果我们把数列 $1,0,1,0,1,0,\ldots$ 与 $0,1,0,1,0,1,\ldots$ 逐项相加,会得到什么?应该是 $1,1,1,1,1,1\ldots$。把它们的生成函数相加又会怎样?结果成立(不妨一试)!

\begin{equation*} \frac{1}{1-x^2} + \frac{x}{1-x^2} = \frac{1}{1-x}. \end{equation*}

$\frac{1}{1-x^2} + \frac{x}{1-x^2} = \frac{1}{1-x}$。

Here's a sneaky one: what happens if you take the derivative of $\frac{1}{1-x}\text{?}$ We get $\frac{1}{(1-x)^2}\text{.}$ On the other hand, if we differentiate term by term in the power series, we get $(1 + x + x^2 + x^3 + \cdots)' = 1 + 2x + 3x^2 + 4x^3 + \cdots$ which is the generating series for $1, 2, 3, 4, \ldots\text{.}$ This says

再来一个巧妙的:如果对 $\frac{1}{1-x}$ 求导(derivative)会怎样?得到 $\frac{1}{(1-x)^2}$。另一方面,如果在幂级数中逐项求导,则有 $(1 + x + x^2 + x^3 + \cdots)' = 1 + 2x + 3x^2 + 4x^3 + \cdots$,这正是数列 $1, 2, 3, 4, \ldots$ 的生成级数。也就是说

$1,2,3,\ldots$

数列 $1, 2, 3, \ldots$(正整数列)。

The generating function for $1, 2, 3, 4, 5, \ldots$ is $\d\frac{1}{(1-x)^2}$.

数列 $1, 2, 3, 4, 5, \ldots$ 的生成函数是 $\d\frac{1}{(1-x)^2}$。

Take a second derivative: $\frac{2}{(1-x)^3} = 2 + 6x + 12x^2 + 20x^3 + \cdots\text{.}$ So $\frac{1}{(1-x)^3} = 1 + 3x + 6x^2 + 10x^3 + \cdots$ is a generating function for the triangular numbers, $1,3,6,10\ldots$ (although here we have $a_0 = 1$ while $T_0 = 0$ usually).

再求一次导:$\frac{2}{(1-x)^3} = 2 + 6x + 12x^2 + 20x^3 + \cdots$。于是 $\frac{1}{(1-x)^3} = 1 + 3x + 6x^2 + 10x^3 + \cdots$ 是三角形数(triangular numbers) $1,3,6,10\ldots$ 的一个生成函数(尽管此处 $a_0 = 1$,而通常 $T_0 = 0$)。

Differencing 差分法

We have seen how to find generating functions from $\frac{1}{1-x}$ using multiplication (by a constant or by $x$), substitution, addition, and differentiation. To use each of these, you must notice a way to transform the sequence $1,1,1,1,1\ldots$ into your desired sequence. This is not always easy. It is also not really the way we have analyzed sequences. One thing we have considered often is the sequence of differences between terms of a sequence. This will turn out to be helpful in finding generating functions as well. The sequence of differences is often simpler than the original sequence. So if we know a generating function for the differences, we would like to use this to find a generating function for the original sequence.

我们已经看到,如何借助乘法(乘常数或乘 $x$)、代入、加法与求导,从 $\frac{1}{1-x}$ 出发求得生成函数。要使用这些手段,你必须想出办法把数列 $1,1,1,1,1\ldots$ 变形成目标数列。这并不总是容易,而且这其实也不是我们分析数列的惯常思路。我们经常考察的一个对象是数列相邻项之间的差分(differences)数列。这在求生成函数时同样有用:差分数列往往比原数列更简单。因此,如果我们知道了差分数列的生成函数,便可以用它反求原数列的生成函数。

For example, consider the sequence $2, 4, 10, 28, 82, \ldots\text{.}$ How could we move to the sequence of first differences: $2, 6, 18, 54,\ldots\text{?}$ We want to subtract 2 from the 4, 4 from the 10, 10 from the 28, and so on. So if we subtract (term by term) the sequence $0, 2, 4, 10, 28,\ldots$ from $2, 4, 10, 28\ldots\text{,}$ we will be set. We can get the generating function for $0,2,4,10,28,\ldots$ from the generating function for $2,4,10,28\ldots$ by multiplying by $x\text{.}$ Use $A$ to represent the generating function for $2, 4, 10, 28, 82, \ldots$ Then:

举例来说,考虑数列 $2, 4, 10, 28, 82, \ldots$。我们怎样得到它的一阶差分数列 $2, 6, 18, 54,\ldots$ 呢?我们要从 4 里减去 2、从 10 里减去 4、从 28 里减去 10,依此类推。因此,如果我们把数列 $2, 4, 10, 28\ldots$ 减去数列 $0, 2, 4, 10, 28,\ldots$(逐项相减),就成功了。而数列 $0,2,4,10,28,\ldots$ 的生成函数,可以由 $2,4,10,28\ldots$ 的生成函数乘以 $x$ 得到。用 $A$ 表示 $2, 4, 10, 28, 82, \ldots$ 的生成函数,则有:

\begin{align*} A &= 2 + 4x + 10x^2 +28x^3 + 82x^4 + \cdots\\ \underline{-xA} &\underline{= 0 + 2x + 4x^2 + 10x^3 + 28 x^4 + 82x^5 + \cdots}\\ (1-x)A &= 2 + 2x + 6x^2 + 18x^3 + 54x^4 + \cdots \end{align*}

将 $A = 2 + 4x + 10x^2 + 28x^3 + \cdots$ 与 $-xA = 0 + 2x + 4x^2 + 10x^3 + \cdots$ 相减,得到 $(1-x)A = 2 + 2x + 6x^2 + 18x^3 + 54x^4 + \cdots$,其系数恰为原数列与平移后数列逐项相减的结果。

While we don't get exactly the sequence of differences, we do get something close. In this particular case, we already know the generating function $A$ (we found it in the previous section) but most of the time we will use this differencing technique to find $A\text{:}$ if we have the generating function for the sequence of differences, we can then solve for $A\text{.}$

虽然我们并没有恰好得到差分数列,但得到了与之相近的东西。在这个具体例子里,我们已经知道了生成函数 $A$(上一节求得的),但大多数时候,我们会用这种差分技巧来反求 $A$:一旦掌握了差分数列的生成函数,便可解出 $A$。

Example 2

示例 2

Find a generating function for $1, 3, 5, 7, 9,\ldots\text{.}$

求数列 $1, 3, 5, 7, 9,\ldots$ 的生成函数。

Solution

解答

Notice that the sequence of differences is constant. We know how to find the generating function for any constant sequence. So denote the generating function for $1, 3, 5, 7, 9, \ldots$ by $A\text{.}$ We have

注意到该数列的差分是常数。而任意常数数列的生成函数我们都已会求。因此,记 $1, 3, 5, 7, 9, \ldots$ 的生成函数为 $A$,则有

\begin{align*} A &= 1 + 3x + 5x^2 + 7x^3 + 9x^4 + \cdots\\ \underline{-xA} &\underline{= 0 + x + 3x^2 + 5x^3 + 7x^4 + 9x^5 + \cdots}\\ (1-x)A &= 1 + 2x + 2x^2 + 2x^3 + 2x^4 + \cdots \end{align*}

相减后得到 $(1-x)A = 1 + 2x + 2x^2 + 2x^3 + 2x^4 + \cdots$。

We know that $2x + 2x^2 + 2x^3 + 2x^4 + \cdots = \dfrac{2x}{1-x}\text{.}$ Thus

我们知道 $2x + 2x^2 + 2x^3 + 2x^4 + \cdots = \dfrac{2x}{1-x}$。于是

\begin{equation*} (1-x)A = 1 + \frac{2x}{1-x}. \end{equation*}

$(1-x)A = 1 + \frac{2x}{1-x}$。

Now solve for $A\text{:}$

现在解出 $A$:

\begin{equation*} A = \frac{1}{1-x} + \frac{2x}{(1-x)^2} = \frac{1+x}{(1-x)^2}. \end{equation*}

$A = \frac{1}{1-x} + \frac{2x}{(1-x)^2} = \frac{1+x}{(1-x)^2}$。

Does this makes sense? Before we simplified the two fractions into one, we were adding the generating function for the sequence $1,1,1,1,\ldots$ to the generating function for the sequence $0, 2, 4, 6, 8, 10, \ldots$ (remember $\frac{1}{(1-x)^2}$ generates $1,2,3,4,5, \ldots\text{,}$ multiplying by $2x$ shifts it over, putting the zero out front, and doubles each term). If we add these term by term, we get the correct sequence $1,3,5,7, 9, \ldots\text{.}$

这合理吗?在把两个分式合并之前,我们其实是在把数列 $1,1,1,1,\ldots$ 的生成函数,与数列 $0, 2, 4, 6, 8, 10, \ldots$ 的生成函数相加(回想一下,$\frac{1}{(1-x)^2}$ 生成 $1,2,3,4,5, \ldots$;乘以 $2x$ 会把它整体平移,使最前面出现 0,并把每一项都翻倍)。把二者逐项相加,就得到正确的数列 $1,3,5,7, 9, \ldots$。

Now that we have a generating function for the odd numbers, we can use that to find the generating function for the squares:

既然已经有了奇数数列的生成函数,我们就能用它来求平方数数列的生成函数:

Example 3

示例 3

Find the generating function for $1, 4, 9, 16, \ldots\text{.}$ Note we take $1 = a_0\text{.}$

求数列 $1, 4, 9, 16, \ldots$ 的生成函数。注意这里取 $1 = a_0$。

Solution

解答

Again we call the generating function for the sequence $A\text{.}$ Using differencing:

同样记该数列的生成函数为 $A$。利用差分:

\begin{align*} A &= 1 + 4x + 9x^2 + 16x^3 + \cdots\\ \underline{- xA} &\underline{ = 0 + x + 4x^2 + 9x^3 + 16x^4 + \cdots}\\ (1-x)A &= 1 + 3x + 5x^2 + 7x^3 + \cdots \end{align*}

相减后得到 $(1-x)A = 1 + 3x + 5x^2 + 7x^3 + \cdots$,而这正是我们在上例中求得奇数列生成函数的式子。

Since $1 + 3x + 5x^2 + 7x^3 + \cdots = \d\frac{1+x}{(1-x)^2}$ we have $A = \d\frac{1+x}{(1-x)^3}\text{.}$

由于 $1 + 3x + 5x^2 + 7x^3 + \cdots = \d\frac{1+x}{(1-x)^2}$,于是 $A = \d\frac{1+x}{(1-x)^3}$。

In each of the examples above, we found the difference between consecutive terms which gave us a sequence of differences for which we knew a generating function. We can generalize this to more complicated relationships between terms of the sequence. For example, if we know that the sequence satisfies the recurrence relation $a_n = 3a_{n-1} - 2a_{n-2}\text{?}$ In other words, if we take a term of the sequence and subtract 3 times the previous term and then add 2 times the term before that, we get 0 (since $a_n - 3a_{n-1} + 2a_{n-2} = 0$. That will hold for all but the first two terms of the sequence. So after the first two terms, the sequence of results of these calculations would be a sequence of 0's, for which we definitely know a generating function.

在上面的每个例子中,我们都通过求相邻项的差,得到了一个已知生成函数的差分数列。我们可以把这种做法推广到数列各项之间更复杂的关系上。例如,若已知数列满足递推关系(recurrence relation) $a_n = 3a_{n-1} - 2a_{n-2}$?换言之,取数列的某一项,减去前一项的 3 倍,再加上再前一项的 2 倍,结果便是 0(因为 $a_n - 3a_{n-1} + 2a_{n-2} = 0$)。这对数列除前两项之外的所有项都成立。因此,在前两项之后,这些计算结果的数列将是一串 0,而这样的数列我们显然知道它的生成函数。

Example 4

示例 4

The sequence $1, 3, 7, 15, 31, 63, \ldots$ satisfies the recurrence relation $a_n = 3a_{n-1} - 2a_{n-2}\text{.}$ Find the generating function for the sequence.

数列 $1, 3, 7, 15, 31, 63, \ldots$ 满足递推关系 $a_n = 3a_{n-1} - 2a_{n-2}$。求该数列的生成函数。

Solution

解答

Call the generating function for the sequence $A\text{.}$ We have

记该数列的生成函数为 $A$,则有

\begin{align*} A &= 1 + 3x + 7x^2 + 15x^3 + 31x^4 + \cdots + a_nx^n + \cdots\\ -3xA &= 0 - 3x - 9x^2 - 21x^3 - 45x^4 - \cdots - 3a_{n-1}x^n - \cdots\\ \underline{+\quad 2x^2A\quad} &\underline{ = 0 + 0x + 2x^2 + 6x^3 + 14x^4 + \cdots + 2a_{n-2}x^n + \cdots}\\ (1-3x+2x^2)A &= 1 \end{align*}

将 $A$、$-3xA$ 与 $2x^2A$ 三式相加:第一行给出各项;第二行(乘 $-3x$)把每项右移一位并乘 $-3$;第三行(乘 $2x^2$)右移两位并乘 2。对应项相加时,恰好是「每一项减前一项的 3 倍、再加再前一项的 2 倍」。对 $a_1$ 之后的每一项,这一结果都为 0,故相加后只剩常数项 1,即 $(1-3x+2x^2)A = 1$。

We multiplied $A$ by $-3x$ which shifts every term over one spot and multiplies them by $-3\text{.}$ On the third line, we multiplied $A$ by $2x^2\text{,}$ which shifted every term over two spots and multiplied them by 2. When we add up the corresponding terms, we are taking each term, subtracting 3 times the previous term, and adding 2 times the term before that. This will happen for each term after $a_1$ because $a_n - 3a_{n-1} + 2a_{n-2} = 0\text{.}$ In general, we might have two terms from the beginning of the generating series, although in this case the second term happens to be 0 as well.

我们用 $-3x$ 乘以 $A$,使每项右移一位并乘上 $-3$;在第三行,用 $2x^2$ 乘以 $A$,使每项右移两位并乘上 2。把对应项相加时,相当于对每一项「减去前一项的 3 倍、再加上再前一项的 2 倍」。对 $a_1$ 之后的每一项都成立,因为 $a_n - 3a_{n-1} + 2a_{n-2} = 0$。一般而言,生成级数的开头可能会残留两项,不过在本例中第二项恰好也是 0。

Now we just need to solve for $A\text{:}$

现在只需解出 $A$:

\begin{equation*} A = \frac{1}{1 - 3x + 2x^2}. \end{equation*}

$A = \frac{1}{1 - 3x + 2x^2}$。

Multiplication and Partial Sums 乘法与部分和

What happens to the sequences when you multiply two generating functions? Let's see: $A = a_0 + a_1x + a_2x^2 + \cdots$ and $B = b_0 + b_1x + b_2x^2 + \cdots\text{.}$ To multiply $A$ and $B\text{,}$ we need to do a lot of distributing (infinite FOIL?) but keep in mind we will group like terms and only need to write down the first few terms to see the pattern. The constant term is $a_0b_0\text{.}$ The coefficient of $x$ is $a_0b_1 + a_1b_0\text{.}$ And so on. We get:

当两个生成函数相乘时,对应的数列会发生什么?来看:$A = a_0 + a_1x + a_2x^2 + \cdots$,$B = b_0 + b_1x + b_2x^2 + \cdots$。要把 $A$ 与 $B$ 相乘,需要做大量的分配展开(无穷的 FOIL?),但记住我们要合并同类项,并且只需写出前几项就能看出规律。常数项是 $a_0b_0$;$x$ 的系数是 $a_0b_1 + a_1b_0$;依此类推。我们得到:

\begin{equation*} AB = a_0b_0 + (a_0b_1 + a_1b_0)x + (a_0b_2 + a_1b_1 + a_2b_0)x^2 + (a_0b_3 + a_1b_2 + a_2b_1 + a_3b_0)x^3 + \cdots \end{equation*}

$AB = a_0b_0 + (a_0b_1 + a_1b_0)x + (a_0b_2 + a_1b_1 + a_2b_0)x^2 + (a_0b_3 + a_1b_2 + a_2b_1 + a_3b_0)x^3 + \cdots$。也就是说,$x^n$ 的系数是 $\sum_{k=0}^n a_k b_{n-k}$(两个数列的卷积)。

Example 5

示例 5

"Multiply" the sequence $1, 2, 3, 4, \ldots$ by the sequence $1, 2, 4, 8, 16, \ldots\text{.}$

把数列 $1, 2, 3, 4, \ldots$ 与数列 $1, 2, 4, 8, 16, \ldots$「相乘」。

Solution

解答

The new constant term is just $1 \cdot 1\text{.}$ The next term will be $1\cdot 2 + 2 \cdot 1 = 4\text{.}$ The next term: $1 \cdot 4 + 2 \cdot 2 + 3 \cdot 1 = 11\text{.}$ One more: $1 \cdot 8 + 2 \cdot 4 + 3 \cdot 2 + 4 \cdot 1 = 28\text{.}$ The resulting sequence is

新的常数项就是 $1 \cdot 1$。下一项是 $1\cdot 2 + 2 \cdot 1 = 4$。再下一项:$1 \cdot 4 + 2 \cdot 2 + 3 \cdot 1 = 11$。再来一项:$1 \cdot 8 + 2 \cdot 4 + 3 \cdot 2 + 4 \cdot 1 = 28$。所得数列为

\begin{equation*} 1, 4, 11, 28, 57, \ldots \end{equation*}

$1, 4, 11, 28, 57, \ldots$。

Since the generating function for $1,2,3,4, \ldots$ is $\frac{1}{(1-x)^2}$ and the generating function for $1,2,4,8, 16, \ldots$ is $\frac{1}{1-2x}\text{,}$ we have that the generating function for $1,4, 11, 28, 57, \ldots$ is $\frac{1}{(1-x)^2(1-2x)}$

由于 $1,2,3,4, \ldots$ 的生成函数是 $\frac{1}{(1-x)^2}$,而 $1,2,4,8, 16, \ldots$ 的生成函数是 $\frac{1}{1-2x}$,故 $1,4, 11, 28, 57, \ldots$ 的生成函数为二者之积 $\frac{1}{(1-x)^2(1-2x)}$。

Consider the special case when you multiply a sequence by $1, 1, 1, \ldots\text{.}$ For example, multiply $1,1,1,\ldots$ by $1, 2, 3, 4, 5\ldots\text{.}$ The first term is $1\cdot 1 = 1\text{.}$ Then $1\cdot 2 + 1 \cdot 1 = 3\text{.}$ Then $1\cdot 3 + 1\cdot 2 + 1 \cdot 1 = 6\text{.}$ The next term will be 10. We are getting the triangular numbers. More precisely, we get the sequence of partial sums of $1,2,3,4,5, \ldots\text{.}$ In terms of generating functions, we take $\frac{1}{1-x}$ (generating $1,1,1,1,1\ldots$ and multiply it by $\frac{1}{(1-x)^2}$ (generating $1,2,3,4,5,\ldots$ and this give $\frac{1}{(1-x)^3}\text{.}$ This should not be a surprise as we found the same generating function for the triangular numbers earlier.

考虑一个特例:把某个数列与 $1, 1, 1, \ldots$ 相乘。例如,将 $1,1,1,\ldots$ 与 $1, 2, 3, 4, 5\ldots$ 相乘。首项是 $1\cdot 1 = 1$;接着 $1\cdot 2 + 1 \cdot 1 = 3$;然后 $1\cdot 3 + 1\cdot 2 + 1 \cdot 1 = 6$;下一项将是 10。我们得到的正是三角形数。更准确地说,得到的是 $1,2,3,4,5, \ldots$ 的部分和(partial sums)数列。用生成函数来表述:取 $\frac{1}{1-x}$(生成 $1,1,1,1,1\ldots$)乘以 $\frac{1}{(1-x)^2}$(生成 $1,2,3,4,5,\ldots$),得到 $\frac{1}{(1-x)^3}$。这并不意外,因为我们之前已经为三角形数求得过同一个生成函数。

The point is, if you need to find a generating function for the sum of the first $n$ terms of a particular sequence, and you know the generating function for that sequence, you can multiply it by $\frac{1}{1-x}\text{.}$ To go back from the sequence of partial sums to the original sequence, you look at the sequence of differences. When you get the sequence of differences you end up multiplying by $1-x\text{,}$ or equivalently, dividing by $\frac{1}{1-x}\text{.}$ Multiplying by $\frac{1}{1-x}$ gives partial sums, dividing by $\frac{1}{1-x}$ gives differences.

要点在于:若要求某个数列前 $n$ 项之和的生成函数,并且你已经知道该数列本身的生成函数,只需把它乘以 $\frac{1}{1-x}$。若要从部分和数列回到原数列,则去考察它的差分数列——也就是乘以 $1-x$,等价于除以 $\frac{1}{1-x}$。乘以 $\frac{1}{1-x}$ 得到部分和,除以 $\frac{1}{1-x}$ 得到差分。

Solving Recurrence Relations with Generating Functions 用生成函数求解递推关系

We conclude with an example of one of the many reasons studying generating functions is helpful. We can use generating functions to solve recurrence relations.

最后用一个例子说明研究生成函数颇有价值的诸多原因之一:我们可以用生成函数来求解递推关系。

Example 6

示例 6

Solve the recurrence relation $a_n = 3a_{n-1} - 2a_{n-2}$ with initial conditions $a_0 = 1$ and $a_1 = 3\text{.}$

求解递推关系 $a_n = 3a_{n-1} - 2a_{n-2}$,初值条件为 $a_0 = 1$、$a_1 = 3$。

Solution

解答

We saw in an example above that this recurrence relation gives the sequence $1, 3, 7, 15, 31, 63, \ldots$ which has generating function $\dfrac{1}{1 - 3x + 2x^2}\text{.}$ We did this by calling the generating function $A$ and then computing $A - 3xA + 2x^2A$ which was just 1, since every other term canceled out.

我们在前面的例子中已经看到,这个递推关系给出的数列是 $1, 3, 7, 15, 31, 63, \ldots$,其生成函数为 $\dfrac{1}{1 - 3x + 2x^2}$。做法是将生成函数记为 $A$,再计算 $A - 3xA + 2x^2A$,由于其余各项全部相消,结果恰好就是 1。

But how does knowing the generating function help us? First, break up the generating function into two simpler ones. For this, we can use partial fraction decomposition. Start by factoring the denominator:

但知道了生成函数又能帮我们什么忙?首先,把这个生成函数拆成两个更简单的部分。为此可以使用部分分式分解(partial fraction decomposition)。先对分母因式分解:

\begin{equation*} \frac{1}{1-3x + 2x^2} = \frac{1}{(1-x)(1-2x)}. \end{equation*}

$\frac{1}{1-3x + 2x^2} = \frac{1}{(1-x)(1-2x)}$。

Partial fraction decomposition tells us that we can write this faction as the sum of two fractions (we decompose the given fraction):

部分分式分解告诉我们,可以把这个分式写成两个分式之和(即对给定的分式作分解):

\begin{equation*} \frac{1}{(1-x)(1-2x)} = \frac{a}{1-x} + \frac{b}{1-2x} \text{ \quad for some constants } a \text{ and } b. \end{equation*}

$\frac{1}{(1-x)(1-2x)} = \frac{a}{1-x} + \frac{b}{1-2x}$,其中 $a$、$b$ 为某两个常数。

To find $a$ and $b$ we add the two decomposed fractions using a common denominator. This gives

为求出 $a$ 与 $b$,我们通分后把两个分解后的分式相加,得到

\begin{equation*} \frac{1}{(1-x)(1-2x)} = \frac{a(1-2x) + b(1-x)}{(1-x)(1-2x)}. \end{equation*}

$\frac{1}{(1-x)(1-2x)} = \frac{a(1-2x) + b(1-x)}{(1-x)(1-2x)}$。

so

于是

\begin{equation*} 1 = a(1-2x) + b(1-x). \end{equation*}

$1 = a(1-2x) + b(1-x)$。

This must be true for all values of $x\text{.}$ If $x = 1\text{,}$ then the equation becomes $1 = -a$ so $a = -1\text{.}$ When $x = \frac{1}{2}$ we get $1 = b/2$ so $b = 2\text{.}$ This tells us that we can decompose the fraction like this:

这必须对一切 $x$ 都成立。若取 $x = 1$,等式变为 $1 = -a$,故 $a = -1$。当 $x = \frac{1}{2}$ 时,得到 $1 = b/2$,故 $b = 2$。由此可知,该分式可以如下分解:

\begin{equation*} \frac{1}{(1-x)(1-2x)} = \frac{-1}{1-x} + \frac{2}{1-2x}. \end{equation*}

$\frac{1}{(1-x)(1-2x)} = \frac{-1}{1-x} + \frac{2}{1-2x}$。

This completes the partial fraction decomposition. Notice that these two fractions are generating functions we know. In fact, we should be able to expand each of them.

部分分式分解至此完成。注意,这两个分式都是我们已经熟悉的生成函数。事实上,我们应当能把它们各自展开。

\begin{equation*} \frac{-1}{1-x} = -1 - x - x^2 -x^3 - x^4 - \cdots \mbox{ which generates } -1, -1, -1, -1, -1, \ldots. \end{equation*} \begin{equation*} \frac{2}{1-2x} = 2 + 4x + 8x^2 + 16x^3 + 32x^4 + \cdots \mbox{ which generates } 2, 4, 8, 16, 32, \ldots. \end{equation*}

$\frac{-1}{1-x} = -1 - x - x^2 -x^3 - x^4 - \cdots$,生成数列 $-1, -1, -1, -1, -1, \ldots$;而 $\frac{2}{1-2x} = 2 + 4x + 8x^2 + 16x^3 + 32x^4 + \cdots$,生成数列 $2, 4, 8, 16, 32, \ldots$。

We can give a closed formula for the $n$th term of each of these sequences. The first is just $a_n = -1\text{.}$ The second is $a_n = 2^{n+1}\text{.}$ The sequence we are interested in is just the sum of these, so the solution to the recurrence relation is

我们可以为这两个数列的第 $n$ 项写出闭式(closed form)。第一个就是 $a_n = -1$;第二个是 $a_n = 2^{n+1}$。我们关心的数列正好是二者之和,因此该递推关系的解为

\begin{equation*} a_n = 2^{n+1} - 1 \end{equation*}

$a_n = 2^{n+1} - 1$。

We can now add generating functions to our list of methods for solving recurrence relations.

至此,我们便可以把生成函数加入到求解递推关系的方法清单之中了。