Skip to content

Commit f75e9bf

Browse files
The binomial sums of chapter 8 are answered in closed form (#1461)
sum(k binomial(n, k), k, 0, n) was left as written. BinomialIdentities reads the sums of chapter 8 of Sullivan and Mackey's An Introduction to Proofs that are not the binomial theorem itself: - a polynomial in the index beside the coefficient, in the falling factorial basis by the Stirling numbers of the second kind, since k^(j falling) binomial(n, k) is n^(j falling) binomial(n - j, k - j) by the chairperson identity applied j times and what is left is the binomial theorem: sum(k binomial(n, k)) is n 2^(n - 1) (Prop 8.4.4), and the same beside x^k y^(n - k); - Vandermonde's convolution, sum(binomial(a, i) binomial(b, k - i), i, 0, k) = binomial(a + b, k) (Prob 8.9.16), the range written to k, a or b, and its square case sum(binomial(n, k)^2) = binomial(2n, n) (Prob 8.9.34); - the summation identity sum(binomial(i, k), i, 0, n) = binomial(n + 1, k + 1) (Thm 8.4.6); - the sums over the even or the odd indices, each 2^(n - 1) for n >= 1 (Ex 8.3.11), with the even sum 1 and the odd sum empty at n = 0. The coefficient as its three factorials is read too, the top one folded into a number where the bound is concrete. The identities under forall are decided from the closed forms. Suite 12567 passed; the kernel gate passed on all 19 gated benchmarks. Part of #1409. Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 01a944f commit f75e9bf

7 files changed

Lines changed: 378 additions & 4 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -976,6 +976,23 @@ the remainder of `acsch(c x)^2/x^4` wrongly along the way
976976
| `"(a+b*acsch(c*x))^2/x^4".Integrate("x")` | left unevaluated | the antiderivative |
977977
| `"x^2/sqrt(1/(a^2*x^2)-1)".Integrate("x")` | left unevaluated | `sgn(x) |a| (2 (1 - a^2 x^2)^(3/2)/3 - 2 sqrt(1 - a^2 x^2))/(2 a^4)` up to the form -- with `sgn(x)` in front, where an answer written in `|x|` used to be given for such shapes |
978978

979+
### The binomial sums of chapter 8 are answered in closed form
980+
981+
`sum(k binomial(n, k), k, 0, n)` was left as written; it is `n 2^(n - 1)`, and with it a
982+
polynomial of any degree beside the coefficient (in the falling factorial basis, by the
983+
chairperson identity), Vandermonde's convolution, `sum(binomial(n, k)^2, k, 0, n) = binomial(2n, n)`,
984+
the summation identity `sum(binomial(i, k), i, 0, n) = binomial(n + 1, k + 1)`, and the sums over
985+
the even or the odd indices, each `2^(n - 1)` — Prop 8.4.4, Thm 8.4.6, Ex 8.3.11 and Probs
986+
8.9.16 and 8.9.34 of Sullivan and Mackey's *An Introduction to Proofs*
987+
([#1409](https://github.com/asc-community/AngouriMath/issues/1409)).
988+
989+
| Input | Was (2.5.0) | Now |
990+
|---|---|---|
991+
| `"sum(k*binomial(n,k), k, 0, n)".ToEntity().Evaled` | `UnhandledParseException` — `binomial` is new since | `n 2^(n - 1) provided n >= 0`, `0` otherwise |
992+
| `"sum(n!/(k!*(n-k)!)*k, k, 0, n)".ToEntity().Evaled` | as written | the same |
993+
| `"sum(binomial(n,k)^2, k, 0, n)".ToEntity().Evaled` | parse error | `binomial(2 n, n)` |
994+
| `"sum(binomial(i,k), i, 0, n)".ToEntity().Evaled` | parse error | `binomial(n + 1, k + 1)` |
995+
979996
### `binomial(n, k)` is a function
980997

981998
**Addition, not silent.** The binomial coefficient is a node, `Entity.Binomialf`, spelled

‎Sources/.editorconfig‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,12 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
291291
[AngouriMath/Functions/NumberTheory/ResidueClasses.cs]
292292
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n
293293

294+
[Tests/UnitTests/Calculus/BinomialIdentitiesTest.cs]
295+
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n
296+
297+
[AngouriMath/Functions/Algebra/Polynomials/BinomialIdentities.cs]
298+
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n
299+
294300
[Tests/UnitTests/Calculus/PolynomialGeometricSeriesTest.cs]
295301
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n
296302

‎Sources/AngouriMath/Docs/Usage/Syntax.md‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,16 @@ leave the polynomial case alone.
264264
factorial `n (n - 1) ... (n - k + 1) / k!` for any `n` — `binomial(5, 2)` is `10`,
265265
`binomial(-1, 3)` is `-1`, `binomial(1/2, 2)` is `-1/8` — with `0` below `k = 0` and above a
266266
non-negative whole `n`; a symbolic argument keeps the node, which prints as `\binom{n}{k}` in
267-
LaTeX, and a `k` that is not whole evaluates numerically through the gamma function.
267+
LaTeX, and a `k` that is not whole evaluates numerically through the gamma function. A `sum`
268+
over the coefficient is answered in closed form: the binomial theorem
269+
(`sum(binomial(n, k) x^k y^(n - k), k, 0, n)` is `(x + y)^n`), a polynomial in the index beside
270+
it (`sum(k binomial(n, k), k, 0, n)` is `n 2^(n - 1)`, read in the falling factorial basis),
271+
Vandermonde's convolution (`sum(binomial(a, i) binomial(b, k - i), i, 0, k)` is
272+
`binomial(a + b, k)`, and `sum(binomial(n, k)^2, k, 0, n)` is `binomial(2 n, n)`), the summation
273+
identity (`sum(binomial(i, k), i, 0, n)` is `binomial(n + 1, k + 1)`), and the sums over the even
274+
or the odd indices (`sum(binomial(n, 2 l), l, 0, floor(n/2))` is `2^(n - 1)` for `n >= 1`) — the
275+
identities of chapter 8 of Sullivan and Mackey's *An Introduction to Proofs*, so that
276+
`forall n in ZZ+ : sum(k binomial(n, k), k, 0, n) = n 2^(n - 1)` is `True`.
268277

269278
**Calculus** — `derivative(expr, var, order)`, `integral(expr, var)`,
270279
`integral(expr, var, from, to)`, `limit(expr, var, dest)`, `limitleft(...)`, `limitright(...)`;
Lines changed: 263 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,263 @@
1+
//
2+
// Copyright (c) 2019-2026 Angouri.
3+
// AngouriMath is licensed under MIT.
4+
// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.
5+
// Website: https://am.angouri.org.
6+
//
7+
8+
using System.Collections.Generic;
9+
using System.Linq;
10+
using PeterO.Numbers;
11+
using static AngouriMath.Entity;
12+
using static AngouriMath.Entity.Number;
13+
14+
namespace AngouriMath.Functions
15+
{
16+
/// <summary>
17+
/// The binomial sums of chapter 8 of Sullivan and Mackey's <i>An Introduction to Proofs</i>
18+
/// that are not the binomial theorem itself, in closed form: a polynomial in the index
19+
/// beside the coefficient (Prop 8.4.4, <c>sum(k binomial(n, k), k, 0, n)</c> is
20+
/// <c>n 2^(n - 1)</c>), Vandermonde's convolution (Prob 8.9.16,
21+
/// <c>sum(binomial(a, i) binomial(b, k - i), i, 0, k)</c> is <c>binomial(a + b, k)</c>) and
22+
/// its square case (Prob 8.9.34, <c>sum(binomial(n, k)^2, k, 0, n)</c> is
23+
/// <c>binomial(2n, n)</c>), the summation identity (Thm 8.4.6,
24+
/// <c>sum(binomial(i, k), i, 0, n)</c> is <c>binomial(n + 1, k + 1)</c>), and the sums over
25+
/// the even or the odd indices (Ex 8.3.11, each <c>2^(n - 1)</c> for <c>n &gt;= 1</c>).
26+
/// </summary>
27+
/// <remarks>
28+
/// <para>
29+
/// <b>A polynomial beside the coefficient</b> is read in the falling factorial basis,
30+
/// <c>k^m = sum_j S(m, j) k (k - 1) ... (k - j + 1)</c> with the Stirling numbers of the second
31+
/// kind, because <c>k (k - 1) ... (k - j + 1) binomial(n, k)</c> is
32+
/// <c>n (n - 1) ... (n - j + 1) binomial(n - j, k - j)</c> -- the chairperson identity
33+
/// applied <c>j</c> times -- so <c>sum_k k^(j falling) binomial(n, k) x^k y^(n - k)</c> is
34+
/// <c>n^(j falling) x^j (x + y)^(n - j)</c> by the binomial theorem on what is left. Exact for
35+
/// every whole <c>n &gt;= 0</c> and every <c>x</c>, <c>y</c>; the empty range below zero is
36+
/// the piecewise <see cref="PolynomialSummation"/> attaches.
37+
/// </para>
38+
/// <para>
39+
/// <b>Vandermonde</b> holds as an identity in <c>a</c> and <c>b</c> for whole <c>a, b &gt;= 0</c>
40+
/// and a whole <c>k >= 0</c>, the terms outside <c>0 &lt;= i &lt;= min(a, k)</c> being zero, so the
41+
/// range may be written to <c>k</c>, to <c>a</c>, or to <c>b</c> where the second coefficient
42+
/// is <c>binomial(b, k - i)</c>. The square is the case <c>a = b = k = n</c> with
43+
/// <c>binomial(n, k) = binomial(n, n - k)</c>. The summation identity is Pascal's rule
44+
/// telescoped. Part of <a href="https://github.com/asc-community/AngouriMath/issues/1409">#1409</a>.
45+
/// </para>
46+
/// </remarks>
47+
internal static class BinomialIdentities
48+
{
49+
internal static Entity? ClosedForm(Entity expression, Entity var, Entity from, Entity to)
50+
{
51+
if (var is not Variable index)
52+
return null;
53+
if (from.Evaled is not Integer { IsZero: true })
54+
return null;
55+
if (to.ContainsNode(index) || !PolynomialSummation.IsWholeOrSymbolic(to))
56+
return null;
57+
var upper = to.InnerSimplified;
58+
var summand = expression.InnerSimplified;
59+
60+
return PolynomialBesideTheCoefficient(summand, index, upper)
61+
?? Vandermonde(summand, index, upper)
62+
?? SummationIdentity(summand, index, upper)
63+
?? OverTheEvenOrTheOddIndices(expression, index, to);
64+
}
65+
66+
/// <summary>
67+
/// <c>p(k) binomial(n, k) x^k y^(n - k)</c> summed to <c>n</c>, for a polynomial <c>p</c> of
68+
/// degree at least one; the degree-zero case is <see cref="BinomialSum"/>'s.
69+
/// </summary>
70+
private static Entity? PolynomialBesideTheCoefficient(Entity summand, Variable index, Entity upper)
71+
{
72+
Entity? polynomial = null;
73+
var sawCoefficient = false;
74+
// ...or the coefficient as its three factorials, n!/(k! (n - k)!), the way
75+
// BinomialSum reads it.
76+
var sawTop = false;
77+
var sawBottomK = false;
78+
var sawBottomRest = false;
79+
Entity? powerOfK = null;
80+
Entity? powerOfRest = null;
81+
Entity constant = Integer.One;
82+
foreach (var factor in Mulf.LinearChildren(summand))
83+
{
84+
if (!factor.ContainsNode(index))
85+
{
86+
if (!sawTop && factor is Factorialf(var topFactorial) && topFactorial == upper)
87+
sawTop = true;
88+
else
89+
constant *= factor;
90+
}
91+
else if (factor is Binomialf(var top, var bottom) && top == upper && bottom == index && !sawCoefficient)
92+
sawCoefficient = true;
93+
else if (factor is Powf(Factorialf(var argument), var power) && power == Integer.MinusOne && argument == index && !sawBottomK)
94+
sawBottomK = true;
95+
else if (factor is Powf(Factorialf(var argument2), var power2) && power2 == Integer.MinusOne
96+
&& argument2 is Minusf(var upperLeft, var indexRight) && upperLeft == upper && indexRight == index && !sawBottomRest)
97+
sawBottomRest = true;
98+
else if (factor is Powf(var b, var e) && !b.ContainsNode(index) && e == index && powerOfK is null)
99+
powerOfK = b;
100+
else if (factor is Powf(var b2, var e2) && !b2.ContainsNode(index) && e2 is Minusf(var left, var right) && left == upper && right == index && powerOfRest is null)
101+
powerOfRest = b2;
102+
else
103+
polynomial = polynomial is null ? factor : polynomial * factor;
104+
}
105+
// The top factorial is a constant the identity does not need, and a concrete one
106+
// has folded into a number before the factors are read: 130! is not a Factorialf
107+
// by then, so the two below on their own are the coefficient over n!.
108+
var overTheTopFactorial = false;
109+
if (sawBottomK && sawBottomRest)
110+
(sawCoefficient, overTheTopFactorial) = (true, !sawTop);
111+
else if (sawTop || sawBottomK || sawBottomRest)
112+
return null;
113+
if (!sawCoefficient || polynomial is null)
114+
return null;
115+
if (!TreeAnalyzer.TryGetPolynomial(polynomial, index, out var monomials) || monomials.Count == 0
116+
|| monomials.Keys.Any(power => power.Sign < 0 || !power.CanFitInInt32()))
117+
return null;
118+
var degree = monomials.Keys.Max()!.ToInt32Checked();
119+
if (degree < 1 || degree > 12)
120+
return null;
121+
122+
// p in the falling factorial basis: the coefficient of k^(j falling) is
123+
// sum_m p_m S(m, j).
124+
var stirling = StirlingSecondKind(degree);
125+
var x = powerOfK ?? Integer.One;
126+
var y = powerOfRest ?? Integer.One;
127+
var xPlusY = (x + y).InnerSimplified;
128+
Entity closed = Integer.Zero;
129+
for (var j = 0; j <= degree; j++)
130+
{
131+
Entity coefficient = Integer.Zero;
132+
for (var m = j; m <= degree; m++)
133+
if (monomials.TryGetValue(EInteger.FromInt32(m), out var pm))
134+
coefficient += Integer.Create(stirling[m][j]) * pm;
135+
coefficient = coefficient.InnerSimplified;
136+
if (coefficient == Integer.Zero)
137+
continue;
138+
// n^(j falling) x^j (x + y)^(n - j)
139+
Entity falling = Integer.One;
140+
for (var i = 0; i < j; i++)
141+
falling *= i == 0 ? upper : upper - Integer.Create(i);
142+
Entity term = coefficient * falling;
143+
if (j > 0 && x != Integer.One)
144+
term *= MathS.Pow(x, j);
145+
term *= MathS.Pow(xPlusY, j == 0 ? upper : (upper - Integer.Create(j)).InnerSimplified);
146+
closed += term;
147+
}
148+
if (overTheTopFactorial)
149+
closed /= MathS.Factorial(upper);
150+
return Ranged((constant * closed).InnerSimplified, upper);
151+
}
152+
153+
/// <summary>
154+
/// <c>binomial(a, i) binomial(b, c - i)</c>, or the square <c>binomial(n, i)^2</c>, summed
155+
/// from zero to <c>c</c>, <c>a</c> or <c>b</c>.
156+
/// </summary>
157+
private static Entity? Vandermonde(Entity summand, Variable index, Entity upper)
158+
{
159+
Entity? a = null;
160+
Entity? b = null;
161+
Entity? c = null;
162+
Entity constant = Integer.One;
163+
foreach (var factor in Mulf.LinearChildren(summand))
164+
{
165+
if (!factor.ContainsNode(index))
166+
constant *= factor;
167+
else if (factor is Powf(Binomialf(var top, var bottom), Integer two) && two == Integer.Create(2) && bottom == index && !top.ContainsNode(index) && a is null && b is null)
168+
(a, b, c) = (top, top, top);
169+
else if (factor is Binomialf(var top2, var bottom2) && !top2.ContainsNode(index) && bottom2 == index && a is null)
170+
a = top2;
171+
else if (factor is Binomialf(var top3, var bottom3) && !top3.ContainsNode(index) && bottom3 is Minusf(var left, var right) && right == index && !left.ContainsNode(index) && b is null)
172+
(b, c) = (top3, left);
173+
else
174+
return null;
175+
}
176+
if (a is null || b is null || c is null)
177+
return null;
178+
if (upper != c && upper != a && upper != b)
179+
return null;
180+
return Ranged((constant * MathS.Binomial((a == b ? Integer.Create(2) * a : a + b).InnerSimplified, c)).InnerSimplified, upper);
181+
}
182+
183+
/// <summary><c>binomial(i, k)</c> summed over <c>i</c> from zero to <c>n</c>: <c>binomial(n + 1, k + 1)</c>.</summary>
184+
private static Entity? SummationIdentity(Entity summand, Variable index, Entity upper)
185+
{
186+
Entity constant = Integer.One;
187+
Entity? k = null;
188+
foreach (var factor in Mulf.LinearChildren(summand))
189+
{
190+
if (!factor.ContainsNode(index))
191+
constant *= factor;
192+
else if (factor is Binomialf(var top, var bottom) && top == index && !bottom.ContainsNode(index) && k is null)
193+
k = bottom;
194+
else
195+
return null;
196+
}
197+
if (k is null)
198+
return null;
199+
return Ranged((constant * MathS.Binomial((upper + Integer.One).InnerSimplified, (k + Integer.One).InnerSimplified)).InnerSimplified, upper);
200+
}
201+
202+
/// <summary>
203+
/// <c>binomial(n, 2 l)</c> summed to <c>floor(n/2)</c>, and <c>binomial(n, 2 l + 1)</c> to
204+
/// <c>floor((n - 1)/2)</c>: each is <c>2^(n - 1)</c> for <c>n &gt;= 1</c>, the two halves of
205+
/// <c>2^n</c> being equal since their difference is <c>(1 - 1)^n</c>; at <c>n = 0</c> the
206+
/// even sum is <c>1</c> and the odd one is empty.
207+
/// </summary>
208+
private static Entity? OverTheEvenOrTheOddIndices(Entity expression, Variable index, Entity to)
209+
{
210+
if (expression is not Binomialf(var n, var bottom) || n.ContainsNode(index))
211+
return null;
212+
bool? odd = bottom switch
213+
{
214+
Mulf(Integer two, var l) when two == Integer.Create(2) && l == index => false,
215+
Mulf(var l, Integer two) when two == Integer.Create(2) && l == index => false,
216+
Sumf(Mulf(Integer two, var l), Integer one) when two == Integer.Create(2) && l == index && one == Integer.One => true,
217+
Sumf(Mulf(var l, Integer two), Integer one) when two == Integer.Create(2) && l == index && one == Integer.One => true,
218+
_ => null,
219+
};
220+
if (odd is null)
221+
return null;
222+
Entity expectedTop = odd.Value ? MathS.Floor((n - Integer.One) / Integer.Create(2)) : MathS.Floor(n / Integer.Create(2));
223+
if (to.InnerSimplified != expectedTop.InnerSimplified && to != expectedTop)
224+
return null;
225+
var half = MathS.Pow(Integer.Create(2), (n - Integer.One).InnerSimplified);
226+
if (n.Evaled is Integer whole)
227+
return whole.EInteger.Sign > 0 ? half.InnerSimplified : (odd.Value ? Integer.Zero : Integer.One);
228+
return MathS.Piecewise(new[]
229+
{
230+
new Providedf(half, new GreaterOrEqualf(n, Integer.One)),
231+
new Providedf(odd.Value ? Integer.Zero : Integer.One, new Equalsf(n, Integer.Zero)),
232+
new Providedf(Integer.Zero, Entity.Boolean.True),
233+
}).InnerSimplified;
234+
}
235+
236+
/// <summary>The closed form over a range that is empty below <c>upper = 0</c>.</summary>
237+
private static Entity Ranged(Entity closed, Entity upper)
238+
=> upper.Evaled is Integer
239+
? closed
240+
: MathS.Piecewise(new[]
241+
{
242+
new Providedf(closed, new GreaterOrEqualf(upper, Integer.Zero)),
243+
new Providedf(Integer.Zero, Entity.Boolean.True),
244+
}).InnerSimplified;
245+
246+
/// <summary>The Stirling numbers of the second kind <c>S(m, j)</c> for <c>m, j</c> up to <paramref name="degree"/>.</summary>
247+
private static EInteger[][] StirlingSecondKind(int degree)
248+
{
249+
var s = new EInteger[degree + 1][];
250+
for (var m = 0; m <= degree; m++)
251+
{
252+
s[m] = new EInteger[degree + 1];
253+
for (var j = 0; j <= degree; j++)
254+
s[m][j] = EInteger.Zero;
255+
}
256+
s[0][0] = EInteger.One;
257+
for (var m = 1; m <= degree; m++)
258+
for (var j = 1; j <= m; j++)
259+
s[m][j] = EInteger.FromInt32(j).Multiply(s[m - 1][j]).Add(s[m - 1][j - 1]);
260+
return s;
261+
}
262+
}
263+
}

‎Sources/AngouriMath/Functions/Evaluation/Evaluation.Continuous/Evaluation.Continuous.Calculus.Classes.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ protected override Entity InnerSimplify(bool isExact) =>
238238
?? Functions.PolynomialSummation.ClosedForm(Expression, Var, From, To)
239239
?? Functions.ExponentialSeries.ClosedForm(Expression, Var, From, To)
240240
?? Functions.BinomialSum.ClosedForm(Expression, Var, From, To)
241+
?? Functions.BinomialIdentities.ClosedForm(Expression, Var, From, To)
241242
?? Functions.GeometricSeries.ClosedForm(Expression, Var, From, To)
242243
?? Functions.PolynomialGeometricSeries.ClosedForm(Expression, Var, From, To)
243244
// Last: a series that converges is summed above, and only what none of them

0 commit comments

Comments
 (0)