Skip to content

Commit 605f530

Browse files
Read a vanishing factor by its order in l'Hôpital's quotient at a point (#2406)
Read a vanishing factor by its order in l'Hopital's quotient at a point At a finite point the rule reads the quotient it gets term by term, as a value or a pole, but a factor that was a sum or vanished there stopped the reading. ln(cos(x) * sqrt(x)) / (1 / (e^(2x) - 1)) steps to a quotient with x^(-1/2) * cos(x) / 2 - sqrt(x) * sin(x) and (e^(2x) - 1)^2 in it, of orders -1/2 and 2, and was left unevaluated. Such a factor is now read as its leading term: a sum by its terms of the lowest order, a function that vanishes by its first derivative that does not. The reading is also asked before another step, not only once the step has grown: tanh(x) * ln(cos(x) * sqrt(x)) at 0 from the right reached the rule with e^(2x) + 1 taken out, so the step did not count as grown, and the descent through the quotient ran out of time. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 2bfd522 commit 605f530

2 files changed

Lines changed: 117 additions & 2 deletions

File tree

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

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1557,15 +1557,82 @@ bool Factors(Entity expr, Entity power, ref Entity value, ref Entity order)
15571557
case var _ when !expr.ContainsNode(x):
15581558
value *= MathS.Pow(expr, power);
15591559
return true;
1560+
case var _ when ItsValueAt(expr, x, dest) is { } there && there.Evaled is Real { IsZero: false }:
1561+
value *= MathS.Pow(there, power);
1562+
return true;
1563+
// A whole power is its base's order that many times, where c * d^k to the n is c^n * d^(kn) exactly.
1564+
case Powf(var @base, Integer times):
1565+
return Factors(@base, power * times, ref value, ref order);
15601566
default:
1561-
if (ItsValueAt(expr, x, dest) is not { } there || there.Evaled is not Real { IsZero: false })
1567+
if (LeadingTerm(expr) is not var (leadingOrder, leadingValue))
15621568
return false;
1563-
value *= MathS.Pow(there, power);
1569+
order += power * leadingOrder;
1570+
value *= MathS.Pow(leadingValue, power);
15641571
return true;
15651572
}
15661573
}
1574+
1575+
// A factor with no finite non-zero value at the point read as c * d^k, d the distance to it: a sum by its
1576+
// terms of the lowest order, where their coefficients do not cancel, and one that vanishes there, a sum
1577+
// among them, by the first of its derivatives that does not. The product rule leaves both inside one factor:
1578+
// ln(cos(x) * sqrt(x)) / (1 / (e^(2x) - 1)) at 0 from the right steps to
1579+
// -1/2 * (x^(-1/2) * cos(x) / 2 - sqrt(x) * sin(x)) * (e^(2x) - 1)^2 / (sqrt(x) * cos(x) * e^(2x)), whose
1580+
// sum is of order -1/2 and whose e^(2x) - 1 is 2x to the first, so it is of order 1 and tends to 0. A
1581+
// derivative read at the point is the Taylor coefficient only where the function is smooth there, so not
1582+
// through an abs, and a value that is not a finite real at the point stops the reading.
1583+
(Entity Order, Entity Value)? LeadingTerm(Entity expr)
1584+
{
1585+
if (ItsValueAt(expr, x, dest) is { } atThePoint && atThePoint.Evaled is Real { IsZero: true })
1586+
return ByItsDerivatives(expr);
1587+
if (expr is Sumf or Minusf)
1588+
{
1589+
Real? lowest = null;
1590+
var parts = new List<(Real Order, Entity Value)>();
1591+
foreach (var (term, negated) in Terms(expr, false))
1592+
{
1593+
Entity termValue = Integer.One, termOrder = Integer.Zero;
1594+
if (!Factors(term, Integer.One, ref termValue, ref termOrder)
1595+
|| termOrder.Evaled is not Real { IsNaN: false, IsFinite: true } termOrderValue)
1596+
return null;
1597+
parts.Add((termOrderValue, negated ? -termValue : termValue));
1598+
if (lowest is null || termOrderValue < lowest)
1599+
lowest = termOrderValue;
1600+
}
1601+
if (lowest is null)
1602+
return null;
1603+
Entity coefficient = Integer.Zero;
1604+
foreach (var part in parts)
1605+
if (part.Order == lowest)
1606+
coefficient += part.Value;
1607+
return coefficient.Evaled is Real { IsNaN: false, IsFinite: true, IsZero: false }
1608+
? (lowest, coefficient.InnerSimplified) : null;
1609+
}
1610+
return null;
1611+
}
1612+
1613+
// The first derivative that is not 0 at the point, over its factorial, and its order.
1614+
(Entity Order, Entity Value)? ByItsDerivatives(Entity expr)
1615+
{
1616+
if (expr.Nodes.Any(static node => node is Absf))
1617+
return null;
1618+
var derivative = expr;
1619+
Entity factorial = Integer.One;
1620+
for (var k = 1; k <= MaxVanishingOrderRead; k++)
1621+
{
1622+
derivative = derivative.Differentiate(x);
1623+
factorial *= k;
1624+
if (ItsValueAt(derivative, x, dest) is not { } derivativeThere || derivativeThere.Evaled is not Real { IsNaN: false, IsFinite: true } read)
1625+
return null;
1626+
if (!read.IsZero)
1627+
return (Integer.Create(k), (derivativeThere / factorial).InnerSimplified);
1628+
}
1629+
return null;
1630+
}
15671631
}
15681632

