-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathallegra.c
434 lines (385 loc) · 10.1 KB
/
allegra.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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/* compile with cc -g -O3 -Wall -Wextra -ffast-math -std=c99 allegra.c -lm -o allegra */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <complex.h>
#undef CALLCOUNT
#define RANGE 40
#define ITERS 30
/* complex number typedefs and assorted effluvia */
typedef struct
{
double re;
double im;
} cx;
static const cx origin = { 0.0, 0.0 };
static const cx ai = { 0.0, 1.0 };
static const cx minusi = {0.0, -1.0};
static const cx rone = {1.0, 0.0};
static const cx rtwo = {2.0, 0.0};
#if 1
static cx add(cx m, cx n)
{
cx out;
out.re = m.re + n.re;
out.im = m.im + n.im;
return out;
}
static cx cdiff(cx m, cx n)
{
cx out;
out.re = m.re - n.re;
out.im = m.im - n.im;
return out;
}
static cx mult(cx m, cx n)
{
cx out;
out.re = m.re*n.re - m.im *n.im;
out.im = m.im*n.re + m.re * n.im;
return out;
}
static cx rmult(double u, cx m)
{
cx out;
out.re = u*m.re;
out.im = u*m.im;
return out;
}
static cx jcon(cx m)
{
cx out;
out.re = m.re;
out.im = -m.im;
return out;
}
static double norm2(cx m)
{
double out;
out = (m.re*m.re + m.im*m.im);
return out;
}
static cx recip(cx m)
{
cx out;
out = rmult(1/norm2(m),jcon(m));
return out;
}
static cx cdiv(cx m, cx v)
{
cx out;
out = mult(m,recip(v));
return out;
}
#else
static cx add(cx m, cx n)
{
complex o = (m.re + m.im * I) + (n.re + n.im * I);
cx out;
out.re = creal(o); out.im = cimag(o);
return out;
}
static cx cdiff(cx m, cx n)
{
complex o = (m.re + m.im * I) - (n.re + n.im * I);
cx out;
out.re = creal(o); out.im = cimag(o);
return out;
}
static cx mult(cx m, cx n)
{
complex o = (m.re + m.im * I) * (n.re + n.im * I);
cx out;
out.re = creal(o); out.im = cimag(o);
return out;
}
static cx rmult(double u, cx m)
{
complex o = (m.re + m.im * I) * u;
cx out;
out.re = creal(o); out.im = cimag(o);
return out;
}
static cx cdiv(cx m, cx n)
{
complex o = (m.re + m.im * I) / (n.re + n.im * I);
cx out;
out.re = creal(o); out.im = cimag(o);
return out;
}
#endif
/* we need to define a raw exponential -- that is something
which takes a complex number and raises it to a complex power, and
I need to do this at the moment without the benefit of libraries
that might slow the computation down */
/* if the imaginary component is zero then all we have to do is
to multiply exp(real)* (cos(imag) + i * sin(imag) -- if imag is
nonzero we have to do some extra fiddling, and we should
do it intelligently -- the less we actually have to deal with
the power series for exp the better, and because of the n^2 term
in the theta function definition, we're not going to need
more than twenty or thirty terms before the difference between the
actual value and the computer representation is small enough
to make no difference to the phase of the complex number being
plotted */
#ifdef CALLCOUNT
static int exp_count, exp_re_count, exp_im_count;
static int pow_count, pow_a_re_count, pow_a_im_count, pow_b_re_count, pow_b_im_count;
#endif
#if 0
/* m.im == 0 is a common case for this function, optimize for it. */
static cx expc(cx m)
{
#ifdef CALLCOUNT
exp_count++;
if (m.re == 0) exp_re_count++;
if (m.im == 0) exp_im_count++;
#endif
cx out;
if (m.im == 0) {
out.re = exp(m.re);
out.im = 0;
} else {
out.re = exp(m.re) * cos(m.im);
out.im = exp(m.re) * sin(m.im);
}
return out;
}
#else
static cx expc(cx m)
{
complex retval = cexp(m.re + m.im * I);
cx out;
out.re = creal(retval); out.im = cimag(retval);
return out;
}
#endif
#if 0
/* bg.re == 0 is a common case for this function, but optimizing for that doesn't
help significantly. */
static cx powc(cx ag, cx bg)
{
#ifdef CALLCOUNT
pow_count++;
if (ag.re == 0) pow_a_re_count++;
if (ag.im == 0) pow_a_im_count++;
if (bg.re == 0) pow_b_re_count++;
if (bg.im == 0) pow_b_im_count++;
#endif
cx out;
cx mesp, frim;
double radius, theta;
/* get the proper polar form of the complex number */
radius = sqrt(ag.re*ag.re + ag.im*ag.im);
theta = atan2(ag.im,ag.re);
/* mesp gives R^(c+di) */
mesp.re = pow(radius,bg.re)*cos(bg.im*log(radius));
mesp.im = pow(radius,bg.re)*sin(bg.im*log(radius));
/* frim gives e^(i theta (c+di)) */
/* now since we already have the machinery
for performing complex exponentiation (just exp), we
can just call that here */
frim.re = -1.0 * bg.im * theta;
frim.im = bg.re * theta;
frim = expc(frim);
out = mult(mesp,frim);
return out;
}
#else
static cx powc(cx ag, cx bg)
{
cx out;
complex retval = cpow(ag.re + ag.im * I, bg.re + bg.im * I);
out.re = creal(retval); out.im = cimag(retval);
return out;
}
#endif
typedef struct {
int red;
int green;
int blue;
} color;
static color hsv2rgb(double hue, double saturation, double value)
{
int xred=0;
int xgreen=0;
int xblue=0;
int lexen;
double nexel;
double M;
double N;
double K;
color out;
if(saturation == 0)
{
// achromatic case
xred = (int) (255.0 * value);
xgreen = (int) (255.0 * value);
xblue = (int) (255.0 * value);
}
else
{
if(hue >= 1.0){
hue = 0.0;
}
else
{
hue = hue * 6;
}
lexen = ((int) (hue));
nexel = hue - ((double) (lexen));
M = value * (1 - saturation);
N = value * (1 - saturation * nexel);
K = value * (1-saturation*(1-nexel));
if(lexen==0) { xred = (int) (255.0*value); xgreen = (int) (255.0*K); xblue = (int) (255.0*M);}
if(lexen==1) { xred = (int) (255.0*N); xgreen = (int) (255.0*value); xblue = (int) (255.0*M);}
if(lexen==2) { xred = (int) (255.0*M); xgreen = (int) (255.0*value); xblue = (int) (255.0*K);}
if(lexen==3) { xred = (int) (255.0*M); xgreen = (int) (255.0*N); xblue = (int) (255.0*value);}
if(lexen==4) { xred = (int) (255.0*K); xgreen = (int) (255.0*M); xblue = (int) (255.0*value);}
if(lexen==5) { xred = (int) (255.0*value); xgreen = (int) (255.0*M); xblue = (int) (255.0*N);}
}
out.red = xred;
out.green = xgreen;
out.blue = xblue;
return out;
}
static double pi = 3.1415926535;
static color argcolor(cx q)
{
return hsv2rgb((atan(q.im/q.re)+pi/2.0)/pi ,1.0,1.0);
//return hsv2rgb(atan2(q.im,q.re)/pi ,1.0,1.0);
//return hsv2rgb((atan2(q.im,q.re)/pi+1)/2.0 ,1.0,1.0);
}
/* here's the actual newton method def -- I"m going to be using
thirty iterations at the moment, because that seems to get fine
results with mpmath */
#ifdef CALLCOUNT
static int newtzeros = 0;
static int newtnonzeros = 0;
static int newtfullcount = 0;
static int newtlatestexit = 0;
#endif
static cx newt(cx z, cx q)
{
/* these don't really need to be defined as global variables, so
I'll define them locally */
cx current;
current = z;
/* precalculate some stuff. blorf could be hoisted out of newt()
but it's already 2 inner loops up from where it was */
cx qpart[2 * RANGE + 1];
cx blorf[2 * RANGE + 1];
int n;
for (n = -RANGE; n <= RANGE; n++) {
cx ponent;
ponent.re = n*n;
ponent.im = 0.0;
qpart[n+RANGE] = powc(q,ponent);
blorf[n+RANGE] = rmult(2*n,ai);
}
int ix;
#ifdef CALLCOUNT
int zerocount = 0;
#endif
for(ix = 0; ix < ITERS; ix++)
{
const cx zpart = expc(current);
cx sum = origin;
cx psum = origin;
int n;
for(n=-RANGE;n<=RANGE;n++)
{
/* if qpart is 0, none of the rest of this matters */
/* XXX this could be moved up to the precalculation loop to control the iterations we do here */
if (qpart[n+RANGE].re == 0 && qpart[n+RANGE].im == 0) continue;
/* calculate jtheta3 */
cx zpart_n = powc(zpart, blorf[n+RANGE]);
sum = add(sum,mult(qpart[n+RANGE],zpart_n));
/* calculate pjtheta3 from that */
zpart_n = mult(blorf[n+RANGE],zpart_n);
psum = add(psum,mult(qpart[n+RANGE],zpart_n));
}
cx next = cdiff(current,cdiv(sum, psum));
// If the iteration has converged, bail.
// If next has NANs, these comparisons will be true, and we'll
// fall out here, which is what we want anyway
if (current.re == next.re && current.im == next.im) {
#ifdef CALLCOUNT
if (ix > newtlatestexit) newtlatestexit = ix;
if (!zerocount) {
// fprintf(stderr, "iter %d\n", ix);
newtzeros++;
zerocount++;
}
if (memcmp(¤t, &next, sizeof current)) {
fprintf(stderr, "%a %a %a %a\n", current.re, current.im, next.re, next.im);
}
#endif
// next may have NANs, use it instead of current, to be similar to old code
current = next;
break;
}
#ifdef CALLCOUNT
else if (zerocount) newtnonzeros++;
if (ix == ITERS) newtfullcount++;
#endif
current = next;
}
return current;
}
int main(int argc, char *argv[])
{
if (argc != 2 && argc != 3) {
fprintf(stderr, "%s displaysize [width]\n", argv[0]);
exit(1);
}
int displaysize = atoi(argv[1]);
if (displaysize % 2 || displaysize < 2) {
fprintf(stderr, "%s: displaysize needs to be even and greater than 0\n",
argv[0]);
exit(1);
}
int width = displaysize;
if (argc == 3) {
width = atoi(argv[2]);
if (width % 2 || width < 2) {
fprintf(stderr, "%s: width needs to be even and greater than 0\n",
argv[0]);
exit(1);
}
}
double halfdisplay = displaysize / 2.0, halfwidth = width / 2.0;
cx testq;
testq.re = 0.001;
testq.im = -.3019;
#if 0
cx testz;
testz.re = 0.212;
testz.im= 0.110;
printf("theta3(testz,testq)= %.20f %.20f i\n", jtheta3(testz,testq).re, jtheta3(testz,testq).im);
#endif
int a, b;
cx z;
printf("P3\n%d %d\n255\n", width, displaysize);
for(a=0;a<displaysize;a++)
{
for(b=0;b<width;b++)
{
z.im = 4.3 * (a-halfdisplay) / halfdisplay;
z.re = 4.3 * (b-halfwidth) / halfwidth;
/* I'm going to keep testq constant now */
// display[a][b] = argcolor(newt(z,testq));
color c = argcolor(newt(z, testq));
printf("%d %d %d\n", c.red, c.green, c.blue);
}
}
#ifdef CALLCOUNT
fprintf(stderr, "exp %d re %d im %d\n"
"pow %d a re %d a im %d b re %d b im %d\n",
exp_count, exp_re_count, exp_im_count,
pow_count, pow_a_re_count, pow_a_im_count, pow_b_re_count, pow_b_im_count);
fprintf(stderr, "full iter count %d zeros %d following nonzeros %d latest exit %d\n", newtfullcount, newtzeros, newtnonzeros, newtlatestexit);
#endif
return 0;
}