diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md
index fb51266a2..5fe7033d8 100644
--- a/BREAKING-CHANGES.md
+++ b/BREAKING-CHANGES.md
@@ -920,6 +920,25 @@ apart as `sgn(P) P` with `provided P^2 > 0` — only where the root is a factor
| `"acsch(a+b*x)/x^2".Integrate("x")` | left unevaluated | the antiderivative |
| `"1/(x*(a+b*x)^2*sqrt(1/(a+b*x)^2-1))".Integrate("x")` | left unevaluated | the antiderivative |
+### A sum of a polynomial times a power of the index is answered in closed form
+
+`sum(k 2^k, k, 1, n)` was left as written; it is `(n - 1) 2^(n + 1) + 2`, by the discrete
+antiderivative `q(k) r^k` with `q` a polynomial of the summand's degree, found by one division
+by `r - 1` per coefficient. A symbolic ratio carries the case `r = 1` separately, where the
+summand is the polynomial, 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)`, is the
+row that asked for it. And a quantified statement whose body is a piecewise with a case on a
+parameter -- `q = 1 and n >= 0`, which is how the sum comes -- is decided by cases on the
+parameter, so their Ex 5.2.6 is answered with the condition the book asks about
+([#1409](https://github.com/asc-community/AngouriMath/issues/1409)).
+
+| Input | Was (2.5.0) | Now |
+|---|---|---|
+| `"sum(k*2^k, k, 1, n)".ToEntity().Evaled` | as written | `((n - 1) 2^(n + 1) + 2) provided n >= 0`, `0` otherwise |
+| `"sum((-1)^(k-1)*k^2, k, 1, n)".ToEntity().Evaled` | as written | `(-1)^(n - 1) n (n + 1)/2`, up to the form |
+| `"sum(k/2^k, k, 1, +oo)".ToEntity().Evaled` | as written | `2` |
+| `"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` |
+
### `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 7d265d09d..11556167f 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/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
+
+[AngouriMath/Functions/Algebra/Polynomials/PolynomialGeometricSeries.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/InverseHyperbolicOfALinearIntegralTest.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 534452337..a0dbca0e3 100644
--- a/Sources/AngouriMath/Docs/Usage/Syntax.md
+++ b/Sources/AngouriMath/Docs/Usage/Syntax.md
@@ -323,8 +323,12 @@ and, with the sum to `n + 1` unfolded to the sum to `n` plus one term and that s
denominator is not zero: decided over the set for the quantified name (`n + 1` is never `0` on
`ZZ+`), and stated as a condition for a free parameter (`forall n in ZZ+ : sum(1/(a k (k + 1)), k, 1, n) =
n/(a (n + 1))` is `True provided not a = 0`). Only the shape with one sum or product
-alone on a side of an equation is read this way; a sum whose closed form has a case on a free
-parameter is left as written. **An inequality with an exponential or a factorial in `n`** is
+alone on a side of an equation is read this way. A sum whose closed form has a case on a free
+parameter — `q = 1` beside `n >= 0`, which is how `sum(q^i, i, 0, n - 1)` comes — is decided
+**by cases on the parameter**: the statement under the hypothesis and under its negation, and it
+holds provided the hypothesis where it holds there alone, so Sullivan and Mackey's 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. **An inequality with an exponential or a factorial in `n`** is
decided by induction too, with the step read off a multiple of the hypothesis: writing it as
`P(n) >= 0`, `P(n + 1) = c P(n) + D` for a `c >= 0` (one, the base of an exponential, `n + 1`
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
a whole one still stays as written, since the index runs over the integers and
`sum(k, k, 1, 5/2)` is `1 + 2` rather than the polynomial at `5/2`.
+A `sum` whose body is a **polynomial in the index times a power with the index in the exponent**
+is answered in closed form too: `sum(k * 2^k, k, 1, n)` is `(n - 1) 2^(n + 1) + 2`, and
+`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
+Mackey's *An Introduction to Proofs*. The discrete antiderivative is `q(k) r^k` with `q` a
+polynomial of the same degree, found by one division by `r - 1` per coefficient, so a symbolic
+ratio carries the case `r = 1` separately, where the sum is the polynomial's. To `+oo` the series
+converges for `|r| < 1`: `sum(k / 2^k, k, 1, +oo)` is `2`, and `sum(k x^k, k, 0, +oo)` is
+`x/(1 - x)^2 provided |x| < 1`.
+
A sum **to `+oo`** is answered where it converges — the geometric, exponential and binomial series
have closed forms — and, failing that, by the **nth-term test**: terms that do not tend to zero
mean no finite value, and a limit with a sign says which infinity, so `sum(2^k, k, 0, +oo)` and
diff --git a/Sources/AngouriMath/Functions/Algebra/Polynomials/GeometricSeries.cs b/Sources/AngouriMath/Functions/Algebra/Polynomials/GeometricSeries.cs
index 234bd7124..5adcc8b40 100644
--- a/Sources/AngouriMath/Functions/Algebra/Polynomials/GeometricSeries.cs
+++ b/Sources/AngouriMath/Functions/Algebra/Polynomials/GeometricSeries.cs
@@ -132,7 +132,7 @@ private static Entity PowerAt(Entity ratio, Entity bound)
/// be symbolic. Read off the tree: k - k does not simplify to a bare 0 but to 0
/// with what it assumes, so the exponent is not rewritten to find out.
///
- private static bool TryReadLinear(Entity exponent, Variable index, out int slope, out Entity offset)
+ internal static bool TryReadLinear(Entity exponent, Variable index, out int slope, out Entity offset)
{
offset = Integer.Zero;
switch (exponent)
diff --git a/Sources/AngouriMath/Functions/Algebra/Polynomials/PolynomialGeometricSeries.cs b/Sources/AngouriMath/Functions/Algebra/Polynomials/PolynomialGeometricSeries.cs
new file mode 100644
index 000000000..7e704bcdf
--- /dev/null
+++ b/Sources/AngouriMath/Functions/Algebra/Polynomials/PolynomialGeometricSeries.cs
@@ -0,0 +1,169 @@
+//
+// 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
+{
+ ///
+ /// A summation whose summand is a polynomial in the index times a power with the index in
+ /// the exponent: sum(k 2^k, k, 1, n) is (n - 1) 2^(n + 1) + 2, and
+ /// sum((-1)^(k - 1) k^2, k, 1, n) is (-1)^(n - 1) n (n + 1)/2, which is
+ /// §5.3.4 Try 6 of Sullivan and Mackey's An Introduction to Proofs.
+ ///
+ ///
+ ///
+ /// The summand is read as p(k) C b^(m k + s), which is C b^s p(k) r^k with the
+ /// ratio r = b^m, the way reads its summand, with a
+ /// polynomial p of degree at least one beside the power. For a ratio other than 1
+ /// there is a polynomial q of the same degree with q(k + 1) r - q(k) = p(k),
+ /// so that q(k) r^k is a discrete antiderivative of the summand, and the sum from
+ /// a to b is q(b + 1) r^(b + 1) - q(a) r^a. The coefficients of
+ /// q are found highest first: the coefficient of k^j in q(k + 1) r - q(k)
+ /// is (r - 1) q_j + r sum_{i > j} binomial(i, j) q_i, one division by r - 1
+ /// each, which is the whole of the linear algebra -- Gosper's algorithm specialised to a
+ /// summand whose ratio of consecutive terms is a rational function with a constant
+ /// denominator.
+ ///
+ ///
+ /// At r = 1 the summand is the polynomial, which
+ /// answers; a symbolic ratio carries provided not r = 1, since the formula divides
+ /// by r - 1. To +oo the series converges exactly when |r| < 1, to
+ /// -q(a) r^a, since q(k) r^k then goes to zero. Part of
+ /// #1409.
+ ///
+ ///
+ internal static class PolynomialGeometricSeries
+ {
+ ///
+ /// sum(expression, index, from, to) in closed form, or
+ /// where the summand is not a polynomial in the index times a power with a whole
+ /// multiple of the index in its exponent, or a bound is a number that is neither whole
+ /// nor +oo.
+ ///
+ internal static Entity? ClosedForm(Entity expression, Entity var, Entity from, Entity to)
+ {
+ if (var is not Variable index)
+ return null;
+ if (!PolynomialSummation.IsWholeOrSymbolic(from))
+ return null;
+ var toInfinity = to.Evaled is Real { IsFinite: false, IsNaN: false, IsNegative: false };
+ if (!toInfinity && !PolynomialSummation.IsWholeOrSymbolic(to))
+ return null;
+
+ // `k/2^k` as `k 2^(-k)`: the power below the bar is the power with its exponent
+ // negated, and the factors are then read as they are read above it.
+ var summand = expression.InnerSimplified;
+ if (summand is Divf(var over, Powf(var underBase, var underExponent)) && !underBase.ContainsNode(index) && underExponent.ContainsNode(index))
+ summand = over * MathS.Pow(underBase, (-underExponent).InnerSimplified);
+ Entity? @base = null;
+ Entity? exponent = null;
+ Entity constant = Integer.One;
+ Entity? polynomial = null;
+ foreach (var factor in Mulf.LinearChildren(summand))
+ {
+ if (!factor.ContainsNode(index))
+ constant *= factor;
+ else if (factor is Powf(var b, var e) && @base is null && !b.ContainsNode(index) && e.ContainsNode(index))
+ {
+ @base = b;
+ exponent = e;
+ }
+ else
+ polynomial = polynomial is null ? factor : polynomial * factor;
+ }
+ if (@base is null || polynomial is null || !GeometricSeries.TryReadLinear(exponent!, index, out var slope, out var offset))
+ 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;
+ if (@base.Evaled is Complex value && IsZero(value))
+ return null;
+
+ if (offset != Integer.Zero)
+ constant *= MathS.Pow(@base, offset);
+ var ratio = slope == 1 ? @base : MathS.Pow(@base, Integer.Create(slope));
+ var ratioIsANumber = ratio.Evaled is Complex;
+ if (ratioIsANumber && ratio.Evaled == Integer.One)
+ return null;
+
+ // The coefficients of q, highest first: (r - 1) q_j + r sum_{i > j} C(i, j) q_i = p_j.
+ var p = new Entity[degree + 1];
+ for (var j = 0; j <= degree; j++)
+ p[j] = monomials.TryGetValue(EInteger.FromInt32(j), out var coefficient) ? coefficient : Integer.Zero;
+ var q = new Entity[degree + 1];
+ var rMinusOne = ratio - Integer.One;
+ for (var j = degree; j >= 0; j--)
+ {
+ Entity above = Integer.Zero;
+ for (var i = j + 1; i <= degree; i++)
+ above += Integer.Create(Binomial(i, j)) * q[i];
+ q[j] = ((p[j] - ratio * above) / rMinusOne).InnerSimplified;
+ }
+ Entity Q(Entity at)
+ {
+ Entity sum = Integer.Zero;
+ for (var j = 0; j <= degree; j++)
+ if (q[j] != Integer.Zero)
+ sum += j == 0 ? q[j] : q[j] * MathS.Pow(at, j);
+ return sum;
+ }
+
+ if (toInfinity)
+ {
+ Entity limit = -constant * Q(from) * PowerAt(ratio, from);
+ if (ratioIsANumber)
+ return ((Complex)ratio.Evaled).Abs() < 1 ? limit.InnerSimplified : null;
+ return new Providedf(limit, new Lessf(MathS.Abs(ratio), Integer.One)).InnerSimplified;
+ }
+
+ var after = (to + Integer.One).InnerSimplified;
+ Entity closed = constant * (Q(after) * PowerAt(ratio, after) - Q(from) * PowerAt(ratio, from));
+ var nonEmpty = new GreaterOrEqualf(to, from - Integer.One);
+ if (ratioIsANumber)
+ return MathS.Piecewise(new[]
+ {
+ new Providedf(closed, nonEmpty),
+ new Providedf(Integer.Zero, Entity.Boolean.True),
+ }).InnerSimplified;
+ // At r = 1 the summand is the polynomial, and that sum is the polynomial reader's:
+ // its closed form is the first case of what it answers, over the same range.
+ Entity atOne = new Summationf(constant * polynomial, index, from, to);
+ if (PolynomialSummation.ClosedForm(constant * polynomial, index, from, to) is Piecewise piecewise
+ && piecewise.Cases.FirstOrDefault() is Providedf(var polynomialSum, _))
+ atOne = polynomialSum;
+ return MathS.Piecewise(new[]
+ {
+ new Providedf(atOne, new Andf(new Equalsf(ratio, Integer.One), nonEmpty)),
+ new Providedf(closed, nonEmpty),
+ new Providedf(Integer.Zero, Entity.Boolean.True),
+ }).InnerSimplified;
+ }
+
+ ///
+ /// ratio ^ bound, as the number 1 where the bound is 0, for the reason
+ /// gives.
+ ///
+ private static Entity PowerAt(Entity ratio, Entity bound)
+ => bound.Evaled == Integer.Zero ? Integer.One : MathS.Pow(ratio, bound);
+
+ private static EInteger Binomial(int n, int k)
+ {
+ var result = EInteger.One;
+ for (var i = 1; i <= k; i++)
+ result = result.Multiply(n - k + i).Divide(i);
+ return result;
+ }
+ }
+}
diff --git a/Sources/AngouriMath/Functions/Boolean/Quantifiers.cs b/Sources/AngouriMath/Functions/Boolean/Quantifiers.cs
index d6be393b2..31df4f5f3 100644
--- a/Sources/AngouriMath/Functions/Boolean/Quantifiers.cs
+++ b/Sources/AngouriMath/Functions/Boolean/Quantifiers.cs
@@ -138,14 +138,56 @@ private static Entity Quantified(Kind kind, Entity var, Entity over, Entity body
// https://github.com/asc-community/AngouriMath/issues/1409
if (body is Piecewise piecewise)
{
- foreach (var @case in piecewise.Cases)
+ var cases = piecewise.Cases.ToList();
+ for (var i = 0; i < cases.Count; i++)
{
+ var @case = cases[i];
var holds = @case.Predicate == Entity.Boolean.True ? Entity.Boolean.True : Decide(Kind.All, x, set, @case.Predicate.InnerSimplified(isExact), isExact);
if (holds == Entity.Boolean.True)
{
by = ($"the case {@case.Predicate} of the piecewise body holds at every member, so the body is that case", "if_pos");
return Decide(kind, x, set, @case.Expression.InnerSimplified(isExact), isExact);
}
+ // A case whose condition is a statement about a parameter beside one that
+ // holds at every member -- `q = 1 and n >= 0`, which is how a sum with a
+ // symbolic ratio comes -- is the case under that hypothesis and the cases
+ // after it under its negation: the statement is decided in each, and holds
+ // provided the hypothesis where it holds there alone. Sullivan and Mackey's
+ // Ex 5.2.6, the geometric sum with `q = 1` set aside.
+ if (SplitOffParameterCondition(@case.Predicate, x, set, isExact) is (Entity hypothesis, var atOne))
+ {
+ var under = (atOne is var (parameter, value) ? @case.Expression.Substitute(parameter, value) : @case.Expression).InnerSimplified(isExact);
+ var probedUnder = ProofRecording.Mark();
+ var whenItHolds = Decide(kind, x, set, under, isExact);
+ if (whenItHolds != Entity.Boolean.True)
+ ProofRecording.Rollback(probedUnder);
+ var rest = i + 1 >= cases.Count ? null
+ : i + 2 == cases.Count && cases[i + 1].Predicate == Entity.Boolean.True ? cases[i + 1].Expression
+ : MathS.Piecewise(cases.Skip(i + 1));
+ var otherwise = rest is null ? null : Decide(kind, x, set, rest.InnerSimplified(isExact), isExact);
+ // Each verdict is True, True under a condition of its own, False, or
+ // nothing; the statement is True under the disjunction of the two cases
+ // where each holds, and False where both fail.
+ var (holdsUnder, conditionUnder) = TrueUnder(whenItHolds);
+ var (holdsOtherwise, conditionOtherwise) = TrueUnder(otherwise);
+ if (holdsUnder && holdsOtherwise)
+ {
+ by = ($"the statement holds both under {hypothesis} and under its negation", "by_cases");
+ return conditionUnder == Entity.Boolean.True && conditionOtherwise == Entity.Boolean.True ? Entity.Boolean.True
+ : Entity.Boolean.True.Provided((hypothesis & conditionUnder) | (!hypothesis & conditionOtherwise));
+ }
+ if (holdsOtherwise && whenItHolds is null or Entity.Boolean(false) or Number { IsNaN: true } or Providedf(Number { IsNaN: true }, _))
+ {
+ by = ($"the statement holds under the negation of {hypothesis}, and not as it stands under it", "by_cases");
+ return Entity.Boolean.True.Provided(conditionOtherwise == Entity.Boolean.True ? !hypothesis : WithoutRepeatedExclusions(!hypothesis & conditionOtherwise));
+ }
+ if (holdsUnder && otherwise is Entity.Boolean(false))
+ {
+ by = ($"the statement holds under {hypothesis} and fails under its negation", "by_cases");
+ return Entity.Boolean.True.Provided(conditionUnder == Entity.Boolean.True ? hypothesis : WithoutRepeatedExclusions(hypothesis & conditionUnder));
+ }
+ break;
+ }
var probed = ProofRecording.Mark();
var somewhere = Decide(Kind.Some, x, set, @case.Predicate.InnerSimplified(isExact), isExact);
ProofRecording.Rollback(probed);
@@ -1126,6 +1168,69 @@ internal static bool SolverReads(Entity body, Variable x)
_ => false,
};
+ ///
+ /// A conjunction with each exclusion of one value written once: not x = 1 and
+ /// not x - 1 = 0 and not (1 - x)^2 = 0 is not x = 1, since each excludes the
+ /// same root. An exclusion is not (l = r) in one variable whose equation the
+ /// solver answers with a listed set; two with the same set are one, and the first kept.
+ ///
+ private static Entity WithoutRepeatedExclusions(Entity condition)
+ {
+ var seen = new HashSet<(Variable, Entity)>();
+ Entity? kept = null;
+ foreach (var conjunct in Andf.LinearChildren(condition))
+ {
+ if (conjunct is Notf(Equalsf(var l, var r) equation) && equation.Vars.ToList() is { Count: 1 } one
+ && equation.Solve(one[0]) is FiniteSet roots)
+ {
+ if (!seen.Add((one[0], roots)))
+ continue;
+ }
+ kept = kept is null ? conjunct : kept & conjunct;
+ }
+ return kept ?? condition;
+ }
+
+ /// Whether a verdict is True, outright or under a condition, and the condition.
+ private static (bool Holds, Entity Condition) TrueUnder(Entity? verdict) => verdict switch
+ {
+ Entity.Boolean(true) => (true, Entity.Boolean.True),
+ Providedf(Entity.Boolean(true), var condition) => (true, condition),
+ _ => (false, Entity.Boolean.False),
+ };
+
+ ///
+ /// A predicate that is a conjunction of a part free of -- the
+ /// hypothesis about a parameter -- and a part in that holds at
+ /// every member of : the hypothesis, and, where it is
+ /// parameter = value, the pair to substitute. where the
+ /// predicate has no parameter part or its member part does not hold throughout.
+ ///
+ private static (Entity Hypothesis, (Variable, Entity)? Substitution)? SplitOffParameterCondition(Entity predicate, Variable x, Set set, bool isExact)
+ {
+ Entity? hypothesis = null;
+ Entity? aboutTheMember = null;
+ foreach (var conjunct in Andf.LinearChildren(predicate))
+ if (conjunct.ContainsNode(x))
+ aboutTheMember = aboutTheMember is null ? conjunct : aboutTheMember & conjunct;
+ else
+ hypothesis = hypothesis is null ? conjunct : hypothesis & conjunct;
+ if (hypothesis is null || hypothesis == Entity.Boolean.True)
+ return null;
+ if (aboutTheMember is { })
+ {
+ var probed = ProofRecording.Mark();
+ var holds = Decide(Kind.All, x, set, aboutTheMember.InnerSimplified(isExact), isExact);
+ ProofRecording.Rollback(probed);
+ if (holds != Entity.Boolean.True)
+ return null;
+ }
+ (Variable, Entity)? substitution = hypothesis is Equalsf(Variable parameter, var value) && !value.ContainsNode(parameter) ? (parameter, value)
+ : hypothesis is Equalsf(var other, Variable named) && !other.ContainsNode(named) ? (named, other)
+ : null;
+ return (hypothesis, substitution);
+ }
+
/// An expression the solver reads as a number: no sets, statements or binders inside.
private static bool Plain(Entity expression)
=> expression.Nodes.All(static node => node is not (Set or Statement or Quantifier or Lambda or Providedf or Piecewise));
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 926b0276e..1f72775ac 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
@@ -239,6 +239,7 @@ protected override Entity InnerSimplify(bool isExact) =>
?? Functions.ExponentialSeries.ClosedForm(Expression, Var, From, To)
?? Functions.BinomialSum.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
// answered is asked whether it diverges.
?? Functions.DivergentSeries.ClosedForm(Expression, Var, From, To)
diff --git a/Sources/Tests/UnitTests/Calculus/GeometricSeriesTest.cs b/Sources/Tests/UnitTests/Calculus/GeometricSeriesTest.cs
index 0936a1955..6b50ba1d8 100644
--- a/Sources/Tests/UnitTests/Calculus/GeometricSeriesTest.cs
+++ b/Sources/Tests/UnitTests/Calculus/GeometricSeriesTest.cs
@@ -90,9 +90,10 @@ public void ASymbolicLowerBoundIsAnsweredToo(string a, string n, string expected
// exponent that is not linear in the index, a base with the index in it.
// `sum(2^k, k, 0, +oo)` was here: this reader still declines it, a ratio at or beyond 1
// having no sum, but the nth-term test now answers it `+oo`. See DivergentSeriesTest.
+ // And `sum(k * (1/2)^k, k, 0, +oo)`: a polynomial beside the power is still not this
+ // reader's, and is summed by PolynomialGeometricSeries to 2, see its test.
[Theory]
[InlineData("sum((-1)^k, k, 0, +oo)")]
- [InlineData("sum(k * (1/2)^k, k, 0, +oo)")]
[InlineData("sum(2^(k^2), k, 0, 200)")]
[InlineData("sum(k^k, k, 1, 200)")]
[InlineData("sum(2^k, k, 0, 5/2)")]
diff --git a/Sources/Tests/UnitTests/Calculus/PolynomialGeometricSeriesTest.cs b/Sources/Tests/UnitTests/Calculus/PolynomialGeometricSeriesTest.cs
new file mode 100644
index 000000000..a1145ecfb
--- /dev/null
+++ b/Sources/Tests/UnitTests/Calculus/PolynomialGeometricSeriesTest.cs
@@ -0,0 +1,77 @@
+//
+// 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
+{
+ ///
+ /// A summation of a polynomial in the index times a power with the index in the exponent
+ /// is summed in closed form, to a bound or to +oo: the discrete antiderivative
+ /// q(k) r^k. §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.
+ ///
+ public sealed class PolynomialGeometricSeriesTest
+ {
+ /// The closed form against the sum written out, at a bound past the expansion's hundred terms and at small ones.
+ [Theory]
+ [InlineData("sum(k * 2^k, k, 1, n)", "(n - 1) * 2^(n + 1) + 2")]
+ [InlineData("sum((-1)^(k - 1) * k^2, k, 1, n)", "(-1)^(n - 1) * n * (n + 1) / 2")]
+ [InlineData("sum(k * 3^k, k, 0, n)", "((2 n - 1) * 3^(n + 1) + 3) / 4")]
+ [InlineData("sum((k^2 + 1) * 3^(2 k + 1), k, 2, n)", "3 * ((77/256 - 9/32 * (n + 1) + (n + 1)^2 / 8) * 9^(n + 1) - 4941/256)")]
+ [InlineData("sum(k^3 * (-2)^(k - 1), k, 1, n)", "sum(k^3 * (-2)^(k - 1), k, 1, n)")]
+ public void AFiniteRangeIsSummedInClosedForm(string sum, string expected)
+ {
+ var closed = sum.ToEntity().Simplify();
+ Assert.IsNotType(closed);
+ foreach (var n in new[] { 1, 2, 3, 7, 12, 150 })
+ {
+ var want = expected.ToEntity().Substitute("n", n).Evaled;
+ Assert.Equal(want, closed.Substitute("n", n).Evaled);
+ Assert.Equal(want, sum.ToEntity().Substitute("n", n).Evaled);
+ }
+ }
+
+ [Fact]
+ public void AnEmptyRangeIsZero()
+ => Assert.Equal(Number.Integer.Zero, "sum(k * 2^k, k, 1, n)".ToEntity().Simplify().Substitute("n", 0).Evaled);
+
+ [Theory]
+ [InlineData("sum(k / 2^k, k, 1, +oo)", "2")]
+ [InlineData("sum(k * 2^(-k), k, 0, +oo)", "2")]
+ [InlineData("sum(k^2 / 3^k, k, 0, +oo)", "3/2")]
+ [InlineData("sum((k + 1) * (-1/2)^k, k, 0, +oo)", "4/9")]
+ public void ASeriesWithANumericRatioBelowOneIsSummed(string sum, string expected)
+ => Assert.Equal(expected.ToEntity(), sum.ToEntity().Simplify());
+
+ [Fact]
+ public void ASeriesWithASymbolicRatioCarriesItsConditionAndItsRatioOneCase()
+ {
+ var value = "sum(k * x^k, k, 0, +oo)".ToEntity().Simplify();
+ Assert.IsNotType(value);
+ Assert.Equal("2".ToEntity(), value.Substitute("x", "1/2").Simplify());
+ Assert.Equal("-2/9".ToEntity(), value.Substitute("x", "-1/2").Simplify());
+ Assert.NotEqual("2".ToEntity(), value.Substitute("x", "2").Simplify());
+
+ var finite = "sum(k^2 * q^k, k, 0, n)".ToEntity().Simplify();
+ Assert.IsNotType(finite);
+ // At q = 1 the summand is k^2 and the sum is n(n + 1)(2n + 1)/6, not a division by zero.
+ Assert.Equal("30".ToEntity(), finite.Substitute("q", 1).Substitute("n", 4).Evaled);
+ Assert.Equal("sum(k^2 * 2^k, k, 0, 4)".ToEntity().Evaled, finite.Substitute("q", 2).Substitute("n", 4).Evaled);
+ }
+
+ /// The statement the book proves by induction is decided from the closed form.
+ [Theory]
+ [InlineData("forall n in ZZ+ : sum((-1)^(k-1)*k^2, k, 1, n) = (-1)^(n-1)*n*(n+1)/2")]
+ [InlineData("forall n in ZZ+ : sum(k*2^k, k, 1, n) = (n-1)*2^(n+1) + 2")]
+ public void TheIdentityIsDecided(string statement) => Assert.Equal(Boolean.True, statement.ToEntity().Evaled);
+ }
+}
diff --git a/Sources/Tests/UnitTests/Core/InductionTest.cs b/Sources/Tests/UnitTests/Core/InductionTest.cs
index 59de6c28e..9d5f71fff 100644
--- a/Sources/Tests/UnitTests/Core/InductionTest.cs
+++ b/Sources/Tests/UnitTests/Core/InductionTest.cs
@@ -63,8 +63,14 @@ public void ProvedByInduction(string statement)
[Theory]
// A closed form over a denominator in a free parameter holds where the denominator is
// not zero, and the answer says so; a denominator in the quantified name is decided
- // non-zero over the set instead, so those rows above carry no condition.
- [InlineData("forall n in ZZ+ : sum(k x^k, k, 1, n) = x (1 - (n + 1) x^n + n x^(n + 1)) / (1 - x)^2", "True provided not (1 - x)^2 = 0")]
+ // non-zero over the set instead, so those rows above carry no condition. The first
+ // row's sum is answered in closed form with the case `x = 1` set aside, and the
+ // statement is decided by cases on it: the sum's own case, not the denominator's
+ // spelling, is the condition it carries.
+ [InlineData("forall n in ZZ+ : sum(k x^k, k, 1, n) = x (1 - (n + 1) x^n + n x^(n + 1)) / (1 - x)^2", "True provided not x = 1")]
+ // Sullivan and Mackey's Ex 5.2.6, the geometric sum for q not 1 -- the book asks why
+ // q = 1 is excluded, and the answer names it.
+ [InlineData("forall n in ZZ+ : sum(q^i, i, 0, n - 1) = (q^n - 1) / (q - 1)", "True provided not q = 1")]
[InlineData("forall n in ZZ+ : sum(1 / (a k (k + 1)), k, 1, n) = n / (a (n + 1))", "True provided not a = 0")]
public void ProvedWhereTheClosedFormExists(string statement, string expected)
=> Assert.Equal(expected.ToEntity(), statement.ToEntity().Evaled);
@@ -116,10 +122,10 @@ public void AnInequalityByInduction(string statement, string expected)
=> Assert.Equal(expected.ToEntity(), statement.ToEntity().Evaled);
[Theory]
- // Not decided: a closed form with a case on a free parameter (q = 1 has its own), a wrong
- // closed form with a free parameter, an inequality whose step is not a multiple of the
- // hypothesis plus something of known sign, and a term the induction cannot unfold.
- [InlineData("forall n in ZZ+ : sum(q^k, k, 0, n - 1) = (q^n - 1) / (q - 1)")]
+ // Not decided: a wrong closed form with a free parameter, an inequality whose step is
+ // not a multiple of the hypothesis plus something of known sign, and a term the
+ // induction cannot unfold. The geometric sum with its case q = 1 was here, and is now
+ // decided by cases on q, see ProvedWhereTheClosedFormExists.
[InlineData("forall n in ZZ+ : sum(k x^k, k, 1, n) = x (1 - (n + 1) x^n + n x^(n + 1)) / (1 - x)")]
[InlineData("forall n in ZZ+ : sum(1 / k^2, k, 1, n) <= 2 - 1 / n")]
[InlineData("forall n in ZZ+ : sum(1 / sqrt(k), k, 1, n) >= sqrt(n)")]