Differentiate w.r.t a matrix If I define $S=H+ae^T/n$, where $a_i=1$ if $h_{ij}=0$ for all $j$,  $a_i=0$ otherwise and $e$ is a column of 1's. 
How to do the differentiation w.r.t $h_{ij}$? I know $a$ is somehow related to $h_{ij}$ but I am not sure how to deal with that. 
 A: Based on your comment, I'm guessing that what you have is this:


*

*$H$ is an $n \times n$ matrix with non-negative entries such that all rows sum to either $1$ (if the row has any non-zero entry) or $0$ (if the entire row is $0$).

*Let $e$ denote the column vector consisting of all ones, and let $I$ be the $n \times n$ identity matrix. Then 
$$
a = e - He = (I - H)e
$$

*$ee^T$ is an $n\times n$ matrix consisting of all ones, call this $M$, so
$$
ae^T = (I-H)ee^T = (I-H)M
$$

*So treating $S$ as a function of $H$,
$$
S(H) = H + \frac{1}{n}(I-H)M.
$$


One could work out $\frac{dS_{ij}}{dH_{kl}}$ from this, but looking at the form of $S$, it would seem like the derivative should have a nice form like $I - \frac{1}{n}M^T$. It turns out this is indeed the case, in the following sense:
Let $\widetilde{A}$ be the "flattening" of $A$, so for example
$$
\widetilde{\begin{pmatrix}a & b \\ c& d\end{pmatrix}} = \begin{pmatrix} a\\ b \\ c \\d \end{pmatrix}.
$$
then
$$
\begin{align}
\frac{d\widetilde{H}}{d\widetilde{H}} &= I \otimes I, \\
\frac{d\widetilde{HM}}{d\widetilde{H}} &= I \otimes M^T,
\end{align}
$$
so
$$
\begin{align}
\frac{d\widetilde{S}}{d\widetilde{H}} &= I \otimes I - \frac{1}{n} I \otimes M^T \\ 
&= I \otimes (I - \frac{1}{n}M^T).
\end{align}
$$
A: I guess you are wondering about the derivative of the PageRank vector regarding changes to the $n\times n$ matrix $H$. Notice that these changes regard only link addition/removal from node $i$ to node $j$. 
The general formula is: 
$$S = H + a^T \cdot v,$$
where $v$ is a personalized $1\times n$ stochastic  vector. In your case, it is $v = e/n$.
As Wong mentioned, we can say that $$a^T \begin{array}[t]{l}= (I - H) \cdot e^T\implies a^T \cdot v = (I-H) \cdot  e^T\cdot v 
\end{array}$$
We know that $$\pi = (1-\alpha)\cdot v\cdot(I-\alpha S)^{-1} $$
Thus, we have after some steps:
$$\dfrac{d\pi}{dh_{ij}}(h_{ij}) \begin{array}[t]{l}= (1  - \alpha) \cdot v\left[ (I - \alpha S)^{-1}  \cdot \alpha\left(\dfrac{dH}{dh_{ij}}-\dfrac{d(a^T\cdot v)}{dh_{ij}}\right) \cdot(I- \alpha S)^{-1} \right]\\
= \alpha\pi \left[\left(\dfrac{dH}{dh_{ij}} -\dfrac{dH}{dh_{ij}}e^T\cdot v\right)\cdot (I - \alpha S)^{-1} \right]\\
=\alpha \pi_i \cdot (e_j-v)(I-\alpha S)^{-1},
\end{array}$$
where $e_j$ is a $1\times n$ vector, with all elements equal to zero, but $j-th $ one, which equals to one.
