# How to do matrix calculus in Mathematica?

I would like to differentiate Ax with respect to A where A is a matrix. Now, obviously, I know the derivative is x but for more nasty expressions, is there a way to differentiate expressions with respect to matrices in Mathematica? I've only seen scalar differentiation so far.

• probably a real nontrivial example would be useful. – george2079 Oct 20 '15 at 16:17
• What you have written is incorrect. The gradient of $Ax$ with respect to $x$ is $A^T$, but the derivative of $Ax$ with respect to $A$ is actually a rank-3 tensor (i.e. you need to take the derivative of $(Ax)_i$ with respect to each $A_{jk}$). – Ross B. Sep 20 '16 at 20:49

I suppose the answer may not be general enough but there isn't to much info in the quesion.

Let's make MMA forget about the fact that A is a matrix.

A = Array[a, {5, 5}];

Block[{A},
D[A x, A]
]

x