Can the momentum operator have an imaginary expectation value? I'm making examples of wave functions to incorporate in a QM exam.
I came up with the following wave function, which gives me some troubles:
$$\psi(x,0) = \begin{cases}
A(a-x), & -a \leq x \leq a,\\
0& \text{otherwise}.
\end{cases}$$
This is basically a quick modification of problem 1.17 of Griffiths 2nd edition, which I thought would make things easier for the first problem of the exam.
After the usual normalization of $A$, which give $A=\sqrt{\frac{3}{8a^3}}$, I want to then have the students compute the expectation values of $x$, $p$, $x^2$ and $p^2$, and then verify the uncertainty principle.
This is where it gets tricky.
I get $\langle x\rangle = -\frac{a}{2}$ and $\langle x^2\rangle = \frac{2a^2}{5}$ easily.
For the momentum though, I get
$$\langle p\rangle = -\frac{3\color{red}{i}\hbar}{4a}\in \color{red}{i} \mathbb{R}$$
... an $\color{red}{\text{imaginary}}$ number!?
How is that possible? What does that mean? Am I doing something wrong in the calculation?
I am wondering if it is because of the form of the function in $x=-a$ and $x=a$.
 A: I henceforth assume $\hbar =1$.
There is no reason to introduce Dirac deltas here, everything is elementary. Moreover as the function $\psi$ is not differentiable, one cannot use the form of the momentum operator $P$ as derivative which is valid only on smooth functions. Forcing this way  would introduce unnecessary difficulties as the derivative must be here interpreted in weak form. 
Rigorously speaking if (as in the present case)  $\psi \in L^2(\mathbb R, dx)$ also belongs to the domain of the momentum operator $D(P)$, we have
 $$(P \psi)(x) = F(K\hat{\psi})(x)$$ where $F$ is the inverse Fourier-Plancherel transform, $\hat{\psi}(k)$ is the Fourier-Plancherel transform of $\psi$ and $K$ is the multiplicative operator $(K\hat{\psi})(k):= k\hat{\psi}(k)$. 
In particular $\psi \in D(P)$ if and only if $\hat{\psi} \in D(K)$ which means
$$\psi \in D(P) \quad \mbox{if and only if} \quad \int_{\mathbb R} k^2 |\hat{\psi}(k)|^2 dk < +\infty\:.$$
However, the existence of $\langle P \rangle_\psi$ is guaranteed under weaker hypotheses. It is enough to have $\psi \in D(\sqrt{|P|})$, which means
$$\int_{\mathbb R} |k| |\hat{\psi}(k)|^2 dk < +\infty\:.\tag{1}$$
In this case 
$$\langle P \rangle_\psi := \int_{\mathbb R} k |\hat{\psi}(k)|^2 dk\tag{2} $$
You see that $\langle P\rangle_\psi$ is real whenever exists since is the integral of a real function.
In the considered case,
$$\hat{\psi}(k) = \frac{A}{\sqrt{2\pi}} \int_{-a}^a (a-x) e^{-ikx}dx=
 \frac{A}{\sqrt{2\pi}}\left(a + i\frac{d}{dk} \right) \int_{-a}^a e^{-ikx}dx
=  \frac{2A}{\sqrt{2\pi}}\left(a + i\frac{d}{dk} \right) \frac{\sin(ka)}{k}\:.
$$
This function is $L^2(\mathbb R, dk)$ as due. Condition (1) reads
$$\frac{2A}{\pi}\int_{\mathbb R}\left|\left(a + i\frac{d}{dk} \right) \frac{\sin(ka)}{k}\right|^2 |k| dk <+\infty$$
that is
$$\frac{2A}{\pi}\int_{\mathbb R} \left[a^2 \frac{\sin^2(ka)}{|k|} + |k|\left(\frac{d}{dk} \frac{\sin(ka)}{k}\right)^2\right]  dk <+\infty\:.$$
The integrand, performing trivial computations, turns out to be
$$a^2\frac{\sin^2(ka) + \cos^2(ka) }{|k|} + \mbox{absolutely integrable terms}$$
namely
$$a^2\frac{1}{|k|} + \mbox{absolutely integrable terms}$$
so the condition (1) is violated because the integral diverges evidently, and $\langle P\rangle_\psi$ does not exist.  $\langle P^2\rangle_\psi$ does not exist similarly.
A: Well, you can conclude that something is wrong by the following logic: momentum is an observable, which means its allowed values must be things that you could read off a measuring device (assuming you had one that measures momentum). These are necessarily real values, and since the expectation value is some linear combination of possible measurements, it also has to be real. There's no way you can get a complex number from a linear combination of reals.
But I think the core of your question is whether you should be looking for a math mistake, or if something about the form of the wavefunction you've chosen causes this result to be invalid. There is, in fact, something wrong with the wavefunction: it's not continuous.

