diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index c60952e70..8951d8dec 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -107,6 +107,30 @@ filed. | `"floor(log(x - 1, x))".ToEntity().Limit("x", 1, ApproachFrom.Right)` | `limitright(floor(log(x - 1, x)), x, 1)` | `-1` | | `"floor(x / log(x, x))".ToEntity().Limit("x", 0, ApproachFrom.Left)` | `limitleft(floor(x / log(x, x)), x, 0)` | `-1` | +### The sign of a limit is read through a change of variable + +**Silent: in a limit, where the variable is only inside one part `u` of an expression `h(u)`, the expression's sign near +the destination is read as that of `h(t)` as `t` tends to the limit of `u` from the side `u` arrives from.** The sign of +something tending to 0 at an infinity was read through `1/x` at 0, where `sinh(e^(-1/x))` is flat -- every derivative +of `e^(-1/x)` vanishes at 0 -- so `sgn`, `floor` and `abs` of such an argument were left unevaluated. Read through +`u = e^x`, it is `(e^t - e^-t) / 2` at 0 from the right, which has a slope. The same reading settles a step function at +a jump when the argument arrives at it through an inner function, as `floor(tanh(x))` at -oo does. Found by limitcheck; +no issue was filed. + +| Input | Was (2.6.0) | Now | +|---|---|---| +| `"sgn(sinh(e^x))".ToEntity().Limit("x", "-oo")` | `limit(sgn((e ^ e ^ x - e ^ (-e ^ x)) / 2), x, -oo)` | `1` | +| `"sgn(sin(1/x))".ToEntity().Limit("x", "-oo")` | `limit(sgn(sin(1 / x)), x, -oo)` | `-1` | +| `"sgn(tan(x^pi))".ToEntity().Limit("x", 0, ApproachFrom.Right)` | `limitright(sgn(tan(x ^ pi)), x, 0)` | `1` | +| `"floor(tanh(x))".ToEntity().Limit("x", "-oo")` | `limit(floor((e ^ (2 * x) - 1) / (e ^ (2 * x) + 1)), x, -oo)` | `-1` | +| `"floor(tanh(1/x))".ToEntity().Limit("x", 0, ApproachFrom.Left)` | `limitleft(floor((e ^ (2 * 1 / x) - 1) / (e ^ (2 * 1 / x) + 1)), x, 0)` | `-1` | +| `"floor(2 - 1/x)".ToEntity().Limit("x", "+oo")` | `limit(floor(2 - 1 / x), x, +oo)` | `1` | +| `"floor(2 - 1/x)".ToEntity().Limit("x", "-oo")` | `limit(floor(2 - 1 / x), x, -oo)` | `2` | +| `"floor(e^x - 3)".ToEntity().Limit("x", "-oo")` | `limit(floor(e ^ x - 3), x, -oo)` | `-3` | +| `"floor(abs(x * x))".ToEntity().Limit("x", 0)` | `limit(floor(abs(x * x)), x, 0)` | `0` | +| `"floor(arcsin(log(x, pi)))".ToEntity().Limit("x", 0, ApproachFrom.Right)` | `limitright(floor(arcsin(log(x, pi))), x, 0)` | `-1` | +| `"floor(erf(x^pi))".ToEntity().Limit("x", 0, ApproachFrom.Right)` | `limitright(floor(erf(x ^ pi)), x, 0)` | `0` | + ### A limit of a quotient over a quotient is read as a quotient of products **Silent: in a limit, `a / (b / c)` with `b` constant is read first as `a c / b`, and that reading is kept where it gives diff --git a/Sources/AngouriMath/Functions/Continuous/Limits/Transformations.cs b/Sources/AngouriMath/Functions/Continuous/Limits/Transformations.cs index fdfeba3fd..f64f938b8 100644 --- a/Sources/AngouriMath/Functions/Continuous/Limits/Transformations.cs +++ b/Sources/AngouriMath/Functions/Continuous/Limits/Transformations.cs @@ -1233,9 +1233,36 @@ private static ApproachFrom[] Sides(Entity dest, ApproachFrom side) // over, and sgn(sinh(e^x)) at -oo asked it. The factors are read at the infinity. if (dest.Evaled is Real { IsFinite: false, IsNaN: false } infinity) return SignFromVanishingOrder(expr.Substitute(x, infinity.IsNegative ? -1 / x : 1 / x), x, Integer.Zero, ApproachFrom.Right) - ?? SignOfTheFactors(expr, x, dest, side); + ?? SignOfTheFactors(expr, x, dest, side) ?? SignThroughAChangeOfVariable(expr, x, dest, side); return SignFromVanishingOrder(expr, x, dest, side) ?? SignFromTheSlope(expr, x, dest, side) - ?? SignOfTheFactors(expr, x, dest, side); + ?? SignOfTheFactors(expr, x, dest, side) ?? SignThroughAChangeOfVariable(expr, x, dest, side); + } + + // Where the variable is only ever inside one part u, the expression is h(u), and near the destination it has the + // sign h(t) has as t tends to the limit of u, from the side u arrives from. sinh(e^x) at -oo is (e^t - e^-t) / 2 + // at 0 from the right, which has a slope; read at -oo itself, through 1/x, it is flat, every derivative of + // e^(-1/x) vanishing at 0, so nothing had a sign for it. u is a proper part, and the side u arrives from is the + // sign of u - L, which is skipped where it is this very expression: ln(1 + e^x) at -oo asks it of (1 + e^x) - 1, + // whose own u - L is (1 + e^x) - 1 again, and that recursion overflowed the stack. Its next part, e^x, answers. + private static int? SignThroughAChangeOfVariable(Entity expr, Variable x, Entity dest, ApproachFrom side) + { + var t = Variable.CreateTemp(expr.Vars); + foreach (var part in expr.Nodes) + { + if (ReferenceEquals(part, expr) || part == x || !part.ContainsNode(x) + || expr.Substitute(part, t) is not { } outer || outer.ContainsNode(x)) + continue; + if (EvalAssumingContinuous(part.Limit(x, dest, side)) is not Real { IsNaN: false } limit) + return null; + if (!limit.IsFinite) + return SignNear(outer, t, limit, ApproachFrom.Left); + if (part - limit is var offset && offset == expr) + continue; + return SignNear(offset, x, dest, side) is { } arrivesFrom + ? SignNear(outer, t, limit, arrivesFrom > 0 ? ApproachFrom.Right : ApproachFrom.Left) + : null; + } + return null; } // A product or a quotient has the product of its parts' signs: sqrt(x) * x at 0 from the diff --git a/Sources/Tests/UnitTests/Calculus/ASignThroughAChangeOfVariableTest.cs b/Sources/Tests/UnitTests/Calculus/ASignThroughAChangeOfVariableTest.cs new file mode 100644 index 000000000..657593877 --- /dev/null +++ b/Sources/Tests/UnitTests/Calculus/ASignThroughAChangeOfVariableTest.cs @@ -0,0 +1,49 @@ +// +// 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 AngouriMath.Extensions; +using Xunit; + +namespace AngouriMath.Tests.Calculus +{ + /// + /// The sign of an expression that tends to 0 at an infinity was read through 1/x, at 0 from the right, and + /// sinh(e^(-1/x)) is flat there: every derivative of e^(-1/x) vanishes at 0, so sgn(sinh(e^x)) at -oo was left + /// unevaluated. Where the variable is only inside one part u, the expression is h(u), and its sign is that of h(t) + /// as t tends to the limit of u from the side u arrives from: (e^t - e^-t) / 2 at 0 from the right is positive. + /// Found by limitcheck; no issue was filed. + /// + [Trait("Area", "Calculus")] + public sealed class ASignThroughAChangeOfVariableTest + { + [Theory] + [InlineData("sgn(sinh(e^x))", "-oo", "1")] + [InlineData("sgn(-sinh(e^x))", "-oo", "-1")] + [InlineData("sgn(sin(e^x))", "-oo", "1")] + [InlineData("sgn(sin(e^(-x)))", "+oo", "1")] + [InlineData("sgn(cos(e^x) - 1)", "-oo", "-1")] + [InlineData("sgn(1 - cosh(1/x))", "+oo", "-1")] + // ln(1 + u) - u is -u^2 / 2 and less near u = 0. + [InlineData("sgn(ln(1 + e^x) - e^x)", "-oo", "-1")] + [InlineData("floor(cos(e^x))", "-oo", "0")] + [InlineData("floor(-sin(e^x))", "-oo", "-1")] + public void IsTheSignOfTheOuterFunctionNearTheInnerLimit(string input, string destination, string expected) + => Assert.Equal(expected.ToEntity(), input.ToEntity().Limit("x", destination, ApproachFrom.BothSides)); + + // The side 1 + e^x arrives at 1 from is the sign of (1 + e^x) - 1, whose own part minus its limit is the same + // expression; asking it again overflowed the stack, which a test process does not survive. + [Fact] + public void APartWhoseOffsetIsTheExpressionItselfIsPassedOver() + => Assert.Equal("1".ToEntity(), "sgn(ln(1 + e^x))".ToEntity().Limit("x", "-oo", ApproachFrom.BothSides)); + + // sin(x) at +oo has no limit and keeps no sign; nothing here gives it one. + [Fact] + public void AnOscillatingArgumentIsStillDeclined() + => Assert.IsType("sgn(sin(x))".ToEntity().Limit("x", "+oo", ApproachFrom.BothSides)); + } +}