Skip to content

Commit fdc586f

Browse files
Take a factor continuous at the point out before l'Hopital's rule (#2370)
sin(x)^tanh(x) at 0 from the right was left unevaluated, where it is 1, and tan(2x)^sin(tanh(x)) declined after thirteen seconds (2.6.0 does not finish it in two minutes). Read through its exponent, the power is a quotient the rule takes, and the rule differentiated every factor of both parts, including those that tend to a number other than 0, such as the 1 - tanh(x)^2 that tanh's derivative brings. With tanh written through exponentials, each such factor doubled the derivative, and it outgrew the rule's bound. A factor that is continuous at a finite destination and has a finite, non-zero real value there is now taken out as that value, kept exact: lim c(x) f(x) / g(x) is c(a) lim f(x) / g(x). Only a factor real for real x and with no step in it, so that its value at the point is its limit from either side. A limit that is an infinity stays that infinity. limitcheck at DEPTH=3, 18 seeds 14-45, master against this: 10 more compared over 6 seeds, 4 fewer that did not finish in 5 s, and nothing moved to another value or to NaN. Suite 18982 passed, AOT and the eight harnesses clean. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 4c203f0 commit fdc586f

3 files changed

Lines changed: 111 additions & 3 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,21 @@ positive on both sides. `x^x` is still `NaN` at 0.
512512
| `"(x^2)^x".ToEntity().Limit("x", 0)` | `NaN` | `1` |
513513
| `"(x*x)^(x - 1)".ToEntity().Limit("x", 0)` | `NaN` | `+oo` |
514514

515+
### A factor continuous at the point is taken out before l'Hopital's rule
516+
517+
**`sin(x)^tanh(x)` at 0 from the right is `1`, where it was left unevaluated.** Read through its exponent, the power is
518+
a quotient l'Hopital's rule takes. The rule differentiated every factor of both parts, including the ones that tend to
519+
a number other than 0, such as the `1 - tanh(x)^2` the derivative of tanh brings. With tanh written through
520+
exponentials, each such factor doubled the derivative, and the result outgrew the rule's bound. A factor that is
521+
continuous at a finite point and has a non-zero real value there is now taken out as that value, kept exact:
522+
lim c(x) f(x) / g(x) is c(a) lim f(x) / g(x).
523+
524+
| Input | Was (2.6.0) | Now |
525+
|---|---|---|
526+
| `"sin(x)^tanh(x)".ToEntity().Limit("x", 0, ApproachFrom.Right)` | `limitright(sin(x) ^ ((e ^ (2 * x) - 1) / (e ^ (2 * x) + 1)), x, 0)` | `1` |
527+
| `"tan(x)^sin(tanh(x))".ToEntity().Limit("x", 0, ApproachFrom.Right)` | `limitright(tan(x) ^ sin((e ^ (2 * x) - 1) / (e ^ (2 * x) + 1)), x, 0)` | `1` |
528+
| `"tan(2*x)^sin(tanh(x))".ToEntity().Limit("x", 0, ApproachFrom.Right)` | did not finish in 2 minutes; unevaluated after 13 s on the previous master | `1` |
529+
515530
### A 0^0 limit whose l'Hopital chain doubles its powers has its value
516531

517532
**Silent: a different answer.** l'Hopital's rule was turned away where a

‎Sources/AngouriMath/Functions/Continuous/Limits/Transformations.cs‎

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1573,6 +1573,42 @@ bool DoesNotSettle(Entity argument) => argument.ContainsNode(x)
15731573
&& EvalAssumingContinuous(argument.Limit(x, dest, side)) is not Real { IsFinite: true };
15741574
}
15751575