When you calculated the momentum, I assume you used the integral
$$\langle p\rangle = -i\hbar\int_{\infty}^{\infty}\psi^*(x)\frac{\partial}{\partial x}\psi(x)\ \mathrm{d}x$$
and you probably broke it up into pieces corresponding to the regions in which you've defined the function:
$$\begin{align}
\langle p\rangle
&= -i\hbar\int_{\infty}^{-a}\psi^*(x)\frac{\partial}{\partial x}\psi(x)\ \mathrm{d}x
- i\hbar\int_{-a}^{a}\psi^*(x)\frac{\partial}{\partial x}\psi(x)\ \mathrm{d}x
- i\hbar\int_{a}^{\infty}\psi^*(x)\frac{\partial}{\partial x}\psi(x)\ \mathrm{d}x \\
&= -i\hbar(0) - i\hbar\int_{-a}^{a} A(a - x)(-A)\ \mathrm{d}x - i\hbar(0) \\
&= -\frac{3i\hbar}{4a}
\end{align}$$
But this ignores what happens to the derivative at the point where the wavefunction is discontinuous, namely $x = -a$. Strictly speaking, the derivative is undefined there, and that makes one point at which you cannot evaluate $\frac{\partial}{\partial x}\psi(x)$. By ignoring that point, you come up with a meaningless result.
In quantum mechanics, we say that wavefunctions have to be continuous, for this and other reasons. My recommendation is that you abandon this example and pick a continuous wavefunction.
A: The wavefunction has a discontinuity at $x=-a$, which gives a term $-2aA i \hbar \delta(x+a)$ when you act with $p$. The contribution from this to the expectation value of momentum exactly cancels the imaginary value you have calculated.
Two more-general points:


*

*The momentum operator is hermitian, which means its expectation value must be real (provided the wavefunction obeys appropriate boundary conditions, which yours does).

