|
| 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 AngouriMath; |
| 9 | +using AngouriMath.Extensions; |
| 10 | +using Xunit; |
| 11 | +using static AngouriMath.Entity; |
| 12 | + |
| 13 | +namespace AngouriMath.Tests.Core |
| 14 | +{ |
| 15 | + /// <summary> |
| 16 | + /// The binomial coefficient's identities: Pascal's rule, the chairperson identity and the |
| 17 | + /// symmetry as rewrite rules, and the binomial theorem read backwards for a sum written |
| 18 | + /// with <c>binomial(n, k)</c>. Item 7 of the reference's docket (Sullivan and Mackey, |
| 19 | + /// §8.4: Props 8.4.1–8.4.4, Thm 8.4.8, Ex 8.4.9, §8.4.5 Try 3). |
| 20 | + /// <see href="https://github.com/asc-community/AngouriMath/issues/1409"/> |
| 21 | + /// </summary> |
| 22 | + [Trait("Area", "Core")] |
| 23 | + public sealed class BinomialIdentityTest |
| 24 | + { |
| 25 | + /// <summary>Props 8.4.1–8.4.3 as equalities of symbols, decided by rewriting one side into the other.</summary> |
| 26 | + [Theory] |
| 27 | + [InlineData("binomial(n, k) = binomial(n, n - k)")] |
| 28 | + [InlineData("binomial(n, k) = binomial(n - 1, k) + binomial(n - 1, k - 1)")] |
| 29 | + [InlineData("binomial(n + 1, k + 1) = binomial(n, k + 1) + binomial(n, k)")] |
| 30 | + [InlineData("k * binomial(n, k) = n * binomial(n - 1, k - 1)")] |
| 31 | + public void AnIdentityIsTrue(string identity) |
| 32 | + => Assert.Equal(Boolean.True, identity.ToEntity().Simplify()); |
| 33 | + |
| 34 | + /// <summary>The rewrites themselves, each in the direction that collects.</summary> |
| 35 | + [Theory] |
| 36 | + [InlineData("binomial(n - 1, k) + binomial(n - 1, k - 1)", "binomial(n, k)")] |
| 37 | + [InlineData("binomial(n, n - 2)", "binomial(n, 2)")] |
| 38 | + [InlineData("binomial(n, n - k)", "binomial(n, k)")] |
| 39 | + [InlineData("n * binomial(n - 1, k - 1)", "binomial(n, k) * k")] |
| 40 | + [InlineData("binomial(7, 3) + binomial(7, 2)", "56")] |
| 41 | + public void ARewriteCollects(string input, string expected) |
| 42 | + => Assert.Equal(expected.ToEntity(), input.ToEntity().Simplify()); |
| 43 | + |
| 44 | + /// <summary>The symmetry fires only where the complement is the smaller expression: <c>binomial(n, 2)</c> stays.</summary> |
| 45 | + [Fact] |
| 46 | + public void TheSymmetryPrefersTheSmallerLowerIndex() |
| 47 | + { |
| 48 | + Assert.Equal("binomial(n, 2)".ToEntity(), "binomial(n, 2)".ToEntity().Simplify()); |
| 49 | + Assert.Equal("binomial(n, k)".ToEntity(), "binomial(n, k)".ToEntity().Simplify()); |
| 50 | + } |
| 51 | + |
| 52 | + /// <summary>Thm 8.4.8, Prop 8.4.4, §8.4.5 Try 3 and Ex 8.4.9: the sum written with the node, in closed form.</summary> |
| 53 | + [Theory] |
| 54 | + [InlineData("sum(binomial(n, k), k, 0, n)", "piecewise((2 ^ n) provided (n >= 0), 0 provided True)")] |
| 55 | + [InlineData("sum(binomial(n, k) * 2^(n - k), k, 0, n)", "piecewise((3 ^ n) provided (n >= 0), 0 provided True)")] |
| 56 | + [InlineData("sum(binomial(n, k) * x^k * y^(n - k), k, 0, n)", "piecewise(((x + y) ^ n) provided (n >= 0), 0 provided True)")] |
| 57 | + [InlineData("sum(binomial(n, n - k) * x^k, k, 0, n)", "piecewise(((x + 1) ^ n) provided (n >= 0), 0 provided True)")] |
| 58 | + [InlineData("sum(binomial(4, k) * 2^(4 - k), k, 0, 4)", "81")] |
| 59 | + [InlineData("sum(binomial(10, k), k, 0, 10)", "1024")] |
| 60 | + public void ABinomialSumIsTheTheoremReadBackwards(string sum, string expected) |
| 61 | + => Assert.Equal(expected.ToEntity().Evaled, sum.ToEntity().Evaled); |
| 62 | + |
| 63 | + /// <summary> |
| 64 | + /// Ex 8.4.9: the alternating sum is <c>0^n</c>, which is <c>1</c> at <c>n = 0</c> and |
| 65 | + /// <c>0</c> above it. Written as the power it carried a condition the piecewise read as |
| 66 | + /// no value, and the sum was <c>0</c> at <c>n = 0</c> where it is <c>1</c> -- in the |
| 67 | + /// factorial spelling too, which is fixed by the same arm. |
| 68 | + /// </summary> |
| 69 | + [Theory] |
| 70 | + [InlineData("sum(binomial(n, k) * (-1)^k, k, 0, n)", "piecewise(1 provided (n = 0), 0 provided True)")] |
| 71 | + [InlineData("sum(n! / (k! * (n - k)!) * (-1)^k, k, 0, n)", "piecewise(1 provided (n = 0), 0 provided True)")] |
| 72 | + [InlineData("sum(binomial(0, k) * (-1)^k, k, 0, 0)", "1")] |
| 73 | + [InlineData("sum(binomial(4, k) * (-1)^k, k, 0, 4)", "0")] |
| 74 | + public void TheAlternatingSumIsOneAtZero(string sum, string expected) |
| 75 | + => Assert.Equal(expected.ToEntity().Evaled, sum.ToEntity().Evaled); |
| 76 | + } |
| 77 | +} |
0 commit comments