diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index b942ff1ca..27db712f2 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -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 diff --git a/Sources/AngouriMath/Functions/Continuous/Limits/Gruntz/Gruntz.cs b/Sources/AngouriMath/Functions/Continuous/Limits/Gruntz/Gruntz.cs index ad63f643c..7a4bd568b 100644 --- a/Sources/AngouriMath/Functions/Continuous/Limits/Gruntz/Gruntz.cs +++ b/Sources/AngouriMath/Functions/Continuous/Limits/Gruntz/Gruntz.cs @@ -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); + } + + /// + /// Whether 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 -oo + /// along it. + /// + /// + /// 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 + /// + 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) @@ -128,7 +163,13 @@ private static bool IsRealOnTheWay(Entity e, Variable x) return true; } - private static Entity? LimitInf(Entity e, Variable x) + /// The expression. + /// The variable tending to +oo. + /// + /// 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. + /// + private static Entity? LimitInf(Entity e, Variable x, bool directed = false) { if (!e.ContainsNode(x)) return e; @@ -137,7 +178,7 @@ 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) @@ -145,17 +186,61 @@ private static bool IsRealOnTheWay(Entity e, Variable x) 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--; } } + /// + /// The infinity in the direction settles in, as the leading coefficient of a + /// term coefficient * w^p with w positive and tending to 0 and p negative, or + /// where the direction does not settle on one of the axes. + /// + /// + /// + /// The modulus of the term grows without bound, and its argument is the coefficient's, since w is + /// positive: the substitution in 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 ln(x) at 0 from the left is already -oo. x^(x - 1) at 0 from the left is + /// (-1/x)^(-1/x - 1) at +oo, which leads with e^(-i pi) * w^(-1), and is -oo: + /// -1.0E+20 + 3.1415927i at x = -1e-20, by mpmath. + /// + /// + /// The library writes four of these, +oo, -oo, +ooi and -ooi, and a direction + /// off the axes is left undecided: x^(x - 1/3) at 0 from the left settles at -pi/3. Where the argument + /// keeps turning, as for x^(1/x) at 0 from the left, whose phase is pi/x, 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 + /// + /// + 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; + } + /// /// Whether the expression is eventually positive, negative or identically zero, or /// where that cannot be settled. This is the one place the @@ -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--; } } @@ -413,15 +498,21 @@ private static HashSet Union(HashSet f, HashSet 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. /// - private static (Entity Coefficient, SeriesExponent Power)? MrvLeadTerm(Entity e, Variable x) + /// + /// The third element says whether w 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 + /// + 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)) { @@ -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; } diff --git a/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Limit.Classes.cs b/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Limit.Classes.cs index f04ae8ef5..ed08bf760 100644 --- a/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Limit.Classes.cs +++ b/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Limit.Classes.cs @@ -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; @@ -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 diff --git a/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Solvers.Definition.cs b/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Solvers.Definition.cs index c284e3022..235d5e8bc 100644 --- a/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Solvers.Definition.cs +++ b/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Solvers.Definition.cs @@ -291,6 +291,21 @@ bool RealAt(Rational offset) => expr.Substitute(x, at + offset).Evaled is Real { IsFinite: true }; } + /// + /// Whether has a finite value that is not real at two points on + /// of a finite real , 10^-6 and 10^-9 away. A witness, as is. + /// https://github.com/asc-community/AngouriMath/issues/2697 + /// + private static bool IsNotRealBeside(Entity expr, Variable x, Entity dest, ApproachFrom side) + { + if (dest.Evaled is not Real { IsFinite: true } at || side is ApproachFrom.BothSides) + return false; + var sign = side is ApproachFrom.Left ? -1 : 1; + return NotRealAt(Rational.Create(sign, 1_000_000)) && NotRealAt(Rational.Create(sign, 1_000_000_000)); + bool NotRealAt(Rational offset) + => expr.Substitute(x, at + offset).Evaled is Complex { IsFinite: true, IsNaN: false } and not Real; + } + /// /// Whether is real on both sides of a real , /// close enough to it, but for its constants, decided structurally: every power to an exponent that is not whole, @@ -907,14 +922,14 @@ node is Derivativef(var expression, Variable variable, var iterations) && iterat { if (fromLeft == fromRight && (acceptNaN || fromLeft.Evaled != MathS.NaN)) - return fromLeft; + return AnInfinityAgreedOffTheRealLine(fromLeft) ? MathS.NaN : fromLeft; // The numerical comparison passes over a point where either side is NaN, so a side that is NaN // agrees with anything there: (x^2 - x)^(x - 1) at 0 was +oo from the left and 0^(-1) from the // right, where the base is negative and the power not real, and the two "agreed" on +oo. // https://github.com/asc-community/AngouriMath/issues/2693 if (fromLeft.Evaled != MathS.NaN && fromRight.Evaled != MathS.NaN && ExpressionNumerical.AreEqual(fromLeft, fromRight)) - return fromLeft; + return AnInfinityAgreedOffTheRealLine(fromLeft) ? MathS.NaN : fromLeft; if (ApplylHopitalRule(expr, x, dest) is { } lhopital && ComputeLimit(lhopital, x, dest, acceptNaN: true) is { } byRule && (acceptNaN || byRule.Evaled != MathS.NaN)) @@ -951,6 +966,16 @@ node is Derivativef(var expression, Variable variable, var iterations) && iterat } throw new AngouriBugException($"Unresolved enum parameter {side}"); + // A power to an exponent that moves, of a base negative on one side, is not real there, and the two sides + // of it are not compared: (-x)^x at 0 is NaN, where both sides tend to 1. Nor where they tend to one + // infinity, which on that side is a direction: x^(x - 1) / x at 0 tends to +oo from the right and from the + // left, where it is 1.0E+40 - 3.1E+20i at x = -1e-20, by mpmath, and it was NaN before the left side was + // read. https://github.com/asc-community/AngouriMath/issues/2697 + bool AnInfinityAgreedOffTheRealLine(Entity agreed) + => agreed.Evaled is Complex { IsFinite: false, IsNaN: false } + && expr.Nodes.Any(node => node is Powf(_, var exponent) && exponent.ContainsNode(x) && !IsRealOnBothSides(node, x, dest)) + && (IsNotRealBeside(expr, x, dest, ApproachFrom.Left) || IsNotRealBeside(expr, x, dest, ApproachFrom.Right)); + Entity? DescendComparingSides(ApproachFrom oneSide) { var stepped = WithStepsAtTheirLimits(expr, x, dest, oneSide); diff --git a/Sources/AngouriMath/Functions/Continuous/Limits/Transformations.cs b/Sources/AngouriMath/Functions/Continuous/Limits/Transformations.cs index 7ec2f5fd6..1b10ceae0 100644 --- a/Sources/AngouriMath/Functions/Continuous/Limits/Transformations.cs +++ b/Sources/AngouriMath/Functions/Continuous/Limits/Transformations.cs @@ -8,6 +8,7 @@ using AngouriMath.Core.Multithreading; using PeterO.Numbers; using System; +using System.Collections.Generic; using System.Linq; using static AngouriMath.Entity; using static AngouriMath.Entity.Number; @@ -932,10 +933,18 @@ internal static bool HoldsAnIndeterminatePowerForm(Entity expr) /// side an argument off the real line comes from is not decided here, so a limit there is /// declined, including one from above, whose answer was right. /// #2266 + /// + /// With , also at the cut's end, -oo, which an argument off the real line + /// reaches as the infinity in that direction, and where a power to an exponent that is not whole still takes its + /// direction from the side: (-x)^(x - 1) at 0 from the right tends to -oo from below the cut, as + /// -1.0E+20 - 3.1i at x = 1e-20, by mpmath, and its square root to -ooi, where the root + /// of -oo is +ooi. A logarithm there tends to +oo from either side, and is not asked. + /// #2697 /// - internal static bool ArrivesOnTheCutOffTheRealLine(Entity argument, Entity? limit, Variable x, Entity dest) + internal static bool ArrivesOnTheCutOffTheRealLine(Entity argument, Entity? limit, Variable x, Entity dest, bool orAtItsEnd = false) => argument.ContainsNode(x) - && limit?.Evaled is Real { IsFinite: true, IsNegative: true } + && limit?.Evaled is Real { IsNegative: true, IsNaN: false } negative + && (negative.IsFinite || orAtItsEnd) && !IsRealOnTheApproach(argument, x, dest); /// @@ -1241,6 +1250,70 @@ private static bool IsRealNear(Entity expr, Variable x, Entity dest, ApproachFro || Functions.TreeAnalyzer.WhereRealValued(expr, x) is { } whereReal && HoldsNear(whereReal, x, dest, oneSide) is true || IsRealOnBothSides(expr, x, dest); + /// + /// The points beside on each side approached at which and + /// read an expression, in pairs: 10^-6 and 10^-9 away from a + /// finite one, 10^2 and 10^3 out towards an infinity. Witnesses, not proofs either way. + /// + private static IEnumerable<(Entity Nearer, Entity Further)> PointsBeside(Entity dest, ApproachFrom side) + { + if (dest.Evaled is not Real { IsNaN: false } at) + yield break; + foreach (var oneSide in Sides(dest, side)) + { + var sign = oneSide is ApproachFrom.Left ? -1 : 1; + if (at.IsFinite) + yield return (at + Rational.Create(sign, 1_000_000_000), at + Rational.Create(sign, 1_000_000)); + else + yield return (at.IsNegative ? -1000 : 1000, at.IsNegative ? -100 : 100); + } + } + + /// with every symbol but read as one generic positive number. + private static Entity WithGenericSymbols(Entity expr, Variable x) + { + var generic = Rational.Create(3719, 10000); + foreach (var other in expr.Vars.Where(v => v != x).Distinct().ToList()) + expr = expr.Substitute(other, generic); + return expr; + } + + /// + /// Whether the values of at two points beside the destination point in directions that + /// differ, so that it is not a constant times a real function there. + /// https://github.com/asc-community/AngouriMath/issues/2697 + /// + internal static bool TurnsBeside(Entity expr, Variable x, Entity dest, ApproachFrom side) + { + expr = WithGenericSymbols(expr, x); + if (!expr.ContainsNode(x)) + return false; + foreach (var (nearer, further) in PointsBeside(dest, side)) + if (expr.Substitute(x, nearer).Evaled is Complex { IsFinite: true, IsNaN: false, IsZero: false } here + && expr.Substitute(x, further).Evaled is Complex { IsFinite: true, IsNaN: false, IsZero: false } there + && (here / there).Evaled is Complex { IsFinite: true } ratio + && ratio.ImaginaryPart.EDecimal.Abs().CompareTo(ratio.RealPart.EDecimal.Abs().Add(ratio.ImaginaryPart.EDecimal.Abs()) + .Multiply(PeterO.Numbers.EDecimal.FromString("1E-15"))) > 0) + return true; + return false; + } + + /// + /// Whether holds a power to an exponent that moves of a base seen to be negative beside + /// the destination. https://github.com/asc-community/AngouriMath/issues/2697 + /// + internal static bool HasAMovingPowerOfANegativeBaseBeside(Entity expr, Variable x, Entity dest, ApproachFrom side) + { + expr = WithGenericSymbols(expr, x); + foreach (var node in expr.Nodes) + if (node is Powf(var @base, var exponent) && exponent.ContainsNode(x) && @base.ContainsNode(x)) + foreach (var (nearer, further) in PointsBeside(dest, side)) + if (@base.Substitute(x, nearer).Evaled is Real { IsNegative: true } + || @base.Substitute(x, further).Evaled is Real { IsNegative: true }) + return true; + return false; + } + // The sides a limit approaches from: both at a finite point asked two-sidedly, and one otherwise, // since an infinity is approached from one side whichever is asked. internal static ApproachFrom[] Sides(Entity dest, ApproachFrom side) @@ -2201,6 +2274,32 @@ bool VanishesIdentically(Entity polynomial) // differentiated, and the smaller quotient is asked instead. if (WithTheFactorsOnBothSidesCancelled(numRest, denRest, x) is { } cancelled) return ComputeLimit(cancelled, x, dest, side) is { } cancelledLimit ? Times(taken, cancelledLimit) : null; + // The rule is a theorem about real functions. Its proof is Cauchy's mean value theorem, + // which fails for a complex-valued denominator: x / (x + x^2 e^(i/x^2)) at 0 from the + // right tends to 1, while the quotient of the derivatives tends to 0. It holds where the + // denominator's derivative keeps one direction, c h' with h' real: the denominator is + // then c h + d, and the quotient is the real rule's over h, applied to the numerator's + // real and imaginary parts apart (and d vanishes against h where h grows, or is real + // where it vanishes). ln(x) at 0 from the left is ln(abs(x)) + i pi, whose derivative + // 1/x is real. Where the denominator's derivative is seen to turn beside the destination, + // the quotient is read upside down, 1/g over 1/f, which is the same indeterminate form + // and may have a denominator that does not: x^pi / log(x, 2) at 0 from the left, whose + // denominator is ln(2) / (ln(abs(x)) + i pi), is ln(x) / (ln(2) x^(-pi)) that way, and + // the derivative of x^(-pi) keeps its direction. Otherwise it is turned away. + // So, for its cost and not its truth, is a power of a negative base to a moving exponent: + // its derivatives are the same power again, and the rule differentiated them chain after + // chain: x^(x - 1) * x at 0 from the left took 5 s, where it is 1 by its exponent in + // under 1 s. https://github.com/asc-community/AngouriMath/issues/2697 + if (HasAMovingPowerOfANegativeBaseBeside(numRest, x, dest, side) + || HasAMovingPowerOfANegativeBaseBeside(denRest, x, dest, side)) + return null; + if (TurnsBeside(denRest.Differentiate(x), x, dest, side)) + { + var (upsideDownAbove, upsideDownBelow) = ((1 / denRest).InnerSimplified, (1 / numRest).InnerSimplified); + if (TurnsBeside(upsideDownBelow.Differentiate(x), x, dest, side)) + return null; + (numRest, denRest) = (upsideDownAbove, upsideDownBelow); + } // Simplified, because the shape of the quotient is what the // machinery below matches on and differentiation does not // produce it: d/dx ln(x)^2 is 2 * ln(x) * (1 / x), a product diff --git a/Sources/Tests/UnitTests/Calculus/ADirectedInfinityOnANonRealSideTest.cs b/Sources/Tests/UnitTests/Calculus/ADirectedInfinityOnANonRealSideTest.cs new file mode 100644 index 000000000..ab6c108f7 --- /dev/null +++ b/Sources/Tests/UnitTests/Calculus/ADirectedInfinityOnANonRealSideTest.cs @@ -0,0 +1,103 @@ +// +// 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 +{ + /// + /// A limit on a side where the expression is not real, whose modulus grows without bound while its argument + /// settles, is the infinity in that direction, as ln(x) at 0 from the left already is -oo: x^(x - 1) at + /// x = -1e-20 is -1.0E+20 + 3.1i, and its limit there is -oo. Where the argument keeps turning, as pi/x does + /// for x^(1/x) at 0 from the left, there is no direction, and the limit is left unevaluated. Values by mpmath. + /// https://github.com/asc-community/AngouriMath/issues/2697 + /// + [Trait("Area", "Calculus")] + public sealed class ADirectedInfinityOnANonRealSideTest + { + private static readonly Complex PositiveImaginaryInfinity = Complex.Create(Integer.Zero, Real.PositiveInfinity); + private static readonly Complex NegativeImaginaryInfinity = Complex.Create(Integer.Zero, Real.NegativeInfinity); + + // The argument settles at pi, or at 0 from below: sin(x)^(x - 2) at x = -1e-20 is 1.0E+40 - 3.1E+20i. + [Theory] + [InlineData("x^(x - 1)", "-oo")] + [InlineData("(e^x - 1)^(x - 1)", "-oo")] + [InlineData("x^(x - 2)", "+oo")] + [InlineData("sin(x)^(x - 2)", "+oo")] + [InlineData("sin(x)^(x - 1)", "-oo")] + public void Issue2697_TendsToARealDirection(string input, string expected) + => Assert.Equal(MathS.FromString(expected), MathS.FromString(input).Limit("x", 0, ApproachFrom.Left)); + + // The argument settles at -pi/2 or pi/2: x^(x - 1/2) at x = -1e-20 is -3.1E-10 - 1.0E+10i, and + // x^(x - 3/2) is 3.1E+10 + 1.0E+30i. The same directions as sqrt(x) / x at 0 from the left. + [Fact] + public void Issue2697_TendsToAnImaginaryDirection() + { + Assert.Equal(NegativeImaginaryInfinity, MathS.FromString("x^(x - 1/2)").Limit("x", 0, ApproachFrom.Left)); + Assert.Equal(PositiveImaginaryInfinity, MathS.FromString("x^(x - 3/2)").Limit("x", 0, ApproachFrom.Left)); + } + + // x^(1/x) was +oo: its argument pi/x keeps turning, and the modulus of x^(1/x) and x^(2/x) grows + // without a direction. x^(x - 1/3) settles at -pi/3, which no infinity the library writes points along. + [Theory] + [InlineData("x^(1/x)")] + [InlineData("x^(2/x)")] + [InlineData("x^(x - 1/3)")] + public void Issue2697_IsLeftUnevaluatedWithoutADirectionOnAnAxis(string input) + => Assert.IsType(MathS.FromString(input).Limit("x", 0, ApproachFrom.Left)); + + // A root or a logarithm of an argument tending to -oo off the real line takes its value from the side of + // the cut the argument comes from, which is not read: (-x)^(x - 1) at x = 1e-20 is -1.0E+20 - 3.1i, below + // the cut, and its root tends to -ooi, where the root of -oo is +ooi. + [Theory] + [InlineData("sqrt((-x)^(x - 1))", ApproachFrom.Right)] + [InlineData("sqrt(x^(x - 1))", ApproachFrom.Left)] + public void Issue2697_IsLeftUnevaluatedOnTheCut(string input, ApproachFrom side) + => Assert.IsType(MathS.FromString(input).Limit("x", 0, side)); + + // The logarithm of an infinity in any direction is +oo. ln(sqrt(x) / x) at 0 from the left was ln(-ooi). + [Theory] + [InlineData("ln(1 / sqrt(x))", "+oo")] + [InlineData("ln(sqrt(x) / x)", "+oo")] + [InlineData("ln(x^(x - 1/2))", "+oo")] + [InlineData("log(1/2, 1 / sqrt(x))", "-oo")] + public void Issue2697_TheLogarithmOfADirectedInfinity(string input, string expected) + => Assert.Equal(MathS.FromString(expected), MathS.FromString(input).Limit("x", 0, ApproachFrom.Left)); + + // Two sides tending to one infinity, one of them as a power of a negative base to an exponent that moves, + // are not compared, as (-x)^x at 0 is not: x^(x - 1) / x is +oo from each side, and NaN two-sided. + [Theory] + [InlineData("x^(x - 1) / x")] + [InlineData("ln(x^(x - 1))")] + public void Issue2697_ASideThatIsNotRealIsNotComparedOnAnInfinity(string input) + => Assert.Equal(MathS.NaN, MathS.FromString(input).Limit("x", 0, ApproachFrom.BothSides)); + + // l'Hopital's rule is a theorem about real functions: x / (x + x^2 e^(i/x^2)) at 0 from the right tends to 1 + // (1.0 - 7.8E-9i at x = 1e-8, by mpmath), while the quotient of the derivatives tends to 0. + [Theory] + [InlineData("x / (x + x^2 * e^(i / x^2))")] + [InlineData("sin(x) / (x + x^2 * e^(i / x^2))")] + public void Issue2697_LHopitalsRuleIsNotAppliedToAComplexDenominator(string input) + => Assert.NotEqual(Entity.Number.Integer.Zero, MathS.FromString(input).Limit("x", 0, ApproachFrom.Right)); + + // A finite limit on a side that is not real, and a two-sided limit with such a side, are as they were. + [Theory] + [InlineData("x^x", "0", ApproachFrom.Left, "1")] + [InlineData("sin(x)^x", "0", ApproachFrom.Left, "1")] + [InlineData("ln(-x) - ln(x)", "+oo", ApproachFrom.BothSides, "i pi")] + [InlineData("(x - 5)^x / x^x", "-oo", ApproachFrom.BothSides, "e^(-5)")] + [InlineData("x^(x - 1)", "0", ApproachFrom.BothSides, "NaN")] + [InlineData("x^(x - 2)", "0", ApproachFrom.BothSides, "NaN")] + [InlineData("(-x)^x", "0", ApproachFrom.BothSides, "NaN")] + [InlineData("ln(x)", "0", ApproachFrom.Left, "-oo")] + [InlineData("ln(x)", "0", ApproachFrom.BothSides, "-oo")] + public void Issue2697_IsAsItWas(string input, string destination, ApproachFrom side, string expected) + => Assert.Equal(MathS.FromString(expected), MathS.FromString(input).Limit("x", destination, side)); + } +} diff --git a/Sources/Tests/UnitTests/Calculus/AGrownQuotientIsReadWithoutAnotherStepTest.cs b/Sources/Tests/UnitTests/Calculus/AGrownQuotientIsReadWithoutAnotherStepTest.cs index 2c6e28b6d..cdc993cb4 100644 --- a/Sources/Tests/UnitTests/Calculus/AGrownQuotientIsReadWithoutAnotherStepTest.cs +++ b/Sources/Tests/UnitTests/Calculus/AGrownQuotientIsReadWithoutAnotherStepTest.cs @@ -32,9 +32,12 @@ public sealed class AGrownQuotientIsReadWithoutAnotherStepTest 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. + // Left of 0, sqrt(x) is imaginary and the quotient has no real limit to give. It grows in the direction of -i, + // as sqrt(x) / x there does: -1.1 - 1.0E+10i at x = -1e-20, by mpmath. + // https://github.com/asc-community/AngouriMath/issues/2697 [Fact] - public void FromTheLeftNothingChanges() - => Assert.IsType("(sqrt(x) - erf(x)) / x".ToEntity().Limit("x", 0, ApproachFrom.Left)); + public void FromTheLeftItIsTheInfinityInItsDirection() + => Assert.Equal(Entity.Number.Complex.Create(Entity.Number.Integer.Zero, Entity.Number.Real.NegativeInfinity), + "(sqrt(x) - erf(x)) / x".ToEntity().Limit("x", 0, ApproachFrom.Left)); } } diff --git a/Sources/Tests/UnitTests/Calculus/ANonRealSideIsNotReadAsRealTest.cs b/Sources/Tests/UnitTests/Calculus/ANonRealSideIsNotReadAsRealTest.cs index 3478665be..49f77793d 100644 --- a/Sources/Tests/UnitTests/Calculus/ANonRealSideIsNotReadAsRealTest.cs +++ b/Sources/Tests/UnitTests/Calculus/ANonRealSideIsNotReadAsRealTest.cs @@ -15,8 +15,8 @@ namespace AngouriMath.Tests.Calculus /// real on that side, and its limit is not read as though it were. x^(x - 1) at 0 from the left was +oo, where /// its principal value tends to -oo + i pi (-1.0E+20 + 3.1415927i at x = -1e-20, by mpmath). Moved out to +oo /// it is (-1/x)^(-1/x - 1), and Gruntz's algorithm found its leading term e^(-(i pi - x) - x) / w and took the - /// exponential for positive, which it is only for a real exponent: e^(-i pi) is -1. It is left unevaluated, as - /// sin(x)^(x - 1) there already is. + /// exponential for positive, which it is only for a real exponent: e^(-i pi) is -1. Its limit is -oo, the + /// infinity in the direction its argument settles in (#2697). /// /// Two-sided, the comparison of the sides took a side that was NaN to agree with the other, so the same powers /// of a base negative on the right of 0 were the left side's +oo. @@ -25,14 +25,16 @@ namespace AngouriMath.Tests.Calculus [Trait("Area", "Calculus")] public sealed class ANonRealSideIsNotReadAsRealTest { + // Not +oo: the infinity in the direction the argument settles in, which for these is the real axis. + // https://github.com/asc-community/AngouriMath/issues/2697 [Theory] - [InlineData("x^(x - 1)")] - [InlineData("(e^x - 1)^(x - 1)")] - [InlineData("x^(x - 2)")] - [InlineData("x^(2x - 1)")] - [InlineData("tan(x)^(x - 1)")] - public void Issue2693_IsLeftUnevaluatedFromTheLeft(string input) - => Assert.IsType(MathS.FromString(input).Limit("x", 0, ApproachFrom.Left)); + [InlineData("x^(x - 1)", "-oo")] + [InlineData("(e^x - 1)^(x - 1)", "-oo")] + [InlineData("x^(x - 2)", "+oo")] + [InlineData("x^(2x - 1)", "-oo")] + [InlineData("tan(x)^(x - 1)", "-oo")] + public void Issue2693_IsNotReadAsRealFromTheLeft(string input, string expected) + => Assert.Equal(MathS.FromString(expected), MathS.FromString(input).Limit("x", 0, ApproachFrom.Left)); // The same powers from the right, where the base is positive and the power real, are +oo as before. [Theory] diff --git a/Sources/Tests/UnitTests/Calculus/AVanishingBaseToANegativeWholePowerTest.cs b/Sources/Tests/UnitTests/Calculus/AVanishingBaseToANegativeWholePowerTest.cs index 51f2f90e5..7d7192528 100644 --- a/Sources/Tests/UnitTests/Calculus/AVanishingBaseToANegativeWholePowerTest.cs +++ b/Sources/Tests/UnitTests/Calculus/AVanishingBaseToANegativeWholePowerTest.cs @@ -34,13 +34,15 @@ public void TendsToAnInfinity(string input, ApproachFrom side, string expected) public void AnOddPowerHasNoTwoSidedLimit() => Assert.Equal(MathS.NaN, MathS.FromString("sin(x)^(-1)").Limit("x", 0, ApproachFrom.BothSides)); - // An exponent that only tends to a whole number is not one near the point. Under a negative base - // the power is complex there, so it is left alone rather than given the odd power's sign. + // An exponent that only tends to a whole number is not one near the point. Under a negative base the + // power is complex there, and its limit is the infinity in the direction its argument settles in, not + // the odd power's sign read off a real base: sin(x)^(x - 2) at x = -1e-20 is 1.0E+40 - 3.1E+20i and + // sin(x)^(x - 1) is -1.0E+20 + 3.1i, by mpmath. https://github.com/asc-community/AngouriMath/issues/2697 [Theory] - [InlineData("sin(x)^(x - 2)")] - [InlineData("sin(x)^(x - 1)")] - public void AnExponentThatOnlyTendsToAWholeNumberIsLeftAloneUnderANegativeBase(string input) - => Assert.IsType(MathS.FromString(input).Limit("x", 0, ApproachFrom.Left)); + [InlineData("sin(x)^(x - 2)", "+oo")] + [InlineData("sin(x)^(x - 1)", "-oo")] + public void AnExponentThatOnlyTendsToAWholeNumberTendsToADirectedInfinityUnderANegativeBase(string input, string expected) + => Assert.Equal(MathS.FromString(expected), MathS.FromString(input).Limit("x", 0, ApproachFrom.Left)); // Under a base positive on the way in, any exponent tending to a negative number gives +oo. [Theory]