Skip to content
Open
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
26 changes: 26 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,32 @@ irrational constants. Where two exponents written over different constants canno
| `(2^x + 3^x) / (3^x + 4^x)` at `+oo` | unevaluated | `0` |
| `x^10 * 2^x / 3^x` at `+oo` | unevaluated | `0` |

### A limit through a cosine, a sine or an error function of a vanishing argument is answered at infinity

**Not silent: limits and definite integrals that were left unevaluated have values.** Gruntz's algorithm, which settles
an infinity less an infinity at `+oo` and so at every finite point moved out there, declined any expression holding a
sine, a cosine or an error function. It now expands one whose argument tends to a finite real number where it is
analytic (the sine, cosine, tangent, cotangent, secant and cosecant away from their poles, the arctangent, and `erf`,
`erfc` and `erfi`) as its Taylor series there, where every logarithm and every power that is not whole in the
expression is of something positive on the way, so that the function is real there; one whose argument does not settle
is declined as before. So
`ln(x) cos(x) - ln(x)` at 0 from the right, which was left unevaluated while the same expression with `e^x` for the
cosine was 0, is 0, and the integrals of `ln(x)` times the sine integral, the error functions and the sine over
`[0, 1]` are answered. Two-sided at 0 the limit is still left unevaluated, the logarithm not being real to the left.
Measured on master `70b17bba4` and on this branch; each value checked against mpmath
([#2690](https://github.com/asc-community/AngouriMath/issues/2690)).

| Call | Was (`70b17bba4`) | Now |
|---|---|---|
| `"ln(x) * cos(x) - ln(x)".Limit("x", 0, ApproachFrom.Right)` | `limitright(ln(x) * cos(x) - ln(x), x, 0)` | `0` |
| `"ln(x) * cos(1/x) - ln(x)".Limit("x", "+oo")` | unevaluated | `0` |
| `"e^(2 * x) * (1 - cos(e^(-x)))".Limit("x", "+oo")` | unevaluated | `1/2` |
| `"integral(ln(x) * Si(x), x, 0, 1)".Simplify()` | `integral(Si(x) * ln(x), x, 0, 1)` | `eulergamma + 1 - (Si(1) + Ci(1) + cos(1))`, -0.2465736342 |
| `"integral(ln(x) * erf(x), x, 0, 1)".Simplify()` | `integral(ln(x) * erf(x), x, 0, 1)` | `(1 + (-1) / e) / sqrt(pi) - (pi ^ (-1/2) * (Ei(-1) - eulergamma) / 2 + erf(1))`, -0.2613483600 |
| `"integral(ln(x) * erfc(x), x, 0, 1)".Simplify()` | not done in 20 s | `(Ei(-1) / 2 - 1 + 1 / e + -eulergamma / 2) / sqrt(pi) - erfc(1)`, -0.7386516400 |
| `"integral(ln(x) * sin(x), x, 0, 1)".Simplify()` | `integral(sin(x) * ln(x), x, 0, 1)` | `Ci(1) - eulergamma`, -0.2398117420 |
| `"ln(x) * cos(x) - ln(x)".Limit("x", 0)` | `limit(ln(x) * cos(x) - ln(x), x, 0)` | unchanged |

### A negative multiple of ln of a rational below 1 is turned round

**Silent, in the printed form only.** `ln(1/b) = -ln(b)` matched a written quotient `1 / b`, never the literal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,53 @@ internal AsymptoticSeries WithoutConstant()
return sum.WithOrder(requested).Multiply(Constant(scale));
}

/// <summary>
/// <paramref name="function"/> of the series, by its Taylor series at the series' constant term: <c>f(c + d) =
/// sum f^(k)(c) d^k / k!</c>, where <c>d</c>, the rest, starts above <c>w^0</c>. The caller has made sure
/// <c>f</c> is analytic at the limit of <c>c</c>.
/// </summary>
/// <remarks>
/// <c>f^(k)(c)</c> is a coefficient, left unexpanded as <c>exp(c)</c> is in <see cref="Exponentiate"/>: <c>c</c>
/// may still hold a function of a slower class than <c>w</c>. Where no term of the rest is known, nothing beyond
/// its order is known of the result either.
/// https://github.com/asc-community/AngouriMath/issues/2690
/// </remarks>
internal AsymptoticSeries? Through(Func<Entity, Entity> function, SeriesExponent requested)
{
var constant = Terms.TryGetValue(SeriesExponent.Zero, out var atZero) ? atZero : Integer.Zero;
var rest = WithoutConstant();
if (rest.LeadingTerm() is var (_, least) && least.CompareTo(SeriesExponent.Zero) <= 0)
return null;
var t = Variable.CreateTemp(constant.Vars);
var derivative = function(t);
if (At(derivative) is not { } value)
return null;
var sum = Constant(value);
var term = Constant(1);
for (var i = 1; i < MaxExpansionTerms; i++)
{
term = term.Multiply(rest).Multiply(Constant(Rational.Create(1, i)));
if (term.LeadingTerm() is not var (_, at) || at.CompareTo(requested) >= 0)
break;
derivative = derivative.Differentiate(t).InnerSimplified;
if (At(derivative) is not { } coefficient)
return null;
sum = sum.Add(term.Multiply(Constant(coefficient)));
}
return sum.WithOrder(rest.Order.CompareTo(requested) < 0 ? rest.Order : requested);

// A derivative written as a quotient whose divisor vanishes at the constant would read as no number there,
// and a coefficient that is no number is no series; where the constant still holds x it is left to the
// limit of the leading coefficient, which is taken afterwards.
Entity? At(Entity derivativeOfF)
{
var coefficient = derivativeOfF.Substitute(t, constant).InnerSimplified;
return constant.Vars.Any() || coefficient.Evaled is Complex { IsNaN: false, IsFinite: true }
? coefficient
: null;
}
}

/// <summary>
/// The logarithm of the series, given the value of <c>log(w)</c>. The leading term
/// has to be found, since <c>log</c> of something that might be zero is not something
Expand Down Expand Up @@ -331,6 +378,28 @@ internal AsymptoticSeries WithoutConstant()
return Expand(antilog, w, logarithmOfW, order)?.Logarithm(logarithmOfW, order) is { } logarithm
? logarithm.Multiply(Constant((1 / MathS.Ln(@base)).InnerSimplified))
: null;
// Only reached for a function Gruntz.Mrv has found analytic where its argument ends up.
// https://github.com/asc-community/AngouriMath/issues/2690
case Sinf(var argument):
return Expand(argument, w, logarithmOfW, order)?.Through(MathS.Sin, order);
case Cosf(var argument):
return Expand(argument, w, logarithmOfW, order)?.Through(MathS.Cos, order);
case Tanf(var argument):
return Expand(argument, w, logarithmOfW, order)?.Through(MathS.Tan, order);
case Cotanf(var argument):
return Expand(argument, w, logarithmOfW, order)?.Through(MathS.Cotan, order);
case Secantf(var argument):
return Expand(argument, w, logarithmOfW, order)?.Through(MathS.Sec, order);
case Cosecantf(var argument):
return Expand(argument, w, logarithmOfW, order)?.Through(MathS.Cosec, order);
case Arctanf(var argument):
return Expand(argument, w, logarithmOfW, order)?.Through(MathS.Arctan, order);
case Erff(var argument):
return Expand(argument, w, logarithmOfW, order)?.Through(MathS.Erf, order);
case Erfcf(var argument):
return Expand(argument, w, logarithmOfW, order)?.Through(MathS.Erfc, order);
case Erfif(var argument):
return Expand(argument, w, logarithmOfW, order)?.Through(MathS.Erfi, order);
default:
return null;
}
Expand Down
107 changes: 102 additions & 5 deletions Sources/AngouriMath/Functions/Continuous/Limits/Gruntz/Gruntz.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,12 @@ namespace AngouriMath.Functions.Algebra
/// <para>
/// The scope here is the exp-log functions: what can be built from x and the rationals
/// with the four operations, exp and log. That is the class the algorithm is proved for,
/// because those functions are eventually monotone and so comparable at all. Anything
/// else -- a sine, an unknown function, a factorial -- makes this decline rather than
/// guess, since sin(x) has no limit at infinity and no comparability class either.
/// because those functions are eventually monotone and so comparable at all. A sine, a
/// cosine, a tangent, the arctangent or an error function of an argument that settles at a
/// point where it is analytic is read as its Taylor series there, which puts it in its
/// argument's class. Anything else -- a sine of an argument that does not settle, an unknown
/// function, a factorial -- makes this decline rather than guess, since sin(x) has no limit at
/// infinity and no comparability class either.
/// </para>
/// </remarks>
internal static class Gruntz
Expand Down Expand Up @@ -86,9 +89,45 @@ private static void Trace(string what, object? value)
if (expr.Nodes.Any(node => LimitFunctional.TakesItsValueOnACut(node, x, Real.PositiveInfinity)))
return null;
expr = AsExponentials(expr, x);
if (expr.Nodes.Any(IsAnalyticFunction) && !IsRealOnTheWay(expr, x))
return null;
return LimitInf(expr, x);
}

