Skip to content
Open
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
32 changes: 32 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,38 @@ 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 constant to a power, or a gamma function, whose argument keeps turning has no limit

**Silent: a wrong `+oo` becomes an unevaluated limit.** The limit of an exponent says how the modulus of `e^g`
grows and nothing about its argument, which is the imaginary part of `g`: `ln(x)^2` at `-oo` is `+oo`, rightly, and
`exp(ln(x)^2)` there was `+oo` where its argument `2 pi ln|x|` keeps turning (arg/pi 0.05, 0.66, -0.74 at
`x = -1e10, -1e11, -1e12`, by mpmath). A constant to a power whose exponent grows now keeps an infinity only where the
argument settles on an axis: the exponent is real near the point, or what is left of it once the addends real there
are taken out has a finite limit, as `pi i` for `exp(x + pi i)`, which is `-oo`. The gamma function along a path off
the real line turns as `Im(z) ln|z|`, and keeps an infinity only for an argument real near the point;
`gamma(x + i/x)` at `+oo`, whose argument does settle, is now left unevaluated with the rest. So are two limits that
need only the modulus: a two-sided limit with one such side, `NaN` before where the other side is `+oo` or `0` and
unevaluated now, as `x^(1/x)` at 0 already was, and `x tanh(ln((1 + 1/x)^x))` at -1, whose tangent tends to 1 for
any argument. Of the 6314 limits of the sweep that found this, 18 answers changed: 14 wrong `+oo` are unevaluated,
and so are the four above, the `NaN`s and the `-1`. Measured on master `409d68805`
and on this branch ([#2708](https://github.com/asc-community/AngouriMath/issues/2708)).

| `.Limit(x, ...)` | Was (`409d68805`) | Now |
|---|---|---|
| `exp((1 + i) x)`, `exp(x + i)`, `exp(x + i ln(x))`, `2^(x + i)`, `gamma(x + i)` at `+oo` | `+oo` | unevaluated |
| `exp(x + 2 i)` at `+oo` | `-oo` | unevaluated |
| `exp(ln(x)^2)`, `exp(-x ln(x))`, `gamma(ln(x))` at `-oo` | `+oo` | unevaluated |
| `exp(ln(x) / x)`, `exp(ln(x)^2)`, `gamma(-ln(x))` at 0 from the left | `+oo` | unevaluated |
| `gamma(ln(ln(x)))`, `exp(1/x + i)` at 0 from the right | `+oo` | unevaluated |
| `exp((x - 1) ln(x))` at 0 from the left | `+oo` | `-oo` |
| `exp(-ln(x) / 2)` at 0 from the left | `+oo` | `-ooi` |
| `2^(x + pi i / ln(2))` at `+oo` | `+oo` | `-oo` |
| `exp((1 + i) x) / x` at `+oo` | `+oo + +ooi` | unevaluated |
| `gamma(x + i/x)` at `+oo` | `+oo` | unevaluated |
| `exp(ln(x) / x)`, `exp(ln(2 x) / (2 x))`, `exp(ln(sqrt(x)) ln(x))` at 0 | `NaN` | unevaluated |
| `x tanh(ln((1 + 1/x)^x))` at -1 | `-1` | unevaluated |
| `exp(x + pi i)`, `exp(x + i/x)`, `gamma(x)` at `+oo`; `exp(-ln(x))`, `x^(x - 1)` at 0 from the left | `-oo`, `+oo`, `+oo`; `-oo`, `-oo` | 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,45 @@ namespace AngouriMath
using static Functions.Algebra.LimitFunctional;
partial record Entity
{
/// <summary>
/// Whether <paramref name="expr"/> is real near <paramref name="dist"/> on each side approached, decided
/// structurally: a power to an exponent that is not whole, and a logarithm, of a base and an argument real and
/// positive there, by <see cref="SignNear"/>, a factorial of a real argument, and otherwise
/// <see cref="Functions.TreeAnalyzer.IsRealValued"/> or <see cref="Functions.TreeAnalyzer.WhereRealValued"/>'s
/// condition holding near the point. Not <see cref="IsRealOnBothSides"/>, which counts a constant that is not
/// real as the same on each side, and so reads <c>i ln(x)</c> as real. Unlisted is not real, which costs
/// coverage and never correctness. https://github.com/asc-community/AngouriMath/issues/2708
/// </summary>
internal static bool IsRealNear(Entity expr, Variable x, Entity dist, ApproachFrom side)
{
if (!expr.ContainsNode(x))
return expr.Evaled is Real { IsNaN: false };
if (Functions.TreeAnalyzer.IsRealValued(expr, x)
|| Functions.TreeAnalyzer.WhereRealValued(expr, x) is { } whereReal
&& Sides(dist, side).All(oneSide => HoldsNear(whereReal, x, dist, oneSide) is true))
return true;
return expr switch
{
Variable => true,
Sumf(var augend, var addend) => Near(augend) && Near(addend),
Minusf(var minuend, var subtrahend) => Near(minuend) && Near(subtrahend),
Mulf(var multiplier, var multiplicand) => Near(multiplier) && Near(multiplicand),
Divf(var dividend, var divisor) => Near(dividend) && Near(divisor),
Powf(var @base, Integer) => Near(@base),
Powf(var @base, var exponent) => Near(@base) && Near(exponent) && Positive(@base),
Logf(var @base, var antilogarithm) => Near(@base) && Near(antilogarithm) && Positive(@base) && Positive(antilogarithm),
Factorialf(var argument) => Near(argument),
Sinf or Cosf or Tanf or Cotanf or Secantf or Cosecantf or Arctanf or Arccotanf or Erff or Erfcf or Erfif
or Sif or Shif => Near(expr.DirectChildren[0]),
Absf => true,
_ => false
};
bool Near(Entity part) => IsRealNear(part, x, dist, side);
bool Positive(Entity part)
=> !part.ContainsNode(x) ? part.Evaled is Real { IsPositive: true }
: Sides(dist, side).All(oneSide => SignNear(part, x, dist, oneSide) is 1);
}

partial record Number
{
internal override Entity? ComputeLimitDivideEtImpera(Variable x, Entity dist, ApproachFrom side) => this;
Expand Down Expand Up @@ -598,6 +637,47 @@ partial record Logf
partial record Powf
{
internal override Entity? ComputeLimitDivideEtImpera(Variable x, Entity dist, ApproachFrom side)
=> ComputeLimitDivideEtImperaCore(x, dist, side) is var limit
&& !Base.ContainsNode(x) && Exponent.ContainsNode(x)
&& limit is { Evaled: Complex { IsFinite: false, IsNaN: false } }
? WhereThePhaseSettles(limit, x, dist, side)
: limit;

/// <summary>
/// An infinity found for a constant to a power that grows, kept only where the argument of the power settles.
/// </summary>
/// <remarks>
/// The modulus of <c>b^g</c> is <c>e^Re(g ln b)</c> and its argument <c>Im(g ln b)</c>, so a limit of the exponent
/// in a direction says how the modulus grows and nothing about the argument: <c>ln(x)^2</c> at <c>-oo</c> is
/// <c>+oo</c>, rightly, and <c>exp(ln(x)^2)</c> was <c>+oo</c> where its argument <c>2 pi ln|x|</c> keeps turning
/// (arg/pi 0.05, 0.66, -0.74 at x = -1e10, -1e11, -1e12, by mpmath). For a positive base the argument is that of
/// <c>b^v</c>, where <c>v</c> is what is left of the exponent once the addends real near the point are taken out,
/// and it settles where <c>v</c> has a finite limit: <c>exp(x + pi i)</c> at <c>+oo</c> is <c>-oo</c> and
/// <c>exp(x + i/x)</c> is <c>+oo</c>, while <c>exp(x + i)</c> settles off the axes and <c>exp((1 + i) x)</c> turns,
/// and both are left unevaluated.
/// https://github.com/asc-community/AngouriMath/issues/2708
/// </remarks>
private Entity? WhereThePhaseSettles(Entity infinity, Variable x, Entity dist, ApproachFrom side)
{
if (Base.Evaled is not Real { IsPositive: true, IsFinite: true })
return null;
Entity? turning = null;
foreach (var addend in Sumf.LinearChildren(Exponent))
if (!IsRealNear(addend, x, dist, side))
turning = turning is null ? addend : turning + addend;
if (turning is null)
return infinity;
if (turning.ComputeLimitDivideEtImpera(x, dist, side) is not { IsFinite: true } settled
|| New(Base, settled).Evaled is not Complex { IsFinite: true, IsNaN: false, IsZero: false } direction)
return null;
if (direction.ImaginaryPart.IsZero)
return direction.RealPart.IsNegative ? Real.NegativeInfinity : Real.PositiveInfinity;
if (direction.RealPart.IsZero)
return Complex.Create(Integer.Zero, direction.ImaginaryPart.IsNegative ? Real.NegativeInfinity : Real.PositiveInfinity);
return null;
}

private Entity? ComputeLimitDivideEtImperaCore(Variable x, Entity dist, ApproachFrom side)
{
// A base tending to a positive number or to +oo, to a constant real exponent that is
// not 0, is continuous there: the power tends to the base's limit to that exponent,
Expand Down Expand Up @@ -946,6 +1026,18 @@ partial record Factorialf
// to an infinity there whose sign depends on the side, so putting the argument's limit in
// would only write the pole down. x! at -1 came back (-1)! once that limit arrived as -1.
internal override Entity? ComputeLimitDivideEtImpera(Variable x, Entity dist, ApproachFrom side)
=> ComputeLimitDivideEtImperaCore(x, dist, side) is var limit
&& limit is { Evaled: Complex { IsFinite: false, IsNaN: false } }
&& !IsRealNear(Argument, x, dist, side)
? null
: limit;

// An infinity is kept only for an argument real near the point. Along a path off the real line the argument of
// z! keeps turning, by Stirling's formula as about Im(z) ln|z|: gamma(x + i) at +oo was +oo where its argument
// is ln(x) (arg/pi 0.72, -0.54, 0.20, 0.40 at x = 10, 100, 1e3, 1e6, by mpmath), and gamma(ln(x)) at -oo, whose
// argument tends to +oo with the imaginary part pi, was +oo too.
// https://github.com/asc-community/AngouriMath/issues/2708
private Entity? ComputeLimitDivideEtImperaCore(Variable x, Entity dist, ApproachFrom side)
{
if (ComputeLimitImpl(this, x, dist, side) is { } lim)
return lim;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2487,7 +2487,13 @@ static Entity Times(Entity taken, Entity limit)
substitutingFactorial = true;
try
{
if (Settled(ComputeLimit(logarithm, x, toInfinity)) is { } exponent)
// e to an infinite limit of the logarithm is a direction only where the logarithm's
// imaginary part, the argument, settles, which it does where it is real: ln((x + i - 1)!) is
// +oo, and gamma(x + i) was +oo where its argument keeps turning as ln(x).
// https://github.com/asc-community/AngouriMath/issues/2708
if (Settled(ComputeLimit(logarithm, x, toInfinity)) is { } exponent
&& (exponent.Evaled is not Complex { IsFinite: false }
|| Entity.IsRealNear(logarithm, x, toInfinity, ApproachFrom.Left)))
return MathS.e.Pow(exponent).InnerSimplified;
}
finally { substitutingFactorial = false; }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
//
// 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;
using static AngouriMath.Entity.Number;

namespace AngouriMath.Tests.Calculus
{
/// <summary>
/// A constant to a power, or a gamma function, whose modulus grows without bound while its argument keeps turning
/// has no direction, and the limit is left unevaluated. The exponent's own limit says how the modulus grows and
/// nothing about the argument: ln(x)^2 at -oo is +oo, and the argument of exp(ln(x)^2) is 2 pi ln|x|. Values by mpmath.
/// https://github.com/asc-community/AngouriMath/issues/2708
/// </summary>
[Trait("Area", "Calculus")]
public sealed class APhaseThatKeepsTurningIsNotADirectionTest
{
// arg/pi of exp((1 + i) x) is x/pi; of exp(x + i ln(x)) ln(x)/pi; of gamma(x + i) 0.72, -0.54, 0.20, 0.40
// at x = 10, 100, 1e3, 1e6. exp(x + i) settles at 1, off the axes.
[Theory]
[InlineData("exp((1 + i) * x)")]
[InlineData("exp(x + i * ln(x))")]
[InlineData("exp(x + i)")]
[InlineData("2^((1 + i) * x)")]
[InlineData("gamma(x + i)")]
[InlineData("gamma(x + i * ln(x))")]
public void Issue2708_AtPositiveInfinity(string input)
=> Assert.IsType<Entity.Limitf>(MathS.FromString(input).Limit("x", Real.PositiveInfinity));

// arg/pi of exp(ln(x)^2) is 0.05, 0.66, -0.74 at x = -1e10, -1e11, -1e12, and of gamma(ln(x)) -0.88, -0.56,
// -0.26, 0.04 at x = -1e10 ... -1e10000. x^ln(x), the first spelled otherwise, was already left unevaluated.
[Theory]
[InlineData("exp(ln(x)^2)")]
[InlineData("x^ln(x)")]
[InlineData("gamma(ln(x))")]
public void Issue2708_AtNegativeInfinity(string input)
=> Assert.IsType<Entity.Limitf>(MathS.FromString(input).Limit("x", Real.NegativeInfinity));

// arg/pi of exp(ln(x)/x) is 0, 0.91, 0.67, 0.77 at x = -1e-3 ... -1.3e-3; x^(1/x) was already unevaluated.
[Theory]
[InlineData("exp(ln(x) / x)", ApproachFrom.Left)]
[InlineData("x^(1/x)", ApproachFrom.Left)]
[InlineData("exp(ln(x)^2)", ApproachFrom.Left)]
[InlineData("gamma(-ln(x))", ApproachFrom.Left)]
[InlineData("gamma(ln(ln(x)))", ApproachFrom.Right)]
[InlineData("exp(1/x + i/x)", ApproachFrom.Right)]
public void Issue2708_AtZero(string input, ApproachFrom side)
=> Assert.IsType<Entity.Limitf>(MathS.FromString(input).Limit("x", 0, side));

// Where the argument settles on an axis the infinity stays: exp(x + pi i) is -e^x, and exp(x + i/x) has the
// argument 1/x. A gamma function of an argument real on the way is real.
[Theory]
[InlineData("exp(x + pi * i)", "-oo")]
[InlineData("exp(x + i / x)", "+oo")]
[InlineData("gamma(x)", "+oo")]
[InlineData("x!", "+oo")]
public void Issue2708_ASettledDirectionStays(string input, string expected)
=> Assert.Equal(MathS.FromString(expected), MathS.FromString(input).Limit("x", Real.PositiveInfinity));

[Fact]
public void Issue2708_ASettledImaginaryDirectionStays()
=> Assert.Equal(Complex.Create(Integer.Zero, Real.PositiveInfinity),
MathS.FromString("exp(x + pi / 2 * i)").Limit("x", Real.PositiveInfinity));

[Theory]
[InlineData("exp(-ln(x))", "-oo")]
[InlineData("exp(-2 * ln(x))", "+oo")]
[InlineData("ln(x)", "-oo")]
[InlineData("x^(x - 1)", "-oo")]
[InlineData("x^(x - 2)", "+oo")]
public void Issue2708_OnTheLeftOfZeroADirectionStays(string input, string expected)
=> Assert.Equal(MathS.FromString(expected), MathS.FromString(input).Limit("x", 0, ApproachFrom.Left));
}
}
Loading