Matrix product chain rule Given that I know
$$
    \frac{\partial x}{\partial C}
$$
Where
$$
    x = f(C) \\
x \in \mathbb{R} \\
    C = AB \\
A \in \mathbb{R}^{m \times n} \\
B \in \mathbb{R}^{n \times p} \\
$$
How do I use the chain rule to compute the following derivatives?
$$
    \frac{\partial x}{\partial A} \\
    \frac{\partial x}{\partial B}
$$
I think that
$$
\frac{\partial x}{\partial A_{i, j}} = \sum_{k=1}^p B_{j, k} \frac{\partial x}{\partial C_{i, k}} \\
\frac{\partial x}{\partial B_{j, k}} = \sum_{i=1}^m A_{i, j} \frac{\partial x}{\partial C_{i, k}}
$$
Is this right? If so, is there a more compact way to write it? It's important that I can write it compactly because it would be too slow in a computer program to not have this vectorized.
 A: Your sums look reasonable. They appear to work out as
$$\frac{\partial x}{\partial A} = \frac{\partial x}{\partial C} B^{\sf Tr} \qquad \frac{\partial x}{\partial B} = A^{\sf Tr} \frac{\partial x}{\partial C}$$
(which suggests that perhaps you ought to represent the matrices of partial derivatives in transposed form).
A: $
\def\o{{\tt1}} \def\p{\partial}
\def\BR#1{\big(#1\big)}
\def\LR#1{\left(#1\right)}
\def\op#1{\operatorname{#1}}
\def\trace#1{\op{Tr}\LR{#1}}
\def\qiq{\quad\implies\quad}
\def\grad#1#2{\frac{\p #1}{\p #2}}
\def\c#1{\color{red}{#1}}
\def\CLR#1{\c{\LR{#1}}}
$Given the following problem variables
$$\eqalign{
C=AB,\qquad x=f(C),\qquad G=\grad{x}{C} \\
}$$
Substitute the differential of $C$ into that of $x$
$$\eqalign{
dx &= G:\c{dC} \\
  &= G:\CLR{dA\,B+A\,dB} \\
  &= \LR{GB^T}:dA + \LR{A^TG}:dB \\
}$$
From this expression the required gradients are readily identified
$$\eqalign{
\grad{x}{A}=GB^T,\qquad\quad \grad{x}{B}=A^TG \\\\
}$$

The above makes use of the Frobenius product, which is a concise notation for the trace
$$\eqalign{
A:M &= \sum_{i=\o}^m\sum_{j=\o}^n A_{ij}M_{ij} \;=\; \trace{A^TM} \\
A:A &= \|A\|^2_F \qquad \{ {\rm Frobenius\:norm} \}\\
}$$
This is also called the double-dot or double contraction product.
When applied to vectors $(n=\o)$ it reduces to the standard dot product.
The properties of the underlying trace function allow the terms in such a
product to be rearranged in many fruitful ways, e.g.
$$\eqalign{
A:M &= M:A \\
A:M &= A^T:M^T \\
C:\LR{AB} &= \LR{CB^T}:A \\&= \LR{A^TC}:B \\
}$$