private static bool IsAnalyticFunction(Entity node)
=> node is Sinf or Cosf or Tanf or Cotanf or Secantf or Cosecantf or Arctanf or Erff or Erfcf or Erfif;

/// <summary>
/// Whether every logarithm in <paramref name="e"/> is of something eventually positive, and every power with an
/// exponent that is not whole of a base that is, as the algorithm's reading of them as real needs.
/// </summary>
/// <remarks>
/// The cut check above reads the value at the destination, and a sine of a vanishing argument is 0 there
/// whichever side it comes from. Asked only of an expression holding one of the functions <see cref="Mrv"/>
/// reads through their Taylor series, which before were declined whole: x^x at 0 from the left is
/// <c>(-1/x)^(-1/x)</c> at <c>+oo</c>, which is not real, and under a sine it was answered <c>sin(1)</c>, and
/// <c>sin(x)^(x - 1)</c> there <c>+oo</c>, where the library leaves a side that is not real unevaluated.
/// https://github.com/asc-community/AngouriMath/issues/2690
/// </remarks>
private static bool IsRealOnTheWay(Entity e, Variable x)
{
foreach (var node in e.Nodes)
{
var positive = node switch
{
Logf(var @base, var antilog) => (@base.ContainsNode(x) ? @base : null, antilog.ContainsNode(x) ? antilog : null),
Powf(var @base, var power) when @base != MathS.e && !power.ContainsNode(x) && power.Evaled is not Integer
=> (@base.ContainsNode(x) ? @base : null, (Entity?)null),
_ => ((Entity?)null, (Entity?)null)
};
if (positive.Item1 is { } first && Sign(first, x) is not 1)
return false;
if (positive.Item2 is { } second && Sign(second, x) is not 1)
return false;
}
return true;
}

