Formalization Blueprint for “An Introduction to Algebraic Combinatorics” by Darij Grinberg

1.10 Integer compositions

1.10.1 Compositions

Definition 1.291
#

(a) An (integer) composition means a (finite) tuple of positive integers.

(b) The size of an integer composition \(\alpha =\left( \alpha _{1},\alpha _{2},\ldots ,\alpha _{m}\right) \) is defined to be the integer \(\alpha _{1}+\alpha _{2}+\cdots +\alpha _{m}\). It is written \(\left\vert \alpha \right\vert \).

(c) The length of an integer composition \(\alpha =\left( \alpha _{1},\alpha _{2},\ldots ,\alpha _{m}\right) \) is defined to be the integer \(m\). It is written \(\ell \left( \alpha \right) \).

(d) Let \(n\in \mathbb {N}\). A composition of \(n\) means a composition whose size is \(n\).

(e) Let \(n\in \mathbb {N}\) and \(k\in \mathbb {N}\). A composition of \(n\) into \(k\) parts is a composition whose size is \(n\) and whose length is \(k\).

Lemma 1.292

The size of the empty composition is \(0\).

Proof

By definition.

Lemma 1.293

The length of the empty composition is \(0\).

Proof

By definition.

Lemma 1.294

The size of a composition \((a) \cdot \alpha \) (prepending \(a\) to \(\alpha \)) is \(a + |\alpha |\).

Proof

By definition.

Lemma 1.295

The length of a composition \((a) \cdot \alpha \) (prepending \(a\) to \(\alpha \)) is \(\ell (\alpha ) + 1\).

Proof

By definition of length.

Lemma 1.296

The size of a composition is at least its length: \(\ell (\alpha ) \leq |\alpha |\), since each part is a positive integer (at least \(1\)).

Proof

By induction on the composition. The base case (empty composition) is trivial. For the inductive step, \(|\alpha | = a + |\beta | \geq 1 + \ell (\beta ) = \ell (\alpha )\) since \(a \geq 1\).

Lemma 1.297

The empty list is the only composition of size \(0\).

Proof

If \(\alpha = (a) \cdot \beta \), then \(|\alpha | = a + |\beta | \geq 1 {\gt} 0\) since \(a\) is a positive integer. Hence a composition of size \(0\) must be empty.

Lemma 1.298

The definition of compositions of \(n\) from Definition 1.291 (as lists of positive integers with given sum) is equivalent to the definition from Mathlib’s composition type.

  • One or more equations did not get rendered due to their size.
Proof

Convert between lists of positive integers and lists of natural numbers with positivity proofs.

Lemma 1.299

There is an equivalence between compositions of \(n\) into \(k\) parts and the subtype of Mathlib compositions of \(n\) having length \(k\).

  • One or more equations did not get rendered due to their size.
Proof

Follows from the equivalence in Lemma 1.298, restricting to compositions of the appropriate length.

1.10.2 Helpers for Theorem 1.304

Lemma 1.300

There is an equivalence between Mathlib’s compositions of \(n\) and subsets of \(\{ 1, 2, \ldots , n-1\} \), obtained by composing two Mathlib equivalences.

Proof

This is the composition of the Mathlib equivalences between compositions of \(n\), composition-as-set representations, and finite subsets of \(\{ 1, 2, \ldots , n-1\} \).

Lemma 1.301

For \(n {\gt} 0\) and a composition-as-set \(c\) of \(n\), the cardinality of the associated finset of interior boundary points \(S(c)\) satisfies \(|S(c)| + 1 = \ell (c)\). This holds because the equivalence extracts the interior boundary points (those other than \(0\) and \(n\)), and a composition of length \(k\) has exactly \(k - 1\) such points.

Proof

The boundary set of \(c\) has \(\ell (c) + 1\) elements (including \(0\) and \(n\)). The equivalence maps to the interior points, obtained by removing \(0\) and \(n\). The resulting finset has \(\ell (c) + 1 - 2 = \ell (c) - 1\) elements.

Lemma 1.302

For \(n {\gt} 0\) and a composition \(c\) of \(n\), the finset \(S(c)\) corresponding to \(c\) under the composition-to-finset equivalence satisfies \(|S(c)| + 1 = \ell (c)\).

