# How is the result of this integral obtained by the function Integrate?

When I use Mathematica to calculate $$\int_0^{+\infty } \frac{\log (1-\mathrm i x)}{1+x^2} \, \mathrm dx ,$$

I get different results using functions NIntegrate and Integrate, as follows:

Mathematically, the result for NIntegrate is correct. First, we perform a complex expansion of the logarithmic function, and the final result is exactly $$\frac{1}{2} \pi \log (2)-\mathrm i\frac{ \pi ^2}{8}$$.

So How is the result of this integral obtained by the function Integrate?

I tried using the Trace function, but it didn't work. I don't know how to analyze its internal logic to get this result.

PS1: My \$Version is "14.0.0 for Microsoft Windows (64-bit) (December 13, 2023)".

PS2: The Integrate for $$\int_0^{+\infty } \frac{\log (1+\mathrm i x)}{1+x^2} \, \mathrm dx$$, which differs by an argument, is correct.

PS3: The code is below:

NIntegrate[Log[1 - I  x]/ (1 + x^2), {x, 0, Infinity}]
Integrate[Log[1 - I  x]/ (1 + x^2), {x, 0, Infinity}]
N@%

• This is not only a bug, but also a regression (again and again). In my Mathematica 8.0.4 (at hand now) I get two equal results, 1.08879 - 1.2337 I . Commented Jul 1 at 10:03

## EDIT: Some people suggested using the package Rubi which I believe is not really a good suggestion; please see the end of this post to see more.

You are integrating a not-everywhere-holomorphic function on complex plane: you can not just specify the initial and end points of the integration path and expect an unambiguous result. Mathematically speaking, you need to specify a path (or a 1-chain) against which you are integrating your function (or rather 1-form).

In your particular case, there is a branch cut of your integrand along the imaginary axis between $$-i$$ and $$\infty$$, and how you connect 0 to $$\infty$$ around this branch cut does matter. In fact, your initial point is on the branch cut, which creates an ambiguity regarding how your path lies on the complex plane with respect to the branch cut!

An immediate solution to the problem is to deform the integration contour so that the path does not intersect the branch cut, and then take a limit: indeed,

N[Integrate[Log[1- I x]/(1+x^2),{x,a,Infinity}]/.a->0]


gives precisely the same result with the numerical computation (which is roughly immune to such symbolic subtleties)

Although this is a sufficient solution, we can also get more insight by still keeping the initial point of the path on the branch cut but by removing the ambiguity by deforming the contour to an explicit side of the cut. We can do this by decomposing the path into two paths where the integration becomes unambiguous. For instance,

N[Integrate[Log[1 - I x]/(1 + x^2), {x, 0, 1}] + Integrate[Log[1 - I x]/(1 + x^2), {x, 1, \[Infinity]}]]


will give precisely the result you want. Of course, there is nothing special about $$1$$ here, you could take any other positive real number so as to force Mathematica to connect $$0$$ and $$\infty$$ from right side of the branch cut; for instance

N[Integrate[Log[1 - I x]/(1 + x^2), {x, 0, Sqrt[11]}] + Integrate[Log[1 - I x]/(1 + x^2), {x, Sqrt[11], \[Infinity]}]]


will give you the same result.

On the other hand, if we choose a path from left side of the branch cut, we then get the aforementioned so-called incorrect result:

N[Integrate[Log[1- I x]/(1+x^2),{x,0,-1}]+Integrate[Log[1- I x]/(1+x^2),{x,-1,\[Infinity]}]]


I do not know why Mathematica chooses this particular path if the given information is ambiguous, and I'm not even sure if there is a canonical reason to choose one. Therefore, I would not necessarily call this a bug but rather a bad documentation regarding the default behavior.

EDIT: Some people suggested using the package Rubi. Although I believe that it is a great package, it is not really appropriate for complex integrations unless you know what you are doing.

Rubi internally computes indefinite integrals. If you are integrating a real function which is analytic everywhere in the integration range, then fundamental theorem of calculus relates this to signed definite integrals, so that you can use Rubi reliably for any signed definite integral Integral[f[x],{x,a,b}] as long as $$f(x)$$ is analytic in $$a.

If your function is not analytic, then Rubi may give incorrect results. For instance, $$\int\limits_{-1}^1\frac{dx}{x}$$ is not functionally defined for $$x\in\mathbb{R}$$ (it is actually a generalized function). You can expand the domain of $$x$$ to complex numbers, but then the integration is no longer unambiguously defined: you need to specify a path. Mathematica knows this, and hence refuses to do the integration:

In[3]:= Integrate[1/x,{x,-1,1}]
During evaluation of In[3]:= Integrate::idiv: Integral of 1/x does not converge on {-1,1}.


On the other hand, Rubi immediately gives a result, i.e. Int[1/x, {x, -1, 1}] evaluates to $$-i\pi$$. This is indeed the result if you choose the path that connects $$-1$$ and $$1$$ in the upper complex half plane (i.e. hence Integrate[1/x, {x, -1, I, 1}] evaluates to this result as well), but if you choose to connect them in the lower half plane, then the result is simply incorrect: Integrate[1/x, {x, -1, -I, 1}] on the other hand yields the correct results $$i\pi$$.

In summary, Rubi may give only a particular solution for a real integrand. For complex integrands, things may easily go to worse: fundamental theorem of calculus fails for higher dimensional integrals (such as integration on complex plane), hence Rubi's internal method of computation may simply give an irrelevant/incorrect result! Unless you know what you are doing very well (i.e. maybe taking relevant De Rham cohomology into account), you should be very cautious while using Rubi for complex integrations!

• (+1) I usually specify a path of integration thus: Integrate[Log[1 - I x]/(1 + x^2), {x, 0, 1, Infinity}] (compare with your 2nd code). Commented Jul 7 at 4:28

It is a bug in Integrate. The numerical result seems to be the correct one.

You can use Rubi integrator for workaround meanwhile.

e = Log[1 - I*x]/(1 + x^2)
NIntegrate[e, {x, 0, Infinity}]


<< Rubi
Int[e, {x, 0, Infinity}]


N[%]


Compare to

Integrate[e, {x, 0, Infinity}]


N[%]


V 14

• Rubi is not necessarily reliable for complex integrations; please see my post for further explanations. Commented Jul 8 at 10:25

Workaround:

Integrate[Integrate[D[Log[1 - a*x]/(1 + x^2), a], {x, 0, Infinity}][[1]], {a, 0, I}] // Expand
(* -((I π^2)/8) + 1/8 π Log[16] *)
% // N
(* 1.08879 - 1.2337 I *)


We see:

Integrate[D[Log[1 - a*x]/(1 + x^2), a], {x, 0, Infinity}][[2]]
(*Im[a] != 0 || Re[a] < 0*)

% /. a -> I
(*True*)

Integrate[Log[1 - a*x]/(1 + x^2), {x, 0, Infinity}][[2]]
(*Re[a] < 0 && Im[a] == 0*)(*MMA assumes for parameters*)

% /. a -> I
(* False *)
`

General solution does not take into account for complex numbers.That way is a bug.