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
25 changes: 25 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,31 @@ one point, or else `Simplify` proving it zero. A singular matrix answers as `[[1
| `"[[sin(x)^2, 1], [1 - cos(x)^2, 1]]^(-1)".Simplify()` | `NaN` | `1 / [[sin(x) ^ 2, 1], [sin(x) ^ 2, 1]]` |
| `"[[e^x, e^(2x)], [2, e^x]]^(-1)".Simplify()` | its inverse | unchanged |

### Ei, Ci, Chi and li have limits at their logarithmic singularity

Unevaluated limits become values. `Ei(u)`, `Ci(u)` and `Chi(u)` are `eulergamma` plus the logarithm of `u` plus what
tends to 0 as `u` does, `ln|u|` for Ei and `ln u` for the other two, and `li(u)` is `Ei(ln u)`. So each is `-oo` at its
singularity along the real line, and a sum holding one with a coefficient that has a finite limit has the limit of the
same sum with `eulergamma` and the logarithm in its place. None of these was answered. Ci and Chi approached from
below 0, where the principal logarithm is `ln|u| + pi i`, are still not answered. The antiderivative of `erf(x) ln(x)`
holds `ln(x)` against `Ei(-x^2)`, so the definite integral over `[0, 1]` was left as written, after about ten seconds of
search for its limit at 0; it is now its value, which mpmath's quadrature gives as `-0.26134836002139896`. `Ei`, `Si` and
`Shi` of a real argument, and `Ci`, `Chi` and `li` of a positive one, are read as real where a two-sided limit asks
whether its two agreeing sides are its limit, so `x Ei(x)` at 0, 0 from each side, is 0 where it was left unevaluated; `x Ci(x)`, which is not real
left of 0, is left unevaluated as it was. Measured on master
`abd100a33` and on this branch ([#2661](https://github.com/asc-community/AngouriMath/issues/2661)).

| Call | Was (`abd100a33`) | Now |
|---|---|---|
| `"Ei(x)".Limit("x", 0)` | `limit(Ei(x), x, 0)` | `-oo` |
| `"Ci(x)".Limit("x", 0, ApproachFrom.Right)` | `limitright(Ci(x), x, 0)` | `-oo` |
| `"li(x)".Limit("x", 1)` | `limit(li(x), x, 1)` | `-oo` |
| `"Ei(x) - ln(x)".Limit("x", 0, ApproachFrom.Right)` | `limitright(Ei(x) - ln(x), x, 0)` | `eulergamma` |
| `"Ei(2x) - Ei(x)".Limit("x", 0, ApproachFrom.Right)` | `limitright(Ei(2 * x) - Ei(x), x, 0)` | `ln(2)` |
| `"x Ei(x)".Limit("x", 0)` | `limit(x * Ei(x), x, 0)` | `0` |
| `"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 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 @@ -771,6 +771,8 @@ partial record Arccosf
}
if (limit.Evaled is not Number { IsFinite: true, IsNaN: false } value)
return null;
if (AtTheLogarithmicSingularity(node, argument, value, x, dist, side) is { } singular)
return singular;
var entire = node is Erff or Erfcf or Erfif or Sif or Shif;
if (!entire && !(value is Real { IsPositive: true } && !(node is Lif && value == Integer.One)))
return null;
Expand All @@ -789,6 +791,38 @@ partial record Arccosf
};
}

/// <summary>
/// <c>-oo</c> for an exponential, cosine or hyperbolic cosine integral whose argument tends to 0, and a
/// logarithmic integral whose argument tends to 1, where the logarithm in it tends to <c>-oo</c> along the
/// real line; <see langword="null"/> otherwise.
/// </summary>
/// <remarks>
/// <c>Ei(u) = gamma + ln|u| + O(u)</c> for real <c>u</c> of either sign, and <c>li(u) = Ei(ln u)</c>, so each
/// tends to <c>-oo</c> as <c>u</c> reaches its singularity along the real line. <c>Ci(u)</c> and <c>Chi(u)</c>
/// are <c>gamma + ln u + O(u^2)</c> with the principal logarithm, which is <c>-oo</c> from above and
/// <c>-oo + pi i</c> from below, so only the approach from above is answered. Undecided, Ei at 0 was asked again
/// by every rewrite the search for a limit holding it made: the antiderivative of <c>erf(x) ln(x)</c> at 0
/// asked the limit of <c>Ei(-1/x^2)</c> at <c>+oo</c> 574 times of the 2000 its budget allows, and the
/// integral over <c>[0, 1]</c> took sixteen seconds to be left as written. Checked against mpmath at
/// <c>±1e-10</c>, where <c>ei</c>, <c>ci</c>, <c>chi</c> and <c>li(1 ± 1e-10)</c> are all near <c>-22.4</c>.
/// https://github.com/asc-community/AngouriMath/issues/2661
/// </remarks>
private static Entity? AtTheLogarithmicSingularity(Entity node, Entity argument, Number value, Variable x, Entity dist, ApproachFrom side)
{
var (logarithmOf, bothSigns) = node switch
{
Eif when value == Integer.Zero => (argument, true),
Cif or Chif when value == Integer.Zero => (argument, false),
Lif when value == Integer.One => ((Entity)MathS.Ln(argument), true),
_ => (null, false)
};
if (logarithmOf is null)
return null;
return SignOnEachSide(logarithmOf, x, dist, side) is { } sign && (sign is 1 || bothSigns && sign is -1)
? Real.NegativeInfinity
: null;
}

