partial derivative of cosine similarity I asked a question about 
derivative of cosine similarity.
But no one has answered my question. 
Therefore I tried to do it my self as bellow.
$$
cossim(a,b)=\frac{a\cdot{b}}{\sqrt{a^2\cdot{b^2}}}
\\\frac{cossim(a,b)}{\partial{a_1}}=\frac{\partial}{\partial{a_1}} \frac{a_1\cdot{b_1}+...+a_n\cdot{b_n}}{|a|\cdot|b|}
\\=\frac{\partial}{\partial{a_1}}a_1\cdot{b_1}\cdot{(a_1^2+a_2^2+...a_n^2)^{-1/2}}\cdot{|b|^{-1}}
\\= {b_1}\cdot{(a_1^2+a_2^2+...a_n^2)^{-1/2}}\cdot{|b|^{-1}}-a_1^2b_1(a_1^2+a_2^2+...a_n^2)^{-3/2} {|b|^{-1}}
\\=\frac{b_1}{|a|\cdot{|b|}}-\frac{a_1|a|^{-2}\cdot{a_1b_1}}{|a|\cdot{|b|}}
\\=\frac{b_1}{|a|\cdot{|b|}}-\frac{a_1\cdot{b_1}}{|a|\cdot{|b|}}\cdot{\frac{a_1}
{|a|^2}}
\\\therefore \frac{\partial}{\partial{a}}cossim(a,b)= \frac{b_1}{|a|\cdot{|b|}}-cossim(a,b)\cdot{\frac{a_1}
{|a|^2}}
$$
Is this correct?
 A: Putting
$$
\cos (\mathbf{v},\mathbf{w}) = \frac{{\mathbf{v} \cdot \mathbf{w}}}
{{\left| {\mathbf{v} } \right|\;\left| \mathbf{w} \right|}}
$$
I would develop the required derivative as follows:
$$
\cos (\mathbf{v} + d\mathbf{v},\mathbf{w}) = \frac{{\mathbf{v} \cdot \mathbf{w} + d\mathbf{v} \cdot \mathbf{w}}}
{{\left| {\mathbf{v} + d\mathbf{v}} \right|\;\left| \mathbf{w} \right|}}
$$
Now $\left| {\mathbf{v} + d\mathbf{v}} \right|$ can be rewritten as:
$$
\begin{gathered}
  \left| {\mathbf{v} + d\mathbf{v}} \right| = \sqrt {\left( {\mathbf{v} + d\mathbf{v}} \right) \cdot \left( {\mathbf{v} + d\mathbf{v}} \right)}  = \sqrt {\left| \mathbf{v} \right|^2  + \left| {d\mathbf{v}} \right|^2  + 2\mathbf{v} \cdot d\mathbf{v}}  =  \hfill \\
   = \left| \mathbf{v} \right|\sqrt {1 + 2\frac{\mathbf{v}}
{{\left| \mathbf{v} \right|^2 }} \cdot d\mathbf{v} + \frac{{\left| {d\mathbf{v}} \right|^2 }}
{{\left| \mathbf{v} \right|^2 }}}  \approx \left| \mathbf{v} \right|\left( {1 + \frac{\mathbf{v}}
{{\left| \mathbf{v} \right|^2 }} \cdot d\mathbf{v}} \right) \hfill \\ 
\end{gathered} 
$$
hence:
$$
\begin{gathered}
  \cos (\mathbf{v} + d\mathbf{v},\mathbf{w}) = \frac{{\mathbf{v} \cdot \mathbf{w} + d\mathbf{v} \cdot \mathbf{w}}}
{{\left| {\mathbf{v} + d\mathbf{v}} \right|\;\left| \mathbf{w} \right|}} \approx \frac{{\mathbf{v} \cdot \mathbf{w} + d\mathbf{v} \cdot \mathbf{w}}}
{{\left| \mathbf{v} \right|\left( {1 + \frac{\mathbf{v}}
{{\left| \mathbf{v} \right|^2 }} \cdot d\mathbf{v}} \right)\;\left| \mathbf{w} \right|}} \approx  \hfill \\
   \approx \frac{{\mathbf{v} \cdot \mathbf{w} + \mathbf{w} \cdot d\mathbf{v}}}
{{\left| \mathbf{v} \right|\;\left| \mathbf{w} \right|}}\left( {1 - \frac{\mathbf{v}}
{{\left| \mathbf{v} \right|^2 }} \cdot d\mathbf{v}} \right) \approx \frac{{\mathbf{v} \cdot \mathbf{w}}}
{{\left| \mathbf{v} \right|\;\left| \mathbf{w} \right|}} + \left( {\frac{\mathbf{w}}
{{\left| \mathbf{v} \right|\;\left| \mathbf{w} \right|}} - \frac{{\mathbf{v} \cdot \mathbf{w}}}
{{\left| \mathbf{v} \right|\;\left| \mathbf{w} \right|}}\frac{\mathbf{v}}
{{\left| \mathbf{v} \right|^2 }}} \right) \cdot d\mathbf{v} =  \hfill \\
   = \cos (\mathbf{v},\mathbf{w}) + \left( {\frac{\mathbf{w}}
{{\left| \mathbf{v} \right|\;\left| \mathbf{w} \right|}} - \cos (\mathbf{v},\mathbf{w})\frac{\mathbf{v}}
{{\left| \mathbf{v} \right|^2 }}} \right) \cdot d\mathbf{v} \hfill \\ 
\end{gathered} 
$$
Therefore, apart from a typo, your derivation
$$
\frac{\partial}{\partial{a_1}}cossim(a,b)= \frac{b_1}{|a|\cdot{|b|}}-cossim(a,b)\cdot{\frac{a_1}
{|a|^2}}
$$
looks to be correct.
A: For typing convenience, define the variables
$$\eqalign{
x &= \|b\|^{-1}\,b \\
\lambda &= \|a\|,\quad \lambda^2 = a^Ta,\quad \lambda\,d\lambda = a^Tda \\
}$$
Denote the cosine similarity by $\phi$ and find its differential and gradient.
$$\eqalign{
\phi &= x:\Big(\frac{a}{\lambda}\Big) \\
d\phi
 &= x:\bigg(\frac{\lambda\,da-a\,d\lambda}{\lambda^2}\bigg) \\
 &= \lambda^{-3}x:(\lambda^2\,da-a\lambda\,d\lambda) \\
 &= \lambda^{-3}x:(\lambda^2\,I-aa^T)\,da \\
 &= \lambda^{-3}(\lambda^2\,I-aa^T)\,x:da \\
\frac{\partial \phi}{\partial a} 
 &= \lambda^{-3}(\lambda^2\,I-aa^T)\,x \\
 &= \frac{(a^Ta)\,b-(a^Tb)\,a}{\|a\|^3\;\|b\|} \\
\\
}$$
Note that above, a colon represents the trace/Frobenius product, i.e.
$$\eqalign{A:B = {\rm Tr}(A^TB)}$$
and is equivalent to the dot product when both arguments are vectors.
But when mixing matrix-vector and vector-vector products in the same expression, e.g.
$$
Ax:b
\quad{\longleftrightarrow}\quad 
(A\cdot x) \cdot b
\quad{\longleftrightarrow}\quad 
b\cdot A\cdot x
$$
the Frobenius product is both clearer and easier to type.
Even purely vector expressions are quicker to type this way
$$
a:a
\quad{\longleftrightarrow}\quad 
a^Ta
\quad{\longleftrightarrow}\quad 
a\cdot a
$$