private static Entity? LimitInf(Entity e, Variable x)
{
if (!e.ContainsNode(x))
Expand Down Expand Up @@ -276,16 +315,74 @@ private static bool IsInfinite(Entity e)
case Logf(var @base, var antilog) when !@base.ContainsNode(x):
return Mrv(antilog, x);

// A function analytic where its argument ends up is a power series in how far the argument still
// is from there, so it is in no class of its own: it is in its argument's.
// https://github.com/asc-community/AngouriMath/issues/2690
case Sinf or Cosf or Tanf or Cotanf or Secantf or Cosecantf or Arctanf or Erff or Erfcf or Erfif
when AnalyticWhereItsArgumentEndsUp(e, x) is { } argument:
return Mrv(argument, x);

default:
// Anything else -- a sine, an unknown function, a factorial -- has no
// comparability class this knows how to place.
// Anything else -- a sine of an argument that does not settle, an unknown
// function, a factorial -- has no comparability class this knows how to place.
return null;
}

HashSet<Entity>? Both(Entity left, Entity right)
=> Mrv(left, x) is { } a && Mrv(right, x) is { } b ? MrvMax(a, b, x) : null;
}

/// <summary>
/// The argument of a trigonometric function, the arctangent or an error function, where it tends to a finite
/// real number at which the function is analytic, or <see langword="null"/>.
/// </summary>
/// <remarks>
/// <para>
/// The algorithm is proved for the exp-log functions, and nothing in it needs more of a function analytic at
/// the limit of its argument: <c>f(u)</c> with <c>u</c> tending to <c>c</c> is the Taylor series of <c>f</c> at
/// <c>c</c> in <c>u - c</c>, which vanishes, so <c>f(u)</c> is in no class faster than <c>u</c>'s, and its
/// series is that Taylor series composed with the series of <c>u</c>. A sine whose argument runs off to
/// infinity has no limit and no class, and is still declined.
/// </para>
/// <para>
/// The tangent and secant are analytic off the zeros of the cosine, the cotangent and cosecant off the zeros of
/// the sine, and those are only asked at a limit whose cosine or sine is clear of 0; the arctangent and the error
/// functions are analytic on the whole real line. Without this, a cosine held this out of every limit at
/// <c>+oo</c> it appeared in, and of every limit at a finite point moved out to <c>+oo</c> with it: <c>ln(x) cos(x) - ln(x)</c> at 0 from the right, which is <c>ln(1/x) (1 - cos(1/x))</c>
/// there and tends to 0, was left unevaluated, where with <c>e^x</c> in place of the cosine it was answered.
/// https://github.com/asc-community/AngouriMath/issues/2690
/// </para>
/// </remarks>
private static Entity? AnalyticWhereItsArgumentEndsUp(Entity e, Variable x)
{
var argument = e switch
{
Sinf(var u) => u,
Cosf(var u) => u,
Tanf(var u) => u,
Cotanf(var u) => u,
Secantf(var u) => u,
Cosecantf(var u) => u,
Arctanf(var u) => u,
Erff(var u) => u,
Erfcf(var u) => u,
Erfif(var u) => u,
_ => null
};
if (argument is null || !argument.ContainsNode(x))
return null;
if (LimitInf(argument, x) is not { } limit
|| Bare(limit).Evaled is not Real { IsFinite: true, IsNaN: false } at)
return null;
const double clearOfZero = 1e-12;
return e switch
{
Tanf or Secantf => System.Math.Abs(System.Math.Cos(at.EDecimal.ToDouble())) > clearOfZero ? argument : null,
Cotanf or Cosecantf => System.Math.Abs(System.Math.Sin(at.EDecimal.ToDouble())) > clearOfZero ? argument : null,
_ => argument
};
}

