From 164c7850a59faebdd299f6b4e367c08c67582938 Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Sun, 6 Dec 2026 03:22:56 +0000 Subject: [PATCH] Keep the imaginary part of a definite integral through ln of a negative number Since #2616 a definite integral took ln(u) of abs(u) in the antiderivative wherever u was real and negative somewhere in the range. That drops i pi where u < 0, which leaves F(b) - F(a) unchanged only where ln(u) stands alone with a constant coefficient, as it does in the antiderivative of 1/u. In x ln(x) - x, the antiderivative of ln(x), it dropped i pi x, so ln(x) over [-2, -1] was 2 ln(2) - 1 instead of 2 ln(2) - 1 + i pi, and ln(x)^2, x^k ln(x), log(b, x), artanh and arcoth past 1 and ln of a linear argument were all answered without their imaginary parts. Now the moduli are taken only when every logarithm negative in the range stands alone: the antiderivative is c ln(u) + G, with c a constant (a sign counts as one between the breaks, where it jumps) and G free of it. Otherwise the integral is not real, and the antiderivative is left as written, whose one-sided limits at a break are also the ones the limit code decides. An antiderivative holding abs(u) or sgn(u) of an argument off the real line in the range is not an antiderivative, since d abs(u) = sgn(u) du and d sgn(u) = 0 only for real u. abs(ln(x)) over [-1, 1] was 0 and abs(ln(x - 1/2)) over [-pi, pi] negative. These are now left unevaluated. Ei(u) tends to i pi sgn(Im u), not 0, as Re u tends to -oo off the real line. And a bound where a logarithm's argument is 0 is now taken as the limit from inside the range, since ln there is -oo whichever side it comes from but a function of it is not. With both, li(-x) over [0, 1] and li(x^3) over [-1, 0] have their values where they were -oo. Checked against mpmath quad on 253 generated rows (the 25 sweep rows of groups B and H among them) and on 436 sweep rows master answered, which are unchanged. Co-Authored-By: Claude Opus 5.5 --- BREAKING-CHANGES.md | 29 +++++++ .../Integration/Integration.Definition.cs | 82 ++++++++++++++++-- .../Limits/Solvers/Limit.Classes.cs | 7 ++ .../Continuous/Limits/Transformations.cs | 21 +++++ ...NegativeNumberKeepsItsImaginaryPartTest.cs | 84 +++++++++++++++++++ 5 files changed, 216 insertions(+), 7 deletions(-) create mode 100644 Sources/Tests/UnitTests/Calculus/ALogarithmOfANegativeNumberKeepsItsImaginaryPartTest.cs diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index 4719df037..0c03da3d4 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -883,6 +883,35 @@ it was ([#2611](https://github.com/asc-community/AngouriMath/issues/2611)). | `integral(1/x, x, -2, -1)` | `ln(-1) - ln(-2)` | `-ln(2)` | | `integral(1/(x^2-1), x, -1/2, 1/2)` | `(ln(-1/3) - ln(-3)) / 2` | `ln(1/3)` | +### A definite integral through ln of a negative number keeps its imaginary part + +**Not silent: wrong values, and an `abs` integrated to 0 or a negative number.** The entry above takes `ln(u)` of +`abs(u)` in an antiderivative. That drops `i pi` where `u < 0`, which is a constant only where `ln(u)` stands alone with +a constant coefficient. It was done wherever `ln` appeared, so `x ln(x) - x`, the antiderivative of `ln(x)`, lost +`i pi x`, and `ln(x)` over `[-2, -1]` came out real. Now it is done only when every such logarithm stands alone, so +`1/x` over `[-2, -1]` is still `-ln(2)`. An antiderivative holding `abs(u)` or `sgn(u)` of an argument off the real line +in the range is not an antiderivative, and such integrals are now left unevaluated. `Ei(u)` tends to `i pi sgn(Im u)`, +not 0, as `Re u` tends to `-oo` off the real line. A bound where a logarithm's argument is 0 is taken as a limit from +inside the range. So the integrals of `li(x)` over negative ranges have values. Measured on master `409d68805` and on +this branch; every value checked against mpmath's quadrature +([#2715](https://github.com/asc-community/AngouriMath/issues/2715), +[#2716](https://github.com/asc-community/AngouriMath/issues/2716)). + +| simplified | Was (`409d68805`) | Now | +|---|---|---| +| `integral(ln(x), x, -2, -1)` | `2 * ln(2) - 1` | `2 * ln(-2) - 1 + (-i) * pi`, `0.3863 + 3.1416i` | +| `integral(ln(x), x, -1, 1)` | `-2` | `i * pi - 2` | +| `integral(ln(x)^2, x, -2, -1)` | `2 + 2 * ln(2) ^ 2 + (-4) * ln(2)` | `-9.6813 + 2.4272i` | +| `integral(x ln(x), x, -1, 0)` | `1/4` | `1/4 + (-1/2 * i) * pi` | +| `integral(log(10, x), x, -2, -1)` | `(2 * ln(2) - 1) / ln(10)` | `0.1678 + 1.3644i` | +| `integral(artanh(x), x, 2, 3)` | `0.4315` | `0.4315 + 1.5708i` | +| `integral(abs(ln(x)), x, -1, 1)` | `0` | unevaluated (it is `4.4092`) | +| `integral(abs(ln(x - 1/2)), x, -pi, pi)` | `-1.1405` | unevaluated (it is `13.9351`) | +| `integral(sgn(ln(x))/sqrt(x), x, -pi, pi)` | `2 * (sqrt(pi) - sqrt(-pi))` | unevaluated | +| `integral(li(-x), x, 0, 1)` | `li(-1) - +oo` | `li(-1) + Ei(2i * pi) + (-i) * pi`, `0.0511 + 3.2701i` | +| `integral(li(x^3), x, -1, 0)` | `li(-1) - +oo` | `0.0277 + 3.2026i` | +| `limit(Ei(2 ln(-x)), x, 0, right)` | `0` | `i * pi` | + ### ln(x) over a rational function symmetric under x -> a^2/x is integrated over [0, +oo) **An answer where there was none.** `ln(x)/(1 + x^2)` has no elementary antiderivative, and its integral over diff --git a/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs b/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs index c17fa3a90..8d0536a7a 100644 --- a/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs +++ b/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs @@ -136,7 +136,12 @@ public Entity Integrate(Variable x, Entity from, Entity to) => (lower, upper) = (null, null); var asFound = antiderivative; if (lower is not null && upper is not null) - antiderivative = WithRealLogarithms(antiderivative, x, lower < upper ? lower : upper, lower < upper ? upper : lower); + { + var (bottom, top) = lower < upper ? (lower, upper) : (upper, lower); + if (HoldsAModulusOffTheRealLine(antiderivative, x, bottom, top)) + return null; + antiderivative = WithRealLogarithms(antiderivative, x, bottom, top); + } // Where an argument off the real line crosses a branch cut, or may, the antiderivative jumps there although the // integrand does not. Between bounds that are not numbers any crossing is one that cannot be placed. // https://github.com/asc-community/AngouriMath/issues/2460 @@ -213,15 +218,65 @@ public Entity Integrate(Variable x, Entity from, Entity to) => 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 = antiderivative.Nodes.OfType().Distinct().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 })) + .ToHashSet(); + // 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); + } + + /// + /// Whether holds the modulus or the sign of an argument in + /// seen to be off the real line in the range. + /// + /// + /// The derivative of abs(u) is sgn(u) u' only where u is real, and that of sgn(u) is + /// 0 only there, so off the real line the antiderivative is not one. abs(ln(x)) over [-1, 1], whose + /// antiderivative is (abs(ln(x)) - sgn(ln(x))) x, was 0 where it is 4.4092..., and + /// abs(ln(x - 1/2)) over [-pi, pi] was negative. + /// https://github.com/asc-community/AngouriMath/issues/2715 + /// + private static bool HoldsAModulusOffTheRealLine(Entity antiderivative, Variable x, Number.Real lowest, Number.Real highest) + { + var arguments = antiderivative.Nodes + .Select(node => node switch { Absf(var argument) => argument, Signumf(var argument) => argument, _ => null }) + .Where(argument => argument is not null && argument.ContainsNode(x)).Distinct().ToList(); + if (arguments.Count == 0) + return false; + var samples = SamplePoints(lowest, highest); + return arguments.Any(argument => samples.Any(point => + argument!.Substitute(x, point).Evaled is Number.Complex value and not Number.Real + && value.ImaginaryPart.EDecimal is { IsFinite: true } imaginary && !imaginary.IsZero)); } /// Finite points spread over the range, its finite bounds among them. @@ -370,8 +425,6 @@ 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) - 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 { @@ -381,6 +434,21 @@ private static bool HoldsAnIncompleteGammaFunction(Entity expr) (Number.Real at, Number.Real other) when at < other => ApproachFrom.Right, _ => null }; + if (!value.InnerSimplified.IsNaN) + { + // A logarithm of 0 is -oo whichever side its argument comes from, and the side is what a function of it + // can depend on: Ei(2 ln(-x)), in the antiderivative of li(-x), tends to i pi at 0 from the right, and + // taken at 0 it was Ei(-oo), which is 0. So the limit from inside the range, where it is decided. + // https://github.com/asc-community/AngouriMath/issues/2716 + if (side is { } within && bound.Evaled is Number.Real { IsFinite: true } + && antiderivative.Nodes.Any(node => node is Logf(_, var argument) && argument.ContainsNode(x) + && argument.Substitute(x, bound).Evaled == Integer.Zero) + && !HoldsAnIncompleteGammaFunction(antiderivative) + && LimitAtTheBound(antiderivative, x, bound, within) is var fromInside + && !fromInside.Nodes.Any(node => node is Limitf) && !fromInside.IsNaN) + return fromInside; + return continuousInside && AtAnInfiniteBound(antiderivative, x, bound, value) is { } atInfinity ? atInfinity : value; + } if (side is not { } inside) return value; // An antiderivative in the incomplete gamma function undefined at a bound is left as written rather than diff --git a/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Limit.Classes.cs b/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Limit.Classes.cs index f058d2089..bf07c775e 100644 --- a/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Limit.Classes.cs +++ b/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Limit.Classes.cs @@ -770,6 +770,13 @@ partial record Arccosf if (limit.Evaled is Real { IsFinite: false, IsNaN: false } infinity) { var upwards = !infinity.IsNegative; + // An argument whose real part runs to an infinity off the real line is read as that infinity. Ei(u) + // is -E1(-u) + i pi sgn(Im u) there, and E1(-u) tends to 0 as Re u tends to -oo, so Ei tends to i pi + // or -i pi, not to 0: Ei(2 ln(-x)) at 0 from the right, in the antiderivative of li(-x), is i pi, and + // the integral of li(-x) over [0, 1] was short of it. The values of the others, off the line, are not + // claimed. https://github.com/asc-community/AngouriMath/issues/2715 + if (argument.ContainsNode(x) && ImaginarySignBeside(argument, x, dist, side) is int offTheLine and (1 or -1)) + return node is Eif && !upwards ? Integer.Create(offTheLine) * MathS.i * MathS.pi : null; return node switch { Erff => upwards ? Integer.One : Integer.MinusOne, diff --git a/Sources/AngouriMath/Functions/Continuous/Limits/Transformations.cs b/Sources/AngouriMath/Functions/Continuous/Limits/Transformations.cs index 3582ce5cc..0b8fde79b 100644 --- a/Sources/AngouriMath/Functions/Continuous/Limits/Transformations.cs +++ b/Sources/AngouriMath/Functions/Continuous/Limits/Transformations.cs @@ -1278,6 +1278,27 @@ private static Entity WithGenericSymbols(Entity expr, Variable x) return expr; } + /// + /// The sign of the imaginary part of at the points beside the destination on each side + /// approached: 0 where it is real at all of them, 1 or -1 where it is off the real line on one side of it at all of + /// them, and where they disagree or a value is not a finite number. A witness, not a proof. + /// + internal static int? ImaginarySignBeside(Entity expr, Variable x, Entity dest, ApproachFrom side) + { + int? sign = null; + foreach (var (nearer, further) in PointsBeside(dest, side)) + foreach (var point in new[] { nearer, further }) + { + if (expr.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; + } + /// /// Whether the values of at two points beside the destination point in directions that /// differ, so that it is not a constant times a real function there. diff --git a/Sources/Tests/UnitTests/Calculus/ALogarithmOfANegativeNumberKeepsItsImaginaryPartTest.cs b/Sources/Tests/UnitTests/Calculus/ALogarithmOfANegativeNumberKeepsItsImaginaryPartTest.cs new file mode 100644 index 000000000..089ee15e1 --- /dev/null +++ b/Sources/Tests/UnitTests/Calculus/ALogarithmOfANegativeNumberKeepsItsImaginaryPartTest.cs @@ -0,0 +1,84 @@ +// +// 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 System.Linq; +using AngouriMath.Core; +using AngouriMath.Extensions; +using Xunit; +using static AngouriMath.Entity; + +namespace AngouriMath.Tests.Calculus +{ + /// + /// ln(u) is taken of abs(u) in an antiderivative only where it stands alone, with a constant + /// coefficient, so that the i pi dropped is a constant dropped. ln(x) over [-2, -1], whose + /// antiderivative is x ln(x) - x, was 2 ln(2) - 1, without its i pi. And an antiderivative + /// through the modulus or the sign of an argument off the real line is not one: abs(ln(x)) over + /// [-1, 1] was 0. Expected values from mpmath quad at 50 digits. + /// https://github.com/asc-community/AngouriMath/issues/2715 + /// + [Trait("Area", "Calculus")] + public sealed class ALogarithmOfANegativeNumberKeepsItsImaginaryPartTest + { + [Theory] + [InlineData("ln(x)", "-2", "-1", "0.38629436111989061883", "3.14159265358979323846")] + [InlineData("ln(x)", "-1", "1", "-2", "3.14159265358979323846")] + [InlineData("ln(x)^2", "-2", "-1", "-9.68128709549273700717", "2.42715905403482204508")] + [InlineData("x ln(x)", "-1", "0", "0.25", "-1.57079632679489661923")] + [InlineData("ln(x - 3)", "0", "1", "0.90954250488443845535", "3.14159265358979323846")] + [InlineData("log(10, x)", "-2", "-1", "0.16776550942471056278", "1.36437635384184134749")] + [InlineData("artanh(x)", "2", "3", "0.43152310867767139116", "1.57079632679489661923")] + // li(u) is Ei(ln(u)), and Ei(u) tends to i pi, not 0, as Re u tends to -oo above the real line. + // https://github.com/asc-community/AngouriMath/issues/2716 + [InlineData("li(x)", "-2", "-1", "0.060089396701437629672", "3.5802459733906890014")] + [InlineData("li(-x)", "0", "1", "0.051107250300079418347", "3.2700886281150946206")] + [InlineData("li(x^3)", "-1", "0", "0.027700673331155747902", "3.2025626003599038676")] + public void TheImaginaryPartIsKept(string integrand, string from, string to, string real, string imaginary) + { + var integral = integrand.ToEntity().Integrate("x", from.ToEntity(), to.ToEntity()).Simplify(); + Assert.DoesNotContain(integral.Nodes, node => node is Integralf); + var value = integral.EvalNumerical(); + Assert.True((value - (real.ToEntity() + MathS.i * imaginary.ToEntity()).EvalNumerical()).Abs() < 1e-12, + $"{integrand} from {from} to {to} was {integral}"); + } + + /// Where ln stands alone the integral is real and written without i, as #2611 has it. + [Theory] + [InlineData("1/x", "-2", "-1", "-ln(2)")] + [InlineData("1/x + 2/(x - 3)", "-2", "-1", "-ln(2) + 2 ln(4/5)")] + public void ALogarithmStandingAloneIsStillReal(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 || node == MathS.i); + Assert.True((integral - expected.ToEntity()).EvalNumerical().Abs() < 1e-12, $"{integrand} was {integral}"); + } + + /// Ei(u) is -E1(-u) + i pi sgn(Im u), and E1(-u) tends to 0 as Re u tends to -oo. + [Theory] + [InlineData("Ei(2 ln(-x))", "0", ApproachFrom.Right, "i pi")] + [InlineData("Ei(x - i)", "-oo", ApproachFrom.Right, "-i pi")] + [InlineData("Ei(x)", "-oo", ApproachFrom.Right, "0")] + public void EiOffTheRealLineTendsToIPi(string expr, string to, ApproachFrom side, string expected) + => Assert.Equal(expected.ToEntity().Evaled, expr.ToEntity().Limit("x", to.ToEntity(), side).Evaled); + + /// Not answered, or answered right: never 0, and never negative for a modulus. + [Theory] + [InlineData("abs(ln(x))", "-1", "1", "4.40923181907942097759", "0")] + [InlineData("abs(ln(x - 1/2))", "-pi", "pi", "13.9351086733895198694", "0")] + [InlineData("abs(ln(x)) / sqrt(x)", "-pi", "pi", "4.96814638521873812564", "-12.9643399414702057786")] + [InlineData("sgn(ln(x)) / sqrt(x)", "-pi", "pi", "2.73855019241837458621", "0.55570052002308297149")] + public void AModulusOffTheRealLineIsNotReadAsReal(string integrand, string from, string to, string real, string imaginary) + { + var integral = integrand.ToEntity().Integrate("x", from.ToEntity(), to.ToEntity()).Simplify(); + if (integral.Nodes.Any(node => node is Integralf)) + return; + var value = integral.EvalNumerical(); + Assert.True((value - (real.ToEntity() + MathS.i * imaginary.ToEntity()).EvalNumerical()).Abs() < 1e-10, + $"{integrand} from {from} to {to} was {integral}"); + } + } +}