I have asked a question here. I want to reproduce the coefficients of a generating function of the form: $$(1 + x)^2 (1 + x + x^2 + x^3+\cdots+x^n)^{n-1}$$ It is important that $x_0$ and $x_n$ to be strictly 0 or 1 and $x_1$ to $x_2$ can be any number within 0 and n (nothing higher). Here are two examples: For $n=3$ we have:
In: (1 + x)^2 (1 + x + x^2 + x^3)^2 // Expand
Out: 1 + 4 x + 8 x^2 + 12 x^3 + 14 x^4 + 12 x^5 + 8 x^6 + 4 x^7 + x^8
We can produce the coefficients as:
n = 3;
m = n + 1;
tabel = Table[
v = Array[x, m, 0];
eqn = Total[v] == t;
constraints =
And[0 <= v[[1]] <= 1, 0 <= v[[2]] <= n, 0 <= v[[3]] <= n,
0 <= v[[4]] <= 1];
v /. Solve[{eqn, constraints}, v, Integers], {t, 0, 8}];
Table[Length[tabel[[i]]], {i, Length[tabel]}]
which gives:
{1, 4, 8, 12, 14, 12, 8, 4, 1}
as desired. For $n=4$ one had to add extra constraint and change the $t$ range. We have:
In: (1 + x)^2 (1 + x + x^2 + x^3 + x^4)^3 // Expand
Out: 1 + 5 x + 13 x^2 + 25 x^3 + 41 x^4 + 58 x^5 + 70 x^6 + 74 x^7 +
70 x^8 + 58 x^9 + 41 x^10 + 25 x^11 + 13 x^12 + 5 x^13 + x^14
thus $t$ should be from 0 to 14, so we have:
n = 4;
m = n + 1;
tabel = Table[
v = Array[x, m, 0];
eqn = Total[v] == t;
constraints =
And[0 <= v[[1]] <= 1, 0 <= v[[2]] <= n, 0 <= v[[3]] <= n,
0 <= v[[4]] <= n, 0 <= v[[5]] <= 1];
v /. Solve[{eqn, constraints}, v, Integers], {t, 0, 14}];
Table[Length[tabel[[i]]], {i, Length[tabel]}]
I wonder if these modifications can be done automatically so that one doesn't have to add a constraint by hand and change the range.
Note: I want the output table
to be in a format so that I can see all of the possibilities of the sums.
For instance, for n=3, table[[2]]
should give all of the possibilities such that the total is equal to 1.
{{0, 0, 0, 1}, {0, 0, 1, 0}, {0, 1, 0, 0}, {1, 0, 0, 0}}