*Your wavefunction is real, and therefore has time-reversal symmetry. This implies that the expectation value of the momentum must be zero.
A: There is a simple way show that expectation value of momentum operator is real for any valid wavefunction defined on the interval from $a$ to $b$ (which can be $-\infty$ and $\infty$, but not necessarily), without needing to know about operator properties (e.g. hermiticity) and their implications.
Any complex function, including the wavefunction, can be written as $\psi(x)=A(x)e^{i\varphi(x)}$, where $A(x)$ is positive and $\varphi(x)$ is real. Expectation value of momentum is then:
\begin{equation}\begin{split}
\langle p \rangle &= -i\hbar\int_a^b \psi^*(x)\frac{d\psi(x)}{dx}dx \\
&=-i\hbar\int_a^b A(x)e^{-i\varphi(x)}\left(
\frac{dA(x)}{dx}e^{i\varphi(x)}
+
iA(x)e^{i\varphi(x)}\frac{d\varphi(x)}{dx}
\right)dx\\
&=-i\hbar\int_a^b A(x)\left(
\frac{dA(x)}{dx}
+
iA(x)\frac{d\varphi(x)}{dx}
\right)dx \\
&=-i\hbar\int_a^b \left(
\frac12\frac{dA(x)^2}{dx}
+
iA(x)^2 \frac{d\varphi(x)}{dx}
\right)dx \\
&=
-i\hbar\frac{A(b)^2-A(a)^2}2
+\hbar\int_a^b A(x)^2 \frac{d\varphi(x)}{dx}dx
\end{split}\end{equation}
On the first glimpse it seems that the expectation value should be complex in a generic case, but only the valid wavefunctions should be considered. They satisfy boundary conditions $\psi(a) = \psi(b) = 0$, which imply $A(a) = A(b) = 0$, and therefore
\begin{equation}
\langle p \rangle = \hbar \int_a^b A(x)^2 \frac{d\varphi(x)}{dx}dx,
\end{equation}
which is obviously always real.
Also note that the integrand is proportional to the derivative of the phase. Therefore, constant phase implies $\langle p \rangle = 0$ and real wavefunction (like the one provided in the question) is just a special case of a wavefunction with the constant phase, therefore it must have zero momentum expectation value.
A: I) One problem is that the momentum operator $\hat{p}$ is an unbounded operator, which means that it is only defined on a domain $D(\hat{p}) \subsetneq {\cal H}$ of the Hilbert space ${\cal H}=L^2(\mathbb{R})$.
When we apply the differentiation operator $\hat{p}=\frac{\hbar}{i}\frac{d}{dx}$ to OP's wave function
$$ \psi(x)~=~A(a-x)\theta(a-|x|), \qquad A~>~0, \tag{1}$$
we get a term proportional to a distribution, cf. Stephen Powell's answer, so that the image $\hat{p}\psi\notin{\cal H}$ is outside the Hilbert space ${\cal H}=L^2(\mathbb{R})$ of square integrable functions. Stated a bit more precise, the issue is that OP's wave function $\psi \notin D(\hat{p}) $ is not in the domain $D(\hat{p})$ of the momentum operator $\hat{p}$, so that the calculation does not make mathematical sense, cf. answer & below comments by Valter Moretti.
II) Nevertheless, apart from standard arguments about expectation values of operators, we can perform various non-rigorous heuristic explicit calculations that indicate that the average momentum $\langle p \rangle$ should be interpreted as zero:
$$\begin{align}  \langle p \rangle
~=~&\frac{\hbar }{i}\int_{\mathbb{R}} \! dx ~\psi(x)\psi^{\prime}(x)\cr
~=~&\frac{\hbar }{2i}\int_{\mathbb{R}} \! dx \frac{d}{dx}\psi(x)^2\cr
~=~&\frac{\hbar }{2i}\left[\psi(x)^2 \right]^{x=\infty}_{x=-\infty}\cr
~=~&0,\end{align} \tag{2} $$
or
$$\begin{align} \langle p \rangle
~=~~&\frac{\hbar }{i}\int_{\mathbb{R}} \! dx ~\psi(x)\psi^{\prime}(x)\cr
~\stackrel{(1)}{=}~~&\frac{\hbar A^2}{i}\int_{\mathbb{R}} \! dx~  (a-x)\theta(a-|x|)\frac{d}{dx}\left\{ (a-x)\theta(a-|x|)\right\}\cr
~\stackrel{\begin{matrix}\text{Leibniz'}\\ \text{rule}\end{matrix}}{=}&
\frac{\hbar A^2}{i}\int_{\mathbb{R}} \! dx~  (a-x)\theta(a-|x|)\cr
&\left\{-\theta(a-|x|) - (a-x){\rm sgn}(x) \delta(a-|x|) \right\} \cr
~=~~&\frac{\hbar A^2}{i}\left\{\int_{-a}^a \! dx~  (x-a) 
- \sum_{x=\pm a} (a-x)^2\theta(a-|x|){\rm sgn}(x) \right\} \cr
~=~~&\frac{\hbar A^2}{i}\left\{ -2a^2+4a^2 \theta(0)-0\right\}\cr
~=~~&0. \end{align} \tag{3}$$
In the last equality we assigned the value $\theta(0)=\frac{1}{2}$ to the Heaviside step function $\theta$, cf. e.g. this Phys.SE post.
III) It becomes more ill-defined to try to calculate
$$\langle p^2 \rangle~=~\left(\frac{\hbar }{i}\right)^2\int_{\mathbb{R}} \! dx ~\psi^{\prime}(x)^2~=~\infty,\tag{4}$$
partly because a square of the Dirac delta distribution is ill-defined, cf. e.g. this Phys.SE post. At least the value $\infty$ in eq. (4) doesn't conflict with the Heisenberg uncertainty relations!
