From 5eab0e0816581e71b1b02ff655250bfae5d79924 Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Tue, 10 Nov 2026 04:27:27 +0000 Subject: [PATCH] Read a vanishing factor by its order in l'Hopital's quotient at a point At a finite point the rule reads the quotient it gets term by term, as a value or a pole, but a factor that was a sum or vanished there stopped the reading. ln(cos(x) * sqrt(x)) / (1 / (e^(2x) - 1)) steps to a quotient with x^(-1/2) * cos(x) / 2 - sqrt(x) * sin(x) and (e^(2x) - 1)^2 in it, of orders -1/2 and 2, and was left unevaluated. Such a factor is now read as its leading term: a sum by its terms of the lowest order, a function that vanishes by its first derivative that does not. The reading is also asked before another step, not only once the step has grown: tanh(x) * ln(cos(x) * sqrt(x)) at 0 from the right reached the rule with e^(2x) + 1 taken out, so the step did not count as grown, and the descent through the quotient ran out of time. Co-Authored-By: Claude Opus 5.5 --- .../Continuous/Limits/Transformations.cs | 79 ++++++++++++++++++- .../AVanishingFactorIsReadByItsOrderTest.cs | 40 ++++++++++ 2 files changed, 117 insertions(+), 2 deletions(-) create mode 100644 Sources/Tests/UnitTests/Calculus/AVanishingFactorIsReadByItsOrderTest.cs diff --git a/Sources/AngouriMath/Functions/Continuous/Limits/Transformations.cs b/Sources/AngouriMath/Functions/Continuous/Limits/Transformations.cs index c3077ede0..def31ec01 100644 --- a/Sources/AngouriMath/Functions/Continuous/Limits/Transformations.cs +++ b/Sources/AngouriMath/Functions/Continuous/Limits/Transformations.cs @@ -1557,15 +1557,82 @@ bool Factors(Entity expr, Entity power, ref Entity value, ref Entity order) case var _ when !expr.ContainsNode(x): value *= MathS.Pow(expr, power); return true; + case var _ when ItsValueAt(expr, x, dest) is { } there && there.Evaled is Real { IsZero: false }: + value *= MathS.Pow(there, power); + return true; + // A whole power is its base's order that many times, where c * d^k to the n is c^n * d^(kn) exactly. + case Powf(var @base, Integer times): + return Factors(@base, power * times, ref value, ref order); default: - if (ItsValueAt(expr, x, dest) is not { } there || there.Evaled is not Real { IsZero: false }) + if (LeadingTerm(expr) is not var (leadingOrder, leadingValue)) return false; - value *= MathS.Pow(there, power); + order += power * leadingOrder; + value *= MathS.Pow(leadingValue, power); return true; } } + + // A factor with no finite non-zero value at the point read as c * d^k, d the distance to it: a sum by its + // terms of the lowest order, where their coefficients do not cancel, and one that vanishes there, a sum + // among them, by the first of its derivatives that does not. The product rule leaves both inside one factor: + // ln(cos(x) * sqrt(x)) / (1 / (e^(2x) - 1)) at 0 from the right steps to + // -1/2 * (x^(-1/2) * cos(x) / 2 - sqrt(x) * sin(x)) * (e^(2x) - 1)^2 / (sqrt(x) * cos(x) * e^(2x)), whose + // sum is of order -1/2 and whose e^(2x) - 1 is 2x to the first, so it is of order 1 and tends to 0. A + // derivative read at the point is the Taylor coefficient only where the function is smooth there, so not + // through an abs, and a value that is not a finite real at the point stops the reading. + (Entity Order, Entity Value)? LeadingTerm(Entity expr) + { + if (ItsValueAt(expr, x, dest) is { } atThePoint && atThePoint.Evaled is Real { IsZero: true }) + return ByItsDerivatives(expr); + if (expr is Sumf or Minusf) + { + Real? lowest = null; + var parts = new List<(Real Order, Entity Value)>(); + foreach (var (term, negated) in Terms(expr, false)) + { + Entity termValue = Integer.One, termOrder = Integer.Zero; + if (!Factors(term, Integer.One, ref termValue, ref termOrder) + || termOrder.Evaled is not Real { IsNaN: false, IsFinite: true } termOrderValue) + return null; + parts.Add((termOrderValue, negated ? -termValue : termValue)); + if (lowest is null || termOrderValue < lowest) + lowest = termOrderValue; + } + if (lowest is null) + return null; + Entity coefficient = Integer.Zero; + foreach (var part in parts) + if (part.Order == lowest) + coefficient += part.Value; + return coefficient.Evaled is Real { IsNaN: false, IsFinite: true, IsZero: false } + ? (lowest, coefficient.InnerSimplified) : null; + } + return null; + } + + // The first derivative that is not 0 at the point, over its factorial, and its order. + (Entity Order, Entity Value)? ByItsDerivatives(Entity expr) + { + if (expr.Nodes.Any(static node => node is Absf)) + return null; + var derivative = expr; + Entity factorial = Integer.One; + for (var k = 1; k <= MaxVanishingOrderRead; k++) + { + derivative = derivative.Differentiate(x); + factorial *= k; + if (ItsValueAt(derivative, x, dest) is not { } derivativeThere || derivativeThere.Evaled is not Real { IsNaN: false, IsFinite: true } read) + return null; + if (!read.IsZero) + return (Integer.Create(k), (derivativeThere / factorial).InnerSimplified); + } + return null; + } } + /// How many derivatives takes of a vanishing factor to find its order. + private const int MaxVanishingOrderRead = 3; + /// /// Whether differentiating both parts has left a quotient bigger than the one it came /// from by more than a step on the way to an answer accounts for. Each step asks what @@ -2087,6 +2154,14 @@ bool VanishesIdentically(Entity polynomial) : dest.IsFinite && ItsValueAt(applied.Expand().InnerSimplified, x, dest) is { } expanded ? Times(taken, expanded) : dest.IsFinite && ItsValueOrPoleAt(applied, x, dest, side) is { } read ? Times(taken, read) : null; + // At a finite point the term-by-term reading is asked before another step too: it costs a + // substitution a factor, where the descent through the quotient may cost seconds. + // tanh(x) * ln(cos(x) * sqrt(x)) at 0 from the right reaches here with e^(2x) + 1 taken + // out, so what was differentiated is smaller and the step does not count as grown, and the + // descent through -1/2 * (x^(-1/2) * cos(x) / 2 - sqrt(x) * sin(x)) * (e^(2x) - 1)^2 / ... + // ran out of time where the reading says 0 at once. + if (dest.IsFinite && ItsValueOrPoleAt(applied, x, dest, side) is { } readFirst) + return Times(taken, readFirst); lHopitalApplications++; MultithreadingFunctional.ExitIfCancelled(); lHopitalChain!.Add(applied); diff --git a/Sources/Tests/UnitTests/Calculus/AVanishingFactorIsReadByItsOrderTest.cs b/Sources/Tests/UnitTests/Calculus/AVanishingFactorIsReadByItsOrderTest.cs new file mode 100644 index 000000000..ad015f997 --- /dev/null +++ b/Sources/Tests/UnitTests/Calculus/AVanishingFactorIsReadByItsOrderTest.cs @@ -0,0 +1,40 @@ +// +// 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.Core; +using AngouriMath.Extensions; +using Xunit; + +namespace AngouriMath.Tests.Calculus +{ + /// + /// At a finite point l'Hopital's rule reads the quotient it gets term by term, as a value or a pole, and now reads a + /// factor that is a sum or vanishes there by its leading order: ln(cos(x) * sqrt(x)) / (1 / (e^(2x) - 1)) steps to + /// a quotient with (x^(-1/2) * cos(x) / 2 - sqrt(x) * sin(x)) and (e^(2x) - 1)^2 in it, of orders -1/2 and 2. It also + /// reads it before another step, rather than only once the step has grown. Found by limitcheck; no issue was filed. + /// + [Trait("Area", "Calculus")] + public sealed class AVanishingFactorIsReadByItsOrderTest + { + [Theory] + [InlineData("(e^(2*x) - 1) * ln(cos(x) * sqrt(x))", "0")] + [InlineData("tanh(x) * ln(cos(x) * sqrt(x))", "0")] + [InlineData("sinh(x) * ln(cos(x) * sqrt(x))", "0")] + [InlineData("(cos(x) * sqrt(x)) ^ tanh(x)", "1")] + [InlineData("(cos(x) * sqrt(x)) ^ arctan(tanh(x))", "1")] + public void TheLimitIsAnswered(string input, string expected) + => Assert.Equal(expected.ToEntity().Evaled, input.ToEntity().Limit("x", 0, ApproachFrom.Right).Evaled); + + [Theory] + [InlineData("(sin(x) - x) / x^3", "-1/6")] + [InlineData("1 / x^2 - 1 / sin(x)^2", "-1/3")] + [InlineData("(cosh(x) - 1) / (x * tanh(x))", "1/2")] + [InlineData("tanh(x) * ln(cos(x) * x^2)", "0")] + public void TheLimitStillHolds(string input, string expected) + => Assert.Equal(expected.ToEntity().Evaled, input.ToEntity().Limit("x", 0, ApproachFrom.BothSides).Evaled); + } +}