Proof

Follows directly from Lemma 1.301 applied to the composition-as-set representation of \(c\).

Lemma 1.303

For \(n {\gt} 0\) and \(k {\gt} 0\), the number of compositions of \(n\) into \(k\) parts (using Mathlib’s composition type) equals \(\binom {n-1}{k-1}\). The proof establishes a bijection between compositions of length \(k\) and \((k-1)\)-element subsets of \(\{ 1, 2, \ldots , n-1\} \) via Lemma 1.302, then uses the formula for counting subsets of a given size.

Proof

Compositions of length \(k\) correspond (via the composition-to-finset equivalence) to \((k-1)\)-element subsets of \(\{ 1, 2, \ldots , n-1\} \). These are counted by \(\binom {n-1}{k-1}\).

Theorem 1.304

Let \(n,k\in \mathbb {N}\). Then, the # of compositions of \(n\) into \(k\) parts is

\[ \binom {n-1}{n-k}=\begin{cases} \binom {n-1}{k-1}, & \text{if }n{\gt}0;\\ \delta _{k,0}, & \text{if }n=0. \end{cases} \]
Proof

Fix \(k\in \mathbb {N}\), but don’t fix \(n\). Let

\begin{equation} a_{n,k}=\left( \text{\# of compositions of }n\text{ into }k\text{ parts}\right) . \label{pf.thm.fps.comps.n-into-k-parts.1st.ank=}\end{equation}
35

We want to find \(a_{n,k}\). We define the generating function

\begin{equation} A_{k}:=\sum _{n\in \mathbb {N}}a_{n,k}x^{n}=\left( a_{0,k},a_{1,k},a_{2,k},\ldots \right) \in \mathbb {Q}\left[ \left[ x\right] \right] . \label{pf.thm.fps.comps.n-into-k-parts.1st.Ak=}\end{equation}
36

Let us write \(\mathbb {P}\) for the set \(\left\{ 1,2,3,\ldots \right\} \). Then, a composition of \(n\) into \(k\) parts is nothing but a \(k\)-tuple \(\left( \alpha _{1},\alpha _{2},\ldots ,\alpha _{k}\right) \in \mathbb {P}^{k}\) satisfying \(\alpha _{1}+\alpha _{2}+\cdots +\alpha _{k}=n\). Hence, (35) can be rewritten as

\begin{align} a_{n,k} & =\left( \text{\# of all }k\text{-tuples }\left( \alpha _{1},\alpha _{2},\ldots ,\alpha _{k}\right) \in \mathbb {P}^{k}\text{ satisfying }\alpha _{1}+\alpha _{2}+\cdots +\alpha _{k}=n\right) \nonumber \\ & =\sum \limits _{\substack{[\left , (, , \alpha , _, {, 1, }, ,, \alpha , _, {, 2, }, ,, \ldots , ,, \alpha , _, {, k, }, \right , ), , \in , \mathbb , {, P, }, ^, {, k, }, ;, \\ , \alpha , _, {, 1, }, +, \alpha , _, {, 2, }, +, \cdots , +, \alpha , _, {, k, }, =, n]}}1. \label{pf.thm.fps.comps.n-into-k-parts.1st.ank=2}\end{align}

Thus, we can rewrite the equality \(A_{k}=\sum _{n\in \mathbb {N}}a_{n,k}x^{n}\) as

\begin{align*} A_{k} & =\sum _{n\in \mathbb {N}}\left( \sum \limits _{\substack{[\left , (, , \alpha , _, {, 1, }, ,, \alpha , _, {, 2, }, ,, \ldots , ,, \alpha , _, {, k, }, \right , ), , \in , \mathbb , {, P, }, ^, {, k, }, ;, \\ , \alpha , _, {, 1, }, +, \alpha , _, {, 2, }, +, \cdots , +, \alpha , _, {, k, }, =, n]}}1\right) x^{n}=\sum _{n\in \mathbb {N}}\ \ \sum \limits _{\substack{[\left , (, , \alpha , _, {, 1, }, ,, \alpha , _, {, 2, }, ,, \ldots , ,, \alpha , _, {, k, }, \right , ), , \in , \mathbb , {, P, }, ^, {, k, }, ;, \\ , \alpha , _, {, 1, }, +, \alpha , _, {, 2, }, +, \cdots , +, \alpha , _, {, k, }, =, n]}}\ \ \underbrace{x^{n}}_{\substack{[=, x, ^, {, \alpha , _, {, 1, }, +, \alpha , _, {, 2, }, +, \cdots , +, \alpha , _, {, k, }, }, \\ , \text , {, (, s, i, n, c, e, , }, \alpha , _, {, 1, }, +, \alpha , _, {, 2, }, +, \cdots , +, \alpha , _, {, k, }, =, n, \text , {, ), }]}}\\ & =\underbrace{\sum _{n\in \mathbb {N}}\ \ \sum \limits _{\substack{[\left , (, , \alpha , _, {, 1, }, ,, \alpha , _, {, 2, }, ,, \ldots , ,, \alpha , _, {, k, }, \right , ), , \in , \mathbb , {, P, }, ^, {, k, }, ;, \\ , \alpha , _, {, 1, }, +, \alpha , _, {, 2, }, +, \cdots , +, \alpha , _, {, k, }, =, n]}}}_{=\sum \limits _{\left( \alpha _{1},\alpha _{2},\ldots ,\alpha _{k}\right) \in \mathbb {P}^{k}}}\ \ \underbrace{x^{\alpha _{1}+\alpha _{2}+\cdots +\alpha _{k}}}_{=x^{\alpha _{1}}x^{\alpha _{2}}\cdots x^{\alpha _{k}}}=\sum \limits _{\left( \alpha _{1},\alpha _{2},\ldots ,\alpha _{k}\right) \in \mathbb {P}^{k}}x^{\alpha _{1}}x^{\alpha _{2}}\cdots x^{\alpha _{k}}\\ & =\left( \sum _{\alpha _{1}\in \mathbb {P}}x^{\alpha _{1}}\right) \left( \sum _{\alpha _{2}\in \mathbb {P}}x^{\alpha _{2}}\right) \cdots \left( \sum _{\alpha _{k}\in \mathbb {P}}x^{\alpha _{k}}\right) \\ & =\left( \sum _{n\in \mathbb {P}}x^{n}\right) ^{k}\end{align*}

(here, we have renamed all the \(k\) summation indices as \(n\), and realized that all \(k\) sums are identical). Since

\[ \sum _{n\in \mathbb {P}}x^{n}=x^{1}+x^{2}+x^{3}+\cdots =x\underbrace{\left( 1+x+x^{2}+\cdots \right) }_{=\frac{1}{1-x}}=x\cdot \frac{1}{1-x}=\frac{x}{1-x}, \]

this can be rewritten further as

\begin{equation} A_{k}=\left( \frac{x}{1-x}\right) ^{k}=x^{k}\left( 1-x\right) ^{-k}. \label{pf.thm.fps.comps.num-comps-n-k.Ak=}\end{equation}
38

In order to simplify this, we need to expand \(\left( 1-x\right) ^{-k}\). This is routine by now: Theorem 1.119 (applied to \(-k\) instead of \(n\)) yields

\[ \left( 1+x\right) ^{-k}=\sum _{j\in \mathbb {N}}\binom {-k}{j}x^{j}. \]

Substituting \(-x\) for \(x\) on both sides of this equality (i.e., applying the map \(f\mapsto f\circ \left( -x\right) \)), we obtain

\begin{align} \left( 1-x\right) ^{-k} & =\sum _{j\in \mathbb {N}}\underbrace{\binom {-k}{j}}_{=\left( -1\right) ^{j}\binom {j+k-1}{j}}\underbrace{\left( -x\right) ^{j}}_{=\left( -1\right) ^{j}x^{j}}=\sum _{j\in \mathbb {N}}\left( -1\right) ^{j}\binom {j+k-1}{j}\cdot \left( -1\right) ^{j}x^{j}\nonumber \\ & =\sum _{j\in \mathbb {N}}\underbrace{\left( \left( -1\right) ^{j}\right) ^{2}}_{=1}\binom {j+k-1}{j}x^{j}=\sum _{j\in \mathbb {N}}\binom {j+k-1}{j}x^{j}\label{pf.thm.fps.comps.num-comps-n-k.1-x-k}\\ & =\sum \limits _{n\geq k}\binom {n-1}{n-k}x^{n-k}\nonumber \end{align}

(where we used Theorem 1.120, applied to \(k\) and \(j\) instead of \(n\) and \(k\), to simplify \(\binom {-k}{j}\); and we have substituted \(n-k\) for \(j\) in the sum). Hence, our above computation of \(A_{k}\) can be completed as follows:

\begin{align*} A_{k} & =x^{k}\underbrace{\left( 1-x\right) ^{-k}}_{=\sum \limits _{n\geq k}\binom {n-1}{n-k}x^{n-k}}=x^{k}\sum \limits _{n\geq k}\binom {n-1}{n-k}x^{n-k}\\ & =\sum \limits _{n\geq k}\binom {n-1}{n-k}\underbrace{x^{k}x^{n-k}}_{=x^{n}}=\sum \limits _{n\geq k}\binom {n-1}{n-k}x^{n}=\sum \limits _{n\in \mathbb {N}}\binom {n-1}{n-k}x^{n}\end{align*}

(here, we have extended the range of the summation from all \(n\geq k\) to all \(n\in \mathbb {N}\); this did not change the sum, since all the newly introduced addends with \(n{\lt}k\) are \(0\)). Comparing coefficients, we thus obtain

\begin{equation} a_{n,k}=\binom {n-1}{n-k}\ \ \ \ \ \ \ \ \ \ \text{for each }n\in \mathbb {N}. \label{pf.thm.fps.comps.n-into-k-parts.1st.rhs1}\end{equation}
40

If \(n{\gt}0\), then we can rewrite the right hand side of this equality as \(\binom {n-1}{k-1}\) (using Theorem 1.9). However, if \(n=0\), then this right hand side equals \(\delta _{k,0}\) instead (where we are using Definition 1.167). Thus, we can rewrite (40) as

\begin{equation} a_{n,k}=\begin{cases} \binom {n-1}{k-1}, & \text{if }n{\gt}0;\\ \delta _{k,0}, & \text{if }n=0 \end{cases} \ \ \ \ \ \ \ \ \ \ \text{for each }n\in \mathbb {N}. \label{pf.thm.fps.comps.n-into-k-parts.1st.rhs2}\end{equation}
41

In the Lean formalization, this proceeds by:

  1. Using the equivalence between our compositions and Mathlib’s composition type (Lemma 1.299).

  2. Using Mathlib’s equivalence between compositions of \(n\) and subsets of \(\{ 1, 2, \ldots , n-1\} \).

  3. Showing that compositions of length \(k\) correspond to subsets of size \(k-1\).

  4. Computing \(|\{ S \subseteq \{ 1, \ldots , n-1\} \mid |S| = k-1\} | = \binom {n-1}{k-1}\) using the formula for counting subsets of a given size.

The edge cases are:

  • For \(n {\gt} 0\) and \(k = 0\): There are no compositions (since parts are positive, and a tuple of zero positive integers sums to \(0\), not \(n\)). This gives \(0 = \binom {n-1}{-1} = 0\).

  • For \(n = 0\): The empty tuple is the only composition of \(0\) (by Lemma 1.297), and it has length \(0\). So the count is \(\delta _{k,0}\).

Theorem 1.305

Let \(n\in \mathbb {N}\). Then, the # of compositions of \(n\) is

\[ \begin{cases} 2^{n-1}, & \text{if }n{\gt}0;\\ 1, & \text{if }n=0. \end{cases} \]
Proof

If \(n=0\), then the # of compositions of \(n\) is \(1\) (since the empty tuple \(\left( {}\right) \) is the only composition of \(0\)). Thus, for the rest of this proof, we WLOG assume that \(n\neq 0\). Hence, we must prove that the # of compositions of \(n\) is \(2^{n-1}\).

If \(\left( n_{1},n_{2},\ldots ,n_{m}\right) \) is a composition of \(n\), then \(m\in \left\{ 1,2,\ldots ,n\right\} \). In other words, any composition of \(n\) is a composition of \(n\) into \(k\) parts for some \(k\in \left\{ 1,2,\ldots ,n\right\} \). Hence,

\begin{align*} & \left( \text{\# of compositions of }n\right) \\ & =\sum _{k\in \left\{ 1,2,\ldots ,n\right\} }\underbrace{\left( \text{\# of compositions of }n\text{ into }k\text{ parts}\right) }_{=\binom {n-1}{n-k}}\\ & =\sum _{k\in \left\{ 1,2,\ldots ,n\right\} }\binom {n-1}{n-k}=\sum _{k=1}^{n}\binom {n-1}{n-k}=\sum _{k=0}^{n-1}\binom {n-1}{k}\end{align*}

(where the second equality uses Theorem 1.304; and we have substituted \(k\) for \(n-k\) in the sum). Comparing this with

\begin{align*} 2^{n-1} & =\left( 1+1\right) ^{n-1}=\sum _{k=0}^{n-1}\binom {n-1}{k}\underbrace{1^{k}1^{n-1-k}}_{=1}\ \ \ \ \ \ \ \ \ \ \left( \text{by the binomial theorem}\right) \\ & =\sum _{k=0}^{n-1}\binom {n-1}{k}, \end{align*}

we obtain \(\left( \text{\# of compositions of }n\right) =2^{n-1}\).

In the Lean formalization, this is proved by establishing an equivalence with Mathlib’s composition type (Lemma 1.298) and using Mathlib’s theorem on the cardinality of compositions.

1.10.3 Weak compositions

Definition 1.306
#

(a) An (integer) weak composition means a (finite) tuple of nonnegative integers.

(b) The size of a weak composition \(\alpha =\left( \alpha _{1},\alpha _{2},\ldots ,\alpha _{m}\right) \) is defined to be the integer \(\alpha _{1}+\alpha _{2}+\cdots +\alpha _{m}\). It is written \(\left\vert \alpha \right\vert \).

(c) The length of a weak composition \(\alpha =\left( \alpha _{1},\alpha _{2},\ldots ,\alpha _{m}\right) \) is defined to be the integer \(m\). It is written \(\ell \left( \alpha \right) \).

(d) Let \(n\in \mathbb {N}\). A weak composition of \(n\) means a weak composition whose size is \(n\).

(e) Let \(n\in \mathbb {N}\) and \(k\in \mathbb {N}\). A weak composition of \(n\) into \(k\) parts is a weak composition whose size is \(n\) and whose length is \(k\).

Lemma 1.307

The list-based representation of weak compositions of \(n\) into \(k\) parts is equivalent to the function-based representation \(\{ f : \{ 0, 1, \ldots , k-1\} \to \mathbb {N} \mid \sum _{i} f(i) = n\} \).

  • One or more equations did not get rendered due to their size.
Proof

The forward direction converts a list \([\alpha _0, \alpha _1, \ldots , \alpha _{k-1}]\) to the function \(i \mapsto \alpha _i\). The backward direction converts a function \(f\) to the list \([f(0), f(1), \ldots , f(k-1)]\).

Lemma 1.308

The function-based weak compositions of \(n\) into \(k\) parts are equivalent to multisets of size \(n\) from a \(k\)-element alphabet.

  • One or more equations did not get rendered due to their size.
Proof

The bijection sends \(f : \{ 0, \ldots , k-1\} \to \mathbb {N}\) with \(\sum _i f(i) = n\) to the multiset containing \(f(i)\) copies of \(i\) for each \(i\), and the inverse sends a multiset \(m\) to the function \(i \mapsto \text{count}(i, m)\).

Lemma 1.309

There is a bijection between weak compositions of \(n\) into \(k\) parts and compositions of \(n+k\) into \(k\) parts, defined by adding \(1\) to each entry.

  • One or more equations did not get rendered due to their size.
Proof

If \(\left( \alpha _{1},\alpha _{2},\ldots ,\alpha _{k}\right) \) is a weak composition of \(n\) into \(k\) parts, then \(\left( \alpha _{1}+1,\alpha _{2}+1,\ldots ,\alpha _{k}+1\right) \) is a composition of \(n+k\) into \(k\) parts (since adding \(1\) to each nonnegative entry yields a positive integer, and the sum increases by \(k\)). The inverse map subtracts \(1\) from each entry.

Lemma 1.310

The number of function-based weak compositions of \(n\) into \(k\) parts equals \(\binom {n+k-1}{n}\). The proof uses the equivalence with multisets of size \(n\) from a \(k\)-element alphabet (Lemma 1.308) and the formula for counting such multisets.

Proof

By the equivalence with multisets of size \(n\) from a \(k\)-element alphabet and the formula \(\binom {n + k - 1}{n}\) for counting such multisets.

Theorem 1.311

Let \(n,k\in \mathbb {N}\). Then, the # of weak compositions of \(n\) into \(k\) parts is

\[ \binom {n+k-1}{n}=\begin{cases} \binom {n+k-1}{k-1}, & \text{if }k{\gt}0;\\ \delta _{n,0}, & \text{if }k=0. \end{cases} \]
Proof

Adding \(1\) to each entry of a weak composition of \(n\) into \(k\) parts gives a composition of \(n+k\) into \(k\) parts (Lemma 1.309). This map is a bijection (its inverse subtracts \(1\) from each entry). Thus, by the bijection principle, we have

\begin{align*} & \left\vert \left\{ \text{weak compositions of }n\text{ into }k\text{ parts}\right\} \right\vert \\ & =\left\vert \left\{ \text{compositions of }n+k\text{ into }k\text{ parts}\right\} \right\vert \\ & =\left( \text{\# of compositions of }n+k\text{ into }k\text{ parts}\right) \\ & =\binom {n+k-1}{n+k-k} =\binom {n+k-1}{n}. \end{align*}

(Here, the last step follows by Theorem 1.304, applied to \(n+k\) instead of \(n\).) It remains to prove that this equals \(\begin{cases} \binom {n+k-1}{k-1}, & \text{if }k{\gt}0;\\ \delta _{n,0}, & \text{if }k=0 \end{cases} \) as well. If \(k=0\), then it is clear by inspection; otherwise it follows from Theorem 1.9.

In the Lean formalization, the primary proof uses the “stars and bars” technique: weak compositions of \(n\) into \(k\) parts are equivalent to multisets of size \(n\) from a \(k\)-element alphabet, via Lemmas 1.307 and 1.308. These are counted by \(\binom {n+k-1}{n}\) using the formula for counting multisets.

1.10.4 Weak compositions with entries from \(\left\{ 0,1,\ldots ,p-1\right\} \)

Lemma 1.312

The geometric series identity: \(\left(\sum _{i=0}^{p-1} x^i\right)(1 - x) = 1 - x^p\).

Proof

By induction on \(p\) and ring arithmetic.

Lemma 1.313

The geometric series equals \((1 - x^p) \cdot (1-x)^{-1}\):

\[ \sum _{i=0}^{p-1} x^i = (1 - x^p) \cdot (1-x)^{-1}. \]
Proof

Multiply both sides by \((1 - x)\) and use Lemma 1.312.

Lemma 1.314

For all \(k \in \mathbb {N}\), \(\left((1 - x)^{-1}\right)^k = (1 - x)^{-k}\). That is, the \(k\)-th power of the formal inverse of \((1 - x)\) equals the formal inverse of \((1 - x)^k\).

Proof

By induction on \(k\), using the identity \((1-x)^{-(a+b)} = (1-x)^{-a} \cdot (1-x)^{-b}\) from Mathlib.

Lemma 1.315

For \(k {\gt} 0\), the coefficient of \(x^n\) in \((1-x)^{-k}\) is \(\binom {k - 1 + n}{k - 1}\).

Proof

This is a standard result from Mathlib’s power series library.

Lemma 1.316

The generating function for bounded weak compositions satisfies \(W_{k,p} = (1-x^p)^k \cdot (1-x)^{-k}\).

Proof

Use Lemma 1.313 and raise both sides to the \(k\)-th power, using Lemma 1.314 to simplify \(\left((1-x)^{-1}\right)^k = (1-x)^{-k}\).

Lemma 1.317

The coefficient of \(x^n\) in the generating function \(W_{k,p}\) equals the number of bounded weak compositions of \(n\) into \(k\) parts with entries in \(\{ 0, 1, \ldots , p-1\} \).

Proof

Use the product rule for power series coefficients and show that the product of indicator functions picks out exactly the bounded weak compositions summing to \(n\).

Lemma 1.318

The coefficient of \(x^n\) in \(W_{k,p}\) equals the inclusion-exclusion formula:

\[ [x^n] W_{k,p} = \sum _{\substack{[j, , \in , \{ , 0, ,, \ldots , ,, k, \} , , \\ , , p, j, , \leq , n]}} (-1)^j \binom {k}{j} \binom {n - pj + k - 1}{n - pj}. \]
theorem AlgebraicCombinatorics.BoundedWeakComposition.coeff_genFun_formula (n k p : ) :
(PowerSeries.coeff n) (genFun k p) = jFinset.range (k + 1) with p * j n, (-1) ^ j * (k.choose j) * ((n - p * j + k - 1).choose (n - p * j))
Proof

Expand the product \((1-x^p)^k \cdot (1-x)^{-k}\) using the binomial theorem for \((1-x^p)^k\) and the expansion of \((1-x)^{-k}\) via Lemma 1.315, then extract the coefficient of \(x^n\) via the convolution formula.

Theorem 1.319

Let \(n,k,p\in \mathbb {N}\). Then, the # of \(k\)-tuples \(\left( \alpha _{1},\alpha _{2},\ldots ,\alpha _{k}\right) \in \left\{ 0,1,\ldots ,p-1\right\} ^{k}\) satisfying \(\alpha _{1}+\alpha _{2}+\cdots +\alpha _{k}=n\) is

\[ \sum _{j=0}^{k}\left( -1\right) ^{j}\binom {k}{j}\binom {n-pj+k-1}{n-pj}. \]
theorem AlgebraicCombinatorics.BoundedWeakComposition.card (n k p : ) :
(Fintype.card (BoundedWeakComposition n k p)) = jFinset.range (k + 1) with p * j n, (-1) ^ j * (k.choose j) * ((n - p * j + k - 1).choose (n - p * j))
Proof

We fix three nonnegative integers \(n\), \(k\) and \(p\). We are looking for the # of \(k\)-tuples \(\left( \alpha _{1},\alpha _{2},\ldots ,\alpha _{k}\right) \in \left\{ 0,1,\ldots ,p-1\right\} ^{k}\) satisfying \(\alpha _{1}+\alpha _{2}+\cdots +\alpha _{k}=n\). In other words, we are looking for the # of all weak compositions of \(n\) into \(k\) parts with the property that each entry is \({\lt}p\). Let us denote this # by \(w_{n,k,p}\).

We invoke a generating function. Define the FPS

\[ W_{k,p}:=\sum _{n\in \mathbb {N}}w_{n,k,p}x^{n}\in \mathbb {Q}\left[ \left[ x\right] \right] . \]

For each \(n\in \mathbb {N}\), we have

\begin{align*} w_{n,k,p} & =\sum \limits _{\substack{[\left , (, , \alpha , _, {, 1, }, ,, \alpha , _, {, 2, }, ,, \ldots , ,, \alpha , _, {, k, }, \right , ), , \in , \left , \{ , , 0, ,, 1, ,, \ldots , ,, p, -, 1, \right , \} , , ^, {, k, }, ;, \\ , \alpha , _, {, 1, }, +, \alpha , _, {, 2, }, +, \cdots , +, \alpha , _, {, k, }, =, n]}}1. \end{align*}

Thus, we can rewrite \(W_{k,p}=\sum _{n\in \mathbb {N}}w_{n,k,p}x^{n}\) as

\begin{align*} W_{k,p} & =\sum \limits _{\left( \alpha _{1},\alpha _{2},\ldots ,\alpha _{k}\right) \in \left\{ 0,1,\ldots ,p-1\right\} ^{k}}x^{\alpha _{1}}x^{\alpha _{2}}\cdots x^{\alpha _{k}}\\ & =\left( \sum _{n\in \left\{ 0,1,\ldots ,p-1\right\} }x^{n}\right) ^{k}\end{align*}

(by the product rule). Since

\[ \sum _{n\in \left\{ 0,1,\ldots ,p-1\right\} }x^{n}=x^{0}+x^{1}+\cdots +x^{p-1}=\frac{1-x^{p}}{1-x}, \]

this can be rewritten further as

\begin{equation} W_{k,p}=\left( \frac{1-x^{p}}{1-x}\right) ^{k}=\left( 1-x^{p}\right) ^{k}\left( 1-x\right) ^{-k}. \label{pf.thm.fps.comps.num-wpcomps-n-k.3}\end{equation}
50

In order to expand the right hand side, let us expand \(\left( 1-x^{p}\right) ^{k}\) and \(\left( 1-x\right) ^{-k}\) separately.

The binomial theorem yields

\[ \left( 1-x^{p}\right) ^{k}=\sum _{j\in \mathbb {N}}\left( -1\right) ^{j}\binom {k}{j}x^{pj}. \]

Multiplying this by (39), we obtain

\begin{align*} & \left( 1-x^{p}\right) ^{k}\left( 1-x\right) ^{-k}\\ & =\sum _{n\in \mathbb {N}}\left( \sum _{\substack{[\left , (, , i, ,, j, \right , ), , \in , \mathbb , {, N, }, \times , \mathbb , {, N, }, ;, \\ , p, j, +, i, =, n]}}\left( -1\right) ^{j}\binom {k}{j}\binom {i+k-1}{i}\right) x^{n}. \end{align*}

Thus, (50) becomes

\[ W_{k,p}=\sum _{n\in \mathbb {N}}\left( \sum _{\substack{[\left , (, , i, ,, j, \right , ), , \in , \mathbb , {, N, }, \times , \mathbb , {, N, }, ;, \\ , p, j, +, i, =, n]}}\left( -1\right) ^{j}\binom {k}{j}\binom {i+k-1}{i}\right) x^{n}. \]

Comparing coefficients, we find that each \(n\in \mathbb {N}\) satisfies

\begin{align*} w_{n,k,p} & =\sum _{j=0}^{k}\left( -1\right) ^{j}\binom {k}{j}\binom {n-pj+k-1}{n-pj}. \end{align*}
Lemma 1.320

Let \(n,k\in \mathbb {N}\). The number of binary \(k\)-strings with exactly \(n\) ones is \(\binom {k}{n}\).

Proof

Establish a bijection between binary \(k\)-strings with \(n\) ones and \(n\)-element subsets of \(\{ 1, 2, \ldots , k\} \) (a binary string corresponds to the set of positions where a \(1\) appears). Then use the formula for counting subsets of a given size.

Proposition 1.321

Let \(n,k\in \mathbb {N}\). Then,

\[ \binom {k}{n}=\sum _{j=0}^{k}\left( -1\right) ^{j}\binom {k}{j}\binom {n-2j+k-1}{n-2j}. \]
theorem AlgebraicCombinatorics.binom_sum_identity (n k : ) :
(k.choose n) = jFinset.range (k + 1) with 2 * j n, (-1) ^ j * (k.choose j) * ((n - 2 * j + k - 1).choose (n - 2 * j))
Proof

The \(k\)-tuples \(\left( \alpha _{1},\alpha _{2},\ldots ,\alpha _{k}\right) \in \left\{ 0,1\right\} ^{k}\) are just the binary \(k\)-strings, i.e., the \(k\)-tuples formed of \(0\)s and \(1\)s. Imposing the condition \(\alpha _{1}+\alpha _{2}+\cdots +\alpha _{k}=n\) on such a \(k\)-tuple is tantamount to requiring that it contain exactly \(n\) many \(1\)s. Therefore, the # of all \(k\)-tuples \(\left( \alpha _{1},\alpha _{2},\ldots ,\alpha _{k}\right) \in \left\{ 0,1\right\} ^{k}\) satisfying \(\alpha _{1}+\alpha _{2}+\cdots +\alpha _{k}=n\) is \(\binom {k}{n}\), since we have to choose which \(n\) of its \(k\) positions will be occupied by \(1\)s (Lemma 1.320). However, Theorem 1.319 (applied to \(p=2\)) yields that this # equals \(\sum _{j=0}^{k}\left( -1\right) ^{j}\binom {k}{j}\binom {n-2j+k-1}{n-2j}\). Comparing these two results, we obtain the identity.