Dealing with Generating Functions accurately

I'm currently working through Iven Niven's "Mathematics of Choice." In the chapter on Generating Functions, the exercises include problems like:

How many solutions in non-negative integers does the equation $2x+3y+7z+9r=20$ have?

This of course ends up being the same as finding the coefficient of $x^{20}$ in $(1+x^2+x^4+x^6+\cdots+x^{20})(1+x^3+x^6+\cdots+x^{18})(1+x^7+x^{14})(1+x^9+x^{18})$. Maybe I'm just being a big weenie, but dealing with polynomials this large ends up being really tedious and error-prone.

I've realized that you can save the most involved multiplication for last, since that requires the least detail, and I'm of course not bothering to keep track of any powers greater than the one I'm interested in.

Any tips for keeping these giant polynomials manageable?

• I do not know if my comment will be of any use. If you perform your long multiplication, you end at degree 70 while you are only concerned by the term of degree 20. Would Taylor series be of any help ? – Claude Leibovici Dec 25 '13 at 10:17
• Are you allowed to answer this question writing a small program which means using simulation ? If yes, it would be a very small code. – Claude Leibovici Dec 25 '13 at 12:57
• I could write a program. I was wondering if there were better techniques without using a computer. This isn't homework. I picked the book up for funsies. – Josh Infiesto Dec 25 '13 at 15:44
• My feeling is that if you do not transform the original question to a question about a certain coefficient of this polynomial to then throw some machinery at it, you can just as well work with the original equation and start counting. – Carsten S Dec 25 '13 at 19:48
• A Computer Algebra System is a lot of help with such problems. – vonbrand Aug 22 '15 at 17:41

When you can only reasonably evaluate by multiplying out the polynomials, you've described what is pretty much the only approach. Just multiply out, simplest factors first, and keep dropping all coefficients higher than the one you are interested in. In some cases this is all you can do, unfortunately. (Or, use a CAS to do the messy algebra for you.)

However, occasionally you can simplify your generating function in a way that allows you to compute the coefficients by other methods, and this is sometimes (though not always) easier.

Let's take your case as an example. The four polynomials in your product can be written as $\displaystyle \frac{1-x^{22}}{1-x^2}$, $\displaystyle \frac{1-x^{21}}{1-x^3}$, $\displaystyle \frac{1-x^{21}}{1-x^7}$, and $\displaystyle \frac{1-x^{27}}{1-x^9}$. Because we don't care about what happens to the coefficients above $x^{20}$, we can ignore the powers higher than $x^{20}$, and take the product of the resulting functions. That is, your problem is to find the coefficient of $x^{20}$ in $$g(x) = \frac{1}{(1-x^2)(1-x^3)(1-x^7)(1-x^9)}.$$

One could then use partial fractions to decompose the product into the sum of functions for which one already knows a formula for the $n$-th coefficient; this is the way to get Binet's formula for the Fibonacci numbers from the generating function $\displaystyle f(x) = \frac{x}{1-x-x^2}$. For this problem that would be a lot of work, perhaps more than just multiplying the polynomials, but in general this is quite powerful. Another method is to take many derivatives, observing that the coefficient of $x^n$ is $\frac{1}{n!}$ times the $n$-th derivative at $0$, though in this case I expect this will be at least as messy as doing the original multiplication.

The full power of generating functions is not generally expressed in finding just a single coefficient, but in the way that it allows you to compactly work with all coefficients: e.g., the same generating function $g(x)$ above will give you the solution (via the coefficient of $x^n$) to this problem when $20$ is replaced by any integer $n$.

You may also be interested in this question and its answers. If you are interested in learning a lot more about generating functions, Wilf's Generatingfunctionology is the way to go.

Frankly, I always resort to a computer algebra system at this point, but here is how I think Niven probably expected the problem to be done.

There are some labor-saving tricks. First, it usually pays to start with the sparser polynomials (those with fewer non-zero terms). Second, you only have to work up to the highest degree needed for your answer-- $x^{20}$ in this case; higher powers can be discarded. Finally, it may not be necessary to perform the final multiplication. In detail, for this problem, let

$P_2 = 1 + x^2 + x^4 + x^6 + x^8 + x^{10} + x^{12} + x^{14} + x^{16} + x^{18} + x^{20}\\ P_3 = 1 + x^3 + x^6 + x^9 + x^{12} + x^{15} + x^{18} \\ P_7 = 1 + x^7 + x^{14} \\ P_9 = 1 + x^9 + x^{18} \\$

Then, discarding any terms higher than $x^{20}$, we have

$P_7 P_9 = 1 + x^7 + x^9+ x^{14} + x^{16} + x^{18} + \dots$

next

$P_3 P_7 P_9 = 1 + x^3 + x^6 + x^7 + 2x^9 + x^{10} + 2x^{12} + x^{13} + x^{14} + 2x^{15} + 2x^{16} +x^{17} + 3x^{18} + 2x^{19} + x^{20} + \dots$

Now it is not necessary to compute $P_2 P_3 P_7 P_9$, because we are only interested in the coefficient of $x^{20}$ in the final product. Since $P_2$ has only even powers of $x$, we can just read off the coefficients of $1, x^2, x^4, \dots , x^{20}$ in $P_3 P_7 P_9$ (the even-powered terms) and add them up:

$1 + 1 + 1 + 2 + 1 + 2 + 3 + 1 = 12$

So the final answer to the number of solutions is 12.

Dealing with coefficients of generating functions is either synthetically (by learning patterns from experience what the formula for coefficients might be) or directly by manipulating the coefficients of constituent gfs (which helps with the first one). Usually the latter ends up by resulting in the multiplication of polymonials.

For multiplying large complicated polynomials in order to get a single coefficient:

• throw away anything above the degree you care about.

• use grid paper to manage coefficients in a matrix

• shift successive lines so you can add by columns instead of diagonals.

For example, for $(1 + 3x^2 + x^4 + 2x^6)(1 + 5x^3 + x^6)$:

$$\begin{array}{c|cccccccc} \hline & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12\\ \hline & 1 & 0 & 3 & 0 & 1 & 0 & 2 \\ \hline 1 & 1 & & 3 & & 1 & & 2 \\ 0 & & & & & & & \\ 0 & & & & & & & \\ 5 & & & & 5 & & 15& & 5 & & 10\\ 0 & & & & & & & \\ 0 & & & & & & & \\ 1 & & & & & & & 1 & & 3 & & 1 & & 2 \\ \hline & 1 & 0 & 3 & 5 & 1 & 15& 3 & 5 & 3 & 10& 1 & 0 & 2 \end{array}$$

for $1 + x^2 + 5x^3 + x^4 + 15 x^5 + 3 x^6 + ...$

If all we wanted was the coefficient of $x^6$, we could have avoided any column beyond 6.

I tried to see if Taylor series could be of any help. They are since you just want to get the coefficient of degree 20. But you can do that from left to right or from right to left (I refer to your writing of the products). It seems to me that from right to left is faster (by hand !).