diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index 50161c114..40d064d4a 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -93,6 +93,21 @@ root near 23. | `"x^7 - 5x^6 + 1 = 0".Solve("x")` | six roots, none of them near 5 | seven roots | | `"x^4 - 3*x^(5/2) + 2 = 0".Solve("x")` | `{ 1, 4.8870…^(2/5) }` | four roots: `1`, `1.3734…^2`, and two off the real line | +### A step function reads the side its argument arrives from off the slope + +**A one-sided limit of a floor or a ceiling whose argument arrives at a jump like a root is answered, where it was +not.** Which side of the jump the argument comes from was read only off the first derivative that does not vanish, +and `sqrt(x)` at 0 has none that is finite. It is now also read off the slope `g(x) / (x - a)`, which for `sqrt(x)` +is `1 / sqrt(x)` and tends to +oo, so `sqrt(x)` is above 0 on the right. + +| Input | Was (2.6.0) | Now | +|---|---|---| +| `"floor(sqrt(x))".ToEntity().Limit("x", 0, ApproachFrom.Right)` | `limitright(floor(sqrt(x)), x, 0)` | `0` | +| `"ceil(sqrt(x))".ToEntity().Limit("x", 0, ApproachFrom.Right)` | `limitright(ceil(sqrt(x)), x, 0)` | `1` | +| `"floor(x - sqrt(x))".ToEntity().Limit("x", 0, ApproachFrom.Right)` | `limitright(floor(x - sqrt(x)), x, 0)` | `-1` | +| `"floor(1 - sqrt(1 - x))".ToEntity().Limit("x", 1, ApproachFrom.Left)` | `limitleft(floor(1 - sqrt(1 - x)), x, 1)` | `0` | +| `"floor(sqrt(x)) - (x^3)^(x/2)".ToEntity().Limit("x", 0, ApproachFrom.Right)` | `NaN` | `-1` | + ### An infinity in an expression is not read as a coefficient **Wrong answer: `log(x, 0)` at 0 from the right is `+oo`, where it was `0`.** `log(x, 0)` is `ln(0) / ln(x)`, and diff --git a/Sources/AngouriMath/Functions/Continuous/Limits/Transformations.cs b/Sources/AngouriMath/Functions/Continuous/Limits/Transformations.cs index 69faf2f5a..5715fad41 100644 --- a/Sources/AngouriMath/Functions/Continuous/Limits/Transformations.cs +++ b/Sources/AngouriMath/Functions/Continuous/Limits/Transformations.cs @@ -1193,7 +1193,41 @@ internal static Entity AtALargeDecimal(Entity expr, Variable x) // those margins keeps it on the same side of zero. if (!limit.IsZero) return limit.IsNegative ? -1 : 1; - return SignFromVanishingOrder(expr, x, dest, side); + return SignFromVanishingOrder(expr, x, dest, side) ?? SignFromTheSlope(expr, x, dest, side); + } + + /// + /// The sign an expression tending to 0 keeps near a finite destination, read off the + /// limit of its slope from the destination, g(x) / (x - a), or + /// where that limit is 0 or not a real number. + /// + /// + /// Where the slope tends to a non-zero L, infinities included, it has the sign of L near + /// a, and x - a is positive on the right and negative on the left, so g has the + /// sign of L on the right and the other sign on the left. This needs no derivative, and + /// answers where cannot: sqrt(x) at 0, whose + /// derivative diverges there, has the slope 1 / sqrt(x), which tends to +oo, and + /// so floor(sqrt(x)) at 0 from the right is 0, where it was not answered. + /// + /// A sign is a question about a real value, and the slope of a complex one can still tend to + /// a real infinity: x ^ tanh(pi) on the left of 0 is |x| ^ tanh(pi) times + /// e ^ (i pi tanh(pi)), and was read as negative. So the expression has to be real + /// throughout the side approached: real wherever it is defined, or real where the bases of its + /// roots are positive and those hold there, which sqrt(x)'s does on the right of 0 and + /// not on the left. Its real domain condition does not say this: that of x ^ tanh(pi) + /// is true. Each base is a part of the expression, so asking about it does not come back here + /// with the same question. + /// + private static int? SignFromTheSlope(Entity expr, Variable x, Entity dest, ApproachFrom side) + { + if (side is not (ApproachFrom.Left or ApproachFrom.Right) || !dest.IsFinite) + return null; + if (!Functions.TreeAnalyzer.IsRealValued(expr, x) + && !(Functions.TreeAnalyzer.WhereRealValued(expr, x) is { } whereReal && HoldsNear(whereReal, x, dest, side) is true)) + return null; + if (EvalAssumingContinuous((expr / (x - dest)).Limit(x, dest, side)) is not Real { IsNaN: false, IsZero: false } slope) + return null; + return slope.IsNegative == (side is ApproachFrom.Left) ? 1 : -1; } /// diff --git a/Sources/Tests/UnitTests/Calculus/ASignFromTheSlopeTest.cs b/Sources/Tests/UnitTests/Calculus/ASignFromTheSlopeTest.cs new file mode 100644 index 000000000..dad63222b --- /dev/null +++ b/Sources/Tests/UnitTests/Calculus/ASignFromTheSlopeTest.cs @@ -0,0 +1,46 @@ +// +// 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 +{ + /// + /// A step function whose argument arrives at a jump asks the sign the argument keeps on the way in, and that sign + /// was read only off the first derivative that does not vanish. sqrt(x) at 0 has a derivative that diverges + /// there, so floor(sqrt(x)) at 0 from the right was not answered, where it is 0 all the way in. The slope + /// g(x) / (x - a) settles it: 1 / sqrt(x) tends to +oo, so sqrt(x) is positive on the right. + /// Found by limitcheck at DEPTH=3, SEED=59; no issue was filed. + /// + [Trait("Area", "Calculus")] + public sealed class ASignFromTheSlopeTest + { + [Theory] + [InlineData("floor(sqrt(x))", "0", ApproachFrom.Right, "0")] + [InlineData("ceil(sqrt(x))", "0", ApproachFrom.Right, "1")] + [InlineData("floor(-sqrt(x))", "0", ApproachFrom.Right, "-1")] + [InlineData("floor(sqrt(x)) - (x^3)^(x/2)", "0", ApproachFrom.Right, "-1")] + [InlineData("floor(sqrt(x) - x)", "0", ApproachFrom.Right, "0")] + [InlineData("floor(x - sqrt(x))", "0", ApproachFrom.Right, "-1")] + [InlineData("floor(1 - sqrt(1 - x))", "1", ApproachFrom.Left, "0")] + [InlineData("ceil(1 - sqrt(1 - x))", "1", ApproachFrom.Left, "1")] + [InlineData("floor(cbrt(x))", "0", ApproachFrom.Right, "0")] + public void ReadsTheSignOffTheSlope(string input, string destination, ApproachFrom side, string expected) + => Assert.Equal(MathS.FromString(expected).Simplify(), + MathS.FromString(input).Limit("x", destination, side).Simplify()); + + // The slope of a complex value can tend to a real infinity: x ^ tanh(pi) on the left of 0 is |x| ^ tanh(pi) + // times e ^ (i pi tanh(pi)), whose sign is not -1. A root's base that is not positive on the side approached + // leaves the sign unread, and a declined limit keeps its node. + [Theory] + [InlineData("sgn(x ^ tanh(pi))", "0", ApproachFrom.Left)] + [InlineData("floor(sqrt(x))", "0", ApproachFrom.Left)] + public void LeavesAComplexValueUnread(string input, string destination, ApproachFrom side) + => Assert.IsNotType(MathS.FromString(input).Limit("x", destination, side)); + } +}