# Chain rule for matrix - i'm confused

I googled around and searched inside the forum but I'm still confused about a problem.

I have 2 matrix functions $f,g : \mathbb{R}^{n \times n} \times \mathbb{R}^{a \times b} \rightarrow \mathbb{R}^{n \times n}$. Starting from this, I have the following expression:

$$t(Q, X, Y) = \text{tr}(f(g(Q, X),Y))$$

where $\text{tr}$ is the trace operator and $X, Y \in \mathbb{R}^{a \times b}$ and $Q \in \mathbb{R}^{n \times n}$.

How do I evaluate $\frac{\partial t(Q,X, Y)}{\partial X}$ and $\frac{\partial t(Q,X, Y)}{\partial Y}$?

I mean, I would like to know how to correctly apply the chain rule.

I will try to give more information about my problem. Suppose that $a = b = n$ and that $f(A,B) = AB$ and $g(A,B) = BA + AB$ (actually this is only an example of possible functions $f$ and $g$). Then I have that:

$$f(g(Q,X),Y) = f(XQ + QX, Y) = XQY + QXY$$

Then, using matrix calculus (hoping there are no error!), I have that:

$$\frac{\partial t(Q,X,Y)}{\partial X} = QY + YQ\\ \frac{\partial t(Q,X,Y)}{\partial Y} = XQ + QX$$

I can easily compute the result if I know the form of $f$ and $g$. Notice that the derivatives I obtained are in a matrix form. But actually I need to deal with generic functions. And for this reason I need to use the chain rule. The problem is that the chain rule formulas I know are helpful to derive the derivative with respect to a certain element of the matrix $X$ (or $Y$). In this case, I'm not able to have a matrix form of the derivatives.

So, my question is... there is a chain rule formula I'm missing which let me describe these derivatives in a matrix form?

It might help to consider the function $h(Q,X,Y) = (g(Q,X),Y)$, whose differential is easily calculated. Then $t(Q,X,Y) = l \circ f \circ h (Q,X,Y)$, where $l$ is the trace – wspin Dec 17 '12 at 20:49
I know this formula (en.wikipedia.org/wiki/… - it is the 7th formula into the table). This is performed on each $X_{i,j}$ separately! I would like to know the formula with respect to all $X$. – the_candyman Dec 18 '12 at 14:34