Blog

A blog of Python-related topics and code.

Cropping a square region from an image

For a basic logistic regression image classification exercise, it is usually desirable for the training and test images to have the same dimensions and images are often chosen to be square. However, many datasets provide images with different sizes and aspect ratios for more advanced classification algorithms.

The Maxwell–Boltzmann distribution in two dimensions

This script demonstrates the relaxation of an ensemble of colliding particles towards the equilibrium, Maxwell-Boltzmann distribution of their speeds. Unlike this previous post, the collision detection and dynamics are handled using NumPy arrays without explicit python loops (except over collision pairs), which improves the performance greatly.

Visualizing the real forms of the spherical harmonics

The spherical harmonics are a set of special functions defined on the surface of a sphere that originate in the solution to Laplace's equation, $\nabla^2f=0$. Because they are basis functions for irreducible representations of SO(3), the group of rotations in three dimensions, they appear in many scientific domains, in particular as the angular part of the wavefunctions of atoms (atomic orbitals). They are described in terms of an integer degree $l=0,1,2,\ldots$ and order $m=-l,-l+1,\ldots,l$.

Ratios of random numbers

The last episode of 3Blue1Brown's "lockdown math" series posed the problem: given two numbers, $x$ and $y$, chosen randomly from the uniform distribution between 0 and 1, what is the proportion that their ratio, when rounded down to the nearest integer, is even? That is, what is $P(\lfloor \frac{x}{y} \rfloor \mod 2 = 0)$?

The Babylonian spiral

A Babyonian spiral (OEIS A256111) is the figure formed by starting with a zero-vector at the origin and concatenating vectors such that each subsequent vector is the next one longer than the previous one that also lands on a position with integral Cartesian coordinates. That is, the $i$th vector has integer components $x_i$ and $y_i$ satisfying, $x_i^2 + y_i^2 = n_i^2 > n_{i-1}^2$. Each vector is chosen such that it minimizes the angular separation from the previous one.