|
| 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 | + /// A summation whose summand is a polynomial in the index times a power with the index in |
| 18 | + /// the exponent: <c>sum(k 2^k, k, 1, n)</c> is <c>(n - 1) 2^(n + 1) + 2</c>, and |
| 19 | + /// <c>sum((-1)^(k - 1) k^2, k, 1, n)</c> is <c>(-1)^(n - 1) n (n + 1)/2</c>, which is |
| 20 | + /// §5.3.4 Try 6 of Sullivan and Mackey's <i>An Introduction to Proofs</i>. |
| 21 | + /// </summary> |
| 22 | + /// <remarks> |
| 23 | + /// <para> |
| 24 | + /// The summand is read as <c>p(k) C b^(m k + s)</c>, which is <c>C b^s p(k) r^k</c> with the |
| 25 | + /// ratio <c>r = b^m</c>, the way <see cref="GeometricSeries"/> reads its summand, with a |
| 26 | + /// polynomial <c>p</c> of degree at least one beside the power. For a ratio other than 1 |
| 27 | + /// there is a polynomial <c>q</c> of the same degree with <c>q(k + 1) r - q(k) = p(k)</c>, |
| 28 | + /// so that <c>q(k) r^k</c> is a discrete antiderivative of the summand, and the sum from |
| 29 | + /// <c>a</c> to <c>b</c> is <c>q(b + 1) r^(b + 1) - q(a) r^a</c>. The coefficients of |
| 30 | + /// <c>q</c> are found highest first: the coefficient of <c>k^j</c> in <c>q(k + 1) r - q(k)</c> |
| 31 | + /// is <c>(r - 1) q_j + r sum_{i > j} binomial(i, j) q_i</c>, one division by <c>r - 1</c> |
| 32 | + /// each, which is the whole of the linear algebra -- Gosper's algorithm specialised to a |
| 33 | + /// summand whose ratio of consecutive terms is a rational function with a constant |
| 34 | + /// denominator. |
| 35 | + /// </para> |
| 36 | + /// <para> |
| 37 | + /// At <c>r = 1</c> the summand is the polynomial, which <see cref="PolynomialSummation"/> |
| 38 | + /// answers; a symbolic ratio carries <c>provided not r = 1</c>, since the formula divides |
| 39 | + /// by <c>r - 1</c>. To <c>+oo</c> the series converges exactly when <c>|r| < 1</c>, to |
| 40 | + /// <c>-q(a) r^a</c>, since <c>q(k) r^k</c> then goes to zero. Part of |
| 41 | + /// <a href="https://github.com/asc-community/AngouriMath/issues/1409">#1409</a>. |
| 42 | + /// </para> |
| 43 | + /// </remarks> |
| 44 | + internal static class PolynomialGeometricSeries |
| 45 | + { |
| 46 | + /// <summary> |
| 47 | + /// <c>sum(expression, index, from, to)</c> in closed form, or <see langword="null"/> |
| 48 | + /// where the summand is not a polynomial in the index times a power with a whole |
| 49 | + /// multiple of the index in its exponent, or a bound is a number that is neither whole |
| 50 | + /// nor <c>+oo</c>. |
| 51 | + /// </summary> |
| 52 | + internal static Entity? ClosedForm(Entity expression, Entity var, Entity from, Entity to) |
| 53 | + { |
| 54 | + if (var is not Variable index) |
| 55 | + return null; |
| 56 | + if (!PolynomialSummation.IsWholeOrSymbolic(from)) |
| 57 | + return null; |
| 58 | + var toInfinity = to.Evaled is Real { IsFinite: false, IsNaN: false, IsNegative: false }; |
| 59 | + if (!toInfinity && !PolynomialSummation.IsWholeOrSymbolic(to)) |
| 60 | + return null; |
| 61 | + |
| 62 | + // `k/2^k` as `k 2^(-k)`: the power below the bar is the power with its exponent |
| 63 | + // negated, and the factors are then read as they are read above it. |
| 64 | + var summand = expression.InnerSimplified; |
| 65 | + if (summand is Divf(var over, Powf(var underBase, var underExponent)) && !underBase.ContainsNode(index) && underExponent.ContainsNode(index)) |
| 66 | + summand = over * MathS.Pow(underBase, (-underExponent).InnerSimplified); |
| 67 | + Entity? @base = null; |
| 68 | + Entity? exponent = null; |
| 69 | + Entity constant = Integer.One; |
| 70 | + Entity? polynomial = null; |
| 71 | + foreach (var factor in Mulf.LinearChildren(summand)) |
| 72 | + { |
| 73 | + if (!factor.ContainsNode(index)) |
| 74 | + constant *= factor; |
| 75 | + else if (factor is Powf(var b, var e) && @base is null && !b.ContainsNode(index) && e.ContainsNode(index)) |
| 76 | + { |
| 77 | + @base = b; |
| 78 | + exponent = e; |
| 79 | + } |
| 80 | + else |
| 81 | + polynomial = polynomial is null ? factor : polynomial * factor; |
| 82 | + } |
| 83 | + if (@base is null || polynomial is null || !GeometricSeries.TryReadLinear(exponent!, index, out var slope, out var offset)) |
| 84 | + return null; |
| 85 | + if (!TreeAnalyzer.TryGetPolynomial(polynomial, index, out var monomials) || monomials.Count == 0 |
| 86 | + || monomials.Keys.Any(power => power.Sign < 0 || !power.CanFitInInt32())) |
| 87 | + return null; |
| 88 | + var degree = monomials.Keys.Max()!.ToInt32Checked(); |
| 89 | + if (degree < 1 || degree > 12) |
| 90 | + return null; |
| 91 | + if (@base.Evaled is Complex value && IsZero(value)) |
| 92 | + return null; |
| 93 | + |
| 94 | + if (offset != Integer.Zero) |
| 95 | + constant *= MathS.Pow(@base, offset); |
| 96 | + var ratio = slope == 1 ? @base : MathS.Pow(@base, Integer.Create(slope)); |
| 97 | + var ratioIsANumber = ratio.Evaled is Complex; |
| 98 | + if (ratioIsANumber && ratio.Evaled == Integer.One) |
| 99 | + return null; |
| 100 | + |
| 101 | + // The coefficients of q, highest first: (r - 1) q_j + r sum_{i > j} C(i, j) q_i = p_j. |
| 102 | + var p = new Entity[degree + 1]; |
| 103 | + for (var j = 0; j <= degree; j++) |
| 104 | + p[j] = monomials.TryGetValue(EInteger.FromInt32(j), out var coefficient) ? coefficient : Integer.Zero; |
| 105 | + var q = new Entity[degree + 1]; |
| 106 | + var rMinusOne = ratio - Integer.One; |
| 107 | + for (var j = degree; j >= 0; j--) |
| 108 | + { |
| 109 | + Entity above = Integer.Zero; |
| 110 | + for (var i = j + 1; i <= degree; i++) |
| 111 | + above += Integer.Create(Binomial(i, j)) * q[i]; |
| 112 | + q[j] = ((p[j] - ratio * above) / rMinusOne).InnerSimplified; |
| 113 | + } |
| 114 | + Entity Q(Entity at) |
| 115 | + { |
| 116 | + Entity sum = Integer.Zero; |
| 117 | + for (var j = 0; j <= degree; j++) |
| 118 | + if (q[j] != Integer.Zero) |
| 119 | + sum += j == 0 ? q[j] : q[j] * MathS.Pow(at, j); |
| 120 | + return sum; |
| 121 | + } |
| 122 | + |
| 123 | + if (toInfinity) |
| 124 | + { |
| 125 | + Entity limit = -constant * Q(from) * PowerAt(ratio, from); |
| 126 | + if (ratioIsANumber) |
| 127 | + return ((Complex)ratio.Evaled).Abs() < 1 ? limit.InnerSimplified : null; |
| 128 | + return new Providedf(limit, new Lessf(MathS.Abs(ratio), Integer.One)).InnerSimplified; |
| 129 | + } |
| 130 | + |
| 131 | + var after = (to + Integer.One).InnerSimplified; |
| 132 | + Entity closed = constant * (Q(after) * PowerAt(ratio, after) - Q(from) * PowerAt(ratio, from)); |
| 133 | + var nonEmpty = new GreaterOrEqualf(to, from - Integer.One); |
| 134 | + if (ratioIsANumber) |
| 135 | + return MathS.Piecewise(new[] |
| 136 | + { |
| 137 | + new Providedf(closed, nonEmpty), |
| 138 | + new Providedf(Integer.Zero, Entity.Boolean.True), |
| 139 | + }).InnerSimplified; |
| 140 | + // At r = 1 the summand is the polynomial, and that sum is the polynomial reader's: |
| 141 | + // its closed form is the first case of what it answers, over the same range. |
| 142 | + Entity atOne = new Summationf(constant * polynomial, index, from, to); |
| 143 | + if (PolynomialSummation.ClosedForm(constant * polynomial, index, from, to) is Piecewise piecewise |
| 144 | + && piecewise.Cases.FirstOrDefault() is Providedf(var polynomialSum, _)) |
| 145 | + atOne = polynomialSum; |
| 146 | + return MathS.Piecewise(new[] |
| 147 | + { |
| 148 | + new Providedf(atOne, new Andf(new Equalsf(ratio, Integer.One), nonEmpty)), |
| 149 | + new Providedf(closed, nonEmpty), |
| 150 | + new Providedf(Integer.Zero, Entity.Boolean.True), |
| 151 | + }).InnerSimplified; |
| 152 | + } |
| 153 | + |
| 154 | + /// <summary> |
| 155 | + /// <c>ratio ^ bound</c>, as the number 1 where the bound is 0, for the reason |
| 156 | + /// <see cref="GeometricSeries"/> gives. |
| 157 | + /// </summary> |
| 158 | + private static Entity PowerAt(Entity ratio, Entity bound) |
| 159 | + => bound.Evaled == Integer.Zero ? Integer.One : MathS.Pow(ratio, bound); |
| 160 | + |
| 161 | + private static EInteger Binomial(int n, int k) |
| 162 | + { |
| 163 | + var result = EInteger.One; |
| 164 | + for (var i = 1; i <= k; i++) |
| 165 | + result = result.Multiply(n - k + i).Divide(i); |
| 166 | + return result; |
| 167 | + } |
| 168 | + } |
| 169 | +} |
0 commit comments