<s>[INST] solve $(D^2-2D+1)y=x\sin x$ 
Find a particular solution to the equation $$(D^2-2D+1)y=x\sin x$$

\begin{align}
\text{P.I.}&=\frac{1}{(D^2-2D+1)}x\sin x\\
&=\frac{1}{(D-1)^2}x\sin x\\
&=\text{I.P.}\left[\frac{1}{(D-1)^2}xe^{ix}\right]\\
&=\text{I.P.}\left[e^{ix}\frac{1}{(D+i-1)^2}x\right]\\
&=\text{I.P.}\left[e^{ix}\frac{1}{2}(1-\frac{iD^2}{2}+D+iD+\cdots)x\right]\\
&=\text{I.P.}\left[(\cos x+i\sin x)(\frac{1}{2}x+\frac{1}{2}+\frac{1}{2}i)\right]\\
&=\text{I.P.}\left[\frac{1}{2}x\cos x+\frac{1}{2}\cos x+\frac{1}{2}i\cos x+\frac{1}{2}xi\sin x+\frac{1}{2}i\sin x-\frac{1}{2}\sin x\right]\\
&=\frac{1}{2}\cos x+\frac{1}{2}x\sin x+\frac{1}{2}\sin x\\
\text{I.P.}&=\text{Imaginary Part}
\end{align}
But the solution provided by Mathematica is: 



Is my solution is wrong$?$ Or I am getting correct answer in different form$!$
 [/INST] Mathematica's answer is correct. Maybe you made a sign mistake somewhere. Here I used Operator Method too. A bit different. 
$$\begin {align}
y_p&=\frac 1 {(D-1)^2} x\sin x  \\
y_p&=(x-\frac 2 {(D-1)})\frac 1 {(D-1)^2} \sin x \\
y_p&=(x-\frac 2 {(D-1)})\frac 1 {-2D} \sin x \\
y_p&=\frac 1 2 (x-\frac 2 {(D-1)})\cos x \\
y_p&=\frac 1 2 x\cos x -\frac 1 {(D-1)}\cos x \\
y_p&=\frac 1 2 x \cos x-\frac {D+1}{(D^2-1)}\cos x  \\
&=\frac 1 2 x\cos x +\frac 1 2 {(D+1)}\cos x\\
&=\frac 1 2 x \cos x+\frac 1 2 (-\sin x +\cos x) \\
y_p&=\frac 1 2 (x \cos x-\sin x +\cos x)
\end{align}
$$

Edit:
For your solution it's $2i$ that you should have at the denominator not 2...
$$P(D)=(D+i-1)^2=D^2+2D(i-1)-2i=-2i(1+iD^2/2-D(1+i))$$
So you have to take all the terms that have a i factor...You are close to the solution
$$P(D)^{-1}=\frac i 2(1+D(1+i)+.......)$$
$$P(D)^{-1}x=\frac i 2(x+1+i)$$
$$y_p=\mathcal {Im} \{e^{ix}P(D)^{-1}x\}$$
$$y_p=\frac 1 2\mathcal {Im} \{(\cos x +i \sin x)(ix+i-1)\}$$
$$y_p=\frac 1 2 \{(x\cos x+\cos x - \sin x) \}$$
</s>