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
14 changes: 14 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,20 @@ 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 |

### l'Hopital's rule cancels any factor written on both sides

**Limits whose derivatives carried a factor written on both sides of the quotient are answered, where
they were NaN in 2.6.0 and left unevaluated after it.** A factor that is not a polynomial, such as the
e^x - e^(-x) of a sinh, is now divided out as a polynomial one is, where it is built from functions
analytic where they are defined and is plainly not 0 at a point.

| Input | Was (2.6.0) | Now |
|---|---|---|
| `"(sinh(x)^3) ^ tan(x)".ToEntity().Limit("x", 0, ApproachFrom.Left)` | `NaN` | `1` |
| `"(sinh(x)^3) ^ tan(x)".ToEntity().Limit("x", 0, ApproachFrom.Right)` | `NaN` | `1` |
| `"x / cos(x) * ln(sinh(x)^3)".ToEntity().Limit("x", 0, ApproachFrom.Right)` | `NaN` | `0` |
| `"sin(x) / cos(x) * ln((e^x - 1)^3)".ToEntity().Limit("x", 0, ApproachFrom.Right)` | `NaN` | `0` |

### l'Hopital's rule leaves out what only makes the derivatives larger

**Limits whose derivatives grew without bound under l'Hopital's rule are answered, where they did not
Expand Down
31 changes: 23 additions & 8 deletions Sources/AngouriMath/Functions/Continuous/Limits/Transformations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1671,26 +1671,32 @@ private static bool IsAPolynomialPower(Entity factor, Variable x)
=> factor.ContainsNode(x) && (factor is Powf(var @base, Integer) ? IsAPolynomialIn(@base, x) : IsAPolynomialIn(factor, x));

