Can someone explain what is going on in the first two terms? How does it compute pairwise Euclidean distance between rows in matrix $X$?
Is it $X^2 + (X^T)^2 -2XX^T$
In order to calculate the input pairwise similarity, we need to compute the pairwise Euclidean distance matrix $\mathbf D$ first. Using the matrix operations we could compute this matrix efficiently without using loops to do pairwise calculation: $$D = \begin{bmatrix} \vdots & \vdots & \vdots \\ \|\mathbf x_{\mathbf i}\|^2 & \ldots & \|\mathbf x_{\mathbf i}\|^2 \\ \vdots & \vdots & \vdots \end{bmatrix} + \begin{bmatrix} \ldots & \|\mathbf x_{\mathbf i}\|^2 & \ldots \\ \ldots & \vdots & \ldots \\ \ldots & \|\mathbf x_{\mathbf i}\|^2 & \ldots \end{bmatrix} -2 \mathbf X \cdot \mathbf X^{\mathbf T} \text{ where } \mathbf X = \begin{bmatrix} \vdots \\ \mathbf x_{\mathbf i} \\ \vdots \end{bmatrix}$$