partial record Erff
{
internal override Entity? ComputeLimitDivideEtImpera(Variable x, Entity dist, ApproachFrom side)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,15 @@ bool RealThere(Entity node)
Ceilf(var argument) => RealThere(argument),
Roundf(var argument) => RealThere(argument),
Signumf(var argument) => RealThere(argument),
// And the exponential, sine and hyperbolic sine integrals of a real argument, and the cosine,
// hyperbolic cosine and logarithmic integrals of a positive one, below which they take the
// logarithm's cut. x Ei(x) at 0 is 0 from each side. https://github.com/asc-community/AngouriMath/issues/2661
Eif(var argument) => RealThere(argument),
Sif(var argument) => RealThere(argument),
Shif(var argument) => RealThere(argument),
Cif(var argument) => RealThere(argument) && PositiveOnBothSides(argument),
Chif(var argument) => RealThere(argument) && PositiveOnBothSides(argument),
Lif(var argument) => RealThere(argument) && PositiveOnBothSides(argument),
_ => false
};
}
Expand Down Expand Up @@ -495,6 +504,60 @@ private static Entity ExpandLogarithm(Entity expr)
_ => expr
};

/// <summary>
/// The limit of <paramref name="expr"/> with an exponential, cosine or hyperbolic cosine integral whose argument
/// tends to 0, or a logarithmic integral whose argument tends to 1, written as the logarithm it differs from by
/// what tends to 0; or <see langword="null"/>.
/// </summary>
/// <remarks>
/// <para>
/// <c>Ei(u) - gamma - ln|u|</c> is <c>sum_(k >= 1) u^k / (k k!)</c> for real <c>u</c>, <c>Ci(u) - gamma - ln u</c>
/// is <c>int_0^u (cos t - 1)/t dt</c> and <c>Chi</c>'s the same with <c>cosh</c>, and <c>li(u) = Ei(ln u)</c>; each
/// tends to 0 at the singularity. Where <c>expr</c> is <c>A + c N</c> in the function <c>N</c>, with <c>c</c>
/// tending to a finite value, <c>c (N - L)</c> tends to 0 for <c>L</c> the logarithmic part, and <c>expr</c> has
/// the limit of <c>A + c L</c>, which the logarithms already in <c>A</c> can cancel. Left as the function, the
/// infinity less an infinity was settled by nothing: the antiderivative of <c>ln(x) erf(x)</c> at 0 holds
/// <c>ln(x)</c> against <c>Ei(-x^2)/(2 sqrt(pi))</c>, and the search for its limit spent its whole budget of
/// 2000 limits in ten seconds, asking 256 questions over and over, and left the integral over <c>[0, 1]</c>
/// unevaluated where it is <c>1/sqrt(pi) + eulergamma/(2 sqrt(pi)) - erf(1) - 1/(e sqrt(pi)) - Ei(-1)/(2 sqrt(pi))</c>,
/// <c>-0.26135</c>. https://github.com/asc-community/AngouriMath/issues/2661
/// </para>
/// <para>
/// Only along the real line: <c>ln|u|</c> is <c>Ei</c>'s logarithmic part for a real <c>u</c> of either sign, and
/// <c>ln u</c> is <c>Ci</c>'s and <c>Chi</c>'s for a positive one. The principal logarithm of a negative
/// <c>u</c> is <c>ln|u| + pi i</c>, which is where they take theirs, and that is not asked.
/// </para>
/// </remarks>
private static (bool Asked, Entity? Limit) ThroughALogarithmicSingularity(Entity expr, Variable x, Entity dest, ApproachFrom side, bool acceptNaN)
{
foreach (var node in expr.Nodes)
{
var (argument, singularAt, logarithmOf, bothSigns) = node switch
{
Eif(var u) => (u, (Entity)Integer.Zero, u, true),
Cif(var u) => (u, Integer.Zero, u, false),
Chif(var u) => (u, Integer.Zero, u, false),
Lif(var u) => (u, Integer.One, MathS.Ln(u), true),
_ => (null!, null!, null!, false)
};
if (argument is null || !argument.ContainsNode(x)
|| ComputeLimit(argument, x, dest, side) is not { } atTheSingularity || atTheSingularity.Evaled != singularAt
|| SignOnEachSide(logarithmOf, x, dest, side) is not { } sign || !(sign is 1 || bothSigns && sign is -1))
continue;
var standIn = Variable.CreateUnique(expr, "u");
var linear = expr.Substitute(node, standIn);
var coefficient = linear.Differentiate(standIn).InnerSimplified;
if (coefficient.ContainsNode(standIn) || coefficient.Nodes.Any(static inner => inner is Derivativef)
|| ComputeLimit(coefficient, x, dest, side) is not { } coefficientLimit
|| coefficientLimit.ContainsNode(x) || coefficientLimit.Nodes.Any(static inner => inner is Limitf)
|| 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));
}
return (false, 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 Expand Up @@ -723,6 +786,12 @@ node is Derivativef(var expression, Variable variable, var iterations) && iterat
expr = expr.Replace(c => AsSineOverCosine(c, x));
MultithreadingFunctional.ExitIfCancelled();

// Asked whole and answered or declined there: what is left is the same limit, without the infinity less an
// infinity that nothing settles, and searching the original for it again spent the budget the definite
// integral's bound is taken within.
if (ThroughALogarithmicSingularity(expr, x, dest, side, acceptNaN) is (true, var throughTheSingularity))
return throughTheSingularity;

if (side is ApproachFrom.Left or ApproachFrom.Right)
{
expr = WithStepsAtTheirLimits(expr, x, dest, side);
Expand Down Expand Up @@ -836,6 +905,7 @@ node is Derivativef(var expression, Variable variable, var iterations) && iterat
else if (DescendComparingSides(ApproachFrom.Left) is { } fromLeft
&& DescendComparingSides(ApproachFrom.Right) is { } fromRight)
{

if (fromLeft == fromRight && (acceptNaN || fromLeft.Evaled != MathS.NaN))
return fromLeft;
if (ExpressionNumerical.AreEqual(fromLeft, fromRight) && (acceptNaN || fromLeft.Evaled != MathS.NaN))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,54 @@ public void IsOneSidedWhereItsArgumentDiverges()
Assert.Equal(MathS.NaN, MathS.FromString("erf(1/x)").Limit("x", 0));
}

// Nothing is claimed on Ci's cut or at li's pole.
// Nothing is claimed on Ci's cut, nor at its singularity from below, where it is -oo + pi i.
[Theory]
[InlineData("Ci(x)", "-2")]
[InlineData("Ci(x)", "-oo")]
public void IsNotAnsweredOnACut(string input, string destination)
=> Assert.IsType<Entity.Limitf>(MathS.FromString(input).Limit("x", destination));

[Fact]
public void IsNotAnsweredAtTheSingularityFromBelowTheCut()
{
Assert.IsType<Entity.Limitf>(MathS.FromString("Ci(x)").Limit("x", 0, ApproachFrom.Left));
Assert.IsType<Entity.Limitf>(MathS.FromString("Chi(x) - ln(-x)").Limit("x", 0, ApproachFrom.Left));
}

private static ApproachFrom Side(string side)
=> side switch { "left" => ApproachFrom.Left, "right" => ApproachFrom.Right, _ => ApproachFrom.BothSides };

/// <summary>
/// Ei, Ci and Chi are <c>eulergamma + ln|u|</c>, or <c>ln u</c> for Ci and Chi, plus what tends to 0 as <c>u</c>
/// does, and li(u) is Ei(ln u): each is <c>-oo</c> at its singularity along the real line, and a sum holding one
/// with a bounded coefficient has the limit of the same sum with the logarithm in its place. Each was left
/// unevaluated, and the antiderivative of <c>ln(x) erf(x)</c> at 0, <c>ln(x)</c> against <c>Ei(-x^2)</c>,
/// spent the whole budget of limits a definite integral's bound is taken within before being left so. Each value
/// was checked against mpmath at 1e-10 from the side approached.
/// https://github.com/asc-community/AngouriMath/issues/2661
/// </summary>
[Theory]
[InlineData("Ei(x)", "0", "both", "-oo")]
[InlineData("Ei(-x^2)", "0", "both", "-oo")]
[InlineData("Ei(-1/x^2)", "+oo", "both", "-oo")]
[InlineData("Ci(x)", "0", "right", "-oo")]
[InlineData("Chi(x)", "0", "right", "-oo")]
[InlineData("li(x)", "1", "both", "-oo")]
[InlineData("Ei(x) - ln(x)", "0", "right", "eulergamma")]
[InlineData("Ei(x) - ln(-x)", "0", "left", "eulergamma")]
[InlineData("Ci(x) - ln(x)", "0", "right", "eulergamma")]
[InlineData("Chi(x) - ln(x)", "0", "right", "eulergamma")]
[InlineData("Ei(-x^2)/2 - ln(x)", "0", "right", "eulergamma/2")]
[InlineData("li(x) - ln(ln(x))", "1", "right", "eulergamma")]
[InlineData("li(x) - ln(-ln(x))", "1", "left", "eulergamma")]
[InlineData("Ei(2x) - Ei(x)", "0", "right", "ln(2)")]
[InlineData("Ei(x)/ln(x)", "0", "right", "1")]
[InlineData("Ei(x) + 1/x", "0", "right", "+oo")]
[InlineData("x Ei(x)", "0", "both", "0")]
public void HasItsLimitAtItsLogarithmicSingularity(string input, string destination, string side, string expected)
{
var limit = MathS.FromString(input).Limit("x", MathS.FromString(destination), Side(side));
Assert.Equal(MathS.FromString(expected).Simplify(), limit.Simplify());
}
}
}
24 changes: 22 additions & 2 deletions Sources/Tests/UnitTests/Calculus/ImproperIntegralTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,33 @@ public void ABoundWhoseLimitIsNotDecidedLeavesTheIntegralAsWritten(string integr
Entity? result = null;
var task = System.Threading.Tasks.Task.Run(() => result = Definite(integrand, "0", "1"));
// The budget bounds the work, in limits; this wall is a backstop against a hang that the budget no longer
// bounds, not a measure of time. The erf integral takes about nine seconds alone, and a wall of sixty
// fired in the full suite on a loaded machine.
// bounds, not a measure of time. The erf integral took fourteen seconds alone, ten of them one limit spending
// the whole budget, and a wall of sixty fired in the full suite on a loaded machine; it takes about four
// since Ei's limit at 0 is read (https://github.com/asc-community/AngouriMath/issues/2661). Written as
// erf(x) ln(x) it is answered (see ABoundAtTheExponentialIntegralsSingularityIsItsLimit), but the limit
// through this spelling's antiderivative is still not decided, and Simplify keeps the integral as written.
Assert.True(task.Wait(System.TimeSpan.FromMinutes(5)), $"the integral of {integrand} from 0 to 1 was still being simplified");
Assert.IsType<Entity.Integralf>(result);
Assert.DoesNotContain(result!.Nodes, node => node == MathS.NaN);
}

