diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index c846cb294..325aca1bf 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -107,6 +107,19 @@ filed. | `"floor(log(x - 1, x))".ToEntity().Limit("x", 1, ApproachFrom.Right)` | `limitright(floor(log(x - 1, x)), x, 1)` | `-1` | | `"floor(x / log(x, x))".ToEntity().Limit("x", 0, ApproachFrom.Left)` | `limitleft(floor(x / log(x, x)), x, 0)` | `-1` | +### A grown derivatives' quotient is read by its terms in a limit + +**Silent: where l'Hopital's rule turns away a derivatives' quotient for growing too much, a one-sided limit at a finite +point now reads it a term at a time** -- each term a value at the point, or a negative power of the distance to it +times factors with non-zero values there, a pole whose sign the side decides. Poles of one sign are that infinity; +anything else is declined as before. `(sqrt(x) - erf(x)) / x` at 0 from the right, which 2.6.0 answered `+oo`, had +been left unevaluated on the way to this release, and is `+oo` again; one limit 2.6.0 left unevaluated is answered. +Found by limitcheck; no issue was filed. + +| Input | Was (2.6.0) | Now | +|---|---|---| +| `"(sqrt(x) - erf(x)) / (2 * x)".ToEntity().Limit("x", 0, ApproachFrom.Right)` | `limitright((sqrt(x) - erf(x)) / (2 * x), x, 0)` | `+oo` | + ### A power of a power of a positive base is one power in a limit **Silent: in a limit, `(g^a)^b` with `a` and `b` real constants, `b` not an integer, is read as `g^(a b)` where `g` is diff --git a/Sources/AngouriMath/Functions/Continuous/Limits/Transformations.cs b/Sources/AngouriMath/Functions/Continuous/Limits/Transformations.cs index bd871eafb..c58552f74 100644 --- a/Sources/AngouriMath/Functions/Continuous/Limits/Transformations.cs +++ b/Sources/AngouriMath/Functions/Continuous/Limits/Transformations.cs @@ -1450,6 +1450,104 @@ private static bool AlreadyBeingDifferentiated(Entity quotient) return there.Evaled is Real { IsFinite: true, IsNaN: false } ? there.InnerSimplified : null; } + /// + /// The limit of a derivatives' quotient that grew past , read term by term at a finite + /// destination without another step of the rule: each term has a finite value at the point, or is a product of + /// factors with finite non-zero values there and a negative power of the variable's distance to it, a pole whose + /// sign the side decides. Poles of one sign are that infinity; poles of both signs, or anything else, are + /// . + /// + /// + /// (sqrt(x) - erf(x)) / x at 0 from the right steps to 1/2 * x^(-1/2) - 2 / sqrt(pi) * e^(-x^2), which is +oo + /// there and has no value at the point, so it was left unevaluated where 2.6.0 answered +oo, and so was + /// floor(sqrt(x) - erf(x)), whose sign is read from that slope. Asking the grown quotient's limit with the rule + /// kept out of it answers the same, but the descent it runs took seconds on the quotients that grow the most, + /// and thirty on one of them. Reading the terms costs a substitution each. + /// + private static Entity? ItsValueOrPoleAt(Entity applied, Variable x, Entity dest, ApproachFrom side) + { + if (!dest.IsFinite || side is ApproachFrom.BothSides + || applied.Nodes.Any(static node => node is Floorf or Ceilf or Roundf or Signumf or Piecewise)) + return null; + var distance = dest is Integer { IsZero: true } ? (Entity)x : x - dest; + Entity finite = Integer.Zero; + var poles = 0; + foreach (var (term, negated) in Terms(applied, false)) + { + Entity value = Integer.One; + Entity order = Integer.Zero; + if (!Factors(term, Integer.One, ref value, ref order)) + return null; + if (order.Evaled is not Real { IsNaN: false, IsFinite: true } orderValue) + return null; + if (value.Evaled is not Real { IsNaN: false, IsFinite: true, IsZero: false } coefficient) + return null; + if (negated) + coefficient = -coefficient; + if (orderValue.IsPositive) + continue; + if (orderValue.IsZero) + { + finite += negated ? -value : value; + continue; + } + // (x - a)^k for k < 0 is positive to the right of a, and to the left (-1)^k where k is an integer and + // not a real number at all where it is not. + var signOfPower = side is ApproachFrom.Right ? 1 + : orderValue is Integer { EInteger: var k } ? (k.IsEven ? 1 : -1) + : 0; + if (signOfPower == 0) + return null; + var sign = signOfPower * (coefficient.IsPositive ? 1 : -1); + if (poles != 0 && poles != sign) + return null; + poles = sign; + } + if (poles != 0) + return poles > 0 ? Real.PositiveInfinity : Real.NegativeInfinity; + return finite.Evaled is Real { IsNaN: false, IsFinite: true } ? finite.InnerSimplified : null; + + // A sum over a constant, or a constant times one, is read a term at a time too. + IEnumerable<(Entity Term, bool Negated)> Terms(Entity expr, bool negated) + => expr switch + { + Sumf(var left, var right) => Terms(left, negated).Concat(Terms(right, negated)), + Minusf(var left, var right) => Terms(left, negated).Concat(Terms(right, !negated)), + Divf(Sumf or Minusf, var divisor) when !divisor.ContainsNode(x) + => Terms(((Divf)expr).Dividend, negated).Select(term => (term.Term / divisor, term.Negated)), + Mulf(var factor, Sumf or Minusf) when !factor.ContainsNode(x) + => Terms(((Mulf)expr).Multiplicand, negated).Select(term => (factor * term.Term, term.Negated)), + _ => new[] { (expr, negated) } + }; + + // The factors of a product or a quotient, with the powers of the distance to the point added up in order + // and the rest multiplied into value at the point; false where a factor is neither. + bool Factors(Entity expr, Entity power, ref Entity value, ref Entity order) + { + switch (expr) + { + case Mulf(var left, var right): + return Factors(left, power, ref value, ref order) && Factors(right, power, ref value, ref order); + case Divf(var left, var right): + return Factors(left, power, ref value, ref order) && Factors(right, -power, ref value, ref order); + case Powf(var @base, var exponent) when !exponent.ContainsNode(x) && @base == distance: + order += power * exponent; + return true; + case var _ when expr == distance: + order += power; + return true; + case var _ when !expr.ContainsNode(x): + value *= MathS.Pow(expr, power); + return true; + default: + if (ItsValueAt(expr, x, dest) is not { } there || there.Evaled is not Real { IsZero: false }) + return false; + value *= MathS.Pow(there, power); + return true; + } + } + } + /// /// 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 @@ -1946,6 +2044,7 @@ bool VanishesIdentically(Entity polynomial) if (GrewTooMuch(dest.IsFinite ? expr : numRest / denRest, applied)) return ItsValueAt(applied, x, dest) is { } value ? Times(taken, value) : 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; lHopitalApplications++; MultithreadingFunctional.ExitIfCancelled(); diff --git a/Sources/Tests/UnitTests/Calculus/AGrownQuotientIsReadWithoutAnotherStepTest.cs b/Sources/Tests/UnitTests/Calculus/AGrownQuotientIsReadWithoutAnotherStepTest.cs new file mode 100644 index 000000000..2c6e28b6d --- /dev/null +++ b/Sources/Tests/UnitTests/Calculus/AGrownQuotientIsReadWithoutAnotherStepTest.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 +{ + /// + /// l'Hopital's rule bounds how much its derivatives' quotient may grow, and one past the bound was taken only where + /// it had a value at the point. (sqrt(x) - erf(x)) / x at 0 from the right steps to + /// 1/2 * x^(-1/2) - 2 / sqrt(pi) * e^(-x^2), which is +oo there, so it was left unevaluated where 2.6.0 answered it. + /// Such a quotient is now also read term by term, each a value at the point or a pole whose sign the side decides. + /// Found by limitcheck; no issue was filed. + /// + [Trait("Area", "Calculus")] + public sealed class AGrownQuotientIsReadWithoutAnotherStepTest + { + [Theory] + [InlineData("(sqrt(x) - erf(x)) / x", "+oo")] + [InlineData("(sqrt(x) - erf(x)) / (2 * x)", "+oo")] + [InlineData("(sqrt(x) + erf(x)) / x", "+oo")] + [InlineData("(erf(x) - sqrt(x)) / x", "-oo")] + [InlineData("(x^(1/3) - erf(x)) / x", "+oo")] + [InlineData("floor(sqrt(x) - erf(x))", "0")] + [InlineData("(1 - cos(x)) / x^3", "+oo")] + public void AtZeroFromTheRight(string input, string expected) + => Assert.Equal(expected.ToEntity(), input.ToEntity().Limit("x", 0, ApproachFrom.Right)); + + // Left of 0, sqrt(x) is imaginary and the quotient has no real limit to give. + [Fact] + public void FromTheLeftNothingChanges() + => Assert.IsType("(sqrt(x) - erf(x)) / x".ToEntity().Limit("x", 0, ApproachFrom.Left)); + } +}