# Derivative of matrix as a function of a vector w.r.t a vector

I want to compute the derivative of the matrix $$diag(x)M$$ with respect to $$x$$, where $$x \in \mathbb{C}^{n \times 1}$$ and $$M \in \mathbb{C}^{n \times m}$$. This is how I have approached it, but I have not been successful.

First, $$Y = diag(x)$$

Then, $$Z = Y M$$

The differential of $$Z$$ is $$dZ = dY M$$

If I am not mistaken $$dY = (I_{n \times n} \otimes 1_{n \times 1}) dx$$. So $$dZ = (I_{n \times n} \otimes 1_{n \times 1}) (dx) M$$

But the dimensions do not make much sense in this last expression. Would you please help me to find the right way?

The mixed vec-diag expression can rearranged using the formula $${\rm vec}\Big(A\,{\rm Diag}(b)\,C\Big) = \Big((C^T\otimes 1_a)\odot(1_c\otimes A)\Big)\,b$$ In this case the variables of interest are $$(A=I_n,\,\,C=M,\,\,b=dx),\,$$ therefore \eqalign{ {\rm vec}(dZ) &= {\rm vec}\Big(I_n\,\,{\rm Diag}(dx)\,\,M\Big) \cr dz &= \Big((M^T\otimes 1_n)\odot(1_m\otimes I_n)\Big)\,dx \cr \frac{\partial z}{\partial x} &= (M^T\otimes 1_n)\odot(1_m\otimes I_n) \cr }