How to compute the gradient of
$$\eqalign{ f(x) &= 1^T \left[ \left( x - 1 \left[1^T x\right] \right) \odot \left(x - 1 \left[1^T x\right] \right) \right]\cr }$$ where $x \in M_{n,1}(\mathbb{R})$, $1 \in M_{n,1}$ is a column vector with all ones, $\odot$ is an element-wise multiplication.

  • $\begingroup$ The quantity $(x-1^Tx)$ is dimensionally incompatible. If $x$ is a vector and $1$ is a vector, then $1^Tx$ is a scalar. You can't subtract a scalar from a vector. $\endgroup$ – greg Apr 7 at 23:49
  • $\begingroup$ I have done some modification. How about now? $\endgroup$ – learning Apr 8 at 4:42

Setting the matrix of ones $11^T = J$, we can write $f$ in terms of a variable $a$ with differential:

\begin{align} a&= (I-J)x \\ da &= (I - J)dx \end{align}

where $I$ is the identity matrix.

Replacing $a$ and writing the expression in terms of the Frobenius product we can calculate the differential of $f$:

\begin{align} f &= 1 : a \odot a\\ df &= 1: 2a \odot da\\ &= 1 \odot 2a : da\\ &= 2a : da \\ &= 2(I-J)x : (I - J)dx\\ &= 2(I-J)^2x : dx \end{align}


\begin{equation} \frac{\partial f}{\partial x} = 2(I-J)^2x = 2x +2(n-2)Jx \end{equation}

edit: thanks @greg

  • 1
    $\begingroup$ Since $J^2=nJ\,$ you can simplify the term $$(I-J)^2 = I+(n-2)J$$ $\endgroup$ – greg Apr 8 at 13:39
  • $\begingroup$ Nice, thank you $\endgroup$ – learning Apr 8 at 20:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.