Let $A \in \mathbb{R}^{n \times n}$ and $x \in \mathbb{R}^{n \times 1}$. I am wondering what the partial derivative of each entry of $A^n x$ with respect to the entries of $A$ is. Is there a closed form expression for this?
$$\frac{\partial}{\partial A_{i,j}} A^nx$$
I wasn't able to find an answer.
Solution
Letting $e_k$ be $k$-th standard basis vector, the derivative is $$ \boxed{ \frac{\partial}{\partial A_{ij}} \left[A^{n}x\right]=\left(\sum_{k=0}^{n-1}A^{k}e_i e_j^\intercal A^{n-k-1}\right)x } $$
Derivation
For brevity, define $E=e_{i}e_{j}^{\intercal}$ and $\partial_{ij}\equiv\partial / \partial A_{ij}$. Then, \begin{align*} \partial_{ij}\left[A^{n}\right] & =\partial_{ij}\left[AA^{n-1}\right]\\ & =\partial_{ij}\left[A\right]A^{n-1}+A\partial_{ij}\left[A^{n-1}\right]\\ & =EA^{n-1}+A\partial_{ij}\left[A^{n-1}\right]. \end{align*} Therefore, by induction, \begin{align*} \partial_{ij}\left[A^{n}\right] & =EA^{n-1}+A\partial_{ij}\left[A^{n-1}\right]\\ & =EA^{n-1}+AEA^{n-2}+A^{2}\partial_{ij}\left[A^{n-2}\right]\\ & =\cdots\\ & =EA^{n-1}+AEA^{n-2}+\cdots+A^{n-1}EA^{n-n}+A^{n}\partial_{ij}\left[A^{n-n}\right]\\ & =EA^{n-1}+AEA^{n-2}+\cdots+A^{n-1}E\\ & =\sum_{k=0}^{n-1}A^{k}EA^{n-k-1}. \end{align*}
Verification
Here is some code to numerically verify (with finite differences) 1000 random instances. Note that in the code, the matrix powers are not computed efficiently.