# Integration of finite, real integrand gives imaginary error function, causes problems

So far, I have found two questions on Stackexchange with a similar problem, the most recent is (111297). However, the question is over a year old, and reported as a bug. Nonetheless, the most recent version of Mathematica 11.1.1.0 still gives these sorts of results.

My problem is the integral

Integrate[Cos[x*Pi*j]*E^(-1 + x)^2, {x, x0, x1}]


The domain is always between 0 and 1, though I need some variation for later in the code. This integrand could not be better behaved. I can put all sorts of conditions without success: limits are real, between 0 and 1, upper limit is larger than the lower limit, the $j$ is integer and positive, etc.

No matter what, the answer involves four imaginary error functions. When I use them, I sometimes get tiny imaginary bits, probably from numerical error. Nonetheless, it causes problems later in the code.

Since this problem has been around for a long time, according to the previous questions, does that mean that it will never get fixed? I know that the answer might be technically correct, and that there might be workarounds, but that doesn't mean it isn't a bug.

• Do you know for a fact that the integral can be represented using only real numbers / functions? Also, would it be viable to pass the numerical results through Chop when you need them later in the code? – MarcoB Aug 3 '17 at 22:00
• On the other side of the ledger, the fact that the result is in some way not liked does not mean it is a bug (and it isn't). – Daniel Lichtblau Aug 3 '17 at 22:01
• As already noted, it is sometimes unavoidable that complex numbers are needed to represent real-valued functions. Just use Re[] at the end if you're sure the result ought to be real-valued anyway. – J. M.'s ennui Aug 4 '17 at 0:45

We can justify the use of Re by showing that expression returned by Mathematica is real. First we evaluate the integral, using $k$ as our positive integer parameter:

Integrate[Cos[x*π*k]*Exp[(-1 + x)^2], {x, x0, x1}]


which gives something like $$\frac{\sqrt{\pi }}4 e^{\frac{\pi k}4 (\pi k-4 i)} \left(e^{2 i \pi k} \left(\text{erfi}\left(-\frac{i \pi k}{2}-\rm{x0}+1\right)-\operatorname{erfi}\left(-\frac{i \pi k}{2} - \rm{x1}+1\right)\right)+\\\operatorname{erfi}\left(\frac{i \pi k}{2}-\rm{x0}+1\right)-\operatorname{erfi}\left(\frac{i \pi k}{2}-\rm{x1}+1\right)\right)$$ We note the $e^{2i\pi k}$ is unity, since $k$ is an integer. The other exponential simplifies to $(-1)^k e^{k^2\pi^2/4}$, which is real. So, the expression contains 4 terms with Erfi each with a real coefficient. More, it contains 2 pairs of terms with Erfi[z] + Erfi[z*], where z* is the complex conjugate of z. We can show that Erfi[z]=Erf[z*]* as follows.

We know that Erfi is defined in terms of Erf, that Erf is odd and that Erf[z*]=Erf[z]*. So,

$\operatorname{erfi}(z) = -i \operatorname{Erf}(iz) \hspace{2em}$ by definition of Erfi

$\hspace{3em}= i \operatorname{Erf}(-iz) \hspace{2em}$ since Erf is odd

$\hspace{3em}= i \operatorname{Erf}(iz^*)^* \hspace{2em}$ since Erf[z*]=Erf[z]*

$\hspace{3em}= \left[-i \operatorname{Erf}(iz^*) \right]^* \hspace{2em}$

$\hspace{3em}= \operatorname{erfi}(z^*)^* \hspace{2em}$ by definition of Erfi

Thus, the expression return by Mathematica is a sum of two complex conjugates plus a sum of two other complex conjugates. Thus Mathematica returns an expression that is provably real when $k$ is an integer, and we are justified in using Re after evaluating the expression. Also, since we showed Erfi[z*]=Erfi[z]*, we could evaluate the Erfi function and use two times real part of that result instead of evaluating Erfi twice and adding two values that we know are complex conjugates. That is, Re[Erfi[z]+Erfi[z*]]=2*Re[Erfi[z]].

Properties of Erf are from the Wikipedia page