-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmobius.C
317 lines (254 loc) · 5.92 KB
/
mobius.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
/*
* mobius.C
*
* FUNCTION:
* display mobius mu, Euler's totient, and other number-theoretic
* functions on the complex disk (poincare disk).
*
* HISTORY:
* quick hack -- Linas Vepstas October 1989
* modernize -- Linas Vepstas March 1996
* more stuff -- January 2000
* more stuff -- October 2004
*/
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include "brat.h"
#include "moebius.h"
#include "totient.h"
int thue_morse(int n)
{
if (0 == n) return 0;
if (1 == n) return 1;
if (0 == n%2) return thue_morse (n/2);
return (1-thue_morse ((n-1)/2));
}
/*
* Compute the divisor arithmetic function
*/
int divisor (int n)
{
int acc = 0;
int d;
for (d=1; d<=n; d++)
{
if (n%d) continue;
acc ++;
}
return acc;
}
double randoid(int n)
{
#define NVAL 65186
static double array[NVAL];
static int inited=0;
if (!inited)
{
int i;
inited = 1;
srand (99);
for (i=0; i<NVAL; i++)
{
array[i] = (rand()>>6) & 0x1;
// array[i] = ((double) rand()) / ((double)RAND_MAX);
}
}
if (NVAL<= n) return 0;
return array[n];
}
static int max_terms;
/* Perform ordinary series sum over one of the funcs */
void plain_series_c (double re_q, double im_q, double *prep, double *pimp)
{
double tmp;
int i;
*prep = 0.0;
*pimp = 0.0;
double rep = 0.0;
double imp = 0.0;
double qpr = 1.0;
double qpi = 0.0;
double qpmod = re_q*re_q+im_q*im_q;
if (1.0 <= qpmod) return;
for (i=0; i<max_terms; i++)
{
// double t = moebius_mu (i+1);
// double t = mertens_m (i+1);
// double t = liouville_omega (i+1);
// double t = liouville_lambda (i+1);
// double t = mangoldt_lambda (i+1);
// double t = thue_morse (i+1);
// int tm = thue_morse (i+1);
// double t = 1.0;
// if (1 == tm) t = -1.0;
// double t = moebius_mu (i+1);
double t = randoid (i+1);
#if 0
t *= (i+1);
t *= (i+1);
t *= (i+1);
#endif
t *= (i+1);
t *= (i+1);
t *= (i+1);
t *= (i+1);
t *= (i+1);
t *= (i+1);
t *= (i+1);
t *= (i+1);
t *= (i+1);
rep += qpr *t;
imp += qpi *t;
/* compute q^k */
tmp = qpr*re_q - qpi * im_q;
qpi = qpr*im_q + qpi * re_q;
qpr = tmp;
qpmod = qpr*qpr + qpi*qpi;
if (qpmod < 1.0e-30) break;
}
if (max_terms-1 < i)
{
// printf ("not converged re=%g im=%g modulus=%g\n", re_q, im_q, qpmod);
}
*prep = rep;
*pimp = imp;
}
/* derivatives */
struct deriv_s
{
double reh;
double imh;
double rehp;
double imhp;
double rehpp;
double imhpp;
};
static void derivatives (double re_q, double im_q, struct deriv_s *dd)
{
double tmp;
int i;
double reh = 0.0;
double imh = 0.0;
double rehp = 0.0;
double imhp = 0.0;
double rehpp = 0.0;
double imhpp = 0.0;
double qpr = 1.0;
double qpi = 0.0;
double qprm1 = 0.0;
double qpim1 = 0.0;
double qprm2 = 0.0;
double qpim2 = 0.0;
double qpmod = re_q*re_q+im_q*im_q;
if (1.0 <= qpmod) return;
for (i=0; i<max_terms; i++)
{
// double t = moebius_mu (i+1);
// double t = mertens_m (i+1);
// double t = liouville_omega (i+1);
// double t = liouville_lambda (i+1);
// double t = mangoldt_lambda (i+1);
// double t = thue_morse (i+1);
// int tm = thue_morse (i+1);
// double t = 1.0;
// if (1 == tm) t = -1.0;
// double t = moebius_mu (i+1);
double t = totient_phi (i+1);
// double t = randoid (i+1);
#if 0
t *= (i+1);
t *= (i+1);
t *= (i+1);
#endif
double eye = i;
reh += t*qpr;
imh += t*qpi;
rehp += eye*qprm1*t;
imhp += eye*qpim1*t;
rehpp += eye*(eye-1.0)*qprm2*t;
imhpp += eye*(eye-1.0)*qpim2*t;
/* save lower derives */
qprm2 = qprm1;
qpim2 = qpim1;
qprm1 = qpr;
qpim1 = qpi;
/* compute q^k */
tmp = qpr*re_q - qpi * im_q;
qpi = qpr*im_q + qpi * re_q;
qpr = tmp;
qpmod = qpr*qpr + qpi*qpi;
if (qpmod < 1.0e-30) break;
}
if (max_terms-1 < i)
{
// printf ("not converged re=%g im=%g modulus=%g\n", re_q, im_q, qpmod);
}
dd->rehpp = rehpp;
dd->imhpp = imhpp;
dd->rehp = rehp;
dd->imhp = imhp;
dd->reh = reh;
dd->imh = imh;
}
/* Computes curvature of geodesics, i.e. curvature of field lines (rays)
* and of equipotentials. Curvature of field line returned in second,
* cuvature of equipotential in first. */
void line_curvature_c (double re_q, double im_q, double *pequi, double *pfie)
{
*pequi = 0.0;
*pfie = 0.0;
struct deriv_s dd;
derivatives (re_q, im_q, &dd);
double norm = pow (dd.rehp*dd.rehp+dd.imhp*dd.imhp, 1.5);
double equipot = - dd.rehpp*(dd.rehp*dd.rehp - dd.imhp*dd.imhp) - 2.0*dd.rehp*dd.imhp*dd.imhpp;
equipot /= norm;
double ray = dd.imhpp*(dd.rehp*dd.rehp - dd.imhp*dd.imhp) - 2.0*dd.rehp*dd.imhp*dd.rehpp;
ray /= norm;
*pequi = equipot;
*pfie = ray;
}
/* Computes scalar curvature of surface (contraction of ricci curvature)
*/
void scalar_c (double re_q, double im_q, double *pcurv, double *pxxx)
{
struct deriv_s dd;
derivatives (re_q, im_q, &dd);
double deno = 1.0+dd.rehp*dd.rehp+dd.imhp*dd.imhp;
deno *= deno;
double numer = - dd.rehpp*dd.rehpp - dd.imhpp*dd.imhpp;
double curvature = -2.0*numer / deno;
*pcurv = curvature;
*pxxx = 0.0;
}
/* Someday,this is going to Compute components of energy-momentum tensor
(actually, just the mass) right now its a test function.
*/
void energy_c (double re_q, double im_q, double *energy, double *moment)
{
struct deriv_s dd;
derivatives (re_q, im_q, &dd);
double gxx = 1.0+dd.rehp*dd.rehp;
double gxy = -dd.rehp*dd.imhp;
double gyy = 1.0+dd.imhp*dd.imhp;
double deno = 1.0+dd.rehp*dd.rehp+dd.imhp*dd.imhp;
double flub = dd.imhp*dd.imhp * dd.rehp*dd.rehp;
flub /= deno;
*energy = flub;
}
static double
mobius_series (double re_q, double im_q, int itermax, double param)
{
double rep, imp;
max_terms = itermax;
// plain_series_c (re_q, im_q, &rep, &imp);
// line_curvature_c (re_q, im_q, &rep, &imp);
scalar_c (re_q, im_q, &rep, &imp);
// return sqrt (rep*rep+imp*imp);
return rep;
// return rep*imp;
// return imp;
// return (atan2 (imp,rep)+M_PI)/(2.0*M_PI);
}
DECL_MAKE_HEIGHT(mobius_series)
/* --------------------------- END OF LIFE ------------------------- */