Skip to content

Instantly share code, notes, and snippets.

@Strilanc
Created August 31, 2016 17:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Strilanc/f2263a74317e5a1cdfb25c58c18d0617 to your computer and use it in GitHub Desktop.
Save Strilanc/f2263a74317e5a1cdfb25c58c18d0617 to your computer and use it in GitHub Desktop.
Renders colorful density plots of the roots of polynomials with coefficients drawn from a small set of sets.
import numpy as np
import cv2
import itertools
import math
import cmath
w = h = 1024
deg = 11
# The root phases to sample frame, and the color that goes with each.
n = 60
col_samp = [(
cmath.exp(1j*math.pi*k/n),
np.array([math.cos(k*math.pi/n*2), math.cos(k*math.pi/n*2+1),math.cos(k*math.pi/n*2+2)])) for k in range(n+1)[1:]]
# Iterate over all polynomials with coefficients limited to 1 or root phase up to a desired degree.
# Dump a density plot of their roots into dat, with colors.
dat = np.array([[[0.0, 0.0, 0.0] for i in range(w)] for j in range(h)])
for p, c in col_samp:
print(p)
for coefs in itertools.product([1, p], repeat=deg):
for r in np.roots([1] + list(coefs)):
x = math.floor(r.real * (w/5.0) + (w/2))
y = math.floor(r.imag * (h/5.0) + (h/2))
if x >= 0 and x < w and y >= 0 and y < h:
dat[y, x, :] += c
# Temp copy for easier re-processing when in the REPL
dat2 = dat*1.0
dat = dat2*1.0
dat = np.abs(dat)
dat *= 25
dat = ((dat > 50)*(math.log(dat/50.0) + 50.0)) + (dat <= 50)*dat
img = np.array(dat, np.uint8)
cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment