An application of the exponential integral

Any integral of the form $$ \int f(z)e^z\;\mathrm{d}z, $$ where $f(z) = P(z)/Q(z)$ is a rational function, can reduced to the form $$ \int R(z)e^z\;\mathrm{d}z + \sum_i \int \frac{e^z}{(z-a_i)^{n_i}}\;\mathrm{d}z, $$ where $R(z)$ is a polynomial (which may be zero) by expansion in partial fractions. The first integral here can be evaluated by standard methods (repeated integration by parts). Provided the path of integration does not pass through any singular points of the integrand, the second term can be written in terms of exponential integrals.

For example, consider the integral $$ I = \int_{-\infty}^{-2} \frac{e^z}{z^2(z-1)}\;\mathrm{d}z. $$ It can easily be shown that $$ \frac{1}{z^2(z-1)} = \frac{1}{z-1} - \frac{1}{z} - \frac{1}{z^2} $$ and so the integral may be written as the three terms $$ I = \int_{-\infty}^{-2} \frac{e^z}{z-1} \;\mathrm{d}z - \int_{-\infty}^{-2} \frac{e^z}{z} \;\mathrm{d}z - \int_{-\infty}^{-2} \frac{e^z}{z^2} \;\mathrm{d}z $$ The second integral is simply $-\mathrm{Ei}(-2)$ and substitution $u=z-1$ resolves the first integral to $e\mathrm{Ei}(-3)$. The last integral may be written in terms of $\mathrm{En}(z)$ or further reduced by integration by parts to $$ \int_{-\infty}^{-2} \frac{e^z}{z^2} \;\mathrm{d}z = -\frac{e^{-2}}{2} + \mathrm{Ei}(-2). $$ Therefore, $$ I = e\mathrm{Ei}(-3) - 2\mathrm{Ei}(-2) - \frac{e^{-2}}{2} $$ In SciPy,

In [x]: import numpy as np
In [x]: from scipy.special import expi
In [x]: np.e * expi(-3) - 2*expi(-2) - np.exp(-2)/2
-0.0053357974213484663