Multinomial theorem
Suppose you have an expression of the form
Yes. It's called the multinomial theorem.
Let's get started
The multinomial theorem can be used to expand any expression of the form
Oh. Uh...
Well, to start with, those values below the summation symbol (Σ) represent conditions. We have
The summation "body," meanwhile, has us multiply a collection of values — that's the capital-pi (Π) symbol; it's the multiplication version of Σ — and then multiply that by something else. The result is then added as part of the summation loop.
The first condition on our summation loop is that the values must all add up to
Hear that one bit with the word "choose?"
That's the notation for binomial coefficients. If we call the top number
Wikipedia offers one example:
Apparently, that generalizes to larger "multinomials;" if your multinomial has
Given a multinomial with
This is all getting pretty out-there, so let's come back to Earth by actually using this.
An example
We'll go with the example I led in with:
This equation is a nested loop. The outermost loop has six iterator variables, and the innermost loop just goes over those. It's a bit like this:
int[...][6] all_possible_k_arrays = ...;
int[6] k;
// summation (big weird "E" symbol):
sum = 0;
for each k[1], k[2], k[3], k[4], k[5], k[6] matching conditions, do:
// capital-pi thing:
product = 1;
for t = 1 to k.length do
product *= ...;
end
// multiply by factorial fraction:
product *= ...;
sum += product;
end
We need to start at the innermost loop and work our way outward to understand this, but the innermost loop uses the six "loop index" variables,
Computing our outer loop variables
Let's compute the values of all possible combinations of
Here's a list of every possible combination of numbers (between zero and 2, inclusive) that can add up to 2:
- 2
- 1 + 1
Of course, we need to distribute those possibilities across sets of six total numbers, where any extra numbers just get set to zero. This means we'll have a lot of repeats — the same numbers being added together, but with the numbers in different slots among the six we have available. Here's what that looks like:
Form |
|
|
|
|
|
| Expression | Sum |
---|---|---|---|---|---|---|---|---|
2 + 0 | 2 | 0 | 0 | 0 | 0 | 0 | 2 + 0 + 0 + 0 + 0 + 0 | 2 |
0 | 2 | 0 | 0 | 0 | 0 | 0 + 2 + 0 + 0 + 0 + 0 | 2 | |
0 | 0 | 2 | 0 | 0 | 0 | 0 + 0 + 2 + 0 + 0 + 0 | 2 | |
0 | 0 | 0 | 2 | 0 | 0 | 0 + 0 + 0 + 2 + 0 + 0 | 2 | |
0 | 0 | 0 | 0 | 2 | 0 | 0 + 0 + 0 + 0 + 2 + 0 | 2 | |
0 | 0 | 0 | 0 | 0 | 2 | 0 + 0 + 0 + 0 + 0 + 2 | 2 | |
1 + 1 | 1 | 1 | 0 | 0 | 0 | 0 | 1 + 1 + 0 + 0 + 0 + 0 | 2 |
1 | 0 | 1 | 0 | 0 | 0 | 1 + 0 + 1 + 0 + 0 + 0 | 2 | |
1 | 0 | 0 | 1 | 0 | 0 | 1 + 0 + 0 + 1 + 0 + 0 | 2 | |
1 | 0 | 0 | 0 | 1 | 0 | 1 + 0 + 0 + 0 + 1 + 0 | 2 | |
1 | 0 | 0 | 0 | 0 | 1 | 1 + 0 + 0 + 0 + 0 + 1 | 2 | |
0 | 1 | 1 | 0 | 0 | 0 | 0 + 1 + 1 + 0 + 0 + 0 | 2 | |
0 | 1 | 0 | 1 | 0 | 0 | 0 + 1 + 0 + 1 + 0 + 0 | 2 | |
0 | 1 | 0 | 0 | 1 | 0 | 0 + 1 + 0 + 0 + 1 + 0 | 2 | |
0 | 1 | 0 | 0 | 0 | 1 | 0 + 1 + 0 + 0 + 0 + 1 | 2 | |
0 | 0 | 1 | 1 | 0 | 0 | 0 + 0 + 1 + 1 + 0 + 0 | 2 | |
0 | 0 | 1 | 0 | 1 | 0 | 0 + 0 + 1 + 0 + 1 + 0 | 2 | |
0 | 0 | 1 | 0 | 0 | 1 | 0 + 0 + 1 + 0 + 0 + 1 | 2 | |
0 | 0 | 0 | 1 | 1 | 0 | 0 + 0 + 0 + 1 + 1 + 0 | 2 | |
0 | 0 | 0 | 1 | 0 | 1 | 0 + 0 + 0 + 1 + 0 + 1 | 2 | |
0 | 0 | 0 | 0 | 1 | 1 | 0 + 0 + 0 + 0 + 1 + 1 | 2 |
Dealing with the inner loop
That's a lot of combinations up there, but let's actually use them and see how that shakes out. The
-
For each
ki in any valid set of values, we computexi . We then multply all of these together; this is the inner loop, called a sequence of products and indicated with the capital pi (Π).to the kith power - After the inner loop is done, we compute the multinomial coefficient (the fraction with the factorials in it), and multiply that with the sequence of products.
So let's start by substituting in our
Form | ||||||
---|---|---|---|---|---|---|
2 + 0 |
|
|
|
|
|
|
|
|
|
|
|
| |
|
|
|
|
|
| |
|
|
|
|
|
| |
|
|
|
|
|
| |
|
|
|
|
|
| |
1 + 1 |
|
|
|
|
|
|
|
|
|
|
|
| |
|
|
|
|
|
| |
|
|
|
|
|
| |
|
|
|
|
|
| |
|
|
|
|
|
| |
|
|
|
|
|
| |
|
|
|
|
|
| |
|
|
|
|
|
| |
|
|
|
|
|
| |
|
|
|
|
|
| |
|
|
|
|
|
| |
|
|
|
|
|
| |
|
|
|
|
|
| |
|
|
|
|
|
|
Now, if you take any value to the zeroth power, then you get 1, so we can strike a lot of terms from that table and just replace them with ones. Similarly, a term to the first power is just itself, so we can strip the exponent.
Form | ||||||
---|---|---|---|---|---|---|
2 + 0 | 1 | 1 | 1 | 1 | 1 | |
1 | 1 | 1 | 1 | 1 | ||
1 | 1 | 1 | 1 | 1 | ||
1 | 1 | 1 | 1 | 1 | ||
1 | 1 | 1 | 1 | 1 | ||
1 | 1 | 1 | 1 | 1 | ||
1 + 1 | 1 | 1 | 1 | 1 | ||
1 | 1 | 1 | 1 | |||
1 | 1 | 1 | 1 | |||
1 | 1 | 1 | 1 | |||
1 | 1 | 1 | 1 | |||
1 | 1 | 1 | 1 | |||
1 | 1 | 1 | 1 | |||
1 | 1 | 1 | 1 | |||
1 | 1 | 1 | 1 | |||
1 | 1 | 1 | 1 | |||
1 | 1 | 1 | 1 | |||
1 | 1 | 1 | 1 | |||
1 | 1 | 1 | 1 | |||
1 | 1 | 1 | 1 | |||
1 | 1 | 1 | 1 |
Now let's add the multinomial coefficients to the table:
Form |
|
|
|
|
|
| MCs |
---|---|---|---|---|---|---|---|
2 + 0 | 1 | 1 | 1 | 1 | 1 | 1 | |
1 | 1 | 1 | 1 | 1 | 1 | ||
1 | 1 | 1 | 1 | 1 | 1 | ||
1 | 1 | 1 | 1 | 1 | 1 | ||
1 | 1 | 1 | 1 | 1 | 1 | ||
1 | 1 | 1 | 1 | 1 | 1 | ||
1 + 1 | 1 | 1 | 1 | 1 | 2 | ||
1 | 1 | 1 | 1 | 2 | |||
1 | 1 | 1 | 1 | 2 | |||
1 | 1 | 1 | 1 | 2 | |||
1 | 1 | 1 | 1 | 2 | |||
1 | 1 | 1 | 1 | 2 | |||
1 | 1 | 1 | 1 | 2 | |||
1 | 1 | 1 | 1 | 2 | |||
1 | 1 | 1 | 1 | 2 | |||
1 | 1 | 1 | 1 | 2 | |||
1 | 1 | 1 | 1 | 2 | |||
1 | 1 | 1 | 1 | 2 | |||
1 | 1 | 1 | 1 | 2 | |||
1 | 1 | 1 | 1 | 2 | |||
1 | 1 | 1 | 1 | 2 |
Putting it all together
Remember that each row represents a valid set of possible values for the
If we multiply all the items in a row together, then we'll get the result of that expression for that given set of
Form | Full entry | Collapsed | Final |
---|---|---|---|
2 + 0 | |||
1 × |
|||
1 × 1 × |
|||
1 × 1 × 1 × |
|||
1 × 1 × 1 × 1 × |
|||
1 × 1 × 1 × 1 × 1 × |
|||
1 + 1 | 2 |
||
2 |
|||
2 |
|||
2 |
|||
2 |
|||
1 × |
2 |
||
1 × |
2 |
||
1 × |
2 |
||
1 × |
2 |
||
1 × 1 × |
2 |
||
1 × 1 × |
2 |
||
1 × 1 × |
2 |
||
1 × 1 × 1 × |
2 |
||
1 × 1 × 1 × |
2 |
||
1 × 1 × 1 × 1 × |
2 |
So those are all the possible values of that one expression. Where were we using it again?
Right! We were going to add those final items together. Let's see what that looks like:
Conclusion
This overall process can be followed for a multinomial of any size