I read in my engineering mathematics textbook that if we have a differential equation of the form $f(D)y=\cos(ax)$ or $f(D)y = \sin(ax)$ where $f(D)$ is a polynomial in $D$ i.e. $\frac{d}{dx}$ then we can find its particular integral using the following rule:
Write P.I as $$\frac{1}{f(D)} \cos(ax)$$ or $$\frac{1}{f(D)} \sin(ax)$$
Replace all occurrences of $D^2$ in $f(D)$ with $-a^2$. For example if $f(D)$ is $2D^3+D^2+D$ then write it as $2D(-a^2)-a^2+D$.
Then use the general methods to find evaluate the particular integral as usual.
But I'm not sure how the step in which we replace $D^2$ by $-a^2$ justified mathematically.
Because ultimately, $D^2$ is getting applied to $\sin(ax)$ and $\cos(ax)$, meaning that applying $D^2$ is the same as multiplying with $-a^2$.