// zeta.cpp
// Riemann zeta function

#include <iostream>
#include <fstream>
#include <complex>
#include <cmath>
using namespace std;

typedef complex<double> complx;

const double pi=3.14159265358979323846;

fstream picture;
long n;
double lg[100001];
int inv[256];

complx eta(complx s) {
	long k;
	complx z=0;
	for (k=1; k<=n; k++) {
		if (k&1) z+=exp(-s*lg[k]);
		else z-=exp(-s*lg[k]);
	}
	return z;
}

complx lngamma(complx z) {
	complx p=1;
	complx q;
	complx r;
	while (real(z)<8) {
		p*=z;
		z+=1.0;
	}
	q=1.0/(z*z);
	r=(z-0.5)*log(z)-z;
	r+=0.5*log(2*pi);
	r+=(1.0/12-q*(1.0/360-q/1260.0))/z;
	r-=log(p);
	return r;
}

complx zeta(complx s) {
	complx a;
	if (real(s)<0.5) {
		a=s*log(2.0)+(s-1.0)*log(pi)+lngamma(1.0-s);
		return exp(a)*sin(0.5*pi*s)*zeta(1.0-s);
	}
	return eta(s)/(1.0-exp((1.0-s)*log(2.0)));
}

void pb(int b) {
	char c=(char) b;
	picture.write(&c,1);
}

void pw(int b) {
	pb(b&255);
	pb((b>>8)&255);
}

void pl(long b) {
	pw((int) (b&65535L));
	pw((int) ((b>>16)&65535L));
}

long bmpx(long x) {
	return (x+3)&~3L;
}

void header(long x, long y) {
	pb('B');
	pb('M');
	pl(1078+bmpx(x)*y);
	pl(0);
	pl(1078);
	pl(40);
	pl(x);
	pl(y);
	pw(1);
	pw(8);
	pl(0);
	pl(bmpx(x)*y);
	pl(2835);
	pl(2835);
	pl(256);
	pl(0);
}

void palette() {
	int i;
	double r;
	double g;
	double b;
	for (i=0; i<256; i++) {
		r=0;
		g=0;
		b=0;
		if (i<16) b=i/255.0;
		if (i>15 && i<64) b=(i-12)/52.0;
		if (i>63 && i<97) b=(97-i)/34.0;
		if (i>174) b=(i-175)/45.0;
		if (i>219) b=1;
		if (i>62 && i<97) g=(i-62)/40.0;
		if (i>96 && i<130) g=0.879+(i-97)/270.0;
		if (i>129 && i<175) g=(175-i)/45.0;
		if (i>215) g=(i-215)/40.0;
		if (i>100) r=(i-100)/30.0;
		if (i>129) r=1;
		inv[i]=255;
		if (0.299*r+0.587*g+0.114*b>0.5) inv[i]=0;
		pb((int) (0.1+255.8*b));
		pb((int) (0.1+255.8*g));
		pb((int) (0.1+255.8*r));
		pb(0);
	}
}

int color(complx z) {
	double r=abs(z);
	double f;
	if (!(r<1e300)) return 255;
	if (r<1) f=128*r;
	else f=128+2.4*log(r);
	if (f>255) f=255;
	return (int) f;
}

int line(double u, long i, long w) {
	long q=(long) floor(u-0.5*w+0.5);
	return (i>=q && i<q+w);
}

int axes(long i, long j, double p, double u0, double v0) {
	long w=(long) (0.04*p+0.5);
	long t=(long) (0.125*p+0.5);
	long t1=t;
	long t2=t;
	long k1=(long) floor((i+0.5-u0)/p+0.5);
	long k2=(long) floor((j+0.5-v0)/p+0.5);
	if (w<1) w=1;
	if (k1%5==0) t1=(long) (0.275*p+0.5);
	if (k2%5==0) t2=(long) (0.275*p+0.5);
	if (line(u0,i,w) || line(v0,j,w)) return 1;
	if (line(u0+k1*p,i,w) && line(v0,j,w+2*t1)) return 1;
	if (line(v0+k2*p,j,w) && line(u0,i,w+2*t2)) return 1;
	return 0;
}

int main() {
	long x;
	long y;
	long i;
	long j;
	long k;
	int f;
	double prz=0;
	double m=24;
	double h=84;
	double re=-8;
	double im=38;
	double c1;
	double c2;
	double p;
	double u0;
	double v0;
	cout << "\nresolution x (e.g., 960) : ";
	cin >> x;
	cout << "number of terms (e.g., 10000) : ";
	cin >> n;
	if (x<1 || n<1 || n>100000) return 1;
	y=(long) ((x*h)/m+0.5);
	p=x/m;
	u0=0.5*x-re*p;
	v0=0.5*y-im*p;
	cout << "creating zeta.bmp (" << x << " x " << y << ")\n";
	picture.open("zeta.bmp", ios::out|ios::binary);
	header(x,y);
	cout << "writing palette\n";
	palette();
	for (k=1; k<=n; k++) lg[k]=log((double) k);
	cout << "calculating picture\n";
	cout.setf(ios::fixed);
	cout.precision(1);
	for (j=0; j<y; j++) {
		c2=im+(m*(2*j+1-y))/(2*x);
		for (i=0; i<x; i++) {
			c1=re+(m*(2*i+1-x))/(2*x);
			f=color(zeta(complx(c1,c2)));
			if (axes(i,j,p,u0,v0)) f=inv[f];
			pb(f);
		}
		for (k=x; k<bmpx(x); k++) pb(0);
		prz=(100.0*(j+1))/y;
		cout << "\r[" << prz << "%]" << flush;
	}
	picture.close();
	cout << "\nready. (mhebel)\n";
	return 0;
}
Betrag der Riemannschen Zeta-Funktion in der komplexen Ebene, mit Achsen; dunkle Stellen sind Nullstellen
Ergebnis des Programms: 960 × 3360 Pixel, 10000 Terme

   zurück