1576+
/// <summary>
1577+
/// The factors of <paramref name="expr"/> that are continuous at a finite destination with a
1578+
/// finite non-zero value there, as that value, and the product of the rest.
1579+
/// </summary>
1580+
/// <remarks>
1581+
/// l'Hopital's rule differentiates both parts of the quotient, and a factor that tends to a
1582+
/// number other than 0 only makes the derivative larger: lim c(x) f(x) / g(x) is c(a) lim
1583+
/// f(x) / g(x). The product rule doubles it each time round, and with tanh written through
1584+
/// exponentials (1 - tanh(x)^2) * x * (1 + x^2) / tanh(x) at 0 from the right did not finish
1585+
/// in a minute, where x * (1 + x^2) / tanh(x) takes a tenth of a second. Only a factor real
1586+
/// for real x and with no step in it, so that its value at the point is its limit from
1587+
/// either side.
1588+
/// </remarks>
1589+
private static (Entity Value, Entity Others) TakeOutContinuousFactors(Entity expr, Variable x, Entity dest)
1590+
{
1591+
if (expr is not Mulf || !dest.IsFinite)
1592+
return (Integer.One, expr);
1593+
Entity value = Integer.One, rest = Integer.One;
1594+
var any = false;
1595+
foreach (var factor in Mulf.LinearChildren(expr))
1596+
{
1597+
if (factor.ContainsNode(x)
1598+
&& !factor.Nodes.Any(static node => node is Floorf or Ceilf or Roundf or Signumf or Piecewise)
1599+
&& TreeAnalyzer.IsRealValued(factor, x)
1600+
&& factor.Substitute(x, dest) is var there
1601+
&& there.Evaled is Real { IsFinite: true, IsNaN: false, IsZero: false })
1602+
{
1603+
value *= there;
1604+
any = true;
1605+
}
1606+
else
1607+
rest *= factor;
1608+
}
1609+
return any && rest.ContainsNode(x) ? (value.InnerSimplified, rest.InnerSimplified) : (Integer.One, expr);
1610+
}
1611+
15761612
private static Entity? ApplylHopitalRuleImpl(Entity expr, Variable x, Entity dest, ApproachFrom side)
15771613
{
15781614
if (expr is not Divf && AsQuotient(expr, x, dest, side) is { } quotient)
@@ -1585,12 +1621,15 @@ bool DoesNotSettle(Entity argument) => argument.ContainsNode(x)
15851621
if (num.ContainsNode(x) && den.ContainsNode(x)
15861622
&& !HasAStepThatJumpsNear(expr, x, dest, side))
15871623
{
1624+
var (numValue, numRest) = TakeOutContinuousFactors(num, x, dest);
1625+
var (denValue, denRest) = TakeOutContinuousFactors(den, x, dest);
1626+
var taken = (numValue / denValue).InnerSimplified;
15881627
// Simplified, because the shape of the quotient is what the
15891628
// machinery below matches on and differentiation does not
15901629
// produce it: d/dx ln(x)^2 is 2 * ln(x) * (1 / x), a product
15911630
// with a quotient inside rather than the quotient
15921631
// 2 * ln(x) / x, and only the second has a limit here.
1593-
var applied = (num.Differentiate(x) / den.Differentiate(x)).Simplify();
1632+
var applied = (numRest.Differentiate(x) / denRest.Differentiate(x)).Simplify();
15941633
// The domain condition simplification leaves behind is what the
15951634
// derivative of a logarithm or a root carries, and a Providedf is
15961635
// not a continuous node, so the quotient would be turned away
@@ -1614,18 +1653,25 @@ bool DoesNotSettle(Entity argument) => argument.ContainsNode(x)
16141653
// was turned away, so cos(x) * sinh(x) / sin(x) from the right at 0 was
16151654
// left unevaluated, and ln(sin(x)) / ln(sinh(x)) with it.
16161655
if (GrewTooMuch(expr, applied))
1617-
return ItsValueAt(applied, x, dest);
1656+
return ItsValueAt(applied, x, dest) is { } value ? Times(taken, value) : null;
16181657
lHopitalApplications++;
16191658
MultithreadingFunctional.ExitIfCancelled();
16201659
lHopitalChain!.Add(applied);
16211660
try
16221661
{
16231662
if (ComputeLimit(applied, x, dest, side) is { } resLim)
1624-
return resLim;
1663+
return Times(taken, resLim);
16251664
}
16261665
finally { lHopitalChain.RemoveAt(lHopitalChain.Count - 1); }
16271666
}
16281667
return null;
1668+
// A limit times the value taken out: unchanged where nothing was, exact where the limit is
1669+
// finite, and the infinity itself where it is one, since a finite non-zero real times +oo
1670+
// is +oo or -oo and a caller compares against that.
1671+
static Entity Times(Entity taken, Entity limit)
1672+
=> taken == Integer.One ? limit
1673+
: limit.Evaled is Real { IsFinite: false, IsNaN: false } && (taken * limit).Evaled is Real { IsNaN: false } infinity ? infinity
1674+
: (taken * limit).InnerSimplified;
16291675
}
16301676

16311677
/// <summary>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//
2+
// Copyright (c) 2019-2026 Angouri.
3+
// AngouriMath is licensed under MIT.
4+
// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.
5+
// Website: https://am.angouri.org.
6+
//
7+
8+
using AngouriMath.Core;
9+
using Xunit;
10+
11+
namespace AngouriMath.Tests.Calculus
12+
{
13+
/// <summary>
14+
/// <c>sin(x)^tanh(x)</c> at 0 from the right was left unevaluated, where it is 1, and <c>tan(2x)^sin(tanh(x))</c>
15+
/// declined after thirteen seconds. Read through its exponent, the power is a quotient l'Hopital's rule takes, and the
16+
/// rule differentiated every factor of both parts, including those that tend to a number other than 0, such as the
17+
/// <c>1 - tanh(x)^2</c> the derivative of tanh brings. With tanh written through exponentials, each such factor
18+
/// doubled the derivative, which outgrew the rule's bound. A factor that is continuous at a finite point with a non-zero
19+
/// value there is now taken out as that value: lim c(x) f(x) / g(x) is c(a) lim f(x) / g(x). Found by limitcheck at
20+
/// <c>DEPTH=3</c>, <c>SEED=42</c>; no issue was filed.
21+
/// </summary>
22+
[Trait("Area", "Calculus")]
23+
public sealed class AContinuousFactorIsNotDifferentiatedTest
24+
{
25+
[Theory]
26+
[InlineData("sin(x)^tanh(x)")]
27+
[InlineData("tan(x)^sin(tanh(x))")]
28+
[InlineData("tan(2*x)^sin(tanh(x))")]
29+
public void IsOneFromTheRight(string input)
30+
=> Assert.Equal(MathS.FromString("1"), MathS.FromString(input).Limit("x", 0, ApproachFrom.Right));
31+
32+
// The value taken out is kept exact, and an infinity it multiplies is still that infinity.
33+
[Theory]
34+
[InlineData("tanh(1 + x) * (x - 1) / (x^2 - 1)", "1", "tanh(2) / 2")]
35+
[InlineData("(2 + cos(x)) * x / sin(x)", "0", "3")]
36+
[InlineData("(2 + cos(x)) / sin(x)^2", "0", "+oo")]
37+
public void KeepsTheValueTakenOut(string input, string destination, string expected)
38+
{
39+
var limit = MathS.FromString(input).Limit("x", destination);
40+
var wanted = MathS.FromString(expected);
41+
if (wanted.Evaled is Entity.Number { IsFinite: false })
42+
Assert.Equal(wanted, limit);
43+
else
44+
Assert.Equal(Entity.Number.Integer.Zero, (limit - wanted).Simplify());
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)