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
39 changes: 39 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,45 @@ and it was `+oo`. It is `NaN`, as `(-x)^x` at 0 is. Measured on master `34d48113
| `x^(1/2 + x) / x` at 0 | `NaN` | unevaluated |
| `x^(x - 1)` at 0 from the right; `x^x`, `x^(x - 1) * x` at 0 from the left | `+oo`; `1`, `1` | unchanged |

### A limit on a side that is not real is the infinity its direction settles in

**Not silent: an unevaluated limit becomes a directed infinity, and a wrong `+oo` becomes an unevaluated limit.** Where
the side approached is not real and the modulus grows without bound while the argument settles, the limit is now the
infinity in that direction, `+oo`, `-oo`, `+ooi` or `-ooi`, as `ln(x)` at 0 from the left already was `-oo` and
`sqrt(x) / x` there `-ooi`. `x^(x - 1)` at 0 from the left is `-1.0E+20 + 3.1i` at `x = -1e-20`, by mpmath, and is
`-oo`. **A `-oo` or `+oo` on such a side is a direction, not the real infinity**: the values are not real, and their
imaginary part need not vanish, or even stay bounded (`x^(x - 2)` there is `1.0E+40 - 3.1E+20i`). Where the argument
settles off the axes, as at `-pi/3` for `x^(x - 1/3)`, the limit is left unevaluated, since no infinity the library
writes points that way; and where it keeps turning, as `pi/x` does for `x^(1/x)` at 0 from the left, there is no
direction, and that limit, which was `+oo`, is unevaluated. A square root or a power to an exponent that is not whole,
of an argument tending to `-oo` off the real line, depends on the side of the cut the argument comes from and is left
unevaluated: `sqrt((-x)^(x - 1))` at 0 from the right tends to `-ooi`, and was going to be answered `+ooi`. The
logarithm of an infinity in any direction is `+oo`, where `ln(sqrt(x) / x)` at 0 from the left was `ln(-ooi)`. A two-sided
limit with such a side, of a power to an exponent that moves, stays `NaN` where both sides tend to one infinity, as
`(-x)^x` at 0 is `NaN` where both sides tend to 1. l'Hopital's rule, a theorem about real functions, is applied to a
complex denominator only where its derivative keeps one direction (`ln(x)` at 0 from the left is `ln(abs(x)) + i pi`,
whose derivative `1/x` is real), or upside down where that one does: `x / (x + x^2 e^(i/x^2))` at 0 from the right,
whose quotient of derivatives tends to 0 where the quotient tends to 1, is left unevaluated. Measured on master
`82eb869e1` and on this branch ([#2697](https://github.com/asc-community/AngouriMath/issues/2697)).

| `.Limit(x, ...)` | Was (`82eb869e1`) | Now |
|---|---|---|
| `x^(x - 1)`, `(e^x - 1)^(x - 1)`, `sin(x)^(x - 1)`, `tan(x)^(x - 1)` at 0 from the left | unevaluated | `-oo` |
| `x^(x - 2)`, `sin(x)^(x - 2)` at 0 from the left | unevaluated | `+oo` |
| `x^(x - 1/2)` at 0 from the left; `x^(-1/2)`, `1 / sqrt(x)` there | unevaluated | `-ooi` |
| `x^(x - 3/2)` at 0 from the left | unevaluated | `+ooi` |
| `(sqrt(x) - erf(x)) / x` at 0 from the left | unevaluated | `-ooi` |
| `(x^2 - x)^(x - 1)` at 0 from the right | unevaluated | `-oo` |
| `x^(1/2 + 1/x)` at `-oo` | unevaluated | `+ooi` |
| `x^(1/x)`, `(e^x - 1)^(1/x)` at 0 from the left; `(x^2 - x)^(-1/x)` at 0 from the right | `+oo` | unevaluated |
| `(-x)^x` at `+oo`; `x^(-x)` at `-oo` | `+oo` | unevaluated |
| `x^(1/x)` at 0 | `NaN` | unevaluated |
| `ln(sqrt(x) / x)` at 0 from the left | `ln(-ooi)` | `+oo` |
| `ln(1 / sqrt(x))` at 0 from the left | unevaluated | `+oo` |
| `sin(x)^(x - 1) * x` at 0 from the left | unevaluated | `1` |
| `x^(x - 1) * ln(x)` at 0 | unevaluated | `NaN` |
| `x^(x - 1) / x` at 0; `x^(x - 1/3)` at 0 from the left; `x^x`, `sin(x)^x` there | `NaN`; unevaluated; `1`, `1` | unchanged |

### A two-sided limit is NaN only where its sides say so

**Not silent: a wrong answer becomes an unevaluated limit.** A two-sided limit came back `NaN`, the claim that it does
Expand Down
115 changes: 104 additions & 11 deletions Sources/AngouriMath/Functions/Continuous/Limits/Gruntz/Gruntz.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,44 @@ private static void Trace(string what, object? value)
if (expr.Nodes.Any(node => LimitFunctional.TakesItsValueOnACut(node, x, Real.PositiveInfinity)))
return null;
expr = AsExponentials(expr, x);
if (expr.Nodes.Any(IsAnalyticFunction) && !IsRealOnTheWay(expr, x))
// An analytic function is read through its Taylor series at the real point its argument tends to, and only
// where that argument is real on the way. Asked of the argument rather than of the whole expression:
// sin(x)^(x - 2) at 0 from the left is (-1/x)^... at +oo, not real, and the sine in it is of a real
// argument; it tends to +oo, as its principal value does. https://github.com/asc-community/AngouriMath/issues/2697
if (expr.Nodes.Any(node => IsAnalyticFunction(node) && !IsRealOnTheWay(node, x)))
return null;
return LimitInf(expr, x);
if (expr.Nodes.Any(node => ArrivesOnTheCutOffTheRealLine(node, x)))
return null;
return LimitInf(expr, x, directed: true);
}

/// <summary>
/// Whether <paramref name="node"/> is a logarithm, or a power to a constant exponent that is not whole, of an
/// argument that is not real on the way and tends to the negative real line, the cut, or for the power to <c>-oo</c>
/// along it.
/// </summary>
/// <remarks>
/// Which side of the cut such an argument arrives from decides the answer, and the series takes the principal
/// value of the leading coefficient, which is the one from above: sqrt((-x)^(x - 1)) at 0 from the right, whose
/// argument tends to -oo from below the cut (-1.0E+20 - 3.1i at x = 1e-20, by mpmath), was +ooi where it is
/// -ooi. An argument real on the way arrives along the cut, where the principal value is the limit, and is read.
/// https://github.com/asc-community/AngouriMath/issues/2697
/// </remarks>
private static bool ArrivesOnTheCutOffTheRealLine(Entity node, Variable x)
{
var argument = node switch
{
Logf(_, var antilog) => antilog,
Powf(var @base, var power) when @base != MathS.e && !power.ContainsNode(x) && power.Evaled is not Integer => @base,
_ => null
};
if (argument is null || !argument.ContainsNode(x)
|| !argument.Nodes.Any(static n => n is Complex and not Real) && IsRealOnTheWay(argument, x))
return false;
// A logarithm of an argument growing without bound grows too, whichever side of the cut it is on.
return LimitInf(argument, x, directed: true) is not { } limit
|| Bare(limit).Evaled is not Complex { IsNaN: false } value
|| value is Real { IsNegative: true } negative && (negative.IsFinite || node is Powf);
}

private static bool IsAnalyticFunction(Entity node)
Expand Down Expand Up @@ -128,7 +163,13 @@ private static bool IsRealOnTheWay(Entity e, Variable x)
return true;
}

private static Entity? LimitInf(Entity e, Variable x)
/// <param name="e">The expression.</param>
/// <param name="x">The variable tending to <c>+oo</c>.</param>
/// <param name="directed">
/// Whether a leading coefficient that is not real may be read for the direction it tends to, which only the
/// limit asked for, and its leading coefficients in turn, are: a comparison of classes is not a value.
/// </param>
private static Entity? LimitInf(Entity e, Variable x, bool directed = false)
{
if (!e.ContainsNode(x))
return e;
Expand All @@ -137,25 +178,69 @@ private static bool IsRealOnTheWay(Entity e, Variable x)
{
MultithreadingFunctional.ExitIfCancelled();
Trace($"limitinf({e.Stringize()})", "...");
if (MrvLeadTerm(e, x) is not var (coefficient, power))
if (MrvLeadTerm(e, x) is not var (coefficient, power, inARealW))
{ Trace($"limitinf({e.Stringize()}) leadterm", null); return null; }
Trace($" leadterm({e.Stringize()})", $"({coefficient.Stringize()}, {power})");
if (power.Sign is not { } sign)
return null;
if (sign > 0)
return 0; // w^positive tends to zero
if (sign == 0)
return LimitInf(coefficient, x);
return LimitInf(coefficient, x, directed);
// The term grows without bound, and its direction is the coefficient's only where w is positive.
// x^(1/x) at 0 from the left, (-1/x)^(-x) at +oo, is written in w = e^(x ln(-1/x)), whose argument
// pi x keeps turning, and with the coefficient 1 it was +oo.
// https://github.com/asc-community/AngouriMath/issues/2697
if (!inARealW)
return null;
return Sign(coefficient, x) switch
{
1 => Real.PositiveInfinity,
-1 => Real.NegativeInfinity,
_ => null
_ => directed ? DirectedInfinity(coefficient, x) : null
};
}
finally { depth--; }
}

/// <summary>
/// The infinity in the direction <paramref name="coefficient"/> settles in, as the leading coefficient of a
/// term <c>coefficient * w^p</c> with <c>w</c> positive and tending to 0 and <c>p</c> negative, or
/// <see langword="null"/> where the direction does not settle on one of the axes.
/// </summary>
/// <remarks>
/// <para>
/// The modulus of the term grows without bound, and its argument is the coefficient's, since <c>w</c> is
/// positive: the substitution in <see cref="Rewrite"/> writes everything in a member whose exponent has a
/// sign, which it has only where it is real. Where the coefficient tends to a number other than 0, or to an
/// infinity with a direction, the argument tends to that one's, and the limit is the infinity in that
/// direction, as <c>ln(x)</c> at 0 from the left is already <c>-oo</c>. x^(x - 1) at 0 from the left is
/// (-1/x)^(-1/x - 1) at <c>+oo</c>, which leads with <c>e^(-i pi) * w^(-1)</c>, and is <c>-oo</c>:
/// -1.0E+20 + 3.1415927i at x = -1e-20, by mpmath.
/// </para>
/// <para>
/// The library writes four of these, <c>+oo</c>, <c>-oo</c>, <c>+ooi</c> and <c>-ooi</c>, and a direction
/// off the axes is left undecided: x^(x - 1/3) at 0 from the left settles at <c>-pi/3</c>. Where the argument
/// keeps turning, as for x^(1/x) at 0 from the left, whose phase is <c>pi/x</c>, the member written in has an
/// exponent that is not real, and the rewriting declined before this is asked.
/// https://github.com/asc-community/AngouriMath/issues/2697
/// </para>
/// </remarks>
private static Entity? DirectedInfinity(Entity coefficient, Variable x)
{
if (LimitInf(coefficient, x, directed: true) is not { } limit
|| Bare(limit).Evaled is not Complex { IsNaN: false } direction)
return null;
var (real, imaginary) = (direction.RealPart, direction.ImaginaryPart);
if (direction.IsFinite && direction.IsZero)
return null;
if (imaginary.IsZero)
return real.IsNegative ? Real.NegativeInfinity : Real.PositiveInfinity;
if (real.IsZero)
return Complex.Create(Integer.Zero, imaginary.IsNegative ? Real.NegativeInfinity : Real.PositiveInfinity);
return null;
}

/// <summary>
/// Whether the expression is eventually positive, negative or identically zero, or
/// <see langword="null"/> where that cannot be settled. This is the one place the
Expand Down Expand Up @@ -196,7 +281,7 @@ private static bool IsRealOnTheWay(Entity e, Variable x)
if (++depth > MaxDepth) { depth--; return null; }
try
{
return MrvLeadTerm(e, x) is var (coefficient, _) ? Sign(coefficient, x) : null;
return MrvLeadTerm(e, x) is ({ } coefficient, _, true) ? Sign(coefficient, x) : null;
}
finally { depth--; }
}
Expand Down Expand Up @@ -413,15 +498,21 @@ private static HashSet<Entity> Union(HashSet<Entity> f, HashSet<Entity> g)
/// The leading coefficient of the expression and the exponent it sits at, once the
/// expression has been rewritten in terms of one member of its mrv set.
/// </summary>
private static (Entity Coefficient, SeriesExponent Power)? MrvLeadTerm(Entity e, Variable x)
/// <remarks>
/// The third element says whether <c>w</c> is real, which it is where the logarithm it is written with is: a
/// member whose exponent is not real tends to 0 in modulus, which the series and its leading exponent rest on,
/// but its argument need not settle, and the sign or direction of a term that does not vanish is then not the
/// coefficient's. https://github.com/asc-community/AngouriMath/issues/2697
/// </remarks>
private static (Entity Coefficient, SeriesExponent Power, bool InARealW)? MrvLeadTerm(Entity e, Variable x)
{
if (!e.ContainsNode(x))
return (e, SeriesExponent.Zero);
return (e, SeriesExponent.Zero, true);
if (Mrv(e, x) is not { } omega)
{ Trace($" mrv({e.Stringize()})", null); return null; }
Trace($" mrv({e.Stringize()})", "{" + string.Join(", ", omega.Select(o => o.Stringize())) + "}");
if (omega.Count == 0)
return (e, SeriesExponent.Zero);
return (e, SeriesExponent.Zero, true);

if (omega.Contains(x))
{
Expand Down Expand Up @@ -466,7 +557,9 @@ private static (Entity Coefficient, SeriesExponent Power)? MrvLeadTerm(Entity e,
continue; // everything cancelled; look further out
// The coefficient has to be free of w and slower than it, or the split into
// coefficient and power is not the one the algorithm's conclusion rests on.
return coefficient.ContainsNode(w) ? null : (coefficient, power);
return coefficient.ContainsNode(w) ? null
: (coefficient, power,
!logarithmOfW.Nodes.Any(static node => node is Complex and not Real) && IsRealOnTheWay(logarithmOfW, x));
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,19 @@ partial record Cotanf
partial record Logf
{
internal override Entity? ComputeLimitDivideEtImpera(Variable x, Entity dist, ApproachFrom side)
=> OfAnInfinity(ComputeLimitDivideEtImperaCore(x, dist, side));

// The logarithm of an infinity in any direction is +oo, its modulus growing while its argument stays
// bounded: ln(sqrt(x) / x) at 0 from the left, whose argument tends to -ooi, was answered ln(-ooi).
// https://github.com/asc-community/AngouriMath/issues/2697
private static Entity? OfAnInfinity(Entity? limit)
=> limit is Logf(var @base, var antilogarithm) && !@base.Vars.Any() && !antilogarithm.Vars.Any()
&& @base.Evaled is Real { IsFinite: true, IsPositive: true } positiveBase && positiveBase != 1
&& antilogarithm.Evaled is Complex { IsFinite: false, IsNaN: false }
? positiveBase > 1 ? Real.PositiveInfinity : Real.NegativeInfinity
: limit;

private Entity? ComputeLimitDivideEtImperaCore(Variable x, Entity dist, ApproachFrom side)
{
if (ComputeLimitImpl(this, x, dist, side) is { } lim)
return lim;
Expand Down Expand Up @@ -616,7 +629,7 @@ partial record Powf
var exponentLimit = Exponent.ComputeLimitDivideEtImpera(x, dist, side);
// A power to an exponent that is not whole has the logarithm's cut, for the reason
// the logarithm above declines it. https://github.com/asc-community/AngouriMath/issues/2266
if (exponentLimit?.Evaled is not Integer && ArrivesOnTheCutOffTheRealLine(Base, baseLimit, x, dist))
if (exponentLimit?.Evaled is not Integer && ArrivesOnTheCutOffTheRealLine(Base, baseLimit, x, dist, orAtItsEnd: true))
return null;
// A base that is never negative and vanishes, to an exponent tending to a negative
// number, tends to +oo: 0 to a negative power has no value, and abs(x)^(-3/2) at 0
Expand Down
Loading
Loading