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
20 changes: 20 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,26 @@ where the divisor is 0 on both sides, since one side alone still approaches the
| `"(abs(x) - x) / (abs(x) - x)".ToEntity().Limit("x", 1)` | `1` | `NaN` |
| `"(abs(x) - x) * x / ((abs(x) - x) * sin(x))".ToEntity().Limit("x", 0, ApproachFrom.Right)` | `1` | `NaN` |

### A limit of a derivative is the limit of the function it is

**A limit of `derivative(f, x)` takes the derivative first and the limit of that function second, where it put the
point into `f` and then differentiated a constant.** Master printed `derivative(0, 0)`, differentiating by a number,
and 2.6.0 answered 0 where the derivative grows without bound. The condition differentiating attaches, such as
`provided not x = 0`, is dropped where it holds on each side approached; where it cannot be decided, as for a
symbolic `y` in `x y`, the limit is left unevaluated. A limit inside a limit no longer has its variable replaced by
the outer point, which made `limit(cos(1), 1, 0)`.

| Input | Was (2.6.0) | Was (master) | Now |
|---|---|---|---|
| `"derivative(sqrt(x), x)".ToEntity().Limit("x", 0, ApproachFrom.Right)` | `0` | `derivative(0, 0)` | `+oo` |
| `"derivative(ln(sqrt(x) cos(x)), x)".ToEntity().Limit("x", 0, ApproachFrom.Right)` | `0` | `0` | `+oo` |
| `"derivative(sin(x)/x, x)".ToEntity().Limit("x", 0)` | `NaN` | `derivative(1, 0)` | `0` |
| `"derivative(ln(x), x)".ToEntity().Limit("x", 0, ApproachFrom.Right)` | `0` | `+oo` | `+oo` |
| `"derivative(abs(x), x)".ToEntity().Limit("x", 0, ApproachFrom.Right)` | `1` | `derivative(0, 0)` | `1` |
| `"derivative(x^3, x, 2)".ToEntity().Limit("x", 1)` | `+oo` | `6` | `6` |
| `"derivative(ln(x y), y)".ToEntity().Limit("x", 0, ApproachFrom.Right)` | `1 / y` | `0` | unevaluated |
| `"limit(sqrt(x) cos(x)/x, x, 0)".ToEntity().Limit("x", 1, ApproachFrom.Right)` | `NaN` | `limit(cos(1), 1, 0)` | unevaluated |

### A complex constant does not make the two sides of a limit differ

**A two-sided limit with a complex constant in it is answered where each side is, where it was NaN.** Where the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -741,14 +741,14 @@ partial record IncompleteGammaf
}
}

// A derivative that closes is taken before the limit (WithTheDerivativesTaken). One that does not is not
// read through its parts: the limit of what is differentiated says nothing about the limit of its derivative,
// and the variable differentiated by is not a value to put a limit in. derivative(sin(x) / x, x) at 0 came
// back derivative(1, 0).
partial record Derivativef
{
internal override Entity? ComputeLimitDivideEtImpera(Variable x, Entity dist, ApproachFrom side) =>
ComputeLimitImpl(this, x, dist, side) is { } lim ? lim
: ComputeLimitImpl(New(
Expression.ComputeLimitDivideEtImpera(x, dist, side) is { IsFinite: true } lim1 ? lim1 : Expression,
Var.ComputeLimitDivideEtImpera(x, dist, side) is { IsFinite: true } lim2 ? lim2 : Var),
x, dist, side);
ComputeLimitImpl(this, x, dist, side);
}

partial record Integralf
Expand All @@ -765,16 +765,24 @@ Range is var (from, to)
x, dist, side);
}

// The variable a limit is taken in is bound by it. Where that is the variable approaching, the inner limit does
// not depend on it, and is its own limit; it is never a value to put a limit in: limit(sqrt(x) cos(x) / x, x, 0)
// at 1 came back limit(cos(1), 1, 0).
partial record Limitf
{
internal override Entity? ComputeLimitDivideEtImpera(Variable x, Entity dist, ApproachFrom side) =>
ComputeLimitImpl(this, x, dist, side) is { } lim ? lim
: ComputeLimitImpl(New(
internal override Entity? ComputeLimitDivideEtImpera(Variable x, Entity dist, ApproachFrom side)
{
if (ComputeLimitImpl(this, x, dist, side) is { } lim)
return lim;
if (Var == x)
return Destination.ContainsNode(x) ? null : this;
return ComputeLimitImpl(New(
Expression.ComputeLimitDivideEtImpera(x, dist, side) is { IsFinite: true } lim1 ? lim1 : Expression,
Var.ComputeLimitDivideEtImpera(x, dist, side) is { IsFinite: true } lim2 ? lim2 : Var,
Var,
Destination.ComputeLimitDivideEtImpera(x, dist, side) is { IsFinite: true } lim3 ? lim3 : Destination,
ApproachFrom),
x, dist, side);
}
}

partial record Signumf
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,8 @@ internal static T WithinABudget<T>(int limits, System.Func<T> compute)
// may only fire while the approach is in scope is otherwise declining through most of
// the work -- which is not a wrong answer but is a slower one, since what it declines
// to collapse the search then has to explore.
if (expr.Nodes.Any(static node => node is Derivativef))
expr = WithTheDerivativesTaken(expr, x, dest, side);
var enclosingApproach = EnterApproach(x, dest);
var (outerComparing, outerRoot) = (comparingSides, descentRoot);
(comparingSides, descentRoot) = (false, null);
Expand All @@ -419,6 +421,33 @@ internal static T WithinABudget<T>(int limits, System.Func<T> compute)
}
}

