How can I solve $A\operatorname{diag}(x)=B\;?$
I am actually an engineer and recently involved in this field.
Thanks.
How can I solve $A\operatorname{diag}(x)=B\;?$
I am actually an engineer and recently involved in this field.
Thanks.
On
We consider element at position $(i, j)$ of the matrix that is the LHS,
$$ [A\, \text{diag}(x)]_{ij} = \sum_{z=1}^d A_{iz} [\text{diag}(x)]_{zj} $$ As $$[\text{diag}(x)]_{ji} = \left\{\begin{array}{cc} x_i & i = j \\ 0 & \text{otherwise} \end{array}\right. $$ then $$ [A\, \text{diag}(x)]_{ij} = A_{ij} x_{j} $$ So we have for all $(i,j)$ $$ A_{ij} x_{j} = B_{ij} $$ whether this is possible satisfy depends.
The following answer uses a convention in which uppercase latin denotes a matrix, lowercase latin a vector, and greek a scalar.
Define ${\rm diag}()$ as a function which returns the main diagonal of its matrix argument as a column vector, and ${\rm Diag}()$ as a function which create a diagonal matrix from a vector argument.
Denote the elementwise/Hadamard product using a $\odot$ symbol
and the trace/Frobenius product using a colon, i.e. $$A:B = {\rm Tr}(A^TB)$$
In the current problem, define the matrix variables $$\eqalign{ X &= {\rm Diag}(x) \\ E &= AX-B \\ }$$ Minimize the norm of $E$ (the error matrix) by formulating a cost function and calculating the point where its gradient equals zero.
$$\eqalign{ \phi &= \tfrac{1}{2}\|E\|^2 = \tfrac{1}{2}E:E \\ d\phi &= E:dE = E:A\,dX = A^TE:dX \\ &= A^TE:{\rm Diag}(dx) \\ &= {\rm diag}\Big(A^T\big(AX-B\big)\Big):dx \\ \frac{\partial\phi}{\partial x} &= {\rm diag}(A^TAX) - {\rm diag}(A^TB) \\ }$$ Set the gradient to zero and solve for the optimal $x$. $$\eqalign{ {\rm diag}(A^TB) &= {\rm diag}(A^TAX) \\ &= \left(I\odot A^TA\right)x \\ x &= (I\odot A^TA)^{-1}\operatorname{diag}\left(A^TB\right) \\ }$$ This vector is the least-squares solution to the problem.