I am interested in the expression:
$$ \frac{d}{d\bf{p}} \left[\left(D_1 +P_1 \right)^{-1}\left(P_1\bf{x} \right) \right] $$
where $\bf{p}, \bf{x} $ are $S \times 1$ vectors and $D_1,P_1$ are $S \times S$ matrices. In this case, only the entries of $P_1$ directly depend on $\bf{p}$ so I was thinking of writing:
$$ \frac{d}{d\bf{p}} \left[\left(D_1 +P_1 \right)^{-1}\left(P_1\bf{x} \right) \right] = \left(D_1 +P_1 \right)^{-1} \frac{dP_1}{d\bf{p}} \left(D_1 +P_1 \right)^{-1} \left(P_1\bf{x} \right) + \left(D_1 +P_1 \right)^{-1} \frac{dP_1\bf{x}}{d\bf{p}}. $$
I don't have much confidence that this is correct though. I don't even know if it makes total sense, since $P_1 \bf{x}$ is a vector and $\frac{dP_1}{d\bf{p}}$ is a tensor? Any help is appreciated.
Generally speaking, when derivatives are involved that are encoded as tensors of higher order than matrices, you can no longer write the derivative just in terms of matrix multiplications, but you have to use tensor-contractions/einsums instead. A good resource is the computer algebra tool http://www.matrixcalculus.org/,
which gives us
$$ \frac{\partial \left( \mathrm{inv}(D+P)\cdot P\cdot x \right)}{\partial P} = x^\top \otimes \mathrm{inv}(D+P)-(\mathrm{inv}(D+P)\cdot P\cdot x)^\top \otimes \mathrm{inv}(D+P) $$
So, by chain rule with $P = P(p)$ we have
$$\begin{aligned} \frac{\partial (D+P)^{-1} P x}{\partial P}\frac{\partial P}{\partial p} &= \Big(x^\top \otimes (D+P)^{-1}-((D+P)^{-1} P x)^\top \otimes (D+P)^{-1}\Big)\frac{\partial P}{\partial p} \\&= (D+P)^{-1}\color{red}{\cdot}\frac{\partial P}{\partial p}\color{green}{\cdot}x \;-\; (D+P)^{-1}\color{red}{\cdot}\frac{\partial P}{\partial p}\color{green}{\cdot}(D+P)^{-1} P x \end{aligned}$$
since matrixcalculus.org uses the convention $AXB = (B^\top\otimes A)\cdot X$. Moreover, note that since
$$\begin{aligned} \frac{\partial f(p)}{\partial p} = \bigg(\frac{\partial \big((D+P)^{-1} P x\big)_i}{p_m}\bigg)_{im} = \sum_{jk}\bigg(\frac{\partial \big((D+P)^{-1} P x\big)_i}{\partial P_{jk}}\bigg)_{i,jk} \bigg(\frac{\partial P_{jk}}{\partial p_m}\bigg)_{jk,m} \end{aligned}$$
In the above,
$$ A\color{red}{\cdot}\frac{\partial P}{\partial p}\color{green}{\cdot}v = \Bigg( \sum_{jk} A_{ij}\frac{\partial P_{jk}}{\partial p_m}v_k\Bigg) _{im} $$