Bromwich integral transformed to an integral on the real axis I am new in complex integration and inverse Laplace transforms. I already asked this question on math.se but got no answer.
The author of a textbook claims that the inverse Laplace transform has expression
$$
f(t) = \frac{2\exp(bt)}{\pi}\int_0^\infty\Re\bigl(\hat{f}(b+iu)\bigr)\cos(ut)\,\mathrm{d}u.
$$
He obtains this formula by substituting $s = b+iu$ in the Bromwich integral
$$
f(t) = \frac{1}{2\pi i}\int_{b-i\infty}^{b+i\infty}\exp(st)\hat{f}(s)\,\mathrm{d}s.
$$
However I've numerically checked this formula and it doesn't seem to hold true:
fhat <- function(s) 1/(s+3) # Laplace transform of exp(-3x)
b <- 5 
integrand <- function(u, x){
  Re(fhat(b+1i*u))*cos(x*u)
}
x <- 2 
2*exp(b*x)/pi * integrate(integrand, 0, Inf, x = x, subdivisions = 10000)$value
# -0.1124648
exp(-3*x)
# 0.002478752

For $b = -2$ the result is close to the expected value $\exp(-3x)$:
b <- -2
2*exp(b*x)/pi * integrate(integrand, 0, Inf, x = x, subdivisions = 10000)$value
# 0.002479138

I understood that $b$ must be choosen anywhere at the right of the singularities of $\hat{f}$ (here $-3$). Am I wrong? Here the result depends on the choice of $b$. Do I misunderstand something, or is there something wrong in this textbook?
Here is the derivation of the formula:

 A: Too long for a comment:
The following paper give a review of existing inverse Lapplace transform algorithms:
Kristopher L. Kuhlman, "Review of inverse Laplace transform algorithms
for Laplace-space numerical approaches", Numerical Algorithms 63, No. 2, pp. 339-355 (2013),  DOI 10.1007/s11075-012-9625-3, MR3057203, Zbl 1269.65134.
and a little bit more specific
Lloyd N. Trefethen, J. André C. Weideman, and Thomas Schmelzer, "Talbot quadratures and rational approximations", BIT Numerical Mathematics, 46(3), pp. 653-670 (2006), DOI 10.1007/s10543-006-0077-9, MR2265580, Zbl 1103.65030.
Of course they do not mention your specific implementation of the Bromwich inversion integral.
Fernando Damian Nieuwveldt has implemented in this code a fine version of the inverse Laplace algorithm (in Python, not in R). Here an excerpt:
Talbot suggested that the Bromwich line be deformed into a contour that begins and ends in the left half plane, i.e., $z \to −\infty$ at both ends.
Due to the exponential factor the integrand decays rapidly on such a contour. In such situations the trapezoidal rule converge extraordinarily rapidly.
For example (in Nieuwveldt's code) here we compute the inverse transform of $F(s) = 1/(s+1)$ at $t = 1$.
If you are interested in more precision you find an algorithm with arbitrary precision in here.
