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
23 changes: 23 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,29 @@ left of 0, is left unevaluated as it was. Measured on master
| `"erf(x) * ln(x)".ToEntity().Integrate("x", 0, 1)` | `integral(erf(x) * ln(x), x, 0, 1)` | `erf(1) * (1 * ln(1) - 1) - ...`, `-0.2613483600...` |
| `"Ci(x)".Limit("x", 0, ApproachFrom.Left)` | `limitleft(Ci(x), x, 0)` | unchanged |

### A logarithm times Ei, Shi, Chi or Si of a vanishing argument has its limit, and its integral its value

**Not silent: limits and definite integrals that were left unevaluated, or did not return, have values.** Where an
exponential, cosine or hyperbolic cosine integral is read as the logarithm it differs from by what tends to 0, what is
left is an infinity less an infinity of logarithms, and it is now handed to Gruntz's algorithm first, falling back to
the rest of the machinery where the algorithm declines. Under the budget a definite integral's bound is taken within,
that rest searched the antiderivative of `ln(x) Ei(x)` at 0 for longer than twenty seconds. The algorithm also expands
the sine and hyperbolic sine integrals as their series where their argument settles. So the integrals of `ln(x)` times
`Ei(x)`, `Shi(x)`, `Shi(2x)`, `Shi(x/2)`, `Chi(2x)`, `Chi(x/2)`, `Si(2x)` and `Si(x/2)` over `[0, 1]`, and of
`ln(x) Shi(sqrt(x))`, are answered, and the integrals that were answered before by a long search, `ln(x) erf(x)` among
them, come back several times faster with the same value. Measured on master `280ff1526` and on this branch; each value
checked against mpmath's quadrature to forty digits
([#2694](https://github.com/asc-community/AngouriMath/issues/2694)).

| Call | Was (`280ff1526`) | Now |
|---|---|---|
| `"integral(ln(x) * Ei(x), x, 0, 1)".Simplify()` | did not return in 20 s | `e - 1 - eulergamma`, `1.1410661635...` |
| `"integral(ln(x) * Shi(x), x, 0, 1)".Simplify()` | did not return in 20 s | `Chi(1) - Shi(1) - 1 + (e + 1 / e) / 2 - eulergamma`, `-0.2535189644...` |
| `"integral(ln(x) * Chi(2 * x), x, 0, 1)".Simplify()` | did not return in 20 s | `Shi(2) / 2 - Chi(2) + -1/4 / e ^ 2 + e ^ 2 / 4`, `0.6115469979...` |
| `"integral(ln(x) * Si(2 * x), x, 0, 1)".Simplify()` | `integral(ln(x) * Si(2 * x), x, 0, 1)` | `(1 - (Ci(2) + cos(2) + 2 * Si(2)) + ln(2) + eulergamma) / 2`, `-0.4736485501...` |
| `"ln(x) * Shi(x)".Limit("x", 0, ApproachFrom.Right)` | `limitright(ln(x) * Shi(x), x, 0)` | `0` |
| `"x * (Shi(1/x) - 1/x) * x^2".Limit("x", "+oo")` | unevaluated | `1/18` |

### A real root beside a pole is found by the numeric search

**More roots in the same finite set.** Where no analytical route answers, `Solve` gives the roots Newton's method
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,15 +273,22 @@ internal AsymptoticSeries WithoutConstant()
/// 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)
/// <param name="function">The function the series is put through.</param>
/// <param name="requested">The order the result is wanted to.</param>
/// <param name="derivativeAtZero">
/// The <c>k</c>-th derivative of <paramref name="function"/> at 0, for a function whose derivative is written as
/// a quotient that has no value there: <c>Si'(t) = sin(t)/t</c> and <c>Shi'(t) = sinh(t)/t</c> are 1 at 0, which
/// substituting does not say. https://github.com/asc-community/AngouriMath/issues/2694
/// </param>
internal AsymptoticSeries? Through(Func<Entity, Entity> function, SeriesExponent requested, Func<int, Entity>? derivativeAtZero = null)
{
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)
if (At(derivative, 0) is not { } value)
return null;
var sum = Constant(value);
var term = Constant(1);
Expand All @@ -291,7 +298,7 @@ internal AsymptoticSeries WithoutConstant()
if (term.LeadingTerm() is not var (_, at) || at.CompareTo(requested) >= 0)
break;
derivative = derivative.Differentiate(t).InnerSimplified;
if (At(derivative) is not { } coefficient)
if (At(derivative, i) is not { } coefficient)
return null;
sum = sum.Add(term.Multiply(Constant(coefficient)));
}
Expand All @@ -300,8 +307,10 @@ internal AsymptoticSeries WithoutConstant()
// 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)
Entity? At(Entity derivativeOfF, int k)
{
if (derivativeAtZero is not null && constant.Evaled is Complex { IsZero: true } && !constant.Vars.Any())
return derivativeAtZero(k);
var coefficient = derivativeOfF.Substitute(t, constant).InnerSimplified;
return constant.Vars.Any() || coefficient.Evaled is Complex { IsNaN: false, IsFinite: true }
? coefficient
Expand Down Expand Up @@ -400,6 +409,15 @@ internal AsymptoticSeries WithoutConstant()
return Expand(argument, w, logarithmOfW, order)?.Through(MathS.Erfc, order);
case Erfif(var argument):
return Expand(argument, w, logarithmOfW, order)?.Through(MathS.Erfi, order);
// Si(t) = sum (-1)^j t^(2j+1) / ((2j+1) (2j+1)!) and Shi(t) the same without the signs, so the k-th
// derivative at 0 is 0 for an even k and (-1)^j / k, or 1 / k, for k = 2j + 1.
// https://github.com/asc-community/AngouriMath/issues/2694
case Sif(var argument):
return Expand(argument, w, logarithmOfW, order)?.Through(MathS.Si, order,
k => k % 2 == 0 ? Integer.Zero : Rational.Create(k % 4 == 1 ? 1 : -1, k));
case Shif(var argument):
return Expand(argument, w, logarithmOfW, order)?.Through(MathS.Shi, order,
k => k % 2 == 0 ? Integer.Zero : Rational.Create(1, k));
default:
return null;
}
Expand Down
19 changes: 14 additions & 5 deletions Sources/AngouriMath/Functions/Continuous/Limits/Gruntz/Gruntz.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ private static bool ArrivesOnTheCutOffTheRealLine(Entity node, Variable 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;
=> node is Sinf or Cosf or Tanf or Cotanf or Secantf or Cosecantf or Arctanf or Erff or Erfcf or Erfif or Sif or Shif;

/// <summary>
/// Whether every logarithm in <paramref name="e"/> is of something eventually positive, and every power with an
Expand Down Expand Up @@ -408,7 +408,7 @@ private static bool IsInfinite(Entity e)
// 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
case Sinf or Cosf or Tanf or Cotanf or Secantf or Cosecantf or Arctanf or Erff or Erfcf or Erfif or Sif or Shif
when AnalyticWhereItsArgumentEndsUp(e, x) is { } argument:
return Mrv(argument, x);

Expand All @@ -423,7 +423,8 @@ when AnalyticWhereItsArgumentEndsUp(e, x) is { } argument:
}

/// <summary>
/// The argument of a trigonometric function, the arctangent or an error function, where it tends to a finite
/// The argument of a trigonometric function, the arctangent, an error function or the sine or hyperbolic sine
/// integral, where it tends to a finite
/// real number at which the function is analytic, or <see langword="null"/>.
/// </summary>
/// <remarks>
Expand All @@ -436,12 +437,18 @@ when AnalyticWhereItsArgumentEndsUp(e, x) is { } argument:
/// </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
/// the sine, and those are only asked at a limit whose cosine or sine is clear of 0; the arctangent, the error
/// functions and the sine and hyperbolic sine integrals 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>
/// <para>
/// The antiderivative of <c>ln(x) Shi(x)</c> at 0 is <c>ln(x) (x Shi(x) - cosh(x)) + ln(x)</c> and terms with a
/// finite limit once its <c>Chi(x)</c> is read as <c>eulergamma + ln(x)</c>, an infinity less an infinity that
/// nothing else settled within twenty seconds, and the hyperbolic sine integral in it held the algorithm out.
/// https://github.com/asc-community/AngouriMath/issues/2694
/// </para>
/// </remarks>
private static Entity? AnalyticWhereItsArgumentEndsUp(Entity e, Variable x)
{
Expand All @@ -457,6 +464,8 @@ when AnalyticWhereItsArgumentEndsUp(e, x) is { } argument:
Erff(var u) => u,
Erfcf(var u) => u,
Erfif(var u) => u,
Sif(var u) => u,
Shif(var u) => u,
_ => null
};
if (argument is null || !argument.ContainsNode(x))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -568,11 +568,45 @@ private static (bool Asked, Entity? Limit) ThroughALogarithmicSingularity(Entity
|| coefficientLimit.Evaled is Number { IsFinite: false } or Number { IsNaN: true })
continue;
var logarithmicPart = MathS.eulergamma + MathS.Ln(sign is 1 ? logarithmOf : -logarithmOf);
return (true, ComputeLimit(linear.Substitute(standIn, logarithmicPart), x, dest, side, acceptNaN));
var withTheLogarithm = linear.Substitute(standIn, logarithmicPart);
return (true, ByGruntz(withTheLogarithm, x, dest, side) ?? ComputeLimit(withTheLogarithm, x, dest, side, acceptNaN));
}
return (false, null);
}