/// <summary>
/// The antiderivative of <c>erf(x) ln(x)</c> holds <c>ln(x)</c> against <c>Ei(-x^2) / (2 sqrt(pi))</c>, an infinity
/// less an infinity at 0 that is <c>-(1 + eulergamma/2) / sqrt(pi)</c> once Ei is read as the logarithm it differs
/// from by what tends to 0. The integral over <c>[0, 1]</c> was left as written, after ten seconds spent on that
/// limit; it is <c>-0.2613483600213989642</c>, which mpmath's quadrature gives to thirty digits.
/// https://github.com/asc-community/AngouriMath/issues/2661
/// </summary>
[Fact]
public void ABoundAtTheExponentialIntegralsSingularityIsItsLimit()
{
var value = "erf(x) * ln(x)".ToEntity().Integrate("x", 0, 1);
Assert.False(value.Nodes.Any(node => node is Entity.Integralf or Entity.Limitf), value.Stringize());
var closedForm = "(1 - Ei(-1)/2 - 1/e + eulergamma/2)/sqrt(pi) - erf(1)".ToEntity();
Assert.True((value - closedForm).EvalNumerical().Abs() < 1e-25, $"was {value}");
Assert.True((value - "-0.2613483600213989642354593897".ToEntity()).EvalNumerical().Abs() < 1e-25, $"was {value}");
}

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