How to find the gradient of norm square How to find the gradient of the function $f(x) = ||g(x)||^2$  
where $x \in \Bbb R^d$ and $g: \Bbb R^d \to \Bbb R$.
 A: I will assume $g:\mathbb R^d \to \mathbb R^n$.
It's nice to avoid using components.  Notice that
\begin{equation}
f(x) = h(g(x))
\end{equation}
where $h(x) = \|x\|^2$.
The chain rule tells us that
\begin{align}
f'(x) &= h'(g(x)) g'(x) \\
&= 2 \underbrace{g(x)^T}_{1 \times n} \underbrace{g'(x)}_{n \times d}.
\end{align}
If we use the convention that $\nabla f(x)$ is a column vector, rather than a row vector, then
\begin{equation}
\nabla f(x) = f'(x)^T = 2 g'(x)^T g(x).
\end{equation}
A: The $k$-th component of the gradient is
$$
\partial_k f
= \partial_k g^2
= 2 g \partial_k g
$$
So we get
$$
\mbox{grad } f = 2 g \, \mbox{grad } g
$$
If $g$ is more-dimensional, we have:
$$
\partial_k f
= \partial_k \sum_i g_i^2
= \sum_i 2 g_i \partial_k g_i
$$
So we get
$$
\mbox{grad } f = 2 J_g^T g
$$
with $(J_g)_{ij} = \partial g_i / \partial x_j$.
A: Well, we know $x = (x_1,...,x_d) $, hence,
$$ ||x||^2 = x_1^2 + ... + x_d^2 $$
Now,
$$ \frac{ \partial f}{\partial x_1 } = 2x_1$$
$$ \frac{ \partial f}{\partial x_2 } = 2x_2 $$
....
$$ \frac{ \partial f}{\partial x_k } = 2x_k $$
Hence, 
$$ grad(f) = (2x_1,....,2x_d) = 2 x $$
