Derivative of 2nd norm of multivariate function What will be the gradient of $\Vert \mathbf{w}^T\mathbf{X-a}\Vert_2^2 + \Vert  \mathbf{w}\Vert_2^2 + \Vert \mathbf{w}-\mathbf{DD}^{\dagger}\mathbf{w}\Vert_2^2$  w.r.to vector $\mathbf{w}$, where $\dagger$ denotes pseudo inverse.
 A: The function in the accepted answer was written incorrectly, i.e. with transposed $X$ and $a$ values.
It should be 
$$f = \|X^Tw-a^T\|^2_2 + \|w\|^2_2 + \|w-DD^+w\|^2_2$$ 
Also note that 
$$\eqalign{
P &= DD^+ \cr
Q & = I-P \cr
}$$
are ortho-projectors, which have several nice properties, (e.g. $P^T=P=P^2)\,$ which can be used to simplify intermediate results. 
$$ $$
Begin by writing the function in terms of the Frobenius (:) Inner Product, rather than the Frobenius Norm
$$\eqalign{
 f &= (X^Tw-a^T):(X^Tw-a^T) + w:w + Qw:Qw \cr
}$$
and taking its differential
$$\eqalign{
df &= 2(X^Tw-a^T):X^Tdw + 2w:dw + 2Qw:Q\,dw \cr
   &= 2X(X^Tw-a^T):dw + 2w:dw + 2Q^TQw:dw \cr
   &= 2\Big(XX^Tw - Xa^T + w + Qw\Big):dw \cr
}$$
Since $df=\big(\frac{\partial f}{\partial w}:dw\big),\,$ the gradient must be
$$\eqalign{
 \frac{\partial f}{\partial w}  &= 2\,(XX^Tw - Xa^T + w + Qw) \cr
   &= 2XX^Tw - 2Xa^T + 2w + 2(I-DD^+)w \cr
   &= 4w + 2XX^Tw - 2Xa^T- 2DD^+w \cr
}$$
A: First, the equation is defined the function as $f(\mathbf{w})$ and is described using only vectors. 
$$
\begin{align}
f(\mathbf{w})=&\Vert \mathbf{X\mathbf{w}-a}\Vert_2^2 + \Vert  \mathbf{w}\Vert_2^2 + \Vert \mathbf{w}-\mathbf{DD}^{\dagger}\mathbf{w}\Vert_2^2 \\
=&(\mathbf{X\mathbf{w}-a})^{\text{T}}(\mathbf{X\mathbf{w}-a})+(\mathbf{w}^{\text{T}}\mathbf{w})+(\mathbf{w}-\mathbf{DD}^{\dagger}\mathbf{w})^{\text{T}}(\mathbf{w}-\mathbf{DD}^{\dagger}\mathbf{w})
\end{align}
$$
Next, we estimate the gradient of the function.
$$
\begin{align}
\cfrac{\partial f}{\partial \mathbf{w}}=&
\cfrac{\partial}{\partial \mathbf{w}}
(\mathbf{X\mathbf{w}-a})^{\text{T}}(\mathbf{X\mathbf{w}-a}) \\
+& \cfrac{\partial}{\partial \mathbf{w}} (\mathbf{w}^{\text{T}}\mathbf{w}) \\
+& \cfrac{\partial}{\partial \mathbf{w}} (\mathbf{w}-\mathbf{DD}^{\dagger}\mathbf{w})^{\text{T}}(\mathbf{w}-\mathbf{DD}^{\dagger}\mathbf{w})
\end{align}
$$
Then, the first term is
$$
\begin{align}
\cfrac{\partial}{\partial \mathbf{w}}
(\mathbf{X\mathbf{w}-a})^{\text{T}}(\mathbf{X\mathbf{w}-a})=& 
\cfrac{\partial}{\partial \mathbf{w}} \{ \mathbf{w}^{\text{T}}(X^{\text{T}}X)\mathbf{w}-2\mathbf{w}^{\text{T}}X\mathbf{a}+\mathbf{a}^{\text{T}}\mathbf{a} \} \\
=& 2(X^{\text{T}}X)\mathbf{w}-2X\mathbf{a}
\end{align}
$$
When $X$ is a symmetric matrix, the above equation can be factorized like $2\mathbf{X}(\mathbf{X\mathbf{w}-a})$. Likewise, the third term is
$$
\begin{align}
\cfrac{\partial}{\partial \mathbf{w}} (\mathbf{w}-\mathbf{DD}^{\dagger}\mathbf{w})^{\text{T}}(\mathbf{w}-\mathbf{DD}^{\dagger}\mathbf{w})=&
\cfrac{\partial}{\partial \mathbf{w}} \{
\mathbf{w}^{\text{T}}(E-\mathbf{DD}^{\dagger})^{\text{T}}(E-\mathbf{DD}^{\dagger})\mathbf{w} \} \\ =&
2(E-\mathbf{DD}^{\dagger})^{\text{T}}(E-\mathbf{DD}^{\dagger})\mathbf{w}
\end{align}
$$
Then, $\mathbf{E}$ is an identical matrix. Also, it can be factorized as $2(\mathbf{E}-\mathbf{DD}^{\dagger})^{2}\mathbf{w}$ when $\mathbf{DD}^{\dagger}$ is a symmetric matrix. Therefore the calculation result is shown as follows totally.
$$
\cfrac{\partial f}{\partial \mathbf{w}}=2\biggl[
(X^{\text{T}}X)\mathbf{w}-X\mathbf{a}+
\mathbf{w}+
(E-\mathbf{DD}^{\dagger})^{\text{T}}(E-\mathbf{DD}^{\dagger})\mathbf{w}
\biggr]
$$
