Skip to content

Commit 29af2fd

Browse files
A rational integral over the roots of its denominator (#1637)
The Rothstein-Trager resultant declined a rational function whose residues lie in a field of degree above two: 1/(x^3 + x + 1) and #1285's sqrt(x)/(1 + x + x^4), 2u^2/(1 + u^2 + u^8) under u = sqrt(x), were left unevaluated. Their logarithms are written now as a sum over the roots of the factor of the denominator they belong to, sum(A(r)/D'(r) ln(x - r), r in { r : E(r) = 0 }), the form #1285 asked for, with the node #1624 added. The poles whose residues are roots of R are the common roots of D and D'^n R(A/D'), so E is their gcd over the rationals, and no arithmetic in the residues' field is needed. Only as a last resort. Asked beside the other rules, the sum answered each of Jeffrey's three terms of (-1 + 4 cos(x) + 5 cos(x)^2)/(-1 - 4 cos(x) - 3 cos(x)^2 + 4 cos(x)^3) with a sum over the roots of a sextic, where the whole is 2 arctan(tan(x/2)^3 - 2 tan(x/2)). So the rule declines as before and notes that a sum would answer, and only where the question comes back unanswered and that was noted is it asked again with sums allowed, under a memo of its own; the answer found then is remembered for the question. The integrator checks an answer in double intervals and then in decimals, and neither read the sum node, so every such answer would have been declined by its own check. PreciseEvaluation encloses each root now: Durand-Kerner's approximations, each widened by its Weierstrass correction in intervals, n |w_k| about z_k, which holds exactly one root where the rectangles are apart (Braess and Hadeler), and the summand worked out on each rectangle. Measured on the Rubi corpus, master at ad05a79 and this change on it, run side by side: the independent suites 1753 -> 1756 of 1814 (Bondarenko 22 and 23, and Hearn 66, 1/(1 - x^4 + x^8)); family 1 at ten problems a file, 295 -> 301 of 377 (1/(x^2 (1 - x^3 + x^6)), (1 + x^4)/(1 - x^4 + x^8), (1 + x^4)/(1 - 5x^4 + x^8), (-1 + 2x^4 + sqrt(3))/(1 - x^4 + x^8), (1 - x^4)/(1 - x^4 + x^8) and x^3 (5 + x + 3x^2 + 2x^3)/(2 + x + 5x^2 + x^3 + 2x^4)); families 2 to 7 at the usual sample, 663 of 722 on both. 0 wrong everywhere. Run alone, those nine are 0 of 9 on master and 9 of 9 here, and the tenth problem that moved, 1.1.4.3 #231, is 17.9 s unevaluated on both: its master timeout was the load of the side-by-side run. A question still declined costs what it did: the problems unevaluated in both arms took 14.2 s against 13.4 s in the independent suites, 65.6 s against 64.7 s in families 2 to 7 and 89.9 s against 92.3 s in the family 1 sample. The performance gate passes on 34c548a8, whose library code is this commit's: allocation is what the baseline says on all 19 gated benchmarks. RothsteinTragerTest: the cubic, quartic and quintic rows that were pinned as declined are sums now, and #1285's integral in PowerSubstitutionIntegralTest; that test's declined witnesses are the elliptic x^2/sqrt(x^4 + x + 1) and x^2/sqrt(x^4 + x^3 + 1), which nothing answers. BinomialDenominatorIntegralTest, PartialFractionsTest and RationalIntegralsTest pinned 1/(x^3 + x + 1), 1/(x^3 + x^2 + x + 2), 1/(x^4 + x + 1) and 1/(x^4 + x^3 + 1) as declined: each is the sum now, checked by differentiating back, and the boundary pinned in their place is the same kind of denominator with a symbol among its coefficients, which the sum is not written for. DecliningStaysCheap keeps its ten-second bound as FindingThatNothingSplitsStaysCheap: the second pass starts only once the first has declined, so the decline it guards is still inside the bound. Closes #1285. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent ad05a79 commit 29af2fd

9 files changed

Lines changed: 329 additions & 66 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1699,6 +1699,31 @@ where it is worked out, is not affected.
16991699
| `"sum(1/(k - x), k, 1, n)".ToEntity().DomainCondition` | `not k - x = 0` | `forall k in { k : k in ZZ and 1 <= k and k <= n } : not k - x = 0` |
17001700
| `"integral(1/(t - x), t, 0, 1)".ToEntity().DomainCondition` | `not t - x = 0` | `forall t in [0; 1] : not t - x = 0` |
17011701

1702+
### A rational function whose residues are of degree three or more is integrated over its poles
1703+
1704+
**Wider.** The logarithmic part of a rational integral is a logarithm at each pole, weighted by the
1705+
residue there. Where the residues are rational or roots of a quadratic they were written out, as
1706+
logarithms and arctangents. Where they lie in a field of degree three or more, the rational
1707+
integrator declined, and `1/(x^3 + x + 1)` was left unevaluated. It is written now as a sum over the
1708+
poles, `sum(A(r)/D'(r) ln(x - r), r in { r : E(r) = 0 })`, with the node above: `E` is the factor
1709+
of the denominator whose poles have those residues, the gcd of `D` and `D'^n R(A/D')` over the
1710+
rationals. The integrand of [#1285](https://github.com/asc-community/AngouriMath/issues/1285),
1711+
`sqrt(x)/(1 + x + x^4)`, is `2u^2/(1 + u^2 + u^8)` under `u = sqrt(x)`, and is answered through it.
1712+
1713+
**Only where nothing else answers.** The integral is asked again with sums over roots allowed once
1714+
the whole of it has come back unevaluated, so an integrand another rule writes in closed form keeps
1715+
that form. Asked with the other rules, the sum answered each of Jeffrey's three terms of
1716+
`(-1 + 4 cos(x) + 5 cos(x)^2)/(-1 - 4 cos(x) - 3 cos(x)^2 + 4 cos(x)^3)` with a sum over the roots of
1717+
a sextic, where the whole is one arctangent. A denominator with a symbol among its coefficients is
1718+
still declined. Both columns measured on a build, `v2.5.0` against this change.
1719+
1720+
| | Was (2.5.0) | Is |
1721+
|---|---|---|
1722+
| `"1/(x^3 + x + 1)".Integrate("x")` | `integral(1 / (x ^ 3 + x + 1), x)` | `sum(1 / (3 * r ^ 2 + 1) * ln(x - r), r in { r : r ^ 3 + r + 1 = 0 }) + C` |
1723+
| `"sqrt(x)/(1 + x + x^4)".Integrate("x")` | `integral(sqrt(x) / (1 + x + x ^ 4), x)` | `sum(2 * r ^ 2 / (8 * r ^ 7 + 2 * r) * ln(x ^ (1/2) - r), r in { r : r ^ 8 + r ^ 2 + 1 = 0 }) + C` |
1724+
| `"1/(1 - x^4 + x^8)".Integrate("x")` | `integral(1 / (1 - x ^ 4 + x ^ 8), x)` | `sum(1 / (8 * r ^ 7 + (-4) * r ^ 3) * ln(x - r), r in { r : r ^ 8 + -r ^ 4 + 1 = 0 }) + C` |
1725+
| `"1/(x^4 + a*x + 1)".Integrate("x")` | `integral(1 / (x ^ 4 + a * x + 1), x)` | the same |
1726+
17021727
### `(a + b asech(c x))/(d + e x)^2` is integrated, and a root written apart with `|x|` no longer needs a parity
17031728

17041729
Rubi's 7.5.1 with a symbolic linear below the bar ran for ten minutes without an answer. By

‎Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs‎

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -446,14 +446,54 @@ private static Entity Normalized(Entity expr, Entity.Variable x) =>
446446
descentTruncated = true;
447447
return null;
448448
}
449-
if (descentDepth == 0)
449+
if (descentDepth != 0)
450+
return ComputeIndefiniteIntegralGuarded(expr, x, integrateByParts);
451+
descentTruncated = false;
452+
inProgress?.Clear();
453+
ASumOverRootsWouldAnswer = false;
454+
var answer = ComputeIndefiniteIntegralGuarded(expr, x, integrateByParts);
455+
if (answer is not null || !ASumOverRootsWouldAnswer || SumsOverRootsAllowed)
456+
return answer;
457+
// Once more with sums over roots, and with a memo of its own, since the first pass
458+
// remembered every one of those rational functions as declined. Last, and only where
459+
// nothing else answered: asked with the rest, a sum over roots answered each of
460+
// Jeffrey's three terms, where the whole is one arctangent the rational integrator
461+
// writes when it is asked the whole.
462+
// https://github.com/asc-community/AngouriMath/issues/1285
463+
var (memo, stamp) = (answered, answeredUnder);
464+
SumsOverRootsAllowed = true;
465+
answered = null;
466+
descentTruncated = false;
467+
inProgress?.Clear();
468+
try
450469
{
451-
descentTruncated = false;
452-
inProgress?.Clear();
470+
answer = ComputeIndefiniteIntegralGuarded(expr, x, integrateByParts);
453471
}
454-
return ComputeIndefiniteIntegralGuarded(expr, x, integrateByParts);
472+
finally
473+
{
474+
SumsOverRootsAllowed = false;
475+
(answered, answeredUnder) = (memo, stamp);
476+
}
477+
// In place of the decline the first pass remembered, which a question asked again
478+
// would otherwise be answered with, without the rule ever being reached to say that a
479+
// second pass could answer it.
480+
if (answer is not null && memo is not null && stamp is not null && SettingsState.StillHolds(stamp))
481+
memo[(Normalized(expr, x), x, integrateByParts, true)] = answer;
482+
return answer;
455483
}
456484

