Amplifying @John's answer, first note:
2 - I Infinity //InputForm
DirectedInfinity[-I]
The real part has been discarded. So both the Integrate
and NIntegrate
versions are using the limits:
{s, -I Infinity, I Infinity}
instead, with the real part suppressed. Now, the two outputs are different, so which is correct? Typically, when there is a discrepancy between Integrate
and NIntegrate
(where NIntegrate
produces a results without error messages), then it is more likely that NIntegrate
is correct, since NIntegrate
doesn't need to check branch cut/pole issues associated with the antiderivative that is computed by Integrate
. That is the case here. Compare:
Integrate[Exp[s]/(s^2-1),{s, -I*Infinity, I*Infinity}]/(2 Pi I) //N
NIntegrate[Exp[s]/(s^2-1),{s, -I*Infinity, I*Infinity}]/(2 Pi I)
1.1752
-0.18394 - 2.70877*10^-14 I
The Integrate
result takes the difference of the antiderivative at the two limits of integration, but it may not be clear which path this result corresponds to. On the other hand, the NIntegrate
result always uses the linear path between the limits, i.e., the path that goes through the origin. Redoing the integrals by specifying that the path goes through the origin:
Integrate[Exp[s]/(s^2-1),{s, -I*Infinity, 0, I*Infinity}]/(2 Pi I) //N
NIntegrate[Exp[s]/(s^2-1),{s, -I*Infinity, 0, I*Infinity}]/(2 Pi I)
-0.18394 + 0. I
-0.18394 - 2.70877*10^-14 I
Now, the two results agree. If we use a path that goes through the point 2:
Integrate[Exp[s]/(s^2-1),{s, -I*Infinity, 2, I*Infinity}]/(2 Pi I) //N
NIntegrate[Exp[s]/(s^2-1),{s, -I*Infinity, 2, I*Infinity}]/(2 Pi I)
1.1752 + 0. I
1.1752 + 0. I
These results agree with the original Integrate
result, so we see that just subtracting the antiderivative at the limits of integration corresponds to the path that goes around the singularities at $\pm 1$ on the right.
Addendum
@xzczd asked whether it is possible to confirm that NIntegrate
does drop the real part. One piece of evidence:
Short[
Last @ Reap @ NIntegrate[
Exp[s]/(s^2-1),
{s, 2-I Infinity, 2+I Infinity},
EvaluationMonitor:>Sow[s]
],
10
]
{{0. -61.8145 I,0. -11.1974 I,0. -4.17292 I,0. -1.95969 I,0. -1. I,0. -0.510285 I,0. -0.23964 I,0. -0.0893062 I,0. -0.0161774 I,0. -374.81 I,0. -22.5193 I,0. -6.53258 I,0. -2.81288 I,0. -1.39298 I,0. -0.717883 I,<<236>>,0. -0.444442 I,0. -0.390297 I,0. -0.354513 I,0. -0.336879 I,0. -0.329806 I,0. -0.312805 I,0. -0.280854 I,0. -0.238097 I,0. -0.190411 I,0. -0.142857 I,0. -0.0989569 I,0. -0.0612234 I,0. -0.0317034 I,0. -0.0118667 I,0. -0.0019933 I}}
All of the evaluation points have a real part of 0.
(2*Pi*I)^(-1)* NIntegrate[Exp[s]/(s^2 - 1), {s, 2 - I*Infinity, 2, 2 + I*Infinity}]
returns the correct result (notice the path,{s, 2 - I*Infinity, 2, 2 + I*Infinity}
). Hmmm. $\endgroup$