From 31f10d8cf1c088c5cbd16cd78c05c3c5fb9a3b66 Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Sat, 5 Dec 2026 23:44:02 +0000 Subject: [PATCH] Diverge across a singularity of Ei, Ci, Chi or li, and integrate a step times a pole piecewise A definite integral whose antiderivative is Ei, Ci, Chi or li of something that passes through 0 (or li's argument through 1) was evaluated by substituting the bounds, so the logarithmic singularity inside the range was never seen: exp(x)/x over [-1, 2], 1/ln(x) over [1/2, 2] and cos(x)/x over [-1, 1] all came back finite although each diverges. The antiderivative breaks there, so it is now listed in AntiderivativeBreaks.Inside, and the one-sided limits at the break decide: -oo - (-oo) is NaN, as for 1/x, and a range ending at the singularity gets the infinity of its one side. Si and Shi are entire and do not break. Taking those limits needed the limit solver to know what the functions do off the positive reals. Ei(u) tends to i pi sgn(Im u) as Re u -> -oo off the real line, li(u) to i pi sgn(arg u) as u -> 0 off the positive reals, and below 0 Ci and Chi go to -oo like gamma + ln u. The logarithmic-singularity rule kept gamma inside the limit of ln(x) - ln|x|, which it cannot take at 0-; it now adds gamma afterwards, which turns (exp(x) - 1)/x over [-1, 2] from unevaluated into its value. A bound sitting on a branch point of one of these functions is now approached from inside rather than substituted. A step times a pole at the step's zero, such as floor(x)/x over [0, 1], gave 0/0 = NaN where the integral converges. When the unit-interval sum is NaN, each unit interval is now integrated with the step replaced by its constant there, up to 64 intervals, and nothing is answered if any piece stays unevaluated. Every changed answer was checked against mpmath at 50 digits; they are in BREAKING-CHANGES.md. Co-Authored-By: Claude Opus 5.5 --- BREAKING-CHANGES.md | 38 ++++++ .../Integration/AntiderivativeBreaks.cs | 13 +- .../Integration/BreakpointIntegration.cs | 29 ++++- .../Integration/Integration.Definition.cs | 30 ++++- .../Limits/Solvers/Limit.Classes.cs | 123 +++++++++++++++--- .../Limits/Solvers/Solvers.Definition.cs | 25 ++-- ...ithmicSingularityOfASpecialFunctionTest.cs | 115 ++++++++++++++++ .../ErrorAndIntegralFunctionLimitsTest.cs | 14 +- 8 files changed, 352 insertions(+), 35 deletions(-) create mode 100644 Sources/Tests/UnitTests/Calculus/AnIntegralAcrossALogarithmicSingularityOfASpecialFunctionTest.cs diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index 4719df037..f5a927c5b 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -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 diff --git a/Sources/AngouriMath/Functions/Continuous/Integration/AntiderivativeBreaks.cs b/Sources/AngouriMath/Functions/Continuous/Integration/AntiderivativeBreaks.cs index 41875dfc2..85f8b5d13 100644 --- a/Sources/AngouriMath/Functions/Continuous/Integration/AntiderivativeBreaks.cs +++ b/Sources/AngouriMath/Functions/Continuous/Integration/AntiderivativeBreaks.cs @@ -31,7 +31,8 @@ namespace AngouriMath.Functions.Algebra /// /// 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 sgn, arccot, arcsec and arccsc, and the zeros of the + /// argument of sgn, arccot, arcsec, arccsc, Ei, Ci and + /// Chi, the zeros of the argument of li 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 -- the caller is told so, @@ -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), diff --git a/Sources/AngouriMath/Functions/Continuous/Integration/BreakpointIntegration.cs b/Sources/AngouriMath/Functions/Continuous/Integration/BreakpointIntegration.cs index 31db98e5d..92dc34730 100644 --- a/Sources/AngouriMath/Functions/Continuous/Integration/BreakpointIntegration.cs +++ b/Sources/AngouriMath/Functions/Continuous/Integration/BreakpointIntegration.cs @@ -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; } + /// The most unit intervals integrated one at a time where their sum is undefined. + private const int MostUnitIntervals = 64; + /// /// The integral from to on a piece where /// the floor is throughout (and the ceiling one more, diff --git a/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs b/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs index c17fa3a90..bbb26437f 100644 --- a/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs +++ b/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs @@ -341,6 +341,34 @@ private static bool HoldsAnIncompleteGammaFunction(Entity expr) return false; } + /// + /// Whether holds Ei, Ci, Chi or li of an argument that + /// is an infinity at the finite , or li of one that is 0 there. + /// + /// + /// There the function's value is its limit along the real line, and an argument off it tends to another: the + /// antiderivative of li(-x) holds Ei(2 ln(-x)), which is Ei(-oo) = 0 at 0 and tends to + /// i pi from the right, where 2 ln(-x) is -oo + 2 pi i, and the integral over [0, 1] was + /// i pi off. So the limit from inside the range is taken. + /// https://github.com/asc-community/AngouriMath/issues/2716 + /// + 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 }; + } + /// /// 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, @@ -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 diff --git a/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Limit.Classes.cs b/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Limit.Classes.cs index f058d2089..55a04b859 100644 --- a/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Limit.Classes.cs +++ b/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Limit.Classes.cs @@ -10,6 +10,7 @@ namespace AngouriMath using AngouriMath.Core.Exceptions; using AngouriMath.Core.Multithreading; using Core; + using System.Collections.Generic; using System.Linq; using static Functions.Algebra.LimitFunctional; partial record Entity @@ -770,22 +771,35 @@ partial record Arccosf if (limit.Evaled is Real { IsFinite: false, IsNaN: false } infinity) { var upwards = !infinity.IsNegative; - return node switch - { - Erff => upwards ? Integer.One : Integer.MinusOne, - Erfcf => upwards ? Integer.Zero : Integer.Create(2), - Erfif or Shif => upwards ? Real.PositiveInfinity : Real.NegativeInfinity, - Sif => upwards ? MathS.pi / 2 : -MathS.pi / 2, - Cif => upwards ? Integer.Zero : null, - Chif or Lif => upwards ? Real.PositiveInfinity : null, - Eif => upwards ? Real.PositiveInfinity : Integer.Zero, - _ => null - }; + // An argument off the real line tends to the same infinity with an imaginary part beside it, which some of + // these read: Ei(z) is -E1(-z) + i pi sgn(Im z) and E1(-z) tends to 0 as Re z tends to -oo, so Ei(2 ln(-x)) + // at 0 from the right, where the argument is -oo + 2 pi i, tends to i pi, and was 0. Erf, erfc, Si and Ci + // at +oo tend to their values for any such part; Ei at +oo, Shi, Chi, li and erfi do not, and are not + // answered. https://github.com/asc-community/AngouriMath/issues/2716 + if (ImaginarySignBeside(argument, x, dist, side) is { } imaginary and not 0) + return node switch + { + Eif when !upwards => imaginary * MathS.pi * MathS.i, + Erff or Erfcf or Sif => OfAnErrorOrIntegralFunctionAtInfinity(node, upwards), + Cif when upwards => Integer.Zero, + _ => null + }; + return OfAnErrorOrIntegralFunctionAtInfinity(node, upwards); } 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; + // li(u) = Ei(ln u) tends to 0 as u tends to 0 through the positive numbers, and to i pi sgn(arg u) from + // anywhere else, where ln u is -oo + i arg u. https://github.com/asc-community/AngouriMath/issues/2716 + if (node is Lif && value == Integer.Zero) + return (RealSignBeside(argument, x, dist, side), ImaginarySignBeside(argument, x, dist, side)) switch + { + (1, _) => Integer.Zero, + (-1, _) => MathS.pi * MathS.i, + (_, int imaginary and not 0) => imaginary * MathS.pi * MathS.i, + _ => null + }; 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; @@ -804,6 +818,74 @@ partial record Arccosf }; } + /// What an error function or an exponential or trigonometric integral tends to at a real infinity. + private static Entity? OfAnErrorOrIntegralFunctionAtInfinity(Entity node, bool upwards) + => node switch + { + Erff => upwards ? Integer.One : Integer.MinusOne, + Erfcf => upwards ? Integer.Zero : Integer.Create(2), + Erfif or Shif => upwards ? Real.PositiveInfinity : Real.NegativeInfinity, + Sif => upwards ? MathS.pi / 2 : -MathS.pi / 2, + Cif => upwards ? Integer.Zero : null, + Chif or Lif => upwards ? Real.PositiveInfinity : null, + Eif => upwards ? Real.PositiveInfinity : Integer.Zero, + _ => null + }; + + /// + /// The points beside on the side approached at which an argument is read, 10^-6 and + /// 10^-9 away from a finite one and 10^2 and 10^3 out towards an infinity; none where it is not a real number. + /// Witnesses, not proofs. + /// + private static IEnumerable ReadBeside(Entity dist, ApproachFrom side) + { + if (dist.Evaled is not Real { IsNaN: false } at) + yield break; + if (!at.IsFinite) + { + yield return at.IsNegative ? -100 : 100; + yield return at.IsNegative ? -1000 : 1000; + yield break; + } + foreach (var sign in side switch { ApproachFrom.Left => new[] { -1 }, ApproachFrom.Right => new[] { 1 }, _ => new[] { -1, 1 } }) + { + yield return at + Rational.Create(sign, 1_000_000); + yield return at + Rational.Create(sign, 1_000_000_000); + } + } + + /// + /// The sign of 's imaginary part beside the destination, 0 where it is real there, or + /// where its values there are not finite numbers or their imaginary parts differ in sign. + /// + private static int? ImaginarySignBeside(Entity argument, Variable x, Entity dist, ApproachFrom side) + { + int? sign = null; + foreach (var point in ReadBeside(dist, side)) + { + if (argument.Substitute(x, point).Evaled is not Complex { IsFinite: true, IsNaN: false } value) + return null; + var here = value.ImaginaryPart.EDecimal.Sign; + if (sign is { } earlier && earlier != here) + return null; + sign = here; + } + return sign; + } + + /// + /// 1 where is a positive real number at each point beside the destination, -1 where + /// it is a negative one at each, and otherwise. + /// + private static int? RealSignBeside(Entity argument, Variable x, Entity dist, ApproachFrom side) + { + var values = ReadBeside(dist, side).Select(point => argument.Substitute(x, point).Evaled).ToList(); + if (values.Count == 0 || values.Any(value => value is not Real { IsFinite: true, IsZero: false })) + return null; + var signs = values.Select(value => ((Real)value).IsPositive ? 1 : -1).Distinct().ToList(); + return signs.Count == 1 ? signs[0] : null; + } + /// /// -oo 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 -oo along the @@ -813,7 +895,7 @@ partial record Arccosf /// Ei(u) = gamma + ln|u| + O(u) for real u of either sign, and li(u) = Ei(ln u), so each /// tends to -oo as u reaches its singularity along the real line. Ci(u) and Chi(u) /// are gamma + ln u + O(u^2) with the principal logarithm, which is -oo from above and - /// -oo + pi i from below, so only the approach from above is answered. Undecided, Ei at 0 was asked again + /// -oo + pi i from below, the infinity ln(x) tends to at 0 from the left. Undecided, Ei at 0 was asked again /// by every rewrite the search for a limit holding it made: the antiderivative of erf(x) ln(x) at 0 /// asked the limit of Ei(-1/x^2) at +oo 574 times of the 2000 its budget allows, and the /// integral over [0, 1] took sixteen seconds to be left as written. Checked against mpmath at @@ -822,16 +904,17 @@ partial record Arccosf /// private static Entity? AtTheLogarithmicSingularity(Entity node, Entity argument, Number value, Variable x, Entity dist, ApproachFrom side) { - var (logarithmOf, bothSigns) = node switch + Entity? logarithmOf = 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) + Eif or Cif or Chif when value == Integer.Zero => argument, + Lif when value == Integer.One => MathS.Ln(argument), + _ => null }; - if (logarithmOf is null) - return null; - return SignOnEachSide(logarithmOf, x, dist, side) is { } sign && (sign is 1 || bothSigns && sign is -1) + // Ci and Chi below 0 are the same infinity with i pi beside it, as ln(x) is at 0 from the left, and each is + // the infinity ln(u) tends to, ln|u| plus a bounded imaginary part, where u comes off the real line. + // https://github.com/asc-community/AngouriMath/issues/2716 + return logarithmOf is not null + && (SignOnEachSide(logarithmOf, x, dist, side) is 1 or -1 || ImaginarySignBeside(logarithmOf, x, dist, side) is 1 or -1) ? Real.NegativeInfinity : null; } diff --git a/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Solvers.Definition.cs b/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Solvers.Definition.cs index 11978f1e2..565e8024d 100644 --- a/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Solvers.Definition.cs +++ b/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Solvers.Definition.cs @@ -539,15 +539,19 @@ private static Entity ExpandLogarithm(Entity expr) /// /// /// Only along the real line: ln|u| is Ei's logarithmic part for a real u of either sign, and - /// ln u is Ci's and Chi's for a positive one. The principal logarithm of a negative - /// u is ln|u| + pi i, which is where they take theirs, and that is not asked. + /// the principal ln u is Ci's and Chi's, ln|u| + pi i for a negative one, so that + /// Ci(x) - ln(x) at 0 from the left is gamma. Off the real line the principal ln u is the + /// logarithmic part of all three, so that x Ci(sqrt(x)) at 0 from the left, where sqrt(x) is + /// imaginary, is 0 as it is from the right. + /// https://github.com/asc-community/AngouriMath/issues/2716 /// /// 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 + // Ei's logarithmic part is ln|u|, and Ci's and Chi's ln(u), the principal logarithm, of either sign. + var (argument, singularAt, logarithmOf, ofTheModulus) = node switch { Eif(var u) => (u, (Entity)Integer.Zero, u, true), Cif(var u) => (u, Integer.Zero, u, false), @@ -556,8 +560,10 @@ private static (bool Asked, Entity? Limit) ThroughALogarithmicSingularity(Entity _ => (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)) + || ComputeLimit(argument, x, dest, side) is not { } atTheSingularity || atTheSingularity.Evaled != singularAt) + continue; + var sign = SignOnEachSide(logarithmOf, x, dest, side); + if (sign is 0 || sign is null && !IsNotRealBeside(logarithmOf, x, dest, side)) continue; var standIn = Variable.CreateUnique(expr, "u"); var linear = expr.Substitute(node, standIn); @@ -567,9 +573,12 @@ private static (bool Asked, Entity? Limit) ThroughALogarithmicSingularity(Entity || 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); - var withTheLogarithm = linear.Substitute(standIn, logarithmicPart); - return (true, ByGruntz(withTheLogarithm, x, dest, side) ?? ComputeLimit(withTheLogarithm, x, dest, side, acceptNaN)); + // A + c (gamma + L) is A + c L, whose limit is asked, plus c gamma, whose limit is that of c: with the + // constant inside, the limit of gamma + ln(x) - ln(abs(x)) at 0, from the antiderivative of (e^x - 1)/x, + // was not decided where that of ln(x) - ln(abs(x)) is. https://github.com/asc-community/AngouriMath/issues/2716 + var withTheLogarithm = linear.Substitute(standIn, MathS.Ln(sign is -1 && ofTheModulus ? -logarithmOf : logarithmOf)); + var withoutTheConstant = ByGruntz(withTheLogarithm, x, dest, side) ?? ComputeLimit(withTheLogarithm, x, dest, side, acceptNaN); + return (true, withoutTheConstant is null ? null : (withoutTheConstant + coefficientLimit * MathS.eulergamma).InnerSimplified); } return (false, null); } diff --git a/Sources/Tests/UnitTests/Calculus/AnIntegralAcrossALogarithmicSingularityOfASpecialFunctionTest.cs b/Sources/Tests/UnitTests/Calculus/AnIntegralAcrossALogarithmicSingularityOfASpecialFunctionTest.cs new file mode 100644 index 000000000..6b0d60bf6 --- /dev/null +++ b/Sources/Tests/UnitTests/Calculus/AnIntegralAcrossALogarithmicSingularityOfASpecialFunctionTest.cs @@ -0,0 +1,115 @@ +// +// Copyright (c) 2019-2026 Angouri. +// AngouriMath is licensed under MIT. +// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md. +// Website: https://am.angouri.org. +// + +using AngouriMath.Core; +using AngouriMath.Extensions; +using Xunit; +using static AngouriMath.Entity; + +namespace AngouriMath.Tests.Calculus +{ + /// + /// Ei, Ci and Chi are ln(abs(u)) plus a continuous part near u = 0, and li + /// near u = 1; 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 the integral of e^x/x over + /// [-1, 2] was Ei(2) - Ei(-1) where it diverges, and that of x li(sqrt(x)) over [-2, 2] + /// was pi/2 off in its imaginary part. Values checked against mpmath. + /// https://github.com/asc-community/AngouriMath/issues/2716 + /// + [Trait("Area", "Calculus")] + public sealed class AnIntegralAcrossALogarithmicSingularityOfASpecialFunctionTest + { + [Theory] + [InlineData("exp(x)/x", "-1", "2")] + [InlineData("exp(2x)/x", "-1", "2")] + [InlineData("exp(-x)/x", "-1", "1")] + [InlineData("exp(x + 1)/(1 + x)", "-2", "2")] + [InlineData("1/ln(x)", "1/2", "3/2")] + [InlineData("1/ln(x)", "1/2", "2")] + [InlineData("x/ln(x)", "1/2", "2")] + [InlineData("cos(x)/x", "-1", "1")] + [InlineData("cos(x)/x", "-1", "2")] + [InlineData("cosh(x)/x", "-1", "2")] + [InlineData("cos(x^2)/x", "-pi", "pi")] + public void ADivergentIntegralHasNoValue(string integrand, string from, string to) + { + var integral = integrand.ToEntity().Integrate("x", from.ToEntity(), to.ToEntity()).Simplify(); + Assert.True(integral.Evaled.IsNaN, $"{integrand} over [{from}, {to}] was {integral}"); + } + + [Theory] + [InlineData("exp(x)/x", "0", "2", "+oo")] + [InlineData("exp(x)/x", "-2", "0", "-oo")] + [InlineData("cos(x)/x", "0", "1", "+oo")] + public void AnIntegralDivergingToOneSideIsThatInfinity(string integrand, string from, string to, string expected) + => Assert.Equal(expected.ToEntity().Evaled, + integrand.ToEntity().Integrate("x", from.ToEntity(), to.ToEntity()).Simplify().Evaled); + + [Theory] + [InlineData("sin(x)/x", "-1", "1", "1.89216614073437", "0")] + [InlineData("sinh(x)/x", "-1", "2", "3.55881830873070", "0")] + [InlineData("(exp(x) - 1)/x", "-1", "2", "4.48047110983747", "0")] + [InlineData("Ei(x)", "-1", "1", "-0.674668505327186", "0")] + [InlineData("Ci(x)", "-1", "1", "-1.00813412381386", "3.14159265358979")] + [InlineData("Chi(x)", "-1", "1", "-0.674668505327186", "3.14159265358979")] + [InlineData("li(x)", "0", "2", "-0.877257534804065", "0")] + [InlineData("x * li(sqrt(x))", "-2", "2", "-3.33120060698478", "-5.91009570163854")] + [InlineData("x * li(sqrt(x))", "0", "2", "-2.22050315016790", "0")] + [InlineData("Ci(sqrt(x))", "-1", "1", "0.161377014376582", "1.57079632679490")] + [InlineData("x * Ci(sqrt(x))", "-1", "1", "-0.166759267132961", "-0.785398163397448")] + [InlineData("abs(x - 1/2) * Chi(-x)", "-1", "1", "-0.195621447040727", "0.785398163397448")] + [InlineData("ln(ln(x))", "0", "2", "-1.77818962128082", "3.14159265358979")] + public void AConvergentIntegralIsItsValue(string integrand, string from, string to, string re, string im) + { + var integral = integrand.ToEntity().Integrate("x", from.ToEntity(), to.ToEntity()).Simplify(); + Assert.DoesNotContain(integral.Nodes, node => node is Integralf); + var difference = (integral.EvalNumerical() - Number.Complex.Create(re.ToEntity().EvalNumerical().RealPart, + im.ToEntity().EvalNumerical().RealPart)).Abs(); + Assert.True(difference < 1e-12, $"{integrand} over [{from}, {to}] was {integral}"); + } + + /// + /// The limits the integrals above are taken through, at the singular point from a side where the argument is + /// off the positive reals. + /// + [Theory] + [InlineData("Ei(2 ln(-x))", "0", ApproachFrom.Right, "i pi")] + [InlineData("Ei(5 ln(sqrt(x)))", "0", ApproachFrom.Left, "i pi")] + [InlineData("Ei(-1/x + i)", "0", ApproachFrom.Right, "i pi")] + [InlineData("Ei(-1/x - i)", "0", ApproachFrom.Right, "-i pi")] + [InlineData("li(x)", "0", ApproachFrom.Left, "i pi")] + [InlineData("li(x)", "0", ApproachFrom.Right, "0")] + [InlineData("x li(-x) + Ei(2 ln(-x))", "0", ApproachFrom.Right, "i pi")] + [InlineData("Ci(x)", "0", ApproachFrom.Left, "-oo")] + [InlineData("Chi(x)", "0", ApproachFrom.Left, "-oo")] + [InlineData("Ci(x) - ln(x)", "0", ApproachFrom.Left, "eulergamma")] + [InlineData("Chi(x) - ln(x)", "0", ApproachFrom.Left, "eulergamma")] + public void ALimitThroughTheSingularity(string expr, string dest, ApproachFrom side, string expected) + { + var limit = expr.ToEntity().Limit("x", dest.ToEntity(), side); + Assert.DoesNotContain(limit.Nodes, node => node is Limitf); + Assert.True(limit.Evaled == expected.ToEntity().Evaled + || (limit - expected.ToEntity()).EvalNumerical().Abs() < 1e-12, $"{expr} at {dest} from the {side} was {limit}"); + } + + /// + /// A step function's integral over a unit interval, found for every step at once, was 0 times an infinity at + /// one step. + /// + [Theory] + [InlineData("floor(x)/x", "0", "1", "0")] + [InlineData("floor(x)/x", "0", "3", "1.50407739677627")] + [InlineData("floor(x) * Si(1/x)", "-pi", "pi", "5.79731491914639")] + public void AStepTimesAPoleAtItsZeroIsItsValue(string integrand, string from, string to, string expected) + { + var integral = integrand.ToEntity().Integrate("x", from.ToEntity(), to.ToEntity()).Simplify(); + Assert.DoesNotContain(integral.Nodes, node => node is Integralf); + Assert.True((integral - expected.ToEntity()).EvalNumerical().Abs() < 1e-12, + $"{integrand} over [{from}, {to}] was {integral}"); + } + } +} diff --git a/Sources/Tests/UnitTests/Calculus/ErrorAndIntegralFunctionLimitsTest.cs b/Sources/Tests/UnitTests/Calculus/ErrorAndIntegralFunctionLimitsTest.cs index 45f3c8e52..a68bc826a 100644 --- a/Sources/Tests/UnitTests/Calculus/ErrorAndIntegralFunctionLimitsTest.cs +++ b/Sources/Tests/UnitTests/Calculus/ErrorAndIntegralFunctionLimitsTest.cs @@ -49,18 +49,24 @@ public void IsOneSidedWhereItsArgumentDiverges() Assert.Equal(MathS.NaN, MathS.FromString("erf(1/x)").Limit("x", 0)); } - // Nothing is claimed on Ci's cut, nor at its singularity from below, where it is -oo + pi i. + // Nothing is claimed on Ci's cut. [Theory] [InlineData("Ci(x)", "-2")] [InlineData("Ci(x)", "-oo")] public void IsNotAnsweredOnACut(string input, string destination) => Assert.IsType(MathS.FromString(input).Limit("x", destination)); + /// + /// From below the cut Ci and Chi are eulergamma + ln(x) with the principal logarithm, ln|x| + pi i, + /// plus what tends to 0: the infinity ln(x) tends to, and eulergamma + pi i beside ln(-x), as + /// mpmath's chi(-1e-20) - log(1e-20) is. https://github.com/asc-community/AngouriMath/issues/2716 + /// [Fact] - public void IsNotAnsweredAtTheSingularityFromBelowTheCut() + public void IsAnsweredAtTheSingularityFromBelowTheCut() { - Assert.IsType(MathS.FromString("Ci(x)").Limit("x", 0, ApproachFrom.Left)); - Assert.IsType(MathS.FromString("Chi(x) - ln(-x)").Limit("x", 0, ApproachFrom.Left)); + Assert.Equal(Entity.Number.Real.NegativeInfinity, MathS.FromString("Ci(x)").Limit("x", 0, ApproachFrom.Left).Evaled); + var belowTheCut = MathS.FromString("Chi(x) - ln(-x)").Limit("x", 0, ApproachFrom.Left); + Assert.True((belowTheCut - MathS.FromString("eulergamma + i pi")).EvalNumerical().Abs() < 1e-12, $"{belowTheCut}"); } private static ApproachFrom Side(string side)