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
15 changes: 15 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/// <summary>
/// The sign an expression tending to 0 keeps near a finite destination, read off the
/// limit of its slope from the destination, <c>g(x) / (x - a)</c>, or
/// <see langword="null"/> where that limit is 0 or not a real number.
/// </summary>
/// <remarks>
/// Where the slope tends to a non-zero L, infinities included, it has the sign of L near
/// a, and <c>x - a</c> 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 <see cref="SignFromVanishingOrder"/> cannot: <c>sqrt(x)</c> at 0, whose
/// derivative diverges there, has the slope <c>1 / sqrt(x)</c>, which tends to +oo, and
/// so <c>floor(sqrt(x))</c> at 0 from the right is 0, where it was not answered.
/// <para/>
/// A sign is a question about a real value, and the slope of a complex one can still tend to
/// a real infinity: <c>x ^ tanh(pi)</c> on the left of 0 is <c>|x| ^ tanh(pi)</c> times
/// <c>e ^ (i pi tanh(pi))</c>, 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 <c>sqrt(x)</c>'s does on the right of 0 and
/// not on the left. Its real domain condition does not say this: that of <c>x ^ tanh(pi)</c>
/// is true. Each base is a part of the expression, so asking about it does not come back here
/// with the same question.
/// </remarks>
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;
}

/// <summary>
Expand Down
46 changes: 46 additions & 0 deletions Sources/Tests/UnitTests/Calculus/ASignFromTheSlopeTest.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// 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. <c>sqrt(x)</c> at 0 has a derivative that diverges
/// there, so <c>floor(sqrt(x))</c> at 0 from the right was not answered, where it is 0 all the way in. The slope
/// <c>g(x) / (x - a)</c> settles it: <c>1 / sqrt(x)</c> tends to +oo, so <c>sqrt(x)</c> is positive on the right.
/// Found by limitcheck at <c>DEPTH=3</c>, <c>SEED=59</c>; no issue was filed.
/// </summary>
[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<Entity.Number.Integer>(MathS.FromString(input).Limit("x", destination, side));
}
}
Loading