<s>[INST] Solve $\int x^2e^x\sin x$ Solve $\int x^2e^x\sin x$
My attempt is as follows:-
$$I_1=\int e^x\sin x$$
$$I_1=e^x\sin x-\int e^x\cos x$$
$$I_1=e^x\sin x-e^x\cos x-\int\sin (x)e^x$$
$$2I_1=e^x\left(\sin x-\cos x\right)$$
$$I_1=\dfrac{e^x\left(\sin x-\cos x\right)}{2}\tag{1}$$
$$I_2=\int e^x\cos x$$
$$I_2=e^x\cos x+\int e^x\sin x$$
$$I_2=e^x\cos x+e^x\sin x-\int e^x \cos x$$
$$I_2=\dfrac{e^x(\cos x+\sin x)}{2}$$
$$I=\int x^2e^x\sin x$$
$$I=I_1x^2-2\int xI_1$$
$$I=I_1x^2-\int xe^x(\sin x-\cos x)$$
$$I=I_1x^2-\int xe^x\sin x+\int xe^x\cos x$$
$$I=I_1x^2-xI_1+\int I_1+xI_2-\int I_2$$
$$I=I_1x^2-xI_1+xI_2+\int I_1-\int I_2$$
$$I=I_1x^2-xI_1+xI_2+\dfrac{1}{2}\int e^x\left(\sin x-\cos x\right) -\dfrac{1}{2}\int e^x\left(\cos x+\sin x\right)$$
$$I=I_1x^2-xI_1+xI_2+\dfrac{I_1}{2}-\dfrac{I_2}{2} -\dfrac{I_2}{2}-\dfrac{I_1}{2}$$
$$I=I_1x^2-xI_1+xI_2-I_2$$
$$\dfrac{e^x}{2}\left(x^2\sin x-x^2\cos x-x\sin x+x\cos x+x\cos x+x\sin x-\sin x-\cos x \right)+C$$
$$\dfrac{e^x}{2}\left((x^2-1)\sin x-(x-1)^2\cos x \right)+C$$
Is there any better way to solve it which is short and clean. Mine got very long. 
 [/INST] Start with writing $\sin x=\color{red}{\Im}e^{ix}$
$$\int x^2e^x \sin x\ dx=\color{red}{\Im}\int x^2e^{(1+i)x}\ dx$$
$$=\color{red}{\Im}\left(\frac{x^2}{1+i}e^{(1+i)x}-\frac{2x}{(1+i)^2}e^{(1+i)x}+\frac{2}{(1+i)^3}e^{(1+i)x}\right)$$
$$=x^2 \color{red}{\Im} \frac{e^{(1+i)x}}{1+i}-2x \color{red}{\Im}\frac{e^{(1+i)x}}{(1+i)^2}+2\color{red}{\color{red}{\Im}} \frac{e^{(1+i)x}}{(1+i)^3}$$
$$=x^2\left(\frac12e^x\sin x-\frac12e^x\cos x\right)-2x\left(-\frac12e^x\cos x\right)+2\left(-\frac14e^x\sin x-\frac14e^x\cos x\right)$$
$$=\frac12e^x\sin x(x^2-1)-\frac12e^x\cos x(x-1)^2+C$$
</s>