Let $p(x)$ be a polynomial, and assume that $ \int_{a}^{b} p(x) \cot \left(\frac{ax}{2} \right) \, \mathrm dx $ converges.
How do you prove that $$ \int_{a}^{b} p(x) \cot \left(\frac{ax}{2} \right) \, \mathrm dx = 2 \sum_{n=0}^{\infty} \int_{a}^{b} p(x) \sin(anx) \, \mathrm dx? $$
I can verify that this identity is true in particular cases, but I'm not sure how to prove it.
EDIT:
The lower limit of the integral and the integrand parameter don't need to be the same.
So the identity could be written as $$ \int_{a}^{b} p(x) \cot \left(\frac{ r x}{2} \right) \, \mathrm dx = 2 \sum_{n=0}^{\infty} \int_{a}^{b} p(x) \sin( r nx) \, \mathrm dx .$$
And as was mentioned below, $p(x)$ doesn't need to be a polynomial.
There are three other similar identities:
$$ \begin{align*} &\int_{a}^{b} p(x) \tan \left(\frac{rx}{2} \right) \, \mathrm dx = -2 \sum_{n=0}^{\infty} (-1)^{k} \int_{a}^{b} p(x) \sin(rnx) \, \mathrm dx \\ &\int_{a}^{b} p(x) \csc \left(rx \right) \, \mathrm dx = 2 \sum_{n=0}^{\infty} \int_{a}^{b} p(x) \sin[(2n+1)rx] \, \mathrm dx \\ &\int_{a}^{b} p(x) \sec \left(rx \right) \, \mathrm dx = 2 \sum_{n=0}^{\infty} (-1)^{k} \int_{a}^{b} p(x) \cos[(2n+1)rx] \, \mathrm dx \end{align*} $$
They can all be derived in the way Daniel Fischer derived the original one by using the following finite sums:
$$ \begin{align} &\sum_{n=0}^{N} (-1)^{n} \sin(rnx) = - \frac{1}{2} \tan \left(\frac{rx}{2}\right) + \frac{(-1)^{N} \sin [(N+\frac{1}{2})rx]}{2\cos (\frac{rx}{2})} \\ &\sum_{n=0}^{N} \sin [(2n+1)rx] = \frac{1}{2} \csc (rx) - \frac{\cos [2(N+1)rx]}{2 \sin (rx)} \\ &\sum_{n=0}^{N} (-1)^{n} \cos [(2n+1)rx] = \frac{1}{2} \sec(rx) + \frac{(-1)^{N}\cos [2(N+1)rx]}{2 \cos (rx)} \end{align}$$
Basically, because of the Riemann-Lebesgue lemma. By summing a geometric sum, or by induction using trigonometric identities, one finds
$$\sum_{n=0}^N 2\sin (anx) = \cot \frac{ax}{2} - \frac{\cos \left(a(N+\frac12)x\right)}{\sin \frac{ax}{2}}.$$
So that yields
$$\int_a^b p(x) \cot \frac{ax}{2}\,dx = 2\sum_{n=0}^N \int_a^b p(x)\sin (anx)\,dx + \int_a^b p(x)\frac{\cos \left(a(N+\frac12)x\right)}{\sin \frac{ax}{2}}\,dx.$$
Now if $\int_a^b p(x)\cot \frac{ax}{2}\,dx$ converges, the same is true for
$$\begin{align} \int_a^b p(x)\frac{\cos \left(a(N+\frac12)x\right)}{\sin \frac{ax}{2}} &= \int_a^b p(x) \frac{\cos (aNx)\cos \frac{ax}{2} - \sin (aNx)\sin \frac{ax}{2}}{\sin \frac{ax}{2}}\,dx\\ &= \int_a^b p(x) \cot \frac{ax}{2}\cos (aNx)\,dx - \int_a^b p(x)\sin (aNx)\,dx, \end{align}$$
and by the Riemann-Lebesgue lemma, both of these integrals converge to $0$ for $N \to \infty$.