From 85c877923c391ee636a03af45877d7c05143463f Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Tue, 22 Sep 2026 03:29:09 +0000 Subject: [PATCH] The binomial sums of chapter 8 are answered in closed form 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. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura --- BREAKING-CHANGES.md | 17 ++ Sources/.editorconfig | 6 + Sources/AngouriMath/Docs/Usage/Syntax.md | 11 +- .../Algebra/Polynomials/BinomialIdentities.cs | 263 ++++++++++++++++++ .../Evaluation.Continuous.Calculus.Classes.cs | 1 + .../Calculus/BinomialIdentitiesTest.cs | 78 ++++++ .../UnitTests/Calculus/BinomialSumTest.cs | 6 +- 7 files changed, 378 insertions(+), 4 deletions(-) create mode 100644 Sources/AngouriMath/Functions/Algebra/Polynomials/BinomialIdentities.cs create mode 100644 Sources/Tests/UnitTests/Calculus/BinomialIdentitiesTest.cs diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index 6a8a13237..3b69672df 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -976,6 +976,23 @@ the remainder of `acsch(c x)^2/x^4` wrongly along the way | `"(a+b*acsch(c*x))^2/x^4".Integrate("x")` | left unevaluated | the antiderivative | | `"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 | +### The binomial sums of chapter 8 are answered in closed form + +`sum(k binomial(n, k), k, 0, n)` was left as written; it is `n 2^(n - 1)`, and with it a +polynomial of any degree beside the coefficient (in the falling factorial basis, by the +chairperson identity), Vandermonde's convolution, `sum(binomial(n, k)^2, k, 0, n) = binomial(2n, n)`, +the summation identity `sum(binomial(i, k), i, 0, n) = binomial(n + 1, k + 1)`, and the sums over +the even or the odd indices, each `2^(n - 1)` — Prop 8.4.4, Thm 8.4.6, Ex 8.3.11 and Probs +8.9.16 and 8.9.34 of Sullivan and Mackey's *An Introduction to Proofs* +([#1409](https://github.com/asc-community/AngouriMath/issues/1409)). + +| Input | Was (2.5.0) | Now | +|---|---|---| +| `"sum(k*binomial(n,k), k, 0, n)".ToEntity().Evaled` | `UnhandledParseException` — `binomial` is new since | `n 2^(n - 1) provided n >= 0`, `0` otherwise | +| `"sum(n!/(k!*(n-k)!)*k, k, 0, n)".ToEntity().Evaled` | as written | the same | +| `"sum(binomial(n,k)^2, k, 0, n)".ToEntity().Evaled` | parse error | `binomial(2 n, n)` | +| `"sum(binomial(i,k), i, 0, n)".ToEntity().Evaled` | parse error | `binomial(n + 1, k + 1)` | + ### `binomial(n, k)` is a function **Addition, not silent.** The binomial coefficient is a node, `Entity.Binomialf`, spelled diff --git a/Sources/.editorconfig b/Sources/.editorconfig index 11556167f..e3ae9673e 100644 --- a/Sources/.editorconfig +++ b/Sources/.editorconfig @@ -291,6 +291,12 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed [AngouriMath/Functions/NumberTheory/ResidueClasses.cs] 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 +[Tests/UnitTests/Calculus/BinomialIdentitiesTest.cs] +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 + +[AngouriMath/Functions/Algebra/Polynomials/BinomialIdentities.cs] +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 + [Tests/UnitTests/Calculus/PolynomialGeometricSeriesTest.cs] 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 diff --git a/Sources/AngouriMath/Docs/Usage/Syntax.md b/Sources/AngouriMath/Docs/Usage/Syntax.md index a0dbca0e3..8188421be 100644 --- a/Sources/AngouriMath/Docs/Usage/Syntax.md +++ b/Sources/AngouriMath/Docs/Usage/Syntax.md @@ -264,7 +264,16 @@ leave the polynomial case alone. factorial `n (n - 1) ... (n - k + 1) / k!` for any `n` — `binomial(5, 2)` is `10`, `binomial(-1, 3)` is `-1`, `binomial(1/2, 2)` is `-1/8` — with `0` below `k = 0` and above a non-negative whole `n`; a symbolic argument keeps the node, which prints as `\binom{n}{k}` in -LaTeX, and a `k` that is not whole evaluates numerically through the gamma function. +LaTeX, and a `k` that is not whole evaluates numerically through the gamma function. A `sum` +over the coefficient is answered in closed form: the binomial theorem +(`sum(binomial(n, k) x^k y^(n - k), k, 0, n)` is `(x + y)^n`), a polynomial in the index beside +it (`sum(k binomial(n, k), k, 0, n)` is `n 2^(n - 1)`, read in the falling factorial basis), +Vandermonde's convolution (`sum(binomial(a, i) binomial(b, k - i), i, 0, k)` is +`binomial(a + b, k)`, and `sum(binomial(n, k)^2, k, 0, n)` is `binomial(2 n, n)`), the summation +identity (`sum(binomial(i, k), i, 0, n)` is `binomial(n + 1, k + 1)`), and the sums over the even +or the odd indices (`sum(binomial(n, 2 l), l, 0, floor(n/2))` is `2^(n - 1)` for `n >= 1`) — the +identities of chapter 8 of Sullivan and Mackey's *An Introduction to Proofs*, so that +`forall n in ZZ+ : sum(k binomial(n, k), k, 0, n) = n 2^(n - 1)` is `True`. **Calculus** — `derivative(expr, var, order)`, `integral(expr, var)`, `integral(expr, var, from, to)`, `limit(expr, var, dest)`, `limitleft(...)`, `limitright(...)`; diff --git a/Sources/AngouriMath/Functions/Algebra/Polynomials/BinomialIdentities.cs b/Sources/AngouriMath/Functions/Algebra/Polynomials/BinomialIdentities.cs new file mode 100644 index 000000000..f40a7ce1f --- /dev/null +++ b/Sources/AngouriMath/Functions/Algebra/Polynomials/BinomialIdentities.cs @@ -0,0 +1,263 @@ +// +// Copyright (c) 2019-2026 Angouri. +// AngouriMath is licensed under MIT. +// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md. +// Website: https://am.angouri.org. +// + +using System.Collections.Generic; +using System.Linq; +using PeterO.Numbers; +using static AngouriMath.Entity; +using static AngouriMath.Entity.Number; + +namespace AngouriMath.Functions +{ + /// + /// The binomial sums of chapter 8 of Sullivan and Mackey's An Introduction to Proofs + /// that are not the binomial theorem itself, in closed form: a polynomial in the index + /// beside the coefficient (Prop 8.4.4, sum(k binomial(n, k), k, 0, n) is + /// n 2^(n - 1)), Vandermonde's convolution (Prob 8.9.16, + /// sum(binomial(a, i) binomial(b, k - i), i, 0, k) is binomial(a + b, k)) and + /// its square case (Prob 8.9.34, sum(binomial(n, k)^2, k, 0, n) is + /// binomial(2n, n)), the summation identity (Thm 8.4.6, + /// sum(binomial(i, k), i, 0, n) is binomial(n + 1, k + 1)), and the sums over + /// the even or the odd indices (Ex 8.3.11, each 2^(n - 1) for n >= 1). + /// + /// + /// + /// A polynomial beside the coefficient is read in the falling factorial basis, + /// k^m = sum_j S(m, j) k (k - 1) ... (k - j + 1) with the Stirling numbers of the second + /// kind, because k (k - 1) ... (k - j + 1) binomial(n, k) is + /// n (n - 1) ... (n - j + 1) binomial(n - j, k - j) -- the chairperson identity + /// applied j times -- so sum_k k^(j falling) binomial(n, k) x^k y^(n - k) is + /// n^(j falling) x^j (x + y)^(n - j) by the binomial theorem on what is left. Exact for + /// every whole n >= 0 and every x, y; the empty range below zero is + /// the piecewise attaches. + /// + /// + /// Vandermonde holds as an identity in a and b for whole a, b >= 0 + /// and a whole k >= 0, the terms outside 0 <= i <= min(a, k) being zero, so the + /// range may be written to k, to a, or to b where the second coefficient + /// is binomial(b, k - i). The square is the case a = b = k = n with + /// binomial(n, k) = binomial(n, n - k). The summation identity is Pascal's rule + /// telescoped. Part of #1409. + /// + /// + internal static class BinomialIdentities + { + internal static Entity? ClosedForm(Entity expression, Entity var, Entity from, Entity to) + { + if (var is not Variable index) + return null; + if (from.Evaled is not Integer { IsZero: true }) + return null; + if (to.ContainsNode(index) || !PolynomialSummation.IsWholeOrSymbolic(to)) + return null; + var upper = to.InnerSimplified; + var summand = expression.InnerSimplified; + + return PolynomialBesideTheCoefficient(summand, index, upper) + ?? Vandermonde(summand, index, upper) + ?? SummationIdentity(summand, index, upper) + ?? OverTheEvenOrTheOddIndices(expression, index, to); + } + + /// + /// p(k) binomial(n, k) x^k y^(n - k) summed to n, for a polynomial p of + /// degree at least one; the degree-zero case is 's. + /// + private static Entity? PolynomialBesideTheCoefficient(Entity summand, Variable index, Entity upper) + { + Entity? polynomial = null; + var sawCoefficient = false; + // ...or the coefficient as its three factorials, n!/(k! (n - k)!), the way + // BinomialSum reads it. + var sawTop = false; + var sawBottomK = false; + var sawBottomRest = false; + Entity? powerOfK = null; + Entity? powerOfRest = null; + Entity constant = Integer.One; + foreach (var factor in Mulf.LinearChildren(summand)) + { + if (!factor.ContainsNode(index)) + { + if (!sawTop && factor is Factorialf(var topFactorial) && topFactorial == upper) + sawTop = true; + else + constant *= factor; + } + else if (factor is Binomialf(var top, var bottom) && top == upper && bottom == index && !sawCoefficient) + sawCoefficient = true; + else if (factor is Powf(Factorialf(var argument), var power) && power == Integer.MinusOne && argument == index && !sawBottomK) + sawBottomK = true; + else if (factor is Powf(Factorialf(var argument2), var power2) && power2 == Integer.MinusOne + && argument2 is Minusf(var upperLeft, var indexRight) && upperLeft == upper && indexRight == index && !sawBottomRest) + sawBottomRest = true; + else if (factor is Powf(var b, var e) && !b.ContainsNode(index) && e == index && powerOfK is null) + powerOfK = b; + 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) + powerOfRest = b2; + else + polynomial = polynomial is null ? factor : polynomial * factor; + } + // The top factorial is a constant the identity does not need, and a concrete one + // has folded into a number before the factors are read: 130! is not a Factorialf + // by then, so the two below on their own are the coefficient over n!. + var overTheTopFactorial = false; + if (sawBottomK && sawBottomRest) + (sawCoefficient, overTheTopFactorial) = (true, !sawTop); + else if (sawTop || sawBottomK || sawBottomRest) + return null; + if (!sawCoefficient || polynomial is null) + return null; + if (!TreeAnalyzer.TryGetPolynomial(polynomial, index, out var monomials) || monomials.Count == 0 + || monomials.Keys.Any(power => power.Sign < 0 || !power.CanFitInInt32())) + return null; + var degree = monomials.Keys.Max()!.ToInt32Checked(); + if (degree < 1 || degree > 12) + return null; + + // p in the falling factorial basis: the coefficient of k^(j falling) is + // sum_m p_m S(m, j). + var stirling = StirlingSecondKind(degree); + var x = powerOfK ?? Integer.One; + var y = powerOfRest ?? Integer.One; + var xPlusY = (x + y).InnerSimplified; + Entity closed = Integer.Zero; + for (var j = 0; j <= degree; j++) + { + Entity coefficient = Integer.Zero; + for (var m = j; m <= degree; m++) + if (monomials.TryGetValue(EInteger.FromInt32(m), out var pm)) + coefficient += Integer.Create(stirling[m][j]) * pm; + coefficient = coefficient.InnerSimplified; + if (coefficient == Integer.Zero) + continue; + // n^(j falling) x^j (x + y)^(n - j) + Entity falling = Integer.One; + for (var i = 0; i < j; i++) + falling *= i == 0 ? upper : upper - Integer.Create(i); + Entity term = coefficient * falling; + if (j > 0 && x != Integer.One) + term *= MathS.Pow(x, j); + term *= MathS.Pow(xPlusY, j == 0 ? upper : (upper - Integer.Create(j)).InnerSimplified); + closed += term; + } + if (overTheTopFactorial) + closed /= MathS.Factorial(upper); + return Ranged((constant * closed).InnerSimplified, upper); + } + + /// + /// binomial(a, i) binomial(b, c - i), or the square binomial(n, i)^2, summed + /// from zero to c, a or b. + /// + private static Entity? Vandermonde(Entity summand, Variable index, Entity upper) + { + Entity? a = null; + Entity? b = null; + Entity? c = null; + Entity constant = Integer.One; + foreach (var factor in Mulf.LinearChildren(summand)) + { + if (!factor.ContainsNode(index)) + constant *= factor; + 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) + (a, b, c) = (top, top, top); + else if (factor is Binomialf(var top2, var bottom2) && !top2.ContainsNode(index) && bottom2 == index && a is null) + a = top2; + 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) + (b, c) = (top3, left); + else + return null; + } + if (a is null || b is null || c is null) + return null; + if (upper != c && upper != a && upper != b) + return null; + return Ranged((constant * MathS.Binomial((a == b ? Integer.Create(2) * a : a + b).InnerSimplified, c)).InnerSimplified, upper); + } + + /// binomial(i, k) summed over i from zero to n: binomial(n + 1, k + 1). + private static Entity? SummationIdentity(Entity summand, Variable index, Entity upper) + { + Entity constant = Integer.One; + Entity? k = null; + foreach (var factor in Mulf.LinearChildren(summand)) + { + if (!factor.ContainsNode(index)) + constant *= factor; + else if (factor is Binomialf(var top, var bottom) && top == index && !bottom.ContainsNode(index) && k is null) + k = bottom; + else + return null; + } + if (k is null) + return null; + return Ranged((constant * MathS.Binomial((upper + Integer.One).InnerSimplified, (k + Integer.One).InnerSimplified)).InnerSimplified, upper); + } + + /// + /// binomial(n, 2 l) summed to floor(n/2), and binomial(n, 2 l + 1) to + /// floor((n - 1)/2): each is 2^(n - 1) for n >= 1, the two halves of + /// 2^n being equal since their difference is (1 - 1)^n; at n = 0 the + /// even sum is 1 and the odd one is empty. + /// + private static Entity? OverTheEvenOrTheOddIndices(Entity expression, Variable index, Entity to) + { + if (expression is not Binomialf(var n, var bottom) || n.ContainsNode(index)) + return null; + bool? odd = bottom switch + { + Mulf(Integer two, var l) when two == Integer.Create(2) && l == index => false, + Mulf(var l, Integer two) when two == Integer.Create(2) && l == index => false, + Sumf(Mulf(Integer two, var l), Integer one) when two == Integer.Create(2) && l == index && one == Integer.One => true, + Sumf(Mulf(var l, Integer two), Integer one) when two == Integer.Create(2) && l == index && one == Integer.One => true, + _ => null, + }; + if (odd is null) + return null; + Entity expectedTop = odd.Value ? MathS.Floor((n - Integer.One) / Integer.Create(2)) : MathS.Floor(n / Integer.Create(2)); + if (to.InnerSimplified != expectedTop.InnerSimplified && to != expectedTop) + return null; + var half = MathS.Pow(Integer.Create(2), (n - Integer.One).InnerSimplified); + if (n.Evaled is Integer whole) + return whole.EInteger.Sign > 0 ? half.InnerSimplified : (odd.Value ? Integer.Zero : Integer.One); + return MathS.Piecewise(new[] + { + new Providedf(half, new GreaterOrEqualf(n, Integer.One)), + new Providedf(odd.Value ? Integer.Zero : Integer.One, new Equalsf(n, Integer.Zero)), + new Providedf(Integer.Zero, Entity.Boolean.True), + }).InnerSimplified; + } + + /// The closed form over a range that is empty below upper = 0. + private static Entity Ranged(Entity closed, Entity upper) + => upper.Evaled is Integer + ? closed + : MathS.Piecewise(new[] + { + new Providedf(closed, new GreaterOrEqualf(upper, Integer.Zero)), + new Providedf(Integer.Zero, Entity.Boolean.True), + }).InnerSimplified; + + /// The Stirling numbers of the second kind S(m, j) for m, j up to . + private static EInteger[][] StirlingSecondKind(int degree) + { + var s = new EInteger[degree + 1][]; + for (var m = 0; m <= degree; m++) + { + s[m] = new EInteger[degree + 1]; + for (var j = 0; j <= degree; j++) + s[m][j] = EInteger.Zero; + } + s[0][0] = EInteger.One; + for (var m = 1; m <= degree; m++) + for (var j = 1; j <= m; j++) + s[m][j] = EInteger.FromInt32(j).Multiply(s[m - 1][j]).Add(s[m - 1][j - 1]); + return s; + } + } +} diff --git a/Sources/AngouriMath/Functions/Evaluation/Evaluation.Continuous/Evaluation.Continuous.Calculus.Classes.cs b/Sources/AngouriMath/Functions/Evaluation/Evaluation.Continuous/Evaluation.Continuous.Calculus.Classes.cs index 1f72775ac..ab116b047 100644 --- a/Sources/AngouriMath/Functions/Evaluation/Evaluation.Continuous/Evaluation.Continuous.Calculus.Classes.cs +++ b/Sources/AngouriMath/Functions/Evaluation/Evaluation.Continuous/Evaluation.Continuous.Calculus.Classes.cs @@ -238,6 +238,7 @@ protected override Entity InnerSimplify(bool isExact) => ?? Functions.PolynomialSummation.ClosedForm(Expression, Var, From, To) ?? Functions.ExponentialSeries.ClosedForm(Expression, Var, From, To) ?? Functions.BinomialSum.ClosedForm(Expression, Var, From, To) + ?? Functions.BinomialIdentities.ClosedForm(Expression, Var, From, To) ?? Functions.GeometricSeries.ClosedForm(Expression, Var, From, To) ?? Functions.PolynomialGeometricSeries.ClosedForm(Expression, Var, From, To) // Last: a series that converges is summed above, and only what none of them diff --git a/Sources/Tests/UnitTests/Calculus/BinomialIdentitiesTest.cs b/Sources/Tests/UnitTests/Calculus/BinomialIdentitiesTest.cs new file mode 100644 index 000000000..b9b71ec7a --- /dev/null +++ b/Sources/Tests/UnitTests/Calculus/BinomialIdentitiesTest.cs @@ -0,0 +1,78 @@ +// +// Copyright (c) 2019-2026 Angouri. +// AngouriMath is licensed under MIT. +// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md. +// Website: https://am.angouri.org. +// + +using AngouriMath; +using AngouriMath.Extensions; +using Xunit; +using static AngouriMath.Entity; + +namespace AngouriMath.Tests.Calculus +{ + /// + /// The binomial sums of chapter 8 of Sullivan and Mackey's An Introduction to Proofs + /// beyond the binomial theorem: a polynomial beside the coefficient (Prop 8.4.4, §8.4.5 + /// Try 2), Vandermonde (Prob 8.9.16) and sum binomial(n, k)^2 = binomial(2n, n) + /// (Prob 8.9.34), the summation identity (Thm 8.4.6), and the sums over the even or the + /// odd indices (Ex 8.3.11). + /// + public sealed class BinomialIdentitiesTest + { + /// The closed form against the sum written out, at small bounds and one past the expansion's hundred terms. + [Theory] + [InlineData("sum(k * binomial(n, k), k, 0, n)", "n * 2^(n - 1)")] + [InlineData("sum(k^2 * binomial(n, k), k, 0, n)", "n * (n + 1) * 2^(n - 2)")] + [InlineData("sum(k * binomial(n, k) * x^k, k, 0, n)", "n * x * (x + 1)^(n - 1)")] + [InlineData("sum((k^2 - k) * binomial(n, k) * 2^k * 3^(n - k), k, 0, n)", "n * (n - 1) * 4 * 5^(n - 2)")] + [InlineData("sum(n! / (k! * (n - k)!) * k, k, 0, n)", "n * 2^(n - 1)")] + [InlineData("sum(binomial(n, k)^2, k, 0, n)", "binomial(2 n, n)")] + [InlineData("sum(binomial(n, i) * binomial(n + 3, 5 - i), i, 0, 5)", "binomial(2 n + 3, 5)")] + [InlineData("sum(binomial(i, 3), i, 0, n)", "binomial(n + 1, 4)")] + [InlineData("sum(binomial(n, 2 * l), l, 0, floor(n / 2))", "2^(n - 1)")] + [InlineData("sum(binomial(n, 2 * l + 1), l, 0, floor((n - 1) / 2))", "2^(n - 1)")] + public void AnIdentityIsClosed(string sum, string expected) + { + var closed = sum.ToEntity().Simplify(); + Assert.IsNotType(closed); + foreach (var n in new[] { 1, 2, 3, 6, 11, 130 }) + { + var want = expected.ToEntity().Substitute("n", n).Substitute("x", "3/7").Evaled; + Assert.Equal(want, closed.Substitute("n", n).Substitute("x", "3/7").Evaled); + Assert.Equal(want, sum.ToEntity().Substitute("n", n).Substitute("x", "3/7").Evaled); + } + } + + /// Vandermonde with both parameters free, and its range written to either of them. + [Theory] + [InlineData("sum(binomial(a, i) * binomial(b, k - i), i, 0, k)")] + [InlineData("sum(binomial(a, i) * binomial(b, k - i), i, 0, a)")] + public void VandermondeWithBothParametersFree(string sum) + { + var closed = sum.ToEntity().Simplify(); + Assert.IsNotType(closed); + foreach (var (a, b, k) in new[] { (4, 5, 3), (7, 2, 6), (3, 3, 3), (10, 12, 8) }) + { + var want = "binomial(a + b, k)".ToEntity().Substitute("a", a).Substitute("b", b).Substitute("k", k).Evaled; + Assert.Equal(want, closed.Substitute("a", a).Substitute("b", b).Substitute("k", k).Evaled); + } + } + + /// Ex 8.3.11 at n = 0: the even sum is the one empty subset, the odd sum is empty. + [Fact] + public void TheParitySumsAtZero() + { + Assert.Equal(Number.Integer.One, "sum(binomial(n, 2 * l), l, 0, floor(n / 2))".ToEntity().Simplify().Substitute("n", 0).Evaled); + Assert.Equal(Number.Integer.Zero, "sum(binomial(n, 2 * l + 1), l, 0, floor((n - 1) / 2))".ToEntity().Simplify().Substitute("n", 0).Evaled); + } + + /// The identities the book proves, decided from the closed forms. + [Theory] + [InlineData("forall n in ZZ+ : sum(k * binomial(n, k), k, 0, n) = n * 2^(n - 1)")] + [InlineData("forall n in ZZ* : sum(binomial(n, k)^2, k, 0, n) = binomial(2 n, n)")] + [InlineData("forall n in ZZ* : sum(binomial(i, k), i, 0, n) = binomial(n + 1, k + 1)")] + public void TheIdentityIsDecided(string statement) => Assert.Equal(Boolean.True, statement.ToEntity().Evaled); + } +} diff --git a/Sources/Tests/UnitTests/Calculus/BinomialSumTest.cs b/Sources/Tests/UnitTests/Calculus/BinomialSumTest.cs index af378fb76..6234d2945 100644 --- a/Sources/Tests/UnitTests/Calculus/BinomialSumTest.cs +++ b/Sources/Tests/UnitTests/Calculus/BinomialSumTest.cs @@ -74,10 +74,10 @@ public void ASymbolicUpperBoundCarriesTheRange() public void AConcreteLongRangeIsClosed() => Assert.Equal("2^300".ToEntity().Evaled, "sum(300! / (k! * (300 - k)!), k, 0, 300)".ToEntity().Simplify().Evaled); - // Not of the shape: a polynomial weight, a trigonometric weight beside a power, a lower - // bound that is not zero, a coefficient whose N is not the upper bound. + // Not of the shape: a trigonometric weight beside a power, a lower bound that is not + // zero, a coefficient whose N is not the upper bound. A polynomial weight was here, and + // is BinomialIdentities' now, see its test. [Theory] - [InlineData("sum(N! / (k! * (N - k)!) * k, k, 0, N)")] [InlineData("sum(N! / (k! * (N - k)!) * x^k * cos(k * t), k, 0, N)")] [InlineData("sum(N! / (k! * (N - k)!), k, 1, N)")] [InlineData("sum(M! / (k! * (M - k)!), k, 0, N)")]