/// <summary>
/// The limit of <paramref name="expr"/> by Gruntz's algorithm, at an infinity or from one side of a finite point
/// moved out to <c>+oo</c>, or <see langword="null"/> where the algorithm does not read it.
/// </summary>
/// <remarks>
/// What <see cref="ThroughALogarithmicSingularity"/> leaves is an infinity less an infinity of logarithms, which
/// is what the algorithm is for, and the rest of the machinery reaches it last. The antiderivative of
/// <c>ln(x) Ei(x)</c> at 0 is left as <c>ln(x) (x (gamma + ln(x)) - e^x) - (x (gamma + ln(x)) - e^x - gamma - ln(x))</c>.
/// Without a budget the descent answers it by asking a quotient in it whole, in half a second; under the budget
/// of limits a definite integral's bound is taken within, that whole question is refused, and l'Hopital's rule
/// simplified its derivatives for longer than twenty seconds, at any size of budget. The algorithm answers it in
/// a hundredth of one.
/// https://github.com/asc-community/AngouriMath/issues/2694
/// </remarks>
private static Entity? ByGruntz(Entity expr, Variable x, Entity dest, ApproachFrom side)
{
Entity? atInfinity = dest switch
{
Real { IsFinite: false, IsNaN: false, IsNegative: false } => expr,
Real { IsFinite: false, IsNaN: false, IsNegative: true } => expr.Substitute(x, -x),
_ when dest.IsFinite && side is ApproachFrom.Right => expr.Substitute(x, dest + 1 / x).InnerSimplified,
_ when dest.IsFinite && side is ApproachFrom.Left => expr.Substitute(x, dest - 1 / x).InnerSimplified,
_ => null
};
if (atInfinity is null)
return null;
var approach = EnterApproach(x, Real.PositiveInfinity);
Entity? limit;
try { limit = Gruntz.LimitToPositiveInfinity(atInfinity, x); }
finally { LeaveApproach(approach); }
return limit is { } found && found.Evaled is not Number { IsNaN: true } ? found.InnerSimplified : null;
}

