4 Case Analysis
When solving functional equations, first substitute appropriate special values to obtain more than one possible value, then classify and discuss the function values to obtain the solution of the functional equation.

Example 5 Find the function \( f: \mathbf{R} \rightarrow \mathbf{R} \), such that for all real numbers \( x, y \), we have
\[
f(x y)(x+f(y))=x^{2} f(y)+y^{2} f(x) \text{. }
\]
(2015, Croatian Mathematical Competition)
Solving: In equation (1), let $x=y=0$, we get
$$
f^{2}(0)=0 \Rightarrow f(0)=0 \text {. }
$$

In equation (1), let $x=y=1$, we get
$$
\begin{array}{l}
f(1)(1+f(1))=2 f(1) \\
\Rightarrow f^{2}(1)=f(1) .
\end{array}
$$

We discuss in two cases.
(1) If $f(1)=0$.

Substituting $x=1$ into equation (1), then for any $y \in \mathbf{R}$, we have
$$
\begin{array}{l}
f(y)(1+f(y))=f(y) \\
\Rightarrow f(y)=0 .
\end{array}
$$
(2) If $f(1)=1$.

Substituting $y=1$ into equation (1), then for any $x \in \mathbf{R}$, we have
$$
\begin{array}{l}
(1+x) f(x)=x^{2}+f(x) \\
\Rightarrow x f(x)=x^{2} .
\end{array}
$$

When $x \neq 0$, $f(x)=x$.
By $f(0)=0$, we know that for any $x \in \mathbf{R}$, $f(x)=x$.
Upon verification, $f(x)=0, f(x)=x$ are both solutions to the original equation.