private static HashSet<Entity>? MrvMax(HashSet<Entity> f, HashSet<Entity> g, Variable x)
{
if (f.Count == 0) return g;
Expand Down
14 changes: 14 additions & 0 deletions Sources/Tests/UnitTests/Calculus/GruntzTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,20 @@ public void WhatNothingBeforeItCouldReach(string expression, string destination,
public void TheOrdinaryOnesAreUnchanged(string expression, string destination, string expected) =>
AssertLimit(expression, destination, expected);

/// <summary>
/// A function analytic where its argument settles is the Taylor series there in what the argument still has to
/// go, and is expanded as one: in <c>w = e^(-x)</c>, <c>1 - cos(e^(-x))</c> is <c>w^2/2 - w^4/24 + ...</c>. A
/// cosine anywhere in the expression held the algorithm out of it, and each of these was left unevaluated. The
/// last is the quartic term, where the cosine and the exponential agree to the square.
/// https://github.com/asc-community/AngouriMath/issues/2690
/// </summary>
[Theory]
[InlineData("e ^ (2 * x) * (1 - cos(e ^ (-x)))", "+oo", "1/2")]
[InlineData("ln(x) * (cos(1/x) - 1)", "+oo", "0")]
[InlineData("x * (cos(1/x) - e ^ (-1/(2 * x ^ 2))) * x ^ 3", "+oo", "-1/12")]
public void AFunctionAnalyticWhereItsArgumentSettlesIsItsSeries(string expression, string destination, string expected) =>
AssertLimit(expression, destination, expected);

/// <summary>
/// The algorithm is stated for the exp-log functions -- what can be built from x and
/// the rationals with the four operations, exp and log -- because those are eventually
Expand Down
Loading
Loading