# Gradient of $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]$ w.r.t. $x$

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.

• 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. – greg Apr 7 at 23:49
• I have done some modification. How about now? – 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}

Thus:

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

edit: thanks @greg

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