diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index d50fcf3b7..d12bfa9ac 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -169,6 +169,22 @@ sum leaves the integral unevaluated ([#2624](https://github.com/asc-community/An | `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 | +### A sum over roots cancels with a logarithm beside it at +oo + +**An answer where there was none.** A sum over roots `sum(c(r) ln(x - r))` grows like `(sum c(r)) ln(x)` at `+oo`. It +was taken to its limit only where `sum c(r)` is 0 alone. The antiderivative of `(x + 2)/(x^4 - 2x^3 - 2x + 3)` is +`-3/4 ln(x - 1)` beside a sum with `sum c(r) = 3/4`. The two cancel together, and the integral over `[3, +oo)` was left +unevaluated. `sum c(r)` is now read exactly by the Euler–Jacobi identity, as the coefficient of `r^(n - 1)` in +`c(r) p'(r)` over `p`'s leading coefficient, and the sum stands as that multiple of `ln(x)` beside the other +logarithms. Where nothing cancels, the integral diverges and is now `+oo`, where it was left unevaluated +([#2636](https://github.com/asc-community/AngouriMath/issues/2636)). + +| simplified | Was (2.10.0) | Now | +|---|---|---| +| `integral((x+2)/(x^4-2x^3-2x+3), x, 3, +oo)` | unevaluated | `3/4 ln(2) - sum(…)` = `0.16929…` | +| `integral((2x+1)/(x^4+2x^3+x^2-x-1), x, 1, +oo)` | unevaluated | a sum over roots = `0.64121…` | +| `integral(x^2/(x^3+x+1), x, 0, +oo)` | `NaN` | `+oo` | + ### 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/Integration.Definition.cs b/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs index cfa71cd04..8301798e7 100644 --- a/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs +++ b/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs @@ -382,42 +382,65 @@ private static bool HoldsAnIncompleteGammaFunction(Entity expr) { if (!positive) return null; + // Each sum replaced by sigma ln(x), which differs from it by what tends to 0, so that its growth meets the + // other logarithms' and the gathering cancels them together: -3/4 ln(x - 1) beside a sum with sigma = 3/4, + // in the antiderivative of (x + 2)/(x^4 - 2x^3 - 2x + 3), left the integral over [3, +oo) unevaluated where + // only a sum with sigma = 0 was taken. https://github.com/asc-community/AngouriMath/issues/2636 var settled = antiderivative.Replace(node => - node is SumOverSetf sum && sum.ContainsNode(x) && VanishesAtPlusInfinity(sum, x) ? Integer.Zero : node); + node is SumOverSetf sum && sum.ContainsNode(x) && GrowthAtPlusInfinity(sum, x) is { } sigma + ? sigma == Integer.Zero ? Integer.Zero : sigma * MathS.Ln(x) + : 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 ). + /// The multiple sigma = sum c(r) of ln(x) that , sum(c(r) ln(x - r)) over + /// the roots of a square-free polynomial p of degree n, grows like at +oo, where + /// c(r) p'(r) is a polynomial N(r) of degree at most n - 1; or . /// - private static bool VanishesAtPlusInfinity(SumOverSetf sum, Variable x) + /// + /// By the Euler–Jacobi identity sum r^k / p'(r) is 0 for k <= n - 2 and 1 / lead(p) for + /// k = n - 1, so sigma is the coefficient of r^(n - 1) in N over p's leading + /// coefficient, exactly (see ). + /// + private static Entity? GrowthAtPlusInfinity(SumOverSetf sum, Variable x) { if (sum.Var is not Variable root || AntiderivativeBreaks.RootPolynomial(sum, x) is not { } polynomial) - return false; + return null; 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; + return null; 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; + return null; 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; + return null; // 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; + return Integer.Zero; + if (DegreeIn(numerator, x) is not { } numeratorDegree || numeratorDegree > degree - 1) + return null; + if (numeratorDegree <= degree - 2) + return Integer.Zero; + return CoefficientOf(polynomial, degree) is { } leading && CoefficientOf(numerator, degree - 1) is { } top + ? (top / leading).InnerSimplified + : null; + + Entity? CoefficientOf(Entity polynomial, int power) + => Functions.Algebra.LimitSolvers.ParseAsPolynomial(polynomial, x) is { } monomials + && monomials.FirstOrDefault(monomial => monomial.Key.CompareTo(PeterO.Numbers.EDecimal.FromInt32(power)) == 0) is { Value: { } coefficient } + ? coefficient + : null; static Entity Bare(Entity expr) { diff --git a/Sources/Tests/UnitTests/Calculus/ASumOverRootsCancelsWithALogarithmBesideItTest.cs b/Sources/Tests/UnitTests/Calculus/ASumOverRootsCancelsWithALogarithmBesideItTest.cs new file mode 100644 index 000000000..c155dbe48 --- /dev/null +++ b/Sources/Tests/UnitTests/Calculus/ASumOverRootsCancelsWithALogarithmBesideItTest.cs @@ -0,0 +1,43 @@ +// +// 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.Extensions; +using Xunit; +using static AngouriMath.Entity; + +namespace AngouriMath.Tests.Calculus +{ + /// + /// The antiderivative of (x + 2)/(x^4 - 2x^3 - 2x + 3) is -3/4 ln(x - 1) plus a sum over the roots of a + /// cubic that grows like 3/4 ln(x) at +oo. The two cancel only together, and a sum over roots was + /// taken at +oo only when it vanished alone, so the integral over [3, +oo) was left unevaluated. + /// https://github.com/asc-community/AngouriMath/issues/2636 + /// + [Trait("Area", "Calculus")] + public sealed class ASumOverRootsCancelsWithALogarithmBesideItTest + { + /// Values from mpmath's quadrature at 30 digits. + [Theory] + [InlineData("(x + 2)/(x^4 - 2x^3 - 2x + 3)", "3", "0.169292631535099354480362331081")] + [InlineData("(2x + 1)/(x^4 + 2x^3 + x^2 - x - 1)", "1", "0.641214919954003672766224690615")] + [InlineData("(2x^2 + 2x - 2)/(x^4 + 2x^3 - 3x^2 - x - 3)", "2", "0.993381234988709602364110923287")] + [InlineData("(x^2 + x - 1)/(x^5 - 2x^4 + x^3 - 2x^2 + x + 1)", "2", "0.558021773663943264758932892579")] + 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 difference = (integral - expected.ToEntity()).EvalNumerical(); + Assert.True(difference.Abs() < 1e-12, $"{integrand} over [{from}, +oo) was {integral}, not {expected}"); + } + + /// Where the growths do not cancel the integral diverges, and is +oo. + [Fact] + public void ADivergentOneIsInfinite() + => Assert.Equal(Number.Real.PositiveInfinity, + "(x^3 + 2)/(x^4 - 2x^3 - 2x + 3)".ToEntity().Integrate("x", 3, "+oo".ToEntity()).Simplify().Evaled); + } +}