Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions Sources/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
17 changes: 15 additions & 2 deletions Sources/AngouriMath/Docs/Usage/Syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ private static Entity PowerAt(Entity ratio, Entity bound)
/// be symbolic. Read off the tree: <c>k - k</c> does not simplify to a bare 0 but to 0
/// with what it assumes, so the exponent is not rewritten to find out.
/// </summary>
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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// A summation whose summand is a polynomial in the index times a power with the index in
/// the exponent: <c>sum(k 2^k, k, 1, n)</c> is <c>(n - 1) 2^(n + 1) + 2</c>, and
/// <c>sum((-1)^(k - 1) k^2, k, 1, n)</c> is <c>(-1)^(n - 1) n (n + 1)/2</c>, which is
/// §5.3.4 Try 6 of Sullivan and Mackey's <i>An Introduction to Proofs</i>.
/// </summary>
/// <remarks>
/// <para>
/// 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
/// ratio <c>r = b^m</c>, the way <see cref="GeometricSeries"/> reads its summand, with a
/// polynomial <c>p</c> of degree at least one beside the power. For a ratio other than 1
/// there is a polynomial <c>q</c> of the same degree with <c>q(k + 1) r - q(k) = p(k)</c>,
/// so that <c>q(k) r^k</c> is a discrete antiderivative of the summand, and the sum from
/// <c>a</c> to <c>b</c> is <c>q(b + 1) r^(b + 1) - q(a) r^a</c>. The coefficients of
/// <c>q</c> are found highest first: the coefficient of <c>k^j</c> in <c>q(k + 1) r - q(k)</c>
/// is <c>(r - 1) q_j + r sum_{i > j} binomial(i, j) q_i</c>, one division by <c>r - 1</c>
/// 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.
/// </para>
/// <para>
/// At <c>r = 1</c> the summand is the polynomial, which <see cref="PolynomialSummation"/>
/// answers; a symbolic ratio carries <c>provided not r = 1</c>, since the formula divides
/// by <c>r - 1</c>. To <c>+oo</c> the series converges exactly when <c>|r| &lt; 1</c>, to
/// <c>-q(a) r^a</c>, since <c>q(k) r^k</c> then goes to zero. Part of
/// <a href="https://github.com/asc-community/AngouriMath/issues/1409">#1409</a>.
/// </para>
/// </remarks>
internal static class PolynomialGeometricSeries
{
/// <summary>
/// <c>sum(expression, index, from, to)</c> in closed form, or <see langword="null"/>
/// 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 <c>+oo</c>.
/// </summary>
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;
}

/// <summary>
/// <c>ratio ^ bound</c>, as the number 1 where the bound is 0, for the reason
/// <see cref="GeometricSeries"/> gives.
/// </summary>
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;
}
}
}
Loading
Loading