diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md
index 7f1a98f7d..cca333393 100644
--- a/BREAKING-CHANGES.md
+++ b/BREAKING-CHANGES.md
@@ -713,6 +713,66 @@ 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 |
+| `e^(x + 1)/(1 + x)` over `[-2, 2]` | `Ei(3) - Ei(-1)` | `NaN` |
+| `1/ln(x)` over `[1/2, 2]` | `li(2) - li(1/2)` | `NaN` |
+| `cos(x^2)/x` over `[-2, 2]` | `0` | `NaN` |
+
+The logarithms of an antiderivative are taken of their argument's modulus, to keep a real integral real, only where
+each that is negative in the range stands alone in it, `c ln(u) + G` with `c` constant and `G` free of it. Taking
+`ln|u|` for `ln(u)` drops `i pi` times the coefficient, which is a constant between the breaks only where the
+coefficient is. It had been taken in every antiderivative, so an integral of the logarithm of a negative number lost
+its imaginary part (`x ln(x) - x` lost `i pi x`), and with the breaks above it reached `ln(x) li(2x)` over `[-pi, pi]`,
+which came out as `pi (li(-2 pi) + li(2 pi)) (ln(pi) - 1)` before this was found and is left unevaluated, as on master.
+Measured on master `2ddbc2e4d` and on this branch.
+
+| `.Integrate("x", a, b).Simplify()` | Was (`2ddbc2e4d`) | Now |
+|---|---|---|
+| `ln(x)` over `[-2, -1]` | `2 ln(2) - 1` | `2 ln(-2) - 1 - i pi`, `0.3862943611 + 3.1415926536i` |
+| `x ln(x)` over `[-2, -1]` | `3/4 - 2 ln(2)` | `-0.6362943611 - 4.7123889804i` |
+| `ln(x)^2` over `[-2, -1]` | `2 + 2 ln(2)^2 - 4 ln(2)` | `-9.6812870955 + 2.4271590540i` |
+| `ln(x)/x` over `[-2, -1]` | `-ln(2)^2/2` | `-0.2402265070 - 2.1775860903i` |
+| `x^2 ln(x)` over `[-1, 1]` | `-2/9` | `-2/9 + i pi/3` |
+| `li(x)/x` over `[-2, -1/2]` | `li(-1/2) ln(1/2) - li(-2) ln(2) - 3/2` | `-0.0902310029 - 4.7805858066i` |
+| `artanh(x)` over `[1, 2]` | unevaluated | `0.9547712524 + 1.5707963268i` |
+| `1/x` over `[-2, -1]` | `-ln(2)` | 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 e89a97ce2..26678b55c 100644
--- a/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs
+++ b/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs
@@ -258,15 +258,41 @@ node is Signumf(var argument) && argument.ContainsNode(x)
private static Entity WithRealLogarithms(Entity antiderivative, Variable x, Number.Real lowest, Number.Real highest)
{
var samples = SamplePoints(lowest, highest);
- return antiderivative.Replace(node =>
+ var taken = new HashSet(antiderivative.Nodes.OfType().Where(node =>
node is Logf(var @base, var argument) and not Logf(_, Absf)
&& argument.ContainsNode(x) && IsRealOnTheRealLine(argument, x)
&& @base.Evaled is Number.Real { IsPositive: true, IsFinite: true } positive && positive != 1
// Only an argument seen to be negative in the range: elsewhere the logarithm is real already, and is
// left as it was written.
- && samples.Any(point => argument.Substitute(x, point).Evaled is Number.Real { IsNegative: true, IsFinite: true })
- ? new Logf(@base, new Absf(argument))
- : node);
+ && samples.Any(point => argument.Substitute(x, point).Evaled is Number.Real { IsNegative: true, IsFinite: true }))
+ );
+ // All of them or none: where one does not stand alone, the integral is not real anyway, and the limits at a
+ // break are found for the logarithms as written, ln(2)/2 for the antiderivative of artanh(x) at 1, where
+ // with some of them taken of the modulus they were left undecided.
+ // https://github.com/asc-community/AngouriMath/issues/2715
+ return taken.Count == 0 || !taken.All(node => StandsAlone(antiderivative, node, x)) ? antiderivative : antiderivative.Replace(node =>
+ node is Logf(var @base, var argument) && taken.Contains(node) ? new Logf(@base, new Absf(argument)) : node);
+ }
+
+ ///
+ /// Whether is c node + G, with c a constant and G free of
+ /// , so that a constant added to moves it by a constant.
+ ///
+ ///
+ /// Only then is i pi taken off a logarithm a constant taken off the antiderivative. The antiderivative of
+ /// ln(x) is x ln(x) - x, and with ln(abs(x)) it is i pi x off over [-2, -1]:
+ /// the integral there, 2 ln(2) - 1 + i pi, was 2 ln(2) - 1.
+ /// https://github.com/asc-community/AngouriMath/issues/2715
+ ///
+ private static bool StandsAlone(Entity antiderivative, Entity node, Variable x)
+ {
+ var standIn = Variable.CreateUnique(antiderivative, "u");
+ var coefficient = antiderivative.Substitute(node, standIn).Differentiate(standIn).InnerSimplified;
+ // A sign is constant between the breaks, where it jumps: 1/x sgn(sin(x)) over [-1, 0] is +oo, through
+ // sgn(sin(x)) ln(x). The derivative's conditions say where it has a value, not what the value is.
+ return !coefficient.ContainsNode(standIn)
+ && !coefficient.Replace(part => part switch { Providedf provided => provided.Expression, Signumf => Integer.One, _ => part })
+ .ContainsNode(x);
}
/// Finite points spread over the range, its finite bounds among them.
@@ -386,6 +412,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,
@@ -415,7 +469,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 bdab203d3..4a555a8c4 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
@@ -949,22 +950,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;
@@ -983,6 +997,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
@@ -992,7 +1074,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
@@ -1001,16 +1083,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 17e6e35e3..882447078 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..b6aed2b59
--- /dev/null
+++ b/Sources/Tests/UnitTests/Calculus/AnIntegralAcrossALogarithmicSingularityOfASpecialFunctionTest.cs
@@ -0,0 +1,140 @@
+//
+// 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 System.Linq;
+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}");
+ }
+
+ ///
+ /// A logarithm in the antiderivative was taken of its argument's modulus wherever the argument was negative in
+ /// the range, which drops i pi times its coefficient. That is a constant between the breaks only where the
+ /// coefficient is one. Through the breaks at the singularities of li(2x), the integral of
+ /// ln(x) li(2x) over [-pi, pi] was pi (li(-2 pi) + li(2 pi)) (ln(pi) - 1), 1.784 + 2.217i.
+ /// Left unevaluated is not wrong; a value must be the right one.
+ ///
+ [Theory]
+ [InlineData("ln(x) * li(2x)", "-pi", "pi", "-34.8874626534103", "2.17066986260916")]
+ [InlineData("ln(x) * li(2x)", "-pi", "-1", "-29.4941728019125", "5.26934525790215")]
+ [InlineData("ln(x) * li(2x)", "-1", "-1/2", "-5.63414529648856", "-0.446022010501057")]
+ [InlineData("x * ln(x)", "-2", "-1", "-0.636294361119891", "-4.71238898038469")]
+ [InlineData("ln(x)", "-2", "-1", "0.386294361119891", "3.14159265358979")]
+ [InlineData("1/x", "-2", "-1", "-0.693147180559945", "0")]
+ public void ALogarithmWithACoefficientKeepsItsImaginaryPart(string integrand, string from, string to, string re, string im)
+ {
+ var integral = integrand.ToEntity().Integrate("x", from.ToEntity(), to.ToEntity()).Simplify();
+ if (integral.Nodes.Any(node => node is Integralf))
+ return;
+ 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)