485+
/// <summary>
486+
/// Whether the rational integrator may answer with a sum over the roots of a factor of the
487+
/// denominator: only in the second pass of a question nothing else answered.
488+
/// </summary>
489+
[System.ThreadStatic] internal static bool SumsOverRootsAllowed;
490+
491+
/// <summary>
492+
/// Set where the rational integrator declined only because the residues lie in a field of
493+
/// degree above two, which a sum over roots would have written.
494+
/// </summary>
495+
[System.ThreadStatic] internal static bool ASumOverRootsWouldAnswer;
496+
457497
/// <summary>
458498
/// <see cref="ComputeIndefiniteIntegral"/> past its entry check: the cycle guard, the
459499
/// depth, and the memo.

‎Sources/AngouriMath/Functions/Continuous/Integration/RothsteinTrager.cs‎

Lines changed: 84 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@ namespace AngouriMath.Functions.Algebra
3939
/// <c>a ± w</c> with <c>w^2</c> rational, and the gcd is computed in <c>Q(w)</c>: for
4040
/// <c>w</c> real the pair is two logarithms with a root in them, and for <c>w = i b</c> it
4141
/// is <c>a ln(P^2 + b^2 Q^2) + b LogToAtan(P, b Q)</c>, Rioboo's continuous arctangent
42-
/// form. A factor of higher degree would put the residues in a field this does not do
43-
/// arithmetic in, and is declined.
42+
/// form. A factor of higher degree puts the residues in a field this does no arithmetic in,
43+
/// and those logarithms are written as a sum over the roots of the factor of the denominator
44+
/// they belong to, <c>sum(A(r)/D'(r) ln(x - r), r in { r : E(r) = 0 })</c>
45+
/// (https://github.com/asc-community/AngouriMath/issues/1285).
4446
/// </para>
4547
/// <para>
4648
/// Bronstein, <i>Symbolic Integration I</i>, §2.2 (HermiteReduce), §2.5
@@ -64,8 +66,8 @@ internal static class RothsteinTrager
6466
/// <summary>
6567
/// The integral of <paramref name="numerator"/> over <paramref name="denominator"/>,
6668
/// both polynomials in <paramref name="x"/> with rational coefficients and the fraction
67-
/// proper; <see langword="null"/> where they are not, or where a residue lies in a field
68-
/// of degree above two.
69+
/// proper; <see langword="null"/> where they are not, or where the answer does not
70+
/// differentiate back to the integrand at the sampled points.
6971
/// </summary>
7072
internal static Entity? Integrate(Entity numerator, Entity denominator, Variable x)
7173
{
@@ -148,23 +150,54 @@ private static (Entity Rational, RationalPolynomial Above, RationalPolynomial Be
148150
if (SquareFree(resultantPolynomial) is not { } residueParts)
149151
return null;
150152

151-
// Every residue field first, and the residues only then: a residue in a field of
152-
// degree above two declines the whole, and the conjugate pair of a quadratic factor
153-
// beside it was twelve seconds of gcds over the extension before the quartic factor
154-
// was reached and declined it.
155-
var factored = new List<(IReadOnlyList<SquareFreeDecomposition.SquareFreePart> Factors, int Multiplicity)>();
153+
// Every residue field first, and the residues only then. A residue in a field of degree
154+
// above two is written as a sum over roots, below, and where there is one, every
155+
// residue that is not rational goes into that sum with it: the conjugate pair of a
156+
// quadratic factor beside a quartic one was twelve seconds of gcds over the extension,
157+
// and a sum over roots needs no arithmetic in any extension at all.
158+
var factors = new List<(IReadOnlyList<SquareFreeDecomposition.SquareFreePart> Factors, int Multiplicity)>();
156159
foreach (var part in residueParts)
157160
{
158-
var factors = PolynomialFactorization.FactorPrimitive(ToInteger(part.Factor).PrimitivePart());
159-
if (factors is null || factors.Any(irreducible => irreducible.Factor.Degree > 2))
161+
if (PolynomialFactorization.FactorPrimitive(ToInteger(part.Factor).PrimitivePart()) is not { } irreducibles)
160162
return null;
161-
factored.Add((factors, part.Multiplicity));
163+
factors.Add((irreducibles, part.Multiplicity));
164+
}
165+
var overRoots = factors.Any(part => part.Factors.Any(irreducible => irreducible.Factor.Degree > 2));
166+
// Only once nothing else has answered the question: see
167+
// Integration.SumsOverRootsAllowed. Until then this declines, as it always did.
168+
if (overRoots && !Integration.SumsOverRootsAllowed)
169+
{
170+
Integration.ASumOverRootsWouldAnswer = true;
171+
return null;
172+
}
173+
var factored = new List<(IReadOnlyList<SquareFreeDecomposition.SquareFreePart> Factors, int Multiplicity)>();
174+
var summed = RationalPolynomial.One;
175+
var summedRoots = 0;
176+
foreach (var (irreducibles, multiplicity) in factors)
177+
{
178+
if (!overRoots)
179+
{
180+
factored.Add((irreducibles, multiplicity));
181+
continue;
182+
}
183+
factored.Add((irreducibles.Where(irreducible => irreducible.Factor.Degree == 1).ToList(), multiplicity));
184+
foreach (var irreducible in irreducibles.Where(irreducible => irreducible.Factor.Degree > 1))
185+
{
186+
summed = summed.Multiply(RationalPolynomial.FromInteger(irreducible.Factor));
187+
summedRoots += irreducible.Factor.Degree * multiplicity;
188+
}
162189
}
163190
Entity total = Integer.Create(0);
164-
foreach (var (factors, multiplicity) in factored)
191+
if (overRoots)
165192
{
166-
var part = (Multiplicity: multiplicity, Factors: factors);
167-
foreach (var irreducible in factors)
193+
if (OverTheRoots(summed, summedRoots, above, below, derivative, x) is not { } sum)
194+
return null;
195+
total = sum;
196+
}
197+
foreach (var (kept, multiplicity) in factored)
198+
{
199+
var part = (Multiplicity: multiplicity, Factors: kept);
200+
foreach (var irreducible in kept)
168201
{
169202
var f = irreducible.Factor;
170203
Entity? term = f.Degree switch
@@ -181,6 +214,42 @@ private static (Entity Rational, RationalPolynomial Above, RationalPolynomial Be
181214
return total;
182215
}
183216

217+
/// <summary>
218+
/// The logarithms at the poles whose residues are roots of <paramref name="residues"/>,
219+
/// as a sum over those poles: <c>sum(A(r)/D'(r) ln(x - r), r in { r : E(r) = 0 })</c>,
220+
/// where <c>E</c> is the factor of <c>D</c> they are the roots of. <see langword="null"/>
221+
/// where <c>E</c> does not have the <paramref name="roots"/> the residues' multiplicities
222+
/// count.
223+
/// </summary>
224+
/// <remarks>
225+
/// The residue at a simple pole <c>b</c> of <c>A/D</c> is <c>A(b)/D'(b)</c>, a root of the
226+
/// resultant, and <c>D'(b)</c> is not zero there. So the poles whose residues are roots of
227+
/// <c>R</c> are the common roots of <c>D</c> and the polynomial <c>D'^n R(A/D')</c>, and
228+
/// <c>E</c> is their gcd over the rationals: no arithmetic in the field the residues lie in
229+
/// is needed. The sum is the one Rothstein–Trager's theorem writes over the roots of the
230+
/// resultant, with its terms taken pole by pole. <c>E</c> has a factor of degree above two,
231+
/// since a pole in a field of degree at most two has its residue there too, so the sum is
232+
/// left standing rather than written out in radicals, unless each such factor is a
233+
/// binomial, whose roots are written.
234+
/// https://github.com/asc-community/AngouriMath/issues/1285
235+
/// </remarks>
236+
private static Entity? OverTheRoots(RationalPolynomial residues, int roots,
237+
RationalPolynomial above, RationalPolynomial below, RationalPolynomial derivative, Variable x)
238+
{
239+
// D'^n R(A/D') = sum over k of r_k A^k D'^(n - k).
240+
var n = residues.Degree;
241+
var cleared = RationalPolynomial.Zero;
242+
for (var k = 0; k <= n; k++)
243+
cleared = cleared.Add(above.Pow(k).Multiply(derivative.Pow(n - k)).ScaleBy(residues[k]));
244+
var poles = RationalPolynomial.FromInteger(IntegerPolynomial.Gcd(ToInteger(below), ToInteger(cleared)).PrimitivePart());
245+
if (poles.Degree != roots)
246+
return null;
247+
// The polynomials are in x alone, so any other name is free.
248+
var r = MathS.Var(x.Name == "r" ? "w" : "r");
249+
var summand = above.ToEntity(r) / derivative.ToEntity(r) * MathS.Ln(x - r);
250+
return MathS.Sum(summand, r, new Set.ConditionalSet(r, poles.ToEntity(r).Equalizes(Integer.Zero)));
251+
}
252+
184253
/// <summary><c>c ln(gcd(D, A - c D'))</c> for a rational residue <paramref name="c"/>.</summary>
185254
private static Entity? RationalResidue(
186255
ERational c, int degree, RationalPolynomial above, RationalPolynomial below, RationalPolynomial derivative, Variable x)

0 commit comments

Comments
 (0)