Skip to content

Commit 134da42

Browse files
A sum of a polynomial times a power of the index is answered in closed form, and a quantified piecewise is decided by cases on a parameter (#1458)
`sum(k 2^k, k, 1, n)` was left as written. PolynomialGeometricSeries reads the summand as p(k) C b^(m k + s), finds the polynomial q of the same degree with q(k + 1) r - q(k) = p(k) by one division by r - 1 per coefficient, and answers q(b + 1) r^(b + 1) - q(a) r^a; a symbolic ratio carries the case r = 1 as the polynomial reader's answer, and to +oo the series converges for |r| < 1. §5.3.4 Try 6 of Sullivan and Mackey's An Introduction to Proofs, sum((-1)^(k - 1) k^2, k, 1, n) = (-1)^(n - 1) n (n + 1)/2, is the row that asked for it. A quantified statement whose body is a piecewise with a case on a parameter beside one that holds at every member -- q = 1 and n >= 0, which is how the sum with a symbolic ratio comes -- is decided under the hypothesis and under its negation, and holds provided the hypothesis where it holds there alone: their Ex 5.2.6, forall n in ZZ+ : sum(q^i, i, 0, n - 1) = (q^n - 1)/(q - 1), is True provided not q = 1, which is the book's own remark on why q = 1 is excluded. The exclusions a condition repeats in several spellings are written once. Suite 12543 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 22ebb2a commit 134da42

10 files changed

Lines changed: 408 additions & 11 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -920,6 +920,25 @@ apart as `sgn(P) P` with `provided P^2 > 0` — only where the root is a factor
920920
| `"acsch(a+b*x)/x^2".Integrate("x")` | left unevaluated | the antiderivative |
921921
| `"1/(x*(a+b*x)^2*sqrt(1/(a+b*x)^2-1))".Integrate("x")` | left unevaluated | the antiderivative |
922922

923+
### A sum of a polynomial times a power of the index is answered in closed form
924+
925+
`sum(k 2^k, k, 1, n)` was left as written; it is `(n - 1) 2^(n + 1) + 2`, by the discrete
926+
antiderivative `q(k) r^k` with `q` a polynomial of the summand's degree, found by one division
927+
by `r - 1` per coefficient. A symbolic ratio carries the case `r = 1` separately, where the
928+
summand is the polynomial, and to `+oo` the series converges for `|r| < 1`. §5.3.4 Try 6 of
929+
Sullivan and Mackey's *An Introduction to Proofs*, `sum((-1)^(k - 1) k^2, k, 1, n)`, is the
930+
row that asked for it. And a quantified statement whose body is a piecewise with a case on a
931+
parameter -- `q = 1 and n >= 0`, which is how the sum comes -- is decided by cases on the
932+
parameter, so their Ex 5.2.6 is answered with the condition the book asks about
933+
([#1409](https://github.com/asc-community/AngouriMath/issues/1409)).
934+
935+
| Input | Was (2.5.0) | Now |
936+
|---|---|---|
937+
| `"sum(k*2^k, k, 1, n)".ToEntity().Evaled` | as written | `((n - 1) 2^(n + 1) + 2) provided n >= 0`, `0` otherwise |
938+
| `"sum((-1)^(k-1)*k^2, k, 1, n)".ToEntity().Evaled` | as written | `(-1)^(n - 1) n (n + 1)/2`, up to the form |
939+
| `"sum(k/2^k, k, 1, +oo)".ToEntity().Evaled` | as written | `2` |
940+
| `"forall n in ZZ+ : sum(q^i, i, 0, n-1) = (q^n - 1)/(q - 1)".ToEntity().Evaled` | `UnhandledParseException` -- quantifiers are new since | `True provided not q = 1` |
941+
923942
### `binomial(n, k)` is a function
924943

925944
**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/PolynomialGeometricSeriesTest.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/PolynomialGeometricSeries.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/InverseHyperbolicOfALinearIntegralTest.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: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,8 +323,12 @@ and, with the sum to `n + 1` unfolded to the sum to `n` plus one term and that s
323323
denominator is not zero: decided over the set for the quantified name (`n + 1` is never `0` on
324324
`ZZ+`), and stated as a condition for a free parameter (`forall n in ZZ+ : sum(1/(a k (k + 1)), k, 1, n) =
325325
n/(a (n + 1))` is `True provided not a = 0`). Only the shape with one sum or product
326-
alone on a side of an equation is read this way; a sum whose closed form has a case on a free
327-
parameter is left as written. **An inequality with an exponential or a factorial in `n`** is
326+
alone on a side of an equation is read this way. A sum whose closed form has a case on a free
327+
parameter — `q = 1` beside `n >= 0`, which is how `sum(q^i, i, 0, n - 1)` comes — is decided
328+
**by cases on the parameter**: the statement under the hypothesis and under its negation, and it
329+
holds provided the hypothesis where it holds there alone, so Sullivan and Mackey's Ex 5.2.6,
330+
`forall n in ZZ+ : sum(q^i, i, 0, n - 1) = (q^n - 1)/(q - 1)`, is `True provided not q = 1` —
331+
which is the book's own remark on why `q = 1` is excluded. **An inequality with an exponential or a factorial in `n`** is
328332
decided by induction too, with the step read off a multiple of the hypothesis: writing it as
329333
`P(n) >= 0`, `P(n + 1) = c P(n) + D` for a `c >= 0` (one, the base of an exponential, `n + 1`
330334
beside a factorial) and a `D` whose sign is read from its shape — a positive base to any power, a
@@ -395,6 +399,15 @@ concrete that condition is decidable and the answer is a number. A bound that is
395399
a whole one still stays as written, since the index runs over the integers and
396400
`sum(k, k, 1, 5/2)` is `1 + 2` rather than the polynomial at `5/2`.
397401

402+
A `sum` whose body is a **polynomial in the index times a power with the index in the exponent**
403+
is answered in closed form too: `sum(k * 2^k, k, 1, n)` is `(n - 1) 2^(n + 1) + 2`, and
404+
`sum((-1)^(k - 1) k^2, k, 1, n)` is `(-1)^(n - 1) n (n + 1)/2` — §5.3.4 Try 6 of Sullivan and
405+
Mackey's *An Introduction to Proofs*. The discrete antiderivative is `q(k) r^k` with `q` a
406+
polynomial of the same degree, found by one division by `r - 1` per coefficient, so a symbolic
407+
ratio carries the case `r = 1` separately, where the sum is the polynomial's. To `+oo` the series
408+
converges for `|r| < 1`: `sum(k / 2^k, k, 1, +oo)` is `2`, and `sum(k x^k, k, 0, +oo)` is
409+
`x/(1 - x)^2 provided |x| < 1`.
410+
398411
A sum **to `+oo`** is answered where it converges — the geometric, exponential and binomial series
399412
have closed forms — and, failing that, by the **nth-term test**: terms that do not tend to zero
400413
mean no finite value, and a limit with a sign says which infinity, so `sum(2^k, k, 0, +oo)` and

‎Sources/AngouriMath/Functions/Algebra/Polynomials/GeometricSeries.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ private static Entity PowerAt(Entity ratio, Entity bound)
132132
/// be symbolic. Read off the tree: <c>k - k</c> does not simplify to a bare 0 but to 0
133133
/// with what it assumes, so the exponent is not rewritten to find out.
134134
/// </summary>
135-
private static bool TryReadLinear(Entity exponent, Variable index, out int slope, out Entity offset)
135+
internal static bool TryReadLinear(Entity exponent, Variable index, out int slope, out Entity offset)
136136
{
137137
offset = Integer.Zero;
138138
switch (exponent)
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
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| &lt; 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

Comments
 (0)