From 572bceaa1401df1bfd4511539f113726af805f13 Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Sun, 29 Nov 2026 04:26:11 +0000 Subject: [PATCH] Take a limit over a constant as its dividend's limit over the constant (#2648) The descent asked a quotient by a nonzero finite constant whole, through the solvers that rewrite it, before it asked the dividend. 4 arctan((6 tan(x/2) + 2)/(4 sqrt(2))) / (4 sqrt(2)) at pi from the left took seventeen seconds, where the dividend's limit takes a fraction of one. The integral of 1/(3 + sin(x)) over a range across pi, which asks that limit at the pole of tan(x/2), did not finish. Over a constant that is finite and not 0, the limit is now the dividend's limit over the constant, taken first. The dividend is asked as a whole limit, which is a smaller question, so a constant factor inside it is taken out quickly too. A dividend whose limit is undecided or undefined goes the long way, as before. Co-Authored-By: Claude Opus 5.5 --- BREAKING-CHANGES.md | 15 +++++ .../Limits/Solvers/Limit.Classes.cs | 11 ++++ ...mitOverAConstantIsTheDividendsLimitTest.cs | 60 +++++++++++++++++++ 3 files changed, 86 insertions(+) create mode 100644 Sources/Tests/UnitTests/Calculus/ALimitOverAConstantIsTheDividendsLimitTest.cs diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index d5b2b35ed..8fe17aa63 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -225,6 +225,21 @@ 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 limit of a quotient by a constant is its dividend's limit over the constant + +**Not silent in time: answers that did not arrive now do.** The descent asked a quotient by a nonzero finite +constant whole, through the solvers that rewrite it, before asking its dividend. So +`4 arctan((6 tan(x/2) + 2)/(4 sqrt(2))) / (4 sqrt(2))` at `pi` from the left took seventeen seconds. The integral of +`1/(3 + sin(x))` over a range across `pi`, which asks that limit at the pole of `tan(x/2)`, did not finish. The +dividend's limit over the constant is now taken first. The values are unchanged where they arrived +([#2648](https://github.com/asc-community/AngouriMath/issues/2648)). + +| simplified | Was (2.10.0), within 20 s | Now | +|---|---|---| +| `integral(1/(3+sin(x)), x, 0, 2 pi)` | did not finish | `sqrt(2) * pi / 2`, 0.8 s | +| `integral(1/(3+sin(x)), x, 0, 5)` | did not finish | closed form = `1.68876…`, 0.8 s | +| `limit(4 arctan((6 tan(x/2) + 2)/(4 sqrt(2)))/(4 sqrt(2)), x, pi-)` | did not finish | `4 * pi / 2 / (4 * sqrt(2))`, 0.4 s | + ### 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 diff --git a/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Limit.Classes.cs b/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Limit.Classes.cs index 135097319..e5b6014ad 100644 --- a/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Limit.Classes.cs +++ b/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Limit.Classes.cs @@ -205,6 +205,17 @@ partial record Divf { internal override Entity? ComputeLimitDivideEtImpera(Variable x, Entity dist, ApproachFrom side) { + // Over a finite constant other than 0, the limit of the dividend over it. Asked as a whole quotient first, + // the solvers rewrite it before the parts are read: arctan((6 tan(x/2) + 2)/(4 sqrt(2))) / (4 sqrt(2)) at + // pi from the left took fourteen seconds where its dividend's limit takes half of one, and the integral of + // 1/(3 + sin(x)) over [0, 2 pi], which asks it at the pole of tan(x/2), did not finish. A dividend whose + // limit is undecided or undefined goes the long way as before. The dividend's limit is asked whole, a + // smaller question than this one, so that a constant factor in it is taken out the same way. + // https://github.com/asc-community/AngouriMath/issues/2648 + if (!Divisor.ContainsNode(x) && Divisor.Evaled is Number.Complex { IsZero: false } constant && Number.PartsAreFinite(constant) + && ComputeLimit(Dividend, x, dist, side) is { } overTheConstant + && !overTheConstant.ContainsNode(x) && overTheConstant.Evaled is Number.Complex { IsNaN: false }) + return ComputeLimitImpl(New(overTheConstant, Divisor), x, dist, side); // Over a quotient, as a quotient of products: a / (b / c) is a c / b wherever it has a value. Read as written, // x^(-pi) / (ln(2) / ln(x)) at +oo is 0/0, and l'Hopital's rule makes each step of it another 0 * oo with a // higher power of ln(x), so it was left unevaluated where x^(-pi) ln(x) / ln(2) is 0 at once. Only a value is diff --git a/Sources/Tests/UnitTests/Calculus/ALimitOverAConstantIsTheDividendsLimitTest.cs b/Sources/Tests/UnitTests/Calculus/ALimitOverAConstantIsTheDividendsLimitTest.cs new file mode 100644 index 000000000..a7f84a84c --- /dev/null +++ b/Sources/Tests/UnitTests/Calculus/ALimitOverAConstantIsTheDividendsLimitTest.cs @@ -0,0 +1,60 @@ +// +// 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; +using System.Threading.Tasks; +using AngouriMath.Core; +using AngouriMath.Extensions; +using Xunit; +using static AngouriMath.Entity; + +namespace AngouriMath.Tests.Calculus +{ + /// + /// A quotient by a nonzero constant was asked whole before its dividend, through the solvers that rewrite it, so + /// the limit of 4 arctan((6 tan(x/2) + 2)/(4 sqrt(2))) / (4 sqrt(2)) at pi took seventeen seconds, + /// and the integral of 1/(3 + sin(x)) over a range across pi, which asks it at the pole of + /// tan(x/2), did not finish. https://github.com/asc-community/AngouriMath/issues/2648 + /// + [Trait("Area", "Calculus")] + public sealed class ALimitOverAConstantIsTheDividendsLimitTest + { + private static Entity Within(Func compute, string what) + { + var task = Task.Run(compute); + Assert.True(task.Wait(TimeSpan.FromSeconds(60)), $"{what} did not finish"); + return task.Result; + } + + /// Values from mpmath's quadrature at 30 digits. + [Theory] + [InlineData("1/(3 + sin(x))", "0", "2 pi", "2.22144146907918312350794049503")] + [InlineData("1/(3 + sin(x))", "0", "5", "1.68876091503969438318989039524")] + public void TheIntegralAcrossThePoleIsAnswered(string integrand, string from, string to, string expected) + { + var integral = Within(() => integrand.ToEntity().Integrate("x", from.ToEntity(), to.ToEntity()).Simplify(), integrand); + Assert.DoesNotContain(integral.Nodes, node => node is Integralf); + Assert.True((integral - expected.ToEntity()).EvalNumerical().Abs() < 1e-12, + $"{integrand} over [{from}, {to}] was {integral}, not {expected}"); + } + + /// + /// Within a budget of 200 limits asked along the way, which the dividend's limit needs a few dozen of; the + /// quotient asked whole spent thousands, which is where the seconds went. + /// + [Theory] + [InlineData("arctan((6 tan(x / 2) + 2) / (4 sqrt(2))) / (4 sqrt(2))", "pi / (8 sqrt(2))")] + [InlineData("4 arctan((6 tan(x / 2) + 2) / (4 sqrt(2))) / (4 sqrt(2))", "pi / (2 sqrt(2))")] + public void TheLimitFromTheLeftIsAnswered(string expression, string expected) + { + var limit = Within(() => Functions.Algebra.LimitFunctional.WithinABudget(200, + () => expression.ToEntity().Limit("x", "pi", ApproachFrom.Left)), expression); + Assert.DoesNotContain(limit.Nodes, node => node is Limitf); + Assert.True((limit - expected.ToEntity()).EvalNumerical().Abs() < 1e-12, $"{expression} at pi- was {limit}"); + } + } +}