|
| 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 >= 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 >= 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 >= 0</c> |
| 40 | + /// and a whole <c>k >= 0</c>, the terms outside <c>0 <= i <= 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 >= 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 | +} |
0 commit comments