diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index 06d2305fe..8257994bf 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -123,6 +123,22 @@ four parts it is built from. | `"ln(arctan(x) + x) / ln(x)".ToEntity().Limit("x", "+oo")` | unevaluated | `1` | | `"tanh(x) * x / (x + tanh(x))".ToEntity().Limit("x", "-oo")` | did not finish in 30 s | `-1` | +### An indeterminate power under a function is read through its exponent + +**A limit of a function of an indeterminate power, such as `0^0` or `oo^0`, is answered where the power alone +is.** `x^x` at 0 from the right is 1, and `sin(x^x)` there was NaN or unevaluated, because the rule that reads +a power through its exponent was asked only of a whole expression. It is asked of a power wherever it sits, +one-sided or at an infinity, where the base is positive on the side approached. Two-sided limits with a +complex side, as `x^x` has on the left of 0, are left as they were. + +| Input | Was (2.6.0) | Now | +|---|---|---| +| `"sin(x^x)".ToEntity().Limit("x", 0, ApproachFrom.Right)` | `NaN` | `sin(1)` | +| `"cos(x^x)".ToEntity().Limit("x", 0, ApproachFrom.Right)` | `NaN` | `cos(1)` | +| `"sin((x - 1)^ln(x))".ToEntity().Limit("x", 1, ApproachFrom.Right)` | `NaN` | `sin(1)` | +| `"sin(x^(1/x))".ToEntity().Limit("x", "+oo")` | unevaluated | `sin(1)` | +| `"sqrt(x) / x^pi + x^x".ToEntity().Limit("x", 0, ApproachFrom.Right)` | `NaN` | `+oo` | + ### A vanishing base to a negative whole power tends to an infinity **A real base tending to 0 with a sign, to a negative whole power, is +oo or -oo, where it was NaN.** For an diff --git a/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Limit.Classes.cs b/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Limit.Classes.cs index 35ee63d07..1304075ff 100644 --- a/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Limit.Classes.cs +++ b/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Limit.Classes.cs @@ -405,6 +405,24 @@ partial record Powf && (Exponent.ContainsNode(x) ? Exponent.Simplify() : Exponent).Evaled is Integer { IsNegative: true } wholeExponent && SignOfAVanishingBase(wholeExponent.EInteger.IsEven) is { } sign) return sign > 0 ? Real.PositiveInfinity : Real.NegativeInfinity; + // An indeterminate pair, 0^0 among them, is read through the exponent, by the rule + // the top level asks of a whole expression and asked of the power here, so that a + // power under a function is read too: x^x at 0+ is 1, and sin(x^x) there was + // unevaluated. One-sided, or at an infinity, as the top level asks it, and only where + // the base is positive on the side approached, so that the power is real there: x^x on + // the left of 0 is complex, and the two-sided limit there stays NaN. Where it settles + // nothing, the pair is substituted as before, and 0^0 stays NaN. Not in the descents + // a two-sided limit compares, where it would settle (-x)^x at 0 and leave x^x NaN, and + // not for the whole of the question, which the path above asks once the descent is done. + if (baseLimit is { } pairBase && exponentLimit is { } pairExponent + && (side is not ApproachFrom.BothSides || !dist.IsFinite) + && MayReadAPowerThroughItsExponent(this) + && (EvalAssumingContinuous(pairBase) == 0 && EvalAssumingContinuous(pairExponent) == 0 + || IsIndeterminatePowerForm(pairBase, pairExponent)) + && (side is ApproachFrom.BothSides ? ApproachFrom.Left : side) is var pairSide + && SignNear(Base, x, dist, pairSide) is 1 + && SolveAsIndeterminatePower(this, x, dist, pairSide) is { } byExponent) + return byExponent; var substituted = (baseLimit, exponentLimit) switch { diff --git a/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Solvers.Definition.cs b/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Solvers.Definition.cs index a0f59dfd6..c860fc67d 100644 --- a/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Solvers.Definition.cs +++ b/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Solvers.Definition.cs @@ -275,6 +275,29 @@ bool PositiveOnBothSides(Entity node) [System.ThreadStatic] private static int bothSidesDepth; + /// + /// Whether the descent under way is one of the two a two-sided limit at a finite point + /// compares. A power under it reads 0^0 through its exponent only one-sidedly, and two + /// sides compared that way make (-x)^x at 0 answer 1 where x^x, its mirror, is NaN: the + /// branch each side of the point takes is not this reading's to settle. + /// Cleared for each limit asked inside the descent, which is a question of its own. + /// + [System.ThreadStatic] private static bool comparingSides; + + /// + /// The expression whose one-sided descent is under way. The one-sided path reads it + /// through its exponent itself once the descent has nothing to say, so the descent does + /// not ask the same question of it first. + /// + [System.ThreadStatic] private static Entity? descentRoot; + + /// + /// Whether a power met in the descent may read itself through its exponent: neither in + /// a two-sided comparison nor as the whole of a one-sided question. + /// + internal static bool MayReadAPowerThroughItsExponent(Entity power) + => !comparingSides && !ReferenceEquals(power, descentRoot); + /// /// A logarithm to a base other than e as a quotient of natural logarithms. /// @@ -342,8 +365,14 @@ internal static T WithinABudget(int limits, System.Func compute) // the work -- which is not a wrong answer but is a slower one, since what it declines // to collapse the search then has to explore. var enclosingApproach = EnterApproach(x, dest); + var (outerComparing, outerRoot) = (comparingSides, descentRoot); + (comparingSides, descentRoot) = (false, null); try { return ComputeLimitWithinApproach(expr, x, dest, side, acceptNaN); } - finally { LeaveApproach(enclosingApproach); } + finally + { + (comparingSides, descentRoot) = (outerComparing, outerRoot); + LeaveApproach(enclosingApproach); + } } private static Entity? ComputeLimitWithinApproach(Entity expr, Variable x, Entity dest, ApproachFrom side, bool acceptNaN) @@ -419,7 +448,10 @@ internal static T WithinABudget(int limits, System.Func compute) if (side is ApproachFrom.Left or ApproachFrom.Right) { expr = WithStepsAtTheirLimits(expr, x, dest, side); - var oneSided = expr.ComputeLimitDivideEtImpera(x, dest, side); + descentRoot = expr; + Entity? oneSided; + try { oneSided = expr.ComputeLimitDivideEtImpera(x, dest, side); } + finally { descentRoot = null; } if (oneSided is { } found && (acceptNaN || found.Evaled != MathS.NaN)) return oneSided; // The same rule the two-sided path falls through to, asked with the side it was @@ -462,7 +494,10 @@ internal static T WithinABudget(int limits, System.Func compute) { // just compute limit with no check for left/right equality // here approach left will be ignored anyways, as dest is infinite number - var atInfinity = expr.ComputeLimitDivideEtImpera(x, dest, ApproachFrom.Left); + descentRoot = expr; + Entity? atInfinity; + try { atInfinity = expr.ComputeLimitDivideEtImpera(x, dest, ApproachFrom.Left); } + finally { descentRoot = null; } if (atInfinity is { } found && found.Evaled != MathS.NaN) return found; // l'Hopital's rule was only ever reached for a finite destination, so the @@ -520,8 +555,8 @@ internal static T WithinABudget(int limits, System.Func compute) } // Each side with its own steps put in: floor(x) ^ x at 0 is 1 from the left and 0 // from the right, and read bare both sides were 1. - else if (WithStepsAtTheirLimits(expr, x, dest, ApproachFrom.Left).ComputeLimitDivideEtImpera(x, dest, ApproachFrom.Left) is { } fromLeft - && WithStepsAtTheirLimits(expr, x, dest, ApproachFrom.Right).ComputeLimitDivideEtImpera(x, dest, ApproachFrom.Right) is { } fromRight) + else if (DescendComparingSides(ApproachFrom.Left) is { } fromLeft + && DescendComparingSides(ApproachFrom.Right) is { } fromRight) { if (fromLeft == fromRight && (acceptNaN || fromLeft.Evaled != MathS.NaN)) return fromLeft; @@ -558,6 +593,14 @@ internal static T WithinABudget(int limits, System.Func compute) } } throw new AngouriBugException($"Unresolved enum parameter {side}"); + + Entity? DescendComparingSides(ApproachFrom oneSide) + { + var stepped = WithStepsAtTheirLimits(expr, x, dest, oneSide); + comparingSides = true; + try { return stepped.ComputeLimitDivideEtImpera(x, dest, oneSide); } + finally { comparingSides = false; } + } } internal static Entity? ComputeLimitImpl(Entity expr, Variable x, Entity dist, ApproachFrom side) => dist switch diff --git a/Sources/AngouriMath/Functions/Continuous/Limits/Transformations.cs b/Sources/AngouriMath/Functions/Continuous/Limits/Transformations.cs index 542eb99f1..b3ef2cd08 100644 --- a/Sources/AngouriMath/Functions/Continuous/Limits/Transformations.cs +++ b/Sources/AngouriMath/Functions/Continuous/Limits/Transformations.cs @@ -33,7 +33,7 @@ private static Entity EquivalenceRules(Entity expr, Variable x, Entity dest) && EvalAssumingContinuous(expr.DirectChildren[0].Limit(x, dest)) == 0 ? expr.DirectChildren[0] : expr; - private static Entity EvalAssumingContinuous(Entity expr) => + internal static Entity EvalAssumingContinuous(Entity expr) => expr.Evaled switch { Providedf(var inner, _) => inner, @@ -498,7 +498,7 @@ private static bool IsRealAlong(Entity expr, Variable x) /// 1^oo is left to , which answers it more directly, /// and 0^oo and oo^oo are not indeterminate at all. /// - private static Entity? SolveAsIndeterminatePower(Entity expr, Variable x, Entity dest, ApproachFrom side) + internal static Entity? SolveAsIndeterminatePower(Entity expr, Variable x, Entity dest, ApproachFrom side) { if (expr is not Powf(var @base, var power) || !@base.ContainsNode(x) || !power.ContainsNode(x) diff --git a/Sources/Tests/UnitTests/Calculus/AnIndeterminatePowerUnderAFunctionTest.cs b/Sources/Tests/UnitTests/Calculus/AnIndeterminatePowerUnderAFunctionTest.cs new file mode 100644 index 000000000..01e1c2613 --- /dev/null +++ b/Sources/Tests/UnitTests/Calculus/AnIndeterminatePowerUnderAFunctionTest.cs @@ -0,0 +1,45 @@ +// +// 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 Xunit; + +namespace AngouriMath.Tests.Calculus +{ + /// + /// An indeterminate power under a function is read through its exponent, as one standing alone is. + /// x^x at 0+ is 1, and sin(x^x) there was unevaluated, because the rule that reads a power through + /// its exponent was asked only of a whole expression. Found by limitcheck at SEED=84; no issue was + /// filed. + /// + [Trait("Area", "Calculus")] + public sealed class AnIndeterminatePowerUnderAFunctionTest + { + [Theory] + [InlineData("sin(x^x)", "0", ApproachFrom.Right, "sin(1)")] + [InlineData("cos(x^x)", "0", ApproachFrom.Right, "cos(1)")] + [InlineData("sin(x^x + 1)", "0", ApproachFrom.Right, "sin(2)")] + [InlineData("sin(sin(x)^x)", "0", ApproachFrom.Right, "sin(1)")] + [InlineData("sin((x - 1)^ln(x))", "1", ApproachFrom.Right, "sin(1)")] + [InlineData("sin(x^(1/x))", "+oo", ApproachFrom.BothSides, "sin(1)")] + public void IsReadThroughItsExponent(string input, string destination, ApproachFrom side, string expected) + => Assert.Equal(MathS.FromString(expected), MathS.FromString(input).Limit("x", destination, side)); + + // The base of x^x is negative on the left of 0, where the power is complex, and the two-sided + // limit there is left as it was. + [Fact] + public void AComplexSideIsNotRead() + { + Assert.Equal(MathS.NaN, MathS.FromString("sin(x^x)").Limit("x", 0, ApproachFrom.BothSides)); + Assert.IsType(MathS.FromString("sin(x^x)").Limit("x", 0, ApproachFrom.Left)); + // Its mirror as well: two sides each read through the exponent would settle (-x)^x at 0 + // and leave x^x NaN. + Assert.Equal(MathS.NaN, MathS.FromString("(-x)^x").Limit("x", 0, ApproachFrom.BothSides)); + Assert.Equal(MathS.NaN, MathS.FromString("arctan((-x)^(2x))").Limit("x", 0, ApproachFrom.BothSides)); + } + } +} diff --git a/Sources/Tests/UnitTests/Calculus/AnInfinityIsNotACoefficientTest.cs b/Sources/Tests/UnitTests/Calculus/AnInfinityIsNotACoefficientTest.cs index b80161820..99296bc4f 100644 --- a/Sources/Tests/UnitTests/Calculus/AnInfinityIsNotACoefficientTest.cs +++ b/Sources/Tests/UnitTests/Calculus/AnInfinityIsNotACoefficientTest.cs @@ -46,12 +46,17 @@ public void LeavesEverythingElseAsItWas() Assert.Equal(MathS.FromString("+oo"), MathS.FromString("+oo * x").Limit("x", "+oo")); } - // The values are 1 near 1/2, and +oo near 0 from the right; a coefficient evaluated to NaN answered NaN for - // both. Neither is answered yet, and a declined limit keeps its node. + // The values are 1 near 1/2; a coefficient evaluated to NaN answered NaN. It is not answered yet, and a + // declined limit keeps its node. [Theory] [InlineData("1 ^ log(1 - x, x - x)", "1/2", ApproachFrom.BothSides)] - [InlineData("sqrt(x) / x ^ pi + x ^ x", "0", ApproachFrom.Right)] public void ANaNCoefficientIsNotAnAnswer(string input, string destination, ApproachFrom side) => Assert.IsType(MathS.FromString(input).Limit("x", destination, side)); + + // The values are +oo near 0 from the right, where a coefficient evaluated to NaN answered NaN too. It is + // answered since x ^ x, an indeterminate power in a sum, is read through its exponent. + [Fact] + public void ANaNCoefficientInASumWithAnIndeterminatePower() + => Assert.Equal(MathS.FromString("+oo"), MathS.FromString("sqrt(x) / x ^ pi + x ^ x").Limit("x", 0, ApproachFrom.Right)); } }