/// <summary>
/// <paramref name="numerator"/> over <paramref name="denominator"/> with every factor that
/// is a polynomial in <paramref name="x"/>, not identically 0, and written on both sides to
/// a whole power, divided out by the smaller of the two powers; null where there is none.
/// <paramref name="numerator"/> over <paramref name="denominator"/> with every factor in
/// <paramref name="x"/> that is not identically 0 and is written on both sides to a whole
/// power divided out by the smaller of the two powers; null where there is none.
/// </summary>
/// <remarks>
/// A polynomial that is not identically 0 has finitely many zeros, so it is not 0 anywhere
/// on a punctured neighbourhood of a point or of an infinity close enough in, and the two
/// quotients agree there. Differentiated instead, the factor is carried through the product
/// quotients agree there. A factor analytic where it is defined and not identically 0 has
/// zeros that may gather at the destination but fill no interval, so the quotient with it
/// cancelled agrees with the other wherever the other is defined, and where it has a limit
/// the other has the same one. l'Hopital's rule turns ln(sinh(x)^3) into a quotient with
/// (e^x - e^(-x))^2 above and (e^x - e^(-x))^3 below, and (sinh(x)^3)^tan(x) at 0, which
/// is 1, was left unevaluated from each side for differentiating them. Differentiated instead, the factor is carried through the product
/// rule with everything beside it: (x - pi)^2 / ((x + tanh(x)) * (x - pi)) at +oo took
/// seven seconds, where x^2 / ((x + tanh(x)) * x), whose x the normalisation cancels, took
/// a fortieth of one; and ln(x + tanh(x)) / ln((x - pi)^2), which l'Hopital's rule turns
/// into the first, did not finish.
/// </remarks>
private static Entity? WithThePolynomialFactorsOnBothSidesCancelled(Entity numerator, Entity denominator, Variable x)
private static Entity? WithTheFactorsOnBothSidesCancelled(Entity numerator, Entity denominator, Variable x)
{
var above = Powers(numerator);
var below = Powers(denominator);
var cancelledAny = false;
foreach (var @base in above.Keys.ToList())
if (below.TryGetValue(@base, out var underneath) && IsAPolynomial(@base) && !VanishesIdentically(@base))
if (below.TryGetValue(@base, out var underneath) && @base.ContainsNode(x)
&& (IsAPolynomialIn(@base, x) ? !VanishesIdentically(@base) : IsAnalyticAndNotZero(@base)))
{
var common = Math.Min(above[@base], underneath);
above[@base] -= common;
Expand Down Expand Up @@ -1724,7 +1730,16 @@ static Entity Rebuild(Dictionary<Entity, int> powers)
built *= pair.Value == 1 ? pair.Key : MathS.Pow(pair.Key, pair.Value);
return built.InnerSimplified;
}
bool IsAPolynomial(Entity node) => node.ContainsNode(x) && IsAPolynomialIn(node, x);
// Any other base is cancelled only where it is made of functions analytic where they
// are defined, so that one that is not 0 everywhere has zeros that do not fill an
// interval, and only where it is plainly not 0 at a point: an abs, a sign, a step or
// a piecewise can be 0 on one side and not the other, as abs(x) - x is, and
// sin(x)^2 + cos(x)^2 - 1 is 0 though it may evaluate to a rounding error.
bool IsAnalyticAndNotZero(Entity @base)
=> !@base.Nodes.Any(static node => node is Absf or Signumf or Floorf or Ceilf or Roundf or Piecewise or Providedf)
&& (ClearlyNotZero(@base.Substitute(x, Rational.Create(7, 3))) || ClearlyNotZero(@base.Substitute(x, Rational.Create(-11, 5))));
static bool ClearlyNotZero(Entity value)
=> value.Evaled is Complex { IsFinite: true } number && !number.IsNaN && Complex.Abs(number) > Rational.Create(1, 1000);
// At two points, since a polynomial that is not 0 everywhere may still be 0 at one:
// x - x is not cancelled, as 0 / 0 is not 1.
bool VanishesIdentically(Entity polynomial)
Expand All @@ -1749,7 +1764,7 @@ bool VanishesIdentically(Entity polynomial)
var taken = (numValue / denValue).InnerSimplified;
// A factor written on both sides is divided out rather than
// differentiated, and the smaller quotient is asked instead.
if (WithThePolynomialFactorsOnBothSidesCancelled(numRest, denRest, x) is { } cancelled)
if (WithTheFactorsOnBothSidesCancelled(numRest, denRest, x) is { } cancelled)
return ComputeLimit(cancelled, x, dest, side) is { } cancelledLimit ? Times(taken, cancelledLimit) : null;
// Simplified, because the shape of the quotient is what the
// machinery below matches on and differentiation does not
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//
// 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 System;
using System.Threading.Tasks;
using AngouriMath.Core;
using Xunit;

namespace AngouriMath.Tests.Calculus
{
/// <summary>
/// l'Hopital's rule turns ln(sinh(x)^3) into a quotient with (e^x - e^(-x))^2 above and
/// (e^x - e^(-x))^3 below, and the factor the two share was differentiated with everything else
/// until the rule's budget ran out: (sinh(x)^3)^tan(x) at 0 was left unevaluated from each side,
/// where it is 1. Found by limitcheck at <c>SEED=62</c>, <c>DEPTH=3</c>; no issue was filed.
/// </summary>
[Trait("Area", "Calculus")]
public sealed class LhopitalCancelsAFactorOnBothSidesTest
{
[Theory]
[InlineData("(sinh(x)^3) ^ tan(x)", ApproachFrom.Left, "1")]
[InlineData("(sinh(x)^3) ^ tan(x)", ApproachFrom.Right, "1")]
[InlineData("x / cos(x) * ln(sinh(x)^3)", ApproachFrom.Right, "0")]
[InlineData("sin(x) / cos(x) * ln((e^x - 1)^3)", ApproachFrom.Right, "0")]
public void Finishes(string input, ApproachFrom side, string expected)
{
var limit = Task.Run(() => MathS.FromString(input).Limit("x", 0, side).Simplify());
Assert.True(limit.Wait(TimeSpan.FromSeconds(60)), $"{input} at 0 from the {side} did not finish");
Assert.Equal(MathS.FromString(expected).Simplify(), limit.Result);
}

// A factor that is 0 everywhere but evaluates to a rounding error is not cancelled.
[Fact]
public void DoesNotCancelAFactorThatIsZeroByAnIdentity()
=> Assert.IsType<Entity.Limitf>(MathS.FromString("(sin(x)^2 + cos(x)^2 - 1) * x / ((sin(x)^2 + cos(x)^2 - 1) * x^2)").Limit("x", 0, ApproachFrom.Right));
}
}
Loading