Code Snippets

Utilities so that I don't have to scour the internet always.


Vectorized Pairwise Distances

For XR...×m×d,YR...×n×d\mathbf{X} \in \mathbb{R}^{... \times m \times d}, \mathbf{Y} \in \mathbb{R}^{... \times n \times d}, the pairwise distance matrix between each pair of these batched matrices is DR...×m×n\mathbf{D} \in \mathbb{R}^{... \times m \times n}, where ... represent arbitrary batch dimension (think batches of pairs of mm and nn samples of dimension dd).

def pairwise_dist(x, y):
xx = (x * x).sum(dim=-1).unsqueeze(-1)
yy = (y * y).sum(dim=-1).unsqueeze(-2)
xy = torch.einsum('...ji,>...jk', x, y)
d = xx + yy - 2. * xy
return d


Localize Settings

Just wrap everything inside


Table Merge Columns

% <column size> (2), <type> (c), <content>

Table Column Spacing


Wrapping figure around text

% "r" for right, "l" for left

Split long equations overflowing page width

Inside align environments, use split. {}& for alignment and the usual new line \\ for split points.

a = b + c \\
z^2 = {}&x^2 + \\{}&y^2
\end{split} \\