Let $ \varphi : \mathbb{R}^m \times \mathbb{R}^{m \times n} \times \mathbb{R}^{n \times m} \to \mathbb{R} $ be defined by
$$ \varphi(x, A, B) = \left \| \left( x^T A B \right)^T - x \right \|^2 $$
What are the gradients of $\varphi$ with respect to $A$ and $B$, $\nabla_A \varphi(x, A, B) $ and $\nabla_B \varphi(x, A, B)$?
I have reached the followings. Are they correct?
$$ \nabla_A \varphi(x, A, B) = 2 ((x^TAB)^T - x)x^TB^T $$
$$ \nabla_B \varphi(x, A, B) = 2 ((x^TAB)^T - x)x^TA^T $$
This is almost correct! Define $\mathbf{C}=\mathbf{A}\mathbf{B}$ and $\mathbf{u}=\mathbf{C}^T\mathbf{x} - \mathbf{x}$
$$ \phi = \left \| \mathbf{u} \right \|^2 $$ thus $ d\phi = 2 \mathbf{u} :d\mathbf{u} = 2 \mathbf{x}\mathbf{u}^T :d\mathbf{C} $. From here, you find $$ \frac{\partial \phi}{\partial \mathbf{A}} = 2 \mathbf{x} (\mathbf{Bu})^T , \frac{\partial \phi}{\partial \mathbf{B}} = 2 (\mathbf{A}^T\mathbf{x}) \mathbf{u}^T $$ which are both matrices of the correct dimensions.