/// <summary>
/// <paramref name="expr"/> with each derivative node that differentiates in closed form replaced by what
/// it differentiates to.
/// </summary>
/// <remarks>
/// A derivative is a function of the point it is taken at, and its limit is that function's limit, not the
/// derivative of the limit. Read as a node, the point was put in before differentiating: derivative(ln(x y), y)
/// at 0 became the derivative of the constant ln(0) and was answered 0, where it is 1 / y; and
/// derivative(sqrt(x), x) at 0 put 0 in for the variable too, and came back derivative(0, 0). A derivative
/// that does not close is left as it is.
/// <para/>
/// What it differentiates to is inner-simplified. Differentiating states where the derivative exists, as
/// 1 / x provided not x = 0 for ln(x), and a limit is taken on the way in and not at the point: the condition is
/// dropped where it holds on each side approached, and kept, which the limit declines, where it does not.
/// In a method of its own, so that the closure is not allocated for every limit.
/// </remarks>
private static Entity WithTheDerivativesTaken(Entity expr, Variable x, Entity dest, ApproachFrom side)
=> expr.Replace(node =>
node is Derivativef(var expression, Variable variable, var iterations) && iterations > 0
&& expression.Differentiate(variable, iterations).InnerSimplified is var taken
&& !taken.Nodes.Any(static inner => inner is Derivativef)
? taken is Providedf(var value, var condition)
&& Sides(dest, side).All(oneSide => HoldsNear(condition, x, dest, oneSide) is true)
? value
: taken
: node);

private static Entity? ComputeLimitWithinApproach(Entity expr, Variable x, Entity dest, ApproachFrom side, bool acceptNaN)
{
// A piecewise is not continuous and is still something a limit can be taken of: it
Expand Down
45 changes: 45 additions & 0 deletions Sources/Tests/UnitTests/Calculus/ALimitOfABinderTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//
// 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;

namespace AngouriMath.Tests.Calculus
{
/// <summary>
/// A limit of a derivative is the limit of the function the derivative is, and the variable a derivative or a limit
/// is taken in is not a value to put a limit in. Read as nodes, derivative(ln(x y), y) at 0 became the derivative of
/// the constant ln(0) and was answered 0, derivative(sqrt(x), x) at 0 came back derivative(0, 0), and
/// limit(sqrt(x) cos(x) / x, x, 0) at 1 came back limit(cos(1), 1, 0). Found by probing a slow limit; no issue
/// was filed.
/// </summary>
[Trait("Area", "Calculus")]
public sealed class ALimitOfABinderTest
{
[Theory]
[InlineData("derivative(sqrt(x), x)", "0", ApproachFrom.Right, "+oo")]
[InlineData("derivative(ln(sqrt(x) * cos(x)), x)", "0", ApproachFrom.Right, "+oo")]
[InlineData("derivative(abs(x), x)", "0", ApproachFrom.Right, "1")]
[InlineData("derivative(sin(x) / x, x)", "0", ApproachFrom.BothSides, "0")]
[InlineData("derivative(ln(x), x)", "0", ApproachFrom.Left, "-oo")]
[InlineData("derivative(x^3, x, 2)", "1", ApproachFrom.BothSides, "6")]
public void ADerivativeIsTakenBeforeTheLimit(string input, string destination, ApproachFrom side, string expected)
=> Assert.Equal(MathS.FromString(expected), MathS.FromString(input).Limit("x", destination, side));

// 1 / y, where x y is not 0; whether it is not 0 near x = 0 depends on y, so the limit is left alone, not 0.
[Fact]
public void ADerivativeByAnotherVariableIsNotTheDerivativeOfAConstant()
=> Assert.IsType<Limitf>(MathS.FromString("derivative(ln(x * y), y)").Limit("x", 0, ApproachFrom.Right));

// The inner limit binds x, so it does not depend on the outer one; its value, if any, is not limit(cos(1), 1, 0).
[Fact]
public void ALimitInItsOwnVariableIsNotSubstitutedInto()
=> Assert.DoesNotContain(MathS.FromString("limit(sqrt(x) * cos(x) / x, x, 0)").Limit("x", 1, ApproachFrom.Right).Nodes,
node => node is Limitf { Var: not Variable });
}
}
Loading