Skip to content

Instantly share code, notes, and snippets.

@quangnle
Created July 26, 2023 11:59
Show Gist options
  • Save quangnle/42d358b6817280e3df547b212556a502 to your computer and use it in GitHub Desktop.
Save quangnle/42d358b6817280e3df547b212556a502 to your computer and use it in GitHub Desktop.
Riemann's Zeta function polar representation
<!DOCTYPE html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.js"></script>
</head>
<body>
<script>
let t = 0;
let minT = -15; // Minimum value of t (imaginary part)
let maxT = 15; // Maximum value of t (imaginary part)
let resolution = 500; // Number of points to plot
function setup() {
createCanvas(800, 800);
pixelDensity(1);
}
function draw() {
background(255);
drawZetaFunction();
minT = -t;
maxT = t;
resolution = t*100;
t=(t+1)%50;
}
function drawZetaFunction() {
let points = [];
for (let i = 0; i < resolution; i++) {
let t = map(i, 0, resolution, minT, maxT);
let s = new Complex(0.5, t);
let magnitude = zeta(s) * 20;
// Convert polar coordinates to Cartesian coordinates
let x = width / 2 + magnitude * cos(t);
let y = height / 2 + magnitude * sin(t);
// Store the point in the array
points.push(createVector(x, y));
}
// Draw lines connecting the points
stroke(0);
for (let i = 0; i < points.length - 1; i++) {
line(points[i].x, points[i].y, points[i + 1].x, points[i + 1].y);
}
fill("#00f");
ellipse(width/2, height/2,5,5);
ellipse(width/2 + 20, height/2,5,5);
line(width/2 + 10, 0, width/2 + 10, height);
}
class Complex {
constructor(re, im) {
this.re = re;
this.im = im;
}
add(other) {
return new Complex(this.re + other.re, this.im + other.im);
}
multiply(other) {
return new Complex(
this.re * other.re - this.im * other.im,
this.re * other.im + this.im * other.re
);
}
magnitude() {
return sqrt(this.re * this.re + this.im * this.im);
}
power(n) {
let result = new Complex(1, 0);
for (let i = 0; i < n; i++) {
result = result.multiply(this);
}
return result;
}
}
// Zeta function definition
function zeta(s) {
let sum = new Complex(0, 0);
let n = 1;
while (n < 100) { // Change the upper limit for more accuracy
let term = new Complex(1 / pow(n, s.re), 0).multiply(
new Complex(cos(-s.im * log(n)), sin(-s.im * log(n)))
);
sum = sum.add(term);
n++;
}
return sum.magnitude();
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment