Computing with Generating Functions

V. E. McHale

2 Jan 2025

For \(p(n)\) the number of integer partitions of \(n\), we have

\[\prod_{n=1}^\infty \frac{1}{1-x^n} = \sum_{n=1}^\infty p(n)x^n\]

That is, \(p(n)\) is the coefficient of \(x^n\) in

\[\prod_{n=1}^\infty \frac{1}{1-x^n}=(1+x+x^2+\cdots)(1+x^2+x^4+\cdots)\cdots(1+x^k+x^{2k}+\cdots)\cdots\]

To compute concrete values from this requires a bit more thought.

The below will have coefficients that agree with \(p(n)\) for \(n\leq 8\):

\[\begin{align*}(1+x&+x^2+x^3+x^4+x^5+x^6+x^7+x^8)\\&(1+x^2+x^4+x^6+x^8)(1+x^3+x^6)(1+x^4+x^8)\\&(1+x^5)(1+x^6)(1+x^7)(1+x^8)\end{align*}\]

See what happens when we write the coefficients of \(1+x+x^2+x^3+x^4+x^5+x^6+x^7+x^7+x^8\) etc. in a table:

1 1 1 1 1 1 1 1 1
1 0 1 0 1 0 1 0 1
1 0 0 1 0 0 1 0 0
1 0 0 0 1 0 0 0 1
1 0 0 0 0 1 0 0 0
1 0 0 0 0 0 1 0 0
1 0 0 0 0 0 0 1 0
1 0 0 0 0 0 0 0 1

1s filling a 9-vector separated by increasing bands of 0s, until the bands have stretched to the limits.

This can be generated with the following in J:

   N =: 8
   ((i.N)+1) {{(x|y)=0}}"0/ i.(N+1)
1 1 1 1 1 1 1 1 1
1 0 1 0 1 0 1 0 1
1 0 0 1 0 0 1 0 0
1 0 0 0 1 0 0 0 1
1 0 0 0 0 1 0 0 0
1 0 0 0 0 0 1 0 0
1 0 0 0 0 0 0 1 0
1 0 0 0 0 0 0 0 1

which is perhaps more elegant as:

0=((i.N)+1) |/ i.(N+1)

with the J idiom +//.@(*/) for polynomial multiplication, we fold to get the desired product, viz.

   pmul=:+//.@(*/)
   pmul/0=((i.N)+1) |/ i.(N+1)
1 1 2 3 5 7 11 15 22 27 36 46 58 71 87 103 121 138 158 179 197 ...

Only the first 9 are coefficients of \(\prod_{n=1}^\infty \frac{1}{1-x^n}\), so we truncate:

   (N+1){.pmul/0=((i.N)+1) |/ i.(N+1)
1 1 2 3 5 7 11 15 22

And indeed this is the number of partitions of \(n\) for \(n=0,1,\ldots 8\)

The array perspective on this problem offers something new and brings the clever and lucid mathematical solution via generating functions all the way to a clear and elegant computation.

Epilogue

Mike Day gives

{{ (y{. +//.@(*/)  )/ (0=}.|/])@i. @ >:y}}

which performs take {. at an earlier point and thus is more efficient.