The other answer tells us why we don't usually see the $-p_i+q_i$ term: $p$ and $q$ are usually residents of the simplex and so sum to one, so this leads to $\sum - [p_i - q_i] = \sum - p_i + \sum q_i = -1 + 1 = 0$.
In this answer, I want to show why those terms are there in the first place, by viewing KL divergence as the Bregman divergence induced by the (negative) Entropy function.
Given some differentiable function $\psi$, the Bregman divergence induced by it is a binary function on the domain of $\psi$:
$$
B_\psi(p,q) = \psi(p)-\psi(q)-\langle\nabla\psi(q),p-q\rangle
$$
Intuitively, the Bregman divergence measures the difference between $\psi$ evaluated at $p$ and the linear approximation to $\psi$ (about $q$) evaluated at $p$. When $\psi$ is convex, this is guaranteed to be nonnegative, and thus so is the Bregman divergence.
Noting that if $\psi(p) = \sum_i p_i \log p_i$, $\nabla\psi(p) = [\log p_i + 1]$, the entropic Bregman divergence is thus:
$$
B_e(p,q) = \sum_i p_i \log p_i - \sum_i q_i \log q_i - \sum_i [\log q_i + 1][p_i-q_i]\\
= \sum_i p_i \log p_i - \sum_i q_i \log q_i - \sum_i [\log q_i (p_i-q_i) + p_i-q_i]\\
= \sum_i p_i \log p_i - \sum_i q_i \log q_i - \sum_i p_i \log q_i + \sum_i q_i\log q_i - \sum_i[p_i-q_i]\\
= \sum_i p_i \log p_i - \sum_i p_i \log q_i - \sum_i[p_i-q_i]\\
= \sum_i p_i \log \frac{p_i}{q_i} + \sum_i[-p_i+q_i]
$$
which we recognize as the KL divergence you mentioned.
rel_entr()
(relative entropy), linked from the above one, relies on the common definition of the KL-divergence for discrete distributions. I'd like to know how and why the additional terms appear. $\endgroup$scipy
$\endgroup$scipy
isn't the only place you'll see this definition, eg the cited reference uses it too, so this Q will be useful to non-scipy users as well.) Feel free to revert my changes or make further ones if you think it will be useful! $\endgroup$