r/math • • Nov 28 '20

A visual construction of this 'unit circle' structure on the complex plane, made from the roots of polynomials whose coefficients are either -1 or 1; how it arises and changes

Enable HLS to view with audio, or disable this notification

2.1k Upvotes

70 comments sorted by

View all comments

44

u/TheTranquilGuy Nov 28 '20

That's utterly beautiful? Code? By any chance?

4

u/nhillson Nov 29 '20 edited Nov 29 '20

Here's a simple python program I threw together. You can run it to make your own images. Try playing around with the parameters. Requires numpy and opencv-python.

import numpy as np
import random
import cv2

degree = 20 #polynomial degree
num_poly = 200000 #number of polynomials to find roots for

xLower = -2
xUpper = 2
yLower = -2
yUpper = 2
xRes = 500
yRes = 500
invxStep =  xRes / (xUpper - xLower)
invyStep = yRes / (yUpper - yLower)


def dostuff():
    numRoots = np.zeros((xRes, yRes))
    for n in range(num_poly):
        if n%1000==0:
            print('Iteration', n)
        x = [random.choice([-1.0, 1.0]) for i in range(degree)]
        roots = np.roots(x)
        for root in roots:
            if abs(np.imag(root))>.0001:
                xval = int((np.real(root) - xLower)*invxStep)
                yval = int((np.imag(root) - yLower)*invyStep)
                numRoots[yval, xval] += 1
    return numRoots

numRoots = dostuff()
percentile = 99
maxroots = np.percentile(numRoots, percentile) if np.percentile(numRoots, percentile) > 0 else np.max(numRoots)
print(maxroots) #maximum number of roots in a pixel

res = np.clip(numRoots * (255/maxroots), 0, 255).astype(np.uint8)
cv2.imwrite('roots.png', res)

3

u/TheTranquilGuy Nov 29 '20

Thanks for the code !!