-
Notifications
You must be signed in to change notification settings - Fork 0
/
swap.C
420 lines (345 loc) · 8.81 KB
/
swap.C
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*
* swap.C
*
* FUNCTION:
* Integral of the permuation group of continued fractions
* Integral of mobius transform having pole on x-axis
* Expect to get Riemann zeta in the Gauss map case
* and that is what we seem to get ... need high integration
* order though to get anything on the r=1/2 axis ...
*
* Results written up in yarh.lyx
*
* Linas Feb 2005
* Linas Dec 2010
* Linas Oct 2015
*/
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include "brat.h"
inline long double swap12 (long double x)
{
long double ox = 1.0L/x;
long double a1 = floorl(ox);
long double r1 = ox - a1;
/* If 1/x-a_1 is zero, this means a2 is infinity.
* So exchanging a1 and a2 gives y=1/infty = 0 */
if (1.0e-10 > r1) return r1;
/* Sometimes, rounding errors above give 0.99999...
* Deal with these properly, as if above was a zero. */
if ((1.0 - r1) < 1.0e-10) return 1.0 - r1;
ox = 1.0L/r1;
long double a2 = floorl(ox);
long double r2 = ox - a2;
r2 += a1;
r1 = 1.0L / r2;
r1 += a2;
x = 1.0L / r1;
return x;
}
inline long double swap13 (long double x)
{
long double ox = 1.0L/x;
long double a1 = floorl(ox);
long double r1 = ox - a1;
if (1.0e-10 > r1) return 0.69314718; // "average" value
ox = 1.0L/r1;
long double a2 = floorl(ox);
long double r2 = ox - a2;
if (1.0e-10 > r2) return r2;
ox = 1.0L/r2;
long double a3 = floorl(ox);
long double r3 = ox - a3;
r3 += a1;
r2 = 1.0L / r3;
r2 += a2;
r1 = 1.0L / r2;
r1 += a3;
x = 1.0L / r1;
return x;
}
inline long double swap23 (long double x)
{
long double ox = 1.0L/x;
long double a1 = floorl(ox);
long double r1 = ox - a1;
if (1.0e-10 > r1) return 0.0;
ox = 1.0L/r1;
long double a2 = floorl(ox);
long double r2 = ox - a2;
if (1.0e-10 > r2) return 0.0;
ox = 1.0L/r2;
long double a3 = floorl(ox);
long double r3 = ox - a3;
r3 += a2;
r2 = 1.0L / r3;
r2 += a3;
r1 = 1.0L / r2;
r1 += a1;
x = 1.0L / r1;
return x;
}
inline long double swap14 (long double x)
{
long double ox = 1.0L/x;
long double a1 = floorl(ox);
long double r1 = ox - a1;
if (1.0e-10 > r1) return 0.0;
ox = 1.0L/r1;
long double a2 = floorl(ox);
long double r2 = ox - a2;
if (1.0e-10 > r2) return 0.0;
ox = 1.0L/r2;
long double a3 = floorl(ox);
long double r3 = ox - a3;
if (1.0e-10 > r3) return 0.0;
ox = 1.0L/r3;
long double a4 = floorl(ox);
long double r4 = ox - a4;
r4 += a1;
r3 = 1.0L / r4;
r3 += a3;
r2 = 1.0L / r3;
r2 += a2;
r1 = 1.0L / r2;
r1 += a4;
x = 1.0L / r1;
return x;
}
inline long double swap_mob (long double x)
{
long double ox = 1.0L/x;
long double a1 = floorl(ox);
long double r1 = ox - a1;
/* If 1/x-a_1 is zero, this means a2 is infinity.
* So exchanging a1 and a2 gives y=1/infty = 0 */
if (1.0e-10 > r1) return r1;
/* Sometimes, rounding errors above give 0.99999...
* Deal with these properly, as if above was a zero. */
if ((1.0 - r1) < 1.0e-10) return 1.0 - r1;
ox = 1.0L/r1;
long double a2 = floorl(ox);
long double r2 = ox - a2;
/* Apply a Mobius xform. For now, just apply T */
long double b1 = a1 + a2;
long double b2 = a2;
r2 += b2;
r1 = 1.0L / r2;
r1 += b1;
x = 1.0L / r1;
return x;
}
inline long double swap12_lin_mix (long double x)
{
long double ox = 1.0L/x;
long double a1 = floorl(ox);
long double r1 = ox - a1;
/* If 1/x-a_1 is zero, this means a2 is infinity.
* So exchanging a1 and a2 gives y=1/infty = 0 */
if (1.0e-10 > r1) return r1;
/* Sometimes, rounding errors above give 0.99999...
* Deal with these properly, as if above was a zero. */
if ((1.0 - r1) < 1.0e-10) return 1.0 - r1;
ox = 1.0L/r1;
long double a2 = floorl(ox);
long double r2 = ox - a2;
// long double frac = 1.0L / 3.0L;
#if 0
long double tmp = frac*a1 + (1.0L-frac)*a2;
a2 = frac*a2 + (1.0L-frac)*a1;
a1 = tmp;
#endif
#if 0
long double tmp = frac*frac*a1*a1 + (1.0L-frac)*(1.0L-frac)*a2*a2;
a2 = frac*frac*a2*a2 + (1.0L-frac)*(1.0L-frac)*a1*a1;
a2 = sqrtl(a2);
a1 = sqrtl(tmp);
#endif
long double tmp = sqrtl(a1*a2);
a2 = tmp;
a1 = tmp;
r2 += a2;
r1 = 1.0L / r2;
r1 += a1;
x = 1.0L / r1;
return x;
}
inline long double mobiux (long double x)
{
// xform is (ax+b)/(cx+d) having pole at 0 <= x=-d/c <= 1
int a = 1;
int b = 0;
int c = -2;
int d = 1;
long double ox = (a*x +b) / (c*x+d);
long double a1 = floorl(ox);
long double r1 = ox - a1;
return r1;
}
/* The integrand, which is swap(x) * x^s */
void grand (long double x, long double sre, long double sim,
long double *pre, long double *pim)
{
#if 0
// This is the basic case, which gives Riemann exactly
long double ox = 1.0L/x;
long double sw = ox - floorl(ox);
#else
// long double sw = swap12 (x);
// long double sw = swap13 (x);
// long double sw = swap23 (x);
// This is used for the sanity-check, "shadow" graph
// long double sw = x;
// The generalized mobius hypothesis
// long double sw = swap_mob (x);
// long double sw = swap12_lin_mix (x);
long double sw = mobiux (x);
#endif
long double lnx = logl (x);
long double ire = expl (sre*lnx);
// long double ire = 1.0L/ sqrtl (x);
long double iim = ire;
long double phi = sim*lnx;
ire *= cosl (phi);
iim *= sinl (phi);
ire *= sw;
iim *= sw;
*pre = ire;
*pim = iim;
}
/* Compute single integral of the integrand.
* actually compute
* zeta = s/(s-1) - s \int_0^1 swap(x) x^{s-1} dx
*/
void gral(int nsteps, long double sre, long double sim,
long double *pre, long double *pim)
{
int i;
long double step = 1.0L / ((long double) nsteps);
long double sum_re= 0.0L;
long double sum_im= 0.0L;
long double x = 1.0L - 0.5*step;
long double r = RAND_MAX;
r = 1.0L / r;
/* integrate in a simple fashion */
int nh = 0;
for (i=0; i<nsteps; i++)
{
// #define DO_RAND
#ifdef DO_RAND
int nr = rand();
if (0 == nr) continue;
x = (long double) nr;
x *= r;
#endif
long double val_re, val_im;
grand (x, sre-1.0, sim, &val_re, &val_im);
x -= step;
sum_re += val_re;
sum_im += val_im;
nh ++;
}
/* Divide by the actual number of samples */
step = 1.0L / ((long double) nh);
sum_re *= step;
sum_im *= step;
#if 0
// the trivial case
sum_re = sre+1.0;
sum_im = sim;
long double t = sum_re*sum_re + sum_im*sum_im;
sum_re /= t;
sum_im = -sum_im / t;
#endif
/* mult by s */
long double tmp = sum_re * sre - sum_im*sim;
sum_im = sum_im *sre + sum_re *sim;
sum_re = tmp;
/* compute 1/(s-1) */
sre -= 1.0L;
long double v = sre*sre+sim*sim;
sre /= v;
sim = -sim/v;
/* subtract */
sum_re = sre - sum_re;
sum_im = sim - sum_im;
/* s/(s-1) = 1/(s-1) + 1 so add 1 now */
sum_re += 1.0L;
*pre = sum_re;
*pim = sum_im;
}
double
rswap (long double sre, long double sim, int itermax)
{
long double zre, zim;
gral (itermax, sre, sim, &zre, &zim);
long double mag = zre*zre+zim*zim;
mag = sqrt (mag);
return mag;
long double phase = atan2l(zim, zre);
phase /= 2.0L * M_PI;
phase += 0.5L;
return phase;
}
// DECL_MAKE_HEIGHT(rswap)
/*-------------------------------------------------------------------*/
/* This routine fills in the interior of the the convergent area of the
*/
void
MakeHisto (
char *name,
float *glob,
int sizex,
int sizey,
double re_center,
double im_center,
double width,
double height,
int itermax,
double renorm)
{
int i,j, globlen;
double re_start, im_start, delta;
double re_position, im_position;
delta = width / (double) sizex;
re_start = re_center - width / 2.0;
im_start = im_center + width * ((double) sizey) / (2.0 * (double) sizex);
double im_end = im_center - width * ((double) sizey) / (2.0 * (double) sizex);
printf ("re=(%g,%g)\n", re_start, re_start+width);
printf ("im=(%g,%g)\n", im_end, im_start);
globlen = sizex*sizey;
for (i=0; i<globlen; i++) glob [i] = 0.0;
im_position = im_start;
for (i=0; i<sizey; i++)
{
if (i%10==0) { printf(" start row %d\n", i); fflush (stdout); }
re_position = re_start;
for (j=0; j<sizex; j++)
{
double phi = rswap (re_position, im_position, itermax);
glob [i*sizex +j] = phi;
#define NORMAL_CRIT_LINES
#ifdef NORMAL_CRIT_LINES
// draw vertical lines showing crit strip
if ((re_position <= 0.0) && (0.0<re_position+delta))
glob [i*sizex +j] = -1.0;
if ((re_position <= 0.5) && (0.5<re_position+delta))
glob [i*sizex +j] = -1.0;
if ((re_position <= 1.0) && (1.0<re_position+delta))
glob [i*sizex +j] = -1.0;
#else
// draw vertical lines showing crit strip, shifted over by one.
if ((re_position <= -1.0) && (-1.0<re_position+delta))
glob [i*sizex +j] = -1.0;
if ((re_position <= -0.5) && (-0.5<re_position+delta))
glob [i*sizex +j] = -1.0;
if ((re_position <= 0.0) && (0.0<re_position+delta))
glob [i*sizex +j] = -1.0;
#endif
re_position += delta;
}
im_position -= delta; /*top to bottom, not bottom to top */
}
}
/* --------------------------- END OF LIFE ------------------------- */