From b3f5dafe5d93d4800bc083dc8a006a8f4d8f0f1a Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Sat, 28 Nov 2026 15:48:22 +0000 Subject: [PATCH] Take a sum over roots to 0 at +oo, and break it at a real root (#2624) The antiderivative of 1/(x^3 + x + 1) is sum(ln(x - r) / (3 r^2 + 1), r) over the roots of r^3 + r + 1. At an infinite bound it was substituted and left with +oo inside. Nothing settles that sum, and it evaluates to NaN, so the integral over [0, +oo) simplified to NaN where it is 0.92176. Its breaks were never listed either: ln(x - r) holds the bound r, whose zeros the solver cannot place. So over [-1, 0] the root sum was taken across the real root -0.6823 as if it were continuous, and the divergent integral came out as a complex number. At +oo, a root sum sum(c(r) ln(x - r)) is (sum c(r)) ln(x) plus terms that tend to 0. Where c(r) p'(r) is a polynomial of degree at most deg p - 2 and p is square-free, the Euler-Jacobi identity makes sum c(r) exactly 0, and the root sum is 0 at the bound. This is the condition for convergence of a rational integrand at +oo. A root sum not shown to vanish, and any root sum at -oo, leaves the bound with no value instead of NaN. The breaks of a root sum are now the real zeros of its polynomial in x. A real root inside the range therefore leaves the integral unevaluated unless the one-sided limits settle. Co-Authored-By: Claude Opus 5.5 --- BREAKING-CHANGES.md | 21 +++++ .../Integration/AntiderivativeBreaks.cs | 25 +++++- .../Integration/Integration.Definition.cs | 87 +++++++++++++++++++ .../ASumOverRootsAtInfinityIsZeroTest.cs | 61 +++++++++++++ 4 files changed, 193 insertions(+), 1 deletion(-) create mode 100644 Sources/Tests/UnitTests/Calculus/ASumOverRootsAtInfinityIsZeroTest.cs diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index 4c1062ec1..75a6e2e8c 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -115,6 +115,27 @@ it was ([#2611](https://github.com/asc-community/AngouriMath/issues/2611)). | `integral(1/x, x, -2, -1)` | `ln(-1) - ln(-2)` | `-ln(2)` | | `integral(1/(x^2-1), x, -1/2, 1/2)` | `(ln(-1/3) - ln(-3)) / 2` | `ln(1/3)` | +### An integral through a sum over roots is not NaN at infinity, nor taken across a real root + +**Not silent: wrong answers.** The antiderivative of `1/(x^3 + x + 1)` is a sum over the roots of `r^3 + r + 1`. +Taken at `+oo` it was that sum with `+oo` inside, which evaluates to `NaN`. So the integral over `[0, +oo)` simplified +to `NaN`, a claim that it does not exist, where it is `0.92176…`. Across a real root of the polynomial, the sum was +treated as continuous. The integral over `[-1, 0]`, which diverges at the root `-0.6823…`, came out as a complex +number. + +At `+oo` the root sum is now 0 where its multiples are `N(r) / p'(r)` with the degree of `N` at most `deg p - 2`. That +is the Euler–Jacobi identity, and it is what the integral's convergence asks of the integrand. A real root strictly +inside the range is now a break, and the integral is left unevaluated there unless both sides settle. At `-oo` a root +sum leaves the integral unevaluated ([#2624](https://github.com/asc-community/AngouriMath/issues/2624)). + +| simplified | Was (2.10.0) | Now | +|---|---|---| +| `integral(1/(x^3+x+1), x, 0, +oo)` | `NaN` | `-sum(ln(-r) / (1 + 3 * r ^ 2), r in { r : 1 + r ^ 3 + r = 0 })` = `0.92176…` | +| `integral(1/(x^3-x^2+1), x, 0, +oo)` | `NaN` | the root sum `1.79044…` | +| `integral(1/(x^5+x+1), x, 0, +oo)` | `NaN` | the root sum `0.79590…` | +| `integral(1/(x^3+x+1), x, -1, 0)` | a complex number | unevaluated (it diverges) | +| `integral(1/(x^3+x+1), x, -oo, 0)` | `NaN` | unevaluated | + ### An inequality with abs of x is solved **Not silent: an inequality that threw has an answer.** `Solve` threw `NotSufficientlySupportedException`, *"Only diff --git a/Sources/AngouriMath/Functions/Continuous/Integration/AntiderivativeBreaks.cs b/Sources/AngouriMath/Functions/Continuous/Integration/AntiderivativeBreaks.cs index 855ae5f7c..41875dfc2 100644 --- a/Sources/AngouriMath/Functions/Continuous/Integration/AntiderivativeBreaks.cs +++ b/Sources/AngouriMath/Functions/Continuous/Integration/AntiderivativeBreaks.cs @@ -1,4 +1,4 @@ -// +// // Copyright (c) 2019-2026 Angouri. // AngouriMath is licensed under MIT. // Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md. @@ -59,6 +59,19 @@ internal static class AntiderivativeBreaks internal static List<(Entity At, Real Value)>? Inside(Entity antiderivative, Variable x, Real lower, Real upper) { var breaks = new List<(Entity At, Real Value)>(); + // A sum over the roots of a polynomial, sum(c(r) ln(x - r)), breaks where x is one of its real roots, + // which are the zeros of the polynomial in x. Its terms are read no further: ln(x - r) has the bound r in + // it, whose zeros the solver cannot list, and so the root sum's breaks were never listed, and the integral + // of 1/(x^3 + x + 1) over [-1, 0] was taken across the pole at its real root as a complex number. + // https://github.com/asc-community/AngouriMath/issues/2624 + if (antiderivative.Nodes.Any(node => node is SumOverSetf sum && sum.ContainsNode(x))) + { + foreach (var sum in antiderivative.Nodes.OfType().Where(sum => sum.ContainsNode(x)).Distinct()) + if (RootPolynomial(sum, x) is not { } polynomial || !ZerosOf(polynomial, x, lower, upper, breaks)) + return null; + var standIn = Variable.CreateUnique(antiderivative, "s"); + antiderivative = antiderivative.Replace(node => node is SumOverSetf sum && sum.ContainsNode(x) ? standIn : node); + } // A subexpression repeated in the antiderivative is read once. var read = new HashSet(); foreach (var node in antiderivative.Nodes) @@ -93,6 +106,16 @@ internal static class AntiderivativeBreaks return distinct; } + /// + /// The polynomial in whose roots runs over, read off its set + /// { r : p(r) = q(r) } as p(x) - q(x), or where the set is not that shape. + /// + internal static Entity? RootPolynomial(SumOverSetf sum, Variable x) + => sum.Over is ConditionalSet { Var: Variable setVariable, Predicate: Equalsf(var left, var right) } + && !left.ContainsNode(x) && !right.ContainsNode(x) + ? (left - right).Substitute(setVariable, x) + : null; + /// /// Adds the real zeros of strictly inside the range, and /// says whether they could all be listed. A cosine or a sine is zero on a periodic set, diff --git a/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs b/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs index cd4f3ece6..7f24af230 100644 --- a/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs +++ b/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs @@ -323,6 +323,9 @@ private static bool HoldsAnIncompleteGammaFunction(Entity expr) /// private static Entity? AtTheBound(Entity antiderivative, Variable x, Entity bound, Entity otherBound, bool continuousInside) { + if (bound.Evaled is Number.Real { EDecimal: var infinite } && infinite.IsInfinity() + && antiderivative.Nodes.Any(node => node is SumOverSetf sum && sum.ContainsNode(x))) + return AtAnInfiniteBoundThroughSumsOverRoots(antiderivative, x, bound, otherBound, continuousInside, infinite.IsPositiveInfinity()); var value = antiderivative.Substitute(x, bound); if (!value.InnerSimplified.IsNaN) return continuousInside && AtAnInfiniteBound(antiderivative, x, bound, value) is { } atInfinity ? atInfinity : value; @@ -352,6 +355,90 @@ private static bool HoldsAnIncompleteGammaFunction(Entity expr) return limit.IsNaN ? value : limit; } + /// + /// The antiderivative's value at an infinite bound where it holds a sum over the roots of a polynomial, each such + /// sum taken at +oo to its limit 0 where that is shown; or . + /// + /// + /// + /// The antiderivative of 1/(x^3 + x + 1) is sum(ln(x - r) / (3 r^2 + 1), r) over the roots of + /// r^3 + r + 1. At +oo it was sum(ln(+oo - r) / (3 r^2 + 1), r), which nothing settles and + /// which evaluates to NaN, and the integral over [0, +oo) simplified to NaN, where it is + /// 0.92176.... + /// https://github.com/asc-community/AngouriMath/issues/2624 + /// + /// + /// sum(c(r) ln(x - r)) is (sum c(r)) ln(x) + sum(c(r) ln(1 - r/x)) for x past every root's + /// modulus, the principal logarithm being continuous near 1. The second part tends to 0 at +oo. The first + /// is 0 where c(r) is N(r) / p'(r) with p square-free and N of degree at most + /// deg p - 2, by the Euler–Jacobi identity sum r^k / p'(r) = 0 for k <= n - 2. That is + /// what the integral's convergence at +oo asks of a rational integrand, and the sum is then 0 at the + /// bound exactly. A sum not shown so, and any at -oo, where ln(x - r) takes a branch of each root's + /// own, leave the bound with no value. + /// + /// + private static Entity? AtAnInfiniteBoundThroughSumsOverRoots(Entity antiderivative, Variable x, Entity bound, + Entity otherBound, bool continuousInside, bool positive) + { + if (!positive) + return null; + var settled = antiderivative.Replace(node => + node is SumOverSetf sum && sum.ContainsNode(x) && VanishesAtPlusInfinity(sum, x) ? Integer.Zero : node); + if (settled.Nodes.Any(node => node is SumOverSetf sum && sum.ContainsNode(x))) + return null; + return AtTheBound(settled.InnerSimplified, x, bound, otherBound, continuousInside); + } + + /// + /// Whether is sum(c(r) ln(x - r)) over the roots of a square-free polynomial + /// p, with c(r) p'(r) a polynomial in r of degree at most deg p - 2: then it tends + /// to 0 at +oo (see ). + /// + private static bool VanishesAtPlusInfinity(SumOverSetf sum, Variable x) + { + if (sum.Var is not Variable root || AntiderivativeBreaks.RootPolynomial(sum, x) is not { } polynomial) + return false; + var logarithm = sum.Expression.Nodes.FirstOrDefault(node => + node is Logf(var logBase, Minusf(var variable, var subtracted)) && logBase == MathS.e && variable == x && subtracted == root); + if (logarithm is null) + return false; + var standIn = Variable.CreateUnique(sum, "u"); + var linear = sum.Expression.Substitute(logarithm, standIn); + var coefficient = linear.Differentiate(standIn).InnerSimplified; + if (coefficient.ContainsNode(standIn) || coefficient.ContainsNode(x) + || Bare((linear - coefficient * standIn).Simplify()) != Integer.Zero) + return false; + if (MathS.Polynomials.SquareFreePart(polynomial, x) is not { } squareFree + || DegreeIn(squareFree, x) is not { } squareFreeDegree + || DegreeIn(polynomial, x) is not { } degree || squareFreeDegree != degree || degree < 2) + return false; + // A condition the simplification writes on, that 3 r^2 + 1 is not 0, holds at every root of a + // square-free p, where p'(r) is not 0. + var numerator = Bare((coefficient.Substitute(root, x) * polynomial.Differentiate(x)).Simplify()); + if (numerator == Integer.Zero) + return true; + return DegreeIn(numerator, x) is { } numeratorDegree && numeratorDegree <= degree - 2; + + static Entity Bare(Entity expr) + { + while (expr is Providedf(var inner, _)) + expr = inner; + return expr; + } + + static int? DegreeIn(Entity polynomial, Variable x) + { + if (Functions.Algebra.LimitSolvers.ParseAsPolynomial(polynomial, x) is not { } monomials + || monomials.Values.Any(coefficient => coefficient.ContainsNode(x))) + return null; + if (monomials.Count == 0) + return 0; + var highest = monomials.Keys.Max()!; + return highest.IsInteger() && highest.Sign >= 0 && highest.CompareTo(PeterO.Numbers.EDecimal.FromInt32(1000)) < 0 + ? highest.ToInt32Checked() : null; + } + } + /// /// The antiderivative's limit at an infinite bound, where substituting the bound left an infinity inside /// the value, or to keep the substituted value as it was. diff --git a/Sources/Tests/UnitTests/Calculus/ASumOverRootsAtInfinityIsZeroTest.cs b/Sources/Tests/UnitTests/Calculus/ASumOverRootsAtInfinityIsZeroTest.cs new file mode 100644 index 000000000..6c4444048 --- /dev/null +++ b/Sources/Tests/UnitTests/Calculus/ASumOverRootsAtInfinityIsZeroTest.cs @@ -0,0 +1,61 @@ +// +// 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.Linq; +using AngouriMath.Extensions; +using Xunit; +using static AngouriMath.Entity; + +namespace AngouriMath.Tests.Calculus +{ + /// + /// The antiderivative of 1/(x^3 + x + 1) is a sum over the roots of r^3 + r + 1, + /// sum(ln(x - r) / (3 r^2 + 1), r). At +oo it was the same sum with +oo in it, which + /// evaluates to NaN, so the integral over [0, +oo) simplified to NaN, a claim that the + /// integral does not exist, where it is 0.92176.... Across a real root the root sum was taken as if it were + /// continuous, and the integral of the same function over [-1, 0], which diverges at the root + /// -0.6823..., was a complex number. + /// https://github.com/asc-community/AngouriMath/issues/2624 + /// + [Trait("Area", "Calculus")] + public sealed class ASumOverRootsAtInfinityIsZeroTest + { + /// Values from mpmath's quadrature. + [Theory] + [InlineData("1/(x^3 + x + 1)", "0", "0.921763372185058")] + [InlineData("1/(x^3 - x^2 + 1)", "0", "1.79044536812823")] + [InlineData("1/(x^5 + x + 1)", "0", "0.795904895024779")] + [InlineData("1/(x^5 + x + 1)", "1", "0.142012887209207")] + [InlineData("x/(x^3 + x + 1)", "0", "0.96934396330681")] + [InlineData("(x + 2)/(x^3 + x + 1)", "0", "2.81287070767692")] + [InlineData("1/(x^4 + x + 1)", "0", "0.835877591843161")] + [InlineData("1/(x^5 + 3x + 5)", "0", "0.229150744145838")] + public void TheIntegralUpToInfinityIsItsValue(string integrand, string from, string expected) + { + var integral = integrand.ToEntity().Integrate("x", from.ToEntity(), "+oo".ToEntity()).Simplify(); + Assert.DoesNotContain(integral.Nodes, node => node is Integralf); + var value = integral.EvalNumerical(); + Assert.True((value - expected.ToEntity().EvalNumerical()).Abs() < 1e-12, + $"{integrand} over [{from}, +oo) was {integral}, {value}, not {expected}"); + } + + /// Never NaN, where the integral diverges or is not settled. + [Theory] + [InlineData("x^2/(x^3 + x + 1)", "0", "+oo")] + [InlineData("1/(x^3 + x + 1)", "-1", "0")] + [InlineData("1/(x^3 + x + 1)", "-oo", "-1")] + [InlineData("1/(x^3 + x + 1)", "-oo", "+oo")] + public void NoValueIsNotNaN(string integrand, string from, string to) + { + var integral = integrand.ToEntity().Integrate("x", from.ToEntity(), to.ToEntity()).Simplify(); + Assert.False(integral.Evaled.IsNaN, $"{integrand} over [{from}, {to}] was NaN"); + // Across the real root -0.6823... the integral diverges, so it has no finite value. + if (from == "-1") + Assert.Contains(integral.Nodes, node => node is Integralf); + } + } +}