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
38 changes: 38 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,44 @@ checked against mpmath's quadrature to forty digits
| `"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 definite integral across a singularity of Ei, Ci, Chi or li diverges, and li below 0 has its limits

**Not silent: wrong values become `NaN`, the right value, or no answer.** `Ei(u)`, `Ci(u)` and `Chi(u)` are `ln|u|`
plus a continuous part near `u = 0`, and `li(u)` near `u = 1`, and `Ci`, `Chi` and `li` also step by `i pi` there or at
`u = 0`. A definite integral was taken through them as though they were continuous, so a divergent integral across the
singularity had a value, `cos(x)/x` over `[-1, 1]` a complex one, and a convergent one across a step of `Chi` was off
by it. They are now points where the antiderivative is taken piece by piece, as a pole is, and a divergent integral
there has no value, as `1/x` over `[-1, 1]` has none. `Ei(u)` tends to `i pi sgn(Im u)`, not 0, as `u` tends to `-oo`
off the real line, and `li(u)` to `i pi sgn(arg u)` as `u` tends to 0 off the positive reals. `Ci` and `Chi` at 0 from
below are `-oo`, as `ln(x)` is, and a sum holding `Ei`, `Ci`, `Chi` or `li` at its singularity is read through
`eulergamma + ln(u)` from below and off the real line as it was from above, with `eulergamma` times its coefficient
taken out before the limit, which a sum holding the constant beside two logarithms did not reach. A step function's
integral over a unit interval found for every step at once can be 0 times an infinity at one step, and where the sum is
`NaN` over finitely many steps each is integrated on its own. Measured on master `409d68805` and on this branch; values
checked against mpmath ([#2716](https://github.com/asc-community/AngouriMath/issues/2716)).

| `.Integrate("x", a, b).Simplify()` or `.Limit` | Was (`409d68805`) | Now |
|---|---|---|
| `e^x/x` over `[-1, 2]` | `Ei(2) - Ei(-1)` | `NaN` |
| `1/ln(x)` over `[1/2, 3/2]` | `li(3/2) - li(1/2)` | `NaN` |
| `cos(x)/x` over `[-1, 1]` | `Ci(1) - Ci(-1)`, which is `-pi i` | `NaN` |
| `cosh(x)/x` over `[-1, 2]` | `Chi(2) - Chi(-1)` | `NaN` |
| `cos(x^2)/x` over `[-pi, pi]` | `0` | `NaN` |
| `abs(x - 1/2) Chi(-x)` over `[-1, 1]` | `-0.1956 + 0.3927i` | `-0.1956214470 + 0.7853981634i` |
| `(e^x - 1)/x` over `[-1, 2]` | `integral((e ^ x - 1) / x, x, -1, 2)` | `Ei(2) - Ei(-1) - ln(2)` |
| `ln(x) li(x)` over `[0, 1]` | `integral(li(x) * ln(x), x, 0, 1)` | `-1/2 + ln(2)` |
| `ln(ln(x))` over `[0, 2]` | `integral(ln(ln(x)), x, 0, 2)` | `2 ln(ln(2)) - li(2) + i pi` |
| `x li(sqrt(x))` over `[-2, 2]` | `-3.3312 - 4.3393i` | `-3.3312006070 - 5.9100957016i` |
| `floor(x)/x` over `[0, 1]` | `NaN` | `0` |
| `floor(x)/x` over `[-2, 1]` | `NaN` | `+oo` |
| `floor(x) Si(1/x)` over `[-pi, pi]` | `NaN` | `5.7973149191...` |
| `ceil(x)/x` over `[-1, 0]` | `NaN` | `0` |
| `"Ei(2 ln(-x))".Limit("x", 0, ApproachFrom.Right)` | `0` | `i pi` |
| `"li(x)".Limit("x", 0, ApproachFrom.Left)` | `limitleft(li(x), x, 0)` | `i pi` |
| `"Ci(x)".Limit("x", 0, ApproachFrom.Left)` | `limitleft(Ci(x), x, 0)` | `-oo` |
| `"Ci(x) - ln(x)".Limit("x", 0, ApproachFrom.Left)` | `limitleft(Ci(x) - ln(x), x, 0)` | `eulergamma` |
| `sin(x)/x` over `[-1, 1]` | `Si(1) - Si(-1)` | 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 @@ -31,7 +31,8 @@ namespace AngouriMath.Functions.Algebra
/// <para>
/// The places read are where a node of the antiderivative stops being continuous: the zeros
/// of a denominator, of a base raised to a negative power, of a logarithm's argument, of the
/// argument of <c>sgn</c>, <c>arccot</c>, <c>arcsec</c> and <c>arccsc</c>, and the zeros of the
/// argument of <c>sgn</c>, <c>arccot</c>, <c>arcsec</c>, <c>arccsc</c>, <c>Ei</c>, <c>Ci</c> and
/// <c>Chi</c>, the zeros of the argument of <c>li</c> and where it is 1, and the zeros of the
/// cosine or the sine under a tangent, secant, cotangent or cosecant of a linear argument.
/// Where one of them cannot be listed -- a root with a symbol in it, a periodic argument
/// that is not linear, more breaks than <see cref="MostBreaks"/> -- the caller is told so,
Expand Down Expand Up @@ -87,6 +88,16 @@ internal static class AntiderivativeBreaks
Arccotanf(var argument) when argument.ContainsNode(x) => ZerosOf(argument, x, lower, upper, breaks),
Arcsecantf(var argument) when argument.ContainsNode(x) => ZerosOf(argument, x, lower, upper, breaks),
Arccosecantf(var argument) when argument.ContainsNode(x) => ZerosOf(argument, x, lower, upper, breaks),
// Ei, Ci and Chi are ln(abs(u)) plus a continuous part near u = 0, and Ci and Chi take i pi more
// below it; li(u) = Ei(ln(u)) is ln(abs(u - 1)) plus a continuous part near u = 1, and steps by
// i pi at u = 0. Taken across one, the integral of e^x/x over [-1, 2] was Ei(2) - Ei(-1), and that
// of cos(x)/x over [-1, 1] was -i pi, where both diverge.
// https://github.com/asc-community/AngouriMath/issues/2716
Eif(var argument) when argument.ContainsNode(x) => ZerosOf(argument, x, lower, upper, breaks),
Cif(var argument) when argument.ContainsNode(x) => ZerosOf(argument, x, lower, upper, breaks),
Chif(var argument) when argument.ContainsNode(x) => ZerosOf(argument, x, lower, upper, breaks),
Lif(var argument) when argument.ContainsNode(x) => ZerosOf(argument, x, lower, upper, breaks)
&& ZerosOf((argument - 1).InnerSimplified, x, lower, upper, breaks),
Tanf(var argument) when argument.ContainsNode(x) => PeriodicZeros(argument, x, lower, upper, halfPeriodShift: true, breaks),
Secantf(var argument) when argument.ContainsNode(x) => PeriodicZeros(argument, x, lower, upper, halfPeriodShift: true, breaks),
Cotanf(var argument) when argument.ContainsNode(x) => PeriodicZeros(argument, x, lower, upper, halfPeriodShift: false, breaks),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -417,9 +417,36 @@ private static bool HasAStep(Entity expr, Variable x)
var sum = new Summationf(overUnitInterval, n, Integer.Create(firstWhole), lastInterval).InnerSimplified;
if (sum is Summationf)
return null;
return (total + sum).InnerSimplified;
var answer = (total + sum).InnerSimplified;
// The integral over a unit interval, found for every n at once, can be 0 times an infinity at one n where the
// piece's own integral is a number: n (ln(n + 1) - ln(n)) at n = 0, where floor(x)/x is 0. Summed, the integral
// of floor(x)/x over [0, 1] was NaN where it is 0, and that of floor(x) Si(1/x) over [-pi, pi] NaN where it is
// 5.7973. Over finitely many unit intervals, each is then integrated with its step put in first, and where one is
// not found the integral is left as written, since the NaN does not say it diverges.
// https://github.com/asc-community/AngouriMath/issues/2716
if (answer.IsNaN && upper is not null && Floor(upper).Subtract(firstWhole).CompareTo(EInteger.FromInt32(MostUnitIntervals)) <= 0)
{
Entity byPieces = total;
for (var k = firstWhole; k.CompareTo(Floor(upper)) < 0; k = k.Add(EInteger.One))
{
// Simplified first, so that 0/x on [0, 1] is 0 before it is integrated.
var known = expr
.Substitute(new Floorf(x), Integer.Create(k))
.Substitute(new Ceilf(x), Integer.Create(k.Add(EInteger.One)))
.InnerSimplified;
var onePiece = known.Integrate(x, Integer.Create(k), Integer.Create(k.Add(EInteger.One)));
if (onePiece.Nodes.Any(node => node is Integralf))
return null;
byPieces += onePiece;
}
return byPieces.InnerSimplified;
}
return answer;
}

/// <summary>The most unit intervals integrated one at a time where their sum is undefined.</summary>
private const int MostUnitIntervals = 64;

/// <summary>
/// The integral from <paramref name="from"/> to <paramref name="to"/> on a piece where
/// the floor is <paramref name="floorValue"/> throughout (and the ceiling one more,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,34 @@ private static bool HoldsAnIncompleteGammaFunction(Entity expr)
return false;
}

/// <summary>
/// Whether <paramref name="antiderivative"/> holds <c>Ei</c>, <c>Ci</c>, <c>Chi</c> or <c>li</c> of an argument that
/// is an infinity at the finite <paramref name="bound"/>, or <c>li</c> of one that is 0 there.
/// </summary>
/// <remarks>
/// There the function's value is its limit along the real line, and an argument off it tends to another: the
/// antiderivative of <c>li(-x)</c> holds <c>Ei(2 ln(-x))</c>, which is <c>Ei(-oo) = 0</c> at 0 and tends to
/// <c>i pi</c> from the right, where <c>2 ln(-x)</c> is <c>-oo + 2 pi i</c>, and the integral over <c>[0, 1]</c> was
/// <c>i pi</c> off. So the limit from inside the range is taken.
/// https://github.com/asc-community/AngouriMath/issues/2716
/// </remarks>
private static bool AtABranchPointOfAnIntegralFunction(Entity antiderivative, Variable x, Entity bound)
{
if (bound.Evaled is not Number.Real { IsFinite: true })
return false;
return antiderivative.Nodes.Any(node => node switch
{
Eif(var argument) => IsAnInfinityThere(argument),
Cif(var argument) => IsAnInfinityThere(argument),
Chif(var argument) => IsAnInfinityThere(argument),
Lif(var argument) => IsAnInfinityThere(argument)
|| argument.ContainsNode(x) && argument.Substitute(x, bound).Evaled is Number.Complex { IsZero: true },
_ => false
});
bool IsAnInfinityThere(Entity argument)
=> argument.ContainsNode(x) && argument.Substitute(x, bound).Evaled is Number.Complex { IsFinite: false, IsNaN: false };
}

/// <summary>
/// The antiderivative's value at one bound of a definite integral: its value there, and
/// where that is undefined, its limit as the bound is approached from inside the range,
Expand Down Expand Up @@ -370,7 +398,7 @@ private static bool HoldsAnIncompleteGammaFunction(Entity expr)
&& antiderivative.Nodes.Any(node => node is SumOverSetf sum && sum.ContainsNode(x)))
return AtAnInfiniteBoundThroughSumsOverRoots(antiderivative, x, bound, otherBound, continuousInside, infinite.IsPositiveInfinity());
var value = antiderivative.Substitute(x, bound);
if (!value.InnerSimplified.IsNaN)
if (!value.InnerSimplified.IsNaN && !AtABranchPointOfAnIntegralFunction(antiderivative, x, bound))
return continuousInside && AtAnInfiniteBound(antiderivative, x, bound, value) is { } atInfinity ? atInfinity : value;
// The bounds' values, so that a bound written as pi or 2 pi is placed as well as 3.
ApproachFrom? side = (bound.Evaled, otherBound.Evaled) switch
Expand Down
Loading
Loading