-
Notifications
You must be signed in to change notification settings - Fork 0
/
q-exp.C
153 lines (123 loc) · 2.78 KB
/
q-exp.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
/*
* q-exp.C
*
* FUNCTION:
* display q-exponential (degenerate basic hypergeoetric series)
*
* 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 "totient.h"
static int max_terms;
void q_factorial (double re_q, double im_q, int n, double *prep, double *pimp)
{
int i;
*prep = 0.0;
*pimp = 0.0;
double fre = 1.0;
double fim = 0.0;
double qnre = re_q;
double qnim = im_q;
for (i=1; i<=n; i++)
{
/* f *= (1-q^n) */
double tmp = fre * (1.0-qnre) + fim *qnim;
fim = (1.0-qnre) * fim - fre * qnim;
fre = tmp;
/* qn *= q */
tmp = re_q * qnre - im_q*qnim;
qnim = qnre * im_q + re_q * qnim;
qnre = tmp;
}
*prep = fre;
*pimp = fim;
}
/* Zagier-Kontsevich power series */
static void q_zagier_c (double re_q, double im_q,
double *prep, double *pimp)
{
int i;
*prep = 0.0;
*pimp = 0.0;
double rep = 1.0;
double imp = 0.0;
// double qpmod = re_q*re_q+im_q*im_q;
// if (1.0 <= qpmod) return;
for (i=1; i<max_terms; i++)
{
double ret, imt;
q_factorial (re_q, im_q, i, &ret, &imt);
rep += ret;
imp += imt;
// 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;
}
static void q_exp_c (double re_q, double im_q,
double *prep, double *pimp, double zee)
{
int i;
*prep = 0.0;
*pimp = 0.0;
double rep = 1.0;
double imp = 0.0;
/* power of q */
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++)
{
/* compute 1-zq^k */
double red = 1.0 - zee * qpr;
double imd = -zee * qpi;
/* compute 1/(1-zq^k) */
double deno = 1.0/(red*red+imd*imd);
double ret = red * deno;
double imt = -imd * deno;
/* compute prod * 1/(1-zq^k) */
double tmp = rep*ret - imp * imt;
imp = rep*imt + imp* ret;
rep = tmp;
/* 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;
}
static double q_exp_series (double re_q, double im_q, int itermax, double param)
{
max_terms = itermax;
double rep, imp;
// q_exp_c (re_q, im_q, &rep, &imp, param);
q_zagier_c (re_q, im_q, &rep, &imp);
// return sqrt (rep*rep+imp*imp);
// return rep;
double phase = atan2 (imp, rep);
phase += M_PI;
phase /= 2.0*M_PI;
return phase;
}
DECL_MAKE_HEIGHT(q_exp_series);
/* --------------------------- END OF LIFE ------------------------- */