/// <summary>
/// How many limits a budgeted computation may still take, every one it asks along the way counted, or
/// <see langword="null"/> where there is no budget. Past it each is undecided. Set by <see cref="WithinABudget"/>.
Expand Down
22 changes: 22 additions & 0 deletions Sources/Tests/UnitTests/Calculus/GruntzTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,28 @@ public void TheOrdinaryOnesAreUnchanged(string expression, string destination, s
public void AFunctionAnalyticWhereItsArgumentSettlesIsItsSeries(string expression, string destination, string expected) =>
AssertLimit(expression, destination, expected);

/// <summary>
/// The sine and hyperbolic sine integrals are analytic on the whole real line, <c>Si(t) = t - t^3/18 + ...</c> and
/// <c>Shi(t) = t + t^3/18 + ...</c>, and are expanded as their series where their argument settles. Their
/// derivatives are written <c>sin(t)/t</c> and <c>sinh(t)/t</c>, which have no value at 0, so the coefficients
/// there are given rather than substituted. A hyperbolic sine integral held the algorithm out of the expression,
/// and the first and last of these were left unevaluated.
/// https://github.com/asc-community/AngouriMath/issues/2694
/// </summary>
[Theory]
[InlineData("x * (Shi(1/x) - 1/x) * x ^ 2", "+oo", "1/18")]
[InlineData("x ^ 3 * (Si(1/x) - 1/x)", "+oo", "-1/18")]
[InlineData("x ^ 3 * (Shi(1/x) - Si(1/x))", "+oo", "1/9")]
[InlineData("ln(x) * (x * Shi(x) - cosh(x)) + ln(x)", "0", "0")]
public void Issue2694_TheSineIntegralsAreTheirSeries(string expression, string destination, string expected)
{
if (destination == "0")
Assert.Equal(expected.ToEntity().Evaled,
expression.ToEntity().Limit("x", 0, AngouriMath.Core.ApproachFrom.Right).Evaled);
else
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
33 changes: 29 additions & 4 deletions Sources/Tests/UnitTests/Calculus/ImproperIntegralTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,12 @@ public void ABoundWhereTheAntiderivativeIsUndefinedIsALimit(string integrand, st
/// The rows were <c>ln(x) erf(x)</c> and <c>ln(x) Si(x)</c> until both were answered
/// (<see cref="ALogarithmTimesAnAnalyticFunctionIsAnswered"/>, https://github.com/asc-community/AngouriMath/issues/2690),
/// so the rows are now integrands whose antiderivative's limit at 0 is still not decided. That of
/// <c>ln(x) Si(2x)</c> holds <c>ln(x) x Si(2x)</c> beside <c>ln(x) cos(2x) - Ci(2x)/2</c>, and Gruntz's algorithm,
/// which settles such an infinity less an infinity, has no series for the sine integral. That of
/// <c>ln(x) arccos(x)</c> holds the logarithm of a quotient that is negative on the whole of <c>(0, 1)</c>, so is
/// not real there. Both were left as written before the change as well.
/// not real there, and was left as written before the change as well. <c>ln(x) Si(2x)</c> was a row too until
/// Gruntz's algorithm had a series for the sine integral
/// (<see cref="Issue2694_ALogarithmTimesAnExponentialOrSineIntegralIsAnswered"/>).
/// </remarks>
[Theory]
[InlineData("ln(x) * Si(2 * x)")]
[InlineData("ln(x) * arccos(x)")]
public void ABoundWhoseLimitIsNotDecidedLeavesTheIntegralAsWritten(string integrand)
{
Expand Down Expand Up @@ -130,6 +129,32 @@ public void ALogarithmTimesAnAnalyticFunctionIsAnswered(string integrand, string
Assert.True((result - value.ToEntity()).EvalNumerical().Abs() < 1e-25, $"was {result}");
}

/// <summary>
/// The antiderivative of <c>ln(x) Ei(x)</c> at 0 is an infinity less an infinity once <c>Ei(x)</c> is read as
/// <c>eulergamma + ln(x)</c>, and that of <c>ln(x) Shi(x)</c> once <c>Chi(x)</c> is, beside <c>ln(x) x Shi(x)</c>.
/// Under the budget a definite integral's bound is taken within, l'Hopital's rule searched the first for longer
/// than twenty seconds, and Gruntz's algorithm, which settles it at once, was reached last; it had no series for
/// the hyperbolic sine integral or the sine integral at all. Each integral was left as written or did not
/// return. Each value is mpmath's quadrature to thirty-six digits, and agrees with the closed form to fifty.
/// https://github.com/asc-community/AngouriMath/issues/2694
/// </summary>
[Theory]
[InlineData("ln(x) * Shi(x)", "cosh(1) + Chi(1) - Shi(1) - 1 - eulergamma", "-0.253518964481809355805770244785462397")]
[InlineData("ln(x) * Ei(x)", "e - 1 - eulergamma", "1.14106616355751237475377538127026007")]
[InlineData("ln(x) * Shi(2 * x)", "(cosh(2) + Chi(2) - 2 * Shi(2) - 1 - ln(2) - eulergamma) / 2", "-0.529317549220441735751071525861212406")]
[InlineData("ln(x) * Shi(x / 2)", "2 * cosh(1/2) + 2 * Chi(1/2) - Shi(1/2) - 2 + 2 * ln(2) - 2 * eulergamma", "-0.125435478003067959586041617084334371")]
[InlineData("ln(x) * Chi(2 * x)", "sinh(2) / 2 + Shi(2) / 2 - Chi(2)", "0.611546997954082682664660585264909175")]
[InlineData("ln(x) * Chi(x / 2)", "2 * sinh(1/2) + 2 * Shi(1/2) - Chi(1/2)", "2.10896095558332273082530706812701215")]
[InlineData("ln(x) * Si(2 * x)", "(1 - cos(2) - Ci(2) - 2 * Si(2) + ln(2) + eulergamma) / 2", "-0.473648550185817067915346504277046293")]
[InlineData("ln(x) * Si(x / 2)", "2 - 2 * cos(1/2) - 2 * Ci(1/2) - Si(1/2) - 2 * ln(2) + 2 * eulergamma", "-0.124567415527411216344009393390834076")]
public void Issue2694_ALogarithmTimesAnExponentialOrSineIntegralIsAnswered(string integrand, string closedForm, string value)
{
var result = Definite(integrand, "0", "1");
Assert.False(result.Nodes.Any(node => node is Entity.Integralf or Entity.Limitf), result.Stringize());
Assert.True((result - closedForm.ToEntity()).EvalNumerical().Abs() < 1e-25, $"was {result}");
Assert.True((result - value.ToEntity()).EvalNumerical().Abs() < 1e-25, $"was {result}");
}

/// <summary>A bound the substitution answers is answered as it was.</summary>
[Theory]
[InlineData("e^(-x)", "0", "+oo", "1")]
Expand Down
Loading