Blog

A blog of Python-related topics and code.

Floyd-Steinberg Dithering

Floyd-Steinberg dithering is a technique for reducing the colour palette of an image (for example, to reduce its file size) whilst keeping as much of the perceived detail as possible. For each pixel in the original image, the nearest colour to that pixel is chosen from a restricted palette and any "error" (difference in pixel colour value, original - new) is distributed across the neighbouring pixels as follows:

Mass Parabolas

An atomic nucleus consists of protons and neutrons (collectively referred to as nucleons) bound together through the strong nuclear force. Models for the nuclear binding energy were introduced in a couple of previous posts on this blog.

The Dottie number

The Dottie number is the (real) root of the equation $\cos x = x$. As the only fixed point of the cosine function, it is the number that is converged to by the iterated function sequence $$ x, \cos(x), \cos(\cos(x)), \cos(\cos(\cos(x))), \ldots, $$ i.e. it is the number returned if you mash the COS button on your calculator enough (apparently the French professor after whom it is named did just this). Its value is about 0.7390851 and it is transcendental.

Direct linear least squares fitting of an ellipse

Fitting a set of data points in the $xy$ plane to an ellipse is a suprisingly common problem in image recognition and analysis. In principle, the problem is one that is open to a linear least squares solution, since the general equation of any conic section can be written $$ F(x, y) = ax^2 + bxy + cy^2 + dx + ey + f = 0, $$ which is linear in its parameters $a$, $b$, $c$, $d$, $e$ and $f$. The polynomial $F(x,y)$ is called the algebraic distance of any point $(x, y)$ from the conic (and is zero if $(x, y)$ happens to lie on the conic).

Chaotic Balls

Inspired by this recent Numberphile video, here is a demonstration of chaos in a simple dynamical system: two balls, with near-identical starting conditions, bounce around elastically off a circular wall. After a short time, the balls' trajectories diverge completely.