I am studying linear algebra from notes for a mathematical physics course and the section on diagonal matrices says that if $M$ is a diagonal matrix and $f$ is analytic, then $f(M)$ is equal to the matrix $M$ with the function $f$ applied to all its diagonal entries. The notes then say this can be shown using Taylor expansion.
I'm confused by this. $f(M)$ seems ill-defined. So do its derivatives. So how do you use a Taylor expansion to show the above?
If$$f(z)=a_0+a_1z+a_2z^2+\cdots,$$then$$f(M)=a_0+a_1M+a_2M^2+\cdots\tag1$$But if$$M=\begin{pmatrix}d_1&0&0&\ldots&0\\0&d_2&0&\ldots&0\\0&0&d_3&\ldots&0\\\vdots&\vdots&\vdots&\ddots&\vdots\\0&0&0&\ldots&d_n\end{pmatrix},$$then $(1)$ is equal to$$a_0\operatorname{Id}_n+a_1\begin{pmatrix}d_1&0&0&\ldots&0\\0&d_2&0&\ldots&0\\0&0&d_3&\ldots&0\\\vdots&\vdots&\vdots&\ddots&\vdots\\0&0&0&\ldots&d_n\end{pmatrix}+a_2\begin{pmatrix}{d_1}^2&0&0&\ldots&0\\0&{d_2}^2&0&\ldots&0\\0&0&{d_3}^2&\ldots&0\\\vdots&\vdots&\vdots&\ddots&\vdots\\0&0&0&\ldots&{d_n}^2\end{pmatrix}+\cdots,$$which is just$$\begin{pmatrix}f(d_1)&0&0&\ldots&0\\0&f(d_2)&0&\ldots&0\\0&0&f(d_3)&\ldots&0\\\vdots&\vdots&\vdots&\ddots&\vdots\\0&0&0&\ldots&f(d_n)\end{pmatrix}.$$