Derivative of a Function of the Diag function

1.3k Views Asked by At

Suppose there is a vector $U \in \mathbb{R}^n$. How would you find the derivative of:

$$ F(U)=trace\left(diag(U) A\ diag(U) \right) $$ where $A \in \mathbb{R}^{n \times n} \succ 0 $ and where $diag(\cdot)$ creates a diagonal matrix with $(\cdot)$ on the leading diagonal. Where the derivative is taken with respect to the vector $U$, i.e.

$$ {\partial F(U) \over \partial U } \\ $$

I am more interested in the method used. Thanks in advance.

2

There are 2 best solutions below

3
On BEST ANSWER

Posting the solution I identified.

Due to the trace operator evaluating the above is equivalent to evaluating:

$$ {\partial \left(\sum\limits_{i=1}^{n} u_i \ A_{(i,i)}u_i\right)\over \partial U }= \left( \begin{align} \begin{array} {\partial \left(\sum\limits_{i=1}^{n} u_iA_{(i,i)}u_i\right)\over \partial u_1}\\ {\partial \left(\sum\limits_{i=1}^{n} u_iA_{(i,i)}u_i\right)\over \partial u_1}&\\ \vdots \ \ \ \ \ \ \ \ \ \ \ &\\ {\partial \left(\sum\limits_{i=1}^{n} u_iA_{(i,i)}u_i\right)\over \partial u_n}&\\ \end{array}\end{align}\right) \\ $$

which becomes:

$$ {\partial \left( trace(diag(U)\ A\ diag(U)) \right)\over \partial U} = 2\ diag( A) U $$

0
On

Here are two useful identities involving the Hadamard $(\odot)$ product of a matrix and two vectors.
$$\eqalign{ A\odot xy^T &= {\rm Diag}(x)\cdot A\cdot{\rm Diag}(y) \\ {\rm Tr}\Big(A\odot xy^T\Big) &= x^T\left(I\odot A\right)y \\ }$$ Note that $(I\odot A)$ preserves of the main diagonal of $A$ but sets all off-diagonal terms to zero.
It is a diagonal matrix and therefore $\;(I\odot A)^T=(I\odot A)$

These identities can be used to re-write the function $$\eqalign{ F &= {\rm Tr}\Big({\rm Diag}(u)\cdot A\cdot{\rm Diag}(u)\Big) \\ &= u^T\left(I\odot A\right)u \\ }$$ and then calculate its differential and gradient $$\eqalign{ dF &= du^T\left(I\odot A\right)u \;+\; u^T\left(I\odot A\right)du \\ &= 2u^T\left(I\odot A\right)du \\ &= g^Tdu \\ \frac{\partial F}{\partial u} &= g \;=\; 2\left(I\odot A\right)u \\ }$$