1633+
/// <summary>How many derivatives <see cref="ItsValueOrPoleAt"/> takes of a vanishing factor to find its order.</summary>
1634+
private const int MaxVanishingOrderRead = 3;
1635+
15691636
/// <summary>
15701637
/// Whether differentiating both parts has left a quotient bigger than the one it came
15711638
/// from by more than a step on the way to an answer accounts for. Each step asks what
@@ -2087,6 +2154,14 @@ bool VanishesIdentically(Entity polynomial)
20872154
: dest.IsFinite && ItsValueAt(applied.Expand().InnerSimplified, x, dest) is { } expanded ? Times(taken, expanded)
20882155
: dest.IsFinite && ItsValueOrPoleAt(applied, x, dest, side) is { } read ? Times(taken, read)
20892156
: null;
2157+
// At a finite point the term-by-term reading is asked before another step too: it costs a
2158+
// substitution a factor, where the descent through the quotient may cost seconds.
2159+
// tanh(x) * ln(cos(x) * sqrt(x)) at 0 from the right reaches here with e^(2x) + 1 taken
2160+
// out, so what was differentiated is smaller and the step does not count as grown, and the
2161+
// descent through -1/2 * (x^(-1/2) * cos(x) / 2 - sqrt(x) * sin(x)) * (e^(2x) - 1)^2 / ...
2162+
// ran out of time where the reading says 0 at once.
2163+
if (dest.IsFinite && ItsValueOrPoleAt(applied, x, dest, side) is { } readFirst)
2164+
return Times(taken, readFirst);
20902165
lHopitalApplications++;
20912166
MultithreadingFunctional.ExitIfCancelled();
20922167
lHopitalChain!.Add(applied);
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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 AngouriMath.Extensions;
10+
using Xunit;
11+
12+
namespace AngouriMath.Tests.Calculus
13+
{
14+
/// <summary>
15+
/// At a finite point l'Hopital's rule reads the quotient it gets term by term, as a value or a pole, and now reads a
16+
/// factor that is a sum or vanishes there by its leading order: ln(cos(x) * sqrt(x)) / (1 / (e^(2x) - 1)) steps to
17+
/// a quotient with (x^(-1/2) * cos(x) / 2 - sqrt(x) * sin(x)) and (e^(2x) - 1)^2 in it, of orders -1/2 and 2. It also
18+
/// reads it before another step, rather than only once the step has grown. Found by limitcheck; no issue was filed.
19+
/// </summary>
20+
[Trait("Area", "Calculus")]
21+
public sealed class AVanishingFactorIsReadByItsOrderTest
22+
{
23+
[Theory]
24+
[InlineData("(e^(2*x) - 1) * ln(cos(x) * sqrt(x))", "0")]
25+
[InlineData("tanh(x) * ln(cos(x) * sqrt(x))", "0")]
26+
[InlineData("sinh(x) * ln(cos(x) * sqrt(x))", "0")]
27+
[InlineData("(cos(x) * sqrt(x)) ^ tanh(x)", "1")]
28+
[InlineData("(cos(x) * sqrt(x)) ^ arctan(tanh(x))", "1")]
29+
public void TheLimitIsAnswered(string input, string expected)
30+
=> Assert.Equal(expected.ToEntity().Evaled, input.ToEntity().Limit("x", 0, ApproachFrom.Right).Evaled);
31+
32+
[Theory]
33+
[InlineData("(sin(x) - x) / x^3", "-1/6")]
34+
[InlineData("1 / x^2 - 1 / sin(x)^2", "-1/3")]
35+
[InlineData("(cosh(x) - 1) / (x * tanh(x))", "1/2")]
36+
[InlineData("tanh(x) * ln(cos(x) * x^2)", "0")]
37+
public void TheLimitStillHolds(string input, string expected)
38+
=> Assert.Equal(expected.ToEntity().Evaled, input.ToEntity().Limit("x", 0, ApproachFrom.BothSides).Evaled);
39+
}
40+
}

0 commit comments

Comments
 (0)