Derivative: matrix quadratic form when $\mathbf{B}$ is dependent on $\mathbf{m}$ I have the following equation:
$$
f = \mathbf{m^{T}Bm}
$$
where B is:
$$
\mathbf{B} = \frac{1}{3(m_{x}^{2} + m_{y}^{2})}\begin{pmatrix}
1 & 0.5 & 1\\ 
0.5 & 1 & 0.5\\
1 & 1 & 0.5
\end{pmatrix}
$$
and m is:
$$
\mathbf{m} = \begin{pmatrix}
m_{x}\\ 
m_{y}\\ 
m_{z}
\end{pmatrix}
$$
also there is another equation:
$$
\mathbf{m} = a\mathbf{x} + b\mathbf{y} + c\mathbf{z}
$$
UPDATE:
I extract the equation from a more complicated one but here
$m_x = a$, $m_y = b$ and $m_z = c$
My question is How can I calculate the derivative of f with respect to a ? Thanks very much. 
 A: Let 
$$\eqalign{
P &= \begin{bmatrix} 2 & 1 & 2 \\ 1 & 2 & 1 \\ 2 & 2 & 1 \end{bmatrix},\,\,
&Q = \begin{bmatrix} 6 & 0 & 0 \\ 0 & 6 & 0 \\ 0 & 0 & 0 \end{bmatrix} \cr\cr
m &= [\,a\,b\,c\,]^T \cr
M &= mm^T,\,\,\,
&f = \frac{P:M}{Q:M} \cr
}$$
where $\,:\,$ represents the inner/Frobenius product, i.e. $A:B=\operatorname{tr}(A^TB)$
 
Find the differential of the function, then the gradient
$$\eqalign{
df &= \frac{(Q:M)\,P:dM-(P:M)\,Q:dM}{(Q:M)^2} \cr
   &= \Bigg(\frac{(Q:M)\,P-(P:M)\,Q}{(Q:M)^2}\Bigg):dM \cr
   &= R:dM \cr
   &= R:(dm\,m^T+m\,dm^T) \cr
   &= R:dm\,m^T+R:m\,dm^T \cr
   &= R:dm\,m^T+R^T:dm\,m^T \cr
   &= (R+R^T)\,m:dm \cr
\cr
g=\frac{\partial f}{\partial m} &= (R+R^T)\,m \cr
\cr
}$$
The derivative wrt $a$ is the first component of the gradient
$$\eqalign{\frac{\partial f}{\partial a} &= g_x \cr\cr\cr}$$
Update
The gradient of the gradient can also be found. To make that calculation easier, let's use symmetric matrices
$$\eqalign{
P &= \begin{bmatrix} 4 & 2 & 4 \\ 2 & 4 & 3 \\ 4 & 3 & 2 \end{bmatrix},\,\,
&Q = \begin{bmatrix} 12 & 0 & 0 \\ 0 & 12 & 0 \\ 0 & 0 & 0 \end{bmatrix} }$$ This won't change the function value because the skew parts are annihilated by the inner product with the symmetric matrix $M$. This change makes $R$ symmetric, too.
Let's also define some scalar coefficients to simplify the expression for $R$
$$\eqalign{
R &= \alpha P-\beta Q,\,\,\,&dR&= P\,d\alpha-Q\,d\beta \cr
\alpha&=(Q:M)^{-1},\,\,\,&d\alpha&=-\alpha^2(Q:dM)=-2\alpha^2\,m^TQ\,dm \cr
\beta &= \alpha^2(P:M) = \alpha f,\,\,\,&d\beta&=\alpha\,df + f\,d\alpha \cr
&\,&\,&= \alpha g^T\,dm - 2f\alpha^2\,m^TQ\,dm \cr
}$$
Now find the differential and gradient of $g$
$$\eqalign{
 g &= 2Rm \cr\cr
dg &= 2\,(R\,dm + dR\,m) \cr
   &= 2\,(R\,dm + Pm\,d\alpha - Qm\,d\beta) \cr
   &= 2\,(R\,dm + Pm\,d\alpha - Qmf\,d\alpha - Qm\alpha\,df) \cr
   &= 2\,(R\,dm + (Pm-fQm)\,d\alpha - \alpha Qmg^T\,dm) \cr
   &= 2\,(R\,dm - Rm\,\frac{d\alpha}{\alpha}- \alpha Qmg^T\,dm) \cr
   &= 2\,(R\,dm - Rm(2\alpha\,m^TQ\,dm) - 2\alpha Qmm^TR\,dm) \cr
 &= 2\,\big(R -2\alpha Rmm^TQ-2\alpha Qmm^TR\big)\,dm \cr
 &= 2\,\big(R -2\alpha RMQ -2\alpha QMR\big)\,dm \cr\cr
\frac{\partial g}{\partial m} &= 2R - 4\alpha(RMQ+QMR) \cr\cr
}$$
So that's the second derivative, but please double-check the algebra before trusting it.  
As a quick check, $(Q,M,R)$ are all symmetric matrices, therefore the second derivative is also a symmetric matrix -- as it should be.
