Derivative of scalar function $\rm v^\top A^n v$ w.r.t. matrix $\rm A$ I would like to derivative of a scalar function with respect to matrix.
In pariticular,
For given vector $v \in \mathbb{R}^n$, let $f(A)=v^\top A^n v$ for any integer $n\in\mathbb{N}$. I want to find $\nabla_A (v^\top A^n v)$
When $n=1$, $$\nabla_A (v^\top A v) = v^\top v$$ and for $n=2$,
$$\nabla_A(v^\top A^2 v) = Avv^\top + vv^\top A^\top$$
But I don't know how to obtain derivative for general $n$.
Is there general form of derivative?

Let
$$f (\mathrm X) := \mathrm a^{\top} \mathrm X^n \, \mathrm a = \mbox{tr} (\mathrm a^{\top} \mathrm X^n \, \mathrm a) = \mbox{tr} (\mathrm a \mathrm a^{\top} \mathrm X^n)$$
Since
$$(\mathrm X + h \mathrm M)^n = \mathrm X^n + h \left( \sum_{k=0}^{n-1} \mathrm X^k \mathrm M \mathrm X^{n-1-k} \right) + O (h^2)$$
then the directional derivative of $f$ in the direction of $\mathrm M$ at $\mathrm X$ is
$$D_{\mathrm M} \, f (\mathrm X) = \mbox{tr} \left( \sum_{k=0}^{n-1} \mathrm a \mathrm a^{\top} \mathrm X^k \mathrm M \mathrm X^{n-1-k} \right) = \mbox{tr} \left( \left( \sum_{k=0}^{n-1} \mathrm X^{n-1-k} \mathrm a \mathrm a^{\top} \mathrm X^k \right) \mathrm M \right)$$
Thus, the gradient is
$$\nabla_{\mathrm X} f (\mathrm X) = \left( \sum_{k=0}^{n-1} \mathrm X^{n-1-k} \mathrm a \mathrm a^{\top} \mathrm X^k \right)^{\top}$$
