Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,29 @@ bool PositiveOnBothSides(Entity node)

[System.ThreadStatic] private static int bothSidesDepth;

/// <summary>
/// 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.
/// </summary>
[System.ThreadStatic] private static bool comparingSides;

/// <summary>
/// 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.
/// </summary>
[System.ThreadStatic] private static Entity? descentRoot;

/// <summary>
/// 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.
/// </summary>
internal static bool MayReadAPowerThroughItsExponent(Entity power)
=> !comparingSides && !ReferenceEquals(power, descentRoot);

/// <summary>
/// A logarithm to a base other than e as a quotient of natural logarithms.
/// </summary>
Expand Down Expand Up @@ -342,8 +365,14 @@ internal static T WithinABudget<T>(int limits, System.Func<T> 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)
Expand Down Expand Up @@ -419,7 +448,10 @@ internal static T WithinABudget<T>(int limits, System.Func<T> 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
Expand Down Expand Up @@ -462,7 +494,10 @@ internal static T WithinABudget<T>(int limits, System.Func<T> 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
Expand Down Expand Up @@ -520,8 +555,8 @@ internal static T WithinABudget<T>(int limits, System.Func<T> 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;
Expand Down Expand Up @@ -558,6 +593,14 @@ internal static T WithinABudget<T>(int limits, System.Func<T> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -498,7 +498,7 @@ private static bool IsRealAlong(Entity expr, Variable x)
/// 1^oo is left to <see cref="ApplySecondRemarkable"/>, which answers it more directly,
/// and 0^oo and oo^oo are not indeterminate at all.
/// </remarks>
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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// 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.
/// </summary>
[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<Entity.Limitf>(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));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Entity.Limitf>(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));
}
}
Loading