Skip to content

Diverge across a singularity of Ei, Ci, Chi or li, and integrate a step times a pole piecewise - #2746

Open
Rafael-SOWNet wants to merge 4 commits into
masterfrom
fix-2716-special-function-pole-and-li-negative
Open

Rafael-SOWNet wants to merge 4 commits into
masterfrom
fix-2716-special-function-pole-and-li-negative

Conversation

@Rafael-SOWNet

@Rafael-SOWNet Rafael-SOWNet commented Dec 6, 2026 •

Copy link
Copy Markdown
Member

A definite integral whose antiderivative is Ei, Ci, Chi or li of something passing through its logarithmic singularity inside the range was evaluated by substituting the bounds, so a divergent integral got a finite value. The singularity is now an antiderivative break, and the one-sided limits there decide: NaN where both sides diverge, ±oo where the range ends at it. Si and Shi are entire and unchanged.

The limit solver learns what these functions do off the positive reals (Ei(u) -> i pi sgn(Im u) as Re u -> -oo, li(u) -> i pi sgn(arg u) at 0, Ci/Chi -> -oo below 0), and the logarithmic-singularity rule adds eulergamma after the limit instead of inside it.

A step times a pole at the step's zero (floor(x)/x over [0, 1]) gave 0/0 = NaN; when the unit-interval sum is NaN, each unit interval is now integrated with the step's constant, up to 64 intervals.

Was (409d688) Now mpmath
exp(x)/x, [-1, 2] finite NaN diverges
exp(x)/x, [0, 2] / [-2, 0] finite +oo / -oo
1/ln(x), [1/2, 2] 1.42383 NaN diverges
cos(x)/x, [-1, 1] finite NaN diverges
cos(x^2)/x, [-pi, pi] 0 NaN diverges (not absolutely; no PV)
(exp(x) - 1)/x, [-1, 2] unevaluated 4.48047110983747 4.48047110983747
abs(x - 1/2) Chi(-x), [-1, 1] -0.19562 + 0.39270i -0.19562 + 0.78540i -0.195621447040727 + 0.785398163397448i
x li(sqrt(x)), [-2, 2] -3.33120 - 4.33930i -3.33120 - 5.91010i -3.33120060698478 - 5.91009570163854i
ln(ln(x)), [0, 2] unevaluated -1.77819 + 3.14159i -1.77818962128082 + 3.14159265358979i
ln(x) li(x), [0, 1] unevaluated 0.193147180559945 0.193147180559945
floor(x)/x, [0, 1] NaN 0 0
floor(x)/x, [0, 3] NaN 1.50407739677627 1.50407739677627
floor(x) Si(1/x), [-pi, pi] NaN 5.79731491914639 5.79731491914639
Ci(x), x -> 0- unevaluated -oo
li(x), x -> 0- unevaluated i pi

The rows of li over negative arguments in the issue (li(x) over [-2, -1], li(-x) over [0, 1], li(x^3) over [-1, 0]) come from ln(u) being replaced by ln|u|; that is the fix for #2715 and is not repeated here. With that fix's working patch applied on top of this branch they are right. #2731 also edits Integration.Definition.cs and BREAKING-CHANGES.md; the hunks are apart.

Checked on master 9b75a26: full unit suite (22405 passed, 0 failed), CasBench (120/121, 0 wrong, as master), PropCheck (1355 checks, 0 failures). A sweep of 757 related integrals: 19 answers changed, every one checked against mpmath (improvements or reorderings); the slowest new answer is abs(x - 1/2) Ci(sqrt(x)) over [-1, 1], 1.3 s -> 2.2 s.

A logarithm is taken of its modulus only where it stands alone (second commit)

