10
by dinko.metalac at gmail
Added all necessary python files. Now it works on the device! |
1 |
"""
|
2 |
Limited tests of the elliptic functions module. A full suite of
|
|
3 |
extensive testing can be found in elliptic_torture_tests.py
|
|
4 |
||
5 |
Author of the first version: M.T. Taschuk
|
|
6 |
||
7 |
References:
|
|
8 |
||
9 |
[1] Abramowitz & Stegun. 'Handbook of Mathematical Functions, 9th Ed.',
|
|
10 |
(Dover duplicate of 1972 edition)
|
|
11 |
[2] Whittaker 'A Course of Modern Analysis, 4th Ed.', 1946,
|
|
12 |
Cambridge University Press
|
|
13 |
||
14 |
"""
|
|
15 |
||
16 |
import sympy.mpmath |
|
17 |
import random |
|
18 |
||
19 |
from sympy.mpmath import * |
|
20 |
||
21 |
def mpc_ae(a, b, eps=eps): |
|
22 |
res = True |
|
23 |
res = res and a.real.ae(b.real, eps) |
|
24 |
res = res and a.imag.ae(b.imag, eps) |
|
25 |
return res |
|
26 |
||
27 |
zero = mpf(0) |
|
28 |
one = mpf(1) |
|
29 |
||
30 |
jsn = ellipfun('sn') |
|
31 |
jcn = ellipfun('cn') |
|
32 |
jdn = ellipfun('dn') |
|
33 |
||
34 |
calculate_nome = lambda k: qfrom(k=k) |
|
35 |
||
36 |
def test_ellipfun(): |
|
37 |
mp.dps = 15 |
|
38 |
assert ellipfun('ss', 0, 0) == 1 |
|
39 |
assert ellipfun('cc', 0, 0) == 1 |
|
40 |
assert ellipfun('dd', 0, 0) == 1 |
|
41 |
assert ellipfun('nn', 0, 0) == 1 |
|
42 |
assert ellipfun('sn', 0.25, 0).ae(sin(0.25)) |
|
43 |
assert ellipfun('cn', 0.25, 0).ae(cos(0.25)) |
|
44 |
assert ellipfun('dn', 0.25, 0).ae(1) |
|
45 |
assert ellipfun('ns', 0.25, 0).ae(csc(0.25)) |
|
46 |
assert ellipfun('nc', 0.25, 0).ae(sec(0.25)) |
|
47 |
assert ellipfun('nd', 0.25, 0).ae(1) |
|
48 |
assert ellipfun('sc', 0.25, 0).ae(tan(0.25)) |
|
49 |
assert ellipfun('sd', 0.25, 0).ae(sin(0.25)) |
|
50 |
assert ellipfun('cd', 0.25, 0).ae(cos(0.25)) |
|
51 |
assert ellipfun('cs', 0.25, 0).ae(cot(0.25)) |
|
52 |
assert ellipfun('dc', 0.25, 0).ae(sec(0.25)) |
|
53 |
assert ellipfun('ds', 0.25, 0).ae(csc(0.25)) |
|
54 |
assert ellipfun('sn', 0.25, 1).ae(tanh(0.25)) |
|
55 |
assert ellipfun('cn', 0.25, 1).ae(sech(0.25)) |
|
56 |
assert ellipfun('dn', 0.25, 1).ae(sech(0.25)) |
|
57 |
assert ellipfun('ns', 0.25, 1).ae(coth(0.25)) |
|
58 |
assert ellipfun('nc', 0.25, 1).ae(cosh(0.25)) |
|
59 |
assert ellipfun('nd', 0.25, 1).ae(cosh(0.25)) |
|
60 |
assert ellipfun('sc', 0.25, 1).ae(sinh(0.25)) |
|
61 |
assert ellipfun('sd', 0.25, 1).ae(sinh(0.25)) |
|
62 |
assert ellipfun('cd', 0.25, 1).ae(1) |
|
63 |
assert ellipfun('cs', 0.25, 1).ae(csch(0.25)) |
|
64 |
assert ellipfun('dc', 0.25, 1).ae(1) |
|
65 |
assert ellipfun('ds', 0.25, 1).ae(csch(0.25)) |
|
66 |
assert ellipfun('sn', 0.25, 0.5).ae(0.24615967096986145833) |
|
67 |
assert ellipfun('cn', 0.25, 0.5).ae(0.96922928989378439337) |
|
68 |
assert ellipfun('dn', 0.25, 0.5).ae(0.98473484156599474563) |
|
69 |
assert ellipfun('ns', 0.25, 0.5).ae(4.0624038700573130369) |
|
70 |
assert ellipfun('nc', 0.25, 0.5).ae(1.0317476065024692949) |
|
71 |
assert ellipfun('nd', 0.25, 0.5).ae(1.0155017958029488665) |
|
72 |
assert ellipfun('sc', 0.25, 0.5).ae(0.25397465134058993408) |
|
73 |
assert ellipfun('sd', 0.25, 0.5).ae(0.24997558792415733063) |
|
74 |
assert ellipfun('cd', 0.25, 0.5).ae(0.98425408443195497052) |
|
75 |
assert ellipfun('cs', 0.25, 0.5).ae(3.9374008182374110826) |
|
76 |
assert ellipfun('dc', 0.25, 0.5).ae(1.0159978158253033913) |
|
77 |
assert ellipfun('ds', 0.25, 0.5).ae(4.0003906313579720593) |
|
78 |
||
79 |
||
80 |
||
81 |
||
82 |
def test_calculate_nome(): |
|
83 |
mp.dps = 100 |
|
84 |
||
85 |
q = calculate_nome(zero) |
|
86 |
assert(q == zero) |
|
87 |
||
88 |
mp.dps = 25 |
|
89 |
# used Mathematica's EllipticNomeQ[m]
|
|
90 |
math1 = [(mpf(1)/10, mpf('0.006584651553858370274473060')), |
|
91 |
(mpf(2)/10, mpf('0.01394285727531826872146409')), |
|
92 |
(mpf(3)/10, mpf('0.02227743615715350822901627')), |
|
93 |
(mpf(4)/10, mpf('0.03188334731336317755064299')), |
|
94 |
(mpf(5)/10, mpf('0.04321391826377224977441774')), |
|
95 |
(mpf(6)/10, mpf('0.05702025781460967637754953')), |
|
96 |
(mpf(7)/10, mpf('0.07468994353717944761143751')), |
|
97 |
(mpf(8)/10, mpf('0.09927369733882489703607378')), |
|
98 |
(mpf(9)/10, mpf('0.1401731269542615524091055')), |
|
99 |
(mpf(9)/10, mpf('0.1401731269542615524091055'))] |
|
100 |
||
101 |
for i in math1: |
|
102 |
m = i[0] |
|
103 |
q = calculate_nome(sqrt(m)) |
|
104 |
assert q.ae(i[1]) |
|
105 |
||
106 |
mp.dps = 15 |
|
107 |
||
108 |
def test_jtheta(): |
|
109 |
mp.dps = 25 |
|
110 |
||
111 |
z = q = zero |
|
112 |
for n in range(1,5): |
|
113 |
value = jtheta(n, z, q) |
|
114 |
assert(value == (n-1)//2) |
|
115 |
||
116 |
for q in [one, mpf(2)]: |
|
117 |
for n in range(1,5): |
|
118 |
raised = True |
|
119 |
try: |
|
120 |
r = jtheta(n, z, q) |
|
121 |
except: |
|
122 |
pass
|
|
123 |
else: |
|
124 |
raised = False |
|
125 |
assert(raised) |
|
126 |
||
127 |
z = one/10 |
|
128 |
q = one/11 |
|
129 |
||
130 |
# Mathematical N[EllipticTheta[1, 1/10, 1/11], 25]
|
|
131 |
res = mpf('0.1069552990104042681962096') |
|
132 |
result = jtheta(1, z, q) |
|
133 |
assert(result.ae(res)) |
|
134 |
||
135 |
# Mathematica N[EllipticTheta[2, 1/10, 1/11], 25]
|
|
136 |
res = mpf('1.101385760258855791140606') |
|
137 |
result = jtheta(2, z, q) |
|
138 |
assert(result.ae(res)) |
|
139 |
||
140 |
# Mathematica N[EllipticTheta[3, 1/10, 1/11], 25]
|
|
141 |
res = mpf('1.178319743354331061795905') |
|
142 |
result = jtheta(3, z, q) |
|
143 |
assert(result.ae(res)) |
|
144 |
||
145 |
# Mathematica N[EllipticTheta[4, 1/10, 1/11], 25]
|
|
146 |
res = mpf('0.8219318954665153577314573') |
|
147 |
result = jtheta(4, z, q) |
|
148 |
assert(result.ae(res)) |
|
149 |
||
150 |
# test for sin zeros for jtheta(1, z, q)
|
|
151 |
# test for cos zeros for jtheta(2, z, q)
|
|
152 |
z1 = pi |
|
153 |
z2 = pi/2 |
|
154 |
for i in range(10): |
|
155 |
qstring = str(random.random()) |
|
156 |
q = mpf(qstring) |
|
157 |
result = jtheta(1, z1, q) |
|
158 |
assert(result.ae(0)) |
|
159 |
result = jtheta(2, z2, q) |
|
160 |
assert(result.ae(0)) |
|
161 |
mp.dps = 15 |
|
162 |
||
163 |
||
164 |
def test_jtheta_issue39(): |
|
165 |
# near the circle of covergence |q| = 1 the convergence slows
|
|
166 |
# down; for |q| > Q_LIM the theta functions raise ValueError
|
|
167 |
mp.dps = 30 |
|
168 |
mp.dps += 30 |
|
169 |
q = mpf(6)/10 - one/10**6 - mpf(8)/10 * j |
|
170 |
mp.dps -= 30 |
|
171 |
# Mathematica run first
|
|
172 |
# N[EllipticTheta[3, 1, 6/10 - 10^-6 - 8/10*I], 2000]
|
|
173 |
# then it works:
|
|
174 |
# N[EllipticTheta[3, 1, 6/10 - 10^-6 - 8/10*I], 30]
|
|
175 |
res = mpf('32.0031009628901652627099524264') + \ |
|
176 |
mpf('16.6153027998236087899308935624') * j |
|
177 |
result = jtheta(3, 1, q) |
|
178 |
# check that for abs(q) > Q_LIM a ValueError exception is raised
|
|
179 |
mp.dps += 30 |
|
180 |
q = mpf(6)/10 - one/10**7 - mpf(8)/10 * j |
|
181 |
mp.dps -= 30 |
|
182 |
try: |
|
183 |
result = jtheta(3, 1, q) |
|
184 |
except ValueError: |
|
185 |
pass
|
|
186 |
else: |
|
187 |
assert(False) |
|
188 |
||
189 |
# bug reported in issue39
|
|
190 |
mp.dps = 100 |
|
191 |
z = (1+j)/3 |
|
192 |
q = mpf(368983957219251)/10**15 + mpf(636363636363636)/10**15 * j |
|
193 |
# Mathematica N[EllipticTheta[1, z, q], 35]
|
|
194 |
res = mpf('2.4439389177990737589761828991467471') + \ |
|
195 |
mpf('0.5446453005688226915290954851851490') *j |
|
196 |
mp.dps = 30 |
|
197 |
result = jtheta(1, z, q) |
|
198 |
assert(result.ae(res)) |
|
199 |
mp.dps = 80 |
|
200 |
z = 3 + 4*j |
|
201 |
q = 0.5 + 0.5*j |
|
202 |
r1 = jtheta(1, z, q) |
|
203 |
mp.dps = 15 |
|
204 |
r2 = jtheta(1, z, q) |
|
205 |
assert r1.ae(r2) |
|
206 |
mp.dps = 80 |
|
207 |
z = 3 + j |
|
208 |
q1 = exp(j*3) |
|
209 |
# longer test
|
|
210 |
# for n in range(1, 6)
|
|
211 |
for n in range(1, 2): |
|
212 |
mp.dps = 80 |
|
213 |
q = q1*(1 - mpf(1)/10**n) |
|
214 |
r1 = jtheta(1, z, q) |
|
215 |
mp.dps = 15 |
|
216 |
r2 = jtheta(1, z, q) |
|
217 |
assert r1.ae(r2) |
|
218 |
mp.dps = 15 |
|
28
by dinko.metalac at gmail
new sympy |
219 |
# issue 3138 about high derivatives
|
10
by dinko.metalac at gmail
Added all necessary python files. Now it works on the device! |
220 |
assert jtheta(3, 4.5, 0.25, 9).ae(1359.04892680683) |
221 |
assert jtheta(3, 4.5, 0.25, 50).ae(-6.14832772630905e+33) |
|
222 |
mp.dps = 50 |
|
223 |
r = jtheta(3, 4.5, 0.25, 9) |
|
224 |
assert r.ae('1359.048926806828939547859396600218966947753213803') |
|
225 |
r = jtheta(3, 4.5, 0.25, 50) |
|
226 |
assert r.ae('-6148327726309051673317975084654262.4119215720343656') |
|
227 |
||
228 |
def test_jtheta_identities(): |
|
229 |
"""
|
|
230 |
Tests the some of the jacobi identidies found in Abramowitz,
|
|
231 |
Sec. 16.28, Pg. 576. The identities are tested to 1 part in 10^98.
|
|
232 |
"""
|
|
233 |
mp.dps = 110 |
|
234 |
eps1 = ldexp(eps, 30) |
|
235 |
||
236 |
for i in range(10): |
|
237 |
qstring = str(random.random()) |
|
238 |
q = mpf(qstring) |
|
239 |
||
240 |
zstring = str(10*random.random()) |
|
241 |
z = mpf(zstring) |
|
242 |
# Abramowitz 16.28.1
|
|
243 |
# v_1(z, q)**2 * v_4(0, q)**2 = v_3(z, q)**2 * v_2(0, q)**2
|
|
244 |
# - v_2(z, q)**2 * v_3(0, q)**2
|
|
245 |
term1 = (jtheta(1, z, q)**2) * (jtheta(4, zero, q)**2) |
|
246 |
term2 = (jtheta(3, z, q)**2) * (jtheta(2, zero, q)**2) |
|
247 |
term3 = (jtheta(2, z, q)**2) * (jtheta(3, zero, q)**2) |
|
248 |
equality = term1 - term2 + term3 |
|
249 |
assert(equality.ae(0, eps1)) |
|
250 |
||
251 |
zstring = str(100*random.random()) |
|
252 |
z = mpf(zstring) |
|
253 |
# Abramowitz 16.28.2
|
|
254 |
# v_2(z, q)**2 * v_4(0, q)**2 = v_4(z, q)**2 * v_2(0, q)**2
|
|
255 |
# - v_1(z, q)**2 * v_3(0, q)**2
|
|
256 |
term1 = (jtheta(2, z, q)**2) * (jtheta(4, zero, q)**2) |
|
257 |
term2 = (jtheta(4, z, q)**2) * (jtheta(2, zero, q)**2) |
|
258 |
term3 = (jtheta(1, z, q)**2) * (jtheta(3, zero, q)**2) |
|
259 |
equality = term1 - term2 + term3 |
|
260 |
assert(equality.ae(0, eps1)) |
|
261 |
||
262 |
# Abramowitz 16.28.3
|
|
263 |
# v_3(z, q)**2 * v_4(0, q)**2 = v_4(z, q)**2 * v_3(0, q)**2
|
|
264 |
# - v_1(z, q)**2 * v_2(0, q)**2
|
|
265 |
term1 = (jtheta(3, z, q)**2) * (jtheta(4, zero, q)**2) |
|
266 |
term2 = (jtheta(4, z, q)**2) * (jtheta(3, zero, q)**2) |
|
267 |
term3 = (jtheta(1, z, q)**2) * (jtheta(2, zero, q)**2) |
|
268 |
equality = term1 - term2 + term3 |
|
269 |
assert(equality.ae(0, eps1)) |
|
270 |
||
271 |
# Abramowitz 16.28.4
|
|
272 |
# v_4(z, q)**2 * v_4(0, q)**2 = v_3(z, q)**2 * v_3(0, q)**2
|
|
273 |
# - v_2(z, q)**2 * v_2(0, q)**2
|
|
274 |
term1 = (jtheta(4, z, q)**2) * (jtheta(4, zero, q)**2) |
|
275 |
term2 = (jtheta(3, z, q)**2) * (jtheta(3, zero, q)**2) |
|
276 |
term3 = (jtheta(2, z, q)**2) * (jtheta(2, zero, q)**2) |
|
277 |
equality = term1 - term2 + term3 |
|
278 |
assert(equality.ae(0, eps1)) |
|
279 |
||
280 |
# Abramowitz 16.28.5
|
|
281 |
# v_2(0, q)**4 + v_4(0, q)**4 == v_3(0, q)**4
|
|
282 |
term1 = (jtheta(2, zero, q))**4 |
|
283 |
term2 = (jtheta(4, zero, q))**4 |
|
284 |
term3 = (jtheta(3, zero, q))**4 |
|
285 |
equality = term1 + term2 - term3 |
|
286 |
assert(equality.ae(0, eps1)) |
|
287 |
mp.dps = 15 |
|
288 |
||
289 |
def test_jtheta_complex(): |
|
290 |
mp.dps = 30 |
|
291 |
z = mpf(1)/4 + j/8 |
|
292 |
q = mpf(1)/3 + j/7 |
|
293 |
# Mathematica N[EllipticTheta[1, 1/4 + I/8, 1/3 + I/7], 35]
|
|
294 |
res = mpf('0.31618034835986160705729105731678285') + \ |
|
295 |
mpf('0.07542013825835103435142515194358975') * j |
|
296 |
r = jtheta(1, z, q) |
|
297 |
assert(mpc_ae(r, res)) |
|
298 |
||
299 |
# Mathematica N[EllipticTheta[2, 1/4 + I/8, 1/3 + I/7], 35]
|
|
300 |
res = mpf('1.6530986428239765928634711417951828') + \ |
|
301 |
mpf('0.2015344864707197230526742145361455') * j |
|
302 |
r = jtheta(2, z, q) |
|
303 |
assert(mpc_ae(r, res)) |
|
304 |
||
305 |
# Mathematica N[EllipticTheta[3, 1/4 + I/8, 1/3 + I/7], 35]
|
|
306 |
res = mpf('1.6520564411784228184326012700348340') + \ |
|
307 |
mpf('0.1998129119671271328684690067401823') * j |
|
308 |
r = jtheta(3, z, q) |
|
309 |
assert(mpc_ae(r, res)) |
|
310 |
||
311 |
# Mathematica N[EllipticTheta[4, 1/4 + I/8, 1/3 + I/7], 35]
|
|
312 |
res = mpf('0.37619082382228348252047624089973824') - \ |
|
313 |
mpf('0.15623022130983652972686227200681074') * j |
|
314 |
r = jtheta(4, z, q) |
|
315 |
assert(mpc_ae(r, res)) |
|
316 |
||
317 |
# check some theta function identities
|
|
318 |
mp.dos = 100 |
|
319 |
z = mpf(1)/4 + j/8 |
|
320 |
q = mpf(1)/3 + j/7 |
|
321 |
mp.dps += 10 |
|
322 |
a = [0,0, jtheta(2, 0, q), jtheta(3, 0, q), jtheta(4, 0, q)] |
|
323 |
t = [0, jtheta(1, z, q), jtheta(2, z, q), jtheta(3, z, q), jtheta(4, z, q)] |
|
324 |
r = [(t[2]*a[4])**2 - (t[4]*a[2])**2 + (t[1] *a[3])**2, |
|
325 |
(t[3]*a[4])**2 - (t[4]*a[3])**2 + (t[1] *a[2])**2, |
|
326 |
(t[1]*a[4])**2 - (t[3]*a[2])**2 + (t[2] *a[3])**2, |
|
327 |
(t[4]*a[4])**2 - (t[3]*a[3])**2 + (t[2] *a[2])**2, |
|
328 |
a[2]**4 + a[4]**4 - a[3]**4] |
|
329 |
mp.dps -= 10 |
|
330 |
for x in r: |
|
331 |
assert(mpc_ae(x, mpc(0))) |
|
332 |
mp.dps = 15 |
|
333 |
||
334 |
def test_djtheta(): |
|
335 |
mp.dps = 30 |
|
336 |
||
337 |
z = one/7 + j/3 |
|
338 |
q = one/8 + j/5 |
|
339 |
# Mathematica N[EllipticThetaPrime[1, 1/7 + I/3, 1/8 + I/5], 35]
|
|
340 |
res = mpf('1.5555195883277196036090928995803201') - \ |
|
341 |
mpf('0.02439761276895463494054149673076275') * j |
|
342 |
result = jtheta(1, z, q, 1) |
|
343 |
assert(mpc_ae(result, res)) |
|
344 |
||
345 |
# Mathematica N[EllipticThetaPrime[2, 1/7 + I/3, 1/8 + I/5], 35]
|
|
346 |
res = mpf('0.19825296689470982332701283509685662') - \ |
|
347 |
mpf('0.46038135182282106983251742935250009') * j |
|
348 |
result = jtheta(2, z, q, 1) |
|
349 |
assert(mpc_ae(result, res)) |
|
350 |
||
351 |
# Mathematica N[EllipticThetaPrime[3, 1/7 + I/3, 1/8 + I/5], 35]
|
|
352 |
res = mpf('0.36492498415476212680896699407390026') - \ |
|
353 |
mpf('0.57743812698666990209897034525640369') * j |
|
354 |
result = jtheta(3, z, q, 1) |
|
355 |
assert(mpc_ae(result, res)) |
|
356 |
||
357 |
# Mathematica N[EllipticThetaPrime[4, 1/7 + I/3, 1/8 + I/5], 35]
|
|
358 |
res = mpf('-0.38936892528126996010818803742007352') + \ |
|
359 |
mpf('0.66549886179739128256269617407313625') * j |
|
360 |
result = jtheta(4, z, q, 1) |
|
361 |
assert(mpc_ae(result, res)) |
|
362 |
||
363 |
for i in range(10): |
|
364 |
q = (one*random.random() + j*random.random())/2 |
|
365 |
# identity in Wittaker, Watson &21.41
|
|
366 |
a = jtheta(1, 0, q, 1) |
|
367 |
b = jtheta(2, 0, q)*jtheta(3, 0, q)*jtheta(4, 0, q) |
|
368 |
assert(a.ae(b)) |
|
369 |
||
370 |
# test higher derivatives
|
|
371 |
mp.dps = 20 |
|
372 |
for q,z in [(one/3, one/5), (one/3 + j/8, one/5), |
|
373 |
(one/3, one/5 + j/8), (one/3 + j/7, one/5 + j/8)]: |
|
374 |
for n in [1, 2, 3, 4]: |
|
375 |
r = jtheta(n, z, q, 2) |
|
376 |
r1 = diff(lambda zz: jtheta(n, zz, q), z, n=2) |
|
377 |
assert r.ae(r1) |
|
378 |
r = jtheta(n, z, q, 3) |
|
379 |
r1 = diff(lambda zz: jtheta(n, zz, q), z, n=3) |
|
380 |
assert r.ae(r1) |
|
381 |
||
382 |
# identity in Wittaker, Watson &21.41
|
|
383 |
q = one/3 |
|
384 |
z = zero |
|
385 |
a = [0]*5 |
|
386 |
a[1] = jtheta(1, z, q, 3)/jtheta(1, z, q, 1) |
|
387 |
for n in [2,3,4]: |
|
388 |
a[n] = jtheta(n, z, q, 2)/jtheta(n, z, q) |
|
389 |
equality = a[2] + a[3] + a[4] - a[1] |
|
390 |
assert(equality.ae(0)) |
|
391 |
mp.dps = 15 |
|
392 |
||
393 |
def test_jsn(): |
|
394 |
"""
|
|
395 |
Test some special cases of the sn(z, q) function.
|
|
396 |
"""
|
|
397 |
mp.dps = 100 |
|
398 |
||
399 |
# trival case
|
|
400 |
result = jsn(zero, zero) |
|
401 |
assert(result == zero) |
|
402 |
||
403 |
# Abramowitz Table 16.5
|
|
404 |
#
|
|
405 |
# sn(0, m) = 0
|
|
406 |
||
407 |
for i in range(10): |
|
408 |
qstring = str(random.random()) |
|
409 |
q = mpf(qstring) |
|
410 |
||
411 |
equality = jsn(zero, q) |
|
412 |
assert(equality.ae(0)) |
|
413 |
||
414 |
# Abramowitz Table 16.6.1
|
|
415 |
#
|
|
416 |
# sn(z, 0) = sin(z), m == 0
|
|
417 |
#
|
|
418 |
# sn(z, 1) = tanh(z), m == 1
|
|
419 |
#
|
|
420 |
# It would be nice to test these, but I find that they run
|
|
421 |
# in to numerical trouble. I'm currently treating as a boundary
|
|
422 |
# case for sn function.
|
|
423 |
||
424 |
mp.dps = 25 |
|
425 |
arg = one/10 |
|
426 |
#N[JacobiSN[1/10, 2^-100], 25]
|
|
427 |
res = mpf('0.09983341664682815230681420') |
|
428 |
m = ldexp(one, -100) |
|
429 |
result = jsn(arg, m) |
|
430 |
assert(result.ae(res)) |
|
431 |
||
432 |
# N[JacobiSN[1/10, 1/10], 25]
|
|
433 |
res = mpf('0.09981686718599080096451168') |
|
434 |
result = jsn(arg, arg) |
|
435 |
assert(result.ae(res)) |
|
436 |
mp.dps = 15 |
|
437 |
||
438 |
def test_jcn(): |
|
439 |
"""
|
|
440 |
Test some special cases of the cn(z, q) function.
|
|
441 |
"""
|
|
442 |
mp.dps = 100 |
|
443 |
||
444 |
# Abramowitz Table 16.5
|
|
445 |
# cn(0, q) = 1
|
|
446 |
qstring = str(random.random()) |
|
447 |
q = mpf(qstring) |
|
448 |
cn = jcn(zero, q) |
|
449 |
assert(cn.ae(one)) |
|
450 |
||
451 |
# Abramowitz Table 16.6.2
|
|
452 |
#
|
|
453 |
# cn(u, 0) = cos(u), m == 0
|
|
454 |
#
|
|
455 |
# cn(u, 1) = sech(z), m == 1
|
|
456 |
#
|
|
457 |
# It would be nice to test these, but I find that they run
|
|
458 |
# in to numerical trouble. I'm currently treating as a boundary
|
|
459 |
# case for cn function.
|
|
460 |
||
461 |
mp.dps = 25 |
|
462 |
arg = one/10 |
|
463 |
m = ldexp(one, -100) |
|
464 |
#N[JacobiCN[1/10, 2^-100], 25]
|
|
465 |
res = mpf('0.9950041652780257660955620') |
|
466 |
result = jcn(arg, m) |
|
467 |
assert(result.ae(res)) |
|
468 |
||
469 |
# N[JacobiCN[1/10, 1/10], 25]
|
|
470 |
res = mpf('0.9950058256237368748520459') |
|
471 |
result = jcn(arg, arg) |
|
472 |
assert(result.ae(res)) |
|
473 |
mp.dps = 15 |
|
474 |
||
475 |
def test_jdn(): |
|
476 |
"""
|
|
477 |
Test some special cases of the dn(z, q) function.
|
|
478 |
"""
|
|
479 |
mp.dps = 100 |
|
480 |
||
481 |
# Abramowitz Table 16.5
|
|
482 |
# dn(0, q) = 1
|
|
483 |
mstring = str(random.random()) |
|
484 |
m = mpf(mstring) |
|
485 |
||
486 |
dn = jdn(zero, m) |
|
487 |
assert(dn.ae(one)) |
|
488 |
||
489 |
mp.dps = 25 |
|
490 |
# N[JacobiDN[1/10, 1/10], 25]
|
|
491 |
res = mpf('0.9995017055025556219713297') |
|
492 |
arg = one/10 |
|
493 |
result = jdn(arg, arg) |
|
494 |
assert(result.ae(res)) |
|
495 |
mp.dps = 15 |
|
496 |
||
497 |
||
498 |
def test_sn_cn_dn_identities(): |
|
499 |
"""
|
|
500 |
Tests the some of the jacobi elliptic function identities found
|
|
501 |
on Mathworld. Haven't found in Abramowitz.
|
|
502 |
"""
|
|
503 |
mp.dps = 100 |
|
504 |
N = 5 |
|
505 |
for i in range(N): |
|
506 |
qstring = str(random.random()) |
|
507 |
q = mpf(qstring) |
|
508 |
zstring = str(100*random.random()) |
|
509 |
z = mpf(zstring) |
|
510 |
||
511 |
# MathWorld
|
|
512 |
# sn(z, q)**2 + cn(z, q)**2 == 1
|
|
513 |
term1 = jsn(z, q)**2 |
|
514 |
term2 = jcn(z, q)**2 |
|
515 |
equality = one - term1 - term2 |
|
516 |
assert(equality.ae(0)) |
|
517 |
||
518 |
# MathWorld
|
|
519 |
# k**2 * sn(z, m)**2 + dn(z, m)**2 == 1
|
|
520 |
for i in range(N): |
|
521 |
mstring = str(random.random()) |
|
522 |
m = mpf(qstring) |
|
523 |
k = m.sqrt() |
|
524 |
zstring = str(10*random.random()) |
|
525 |
z = mpf(zstring) |
|
526 |
term1 = k**2 * jsn(z, m)**2 |
|
527 |
term2 = jdn(z, m)**2 |
|
528 |
equality = one - term1 - term2 |
|
529 |
assert(equality.ae(0)) |
|
530 |
||
531 |
||
532 |
for i in range(N): |
|
533 |
mstring = str(random.random()) |
|
534 |
m = mpf(mstring) |
|
535 |
k = m.sqrt() |
|
536 |
zstring = str(random.random()) |
|
537 |
z = mpf(zstring) |
|
538 |
||
539 |
# MathWorld
|
|
540 |
# k**2 * cn(z, m)**2 + (1 - k**2) = dn(z, m)**2
|
|
541 |
term1 = k**2 * jcn(z, m)**2 |
|
542 |
term2 = 1 - k**2 |
|
543 |
term3 = jdn(z, m)**2 |
|
544 |
equality = term3 - term1 - term2 |
|
545 |
assert(equality.ae(0)) |
|
546 |
||
547 |
K = ellipk(k**2) |
|
548 |
# Abramowitz Table 16.5
|
|
549 |
# sn(K, m) = 1; K is K(k), first complete elliptic integral
|
|
550 |
r = jsn(K, m) |
|
551 |
assert(r.ae(one)) |
|
552 |
||
553 |
# Abramowitz Table 16.5
|
|
554 |
# cn(K, q) = 0; K is K(k), first complete elliptic integral
|
|
555 |
equality = jcn(K, m) |
|
556 |
assert(equality.ae(0)) |
|
557 |
||
558 |
# Abramowitz Table 16.6.3
|
|
559 |
# dn(z, 0) = 1, m == 0
|
|
560 |
z = m |
|
561 |
value = jdn(z, zero) |
|
562 |
assert(value.ae(one)) |
|
563 |
||
564 |
mp.dps = 15 |
|
565 |
||
566 |
def test_sn_cn_dn_complex(): |
|
567 |
mp.dps = 30 |
|
568 |
# N[JacobiSN[1/4 + I/8, 1/3 + I/7], 35] in Mathematica
|
|
569 |
res = mpf('0.2495674401066275492326652143537') + \ |
|
570 |
mpf('0.12017344422863833381301051702823') * j |
|
571 |
u = mpf(1)/4 + j/8 |
|
572 |
m = mpf(1)/3 + j/7 |
|
573 |
r = jsn(u, m) |
|
574 |
assert(mpc_ae(r, res)) |
|
575 |
||
576 |
#N[JacobiCN[1/4 + I/8, 1/3 + I/7], 35]
|
|
577 |
res = mpf('0.9762691700944007312693721148331') - \ |
|
578 |
mpf('0.0307203994181623243583169154824')*j |
|
579 |
r = jcn(u, m) |
|
28
by dinko.metalac at gmail
new sympy |
580 |
#assert r.real.ae(res.real)
|
581 |
#assert r.imag.ae(res.imag)
|
|
10
by dinko.metalac at gmail
Added all necessary python files. Now it works on the device! |
582 |
assert(mpc_ae(r, res)) |
583 |
||
584 |
#N[JacobiDN[1/4 + I/8, 1/3 + I/7], 35]
|
|
585 |
res = mpf('0.99639490163039577560547478589753039') - \ |
|
586 |
mpf('0.01346296520008176393432491077244994')*j |
|
587 |
r = jdn(u, m) |
|
588 |
assert(mpc_ae(r, res)) |
|
589 |
mp.dps = 15 |
|
590 |
||
591 |
def test_elliptic_integrals(): |
|
592 |
# Test cases from Carlson's paper
|
|
593 |
mp.dps = 15 |
|
594 |
assert elliprd(0,2,1).ae(1.7972103521033883112) |
|
595 |
assert elliprd(2,3,4).ae(0.16510527294261053349) |
|
596 |
assert elliprd(j,-j,2).ae(0.65933854154219768919) |
|
597 |
assert elliprd(0,j,-j).ae(1.2708196271909686299 + 2.7811120159520578777j) |
|
598 |
assert elliprd(0,j-1,j).ae(-1.8577235439239060056 - 0.96193450888838559989j) |
|
599 |
assert elliprd(-2-j,-j,-1+j).ae(1.8249027393703805305 - 1.2218475784827035855j) |
|
600 |
for n in [5, 15, 30, 60, 100]: |
|
601 |
mp.dps = n |
|
602 |
assert elliprf(1,2,0).ae('1.3110287771460599052324197949455597068413774757158115814084108519003952935352071251151477664807145467230678763') |
|
603 |
assert elliprf(0.5,1,0).ae('1.854074677301371918433850347195260046217598823521766905585928045056021776838119978357271861650371897277771871') |
|
604 |
assert elliprf(j,-j,0).ae('1.854074677301371918433850347195260046217598823521766905585928045056021776838119978357271861650371897277771871') |
|
605 |
assert elliprf(j-1,j,0).ae(mpc('0.79612586584233913293056938229563057846592264089185680214929401744498956943287031832657642790719940442165621412', |
|
606 |
'-1.2138566698364959864300942567386038975419875860741507618279563735753073152507112254567291141460317931258599889')) |
|
607 |
assert elliprf(2,3,4).ae('0.58408284167715170669284916892566789240351359699303216166309375305508295130412919665541330837704050454472379308') |
|
608 |
assert elliprf(j,-j,2).ae('1.0441445654064360931078658361850779139591660747973017593275012615517220315993723776182276555339288363064476126') |
|
609 |
assert elliprf(j-1,j,1-j).ae(mpc('0.93912050218619371196624617169781141161485651998254431830645241993282941057500174238125105410055253623847335313', |
|
610 |
'-0.53296252018635269264859303449447908970360344322834582313172115220559316331271520508208025270300138589669326136')) |
|
611 |
assert elliprc(0,0.25).ae(+pi) |
|
612 |
assert elliprc(2.25,2).ae(+ln2) |
|
613 |
assert elliprc(0,j).ae(mpc('1.1107207345395915617539702475151734246536554223439225557713489017391086982748684776438317336911913093408525532', |
|
614 |
'-1.1107207345395915617539702475151734246536554223439225557713489017391086982748684776438317336911913093408525532')) |
|
615 |
assert elliprc(-j,j).ae(mpc('1.2260849569072198222319655083097718755633725139745941606203839524036426936825652935738621522906572884239069297', |
|
616 |
'-0.34471136988767679699935618332997956653521218571295874986708834375026550946053920574015526038040124556716711353')) |
|
617 |
assert elliprc(0.25,-2).ae(ln2/3) |
|
618 |
assert elliprc(j,-1).ae(mpc('0.77778596920447389875196055840799837589537035343923012237628610795937014001905822029050288316217145443865649819', |
|
619 |
'0.1983248499342877364755170948292130095921681309577950696116251029742793455964385947473103628983664877025779304')) |
|
620 |
assert elliprj(0,1,2,3).ae('0.77688623778582332014190282640545501102298064276022952731669118325952563819813258230708177398475643634103990878') |
|
621 |
assert elliprj(2,3,4,5).ae('0.14297579667156753833233879421985774801466647854232626336218889885463800128817976132826443904216546421431528308') |
|
622 |
assert elliprj(2,3,4,-1+j).ae(mpc('0.13613945827770535203521374457913768360237593025944342652613569368333226052158214183059386307242563164036672709', |
|
623 |
'-0.38207561624427164249600936454845112611060375760094156571007648297226090050927156176977091273224510621553615189')) |
|
624 |
assert elliprj(j,-j,0,2).ae('1.6490011662710884518243257224860232300246792717163891216346170272567376981346412066066050103935109581019055806') |
|
625 |
assert elliprj(-1+j,-1-j,1,2).ae('0.94148358841220238083044612133767270187474673547917988681610772381758628963408843935027667916713866133196845063') |
|
626 |
assert elliprj(j,-j,0,1-j).ae(mpc('1.8260115229009316249372594065790946657011067182850435297162034335356430755397401849070610280860044610878657501', |
|
627 |
'1.2290661908643471500163617732957042849283739403009556715926326841959667290840290081010472716420690899886276961')) |
|
628 |
assert elliprj(-1+j,-1-j,1,-3+j).ae(mpc('-0.61127970812028172123588152373622636829986597243716610650831553882054127570542477508023027578037045504958619422', |
|
629 |
'-1.0684038390006807880182112972232562745485871763154040245065581157751693730095703406209466903752930797510491155')) |
|
630 |
assert elliprj(-1+j,-2-j,-j,-1+j).ae(mpc('1.8249027393703805304622013339009022294368078659619988943515764258335975852685224202567854526307030593012768954', |
|
631 |
'-1.2218475784827035854568450371590419833166777535029296025352291308244564398645467465067845461070602841312456831')) |
|
632 |
||
633 |
assert elliprg(0,16,16).ae(+pi) |
|
634 |
assert elliprg(2,3,4).ae('1.7255030280692277601061148835701141842692457170470456590515892070736643637303053506944907685301315299153040991') |
|
635 |
assert elliprg(0,j,-j).ae('0.42360654239698954330324956174109581824072295516347109253028968632986700241706737986160014699730561497106114281') |
|
636 |
assert elliprg(j-1,j,0).ae(mpc('0.44660591677018372656731970402124510811555212083508861036067729944477855594654762496407405328607219895053798354', |
|
637 |
'0.70768352357515390073102719507612395221369717586839400605901402910893345301718731499237159587077682267374159282')) |
|
638 |
assert elliprg(-j,j-1,j).ae(mpc('0.36023392184473309033675652092928695596803358846377334894215349632203382573844427952830064383286995172598964266', |
|
639 |
'0.40348623401722113740956336997761033878615232917480045914551915169013722542827052849476969199578321834819903921')) |
|
640 |
assert elliprg(0, mpf('0.0796'), 4).ae('1.0284758090288040009838871385180217366569777284430590125081211090574701293154645750017813190805144572673802094') |
|
641 |
mp.dps = 15 |
|
642 |
||
28
by dinko.metalac at gmail
new sympy |
643 |
def test_issue_3297(): |
10
by dinko.metalac at gmail
Added all necessary python files. Now it works on the device! |
644 |
assert isnan(qfrom(m=nan)) |