Welcome, guest | Sign In | My Account | Store | Cart

Riemann Zeta Function Graph

Python, 56 lines
 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56``` ```# Riemann Zeta Function Graph # http://en.wikipedia.org/wiki/Riemann_zeta_function # FB - 20130721 import math from PIL import Image, ImageDraw imgx = 512; imgy = 512 image = Image.new("RGB", (imgx, imgy)) draw = ImageDraw.Draw(image) pixels = image.load() maxIt = 256 xa = -40.0; xb = 40.0 ya = -40.0; yb = 40.0 def f(z): t = 0.0 for i in range(1, maxIt): t += 1.0 / i ** z return t print "Finding max values of x, y, z..." maxAbsX = 0.0; maxAbsY = 0.0; maxAbsZ = 0.0 percent = 0 for ky in range(imgy): pc = 100 * ky / (imgy - 1) if pc > percent: percent = pc; print '%' + str(percent) y0 = ya + (yb - ya) * ky / (imgy - 1) for kx in range(imgx): x0 = xa + (xb - xa) * kx / (imgx - 1) z = f(complex(x0, y0)) if abs(z.real) > maxAbsX: maxAbsX = abs(z.real) if abs(z.imag) > maxAbsY: maxAbsY = abs(z.imag) if abs(z) > maxAbsZ: maxAbsZ = abs(z) print "Drawing..." percent = 0 for ky in range(imgy): pc = 100 * ky / (imgy - 1) if pc > percent: percent = pc; print '%' + str(percent) y0 = ya + (yb - ya) * ky / (imgy - 1) for kx in range(imgx): x0 = xa + (xb - xa) * kx / (imgx - 1) z = f(complex(x0, y0)) v0 = int(255 * abs(z.real) / maxAbsX) v1 = int(255 * abs(z.imag) / maxAbsY) v2 = int(255 * abs(z) / maxAbsZ) v3 = int(255 * abs(math.atan2(z.imag, z.real)) / math.pi) v = v3 * 256 ** 3 + v2 * 256 ** 2 + v1 * 256 + v0 colorRGB = int(16777215 * v / 256 ** 4) red = int(colorRGB / 65536) grn = int(colorRGB / 256) % 256 blu = colorRGB % 256 pixels[kx, ky] = (red, grn, blu) st = str(xa) + " <= x <= " + str(xb) + " and " + str(ya) + " <= y <= " + str(yb) draw.text((0, 0), st, (255, 0, 0)) image.save("RiemannZetaFunctionGraph.png", "PNG") ```

#### 1 comment

FB36 (author) 9 years, 7 months ago

This one looks like a fractal:

``````# Riemann Zeta Function Graph
# http://en.wikipedia.org/wiki/Riemann_zeta_function
# FB - 20130819
import math
from PIL import Image, ImageDraw
imgx = 512; imgy = 512
image = Image.new("RGB", (imgx, imgy))
draw = ImageDraw.Draw(image)
pixels = image.load()
maxIt = 256
xa = -10.0; xb = 10.0
ya = -10.0; yb = 10.0

def f(z):
t = 0.0
for i in range(1, maxIt):
t += 1.0 / i ** z
return t

percent = 0
for ky in range(imgy):
pc = 100 * ky / (imgy - 1)
if pc > percent: percent = pc; print '%' + str(percent)
y0 = ya + (yb - ya) * ky / (imgy - 1)
for kx in range(imgx):
x0 = xa + (xb - xa) * kx / (imgx - 1)
z = f(complex(x0, y0))
v0 = int(255 * abs(z)) % 256
v1 = int(255 * abs(math.atan2(z.imag, z.real)) / math.pi) % 256
v2 = int(255 * abs(z.real)) % 256
v3 = int(255 * abs(z.imag)) % 256
v = v3 * 256 ** 3 + v2 * 256 ** 2 + v1 * 256 + v0
colorRGB = int(16777215 * v / 256 ** 4)
red = int(colorRGB / 65536)
grn = int(colorRGB / 256) % 256
blu = colorRGB % 256
pixels[kx, ky] = (red, grn, blu)

st = str(xa) + " <= x <= " + str(xb) + " and " + str(ya) + " <= y <= " + str(yb)
draw.text((0, 0), st, (255, 0, 0))
image.save("RiemannZetaFunctionGraph.png", "PNG")
``````
 Created by FB36 on Sun, 21 Jul 2013 (MIT)