With the breaks above, ln(x) li(2x) over [-pi, pi] came out as pi (li(-2 pi) + li(2 pi)) (ln(pi) - 1) = 1.784 + 2.217i, where mpmath gives -34.8874626534 + 2.1706698626i (master timed out). The cause was not the breaks but WithRealLogarithms (#2616), which replaced every ln(u) whose argument is negative in the range by ln(abs(u)). That drops i pi times the coefficient, which is a constant between the breaks only where the coefficient is; this antiderivative holds ln(x) against li(2x) and Ei(2 ln(2x)). Tested by the fix it predicts: with the moduli taken only when each logarithm is c ln(u) + G (c constant, G free of it), the integral is left unevaluated as on master, and its pieces over [-pi, -1] and [-1, -1/2] are right (the second was -oo on both before).

That is #2751's WithRealLogarithms/StandsAlone hunk, copied byte for byte, so the two agree there. It also fixes, ahead of #2751, integrals of the logarithm of a negative number that lost their imaginary part: ln(x) over [-2, -1] was 2 ln(2) - 1, now 0.38629 + 3.14159i; x ln(x), ln(x)^2, ln(x)/x, x^2 ln(x) likewise (entries in BREAKING-CHANGES.md). 1/x over [-2, -1] is still -ln(2).

Sweeps against mpmath quad split at every singularity, master 2ddbc2e4d vs this head:

rows right wrong declined timeout
{ln(x), x, x^2, 1/x} times {li, Ei, Ci, Si, Chi, Shi}(a x + b), intervals across 0, the pole, the branch point: master 864 400 58 309 97
same, this branch 864 537 0 276 51
x^k ln(u)^m, artanh, ln(x)/x, 1/(x - a): master 216 93 80 14 29
same, this branch 216 192 0 2 22

No row got worse. Ten rows went from declined to timeout under load; I re-ran them alone and they decline on both sides in the same time. Three li(a x)/x rows ending at 0 from the negative side are now ∓i oo. That is right, since li(x) -> i pi there. On rows answered the same way on both sides, the median time is 69 -> 48 ms and 21 -> 16 ms.

Merged master 8b4f507c1. Full unit suite: 22584 passed and 2 failed, run alongside the sweeps at load ~20. The two failures were wall-clock tests (IntegralAnswerCacheTest.AnIntegralWithNoAnswerStillFinishesQuickly, ALimitAtABreakIsRememberedTest for 1/(a + sin(x))), and both pass when run alone. 1/(a + sin(x)) over [0, 2 pi] takes 7.8 s on master and 8.0 s here. CasBench 120/121 with 0 wrong, PropCheck 1355 checks with 0 failures, SimpSweep 10463/10463 agree, BoundCheck 0 disagreements.

For #2751: after this merges, the StandsAlone hunk is identical on both sides. Its only conflict in Integration.Definition.cs is where HoldsAModulusOffTheRealLine follows StandsAlone; take #2751's side there. The AtTheBound conflict and the Limit.Classes.cs conflict are as before, and this commit does not touch Limit.Classes.cs.

Part of #2716. Fixes #2737: integral(abs(x - 1/2) Chi(-x), x, -1, 1) is now -0.19562144704 + 0.78539816340i, matching mpmath (master had half the imaginary part).: parts 1 and 3. Part 2 (li over negative arguments) lands with the fix for #2715.

🤖 Generated with Claude Code

…ep 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 <noreply@anthropic.com>
@codecov

codecov Bot commented Dec 6, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 79.46429% with 23 lines in your changes missing coverage. Please review.
✅ Project coverage is 86.69%. Comparing base (8b4f507) to head (c8993db).

Files with missing lines Patch % Lines
...nctions/Continuous/Limits/Solvers/Limit.Classes.cs 72.88% 7 Missing and 9 partials ⚠️
...s/Continuous/Integration/Integration.Definition.cs 86.36% 0 Missing and 3 partials ⚠️
...ns/Continuous/Integration/BreakpointIntegration.cs 85.71% 1 Missing and 1 partial ⚠️
...ons/Continuous/Integration/AntiderivativeBreaks.cs 90.00% 0 Missing and 1 partial ⚠️
...ns/Continuous/Limits/Solvers/Solvers.Definition.cs 85.71% 0 Missing and 1 partial ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2746      +/-   ##
==========================================
- Coverage   86.70%   86.69%   -0.02%     
==========================================
  Files         347      347              
  Lines       77376    77457      +81     
  Branches    22783    22814      +31     
==========================================
+ Hits        67089    67149      +60     
- Misses       5546     5558      +12     
- Partials     4741     4750       +9     
Flag Coverage Δ
cpp 37.77% <ø> (ø)
csharp 87.17% <79.46%> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
...ons/Continuous/Integration/AntiderivativeBreaks.cs 64.65% <90.00%> (+1.06%) ⬆️
...ns/Continuous/Limits/Solvers/Solvers.Definition.cs 90.37% <85.71%> (+0.27%) ⬆️
...ns/Continuous/Integration/BreakpointIntegration.cs 69.29% <85.71%> (+1.31%) ⬆️
...s/Continuous/Integration/Integration.Definition.cs 87.07% <86.36%> (+0.02%) ⬆️
...nctions/Continuous/Limits/Solvers/Limit.Classes.cs 79.87% <72.88%> (-0.64%) ⬇️

... and 7 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Rafael-SOWNet and others added 3 commits December 6, 2026 16:56
…iderivative

A definite integral took ln(abs(u)) for every ln(u) in the antiderivative whose
argument was negative somewhere in the range (#2616). That drops i pi times the
logarithm's coefficient, which is a constant between the breaks only where the
coefficient is. With the breaks this branch adds at the singularities of li,
ln(x) li(2x) over [-pi, pi] reached that step: its antiderivative holds ln(x)
against li(2x) and Ei(2 ln(2x)), and with the moduli taken it came out as
pi (li(-2 pi) + li(2 pi)) (ln(pi) - 1), 1.784 + 2.217i, where mpmath gives
-34.8874626534 + 2.1706698626i. Master timed out on it.

The moduli are now taken only when every such logarithm is c ln(u) + G with c
constant and G free of it, the same rule as #2751 (the hunk is identical, so
that the two merge cleanly there). ln(x) li(2x) over [-pi, pi] is left
unevaluated, as on master; over [-pi, -1] and [-1, -1/2] it is right, and an
integral of the logarithm of a negative number keeps its imaginary part
(ln(x) over [-2, -1] was 2 ln(2) - 1, and is 0.3863 + 3.1416i).

Sweeps against mpmath quad split at every singularity, master 2ddbc2e vs
this branch: {ln(x), x, x^2, 1/x} times {li, Ei, Ci, Si, Chi, Shi}(a x + b),
864 rows: right 400 -> 537, wrong 58 -> 0, declined 309 -> 276, timeout
97 -> 51. Logarithm class (x^k ln(u)^m, artanh, ln(x)/x), 216 rows: right
93 -> 192, wrong 80 -> 0. No row got worse (six declined -> timeout under
load were re-run alone and decline on both in the same time).

Part of #2716

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
netstandard2.0 has no Enumerable.ToHashSet, so the library did not build
for that target. The HashSet constructor removes duplicates the same way.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

The definite integral of abs(x - 1/2) Chi(-x) over [-1, 1] has half its imaginary part

1 participant