-
Notifications
You must be signed in to change notification settings - Fork 1
/
completeEllipticIntegrals.cpp
338 lines (308 loc) · 15.8 KB
/
completeEllipticIntegrals.cpp
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
////////////////////////////////////////////////////////////////////////////////
// File: complete_elliptic_integrals.c //
// Routine(s): //
// Complete_Elliptic_Integral_First_Kind //
// Complete_Elliptic_Integral_Second_Kind //
////////////////////////////////////////////////////////////////////////////////
#include"completeEllipticIntegrals.h"
////////////////////////////////////////////////////////////////////////////////
// double Complete_Elliptic_Integral_First_Kind(char arg, double x) //
// //
// Description: //
// The complete elliptic integral of the first kind is the integral from //
// 0 to pi / 2 of the integrand //
// dtheta / sqrt( 1 - k^2 sin^2(theta) ). //
// The parameter k is called the modulus. This integral is even in k. //
// The modulus, k, must satisfy |k| <= 1. If k = 0 then the integral //
// can be readily evaluated. If |k| = 1, then the integral is infinite. //
// Otherwise it must be approximated. //
// //
// In practise the arguments the complete elliptic function of the first //
// kind are also given as F(pi/2 \ alpha) or F(pi/2 | m) where the angle //
// alpha, called the modular angle, satisfies k = sin(alpha) and the //
// argument m = k^2 is simply called the parameter. //
// In terms of these arguments K = F(pi/2 \ alpha) = F(pi/2, sin(alpha)) //
// and K = F(pi/2 | m) = F(pi/2, sqrt(m)), where //
// K = Complete_Elliptic_Integral_First_Kind( k ). //
// //
// Let K(k) be the complete elliptic integral of the second kind where //
// k is the modulus and k' = sqrt(1-k^2) is the complementary modulus. //
// //
// The common mean method, sometimes called the Gauss transform, is a //
// variant of the descending Landen transformation in which two sequences //
// are formed: Setting a[0] = 1 and g[0] = k', the complementary modulus, //
// a[i] is the arithmetic average and g[i] is the geometric mean of a[i-1]//
// and g[i-1], i.e. a[i+1] = (a[i] + g[i])/2 and g[i+1] = sqrt(a[i]*g[i]).//
// The sequences satisfy the inequalities g[0] < g[1] < ... < a[1] < a[0].//
// Further, lim g[n] = lim a[n]. //
// The value of the complete elliptic integral of the first kind is //
// (pi/2) lim (1/G[n]) as n -> inf. //
// //
// Arguments: //
// char arg //
// The type of argument of the second argument of F(): //
// If arg = 'k', then x = k, the modulus of F(pi/2,k). //
// If arg = 'a', then x = alpha, the modular angle of //
// F(pi/2 \ alpha), alpha in radians. //
// If arg = 'm', then x = m, the parameter of F(pi/2 | m). //
// The value of arg defaults to 'k'. //
// double x //
// The second argument of the elliptic function F(pi/2,k), //
// F(pi/2 \ alpha) or F(pi/2 | m) corresponding to the value //
// of 'arg'. Note that if arg = 'k', then | x | <= 1 and if //
// arg = 'm', then 0 <= x <= 1. //
// //
// Return Value: //
// The value of the complete elliptic integral of the first kind for the //
// given modulus, modular angle, or parameter. Note that if |k| = 1, //
// or m = 1 or a = (+/-) pi/2 then the integral is infinite and DBL_MAX //
// is returned. //
// //
// Example: //
// double K; //
// double m, k, a; //
// //
// ( code to initialize a ) //
// //
// k = sin(a); //
// m = k * k; //
// K = Complete_Elliptic_Integral_First_Kind( 'a', a ); //
// printf("K(alpha) = %12.6f where angle(radians) = %12.6f\n",K, a); //
// K = Complete_Elliptic_Integral_First_Kind( 'k', k ); //
// printf("K(k) = %12.6f where k = %12.6f\n",K, k); //
// K = Complete_Elliptic_Integral_First_Kind( 'm', m ); //
// printf("K(m) = %12.6f where m = %12.6f\n",K, m); //
////////////////////////////////////////////////////////////////////////////////
#ifndef M_PI_2
#define M_PI_2 1.57079632679489661923
#endif
double Complete_Elliptic_Integral_First_Kind(char arg, double x)
{
long double k; // modulus
long double m; // parameter
long double a; // average
long double g; // geometric mean
long double a_old; // previous average
long double g_old; // previous geometric mean
if ( x == 0.0 ) return M_PI_2;
switch (arg) {
case 'k': k = fabsl((long double) x);
m = k * k;
break;
case 'm': m = (long double) x;
k = sqrtl(fabsl(m));
break;
case 'a': k = sinl((long double)x);
m = k * k;
break;
default: k = fabsl((long double) x);
m = k * k;
}
if ( m == 1.0 ) return DBL_MAX;
a = 1.0L;
g = sqrtl(1.0L - m);
while (1) {
g_old = g;
a_old = a;
a = 0.5L * (g_old + a_old);
g = sqrtl(g_old * a_old);
if ( fabsl(a_old - g_old) <= (a_old * LDBL_EPSILON) ) break;
}
return (double) (M_PI_2 / g);
}
////////////////////////////////////////////////////////////////////////////////
// double Complete_Elliptic_Integral_Second_Kind(cahr arg, double x) //
// //
// Description: //
// The complete elliptic integral of the second kind is the integral from //
// 0 to pi / 2 of the integrand //
// sqrt( 1 - k^2 sin^2(theta) ) dtheta . //
// The parameter k is called the modulus. This integral is even in k. //
// The modulus, k, must satisfy |k| <= 1. If k = 0 or |k| = 1 then the //
// integral can be readily evaluated. Otherwise it must be approximated. //
// //
// In practise the arguments the elliptic function of the second kind are //
// also given as E(pi/2 \ alpha) or E(pi/2 | m) where the angle alpha, //
// called the modular angle, satisfies k = sin(alpha) and the argument //
// m = k^2 is simply called the parameter. //
// In terms of these arguments E = E(pi/2 \ alpha) = E(pi/2, sin(alpha)) //
// and E = E(pi/2 | m) = E(pi/2, sqrt(m)), where //
// E = Complete_Elliptic_Integral_Second_Kind( k ). //
// //
// Let K(k) be the complete elliptic integral of the second kind where //
// k is the modulus and k' = sqrt(1-k^2) is the complementary modulus. //
// //
// The common mean method, sometimes called the Gauss transform, is a //
// variant of the descending Landen transformation in which two sequences //
// are formed: Setting a[0] = 1 and g[0] = k', the complementary modulus, //
// a[i] is the arithmetic average and g[i] is the geometric mean of a[i-1]//
// and g[i-1], i.e. a[i+1] = (a[i] + g[i])/2 and g[i+1] = sqrt(a[i]*g[i]).//
// The sequences satisfy the inequalities g[0] < g[1] < ... < a[1] < a[0].//
// Further, lim g[n] = lim a[n] as n -> inf. //
// The value of the complete elliptic integral of the second kind is //
// E(k) = lim (pi/8g[n]) (4 - 2k^2 - Sum(2^j(a[j]-g[j])^2)). //
// where the limit is as n -> inf and the sum extends from j = 0 to n. //
// The sum of 2^j (a[j]^2 - g[j]^2) from j = 1 to n equals //
// (1/2) Sum (2^i (a[i] - g[i])^2 for i = 0,...,n-1, so that //
// E(k) = lim (pi/4g[n]) (2 - k^2 - Sum(2^j(a[j]^2 -g[j]^2))). //
// //
// Arguments: //
// char arg //
// The type of argument of the second argument of E(): //
// If arg = 'k', then x = k, the modulus of E(pi/2,k). //
// If arg = 'a', then x = alpha, the modular angle of //
// E(pi/2 \ alpha), alpha in radians. //
// If arg = 'm', then x = m, the parameter of E(pi/2 | m). //
// The value of arg defaults to 'k'. //
// double x //
// The second argument of the elliptic function E(pi/2,k), //
// E(pi/2 \ alpha) or E(pi/2 | m) corresponding to the value //
// of 'arg'. Note that if arg = 'k', then | x | <= 1 and if //
// arg = 'm', then 0 <= x <= 1. //
// //
// Return Value: //
// The value of the complete elliptic integral of the second kind for the //
// given modulus, modular angle, or parameter. //
// //
// Example: //
// double E; //
// double m, k, a; //
// //
// ( code to initialize a ) //
// //
// k = sin(a); //
// m = k * k; //
// E = Complete_Elliptic_Integral_Second_Kind( 'a', a ); //
// printf("E(alpha) = %12.6f where angle(radians) = %12.6f\n",E, a); //
// E = Complete_Elliptic_Integral_Second_Kind( 'k', k ); //
// printf("E(k) = %12.6f where k = %12.6f\n",E, k); //
// E = Complete_Elliptic_Integral_Second_Kind( 'm', m ); //
// printf("E(m) = %12.6f where m = %12.6f\n",E, m); //
////////////////////////////////////////////////////////////////////////////////
double Complete_Elliptic_Integral_Second_Kind(char arg, double x)
{
long double k; // modulus
long double m; // the parameter of the elliptic function m = modulus^2
long double a; // arithmetic mean
long double g; // geometric mean
long double a_old; // previous arithmetic mean
long double g_old; // previous geometric mean
long double two_n; // power of 2
long double Ek;
if ( x == 0.0 ) return M_PI_2;
switch (arg) {
case 'k': k = fabsl((long double) x);
m = k * k;
break;
case 'm': m = (long double) x;
k = sqrtl(fabsl(m));
break;
case 'a': k = sinl((long double)x);
m = k * k;
break;
default: k = fabsl((long double) x);
m = k * k;
}
if ( m == 1.0 ) return 1.0;
a = 1.0L;
g = sqrtl(1.0L - m);
two_n = 1.0L;
Ek = 2.0L - m;
while (1) {
g_old = g;
a_old = a;
a = 0.5L * (g_old + a_old);
g = a_old * g_old;
two_n += two_n;
Ek -= two_n * (a * a - g);
if ( fabsl(a_old - g_old) <= (a_old * LDBL_EPSILON) ) break;
g = sqrtl(g);
}
return (double) ((PI_4 / a) * Ek);
}
void Complete_Elliptic_Integrals(char arg, double x, double& K, double& E)
{
long double k; // modulus
long double m; // parameter
long double a; // average
long double g; // geometric mean
long double a_old; // previous average
long double g_old; // previous geometric mean
long double two_n; // power of 2
if ( x == 0.0 ){
K = E = M_PI_2;
return;
}
switch (arg) {
case 'k': k = (long double) x;
m = k * k;
break;
case 'm': m = (long double) x;
k = sqrtl(fabsl(m));
break;
case 'a': k = sinl((long double)x);
m = k * k;
break;
default: k = (long double) x;
m = k * k;
}
if ( m == 1.0 ){
K = DBL_MAX;
E = 1.0;
return;
}
a = 1.0L;
g = sqrtl(1.0L - m);
two_n = 1.0L;
E = 2.0L - m;
while(true){
g_old = g;
a_old = a;
a = 0.5L * (g_old + a_old);
g = g_old * a_old;
two_n += two_n;
E -= two_n * (a * a - g);
g = sqrtl(g);
if ( fabsl(a_old - g_old) <= (a_old * LDBL_EPSILON) ) break;
}
K = (double) (M_PI_2 / g);
E = (double) ((PI_4 / a) * E);
}
void Complete_Elliptic_Integrals_Modulus(double x, double& K, double& E)
{
long double k; // modulus
long double m; // parameter
long double a; // average
long double g; // geometric mean
long double a_old; // previous average
long double g_old; // previous geometric mean
long double two_n; // power of 2
long double cE; // sum
if ( x == 0.0 ){
K = E = M_PI_2;
return;
}
k = (long double) x;
m = k * k;
if ( x == 1.0 ){
K = DBL_MAX;
E = 1.0;
return;
}
a = 1.0L;
g = sqrtl(1.0L - m);
two_n = 1.0L;
cE = 2.0L - m;
while(true){
g_old = g;
a_old = a;
a = 0.5L * (g_old + a_old);
g = g_old * a_old;
two_n += two_n;
cE -= two_n * (a * a - g);
g = sqrtl(g);
if ( fabsl(a_old - g_old) <= (a_old * LDBL_EPSILON) ) break;
}
K = (double) (M_PI_2 / g);
E = (double) ((PI_4 / a) * cE);
}