diff --git a/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Solvers.Definition.cs b/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Solvers.Definition.cs index 6bed93880..4ec47e127 100644 --- a/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Solvers.Definition.cs +++ b/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Solvers.Definition.cs @@ -534,7 +534,7 @@ node is Derivativef(var expression, Variable variable, var iterations) && iterat // it, where the same expression approached from both sides gives e. expr = expr.Replace(a => TrivialTrigonometricReplacement(a, x)); expr = ApplyTrivialTransformations(expr, x, dest, side, (_, exprLim) => exprLim); - expr = ApplyFirstRemarkableOverFactors(expr, x, dest); + expr = ApplyFirstRemarkableOverFactors(expr, x, dest, side); expr = expr.Replace(c => ApplySecondRemarkable(c, x, dest)); // A tangent is a quotient as much as a cosecant is a reciprocal, and leaving it // written as one function makes it opaque to everything that reads quotients -- diff --git a/Sources/AngouriMath/Functions/Continuous/Limits/Transformations.cs b/Sources/AngouriMath/Functions/Continuous/Limits/Transformations.cs index 193f26807..771660b7c 100644 --- a/Sources/AngouriMath/Functions/Continuous/Limits/Transformations.cs +++ b/Sources/AngouriMath/Functions/Continuous/Limits/Transformations.cs @@ -28,9 +28,9 @@ partial class LimitFunctional /// to pi - x -- so rewriting it as x turned lim x->pi sin(x) / (x - pi), which /// is -1, into pi / 0. /// - private static Entity EquivalenceRules(Entity expr, Variable x, Entity dest) + private static Entity EquivalenceRules(Entity expr, Variable x, Entity dest, ApproachFrom side) => expr is (Sinf or Tanf or Arcsinf or Arctanf) - && EvalAssumingContinuous(expr.DirectChildren[0].Limit(x, dest)) == 0 + && EvalAssumingContinuous(expr.DirectChildren[0].Limit(x, dest, side)) == 0 ? expr.DirectChildren[0] : expr; internal static Entity EvalAssumingContinuous(Entity expr) => @@ -118,12 +118,21 @@ internal static bool RealCodomainWithdraws(Entity expr, Variable x, Entity dest, => MathS.Settings.Codomain.Value is AngouriMath.Core.Domain.Real && LeavesTheRealsOnTheApproach(expr, x, dest, side); - private static Entity ApplyFirstRemarkable(Entity expr, Variable x, Entity dest) + /// + /// Each part is asked from the side of the limit. The equivalence of a sine and its argument holds on one side + /// of the point as much as on both, so a one-sided limit needs its parts to vanish on its side alone; asked + /// from both, (e^((x - 1)^ln(x)) - e^(-(x - 1)^ln(x))) / 2 at 1 from the right spent eight seconds on the left + /// of its dividend, where (x - 1)^ln(x) is not real. The simpler part is asked first, since a part that does + /// not vanish settles the question and a simple one is the cheaper to ask: the divisor of that quotient, a + /// constant, but the dividend of (-1 - x) / (ln(2) / ln(x))^(x - 1) at 1 from the left, whose divisor takes + /// seconds and is asked again by the descent. + /// + private static Entity ApplyFirstRemarkable(Entity expr, Variable x, Entity dest, ApproachFrom side) => expr switch { Divf(var a, var b) div - when EvalAssumingContinuous(a.Limit(x, dest)) == 0 && EvalAssumingContinuous(b.Limit(x, dest)) == 0 - => div.New(EquivalenceRules(a, x, dest), EquivalenceRules(b, x, dest)), + when BothVanish(a.Complexity <= b.Complexity ? (a, b) : (b, a), x, dest, side) + => div.New(EquivalenceRules(a, x, dest, side), EquivalenceRules(b, x, dest, side)), // A product takes the substitution as much as a quotient does -- it is the ratio // of the two forms tending to 1 that licenses it, and that says nothing about @@ -131,14 +140,18 @@ when EvalAssumingContinuous(a.Limit(x, dest)) == 0 && EvalAssumingContinuous(b.L // had it, so lim x->0+ tan(x) * ln(x) went the long way round through l'Hopital's // rule and came back NaN, where the same limit written sin(x) * ln(x) is 0. Mulf(var a, var b) - when EquivalenceRules(a, x, dest) is var equivalentA - && EquivalenceRules(b, x, dest) is var equivalentB + when EquivalenceRules(a, x, dest, side) is var equivalentA + && EquivalenceRules(b, x, dest, side) is var equivalentB && (!ReferenceEquals(equivalentA, a) || !ReferenceEquals(equivalentB, b)) => equivalentA * equivalentB, _ => expr }; + private static bool BothVanish((Entity First, Entity Second) parts, Variable x, Entity dest, ApproachFrom side) + => EvalAssumingContinuous(parts.First.Limit(x, dest, side)) == 0 + && EvalAssumingContinuous(parts.Second.Limit(x, dest, side)) == 0; + /// /// applied down the product-and-quotient spine /// rather than at the root alone. @@ -161,13 +174,13 @@ when EquivalenceRules(a, x, dest) is var equivalentA /// factor of the whole. /// #749 /// - private static Entity ApplyFirstRemarkableOverFactors(Entity expr, Variable x, Entity dest) + private static Entity ApplyFirstRemarkableOverFactors(Entity expr, Variable x, Entity dest, ApproachFrom side) { - var applied = ApplyFirstRemarkable(expr, x, dest); + var applied = ApplyFirstRemarkable(expr, x, dest, side); if (applied is not (Mulf or Divf)) return applied; - var left = ApplyFirstRemarkableOverFactors(applied.DirectChildren[0], x, dest); - var right = ApplyFirstRemarkableOverFactors(applied.DirectChildren[1], x, dest); + var left = ApplyFirstRemarkableOverFactors(applied.DirectChildren[0], x, dest, side); + var right = ApplyFirstRemarkableOverFactors(applied.DirectChildren[1], x, dest, side); // Rebuilt only where a factor actually changed, so an expression the rule has // nothing to say about comes back as the very node it went in as, keeping whatever // the rest of the machinery has already cached against it. diff --git a/Sources/Tests/UnitTests/Calculus/ARemarkableLimitIsAskedFromItsSideTest.cs b/Sources/Tests/UnitTests/Calculus/ARemarkableLimitIsAskedFromItsSideTest.cs new file mode 100644 index 000000000..6bb50f29e --- /dev/null +++ b/Sources/Tests/UnitTests/Calculus/ARemarkableLimitIsAskedFromItsSideTest.cs @@ -0,0 +1,47 @@ +// +// 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; + +namespace AngouriMath.Tests.Calculus +{ + /// + /// The first remarkable limit asked whether a quotient's parts vanish from both sides of the point, also inside a + /// one-sided limit, and asked the dividend first. sinh((x - 1)^ln(x)) at 1 from the right is a quotient over 2, and + /// its dividend was taken through the left side, where (x - 1)^ln(x) is not real, for eight seconds. Found by + /// limitcheck; no issue was filed. + /// + [Trait("Area", "Calculus")] + public sealed class ARemarkableLimitIsAskedFromItsSideTest + { + [Theory] + [InlineData("sinh((x - 1)^ln(x))", 1, ApproachFrom.Right, "sinh(1)")] + [InlineData("cosh((x - 1)^ln(x))", 1, ApproachFrom.Right, "cosh(1)")] + public void AFunctionOfAPowerFinishes(string input, int at, ApproachFrom side, string expected) + { + Entity? limit = null; + var task = Task.Factory.StartNew(() => limit = input.ToEntity().Limit("x", at, side), TaskCreationOptions.LongRunning); + Assert.True(task.Wait(TimeSpan.FromSeconds(30)), "the limit did not finish in 30 seconds"); + Assert.NotNull(limit); + Assert.Equal(expected.ToEntity().EvalNumerical(), limit!.EvalNumerical()); + } + + // The remarkable limits themselves, from either side and both. + [Theory] + [InlineData("sin(x) / x", ApproachFrom.BothSides, "1")] + [InlineData("sin(x) / x", ApproachFrom.Left, "1")] + [InlineData("tan(2 * x) / sin(x)", ApproachFrom.Right, "2")] + [InlineData("tan(x) * ln(x)", ApproachFrom.Right, "0")] + [InlineData("(sin(x) / x - 1) / x^2", ApproachFrom.BothSides, "-1/6")] + public void TheRemarkableLimitStillHolds(string input, ApproachFrom side, string expected) + => Assert.Equal(expected.ToEntity(), input.ToEntity().Limit("x", 0, side)); + } +}