applied-ai-018 commited on
Commit
36cb9f1
·
verified ·
1 Parent(s): f89dba6

Add files using upload-large-folder tool

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. llmeval-env/lib/python3.10/site-packages/mpmath/__init__.py +468 -0
  2. llmeval-env/lib/python3.10/site-packages/mpmath/ctx_base.py +494 -0
  3. llmeval-env/lib/python3.10/site-packages/mpmath/ctx_fp.py +253 -0
  4. llmeval-env/lib/python3.10/site-packages/mpmath/ctx_iv.py +551 -0
  5. llmeval-env/lib/python3.10/site-packages/mpmath/ctx_mp.py +1339 -0
  6. llmeval-env/lib/python3.10/site-packages/mpmath/ctx_mp_python.py +1149 -0
  7. llmeval-env/lib/python3.10/site-packages/mpmath/function_docs.py +0 -0
  8. llmeval-env/lib/python3.10/site-packages/mpmath/identification.py +844 -0
  9. llmeval-env/lib/python3.10/site-packages/mpmath/math2.py +672 -0
  10. llmeval-env/lib/python3.10/site-packages/mpmath/rational.py +240 -0
  11. llmeval-env/lib/python3.10/site-packages/mpmath/tests/__init__.py +0 -0
  12. llmeval-env/lib/python3.10/site-packages/mpmath/tests/__pycache__/test_basic_ops.cpython-310.pyc +0 -0
  13. llmeval-env/lib/python3.10/site-packages/mpmath/tests/__pycache__/test_convert.cpython-310.pyc +0 -0
  14. llmeval-env/lib/python3.10/site-packages/mpmath/tests/__pycache__/test_diff.cpython-310.pyc +0 -0
  15. llmeval-env/lib/python3.10/site-packages/mpmath/tests/__pycache__/test_division.cpython-310.pyc +0 -0
  16. llmeval-env/lib/python3.10/site-packages/mpmath/tests/__pycache__/test_eigen.cpython-310.pyc +0 -0
  17. llmeval-env/lib/python3.10/site-packages/mpmath/tests/__pycache__/test_functions2.cpython-310.pyc +0 -0
  18. llmeval-env/lib/python3.10/site-packages/mpmath/tests/__pycache__/test_hp.cpython-310.pyc +0 -0
  19. llmeval-env/lib/python3.10/site-packages/mpmath/tests/__pycache__/test_identify.cpython-310.pyc +0 -0
  20. llmeval-env/lib/python3.10/site-packages/mpmath/tests/__pycache__/test_levin.cpython-310.pyc +0 -0
  21. llmeval-env/lib/python3.10/site-packages/mpmath/tests/__pycache__/test_linalg.cpython-310.pyc +0 -0
  22. llmeval-env/lib/python3.10/site-packages/mpmath/tests/__pycache__/test_ode.cpython-310.pyc +0 -0
  23. llmeval-env/lib/python3.10/site-packages/mpmath/tests/extratest_gamma.py +215 -0
  24. llmeval-env/lib/python3.10/site-packages/mpmath/tests/extratest_zeta.py +30 -0
  25. llmeval-env/lib/python3.10/site-packages/mpmath/tests/runtests.py +161 -0
  26. llmeval-env/lib/python3.10/site-packages/mpmath/tests/test_basic_ops.py +451 -0
  27. llmeval-env/lib/python3.10/site-packages/mpmath/tests/test_bitwise.py +188 -0
  28. llmeval-env/lib/python3.10/site-packages/mpmath/tests/test_calculus.py +216 -0
  29. llmeval-env/lib/python3.10/site-packages/mpmath/tests/test_compatibility.py +77 -0
  30. llmeval-env/lib/python3.10/site-packages/mpmath/tests/test_convert.py +233 -0
  31. llmeval-env/lib/python3.10/site-packages/mpmath/tests/test_diff.py +61 -0
  32. llmeval-env/lib/python3.10/site-packages/mpmath/tests/test_division.py +143 -0
  33. llmeval-env/lib/python3.10/site-packages/mpmath/tests/test_eigen.py +179 -0
  34. llmeval-env/lib/python3.10/site-packages/mpmath/tests/test_eigen_symmetric.py +357 -0
  35. llmeval-env/lib/python3.10/site-packages/mpmath/tests/test_elliptic.py +670 -0
  36. llmeval-env/lib/python3.10/site-packages/mpmath/tests/test_fp.py +1671 -0
  37. llmeval-env/lib/python3.10/site-packages/mpmath/tests/test_functions.py +920 -0
  38. llmeval-env/lib/python3.10/site-packages/mpmath/tests/test_functions2.py +2384 -0
  39. llmeval-env/lib/python3.10/site-packages/mpmath/tests/test_gammazeta.py +698 -0
  40. llmeval-env/lib/python3.10/site-packages/mpmath/tests/test_hp.py +291 -0
  41. llmeval-env/lib/python3.10/site-packages/mpmath/tests/test_identify.py +19 -0
  42. llmeval-env/lib/python3.10/site-packages/mpmath/tests/test_interval.py +453 -0
  43. llmeval-env/lib/python3.10/site-packages/mpmath/tests/test_levin.py +153 -0
  44. llmeval-env/lib/python3.10/site-packages/mpmath/tests/test_linalg.py +332 -0
  45. llmeval-env/lib/python3.10/site-packages/mpmath/tests/test_matrices.py +253 -0
  46. llmeval-env/lib/python3.10/site-packages/mpmath/tests/test_mpmath.py +7 -0
  47. llmeval-env/lib/python3.10/site-packages/mpmath/tests/test_ode.py +73 -0
  48. llmeval-env/lib/python3.10/site-packages/mpmath/tests/test_pickle.py +27 -0
  49. llmeval-env/lib/python3.10/site-packages/mpmath/tests/test_power.py +156 -0
  50. llmeval-env/lib/python3.10/site-packages/mpmath/tests/test_quad.py +95 -0
llmeval-env/lib/python3.10/site-packages/mpmath/__init__.py ADDED
@@ -0,0 +1,468 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ __version__ = '1.3.0'
2
+
3
+ from .usertools import monitor, timing
4
+
5
+ from .ctx_fp import FPContext
6
+ from .ctx_mp import MPContext
7
+ from .ctx_iv import MPIntervalContext
8
+
9
+ fp = FPContext()
10
+ mp = MPContext()
11
+ iv = MPIntervalContext()
12
+
13
+ fp._mp = mp
14
+ mp._mp = mp
15
+ iv._mp = mp
16
+ mp._fp = fp
17
+ fp._fp = fp
18
+ mp._iv = iv
19
+ fp._iv = iv
20
+ iv._iv = iv
21
+
22
+ # XXX: extremely bad pickle hack
23
+ from . import ctx_mp as _ctx_mp
24
+ _ctx_mp._mpf_module.mpf = mp.mpf
25
+ _ctx_mp._mpf_module.mpc = mp.mpc
26
+
27
+ make_mpf = mp.make_mpf
28
+ make_mpc = mp.make_mpc
29
+
30
+ extraprec = mp.extraprec
31
+ extradps = mp.extradps
32
+ workprec = mp.workprec
33
+ workdps = mp.workdps
34
+ autoprec = mp.autoprec
35
+ maxcalls = mp.maxcalls
36
+ memoize = mp.memoize
37
+
38
+ mag = mp.mag
39
+
40
+ bernfrac = mp.bernfrac
41
+
42
+ qfrom = mp.qfrom
43
+ mfrom = mp.mfrom
44
+ kfrom = mp.kfrom
45
+ taufrom = mp.taufrom
46
+ qbarfrom = mp.qbarfrom
47
+ ellipfun = mp.ellipfun
48
+ jtheta = mp.jtheta
49
+ kleinj = mp.kleinj
50
+ eta = mp.eta
51
+
52
+ qp = mp.qp
53
+ qhyper = mp.qhyper
54
+ qgamma = mp.qgamma
55
+ qfac = mp.qfac
56
+
57
+ nint_distance = mp.nint_distance
58
+
59
+ plot = mp.plot
60
+ cplot = mp.cplot
61
+ splot = mp.splot
62
+
63
+ odefun = mp.odefun
64
+
65
+ jacobian = mp.jacobian
66
+ findroot = mp.findroot
67
+ multiplicity = mp.multiplicity
68
+
69
+ isinf = mp.isinf
70
+ isnan = mp.isnan
71
+ isnormal = mp.isnormal
72
+ isint = mp.isint
73
+ isfinite = mp.isfinite
74
+ almosteq = mp.almosteq
75
+ nan = mp.nan
76
+ rand = mp.rand
77
+
78
+ absmin = mp.absmin
79
+ absmax = mp.absmax
80
+
81
+ fraction = mp.fraction
82
+
83
+ linspace = mp.linspace
84
+ arange = mp.arange
85
+
86
+ mpmathify = convert = mp.convert
87
+ mpc = mp.mpc
88
+
89
+ mpi = iv._mpi
90
+
91
+ nstr = mp.nstr
92
+ nprint = mp.nprint
93
+ chop = mp.chop
94
+
95
+ fneg = mp.fneg
96
+ fadd = mp.fadd
97
+ fsub = mp.fsub
98
+ fmul = mp.fmul
99
+ fdiv = mp.fdiv
100
+ fprod = mp.fprod
101
+
102
+ quad = mp.quad
103
+ quadgl = mp.quadgl
104
+ quadts = mp.quadts
105
+ quadosc = mp.quadosc
106
+ quadsubdiv = mp.quadsubdiv
107
+
108
+ invertlaplace = mp.invertlaplace
109
+ invlaptalbot = mp.invlaptalbot
110
+ invlapstehfest = mp.invlapstehfest
111
+ invlapdehoog = mp.invlapdehoog
112
+
113
+ pslq = mp.pslq
114
+ identify = mp.identify
115
+ findpoly = mp.findpoly
116
+
117
+ richardson = mp.richardson
118
+ shanks = mp.shanks
119
+ levin = mp.levin
120
+ cohen_alt = mp.cohen_alt
121
+ nsum = mp.nsum
122
+ nprod = mp.nprod
123
+ difference = mp.difference
124
+ diff = mp.diff
125
+ diffs = mp.diffs
126
+ diffs_prod = mp.diffs_prod
127
+ diffs_exp = mp.diffs_exp
128
+ diffun = mp.diffun
129
+ differint = mp.differint
130
+ taylor = mp.taylor
131
+ pade = mp.pade
132
+ polyval = mp.polyval
133
+ polyroots = mp.polyroots
134
+ fourier = mp.fourier
135
+ fourierval = mp.fourierval
136
+ sumem = mp.sumem
137
+ sumap = mp.sumap
138
+ chebyfit = mp.chebyfit
139
+ limit = mp.limit
140
+
141
+ matrix = mp.matrix
142
+ eye = mp.eye
143
+ diag = mp.diag
144
+ zeros = mp.zeros
145
+ ones = mp.ones
146
+ hilbert = mp.hilbert
147
+ randmatrix = mp.randmatrix
148
+ swap_row = mp.swap_row
149
+ extend = mp.extend
150
+ norm = mp.norm
151
+ mnorm = mp.mnorm
152
+
153
+ lu_solve = mp.lu_solve
154
+ lu = mp.lu
155
+ qr = mp.qr
156
+ unitvector = mp.unitvector
157
+ inverse = mp.inverse
158
+ residual = mp.residual
159
+ qr_solve = mp.qr_solve
160
+ cholesky = mp.cholesky
161
+ cholesky_solve = mp.cholesky_solve
162
+ det = mp.det
163
+ cond = mp.cond
164
+ hessenberg = mp.hessenberg
165
+ schur = mp.schur
166
+ eig = mp.eig
167
+ eig_sort = mp.eig_sort
168
+ eigsy = mp.eigsy
169
+ eighe = mp.eighe
170
+ eigh = mp.eigh
171
+ svd_r = mp.svd_r
172
+ svd_c = mp.svd_c
173
+ svd = mp.svd
174
+ gauss_quadrature = mp.gauss_quadrature
175
+
176
+ expm = mp.expm
177
+ sqrtm = mp.sqrtm
178
+ powm = mp.powm
179
+ logm = mp.logm
180
+ sinm = mp.sinm
181
+ cosm = mp.cosm
182
+
183
+ mpf = mp.mpf
184
+ j = mp.j
185
+ exp = mp.exp
186
+ expj = mp.expj
187
+ expjpi = mp.expjpi
188
+ ln = mp.ln
189
+ im = mp.im
190
+ re = mp.re
191
+ inf = mp.inf
192
+ ninf = mp.ninf
193
+ sign = mp.sign
194
+
195
+ eps = mp.eps
196
+ pi = mp.pi
197
+ ln2 = mp.ln2
198
+ ln10 = mp.ln10
199
+ phi = mp.phi
200
+ e = mp.e
201
+ euler = mp.euler
202
+ catalan = mp.catalan
203
+ khinchin = mp.khinchin
204
+ glaisher = mp.glaisher
205
+ apery = mp.apery
206
+ degree = mp.degree
207
+ twinprime = mp.twinprime
208
+ mertens = mp.mertens
209
+
210
+ ldexp = mp.ldexp
211
+ frexp = mp.frexp
212
+
213
+ fsum = mp.fsum
214
+ fdot = mp.fdot
215
+
216
+ sqrt = mp.sqrt
217
+ cbrt = mp.cbrt
218
+ exp = mp.exp
219
+ ln = mp.ln
220
+ log = mp.log
221
+ log10 = mp.log10
222
+ power = mp.power
223
+ cos = mp.cos
224
+ sin = mp.sin
225
+ tan = mp.tan
226
+ cosh = mp.cosh
227
+ sinh = mp.sinh
228
+ tanh = mp.tanh
229
+ acos = mp.acos
230
+ asin = mp.asin
231
+ atan = mp.atan
232
+ asinh = mp.asinh
233
+ acosh = mp.acosh
234
+ atanh = mp.atanh
235
+ sec = mp.sec
236
+ csc = mp.csc
237
+ cot = mp.cot
238
+ sech = mp.sech
239
+ csch = mp.csch
240
+ coth = mp.coth
241
+ asec = mp.asec
242
+ acsc = mp.acsc
243
+ acot = mp.acot
244
+ asech = mp.asech
245
+ acsch = mp.acsch
246
+ acoth = mp.acoth
247
+ cospi = mp.cospi
248
+ sinpi = mp.sinpi
249
+ sinc = mp.sinc
250
+ sincpi = mp.sincpi
251
+ cos_sin = mp.cos_sin
252
+ cospi_sinpi = mp.cospi_sinpi
253
+ fabs = mp.fabs
254
+ re = mp.re
255
+ im = mp.im
256
+ conj = mp.conj
257
+ floor = mp.floor
258
+ ceil = mp.ceil
259
+ nint = mp.nint
260
+ frac = mp.frac
261
+ root = mp.root
262
+ nthroot = mp.nthroot
263
+ hypot = mp.hypot
264
+ fmod = mp.fmod
265
+ ldexp = mp.ldexp
266
+ frexp = mp.frexp
267
+ sign = mp.sign
268
+ arg = mp.arg
269
+ phase = mp.phase
270
+ polar = mp.polar
271
+ rect = mp.rect
272
+ degrees = mp.degrees
273
+ radians = mp.radians
274
+ atan2 = mp.atan2
275
+ fib = mp.fib
276
+ fibonacci = mp.fibonacci
277
+ lambertw = mp.lambertw
278
+ zeta = mp.zeta
279
+ altzeta = mp.altzeta
280
+ gamma = mp.gamma
281
+ rgamma = mp.rgamma
282
+ factorial = mp.factorial
283
+ fac = mp.fac
284
+ fac2 = mp.fac2
285
+ beta = mp.beta
286
+ betainc = mp.betainc
287
+ psi = mp.psi
288
+ #psi0 = mp.psi0
289
+ #psi1 = mp.psi1
290
+ #psi2 = mp.psi2
291
+ #psi3 = mp.psi3
292
+ polygamma = mp.polygamma
293
+ digamma = mp.digamma
294
+ #trigamma = mp.trigamma
295
+ #tetragamma = mp.tetragamma
296
+ #pentagamma = mp.pentagamma
297
+ harmonic = mp.harmonic
298
+ bernoulli = mp.bernoulli
299
+ bernfrac = mp.bernfrac
300
+ stieltjes = mp.stieltjes
301
+ hurwitz = mp.hurwitz
302
+ dirichlet = mp.dirichlet
303
+ bernpoly = mp.bernpoly
304
+ eulerpoly = mp.eulerpoly
305
+ eulernum = mp.eulernum
306
+ polylog = mp.polylog
307
+ clsin = mp.clsin
308
+ clcos = mp.clcos
309
+ gammainc = mp.gammainc
310
+ gammaprod = mp.gammaprod
311
+ binomial = mp.binomial
312
+ rf = mp.rf
313
+ ff = mp.ff
314
+ hyper = mp.hyper
315
+ hyp0f1 = mp.hyp0f1
316
+ hyp1f1 = mp.hyp1f1
317
+ hyp1f2 = mp.hyp1f2
318
+ hyp2f1 = mp.hyp2f1
319
+ hyp2f2 = mp.hyp2f2
320
+ hyp2f0 = mp.hyp2f0
321
+ hyp2f3 = mp.hyp2f3
322
+ hyp3f2 = mp.hyp3f2
323
+ hyperu = mp.hyperu
324
+ hypercomb = mp.hypercomb
325
+ meijerg = mp.meijerg
326
+ appellf1 = mp.appellf1
327
+ appellf2 = mp.appellf2
328
+ appellf3 = mp.appellf3
329
+ appellf4 = mp.appellf4
330
+ hyper2d = mp.hyper2d
331
+ bihyper = mp.bihyper
332
+ erf = mp.erf
333
+ erfc = mp.erfc
334
+ erfi = mp.erfi
335
+ erfinv = mp.erfinv
336
+ npdf = mp.npdf
337
+ ncdf = mp.ncdf
338
+ expint = mp.expint
339
+ e1 = mp.e1
340
+ ei = mp.ei
341
+ li = mp.li
342
+ ci = mp.ci
343
+ si = mp.si
344
+ chi = mp.chi
345
+ shi = mp.shi
346
+ fresnels = mp.fresnels
347
+ fresnelc = mp.fresnelc
348
+ airyai = mp.airyai
349
+ airybi = mp.airybi
350
+ airyaizero = mp.airyaizero
351
+ airybizero = mp.airybizero
352
+ scorergi = mp.scorergi
353
+ scorerhi = mp.scorerhi
354
+ ellipk = mp.ellipk
355
+ ellipe = mp.ellipe
356
+ ellipf = mp.ellipf
357
+ ellippi = mp.ellippi
358
+ elliprc = mp.elliprc
359
+ elliprj = mp.elliprj
360
+ elliprf = mp.elliprf
361
+ elliprd = mp.elliprd
362
+ elliprg = mp.elliprg
363
+ agm = mp.agm
364
+ jacobi = mp.jacobi
365
+ chebyt = mp.chebyt
366
+ chebyu = mp.chebyu
367
+ legendre = mp.legendre
368
+ legenp = mp.legenp
369
+ legenq = mp.legenq
370
+ hermite = mp.hermite
371
+ pcfd = mp.pcfd
372
+ pcfu = mp.pcfu
373
+ pcfv = mp.pcfv
374
+ pcfw = mp.pcfw
375
+ gegenbauer = mp.gegenbauer
376
+ laguerre = mp.laguerre
377
+ spherharm = mp.spherharm
378
+ besselj = mp.besselj
379
+ j0 = mp.j0
380
+ j1 = mp.j1
381
+ besseli = mp.besseli
382
+ bessely = mp.bessely
383
+ besselk = mp.besselk
384
+ besseljzero = mp.besseljzero
385
+ besselyzero = mp.besselyzero
386
+ hankel1 = mp.hankel1
387
+ hankel2 = mp.hankel2
388
+ struveh = mp.struveh
389
+ struvel = mp.struvel
390
+ angerj = mp.angerj
391
+ webere = mp.webere
392
+ lommels1 = mp.lommels1
393
+ lommels2 = mp.lommels2
394
+ whitm = mp.whitm
395
+ whitw = mp.whitw
396
+ ber = mp.ber
397
+ bei = mp.bei
398
+ ker = mp.ker
399
+ kei = mp.kei
400
+ coulombc = mp.coulombc
401
+ coulombf = mp.coulombf
402
+ coulombg = mp.coulombg
403
+ barnesg = mp.barnesg
404
+ superfac = mp.superfac
405
+ hyperfac = mp.hyperfac
406
+ loggamma = mp.loggamma
407
+ siegeltheta = mp.siegeltheta
408
+ siegelz = mp.siegelz
409
+ grampoint = mp.grampoint
410
+ zetazero = mp.zetazero
411
+ riemannr = mp.riemannr
412
+ primepi = mp.primepi
413
+ primepi2 = mp.primepi2
414
+ primezeta = mp.primezeta
415
+ bell = mp.bell
416
+ polyexp = mp.polyexp
417
+ expm1 = mp.expm1
418
+ log1p = mp.log1p
419
+ powm1 = mp.powm1
420
+ unitroots = mp.unitroots
421
+ cyclotomic = mp.cyclotomic
422
+ mangoldt = mp.mangoldt
423
+ secondzeta = mp.secondzeta
424
+ nzeros = mp.nzeros
425
+ backlunds = mp.backlunds
426
+ lerchphi = mp.lerchphi
427
+ stirling1 = mp.stirling1
428
+ stirling2 = mp.stirling2
429
+ squarew = mp.squarew
430
+ trianglew = mp.trianglew
431
+ sawtoothw = mp.sawtoothw
432
+ unit_triangle = mp.unit_triangle
433
+ sigmoid = mp.sigmoid
434
+
435
+ # be careful when changing this name, don't use test*!
436
+ def runtests():
437
+ """
438
+ Run all mpmath tests and print output.
439
+ """
440
+ import os.path
441
+ from inspect import getsourcefile
442
+ from .tests import runtests as tests
443
+ testdir = os.path.dirname(os.path.abspath(getsourcefile(tests)))
444
+ importdir = os.path.abspath(testdir + '/../..')
445
+ tests.testit(importdir, testdir)
446
+
447
+ def doctests(filter=[]):
448
+ import sys
449
+ from timeit import default_timer as clock
450
+ for i, arg in enumerate(sys.argv):
451
+ if '__init__.py' in arg:
452
+ filter = [sn for sn in sys.argv[i+1:] if not sn.startswith("-")]
453
+ break
454
+ import doctest
455
+ globs = globals().copy()
456
+ for obj in globs: #sorted(globs.keys()):
457
+ if filter:
458
+ if not sum([pat in obj for pat in filter]):
459
+ continue
460
+ sys.stdout.write(str(obj) + " ")
461
+ sys.stdout.flush()
462
+ t1 = clock()
463
+ doctest.run_docstring_examples(globs[obj], {}, verbose=("-v" in sys.argv))
464
+ t2 = clock()
465
+ print(round(t2-t1, 3))
466
+
467
+ if __name__ == '__main__':
468
+ doctests()
llmeval-env/lib/python3.10/site-packages/mpmath/ctx_base.py ADDED
@@ -0,0 +1,494 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from operator import gt, lt
2
+
3
+ from .libmp.backend import xrange
4
+
5
+ from .functions.functions import SpecialFunctions
6
+ from .functions.rszeta import RSCache
7
+ from .calculus.quadrature import QuadratureMethods
8
+ from .calculus.inverselaplace import LaplaceTransformInversionMethods
9
+ from .calculus.calculus import CalculusMethods
10
+ from .calculus.optimization import OptimizationMethods
11
+ from .calculus.odes import ODEMethods
12
+ from .matrices.matrices import MatrixMethods
13
+ from .matrices.calculus import MatrixCalculusMethods
14
+ from .matrices.linalg import LinearAlgebraMethods
15
+ from .matrices.eigen import Eigen
16
+ from .identification import IdentificationMethods
17
+ from .visualization import VisualizationMethods
18
+
19
+ from . import libmp
20
+
21
+ class Context(object):
22
+ pass
23
+
24
+ class StandardBaseContext(Context,
25
+ SpecialFunctions,
26
+ RSCache,
27
+ QuadratureMethods,
28
+ LaplaceTransformInversionMethods,
29
+ CalculusMethods,
30
+ MatrixMethods,
31
+ MatrixCalculusMethods,
32
+ LinearAlgebraMethods,
33
+ Eigen,
34
+ IdentificationMethods,
35
+ OptimizationMethods,
36
+ ODEMethods,
37
+ VisualizationMethods):
38
+
39
+ NoConvergence = libmp.NoConvergence
40
+ ComplexResult = libmp.ComplexResult
41
+
42
+ def __init__(ctx):
43
+ ctx._aliases = {}
44
+ # Call those that need preinitialization (e.g. for wrappers)
45
+ SpecialFunctions.__init__(ctx)
46
+ RSCache.__init__(ctx)
47
+ QuadratureMethods.__init__(ctx)
48
+ LaplaceTransformInversionMethods.__init__(ctx)
49
+ CalculusMethods.__init__(ctx)
50
+ MatrixMethods.__init__(ctx)
51
+
52
+ def _init_aliases(ctx):
53
+ for alias, value in ctx._aliases.items():
54
+ try:
55
+ setattr(ctx, alias, getattr(ctx, value))
56
+ except AttributeError:
57
+ pass
58
+
59
+ _fixed_precision = False
60
+
61
+ # XXX
62
+ verbose = False
63
+
64
+ def warn(ctx, msg):
65
+ print("Warning:", msg)
66
+
67
+ def bad_domain(ctx, msg):
68
+ raise ValueError(msg)
69
+
70
+ def _re(ctx, x):
71
+ if hasattr(x, "real"):
72
+ return x.real
73
+ return x
74
+
75
+ def _im(ctx, x):
76
+ if hasattr(x, "imag"):
77
+ return x.imag
78
+ return ctx.zero
79
+
80
+ def _as_points(ctx, x):
81
+ return x
82
+
83
+ def fneg(ctx, x, **kwargs):
84
+ return -ctx.convert(x)
85
+
86
+ def fadd(ctx, x, y, **kwargs):
87
+ return ctx.convert(x)+ctx.convert(y)
88
+
89
+ def fsub(ctx, x, y, **kwargs):
90
+ return ctx.convert(x)-ctx.convert(y)
91
+
92
+ def fmul(ctx, x, y, **kwargs):
93
+ return ctx.convert(x)*ctx.convert(y)
94
+
95
+ def fdiv(ctx, x, y, **kwargs):
96
+ return ctx.convert(x)/ctx.convert(y)
97
+
98
+ def fsum(ctx, args, absolute=False, squared=False):
99
+ if absolute:
100
+ if squared:
101
+ return sum((abs(x)**2 for x in args), ctx.zero)
102
+ return sum((abs(x) for x in args), ctx.zero)
103
+ if squared:
104
+ return sum((x**2 for x in args), ctx.zero)
105
+ return sum(args, ctx.zero)
106
+
107
+ def fdot(ctx, xs, ys=None, conjugate=False):
108
+ if ys is not None:
109
+ xs = zip(xs, ys)
110
+ if conjugate:
111
+ cf = ctx.conj
112
+ return sum((x*cf(y) for (x,y) in xs), ctx.zero)
113
+ else:
114
+ return sum((x*y for (x,y) in xs), ctx.zero)
115
+
116
+ def fprod(ctx, args):
117
+ prod = ctx.one
118
+ for arg in args:
119
+ prod *= arg
120
+ return prod
121
+
122
+ def nprint(ctx, x, n=6, **kwargs):
123
+ """
124
+ Equivalent to ``print(nstr(x, n))``.
125
+ """
126
+ print(ctx.nstr(x, n, **kwargs))
127
+
128
+ def chop(ctx, x, tol=None):
129
+ """
130
+ Chops off small real or imaginary parts, or converts
131
+ numbers close to zero to exact zeros. The input can be a
132
+ single number or an iterable::
133
+
134
+ >>> from mpmath import *
135
+ >>> mp.dps = 15; mp.pretty = False
136
+ >>> chop(5+1e-10j, tol=1e-9)
137
+ mpf('5.0')
138
+ >>> nprint(chop([1.0, 1e-20, 3+1e-18j, -4, 2]))
139
+ [1.0, 0.0, 3.0, -4.0, 2.0]
140
+
141
+ The tolerance defaults to ``100*eps``.
142
+ """
143
+ if tol is None:
144
+ tol = 100*ctx.eps
145
+ try:
146
+ x = ctx.convert(x)
147
+ absx = abs(x)
148
+ if abs(x) < tol:
149
+ return ctx.zero
150
+ if ctx._is_complex_type(x):
151
+ #part_tol = min(tol, absx*tol)
152
+ part_tol = max(tol, absx*tol)
153
+ if abs(x.imag) < part_tol:
154
+ return x.real
155
+ if abs(x.real) < part_tol:
156
+ return ctx.mpc(0, x.imag)
157
+ except TypeError:
158
+ if isinstance(x, ctx.matrix):
159
+ return x.apply(lambda a: ctx.chop(a, tol))
160
+ if hasattr(x, "__iter__"):
161
+ return [ctx.chop(a, tol) for a in x]
162
+ return x
163
+
164
+ def almosteq(ctx, s, t, rel_eps=None, abs_eps=None):
165
+ r"""
166
+ Determine whether the difference between `s` and `t` is smaller
167
+ than a given epsilon, either relatively or absolutely.
168
+
169
+ Both a maximum relative difference and a maximum difference
170
+ ('epsilons') may be specified. The absolute difference is
171
+ defined as `|s-t|` and the relative difference is defined
172
+ as `|s-t|/\max(|s|, |t|)`.
173
+
174
+ If only one epsilon is given, both are set to the same value.
175
+ If none is given, both epsilons are set to `2^{-p+m}` where
176
+ `p` is the current working precision and `m` is a small
177
+ integer. The default setting typically allows :func:`~mpmath.almosteq`
178
+ to be used to check for mathematical equality
179
+ in the presence of small rounding errors.
180
+
181
+ **Examples**
182
+
183
+ >>> from mpmath import *
184
+ >>> mp.dps = 15
185
+ >>> almosteq(3.141592653589793, 3.141592653589790)
186
+ True
187
+ >>> almosteq(3.141592653589793, 3.141592653589700)
188
+ False
189
+ >>> almosteq(3.141592653589793, 3.141592653589700, 1e-10)
190
+ True
191
+ >>> almosteq(1e-20, 2e-20)
192
+ True
193
+ >>> almosteq(1e-20, 2e-20, rel_eps=0, abs_eps=0)
194
+ False
195
+
196
+ """
197
+ t = ctx.convert(t)
198
+ if abs_eps is None and rel_eps is None:
199
+ rel_eps = abs_eps = ctx.ldexp(1, -ctx.prec+4)
200
+ if abs_eps is None:
201
+ abs_eps = rel_eps
202
+ elif rel_eps is None:
203
+ rel_eps = abs_eps
204
+ diff = abs(s-t)
205
+ if diff <= abs_eps:
206
+ return True
207
+ abss = abs(s)
208
+ abst = abs(t)
209
+ if abss < abst:
210
+ err = diff/abst
211
+ else:
212
+ err = diff/abss
213
+ return err <= rel_eps
214
+
215
+ def arange(ctx, *args):
216
+ r"""
217
+ This is a generalized version of Python's :func:`~mpmath.range` function
218
+ that accepts fractional endpoints and step sizes and
219
+ returns a list of ``mpf`` instances. Like :func:`~mpmath.range`,
220
+ :func:`~mpmath.arange` can be called with 1, 2 or 3 arguments:
221
+
222
+ ``arange(b)``
223
+ `[0, 1, 2, \ldots, x]`
224
+ ``arange(a, b)``
225
+ `[a, a+1, a+2, \ldots, x]`
226
+ ``arange(a, b, h)``
227
+ `[a, a+h, a+h, \ldots, x]`
228
+
229
+ where `b-1 \le x < b` (in the third case, `b-h \le x < b`).
230
+
231
+ Like Python's :func:`~mpmath.range`, the endpoint is not included. To
232
+ produce ranges where the endpoint is included, :func:`~mpmath.linspace`
233
+ is more convenient.
234
+
235
+ **Examples**
236
+
237
+ >>> from mpmath import *
238
+ >>> mp.dps = 15; mp.pretty = False
239
+ >>> arange(4)
240
+ [mpf('0.0'), mpf('1.0'), mpf('2.0'), mpf('3.0')]
241
+ >>> arange(1, 2, 0.25)
242
+ [mpf('1.0'), mpf('1.25'), mpf('1.5'), mpf('1.75')]
243
+ >>> arange(1, -1, -0.75)
244
+ [mpf('1.0'), mpf('0.25'), mpf('-0.5')]
245
+
246
+ """
247
+ if not len(args) <= 3:
248
+ raise TypeError('arange expected at most 3 arguments, got %i'
249
+ % len(args))
250
+ if not len(args) >= 1:
251
+ raise TypeError('arange expected at least 1 argument, got %i'
252
+ % len(args))
253
+ # set default
254
+ a = 0
255
+ dt = 1
256
+ # interpret arguments
257
+ if len(args) == 1:
258
+ b = args[0]
259
+ elif len(args) >= 2:
260
+ a = args[0]
261
+ b = args[1]
262
+ if len(args) == 3:
263
+ dt = args[2]
264
+ a, b, dt = ctx.mpf(a), ctx.mpf(b), ctx.mpf(dt)
265
+ assert a + dt != a, 'dt is too small and would cause an infinite loop'
266
+ # adapt code for sign of dt
267
+ if a > b:
268
+ if dt > 0:
269
+ return []
270
+ op = gt
271
+ else:
272
+ if dt < 0:
273
+ return []
274
+ op = lt
275
+ # create list
276
+ result = []
277
+ i = 0
278
+ t = a
279
+ while 1:
280
+ t = a + dt*i
281
+ i += 1
282
+ if op(t, b):
283
+ result.append(t)
284
+ else:
285
+ break
286
+ return result
287
+
288
+ def linspace(ctx, *args, **kwargs):
289
+ """
290
+ ``linspace(a, b, n)`` returns a list of `n` evenly spaced
291
+ samples from `a` to `b`. The syntax ``linspace(mpi(a,b), n)``
292
+ is also valid.
293
+
294
+ This function is often more convenient than :func:`~mpmath.arange`
295
+ for partitioning an interval into subintervals, since
296
+ the endpoint is included::
297
+
298
+ >>> from mpmath import *
299
+ >>> mp.dps = 15; mp.pretty = False
300
+ >>> linspace(1, 4, 4)
301
+ [mpf('1.0'), mpf('2.0'), mpf('3.0'), mpf('4.0')]
302
+
303
+ You may also provide the keyword argument ``endpoint=False``::
304
+
305
+ >>> linspace(1, 4, 4, endpoint=False)
306
+ [mpf('1.0'), mpf('1.75'), mpf('2.5'), mpf('3.25')]
307
+
308
+ """
309
+ if len(args) == 3:
310
+ a = ctx.mpf(args[0])
311
+ b = ctx.mpf(args[1])
312
+ n = int(args[2])
313
+ elif len(args) == 2:
314
+ assert hasattr(args[0], '_mpi_')
315
+ a = args[0].a
316
+ b = args[0].b
317
+ n = int(args[1])
318
+ else:
319
+ raise TypeError('linspace expected 2 or 3 arguments, got %i' \
320
+ % len(args))
321
+ if n < 1:
322
+ raise ValueError('n must be greater than 0')
323
+ if not 'endpoint' in kwargs or kwargs['endpoint']:
324
+ if n == 1:
325
+ return [ctx.mpf(a)]
326
+ step = (b - a) / ctx.mpf(n - 1)
327
+ y = [i*step + a for i in xrange(n)]
328
+ y[-1] = b
329
+ else:
330
+ step = (b - a) / ctx.mpf(n)
331
+ y = [i*step + a for i in xrange(n)]
332
+ return y
333
+
334
+ def cos_sin(ctx, z, **kwargs):
335
+ return ctx.cos(z, **kwargs), ctx.sin(z, **kwargs)
336
+
337
+ def cospi_sinpi(ctx, z, **kwargs):
338
+ return ctx.cospi(z, **kwargs), ctx.sinpi(z, **kwargs)
339
+
340
+ def _default_hyper_maxprec(ctx, p):
341
+ return int(1000 * p**0.25 + 4*p)
342
+
343
+ _gcd = staticmethod(libmp.gcd)
344
+ list_primes = staticmethod(libmp.list_primes)
345
+ isprime = staticmethod(libmp.isprime)
346
+ bernfrac = staticmethod(libmp.bernfrac)
347
+ moebius = staticmethod(libmp.moebius)
348
+ _ifac = staticmethod(libmp.ifac)
349
+ _eulernum = staticmethod(libmp.eulernum)
350
+ _stirling1 = staticmethod(libmp.stirling1)
351
+ _stirling2 = staticmethod(libmp.stirling2)
352
+
353
+ def sum_accurately(ctx, terms, check_step=1):
354
+ prec = ctx.prec
355
+ try:
356
+ extraprec = 10
357
+ while 1:
358
+ ctx.prec = prec + extraprec + 5
359
+ max_mag = ctx.ninf
360
+ s = ctx.zero
361
+ k = 0
362
+ for term in terms():
363
+ s += term
364
+ if (not k % check_step) and term:
365
+ term_mag = ctx.mag(term)
366
+ max_mag = max(max_mag, term_mag)
367
+ sum_mag = ctx.mag(s)
368
+ if sum_mag - term_mag > ctx.prec:
369
+ break
370
+ k += 1
371
+ cancellation = max_mag - sum_mag
372
+ if cancellation != cancellation:
373
+ break
374
+ if cancellation < extraprec or ctx._fixed_precision:
375
+ break
376
+ extraprec += min(ctx.prec, cancellation)
377
+ return s
378
+ finally:
379
+ ctx.prec = prec
380
+
381
+ def mul_accurately(ctx, factors, check_step=1):
382
+ prec = ctx.prec
383
+ try:
384
+ extraprec = 10
385
+ while 1:
386
+ ctx.prec = prec + extraprec + 5
387
+ max_mag = ctx.ninf
388
+ one = ctx.one
389
+ s = one
390
+ k = 0
391
+ for factor in factors():
392
+ s *= factor
393
+ term = factor - one
394
+ if (not k % check_step):
395
+ term_mag = ctx.mag(term)
396
+ max_mag = max(max_mag, term_mag)
397
+ sum_mag = ctx.mag(s-one)
398
+ #if sum_mag - term_mag > ctx.prec:
399
+ # break
400
+ if -term_mag > ctx.prec:
401
+ break
402
+ k += 1
403
+ cancellation = max_mag - sum_mag
404
+ if cancellation != cancellation:
405
+ break
406
+ if cancellation < extraprec or ctx._fixed_precision:
407
+ break
408
+ extraprec += min(ctx.prec, cancellation)
409
+ return s
410
+ finally:
411
+ ctx.prec = prec
412
+
413
+ def power(ctx, x, y):
414
+ r"""Converts `x` and `y` to mpmath numbers and evaluates
415
+ `x^y = \exp(y \log(x))`::
416
+
417
+ >>> from mpmath import *
418
+ >>> mp.dps = 30; mp.pretty = True
419
+ >>> power(2, 0.5)
420
+ 1.41421356237309504880168872421
421
+
422
+ This shows the leading few digits of a large Mersenne prime
423
+ (performing the exact calculation ``2**43112609-1`` and
424
+ displaying the result in Python would be very slow)::
425
+
426
+ >>> power(2, 43112609)-1
427
+ 3.16470269330255923143453723949e+12978188
428
+ """
429
+ return ctx.convert(x) ** ctx.convert(y)
430
+
431
+ def _zeta_int(ctx, n):
432
+ return ctx.zeta(n)
433
+
434
+ def maxcalls(ctx, f, N):
435
+ """
436
+ Return a wrapped copy of *f* that raises ``NoConvergence`` when *f*
437
+ has been called more than *N* times::
438
+
439
+ >>> from mpmath import *
440
+ >>> mp.dps = 15
441
+ >>> f = maxcalls(sin, 10)
442
+ >>> print(sum(f(n) for n in range(10)))
443
+ 1.95520948210738
444
+ >>> f(10) # doctest: +IGNORE_EXCEPTION_DETAIL
445
+ Traceback (most recent call last):
446
+ ...
447
+ NoConvergence: maxcalls: function evaluated 10 times
448
+
449
+ """
450
+ counter = [0]
451
+ def f_maxcalls_wrapped(*args, **kwargs):
452
+ counter[0] += 1
453
+ if counter[0] > N:
454
+ raise ctx.NoConvergence("maxcalls: function evaluated %i times" % N)
455
+ return f(*args, **kwargs)
456
+ return f_maxcalls_wrapped
457
+
458
+ def memoize(ctx, f):
459
+ """
460
+ Return a wrapped copy of *f* that caches computed values, i.e.
461
+ a memoized copy of *f*. Values are only reused if the cached precision
462
+ is equal to or higher than the working precision::
463
+
464
+ >>> from mpmath import *
465
+ >>> mp.dps = 15; mp.pretty = True
466
+ >>> f = memoize(maxcalls(sin, 1))
467
+ >>> f(2)
468
+ 0.909297426825682
469
+ >>> f(2)
470
+ 0.909297426825682
471
+ >>> mp.dps = 25
472
+ >>> f(2) # doctest: +IGNORE_EXCEPTION_DETAIL
473
+ Traceback (most recent call last):
474
+ ...
475
+ NoConvergence: maxcalls: function evaluated 1 times
476
+
477
+ """
478
+ f_cache = {}
479
+ def f_cached(*args, **kwargs):
480
+ if kwargs:
481
+ key = args, tuple(kwargs.items())
482
+ else:
483
+ key = args
484
+ prec = ctx.prec
485
+ if key in f_cache:
486
+ cprec, cvalue = f_cache[key]
487
+ if cprec >= prec:
488
+ return +cvalue
489
+ value = f(*args, **kwargs)
490
+ f_cache[key] = (prec, value)
491
+ return value
492
+ f_cached.__name__ = f.__name__
493
+ f_cached.__doc__ = f.__doc__
494
+ return f_cached
llmeval-env/lib/python3.10/site-packages/mpmath/ctx_fp.py ADDED
@@ -0,0 +1,253 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from .ctx_base import StandardBaseContext
2
+
3
+ import math
4
+ import cmath
5
+ from . import math2
6
+
7
+ from . import function_docs
8
+
9
+ from .libmp import mpf_bernoulli, to_float, int_types
10
+ from . import libmp
11
+
12
+ class FPContext(StandardBaseContext):
13
+ """
14
+ Context for fast low-precision arithmetic (53-bit precision, giving at most
15
+ about 15-digit accuracy), using Python's builtin float and complex.
16
+ """
17
+
18
+ def __init__(ctx):
19
+ StandardBaseContext.__init__(ctx)
20
+
21
+ # Override SpecialFunctions implementation
22
+ ctx.loggamma = math2.loggamma
23
+ ctx._bernoulli_cache = {}
24
+ ctx.pretty = False
25
+
26
+ ctx._init_aliases()
27
+
28
+ _mpq = lambda cls, x: float(x[0])/x[1]
29
+
30
+ NoConvergence = libmp.NoConvergence
31
+
32
+ def _get_prec(ctx): return 53
33
+ def _set_prec(ctx, p): return
34
+ def _get_dps(ctx): return 15
35
+ def _set_dps(ctx, p): return
36
+
37
+ _fixed_precision = True
38
+
39
+ prec = property(_get_prec, _set_prec)
40
+ dps = property(_get_dps, _set_dps)
41
+
42
+ zero = 0.0
43
+ one = 1.0
44
+ eps = math2.EPS
45
+ inf = math2.INF
46
+ ninf = math2.NINF
47
+ nan = math2.NAN
48
+ j = 1j
49
+
50
+ # Called by SpecialFunctions.__init__()
51
+ @classmethod
52
+ def _wrap_specfun(cls, name, f, wrap):
53
+ if wrap:
54
+ def f_wrapped(ctx, *args, **kwargs):
55
+ convert = ctx.convert
56
+ args = [convert(a) for a in args]
57
+ return f(ctx, *args, **kwargs)
58
+ else:
59
+ f_wrapped = f
60
+ f_wrapped.__doc__ = function_docs.__dict__.get(name, f.__doc__)
61
+ setattr(cls, name, f_wrapped)
62
+
63
+ def bernoulli(ctx, n):
64
+ cache = ctx._bernoulli_cache
65
+ if n in cache:
66
+ return cache[n]
67
+ cache[n] = to_float(mpf_bernoulli(n, 53, 'n'), strict=True)
68
+ return cache[n]
69
+
70
+ pi = math2.pi
71
+ e = math2.e
72
+ euler = math2.euler
73
+ sqrt2 = 1.4142135623730950488
74
+ sqrt5 = 2.2360679774997896964
75
+ phi = 1.6180339887498948482
76
+ ln2 = 0.69314718055994530942
77
+ ln10 = 2.302585092994045684
78
+ euler = 0.57721566490153286061
79
+ catalan = 0.91596559417721901505
80
+ khinchin = 2.6854520010653064453
81
+ apery = 1.2020569031595942854
82
+ glaisher = 1.2824271291006226369
83
+
84
+ absmin = absmax = abs
85
+
86
+ def is_special(ctx, x):
87
+ return x - x != 0.0
88
+
89
+ def isnan(ctx, x):
90
+ return x != x
91
+
92
+ def isinf(ctx, x):
93
+ return abs(x) == math2.INF
94
+
95
+ def isnormal(ctx, x):
96
+ if x:
97
+ return x - x == 0.0
98
+ return False
99
+
100
+ def isnpint(ctx, x):
101
+ if type(x) is complex:
102
+ if x.imag:
103
+ return False
104
+ x = x.real
105
+ return x <= 0.0 and round(x) == x
106
+
107
+ mpf = float
108
+ mpc = complex
109
+
110
+ def convert(ctx, x):
111
+ try:
112
+ return float(x)
113
+ except:
114
+ return complex(x)
115
+
116
+ power = staticmethod(math2.pow)
117
+ sqrt = staticmethod(math2.sqrt)
118
+ exp = staticmethod(math2.exp)
119
+ ln = log = staticmethod(math2.log)
120
+ cos = staticmethod(math2.cos)
121
+ sin = staticmethod(math2.sin)
122
+ tan = staticmethod(math2.tan)
123
+ cos_sin = staticmethod(math2.cos_sin)
124
+ acos = staticmethod(math2.acos)
125
+ asin = staticmethod(math2.asin)
126
+ atan = staticmethod(math2.atan)
127
+ cosh = staticmethod(math2.cosh)
128
+ sinh = staticmethod(math2.sinh)
129
+ tanh = staticmethod(math2.tanh)
130
+ gamma = staticmethod(math2.gamma)
131
+ rgamma = staticmethod(math2.rgamma)
132
+ fac = factorial = staticmethod(math2.factorial)
133
+ floor = staticmethod(math2.floor)
134
+ ceil = staticmethod(math2.ceil)
135
+ cospi = staticmethod(math2.cospi)
136
+ sinpi = staticmethod(math2.sinpi)
137
+ cbrt = staticmethod(math2.cbrt)
138
+ _nthroot = staticmethod(math2.nthroot)
139
+ _ei = staticmethod(math2.ei)
140
+ _e1 = staticmethod(math2.e1)
141
+ _zeta = _zeta_int = staticmethod(math2.zeta)
142
+
143
+ # XXX: math2
144
+ def arg(ctx, z):
145
+ z = complex(z)
146
+ return math.atan2(z.imag, z.real)
147
+
148
+ def expj(ctx, x):
149
+ return ctx.exp(ctx.j*x)
150
+
151
+ def expjpi(ctx, x):
152
+ return ctx.exp(ctx.j*ctx.pi*x)
153
+
154
+ ldexp = math.ldexp
155
+ frexp = math.frexp
156
+
157
+ def mag(ctx, z):
158
+ if z:
159
+ return ctx.frexp(abs(z))[1]
160
+ return ctx.ninf
161
+
162
+ def isint(ctx, z):
163
+ if hasattr(z, "imag"): # float/int don't have .real/.imag in py2.5
164
+ if z.imag:
165
+ return False
166
+ z = z.real
167
+ try:
168
+ return z == int(z)
169
+ except:
170
+ return False
171
+
172
+ def nint_distance(ctx, z):
173
+ if hasattr(z, "imag"): # float/int don't have .real/.imag in py2.5
174
+ n = round(z.real)
175
+ else:
176
+ n = round(z)
177
+ if n == z:
178
+ return n, ctx.ninf
179
+ return n, ctx.mag(abs(z-n))
180
+
181
+ def _convert_param(ctx, z):
182
+ if type(z) is tuple:
183
+ p, q = z
184
+ return ctx.mpf(p) / q, 'R'
185
+ if hasattr(z, "imag"): # float/int don't have .real/.imag in py2.5
186
+ intz = int(z.real)
187
+ else:
188
+ intz = int(z)
189
+ if z == intz:
190
+ return intz, 'Z'
191
+ return z, 'R'
192
+
193
+ def _is_real_type(ctx, z):
194
+ return isinstance(z, float) or isinstance(z, int_types)
195
+
196
+ def _is_complex_type(ctx, z):
197
+ return isinstance(z, complex)
198
+
199
+ def hypsum(ctx, p, q, types, coeffs, z, maxterms=6000, **kwargs):
200
+ coeffs = list(coeffs)
201
+ num = range(p)
202
+ den = range(p,p+q)
203
+ tol = ctx.eps
204
+ s = t = 1.0
205
+ k = 0
206
+ while 1:
207
+ for i in num: t *= (coeffs[i]+k)
208
+ for i in den: t /= (coeffs[i]+k)
209
+ k += 1; t /= k; t *= z; s += t
210
+ if abs(t) < tol:
211
+ return s
212
+ if k > maxterms:
213
+ raise ctx.NoConvergence
214
+
215
+ def atan2(ctx, x, y):
216
+ return math.atan2(x, y)
217
+
218
+ def psi(ctx, m, z):
219
+ m = int(m)
220
+ if m == 0:
221
+ return ctx.digamma(z)
222
+ return (-1)**(m+1) * ctx.fac(m) * ctx.zeta(m+1, z)
223
+
224
+ digamma = staticmethod(math2.digamma)
225
+
226
+ def harmonic(ctx, x):
227
+ x = ctx.convert(x)
228
+ if x == 0 or x == 1:
229
+ return x
230
+ return ctx.digamma(x+1) + ctx.euler
231
+
232
+ nstr = str
233
+
234
+ def to_fixed(ctx, x, prec):
235
+ return int(math.ldexp(x, prec))
236
+
237
+ def rand(ctx):
238
+ import random
239
+ return random.random()
240
+
241
+ _erf = staticmethod(math2.erf)
242
+ _erfc = staticmethod(math2.erfc)
243
+
244
+ def sum_accurately(ctx, terms, check_step=1):
245
+ s = ctx.zero
246
+ k = 0
247
+ for term in terms():
248
+ s += term
249
+ if (not k % check_step) and term:
250
+ if abs(term) <= 1e-18*abs(s):
251
+ break
252
+ k += 1
253
+ return s
llmeval-env/lib/python3.10/site-packages/mpmath/ctx_iv.py ADDED
@@ -0,0 +1,551 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import operator
2
+
3
+ from . import libmp
4
+
5
+ from .libmp.backend import basestring
6
+
7
+ from .libmp import (
8
+ int_types, MPZ_ONE,
9
+ prec_to_dps, dps_to_prec, repr_dps,
10
+ round_floor, round_ceiling,
11
+ fzero, finf, fninf, fnan,
12
+ mpf_le, mpf_neg,
13
+ from_int, from_float, from_str, from_rational,
14
+ mpi_mid, mpi_delta, mpi_str,
15
+ mpi_abs, mpi_pos, mpi_neg, mpi_add, mpi_sub,
16
+ mpi_mul, mpi_div, mpi_pow_int, mpi_pow,
17
+ mpi_from_str,
18
+ mpci_pos, mpci_neg, mpci_add, mpci_sub, mpci_mul, mpci_div, mpci_pow,
19
+ mpci_abs, mpci_pow, mpci_exp, mpci_log,
20
+ ComplexResult,
21
+ mpf_hash, mpc_hash)
22
+ from .matrices.matrices import _matrix
23
+
24
+ mpi_zero = (fzero, fzero)
25
+
26
+ from .ctx_base import StandardBaseContext
27
+
28
+ new = object.__new__
29
+
30
+ def convert_mpf_(x, prec, rounding):
31
+ if hasattr(x, "_mpf_"): return x._mpf_
32
+ if isinstance(x, int_types): return from_int(x, prec, rounding)
33
+ if isinstance(x, float): return from_float(x, prec, rounding)
34
+ if isinstance(x, basestring): return from_str(x, prec, rounding)
35
+ raise NotImplementedError
36
+
37
+
38
+ class ivmpf(object):
39
+ """
40
+ Interval arithmetic class. Precision is controlled by iv.prec.
41
+ """
42
+
43
+ def __new__(cls, x=0):
44
+ return cls.ctx.convert(x)
45
+
46
+ def cast(self, cls, f_convert):
47
+ a, b = self._mpi_
48
+ if a == b:
49
+ return cls(f_convert(a))
50
+ raise ValueError
51
+
52
+ def __int__(self):
53
+ return self.cast(int, libmp.to_int)
54
+
55
+ def __float__(self):
56
+ return self.cast(float, libmp.to_float)
57
+
58
+ def __complex__(self):
59
+ return self.cast(complex, libmp.to_float)
60
+
61
+ def __hash__(self):
62
+ a, b = self._mpi_
63
+ if a == b:
64
+ return mpf_hash(a)
65
+ else:
66
+ return hash(self._mpi_)
67
+
68
+ @property
69
+ def real(self): return self
70
+
71
+ @property
72
+ def imag(self): return self.ctx.zero
73
+
74
+ def conjugate(self): return self
75
+
76
+ @property
77
+ def a(self):
78
+ a, b = self._mpi_
79
+ return self.ctx.make_mpf((a, a))
80
+
81
+ @property
82
+ def b(self):
83
+ a, b = self._mpi_
84
+ return self.ctx.make_mpf((b, b))
85
+
86
+ @property
87
+ def mid(self):
88
+ ctx = self.ctx
89
+ v = mpi_mid(self._mpi_, ctx.prec)
90
+ return ctx.make_mpf((v, v))
91
+
92
+ @property
93
+ def delta(self):
94
+ ctx = self.ctx
95
+ v = mpi_delta(self._mpi_, ctx.prec)
96
+ return ctx.make_mpf((v,v))
97
+
98
+ @property
99
+ def _mpci_(self):
100
+ return self._mpi_, mpi_zero
101
+
102
+ def _compare(*args):
103
+ raise TypeError("no ordering relation is defined for intervals")
104
+
105
+ __gt__ = _compare
106
+ __le__ = _compare
107
+ __gt__ = _compare
108
+ __ge__ = _compare
109
+
110
+ def __contains__(self, t):
111
+ t = self.ctx.mpf(t)
112
+ return (self.a <= t.a) and (t.b <= self.b)
113
+
114
+ def __str__(self):
115
+ return mpi_str(self._mpi_, self.ctx.prec)
116
+
117
+ def __repr__(self):
118
+ if self.ctx.pretty:
119
+ return str(self)
120
+ a, b = self._mpi_
121
+ n = repr_dps(self.ctx.prec)
122
+ a = libmp.to_str(a, n)
123
+ b = libmp.to_str(b, n)
124
+ return "mpi(%r, %r)" % (a, b)
125
+
126
+ def _compare(s, t, cmpfun):
127
+ if not hasattr(t, "_mpi_"):
128
+ try:
129
+ t = s.ctx.convert(t)
130
+ except:
131
+ return NotImplemented
132
+ return cmpfun(s._mpi_, t._mpi_)
133
+
134
+ def __eq__(s, t): return s._compare(t, libmp.mpi_eq)
135
+ def __ne__(s, t): return s._compare(t, libmp.mpi_ne)
136
+ def __lt__(s, t): return s._compare(t, libmp.mpi_lt)
137
+ def __le__(s, t): return s._compare(t, libmp.mpi_le)
138
+ def __gt__(s, t): return s._compare(t, libmp.mpi_gt)
139
+ def __ge__(s, t): return s._compare(t, libmp.mpi_ge)
140
+
141
+ def __abs__(self):
142
+ return self.ctx.make_mpf(mpi_abs(self._mpi_, self.ctx.prec))
143
+ def __pos__(self):
144
+ return self.ctx.make_mpf(mpi_pos(self._mpi_, self.ctx.prec))
145
+ def __neg__(self):
146
+ return self.ctx.make_mpf(mpi_neg(self._mpi_, self.ctx.prec))
147
+
148
+ def ae(s, t, rel_eps=None, abs_eps=None):
149
+ return s.ctx.almosteq(s, t, rel_eps, abs_eps)
150
+
151
+ class ivmpc(object):
152
+
153
+ def __new__(cls, re=0, im=0):
154
+ re = cls.ctx.convert(re)
155
+ im = cls.ctx.convert(im)
156
+ y = new(cls)
157
+ y._mpci_ = re._mpi_, im._mpi_
158
+ return y
159
+
160
+ def __hash__(self):
161
+ (a, b), (c,d) = self._mpci_
162
+ if a == b and c == d:
163
+ return mpc_hash((a, c))
164
+ else:
165
+ return hash(self._mpci_)
166
+
167
+ def __repr__(s):
168
+ if s.ctx.pretty:
169
+ return str(s)
170
+ return "iv.mpc(%s, %s)" % (repr(s.real), repr(s.imag))
171
+
172
+ def __str__(s):
173
+ return "(%s + %s*j)" % (str(s.real), str(s.imag))
174
+
175
+ @property
176
+ def a(self):
177
+ (a, b), (c,d) = self._mpci_
178
+ return self.ctx.make_mpf((a, a))
179
+
180
+ @property
181
+ def b(self):
182
+ (a, b), (c,d) = self._mpci_
183
+ return self.ctx.make_mpf((b, b))
184
+
185
+ @property
186
+ def c(self):
187
+ (a, b), (c,d) = self._mpci_
188
+ return self.ctx.make_mpf((c, c))
189
+
190
+ @property
191
+ def d(self):
192
+ (a, b), (c,d) = self._mpci_
193
+ return self.ctx.make_mpf((d, d))
194
+
195
+ @property
196
+ def real(s):
197
+ return s.ctx.make_mpf(s._mpci_[0])
198
+
199
+ @property
200
+ def imag(s):
201
+ return s.ctx.make_mpf(s._mpci_[1])
202
+
203
+ def conjugate(s):
204
+ a, b = s._mpci_
205
+ return s.ctx.make_mpc((a, mpf_neg(b)))
206
+
207
+ def overlap(s, t):
208
+ t = s.ctx.convert(t)
209
+ real_overlap = (s.a <= t.a <= s.b) or (s.a <= t.b <= s.b) or (t.a <= s.a <= t.b) or (t.a <= s.b <= t.b)
210
+ imag_overlap = (s.c <= t.c <= s.d) or (s.c <= t.d <= s.d) or (t.c <= s.c <= t.d) or (t.c <= s.d <= t.d)
211
+ return real_overlap and imag_overlap
212
+
213
+ def __contains__(s, t):
214
+ t = s.ctx.convert(t)
215
+ return t.real in s.real and t.imag in s.imag
216
+
217
+ def _compare(s, t, ne=False):
218
+ if not isinstance(t, s.ctx._types):
219
+ try:
220
+ t = s.ctx.convert(t)
221
+ except:
222
+ return NotImplemented
223
+ if hasattr(t, '_mpi_'):
224
+ tval = t._mpi_, mpi_zero
225
+ elif hasattr(t, '_mpci_'):
226
+ tval = t._mpci_
227
+ if ne:
228
+ return s._mpci_ != tval
229
+ return s._mpci_ == tval
230
+
231
+ def __eq__(s, t): return s._compare(t)
232
+ def __ne__(s, t): return s._compare(t, True)
233
+
234
+ def __lt__(s, t): raise TypeError("complex intervals cannot be ordered")
235
+ __le__ = __gt__ = __ge__ = __lt__
236
+
237
+ def __neg__(s): return s.ctx.make_mpc(mpci_neg(s._mpci_, s.ctx.prec))
238
+ def __pos__(s): return s.ctx.make_mpc(mpci_pos(s._mpci_, s.ctx.prec))
239
+ def __abs__(s): return s.ctx.make_mpf(mpci_abs(s._mpci_, s.ctx.prec))
240
+
241
+ def ae(s, t, rel_eps=None, abs_eps=None):
242
+ return s.ctx.almosteq(s, t, rel_eps, abs_eps)
243
+
244
+ def _binary_op(f_real, f_complex):
245
+ def g_complex(ctx, sval, tval):
246
+ return ctx.make_mpc(f_complex(sval, tval, ctx.prec))
247
+ def g_real(ctx, sval, tval):
248
+ try:
249
+ return ctx.make_mpf(f_real(sval, tval, ctx.prec))
250
+ except ComplexResult:
251
+ sval = (sval, mpi_zero)
252
+ tval = (tval, mpi_zero)
253
+ return g_complex(ctx, sval, tval)
254
+ def lop_real(s, t):
255
+ if isinstance(t, _matrix): return NotImplemented
256
+ ctx = s.ctx
257
+ if not isinstance(t, ctx._types): t = ctx.convert(t)
258
+ if hasattr(t, "_mpi_"): return g_real(ctx, s._mpi_, t._mpi_)
259
+ if hasattr(t, "_mpci_"): return g_complex(ctx, (s._mpi_, mpi_zero), t._mpci_)
260
+ return NotImplemented
261
+ def rop_real(s, t):
262
+ ctx = s.ctx
263
+ if not isinstance(t, ctx._types): t = ctx.convert(t)
264
+ if hasattr(t, "_mpi_"): return g_real(ctx, t._mpi_, s._mpi_)
265
+ if hasattr(t, "_mpci_"): return g_complex(ctx, t._mpci_, (s._mpi_, mpi_zero))
266
+ return NotImplemented
267
+ def lop_complex(s, t):
268
+ if isinstance(t, _matrix): return NotImplemented
269
+ ctx = s.ctx
270
+ if not isinstance(t, s.ctx._types):
271
+ try:
272
+ t = s.ctx.convert(t)
273
+ except (ValueError, TypeError):
274
+ return NotImplemented
275
+ return g_complex(ctx, s._mpci_, t._mpci_)
276
+ def rop_complex(s, t):
277
+ ctx = s.ctx
278
+ if not isinstance(t, s.ctx._types):
279
+ t = s.ctx.convert(t)
280
+ return g_complex(ctx, t._mpci_, s._mpci_)
281
+ return lop_real, rop_real, lop_complex, rop_complex
282
+
283
+ ivmpf.__add__, ivmpf.__radd__, ivmpc.__add__, ivmpc.__radd__ = _binary_op(mpi_add, mpci_add)
284
+ ivmpf.__sub__, ivmpf.__rsub__, ivmpc.__sub__, ivmpc.__rsub__ = _binary_op(mpi_sub, mpci_sub)
285
+ ivmpf.__mul__, ivmpf.__rmul__, ivmpc.__mul__, ivmpc.__rmul__ = _binary_op(mpi_mul, mpci_mul)
286
+ ivmpf.__div__, ivmpf.__rdiv__, ivmpc.__div__, ivmpc.__rdiv__ = _binary_op(mpi_div, mpci_div)
287
+ ivmpf.__pow__, ivmpf.__rpow__, ivmpc.__pow__, ivmpc.__rpow__ = _binary_op(mpi_pow, mpci_pow)
288
+
289
+ ivmpf.__truediv__ = ivmpf.__div__; ivmpf.__rtruediv__ = ivmpf.__rdiv__
290
+ ivmpc.__truediv__ = ivmpc.__div__; ivmpc.__rtruediv__ = ivmpc.__rdiv__
291
+
292
+ class ivmpf_constant(ivmpf):
293
+ def __new__(cls, f):
294
+ self = new(cls)
295
+ self._f = f
296
+ return self
297
+ def _get_mpi_(self):
298
+ prec = self.ctx._prec[0]
299
+ a = self._f(prec, round_floor)
300
+ b = self._f(prec, round_ceiling)
301
+ return a, b
302
+ _mpi_ = property(_get_mpi_)
303
+
304
+ class MPIntervalContext(StandardBaseContext):
305
+
306
+ def __init__(ctx):
307
+ ctx.mpf = type('ivmpf', (ivmpf,), {})
308
+ ctx.mpc = type('ivmpc', (ivmpc,), {})
309
+ ctx._types = (ctx.mpf, ctx.mpc)
310
+ ctx._constant = type('ivmpf_constant', (ivmpf_constant,), {})
311
+ ctx._prec = [53]
312
+ ctx._set_prec(53)
313
+ ctx._constant._ctxdata = ctx.mpf._ctxdata = ctx.mpc._ctxdata = [ctx.mpf, new, ctx._prec]
314
+ ctx._constant.ctx = ctx.mpf.ctx = ctx.mpc.ctx = ctx
315
+ ctx.pretty = False
316
+ StandardBaseContext.__init__(ctx)
317
+ ctx._init_builtins()
318
+
319
+ def _mpi(ctx, a, b=None):
320
+ if b is None:
321
+ return ctx.mpf(a)
322
+ return ctx.mpf((a,b))
323
+
324
+ def _init_builtins(ctx):
325
+ ctx.one = ctx.mpf(1)
326
+ ctx.zero = ctx.mpf(0)
327
+ ctx.inf = ctx.mpf('inf')
328
+ ctx.ninf = -ctx.inf
329
+ ctx.nan = ctx.mpf('nan')
330
+ ctx.j = ctx.mpc(0,1)
331
+ ctx.exp = ctx._wrap_mpi_function(libmp.mpi_exp, libmp.mpci_exp)
332
+ ctx.sqrt = ctx._wrap_mpi_function(libmp.mpi_sqrt)
333
+ ctx.ln = ctx._wrap_mpi_function(libmp.mpi_log, libmp.mpci_log)
334
+ ctx.cos = ctx._wrap_mpi_function(libmp.mpi_cos, libmp.mpci_cos)
335
+ ctx.sin = ctx._wrap_mpi_function(libmp.mpi_sin, libmp.mpci_sin)
336
+ ctx.tan = ctx._wrap_mpi_function(libmp.mpi_tan)
337
+ ctx.gamma = ctx._wrap_mpi_function(libmp.mpi_gamma, libmp.mpci_gamma)
338
+ ctx.loggamma = ctx._wrap_mpi_function(libmp.mpi_loggamma, libmp.mpci_loggamma)
339
+ ctx.rgamma = ctx._wrap_mpi_function(libmp.mpi_rgamma, libmp.mpci_rgamma)
340
+ ctx.factorial = ctx._wrap_mpi_function(libmp.mpi_factorial, libmp.mpci_factorial)
341
+ ctx.fac = ctx.factorial
342
+
343
+ ctx.eps = ctx._constant(lambda prec, rnd: (0, MPZ_ONE, 1-prec, 1))
344
+ ctx.pi = ctx._constant(libmp.mpf_pi)
345
+ ctx.e = ctx._constant(libmp.mpf_e)
346
+ ctx.ln2 = ctx._constant(libmp.mpf_ln2)
347
+ ctx.ln10 = ctx._constant(libmp.mpf_ln10)
348
+ ctx.phi = ctx._constant(libmp.mpf_phi)
349
+ ctx.euler = ctx._constant(libmp.mpf_euler)
350
+ ctx.catalan = ctx._constant(libmp.mpf_catalan)
351
+ ctx.glaisher = ctx._constant(libmp.mpf_glaisher)
352
+ ctx.khinchin = ctx._constant(libmp.mpf_khinchin)
353
+ ctx.twinprime = ctx._constant(libmp.mpf_twinprime)
354
+
355
+ def _wrap_mpi_function(ctx, f_real, f_complex=None):
356
+ def g(x, **kwargs):
357
+ if kwargs:
358
+ prec = kwargs.get('prec', ctx._prec[0])
359
+ else:
360
+ prec = ctx._prec[0]
361
+ x = ctx.convert(x)
362
+ if hasattr(x, "_mpi_"):
363
+ return ctx.make_mpf(f_real(x._mpi_, prec))
364
+ if hasattr(x, "_mpci_"):
365
+ return ctx.make_mpc(f_complex(x._mpci_, prec))
366
+ raise ValueError
367
+ return g
368
+
369
+ @classmethod
370
+ def _wrap_specfun(cls, name, f, wrap):
371
+ if wrap:
372
+ def f_wrapped(ctx, *args, **kwargs):
373
+ convert = ctx.convert
374
+ args = [convert(a) for a in args]
375
+ prec = ctx.prec
376
+ try:
377
+ ctx.prec += 10
378
+ retval = f(ctx, *args, **kwargs)
379
+ finally:
380
+ ctx.prec = prec
381
+ return +retval
382
+ else:
383
+ f_wrapped = f
384
+ setattr(cls, name, f_wrapped)
385
+
386
+ def _set_prec(ctx, n):
387
+ ctx._prec[0] = max(1, int(n))
388
+ ctx._dps = prec_to_dps(n)
389
+
390
+ def _set_dps(ctx, n):
391
+ ctx._prec[0] = dps_to_prec(n)
392
+ ctx._dps = max(1, int(n))
393
+
394
+ prec = property(lambda ctx: ctx._prec[0], _set_prec)
395
+ dps = property(lambda ctx: ctx._dps, _set_dps)
396
+
397
+ def make_mpf(ctx, v):
398
+ a = new(ctx.mpf)
399
+ a._mpi_ = v
400
+ return a
401
+
402
+ def make_mpc(ctx, v):
403
+ a = new(ctx.mpc)
404
+ a._mpci_ = v
405
+ return a
406
+
407
+ def _mpq(ctx, pq):
408
+ p, q = pq
409
+ a = libmp.from_rational(p, q, ctx.prec, round_floor)
410
+ b = libmp.from_rational(p, q, ctx.prec, round_ceiling)
411
+ return ctx.make_mpf((a, b))
412
+
413
+ def convert(ctx, x):
414
+ if isinstance(x, (ctx.mpf, ctx.mpc)):
415
+ return x
416
+ if isinstance(x, ctx._constant):
417
+ return +x
418
+ if isinstance(x, complex) or hasattr(x, "_mpc_"):
419
+ re = ctx.convert(x.real)
420
+ im = ctx.convert(x.imag)
421
+ return ctx.mpc(re,im)
422
+ if isinstance(x, basestring):
423
+ v = mpi_from_str(x, ctx.prec)
424
+ return ctx.make_mpf(v)
425
+ if hasattr(x, "_mpi_"):
426
+ a, b = x._mpi_
427
+ else:
428
+ try:
429
+ a, b = x
430
+ except (TypeError, ValueError):
431
+ a = b = x
432
+ if hasattr(a, "_mpi_"):
433
+ a = a._mpi_[0]
434
+ else:
435
+ a = convert_mpf_(a, ctx.prec, round_floor)
436
+ if hasattr(b, "_mpi_"):
437
+ b = b._mpi_[1]
438
+ else:
439
+ b = convert_mpf_(b, ctx.prec, round_ceiling)
440
+ if a == fnan or b == fnan:
441
+ a = fninf
442
+ b = finf
443
+ assert mpf_le(a, b), "endpoints must be properly ordered"
444
+ return ctx.make_mpf((a, b))
445
+
446
+ def nstr(ctx, x, n=5, **kwargs):
447
+ x = ctx.convert(x)
448
+ if hasattr(x, "_mpi_"):
449
+ return libmp.mpi_to_str(x._mpi_, n, **kwargs)
450
+ if hasattr(x, "_mpci_"):
451
+ re = libmp.mpi_to_str(x._mpci_[0], n, **kwargs)
452
+ im = libmp.mpi_to_str(x._mpci_[1], n, **kwargs)
453
+ return "(%s + %s*j)" % (re, im)
454
+
455
+ def mag(ctx, x):
456
+ x = ctx.convert(x)
457
+ if isinstance(x, ctx.mpc):
458
+ return max(ctx.mag(x.real), ctx.mag(x.imag)) + 1
459
+ a, b = libmp.mpi_abs(x._mpi_)
460
+ sign, man, exp, bc = b
461
+ if man:
462
+ return exp+bc
463
+ if b == fzero:
464
+ return ctx.ninf
465
+ if b == fnan:
466
+ return ctx.nan
467
+ return ctx.inf
468
+
469
+ def isnan(ctx, x):
470
+ return False
471
+
472
+ def isinf(ctx, x):
473
+ return x == ctx.inf
474
+
475
+ def isint(ctx, x):
476
+ x = ctx.convert(x)
477
+ a, b = x._mpi_
478
+ if a == b:
479
+ sign, man, exp, bc = a
480
+ if man:
481
+ return exp >= 0
482
+ return a == fzero
483
+ return None
484
+
485
+ def ldexp(ctx, x, n):
486
+ a, b = ctx.convert(x)._mpi_
487
+ a = libmp.mpf_shift(a, n)
488
+ b = libmp.mpf_shift(b, n)
489
+ return ctx.make_mpf((a,b))
490
+
491
+ def absmin(ctx, x):
492
+ return abs(ctx.convert(x)).a
493
+
494
+ def absmax(ctx, x):
495
+ return abs(ctx.convert(x)).b
496
+
497
+ def atan2(ctx, y, x):
498
+ y = ctx.convert(y)._mpi_
499
+ x = ctx.convert(x)._mpi_
500
+ return ctx.make_mpf(libmp.mpi_atan2(y,x,ctx.prec))
501
+
502
+ def _convert_param(ctx, x):
503
+ if isinstance(x, libmp.int_types):
504
+ return x, 'Z'
505
+ if isinstance(x, tuple):
506
+ p, q = x
507
+ return (ctx.mpf(p) / ctx.mpf(q), 'R')
508
+ x = ctx.convert(x)
509
+ if isinstance(x, ctx.mpf):
510
+ return x, 'R'
511
+ if isinstance(x, ctx.mpc):
512
+ return x, 'C'
513
+ raise ValueError
514
+
515
+ def _is_real_type(ctx, z):
516
+ return isinstance(z, ctx.mpf) or isinstance(z, int_types)
517
+
518
+ def _is_complex_type(ctx, z):
519
+ return isinstance(z, ctx.mpc)
520
+
521
+ def hypsum(ctx, p, q, types, coeffs, z, maxterms=6000, **kwargs):
522
+ coeffs = list(coeffs)
523
+ num = range(p)
524
+ den = range(p,p+q)
525
+ #tol = ctx.eps
526
+ s = t = ctx.one
527
+ k = 0
528
+ while 1:
529
+ for i in num: t *= (coeffs[i]+k)
530
+ for i in den: t /= (coeffs[i]+k)
531
+ k += 1; t /= k; t *= z; s += t
532
+ if t == 0:
533
+ return s
534
+ #if abs(t) < tol:
535
+ # return s
536
+ if k > maxterms:
537
+ raise ctx.NoConvergence
538
+
539
+
540
+ # Register with "numbers" ABC
541
+ # We do not subclass, hence we do not use the @abstractmethod checks. While
542
+ # this is less invasive it may turn out that we do not actually support
543
+ # parts of the expected interfaces. See
544
+ # http://docs.python.org/2/library/numbers.html for list of abstract
545
+ # methods.
546
+ try:
547
+ import numbers
548
+ numbers.Complex.register(ivmpc)
549
+ numbers.Real.register(ivmpf)
550
+ except ImportError:
551
+ pass
llmeval-env/lib/python3.10/site-packages/mpmath/ctx_mp.py ADDED
@@ -0,0 +1,1339 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ This module defines the mpf, mpc classes, and standard functions for
3
+ operating with them.
4
+ """
5
+ __docformat__ = 'plaintext'
6
+
7
+ import functools
8
+
9
+ import re
10
+
11
+ from .ctx_base import StandardBaseContext
12
+
13
+ from .libmp.backend import basestring, BACKEND
14
+
15
+ from . import libmp
16
+
17
+ from .libmp import (MPZ, MPZ_ZERO, MPZ_ONE, int_types, repr_dps,
18
+ round_floor, round_ceiling, dps_to_prec, round_nearest, prec_to_dps,
19
+ ComplexResult, to_pickable, from_pickable, normalize,
20
+ from_int, from_float, from_str, to_int, to_float, to_str,
21
+ from_rational, from_man_exp,
22
+ fone, fzero, finf, fninf, fnan,
23
+ mpf_abs, mpf_pos, mpf_neg, mpf_add, mpf_sub, mpf_mul, mpf_mul_int,
24
+ mpf_div, mpf_rdiv_int, mpf_pow_int, mpf_mod,
25
+ mpf_eq, mpf_cmp, mpf_lt, mpf_gt, mpf_le, mpf_ge,
26
+ mpf_hash, mpf_rand,
27
+ mpf_sum,
28
+ bitcount, to_fixed,
29
+ mpc_to_str,
30
+ mpc_to_complex, mpc_hash, mpc_pos, mpc_is_nonzero, mpc_neg, mpc_conjugate,
31
+ mpc_abs, mpc_add, mpc_add_mpf, mpc_sub, mpc_sub_mpf, mpc_mul, mpc_mul_mpf,
32
+ mpc_mul_int, mpc_div, mpc_div_mpf, mpc_pow, mpc_pow_mpf, mpc_pow_int,
33
+ mpc_mpf_div,
34
+ mpf_pow,
35
+ mpf_pi, mpf_degree, mpf_e, mpf_phi, mpf_ln2, mpf_ln10,
36
+ mpf_euler, mpf_catalan, mpf_apery, mpf_khinchin,
37
+ mpf_glaisher, mpf_twinprime, mpf_mertens,
38
+ int_types)
39
+
40
+ from . import function_docs
41
+ from . import rational
42
+
43
+ new = object.__new__
44
+
45
+ get_complex = re.compile(r'^\(?(?P<re>[\+\-]?\d*(\.\d*)?(e[\+\-]?\d+)?)??'
46
+ r'(?P<im>[\+\-]?\d*(\.\d*)?(e[\+\-]?\d+)?j)?\)?$')
47
+
48
+ if BACKEND == 'sage':
49
+ from sage.libs.mpmath.ext_main import Context as BaseMPContext
50
+ # pickle hack
51
+ import sage.libs.mpmath.ext_main as _mpf_module
52
+ else:
53
+ from .ctx_mp_python import PythonMPContext as BaseMPContext
54
+ from . import ctx_mp_python as _mpf_module
55
+
56
+ from .ctx_mp_python import _mpf, _mpc, mpnumeric
57
+
58
+ class MPContext(BaseMPContext, StandardBaseContext):
59
+ """
60
+ Context for multiprecision arithmetic with a global precision.
61
+ """
62
+
63
+ def __init__(ctx):
64
+ BaseMPContext.__init__(ctx)
65
+ ctx.trap_complex = False
66
+ ctx.pretty = False
67
+ ctx.types = [ctx.mpf, ctx.mpc, ctx.constant]
68
+ ctx._mpq = rational.mpq
69
+ ctx.default()
70
+ StandardBaseContext.__init__(ctx)
71
+
72
+ ctx.mpq = rational.mpq
73
+ ctx.init_builtins()
74
+
75
+ ctx.hyp_summators = {}
76
+
77
+ ctx._init_aliases()
78
+
79
+ # XXX: automate
80
+ try:
81
+ ctx.bernoulli.im_func.func_doc = function_docs.bernoulli
82
+ ctx.primepi.im_func.func_doc = function_docs.primepi
83
+ ctx.psi.im_func.func_doc = function_docs.psi
84
+ ctx.atan2.im_func.func_doc = function_docs.atan2
85
+ except AttributeError:
86
+ # python 3
87
+ ctx.bernoulli.__func__.func_doc = function_docs.bernoulli
88
+ ctx.primepi.__func__.func_doc = function_docs.primepi
89
+ ctx.psi.__func__.func_doc = function_docs.psi
90
+ ctx.atan2.__func__.func_doc = function_docs.atan2
91
+
92
+ ctx.digamma.func_doc = function_docs.digamma
93
+ ctx.cospi.func_doc = function_docs.cospi
94
+ ctx.sinpi.func_doc = function_docs.sinpi
95
+
96
+ def init_builtins(ctx):
97
+
98
+ mpf = ctx.mpf
99
+ mpc = ctx.mpc
100
+
101
+ # Exact constants
102
+ ctx.one = ctx.make_mpf(fone)
103
+ ctx.zero = ctx.make_mpf(fzero)
104
+ ctx.j = ctx.make_mpc((fzero,fone))
105
+ ctx.inf = ctx.make_mpf(finf)
106
+ ctx.ninf = ctx.make_mpf(fninf)
107
+ ctx.nan = ctx.make_mpf(fnan)
108
+
109
+ eps = ctx.constant(lambda prec, rnd: (0, MPZ_ONE, 1-prec, 1),
110
+ "epsilon of working precision", "eps")
111
+ ctx.eps = eps
112
+
113
+ # Approximate constants
114
+ ctx.pi = ctx.constant(mpf_pi, "pi", "pi")
115
+ ctx.ln2 = ctx.constant(mpf_ln2, "ln(2)", "ln2")
116
+ ctx.ln10 = ctx.constant(mpf_ln10, "ln(10)", "ln10")
117
+ ctx.phi = ctx.constant(mpf_phi, "Golden ratio phi", "phi")
118
+ ctx.e = ctx.constant(mpf_e, "e = exp(1)", "e")
119
+ ctx.euler = ctx.constant(mpf_euler, "Euler's constant", "euler")
120
+ ctx.catalan = ctx.constant(mpf_catalan, "Catalan's constant", "catalan")
121
+ ctx.khinchin = ctx.constant(mpf_khinchin, "Khinchin's constant", "khinchin")
122
+ ctx.glaisher = ctx.constant(mpf_glaisher, "Glaisher's constant", "glaisher")
123
+ ctx.apery = ctx.constant(mpf_apery, "Apery's constant", "apery")
124
+ ctx.degree = ctx.constant(mpf_degree, "1 deg = pi / 180", "degree")
125
+ ctx.twinprime = ctx.constant(mpf_twinprime, "Twin prime constant", "twinprime")
126
+ ctx.mertens = ctx.constant(mpf_mertens, "Mertens' constant", "mertens")
127
+
128
+ # Standard functions
129
+ ctx.sqrt = ctx._wrap_libmp_function(libmp.mpf_sqrt, libmp.mpc_sqrt)
130
+ ctx.cbrt = ctx._wrap_libmp_function(libmp.mpf_cbrt, libmp.mpc_cbrt)
131
+ ctx.ln = ctx._wrap_libmp_function(libmp.mpf_log, libmp.mpc_log)
132
+ ctx.atan = ctx._wrap_libmp_function(libmp.mpf_atan, libmp.mpc_atan)
133
+ ctx.exp = ctx._wrap_libmp_function(libmp.mpf_exp, libmp.mpc_exp)
134
+ ctx.expj = ctx._wrap_libmp_function(libmp.mpf_expj, libmp.mpc_expj)
135
+ ctx.expjpi = ctx._wrap_libmp_function(libmp.mpf_expjpi, libmp.mpc_expjpi)
136
+ ctx.sin = ctx._wrap_libmp_function(libmp.mpf_sin, libmp.mpc_sin)
137
+ ctx.cos = ctx._wrap_libmp_function(libmp.mpf_cos, libmp.mpc_cos)
138
+ ctx.tan = ctx._wrap_libmp_function(libmp.mpf_tan, libmp.mpc_tan)
139
+ ctx.sinh = ctx._wrap_libmp_function(libmp.mpf_sinh, libmp.mpc_sinh)
140
+ ctx.cosh = ctx._wrap_libmp_function(libmp.mpf_cosh, libmp.mpc_cosh)
141
+ ctx.tanh = ctx._wrap_libmp_function(libmp.mpf_tanh, libmp.mpc_tanh)
142
+ ctx.asin = ctx._wrap_libmp_function(libmp.mpf_asin, libmp.mpc_asin)
143
+ ctx.acos = ctx._wrap_libmp_function(libmp.mpf_acos, libmp.mpc_acos)
144
+ ctx.atan = ctx._wrap_libmp_function(libmp.mpf_atan, libmp.mpc_atan)
145
+ ctx.asinh = ctx._wrap_libmp_function(libmp.mpf_asinh, libmp.mpc_asinh)
146
+ ctx.acosh = ctx._wrap_libmp_function(libmp.mpf_acosh, libmp.mpc_acosh)
147
+ ctx.atanh = ctx._wrap_libmp_function(libmp.mpf_atanh, libmp.mpc_atanh)
148
+ ctx.sinpi = ctx._wrap_libmp_function(libmp.mpf_sin_pi, libmp.mpc_sin_pi)
149
+ ctx.cospi = ctx._wrap_libmp_function(libmp.mpf_cos_pi, libmp.mpc_cos_pi)
150
+ ctx.floor = ctx._wrap_libmp_function(libmp.mpf_floor, libmp.mpc_floor)
151
+ ctx.ceil = ctx._wrap_libmp_function(libmp.mpf_ceil, libmp.mpc_ceil)
152
+ ctx.nint = ctx._wrap_libmp_function(libmp.mpf_nint, libmp.mpc_nint)
153
+ ctx.frac = ctx._wrap_libmp_function(libmp.mpf_frac, libmp.mpc_frac)
154
+ ctx.fib = ctx.fibonacci = ctx._wrap_libmp_function(libmp.mpf_fibonacci, libmp.mpc_fibonacci)
155
+
156
+ ctx.gamma = ctx._wrap_libmp_function(libmp.mpf_gamma, libmp.mpc_gamma)
157
+ ctx.rgamma = ctx._wrap_libmp_function(libmp.mpf_rgamma, libmp.mpc_rgamma)
158
+ ctx.loggamma = ctx._wrap_libmp_function(libmp.mpf_loggamma, libmp.mpc_loggamma)
159
+ ctx.fac = ctx.factorial = ctx._wrap_libmp_function(libmp.mpf_factorial, libmp.mpc_factorial)
160
+
161
+ ctx.digamma = ctx._wrap_libmp_function(libmp.mpf_psi0, libmp.mpc_psi0)
162
+ ctx.harmonic = ctx._wrap_libmp_function(libmp.mpf_harmonic, libmp.mpc_harmonic)
163
+ ctx.ei = ctx._wrap_libmp_function(libmp.mpf_ei, libmp.mpc_ei)
164
+ ctx.e1 = ctx._wrap_libmp_function(libmp.mpf_e1, libmp.mpc_e1)
165
+ ctx._ci = ctx._wrap_libmp_function(libmp.mpf_ci, libmp.mpc_ci)
166
+ ctx._si = ctx._wrap_libmp_function(libmp.mpf_si, libmp.mpc_si)
167
+ ctx.ellipk = ctx._wrap_libmp_function(libmp.mpf_ellipk, libmp.mpc_ellipk)
168
+ ctx._ellipe = ctx._wrap_libmp_function(libmp.mpf_ellipe, libmp.mpc_ellipe)
169
+ ctx.agm1 = ctx._wrap_libmp_function(libmp.mpf_agm1, libmp.mpc_agm1)
170
+ ctx._erf = ctx._wrap_libmp_function(libmp.mpf_erf, None)
171
+ ctx._erfc = ctx._wrap_libmp_function(libmp.mpf_erfc, None)
172
+ ctx._zeta = ctx._wrap_libmp_function(libmp.mpf_zeta, libmp.mpc_zeta)
173
+ ctx._altzeta = ctx._wrap_libmp_function(libmp.mpf_altzeta, libmp.mpc_altzeta)
174
+
175
+ # Faster versions
176
+ ctx.sqrt = getattr(ctx, "_sage_sqrt", ctx.sqrt)
177
+ ctx.exp = getattr(ctx, "_sage_exp", ctx.exp)
178
+ ctx.ln = getattr(ctx, "_sage_ln", ctx.ln)
179
+ ctx.cos = getattr(ctx, "_sage_cos", ctx.cos)
180
+ ctx.sin = getattr(ctx, "_sage_sin", ctx.sin)
181
+
182
+ def to_fixed(ctx, x, prec):
183
+ return x.to_fixed(prec)
184
+
185
+ def hypot(ctx, x, y):
186
+ r"""
187
+ Computes the Euclidean norm of the vector `(x, y)`, equal
188
+ to `\sqrt{x^2 + y^2}`. Both `x` and `y` must be real."""
189
+ x = ctx.convert(x)
190
+ y = ctx.convert(y)
191
+ return ctx.make_mpf(libmp.mpf_hypot(x._mpf_, y._mpf_, *ctx._prec_rounding))
192
+
193
+ def _gamma_upper_int(ctx, n, z):
194
+ n = int(ctx._re(n))
195
+ if n == 0:
196
+ return ctx.e1(z)
197
+ if not hasattr(z, '_mpf_'):
198
+ raise NotImplementedError
199
+ prec, rounding = ctx._prec_rounding
200
+ real, imag = libmp.mpf_expint(n, z._mpf_, prec, rounding, gamma=True)
201
+ if imag is None:
202
+ return ctx.make_mpf(real)
203
+ else:
204
+ return ctx.make_mpc((real, imag))
205
+
206
+ def _expint_int(ctx, n, z):
207
+ n = int(n)
208
+ if n == 1:
209
+ return ctx.e1(z)
210
+ if not hasattr(z, '_mpf_'):
211
+ raise NotImplementedError
212
+ prec, rounding = ctx._prec_rounding
213
+ real, imag = libmp.mpf_expint(n, z._mpf_, prec, rounding)
214
+ if imag is None:
215
+ return ctx.make_mpf(real)
216
+ else:
217
+ return ctx.make_mpc((real, imag))
218
+
219
+ def _nthroot(ctx, x, n):
220
+ if hasattr(x, '_mpf_'):
221
+ try:
222
+ return ctx.make_mpf(libmp.mpf_nthroot(x._mpf_, n, *ctx._prec_rounding))
223
+ except ComplexResult:
224
+ if ctx.trap_complex:
225
+ raise
226
+ x = (x._mpf_, libmp.fzero)
227
+ else:
228
+ x = x._mpc_
229
+ return ctx.make_mpc(libmp.mpc_nthroot(x, n, *ctx._prec_rounding))
230
+
231
+ def _besselj(ctx, n, z):
232
+ prec, rounding = ctx._prec_rounding
233
+ if hasattr(z, '_mpf_'):
234
+ return ctx.make_mpf(libmp.mpf_besseljn(n, z._mpf_, prec, rounding))
235
+ elif hasattr(z, '_mpc_'):
236
+ return ctx.make_mpc(libmp.mpc_besseljn(n, z._mpc_, prec, rounding))
237
+
238
+ def _agm(ctx, a, b=1):
239
+ prec, rounding = ctx._prec_rounding
240
+ if hasattr(a, '_mpf_') and hasattr(b, '_mpf_'):
241
+ try:
242
+ v = libmp.mpf_agm(a._mpf_, b._mpf_, prec, rounding)
243
+ return ctx.make_mpf(v)
244
+ except ComplexResult:
245
+ pass
246
+ if hasattr(a, '_mpf_'): a = (a._mpf_, libmp.fzero)
247
+ else: a = a._mpc_
248
+ if hasattr(b, '_mpf_'): b = (b._mpf_, libmp.fzero)
249
+ else: b = b._mpc_
250
+ return ctx.make_mpc(libmp.mpc_agm(a, b, prec, rounding))
251
+
252
+ def bernoulli(ctx, n):
253
+ return ctx.make_mpf(libmp.mpf_bernoulli(int(n), *ctx._prec_rounding))
254
+
255
+ def _zeta_int(ctx, n):
256
+ return ctx.make_mpf(libmp.mpf_zeta_int(int(n), *ctx._prec_rounding))
257
+
258
+ def atan2(ctx, y, x):
259
+ x = ctx.convert(x)
260
+ y = ctx.convert(y)
261
+ return ctx.make_mpf(libmp.mpf_atan2(y._mpf_, x._mpf_, *ctx._prec_rounding))
262
+
263
+ def psi(ctx, m, z):
264
+ z = ctx.convert(z)
265
+ m = int(m)
266
+ if ctx._is_real_type(z):
267
+ return ctx.make_mpf(libmp.mpf_psi(m, z._mpf_, *ctx._prec_rounding))
268
+ else:
269
+ return ctx.make_mpc(libmp.mpc_psi(m, z._mpc_, *ctx._prec_rounding))
270
+
271
+ def cos_sin(ctx, x, **kwargs):
272
+ if type(x) not in ctx.types:
273
+ x = ctx.convert(x)
274
+ prec, rounding = ctx._parse_prec(kwargs)
275
+ if hasattr(x, '_mpf_'):
276
+ c, s = libmp.mpf_cos_sin(x._mpf_, prec, rounding)
277
+ return ctx.make_mpf(c), ctx.make_mpf(s)
278
+ elif hasattr(x, '_mpc_'):
279
+ c, s = libmp.mpc_cos_sin(x._mpc_, prec, rounding)
280
+ return ctx.make_mpc(c), ctx.make_mpc(s)
281
+ else:
282
+ return ctx.cos(x, **kwargs), ctx.sin(x, **kwargs)
283
+
284
+ def cospi_sinpi(ctx, x, **kwargs):
285
+ if type(x) not in ctx.types:
286
+ x = ctx.convert(x)
287
+ prec, rounding = ctx._parse_prec(kwargs)
288
+ if hasattr(x, '_mpf_'):
289
+ c, s = libmp.mpf_cos_sin_pi(x._mpf_, prec, rounding)
290
+ return ctx.make_mpf(c), ctx.make_mpf(s)
291
+ elif hasattr(x, '_mpc_'):
292
+ c, s = libmp.mpc_cos_sin_pi(x._mpc_, prec, rounding)
293
+ return ctx.make_mpc(c), ctx.make_mpc(s)
294
+ else:
295
+ return ctx.cos(x, **kwargs), ctx.sin(x, **kwargs)
296
+
297
+ def clone(ctx):
298
+ """
299
+ Create a copy of the context, with the same working precision.
300
+ """
301
+ a = ctx.__class__()
302
+ a.prec = ctx.prec
303
+ return a
304
+
305
+ # Several helper methods
306
+ # TODO: add more of these, make consistent, write docstrings, ...
307
+
308
+ def _is_real_type(ctx, x):
309
+ if hasattr(x, '_mpc_') or type(x) is complex:
310
+ return False
311
+ return True
312
+
313
+ def _is_complex_type(ctx, x):
314
+ if hasattr(x, '_mpc_') or type(x) is complex:
315
+ return True
316
+ return False
317
+
318
+ def isnan(ctx, x):
319
+ """
320
+ Return *True* if *x* is a NaN (not-a-number), or for a complex
321
+ number, whether either the real or complex part is NaN;
322
+ otherwise return *False*::
323
+
324
+ >>> from mpmath import *
325
+ >>> isnan(3.14)
326
+ False
327
+ >>> isnan(nan)
328
+ True
329
+ >>> isnan(mpc(3.14,2.72))
330
+ False
331
+ >>> isnan(mpc(3.14,nan))
332
+ True
333
+
334
+ """
335
+ if hasattr(x, "_mpf_"):
336
+ return x._mpf_ == fnan
337
+ if hasattr(x, "_mpc_"):
338
+ return fnan in x._mpc_
339
+ if isinstance(x, int_types) or isinstance(x, rational.mpq):
340
+ return False
341
+ x = ctx.convert(x)
342
+ if hasattr(x, '_mpf_') or hasattr(x, '_mpc_'):
343
+ return ctx.isnan(x)
344
+ raise TypeError("isnan() needs a number as input")
345
+
346
+ def isfinite(ctx, x):
347
+ """
348
+ Return *True* if *x* is a finite number, i.e. neither
349
+ an infinity or a NaN.
350
+
351
+ >>> from mpmath import *
352
+ >>> isfinite(inf)
353
+ False
354
+ >>> isfinite(-inf)
355
+ False
356
+ >>> isfinite(3)
357
+ True
358
+ >>> isfinite(nan)
359
+ False
360
+ >>> isfinite(3+4j)
361
+ True
362
+ >>> isfinite(mpc(3,inf))
363
+ False
364
+ >>> isfinite(mpc(nan,3))
365
+ False
366
+
367
+ """
368
+ if ctx.isinf(x) or ctx.isnan(x):
369
+ return False
370
+ return True
371
+
372
+ def isnpint(ctx, x):
373
+ """
374
+ Determine if *x* is a nonpositive integer.
375
+ """
376
+ if not x:
377
+ return True
378
+ if hasattr(x, '_mpf_'):
379
+ sign, man, exp, bc = x._mpf_
380
+ return sign and exp >= 0
381
+ if hasattr(x, '_mpc_'):
382
+ return not x.imag and ctx.isnpint(x.real)
383
+ if type(x) in int_types:
384
+ return x <= 0
385
+ if isinstance(x, ctx.mpq):
386
+ p, q = x._mpq_
387
+ if not p:
388
+ return True
389
+ return q == 1 and p <= 0
390
+ return ctx.isnpint(ctx.convert(x))
391
+
392
+ def __str__(ctx):
393
+ lines = ["Mpmath settings:",
394
+ (" mp.prec = %s" % ctx.prec).ljust(30) + "[default: 53]",
395
+ (" mp.dps = %s" % ctx.dps).ljust(30) + "[default: 15]",
396
+ (" mp.trap_complex = %s" % ctx.trap_complex).ljust(30) + "[default: False]",
397
+ ]
398
+ return "\n".join(lines)
399
+
400
+ @property
401
+ def _repr_digits(ctx):
402
+ return repr_dps(ctx._prec)
403
+
404
+ @property
405
+ def _str_digits(ctx):
406
+ return ctx._dps
407
+
408
+ def extraprec(ctx, n, normalize_output=False):
409
+ """
410
+ The block
411
+
412
+ with extraprec(n):
413
+ <code>
414
+
415
+ increases the precision n bits, executes <code>, and then
416
+ restores the precision.
417
+
418
+ extraprec(n)(f) returns a decorated version of the function f
419
+ that increases the working precision by n bits before execution,
420
+ and restores the parent precision afterwards. With
421
+ normalize_output=True, it rounds the return value to the parent
422
+ precision.
423
+ """
424
+ return PrecisionManager(ctx, lambda p: p + n, None, normalize_output)
425
+
426
+ def extradps(ctx, n, normalize_output=False):
427
+ """
428
+ This function is analogous to extraprec (see documentation)
429
+ but changes the decimal precision instead of the number of bits.
430
+ """
431
+ return PrecisionManager(ctx, None, lambda d: d + n, normalize_output)
432
+
433
+ def workprec(ctx, n, normalize_output=False):
434
+ """
435
+ The block
436
+
437
+ with workprec(n):
438
+ <code>
439
+
440
+ sets the precision to n bits, executes <code>, and then restores
441
+ the precision.
442
+
443
+ workprec(n)(f) returns a decorated version of the function f
444
+ that sets the precision to n bits before execution,
445
+ and restores the precision afterwards. With normalize_output=True,
446
+ it rounds the return value to the parent precision.
447
+ """
448
+ return PrecisionManager(ctx, lambda p: n, None, normalize_output)
449
+
450
+ def workdps(ctx, n, normalize_output=False):
451
+ """
452
+ This function is analogous to workprec (see documentation)
453
+ but changes the decimal precision instead of the number of bits.
454
+ """
455
+ return PrecisionManager(ctx, None, lambda d: n, normalize_output)
456
+
457
+ def autoprec(ctx, f, maxprec=None, catch=(), verbose=False):
458
+ r"""
459
+ Return a wrapped copy of *f* that repeatedly evaluates *f*
460
+ with increasing precision until the result converges to the
461
+ full precision used at the point of the call.
462
+
463
+ This heuristically protects against rounding errors, at the cost of
464
+ roughly a 2x slowdown compared to manually setting the optimal
465
+ precision. This method can, however, easily be fooled if the results
466
+ from *f* depend "discontinuously" on the precision, for instance
467
+ if catastrophic cancellation can occur. Therefore, :func:`~mpmath.autoprec`
468
+ should be used judiciously.
469
+
470
+ **Examples**
471
+
472
+ Many functions are sensitive to perturbations of the input arguments.
473
+ If the arguments are decimal numbers, they may have to be converted
474
+ to binary at a much higher precision. If the amount of required
475
+ extra precision is unknown, :func:`~mpmath.autoprec` is convenient::
476
+
477
+ >>> from mpmath import *
478
+ >>> mp.dps = 15
479
+ >>> mp.pretty = True
480
+ >>> besselj(5, 125 * 10**28) # Exact input
481
+ -8.03284785591801e-17
482
+ >>> besselj(5, '1.25e30') # Bad
483
+ 7.12954868316652e-16
484
+ >>> autoprec(besselj)(5, '1.25e30') # Good
485
+ -8.03284785591801e-17
486
+
487
+ The following fails to converge because `\sin(\pi) = 0` whereas all
488
+ finite-precision approximations of `\pi` give nonzero values::
489
+
490
+ >>> autoprec(sin)(pi) # doctest: +IGNORE_EXCEPTION_DETAIL
491
+ Traceback (most recent call last):
492
+ ...
493
+ NoConvergence: autoprec: prec increased to 2910 without convergence
494
+
495
+ As the following example shows, :func:`~mpmath.autoprec` can protect against
496
+ cancellation, but is fooled by too severe cancellation::
497
+
498
+ >>> x = 1e-10
499
+ >>> exp(x)-1; expm1(x); autoprec(lambda t: exp(t)-1)(x)
500
+ 1.00000008274037e-10
501
+ 1.00000000005e-10
502
+ 1.00000000005e-10
503
+ >>> x = 1e-50
504
+ >>> exp(x)-1; expm1(x); autoprec(lambda t: exp(t)-1)(x)
505
+ 0.0
506
+ 1.0e-50
507
+ 0.0
508
+
509
+ With *catch*, an exception or list of exceptions to intercept
510
+ may be specified. The raised exception is interpreted
511
+ as signaling insufficient precision. This permits, for example,
512
+ evaluating a function where a too low precision results in a
513
+ division by zero::
514
+
515
+ >>> f = lambda x: 1/(exp(x)-1)
516
+ >>> f(1e-30)
517
+ Traceback (most recent call last):
518
+ ...
519
+ ZeroDivisionError
520
+ >>> autoprec(f, catch=ZeroDivisionError)(1e-30)
521
+ 1.0e+30
522
+
523
+
524
+ """
525
+ def f_autoprec_wrapped(*args, **kwargs):
526
+ prec = ctx.prec
527
+ if maxprec is None:
528
+ maxprec2 = ctx._default_hyper_maxprec(prec)
529
+ else:
530
+ maxprec2 = maxprec
531
+ try:
532
+ ctx.prec = prec + 10
533
+ try:
534
+ v1 = f(*args, **kwargs)
535
+ except catch:
536
+ v1 = ctx.nan
537
+ prec2 = prec + 20
538
+ while 1:
539
+ ctx.prec = prec2
540
+ try:
541
+ v2 = f(*args, **kwargs)
542
+ except catch:
543
+ v2 = ctx.nan
544
+ if v1 == v2:
545
+ break
546
+ err = ctx.mag(v2-v1) - ctx.mag(v2)
547
+ if err < (-prec):
548
+ break
549
+ if verbose:
550
+ print("autoprec: target=%s, prec=%s, accuracy=%s" \
551
+ % (prec, prec2, -err))
552
+ v1 = v2
553
+ if prec2 >= maxprec2:
554
+ raise ctx.NoConvergence(\
555
+ "autoprec: prec increased to %i without convergence"\
556
+ % prec2)
557
+ prec2 += int(prec2*2)
558
+ prec2 = min(prec2, maxprec2)
559
+ finally:
560
+ ctx.prec = prec
561
+ return +v2
562
+ return f_autoprec_wrapped
563
+
564
+ def nstr(ctx, x, n=6, **kwargs):
565
+ """
566
+ Convert an ``mpf`` or ``mpc`` to a decimal string literal with *n*
567
+ significant digits. The small default value for *n* is chosen to
568
+ make this function useful for printing collections of numbers
569
+ (lists, matrices, etc).
570
+
571
+ If *x* is a list or tuple, :func:`~mpmath.nstr` is applied recursively
572
+ to each element. For unrecognized classes, :func:`~mpmath.nstr`
573
+ simply returns ``str(x)``.
574
+
575
+ The companion function :func:`~mpmath.nprint` prints the result
576
+ instead of returning it.
577
+
578
+ The keyword arguments *strip_zeros*, *min_fixed*, *max_fixed*
579
+ and *show_zero_exponent* are forwarded to :func:`~mpmath.libmp.to_str`.
580
+
581
+ The number will be printed in fixed-point format if the position
582
+ of the leading digit is strictly between min_fixed
583
+ (default = min(-dps/3,-5)) and max_fixed (default = dps).
584
+
585
+ To force fixed-point format always, set min_fixed = -inf,
586
+ max_fixed = +inf. To force floating-point format, set
587
+ min_fixed >= max_fixed.
588
+
589
+ >>> from mpmath import *
590
+ >>> nstr([+pi, ldexp(1,-500)])
591
+ '[3.14159, 3.05494e-151]'
592
+ >>> nprint([+pi, ldexp(1,-500)])
593
+ [3.14159, 3.05494e-151]
594
+ >>> nstr(mpf("5e-10"), 5)
595
+ '5.0e-10'
596
+ >>> nstr(mpf("5e-10"), 5, strip_zeros=False)
597
+ '5.0000e-10'
598
+ >>> nstr(mpf("5e-10"), 5, strip_zeros=False, min_fixed=-11)
599
+ '0.00000000050000'
600
+ >>> nstr(mpf(0), 5, show_zero_exponent=True)
601
+ '0.0e+0'
602
+
603
+ """
604
+ if isinstance(x, list):
605
+ return "[%s]" % (", ".join(ctx.nstr(c, n, **kwargs) for c in x))
606
+ if isinstance(x, tuple):
607
+ return "(%s)" % (", ".join(ctx.nstr(c, n, **kwargs) for c in x))
608
+ if hasattr(x, '_mpf_'):
609
+ return to_str(x._mpf_, n, **kwargs)
610
+ if hasattr(x, '_mpc_'):
611
+ return "(" + mpc_to_str(x._mpc_, n, **kwargs) + ")"
612
+ if isinstance(x, basestring):
613
+ return repr(x)
614
+ if isinstance(x, ctx.matrix):
615
+ return x.__nstr__(n, **kwargs)
616
+ return str(x)
617
+
618
+ def _convert_fallback(ctx, x, strings):
619
+ if strings and isinstance(x, basestring):
620
+ if 'j' in x.lower():
621
+ x = x.lower().replace(' ', '')
622
+ match = get_complex.match(x)
623
+ re = match.group('re')
624
+ if not re:
625
+ re = 0
626
+ im = match.group('im').rstrip('j')
627
+ return ctx.mpc(ctx.convert(re), ctx.convert(im))
628
+ if hasattr(x, "_mpi_"):
629
+ a, b = x._mpi_
630
+ if a == b:
631
+ return ctx.make_mpf(a)
632
+ else:
633
+ raise ValueError("can only create mpf from zero-width interval")
634
+ raise TypeError("cannot create mpf from " + repr(x))
635
+
636
+ def mpmathify(ctx, *args, **kwargs):
637
+ return ctx.convert(*args, **kwargs)
638
+
639
+ def _parse_prec(ctx, kwargs):
640
+ if kwargs:
641
+ if kwargs.get('exact'):
642
+ return 0, 'f'
643
+ prec, rounding = ctx._prec_rounding
644
+ if 'rounding' in kwargs:
645
+ rounding = kwargs['rounding']
646
+ if 'prec' in kwargs:
647
+ prec = kwargs['prec']
648
+ if prec == ctx.inf:
649
+ return 0, 'f'
650
+ else:
651
+ prec = int(prec)
652
+ elif 'dps' in kwargs:
653
+ dps = kwargs['dps']
654
+ if dps == ctx.inf:
655
+ return 0, 'f'
656
+ prec = dps_to_prec(dps)
657
+ return prec, rounding
658
+ return ctx._prec_rounding
659
+
660
+ _exact_overflow_msg = "the exact result does not fit in memory"
661
+
662
+ _hypsum_msg = """hypsum() failed to converge to the requested %i bits of accuracy
663
+ using a working precision of %i bits. Try with a higher maxprec,
664
+ maxterms, or set zeroprec."""
665
+
666
+ def hypsum(ctx, p, q, flags, coeffs, z, accurate_small=True, **kwargs):
667
+ if hasattr(z, "_mpf_"):
668
+ key = p, q, flags, 'R'
669
+ v = z._mpf_
670
+ elif hasattr(z, "_mpc_"):
671
+ key = p, q, flags, 'C'
672
+ v = z._mpc_
673
+ if key not in ctx.hyp_summators:
674
+ ctx.hyp_summators[key] = libmp.make_hyp_summator(key)[1]
675
+ summator = ctx.hyp_summators[key]
676
+ prec = ctx.prec
677
+ maxprec = kwargs.get('maxprec', ctx._default_hyper_maxprec(prec))
678
+ extraprec = 50
679
+ epsshift = 25
680
+ # Jumps in magnitude occur when parameters are close to negative
681
+ # integers. We must ensure that these terms are included in
682
+ # the sum and added accurately
683
+ magnitude_check = {}
684
+ max_total_jump = 0
685
+ for i, c in enumerate(coeffs):
686
+ if flags[i] == 'Z':
687
+ if i >= p and c <= 0:
688
+ ok = False
689
+ for ii, cc in enumerate(coeffs[:p]):
690
+ # Note: c <= cc or c < cc, depending on convention
691
+ if flags[ii] == 'Z' and cc <= 0 and c <= cc:
692
+ ok = True
693
+ if not ok:
694
+ raise ZeroDivisionError("pole in hypergeometric series")
695
+ continue
696
+ n, d = ctx.nint_distance(c)
697
+ n = -int(n)
698
+ d = -d
699
+ if i >= p and n >= 0 and d > 4:
700
+ if n in magnitude_check:
701
+ magnitude_check[n] += d
702
+ else:
703
+ magnitude_check[n] = d
704
+ extraprec = max(extraprec, d - prec + 60)
705
+ max_total_jump += abs(d)
706
+ while 1:
707
+ if extraprec > maxprec:
708
+ raise ValueError(ctx._hypsum_msg % (prec, prec+extraprec))
709
+ wp = prec + extraprec
710
+ if magnitude_check:
711
+ mag_dict = dict((n,None) for n in magnitude_check)
712
+ else:
713
+ mag_dict = {}
714
+ zv, have_complex, magnitude = summator(coeffs, v, prec, wp, \
715
+ epsshift, mag_dict, **kwargs)
716
+ cancel = -magnitude
717
+ jumps_resolved = True
718
+ if extraprec < max_total_jump:
719
+ for n in mag_dict.values():
720
+ if (n is None) or (n < prec):
721
+ jumps_resolved = False
722
+ break
723
+ accurate = (cancel < extraprec-25-5 or not accurate_small)
724
+ if jumps_resolved:
725
+ if accurate:
726
+ break
727
+ # zero?
728
+ zeroprec = kwargs.get('zeroprec')
729
+ if zeroprec is not None:
730
+ if cancel > zeroprec:
731
+ if have_complex:
732
+ return ctx.mpc(0)
733
+ else:
734
+ return ctx.zero
735
+
736
+ # Some near-singularities were not included, so increase
737
+ # precision and repeat until they are
738
+ extraprec *= 2
739
+ # Possible workaround for bad roundoff in fixed-point arithmetic
740
+ epsshift += 5
741
+ extraprec += 5
742
+
743
+ if type(zv) is tuple:
744
+ if have_complex:
745
+ return ctx.make_mpc(zv)
746
+ else:
747
+ return ctx.make_mpf(zv)
748
+ else:
749
+ return zv
750
+
751
+ def ldexp(ctx, x, n):
752
+ r"""
753
+ Computes `x 2^n` efficiently. No rounding is performed.
754
+ The argument `x` must be a real floating-point number (or
755
+ possible to convert into one) and `n` must be a Python ``int``.
756
+
757
+ >>> from mpmath import *
758
+ >>> mp.dps = 15; mp.pretty = False
759
+ >>> ldexp(1, 10)
760
+ mpf('1024.0')
761
+ >>> ldexp(1, -3)
762
+ mpf('0.125')
763
+
764
+ """
765
+ x = ctx.convert(x)
766
+ return ctx.make_mpf(libmp.mpf_shift(x._mpf_, n))
767
+
768
+ def frexp(ctx, x):
769
+ r"""
770
+ Given a real number `x`, returns `(y, n)` with `y \in [0.5, 1)`,
771
+ `n` a Python integer, and such that `x = y 2^n`. No rounding is
772
+ performed.
773
+
774
+ >>> from mpmath import *
775
+ >>> mp.dps = 15; mp.pretty = False
776
+ >>> frexp(7.5)
777
+ (mpf('0.9375'), 3)
778
+
779
+ """
780
+ x = ctx.convert(x)
781
+ y, n = libmp.mpf_frexp(x._mpf_)
782
+ return ctx.make_mpf(y), n
783
+
784
+ def fneg(ctx, x, **kwargs):
785
+ """
786
+ Negates the number *x*, giving a floating-point result, optionally
787
+ using a custom precision and rounding mode.
788
+
789
+ See the documentation of :func:`~mpmath.fadd` for a detailed description
790
+ of how to specify precision and rounding.
791
+
792
+ **Examples**
793
+
794
+ An mpmath number is returned::
795
+
796
+ >>> from mpmath import *
797
+ >>> mp.dps = 15; mp.pretty = False
798
+ >>> fneg(2.5)
799
+ mpf('-2.5')
800
+ >>> fneg(-5+2j)
801
+ mpc(real='5.0', imag='-2.0')
802
+
803
+ Precise control over rounding is possible::
804
+
805
+ >>> x = fadd(2, 1e-100, exact=True)
806
+ >>> fneg(x)
807
+ mpf('-2.0')
808
+ >>> fneg(x, rounding='f')
809
+ mpf('-2.0000000000000004')
810
+
811
+ Negating with and without roundoff::
812
+
813
+ >>> n = 200000000000000000000001
814
+ >>> print(int(-mpf(n)))
815
+ -200000000000000016777216
816
+ >>> print(int(fneg(n)))
817
+ -200000000000000016777216
818
+ >>> print(int(fneg(n, prec=log(n,2)+1)))
819
+ -200000000000000000000001
820
+ >>> print(int(fneg(n, dps=log(n,10)+1)))
821
+ -200000000000000000000001
822
+ >>> print(int(fneg(n, prec=inf)))
823
+ -200000000000000000000001
824
+ >>> print(int(fneg(n, dps=inf)))
825
+ -200000000000000000000001
826
+ >>> print(int(fneg(n, exact=True)))
827
+ -200000000000000000000001
828
+
829
+ """
830
+ prec, rounding = ctx._parse_prec(kwargs)
831
+ x = ctx.convert(x)
832
+ if hasattr(x, '_mpf_'):
833
+ return ctx.make_mpf(mpf_neg(x._mpf_, prec, rounding))
834
+ if hasattr(x, '_mpc_'):
835
+ return ctx.make_mpc(mpc_neg(x._mpc_, prec, rounding))
836
+ raise ValueError("Arguments need to be mpf or mpc compatible numbers")
837
+
838
+ def fadd(ctx, x, y, **kwargs):
839
+ """
840
+ Adds the numbers *x* and *y*, giving a floating-point result,
841
+ optionally using a custom precision and rounding mode.
842
+
843
+ The default precision is the working precision of the context.
844
+ You can specify a custom precision in bits by passing the *prec* keyword
845
+ argument, or by providing an equivalent decimal precision with the *dps*
846
+ keyword argument. If the precision is set to ``+inf``, or if the flag
847
+ *exact=True* is passed, an exact addition with no rounding is performed.
848
+
849
+ When the precision is finite, the optional *rounding* keyword argument
850
+ specifies the direction of rounding. Valid options are ``'n'`` for
851
+ nearest (default), ``'f'`` for floor, ``'c'`` for ceiling, ``'d'``
852
+ for down, ``'u'`` for up.
853
+
854
+ **Examples**
855
+
856
+ Using :func:`~mpmath.fadd` with precision and rounding control::
857
+
858
+ >>> from mpmath import *
859
+ >>> mp.dps = 15; mp.pretty = False
860
+ >>> fadd(2, 1e-20)
861
+ mpf('2.0')
862
+ >>> fadd(2, 1e-20, rounding='u')
863
+ mpf('2.0000000000000004')
864
+ >>> nprint(fadd(2, 1e-20, prec=100), 25)
865
+ 2.00000000000000000001
866
+ >>> nprint(fadd(2, 1e-20, dps=15), 25)
867
+ 2.0
868
+ >>> nprint(fadd(2, 1e-20, dps=25), 25)
869
+ 2.00000000000000000001
870
+ >>> nprint(fadd(2, 1e-20, exact=True), 25)
871
+ 2.00000000000000000001
872
+
873
+ Exact addition avoids cancellation errors, enforcing familiar laws
874
+ of numbers such as `x+y-x = y`, which don't hold in floating-point
875
+ arithmetic with finite precision::
876
+
877
+ >>> x, y = mpf(2), mpf('1e-1000')
878
+ >>> print(x + y - x)
879
+ 0.0
880
+ >>> print(fadd(x, y, prec=inf) - x)
881
+ 1.0e-1000
882
+ >>> print(fadd(x, y, exact=True) - x)
883
+ 1.0e-1000
884
+
885
+ Exact addition can be inefficient and may be impossible to perform
886
+ with large magnitude differences::
887
+
888
+ >>> fadd(1, '1e-100000000000000000000', prec=inf)
889
+ Traceback (most recent call last):
890
+ ...
891
+ OverflowError: the exact result does not fit in memory
892
+
893
+ """
894
+ prec, rounding = ctx._parse_prec(kwargs)
895
+ x = ctx.convert(x)
896
+ y = ctx.convert(y)
897
+ try:
898
+ if hasattr(x, '_mpf_'):
899
+ if hasattr(y, '_mpf_'):
900
+ return ctx.make_mpf(mpf_add(x._mpf_, y._mpf_, prec, rounding))
901
+ if hasattr(y, '_mpc_'):
902
+ return ctx.make_mpc(mpc_add_mpf(y._mpc_, x._mpf_, prec, rounding))
903
+ if hasattr(x, '_mpc_'):
904
+ if hasattr(y, '_mpf_'):
905
+ return ctx.make_mpc(mpc_add_mpf(x._mpc_, y._mpf_, prec, rounding))
906
+ if hasattr(y, '_mpc_'):
907
+ return ctx.make_mpc(mpc_add(x._mpc_, y._mpc_, prec, rounding))
908
+ except (ValueError, OverflowError):
909
+ raise OverflowError(ctx._exact_overflow_msg)
910
+ raise ValueError("Arguments need to be mpf or mpc compatible numbers")
911
+
912
+ def fsub(ctx, x, y, **kwargs):
913
+ """
914
+ Subtracts the numbers *x* and *y*, giving a floating-point result,
915
+ optionally using a custom precision and rounding mode.
916
+
917
+ See the documentation of :func:`~mpmath.fadd` for a detailed description
918
+ of how to specify precision and rounding.
919
+
920
+ **Examples**
921
+
922
+ Using :func:`~mpmath.fsub` with precision and rounding control::
923
+
924
+ >>> from mpmath import *
925
+ >>> mp.dps = 15; mp.pretty = False
926
+ >>> fsub(2, 1e-20)
927
+ mpf('2.0')
928
+ >>> fsub(2, 1e-20, rounding='d')
929
+ mpf('1.9999999999999998')
930
+ >>> nprint(fsub(2, 1e-20, prec=100), 25)
931
+ 1.99999999999999999999
932
+ >>> nprint(fsub(2, 1e-20, dps=15), 25)
933
+ 2.0
934
+ >>> nprint(fsub(2, 1e-20, dps=25), 25)
935
+ 1.99999999999999999999
936
+ >>> nprint(fsub(2, 1e-20, exact=True), 25)
937
+ 1.99999999999999999999
938
+
939
+ Exact subtraction avoids cancellation errors, enforcing familiar laws
940
+ of numbers such as `x-y+y = x`, which don't hold in floating-point
941
+ arithmetic with finite precision::
942
+
943
+ >>> x, y = mpf(2), mpf('1e1000')
944
+ >>> print(x - y + y)
945
+ 0.0
946
+ >>> print(fsub(x, y, prec=inf) + y)
947
+ 2.0
948
+ >>> print(fsub(x, y, exact=True) + y)
949
+ 2.0
950
+
951
+ Exact addition can be inefficient and may be impossible to perform
952
+ with large magnitude differences::
953
+
954
+ >>> fsub(1, '1e-100000000000000000000', prec=inf)
955
+ Traceback (most recent call last):
956
+ ...
957
+ OverflowError: the exact result does not fit in memory
958
+
959
+ """
960
+ prec, rounding = ctx._parse_prec(kwargs)
961
+ x = ctx.convert(x)
962
+ y = ctx.convert(y)
963
+ try:
964
+ if hasattr(x, '_mpf_'):
965
+ if hasattr(y, '_mpf_'):
966
+ return ctx.make_mpf(mpf_sub(x._mpf_, y._mpf_, prec, rounding))
967
+ if hasattr(y, '_mpc_'):
968
+ return ctx.make_mpc(mpc_sub((x._mpf_, fzero), y._mpc_, prec, rounding))
969
+ if hasattr(x, '_mpc_'):
970
+ if hasattr(y, '_mpf_'):
971
+ return ctx.make_mpc(mpc_sub_mpf(x._mpc_, y._mpf_, prec, rounding))
972
+ if hasattr(y, '_mpc_'):
973
+ return ctx.make_mpc(mpc_sub(x._mpc_, y._mpc_, prec, rounding))
974
+ except (ValueError, OverflowError):
975
+ raise OverflowError(ctx._exact_overflow_msg)
976
+ raise ValueError("Arguments need to be mpf or mpc compatible numbers")
977
+
978
+ def fmul(ctx, x, y, **kwargs):
979
+ """
980
+ Multiplies the numbers *x* and *y*, giving a floating-point result,
981
+ optionally using a custom precision and rounding mode.
982
+
983
+ See the documentation of :func:`~mpmath.fadd` for a detailed description
984
+ of how to specify precision and rounding.
985
+
986
+ **Examples**
987
+
988
+ The result is an mpmath number::
989
+
990
+ >>> from mpmath import *
991
+ >>> mp.dps = 15; mp.pretty = False
992
+ >>> fmul(2, 5.0)
993
+ mpf('10.0')
994
+ >>> fmul(0.5j, 0.5)
995
+ mpc(real='0.0', imag='0.25')
996
+
997
+ Avoiding roundoff::
998
+
999
+ >>> x, y = 10**10+1, 10**15+1
1000
+ >>> print(x*y)
1001
+ 10000000001000010000000001
1002
+ >>> print(mpf(x) * mpf(y))
1003
+ 1.0000000001e+25
1004
+ >>> print(int(mpf(x) * mpf(y)))
1005
+ 10000000001000011026399232
1006
+ >>> print(int(fmul(x, y)))
1007
+ 10000000001000011026399232
1008
+ >>> print(int(fmul(x, y, dps=25)))
1009
+ 10000000001000010000000001
1010
+ >>> print(int(fmul(x, y, exact=True)))
1011
+ 10000000001000010000000001
1012
+
1013
+ Exact multiplication with complex numbers can be inefficient and may
1014
+ be impossible to perform with large magnitude differences between
1015
+ real and imaginary parts::
1016
+
1017
+ >>> x = 1+2j
1018
+ >>> y = mpc(2, '1e-100000000000000000000')
1019
+ >>> fmul(x, y)
1020
+ mpc(real='2.0', imag='4.0')
1021
+ >>> fmul(x, y, rounding='u')
1022
+ mpc(real='2.0', imag='4.0000000000000009')
1023
+ >>> fmul(x, y, exact=True)
1024
+ Traceback (most recent call last):
1025
+ ...
1026
+ OverflowError: the exact result does not fit in memory
1027
+
1028
+ """
1029
+ prec, rounding = ctx._parse_prec(kwargs)
1030
+ x = ctx.convert(x)
1031
+ y = ctx.convert(y)
1032
+ try:
1033
+ if hasattr(x, '_mpf_'):
1034
+ if hasattr(y, '_mpf_'):
1035
+ return ctx.make_mpf(mpf_mul(x._mpf_, y._mpf_, prec, rounding))
1036
+ if hasattr(y, '_mpc_'):
1037
+ return ctx.make_mpc(mpc_mul_mpf(y._mpc_, x._mpf_, prec, rounding))
1038
+ if hasattr(x, '_mpc_'):
1039
+ if hasattr(y, '_mpf_'):
1040
+ return ctx.make_mpc(mpc_mul_mpf(x._mpc_, y._mpf_, prec, rounding))
1041
+ if hasattr(y, '_mpc_'):
1042
+ return ctx.make_mpc(mpc_mul(x._mpc_, y._mpc_, prec, rounding))
1043
+ except (ValueError, OverflowError):
1044
+ raise OverflowError(ctx._exact_overflow_msg)
1045
+ raise ValueError("Arguments need to be mpf or mpc compatible numbers")
1046
+
1047
+ def fdiv(ctx, x, y, **kwargs):
1048
+ """
1049
+ Divides the numbers *x* and *y*, giving a floating-point result,
1050
+ optionally using a custom precision and rounding mode.
1051
+
1052
+ See the documentation of :func:`~mpmath.fadd` for a detailed description
1053
+ of how to specify precision and rounding.
1054
+
1055
+ **Examples**
1056
+
1057
+ The result is an mpmath number::
1058
+
1059
+ >>> from mpmath import *
1060
+ >>> mp.dps = 15; mp.pretty = False
1061
+ >>> fdiv(3, 2)
1062
+ mpf('1.5')
1063
+ >>> fdiv(2, 3)
1064
+ mpf('0.66666666666666663')
1065
+ >>> fdiv(2+4j, 0.5)
1066
+ mpc(real='4.0', imag='8.0')
1067
+
1068
+ The rounding direction and precision can be controlled::
1069
+
1070
+ >>> fdiv(2, 3, dps=3) # Should be accurate to at least 3 digits
1071
+ mpf('0.6666259765625')
1072
+ >>> fdiv(2, 3, rounding='d')
1073
+ mpf('0.66666666666666663')
1074
+ >>> fdiv(2, 3, prec=60)
1075
+ mpf('0.66666666666666667')
1076
+ >>> fdiv(2, 3, rounding='u')
1077
+ mpf('0.66666666666666674')
1078
+
1079
+ Checking the error of a division by performing it at higher precision::
1080
+
1081
+ >>> fdiv(2, 3) - fdiv(2, 3, prec=100)
1082
+ mpf('-3.7007434154172148e-17')
1083
+
1084
+ Unlike :func:`~mpmath.fadd`, :func:`~mpmath.fmul`, etc., exact division is not
1085
+ allowed since the quotient of two floating-point numbers generally
1086
+ does not have an exact floating-point representation. (In the
1087
+ future this might be changed to allow the case where the division
1088
+ is actually exact.)
1089
+
1090
+ >>> fdiv(2, 3, exact=True)
1091
+ Traceback (most recent call last):
1092
+ ...
1093
+ ValueError: division is not an exact operation
1094
+
1095
+ """
1096
+ prec, rounding = ctx._parse_prec(kwargs)
1097
+ if not prec:
1098
+ raise ValueError("division is not an exact operation")
1099
+ x = ctx.convert(x)
1100
+ y = ctx.convert(y)
1101
+ if hasattr(x, '_mpf_'):
1102
+ if hasattr(y, '_mpf_'):
1103
+ return ctx.make_mpf(mpf_div(x._mpf_, y._mpf_, prec, rounding))
1104
+ if hasattr(y, '_mpc_'):
1105
+ return ctx.make_mpc(mpc_div((x._mpf_, fzero), y._mpc_, prec, rounding))
1106
+ if hasattr(x, '_mpc_'):
1107
+ if hasattr(y, '_mpf_'):
1108
+ return ctx.make_mpc(mpc_div_mpf(x._mpc_, y._mpf_, prec, rounding))
1109
+ if hasattr(y, '_mpc_'):
1110
+ return ctx.make_mpc(mpc_div(x._mpc_, y._mpc_, prec, rounding))
1111
+ raise ValueError("Arguments need to be mpf or mpc compatible numbers")
1112
+
1113
+ def nint_distance(ctx, x):
1114
+ r"""
1115
+ Return `(n,d)` where `n` is the nearest integer to `x` and `d` is
1116
+ an estimate of `\log_2(|x-n|)`. If `d < 0`, `-d` gives the precision
1117
+ (measured in bits) lost to cancellation when computing `x-n`.
1118
+
1119
+ >>> from mpmath import *
1120
+ >>> n, d = nint_distance(5)
1121
+ >>> print(n); print(d)
1122
+ 5
1123
+ -inf
1124
+ >>> n, d = nint_distance(mpf(5))
1125
+ >>> print(n); print(d)
1126
+ 5
1127
+ -inf
1128
+ >>> n, d = nint_distance(mpf(5.00000001))
1129
+ >>> print(n); print(d)
1130
+ 5
1131
+ -26
1132
+ >>> n, d = nint_distance(mpf(4.99999999))
1133
+ >>> print(n); print(d)
1134
+ 5
1135
+ -26
1136
+ >>> n, d = nint_distance(mpc(5,10))
1137
+ >>> print(n); print(d)
1138
+ 5
1139
+ 4
1140
+ >>> n, d = nint_distance(mpc(5,0.000001))
1141
+ >>> print(n); print(d)
1142
+ 5
1143
+ -19
1144
+
1145
+ """
1146
+ typx = type(x)
1147
+ if typx in int_types:
1148
+ return int(x), ctx.ninf
1149
+ elif typx is rational.mpq:
1150
+ p, q = x._mpq_
1151
+ n, r = divmod(p, q)
1152
+ if 2*r >= q:
1153
+ n += 1
1154
+ elif not r:
1155
+ return n, ctx.ninf
1156
+ # log(p/q-n) = log((p-nq)/q) = log(p-nq) - log(q)
1157
+ d = bitcount(abs(p-n*q)) - bitcount(q)
1158
+ return n, d
1159
+ if hasattr(x, "_mpf_"):
1160
+ re = x._mpf_
1161
+ im_dist = ctx.ninf
1162
+ elif hasattr(x, "_mpc_"):
1163
+ re, im = x._mpc_
1164
+ isign, iman, iexp, ibc = im
1165
+ if iman:
1166
+ im_dist = iexp + ibc
1167
+ elif im == fzero:
1168
+ im_dist = ctx.ninf
1169
+ else:
1170
+ raise ValueError("requires a finite number")
1171
+ else:
1172
+ x = ctx.convert(x)
1173
+ if hasattr(x, "_mpf_") or hasattr(x, "_mpc_"):
1174
+ return ctx.nint_distance(x)
1175
+ else:
1176
+ raise TypeError("requires an mpf/mpc")
1177
+ sign, man, exp, bc = re
1178
+ mag = exp+bc
1179
+ # |x| < 0.5
1180
+ if mag < 0:
1181
+ n = 0
1182
+ re_dist = mag
1183
+ elif man:
1184
+ # exact integer
1185
+ if exp >= 0:
1186
+ n = man << exp
1187
+ re_dist = ctx.ninf
1188
+ # exact half-integer
1189
+ elif exp == -1:
1190
+ n = (man>>1)+1
1191
+ re_dist = 0
1192
+ else:
1193
+ d = (-exp-1)
1194
+ t = man >> d
1195
+ if t & 1:
1196
+ t += 1
1197
+ man = (t<<d) - man
1198
+ else:
1199
+ man -= (t<<d)
1200
+ n = t>>1 # int(t)>>1
1201
+ re_dist = exp+bitcount(man)
1202
+ if sign:
1203
+ n = -n
1204
+ elif re == fzero:
1205
+ re_dist = ctx.ninf
1206
+ n = 0
1207
+ else:
1208
+ raise ValueError("requires a finite number")
1209
+ return n, max(re_dist, im_dist)
1210
+
1211
+ def fprod(ctx, factors):
1212
+ r"""
1213
+ Calculates a product containing a finite number of factors (for
1214
+ infinite products, see :func:`~mpmath.nprod`). The factors will be
1215
+ converted to mpmath numbers.
1216
+
1217
+ >>> from mpmath import *
1218
+ >>> mp.dps = 15; mp.pretty = False
1219
+ >>> fprod([1, 2, 0.5, 7])
1220
+ mpf('7.0')
1221
+
1222
+ """
1223
+ orig = ctx.prec
1224
+ try:
1225
+ v = ctx.one
1226
+ for p in factors:
1227
+ v *= p
1228
+ finally:
1229
+ ctx.prec = orig
1230
+ return +v
1231
+
1232
+ def rand(ctx):
1233
+ """
1234
+ Returns an ``mpf`` with value chosen randomly from `[0, 1)`.
1235
+ The number of randomly generated bits in the mantissa is equal
1236
+ to the working precision.
1237
+ """
1238
+ return ctx.make_mpf(mpf_rand(ctx._prec))
1239
+
1240
+ def fraction(ctx, p, q):
1241
+ """
1242
+ Given Python integers `(p, q)`, returns a lazy ``mpf`` representing
1243
+ the fraction `p/q`. The value is updated with the precision.
1244
+
1245
+ >>> from mpmath import *
1246
+ >>> mp.dps = 15
1247
+ >>> a = fraction(1,100)
1248
+ >>> b = mpf(1)/100
1249
+ >>> print(a); print(b)
1250
+ 0.01
1251
+ 0.01
1252
+ >>> mp.dps = 30
1253
+ >>> print(a); print(b) # a will be accurate
1254
+ 0.01
1255
+ 0.0100000000000000002081668171172
1256
+ >>> mp.dps = 15
1257
+ """
1258
+ return ctx.constant(lambda prec, rnd: from_rational(p, q, prec, rnd),
1259
+ '%s/%s' % (p, q))
1260
+
1261
+ def absmin(ctx, x):
1262
+ return abs(ctx.convert(x))
1263
+
1264
+ def absmax(ctx, x):
1265
+ return abs(ctx.convert(x))
1266
+
1267
+ def _as_points(ctx, x):
1268
+ # XXX: remove this?
1269
+ if hasattr(x, '_mpi_'):
1270
+ a, b = x._mpi_
1271
+ return [ctx.make_mpf(a), ctx.make_mpf(b)]
1272
+ return x
1273
+
1274
+ '''
1275
+ def _zetasum(ctx, s, a, b):
1276
+ """
1277
+ Computes sum of k^(-s) for k = a, a+1, ..., b with a, b both small
1278
+ integers.
1279
+ """
1280
+ a = int(a)
1281
+ b = int(b)
1282
+ s = ctx.convert(s)
1283
+ prec, rounding = ctx._prec_rounding
1284
+ if hasattr(s, '_mpf_'):
1285
+ v = ctx.make_mpf(libmp.mpf_zetasum(s._mpf_, a, b, prec))
1286
+ elif hasattr(s, '_mpc_'):
1287
+ v = ctx.make_mpc(libmp.mpc_zetasum(s._mpc_, a, b, prec))
1288
+ return v
1289
+ '''
1290
+
1291
+ def _zetasum_fast(ctx, s, a, n, derivatives=[0], reflect=False):
1292
+ if not (ctx.isint(a) and hasattr(s, "_mpc_")):
1293
+ raise NotImplementedError
1294
+ a = int(a)
1295
+ prec = ctx._prec
1296
+ xs, ys = libmp.mpc_zetasum(s._mpc_, a, n, derivatives, reflect, prec)
1297
+ xs = [ctx.make_mpc(x) for x in xs]
1298
+ ys = [ctx.make_mpc(y) for y in ys]
1299
+ return xs, ys
1300
+
1301
+ class PrecisionManager:
1302
+ def __init__(self, ctx, precfun, dpsfun, normalize_output=False):
1303
+ self.ctx = ctx
1304
+ self.precfun = precfun
1305
+ self.dpsfun = dpsfun
1306
+ self.normalize_output = normalize_output
1307
+ def __call__(self, f):
1308
+ @functools.wraps(f)
1309
+ def g(*args, **kwargs):
1310
+ orig = self.ctx.prec
1311
+ try:
1312
+ if self.precfun:
1313
+ self.ctx.prec = self.precfun(self.ctx.prec)
1314
+ else:
1315
+ self.ctx.dps = self.dpsfun(self.ctx.dps)
1316
+ if self.normalize_output:
1317
+ v = f(*args, **kwargs)
1318
+ if type(v) is tuple:
1319
+ return tuple([+a for a in v])
1320
+ return +v
1321
+ else:
1322
+ return f(*args, **kwargs)
1323
+ finally:
1324
+ self.ctx.prec = orig
1325
+ return g
1326
+ def __enter__(self):
1327
+ self.origp = self.ctx.prec
1328
+ if self.precfun:
1329
+ self.ctx.prec = self.precfun(self.ctx.prec)
1330
+ else:
1331
+ self.ctx.dps = self.dpsfun(self.ctx.dps)
1332
+ def __exit__(self, exc_type, exc_val, exc_tb):
1333
+ self.ctx.prec = self.origp
1334
+ return False
1335
+
1336
+
1337
+ if __name__ == '__main__':
1338
+ import doctest
1339
+ doctest.testmod()
llmeval-env/lib/python3.10/site-packages/mpmath/ctx_mp_python.py ADDED
@@ -0,0 +1,1149 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #from ctx_base import StandardBaseContext
2
+
3
+ from .libmp.backend import basestring, exec_
4
+
5
+ from .libmp import (MPZ, MPZ_ZERO, MPZ_ONE, int_types, repr_dps,
6
+ round_floor, round_ceiling, dps_to_prec, round_nearest, prec_to_dps,
7
+ ComplexResult, to_pickable, from_pickable, normalize,
8
+ from_int, from_float, from_npfloat, from_Decimal, from_str, to_int, to_float, to_str,
9
+ from_rational, from_man_exp,
10
+ fone, fzero, finf, fninf, fnan,
11
+ mpf_abs, mpf_pos, mpf_neg, mpf_add, mpf_sub, mpf_mul, mpf_mul_int,
12
+ mpf_div, mpf_rdiv_int, mpf_pow_int, mpf_mod,
13
+ mpf_eq, mpf_cmp, mpf_lt, mpf_gt, mpf_le, mpf_ge,
14
+ mpf_hash, mpf_rand,
15
+ mpf_sum,
16
+ bitcount, to_fixed,
17
+ mpc_to_str,
18
+ mpc_to_complex, mpc_hash, mpc_pos, mpc_is_nonzero, mpc_neg, mpc_conjugate,
19
+ mpc_abs, mpc_add, mpc_add_mpf, mpc_sub, mpc_sub_mpf, mpc_mul, mpc_mul_mpf,
20
+ mpc_mul_int, mpc_div, mpc_div_mpf, mpc_pow, mpc_pow_mpf, mpc_pow_int,
21
+ mpc_mpf_div,
22
+ mpf_pow,
23
+ mpf_pi, mpf_degree, mpf_e, mpf_phi, mpf_ln2, mpf_ln10,
24
+ mpf_euler, mpf_catalan, mpf_apery, mpf_khinchin,
25
+ mpf_glaisher, mpf_twinprime, mpf_mertens,
26
+ int_types)
27
+
28
+ from . import rational
29
+ from . import function_docs
30
+
31
+ new = object.__new__
32
+
33
+ class mpnumeric(object):
34
+ """Base class for mpf and mpc."""
35
+ __slots__ = []
36
+ def __new__(cls, val):
37
+ raise NotImplementedError
38
+
39
+ class _mpf(mpnumeric):
40
+ """
41
+ An mpf instance holds a real-valued floating-point number. mpf:s
42
+ work analogously to Python floats, but support arbitrary-precision
43
+ arithmetic.
44
+ """
45
+ __slots__ = ['_mpf_']
46
+
47
+ def __new__(cls, val=fzero, **kwargs):
48
+ """A new mpf can be created from a Python float, an int, a
49
+ or a decimal string representing a number in floating-point
50
+ format."""
51
+ prec, rounding = cls.context._prec_rounding
52
+ if kwargs:
53
+ prec = kwargs.get('prec', prec)
54
+ if 'dps' in kwargs:
55
+ prec = dps_to_prec(kwargs['dps'])
56
+ rounding = kwargs.get('rounding', rounding)
57
+ if type(val) is cls:
58
+ sign, man, exp, bc = val._mpf_
59
+ if (not man) and exp:
60
+ return val
61
+ v = new(cls)
62
+ v._mpf_ = normalize(sign, man, exp, bc, prec, rounding)
63
+ return v
64
+ elif type(val) is tuple:
65
+ if len(val) == 2:
66
+ v = new(cls)
67
+ v._mpf_ = from_man_exp(val[0], val[1], prec, rounding)
68
+ return v
69
+ if len(val) == 4:
70
+ if val not in (finf, fninf, fnan):
71
+ sign, man, exp, bc = val
72
+ val = normalize(sign, MPZ(man), exp, bc, prec, rounding)
73
+ v = new(cls)
74
+ v._mpf_ = val
75
+ return v
76
+ raise ValueError
77
+ else:
78
+ v = new(cls)
79
+ v._mpf_ = mpf_pos(cls.mpf_convert_arg(val, prec, rounding), prec, rounding)
80
+ return v
81
+
82
+ @classmethod
83
+ def mpf_convert_arg(cls, x, prec, rounding):
84
+ if isinstance(x, int_types): return from_int(x)
85
+ if isinstance(x, float): return from_float(x)
86
+ if isinstance(x, basestring): return from_str(x, prec, rounding)
87
+ if isinstance(x, cls.context.constant): return x.func(prec, rounding)
88
+ if hasattr(x, '_mpf_'): return x._mpf_
89
+ if hasattr(x, '_mpmath_'):
90
+ t = cls.context.convert(x._mpmath_(prec, rounding))
91
+ if hasattr(t, '_mpf_'):
92
+ return t._mpf_
93
+ if hasattr(x, '_mpi_'):
94
+ a, b = x._mpi_
95
+ if a == b:
96
+ return a
97
+ raise ValueError("can only create mpf from zero-width interval")
98
+ raise TypeError("cannot create mpf from " + repr(x))
99
+
100
+ @classmethod
101
+ def mpf_convert_rhs(cls, x):
102
+ if isinstance(x, int_types): return from_int(x)
103
+ if isinstance(x, float): return from_float(x)
104
+ if isinstance(x, complex_types): return cls.context.mpc(x)
105
+ if isinstance(x, rational.mpq):
106
+ p, q = x._mpq_
107
+ return from_rational(p, q, cls.context.prec)
108
+ if hasattr(x, '_mpf_'): return x._mpf_
109
+ if hasattr(x, '_mpmath_'):
110
+ t = cls.context.convert(x._mpmath_(*cls.context._prec_rounding))
111
+ if hasattr(t, '_mpf_'):
112
+ return t._mpf_
113
+ return t
114
+ return NotImplemented
115
+
116
+ @classmethod
117
+ def mpf_convert_lhs(cls, x):
118
+ x = cls.mpf_convert_rhs(x)
119
+ if type(x) is tuple:
120
+ return cls.context.make_mpf(x)
121
+ return x
122
+
123
+ man_exp = property(lambda self: self._mpf_[1:3])
124
+ man = property(lambda self: self._mpf_[1])
125
+ exp = property(lambda self: self._mpf_[2])
126
+ bc = property(lambda self: self._mpf_[3])
127
+
128
+ real = property(lambda self: self)
129
+ imag = property(lambda self: self.context.zero)
130
+
131
+ conjugate = lambda self: self
132
+
133
+ def __getstate__(self): return to_pickable(self._mpf_)
134
+ def __setstate__(self, val): self._mpf_ = from_pickable(val)
135
+
136
+ def __repr__(s):
137
+ if s.context.pretty:
138
+ return str(s)
139
+ return "mpf('%s')" % to_str(s._mpf_, s.context._repr_digits)
140
+
141
+ def __str__(s): return to_str(s._mpf_, s.context._str_digits)
142
+ def __hash__(s): return mpf_hash(s._mpf_)
143
+ def __int__(s): return int(to_int(s._mpf_))
144
+ def __long__(s): return long(to_int(s._mpf_))
145
+ def __float__(s): return to_float(s._mpf_, rnd=s.context._prec_rounding[1])
146
+ def __complex__(s): return complex(float(s))
147
+ def __nonzero__(s): return s._mpf_ != fzero
148
+
149
+ __bool__ = __nonzero__
150
+
151
+ def __abs__(s):
152
+ cls, new, (prec, rounding) = s._ctxdata
153
+ v = new(cls)
154
+ v._mpf_ = mpf_abs(s._mpf_, prec, rounding)
155
+ return v
156
+
157
+ def __pos__(s):
158
+ cls, new, (prec, rounding) = s._ctxdata
159
+ v = new(cls)
160
+ v._mpf_ = mpf_pos(s._mpf_, prec, rounding)
161
+ return v
162
+
163
+ def __neg__(s):
164
+ cls, new, (prec, rounding) = s._ctxdata
165
+ v = new(cls)
166
+ v._mpf_ = mpf_neg(s._mpf_, prec, rounding)
167
+ return v
168
+
169
+ def _cmp(s, t, func):
170
+ if hasattr(t, '_mpf_'):
171
+ t = t._mpf_
172
+ else:
173
+ t = s.mpf_convert_rhs(t)
174
+ if t is NotImplemented:
175
+ return t
176
+ return func(s._mpf_, t)
177
+
178
+ def __cmp__(s, t): return s._cmp(t, mpf_cmp)
179
+ def __lt__(s, t): return s._cmp(t, mpf_lt)
180
+ def __gt__(s, t): return s._cmp(t, mpf_gt)
181
+ def __le__(s, t): return s._cmp(t, mpf_le)
182
+ def __ge__(s, t): return s._cmp(t, mpf_ge)
183
+
184
+ def __ne__(s, t):
185
+ v = s.__eq__(t)
186
+ if v is NotImplemented:
187
+ return v
188
+ return not v
189
+
190
+ def __rsub__(s, t):
191
+ cls, new, (prec, rounding) = s._ctxdata
192
+ if type(t) in int_types:
193
+ v = new(cls)
194
+ v._mpf_ = mpf_sub(from_int(t), s._mpf_, prec, rounding)
195
+ return v
196
+ t = s.mpf_convert_lhs(t)
197
+ if t is NotImplemented:
198
+ return t
199
+ return t - s
200
+
201
+ def __rdiv__(s, t):
202
+ cls, new, (prec, rounding) = s._ctxdata
203
+ if isinstance(t, int_types):
204
+ v = new(cls)
205
+ v._mpf_ = mpf_rdiv_int(t, s._mpf_, prec, rounding)
206
+ return v
207
+ t = s.mpf_convert_lhs(t)
208
+ if t is NotImplemented:
209
+ return t
210
+ return t / s
211
+
212
+ def __rpow__(s, t):
213
+ t = s.mpf_convert_lhs(t)
214
+ if t is NotImplemented:
215
+ return t
216
+ return t ** s
217
+
218
+ def __rmod__(s, t):
219
+ t = s.mpf_convert_lhs(t)
220
+ if t is NotImplemented:
221
+ return t
222
+ return t % s
223
+
224
+ def sqrt(s):
225
+ return s.context.sqrt(s)
226
+
227
+ def ae(s, t, rel_eps=None, abs_eps=None):
228
+ return s.context.almosteq(s, t, rel_eps, abs_eps)
229
+
230
+ def to_fixed(self, prec):
231
+ return to_fixed(self._mpf_, prec)
232
+
233
+ def __round__(self, *args):
234
+ return round(float(self), *args)
235
+
236
+ mpf_binary_op = """
237
+ def %NAME%(self, other):
238
+ mpf, new, (prec, rounding) = self._ctxdata
239
+ sval = self._mpf_
240
+ if hasattr(other, '_mpf_'):
241
+ tval = other._mpf_
242
+ %WITH_MPF%
243
+ ttype = type(other)
244
+ if ttype in int_types:
245
+ %WITH_INT%
246
+ elif ttype is float:
247
+ tval = from_float(other)
248
+ %WITH_MPF%
249
+ elif hasattr(other, '_mpc_'):
250
+ tval = other._mpc_
251
+ mpc = type(other)
252
+ %WITH_MPC%
253
+ elif ttype is complex:
254
+ tval = from_float(other.real), from_float(other.imag)
255
+ mpc = self.context.mpc
256
+ %WITH_MPC%
257
+ if isinstance(other, mpnumeric):
258
+ return NotImplemented
259
+ try:
260
+ other = mpf.context.convert(other, strings=False)
261
+ except TypeError:
262
+ return NotImplemented
263
+ return self.%NAME%(other)
264
+ """
265
+
266
+ return_mpf = "; obj = new(mpf); obj._mpf_ = val; return obj"
267
+ return_mpc = "; obj = new(mpc); obj._mpc_ = val; return obj"
268
+
269
+ mpf_pow_same = """
270
+ try:
271
+ val = mpf_pow(sval, tval, prec, rounding) %s
272
+ except ComplexResult:
273
+ if mpf.context.trap_complex:
274
+ raise
275
+ mpc = mpf.context.mpc
276
+ val = mpc_pow((sval, fzero), (tval, fzero), prec, rounding) %s
277
+ """ % (return_mpf, return_mpc)
278
+
279
+ def binary_op(name, with_mpf='', with_int='', with_mpc=''):
280
+ code = mpf_binary_op
281
+ code = code.replace("%WITH_INT%", with_int)
282
+ code = code.replace("%WITH_MPC%", with_mpc)
283
+ code = code.replace("%WITH_MPF%", with_mpf)
284
+ code = code.replace("%NAME%", name)
285
+ np = {}
286
+ exec_(code, globals(), np)
287
+ return np[name]
288
+
289
+ _mpf.__eq__ = binary_op('__eq__',
290
+ 'return mpf_eq(sval, tval)',
291
+ 'return mpf_eq(sval, from_int(other))',
292
+ 'return (tval[1] == fzero) and mpf_eq(tval[0], sval)')
293
+
294
+ _mpf.__add__ = binary_op('__add__',
295
+ 'val = mpf_add(sval, tval, prec, rounding)' + return_mpf,
296
+ 'val = mpf_add(sval, from_int(other), prec, rounding)' + return_mpf,
297
+ 'val = mpc_add_mpf(tval, sval, prec, rounding)' + return_mpc)
298
+
299
+ _mpf.__sub__ = binary_op('__sub__',
300
+ 'val = mpf_sub(sval, tval, prec, rounding)' + return_mpf,
301
+ 'val = mpf_sub(sval, from_int(other), prec, rounding)' + return_mpf,
302
+ 'val = mpc_sub((sval, fzero), tval, prec, rounding)' + return_mpc)
303
+
304
+ _mpf.__mul__ = binary_op('__mul__',
305
+ 'val = mpf_mul(sval, tval, prec, rounding)' + return_mpf,
306
+ 'val = mpf_mul_int(sval, other, prec, rounding)' + return_mpf,
307
+ 'val = mpc_mul_mpf(tval, sval, prec, rounding)' + return_mpc)
308
+
309
+ _mpf.__div__ = binary_op('__div__',
310
+ 'val = mpf_div(sval, tval, prec, rounding)' + return_mpf,
311
+ 'val = mpf_div(sval, from_int(other), prec, rounding)' + return_mpf,
312
+ 'val = mpc_mpf_div(sval, tval, prec, rounding)' + return_mpc)
313
+
314
+ _mpf.__mod__ = binary_op('__mod__',
315
+ 'val = mpf_mod(sval, tval, prec, rounding)' + return_mpf,
316
+ 'val = mpf_mod(sval, from_int(other), prec, rounding)' + return_mpf,
317
+ 'raise NotImplementedError("complex modulo")')
318
+
319
+ _mpf.__pow__ = binary_op('__pow__',
320
+ mpf_pow_same,
321
+ 'val = mpf_pow_int(sval, other, prec, rounding)' + return_mpf,
322
+ 'val = mpc_pow((sval, fzero), tval, prec, rounding)' + return_mpc)
323
+
324
+ _mpf.__radd__ = _mpf.__add__
325
+ _mpf.__rmul__ = _mpf.__mul__
326
+ _mpf.__truediv__ = _mpf.__div__
327
+ _mpf.__rtruediv__ = _mpf.__rdiv__
328
+
329
+
330
+ class _constant(_mpf):
331
+ """Represents a mathematical constant with dynamic precision.
332
+ When printed or used in an arithmetic operation, a constant
333
+ is converted to a regular mpf at the working precision. A
334
+ regular mpf can also be obtained using the operation +x."""
335
+
336
+ def __new__(cls, func, name, docname=''):
337
+ a = object.__new__(cls)
338
+ a.name = name
339
+ a.func = func
340
+ a.__doc__ = getattr(function_docs, docname, '')
341
+ return a
342
+
343
+ def __call__(self, prec=None, dps=None, rounding=None):
344
+ prec2, rounding2 = self.context._prec_rounding
345
+ if not prec: prec = prec2
346
+ if not rounding: rounding = rounding2
347
+ if dps: prec = dps_to_prec(dps)
348
+ return self.context.make_mpf(self.func(prec, rounding))
349
+
350
+ @property
351
+ def _mpf_(self):
352
+ prec, rounding = self.context._prec_rounding
353
+ return self.func(prec, rounding)
354
+
355
+ def __repr__(self):
356
+ return "<%s: %s~>" % (self.name, self.context.nstr(self(dps=15)))
357
+
358
+
359
+ class _mpc(mpnumeric):
360
+ """
361
+ An mpc represents a complex number using a pair of mpf:s (one
362
+ for the real part and another for the imaginary part.) The mpc
363
+ class behaves fairly similarly to Python's complex type.
364
+ """
365
+
366
+ __slots__ = ['_mpc_']
367
+
368
+ def __new__(cls, real=0, imag=0):
369
+ s = object.__new__(cls)
370
+ if isinstance(real, complex_types):
371
+ real, imag = real.real, real.imag
372
+ elif hasattr(real, '_mpc_'):
373
+ s._mpc_ = real._mpc_
374
+ return s
375
+ real = cls.context.mpf(real)
376
+ imag = cls.context.mpf(imag)
377
+ s._mpc_ = (real._mpf_, imag._mpf_)
378
+ return s
379
+
380
+ real = property(lambda self: self.context.make_mpf(self._mpc_[0]))
381
+ imag = property(lambda self: self.context.make_mpf(self._mpc_[1]))
382
+
383
+ def __getstate__(self):
384
+ return to_pickable(self._mpc_[0]), to_pickable(self._mpc_[1])
385
+
386
+ def __setstate__(self, val):
387
+ self._mpc_ = from_pickable(val[0]), from_pickable(val[1])
388
+
389
+ def __repr__(s):
390
+ if s.context.pretty:
391
+ return str(s)
392
+ r = repr(s.real)[4:-1]
393
+ i = repr(s.imag)[4:-1]
394
+ return "%s(real=%s, imag=%s)" % (type(s).__name__, r, i)
395
+
396
+ def __str__(s):
397
+ return "(%s)" % mpc_to_str(s._mpc_, s.context._str_digits)
398
+
399
+ def __complex__(s):
400
+ return mpc_to_complex(s._mpc_, rnd=s.context._prec_rounding[1])
401
+
402
+ def __pos__(s):
403
+ cls, new, (prec, rounding) = s._ctxdata
404
+ v = new(cls)
405
+ v._mpc_ = mpc_pos(s._mpc_, prec, rounding)
406
+ return v
407
+
408
+ def __abs__(s):
409
+ prec, rounding = s.context._prec_rounding
410
+ v = new(s.context.mpf)
411
+ v._mpf_ = mpc_abs(s._mpc_, prec, rounding)
412
+ return v
413
+
414
+ def __neg__(s):
415
+ cls, new, (prec, rounding) = s._ctxdata
416
+ v = new(cls)
417
+ v._mpc_ = mpc_neg(s._mpc_, prec, rounding)
418
+ return v
419
+
420
+ def conjugate(s):
421
+ cls, new, (prec, rounding) = s._ctxdata
422
+ v = new(cls)
423
+ v._mpc_ = mpc_conjugate(s._mpc_, prec, rounding)
424
+ return v
425
+
426
+ def __nonzero__(s):
427
+ return mpc_is_nonzero(s._mpc_)
428
+
429
+ __bool__ = __nonzero__
430
+
431
+ def __hash__(s):
432
+ return mpc_hash(s._mpc_)
433
+
434
+ @classmethod
435
+ def mpc_convert_lhs(cls, x):
436
+ try:
437
+ y = cls.context.convert(x)
438
+ return y
439
+ except TypeError:
440
+ return NotImplemented
441
+
442
+ def __eq__(s, t):
443
+ if not hasattr(t, '_mpc_'):
444
+ if isinstance(t, str):
445
+ return False
446
+ t = s.mpc_convert_lhs(t)
447
+ if t is NotImplemented:
448
+ return t
449
+ return s.real == t.real and s.imag == t.imag
450
+
451
+ def __ne__(s, t):
452
+ b = s.__eq__(t)
453
+ if b is NotImplemented:
454
+ return b
455
+ return not b
456
+
457
+ def _compare(*args):
458
+ raise TypeError("no ordering relation is defined for complex numbers")
459
+
460
+ __gt__ = _compare
461
+ __le__ = _compare
462
+ __gt__ = _compare
463
+ __ge__ = _compare
464
+
465
+ def __add__(s, t):
466
+ cls, new, (prec, rounding) = s._ctxdata
467
+ if not hasattr(t, '_mpc_'):
468
+ t = s.mpc_convert_lhs(t)
469
+ if t is NotImplemented:
470
+ return t
471
+ if hasattr(t, '_mpf_'):
472
+ v = new(cls)
473
+ v._mpc_ = mpc_add_mpf(s._mpc_, t._mpf_, prec, rounding)
474
+ return v
475
+ v = new(cls)
476
+ v._mpc_ = mpc_add(s._mpc_, t._mpc_, prec, rounding)
477
+ return v
478
+
479
+ def __sub__(s, t):
480
+ cls, new, (prec, rounding) = s._ctxdata
481
+ if not hasattr(t, '_mpc_'):
482
+ t = s.mpc_convert_lhs(t)
483
+ if t is NotImplemented:
484
+ return t
485
+ if hasattr(t, '_mpf_'):
486
+ v = new(cls)
487
+ v._mpc_ = mpc_sub_mpf(s._mpc_, t._mpf_, prec, rounding)
488
+ return v
489
+ v = new(cls)
490
+ v._mpc_ = mpc_sub(s._mpc_, t._mpc_, prec, rounding)
491
+ return v
492
+
493
+ def __mul__(s, t):
494
+ cls, new, (prec, rounding) = s._ctxdata
495
+ if not hasattr(t, '_mpc_'):
496
+ if isinstance(t, int_types):
497
+ v = new(cls)
498
+ v._mpc_ = mpc_mul_int(s._mpc_, t, prec, rounding)
499
+ return v
500
+ t = s.mpc_convert_lhs(t)
501
+ if t is NotImplemented:
502
+ return t
503
+ if hasattr(t, '_mpf_'):
504
+ v = new(cls)
505
+ v._mpc_ = mpc_mul_mpf(s._mpc_, t._mpf_, prec, rounding)
506
+ return v
507
+ t = s.mpc_convert_lhs(t)
508
+ v = new(cls)
509
+ v._mpc_ = mpc_mul(s._mpc_, t._mpc_, prec, rounding)
510
+ return v
511
+
512
+ def __div__(s, t):
513
+ cls, new, (prec, rounding) = s._ctxdata
514
+ if not hasattr(t, '_mpc_'):
515
+ t = s.mpc_convert_lhs(t)
516
+ if t is NotImplemented:
517
+ return t
518
+ if hasattr(t, '_mpf_'):
519
+ v = new(cls)
520
+ v._mpc_ = mpc_div_mpf(s._mpc_, t._mpf_, prec, rounding)
521
+ return v
522
+ v = new(cls)
523
+ v._mpc_ = mpc_div(s._mpc_, t._mpc_, prec, rounding)
524
+ return v
525
+
526
+ def __pow__(s, t):
527
+ cls, new, (prec, rounding) = s._ctxdata
528
+ if isinstance(t, int_types):
529
+ v = new(cls)
530
+ v._mpc_ = mpc_pow_int(s._mpc_, t, prec, rounding)
531
+ return v
532
+ t = s.mpc_convert_lhs(t)
533
+ if t is NotImplemented:
534
+ return t
535
+ v = new(cls)
536
+ if hasattr(t, '_mpf_'):
537
+ v._mpc_ = mpc_pow_mpf(s._mpc_, t._mpf_, prec, rounding)
538
+ else:
539
+ v._mpc_ = mpc_pow(s._mpc_, t._mpc_, prec, rounding)
540
+ return v
541
+
542
+ __radd__ = __add__
543
+
544
+ def __rsub__(s, t):
545
+ t = s.mpc_convert_lhs(t)
546
+ if t is NotImplemented:
547
+ return t
548
+ return t - s
549
+
550
+ def __rmul__(s, t):
551
+ cls, new, (prec, rounding) = s._ctxdata
552
+ if isinstance(t, int_types):
553
+ v = new(cls)
554
+ v._mpc_ = mpc_mul_int(s._mpc_, t, prec, rounding)
555
+ return v
556
+ t = s.mpc_convert_lhs(t)
557
+ if t is NotImplemented:
558
+ return t
559
+ return t * s
560
+
561
+ def __rdiv__(s, t):
562
+ t = s.mpc_convert_lhs(t)
563
+ if t is NotImplemented:
564
+ return t
565
+ return t / s
566
+
567
+ def __rpow__(s, t):
568
+ t = s.mpc_convert_lhs(t)
569
+ if t is NotImplemented:
570
+ return t
571
+ return t ** s
572
+
573
+ __truediv__ = __div__
574
+ __rtruediv__ = __rdiv__
575
+
576
+ def ae(s, t, rel_eps=None, abs_eps=None):
577
+ return s.context.almosteq(s, t, rel_eps, abs_eps)
578
+
579
+
580
+ complex_types = (complex, _mpc)
581
+
582
+
583
+ class PythonMPContext(object):
584
+
585
+ def __init__(ctx):
586
+ ctx._prec_rounding = [53, round_nearest]
587
+ ctx.mpf = type('mpf', (_mpf,), {})
588
+ ctx.mpc = type('mpc', (_mpc,), {})
589
+ ctx.mpf._ctxdata = [ctx.mpf, new, ctx._prec_rounding]
590
+ ctx.mpc._ctxdata = [ctx.mpc, new, ctx._prec_rounding]
591
+ ctx.mpf.context = ctx
592
+ ctx.mpc.context = ctx
593
+ ctx.constant = type('constant', (_constant,), {})
594
+ ctx.constant._ctxdata = [ctx.mpf, new, ctx._prec_rounding]
595
+ ctx.constant.context = ctx
596
+
597
+ def make_mpf(ctx, v):
598
+ a = new(ctx.mpf)
599
+ a._mpf_ = v
600
+ return a
601
+
602
+ def make_mpc(ctx, v):
603
+ a = new(ctx.mpc)
604
+ a._mpc_ = v
605
+ return a
606
+
607
+ def default(ctx):
608
+ ctx._prec = ctx._prec_rounding[0] = 53
609
+ ctx._dps = 15
610
+ ctx.trap_complex = False
611
+
612
+ def _set_prec(ctx, n):
613
+ ctx._prec = ctx._prec_rounding[0] = max(1, int(n))
614
+ ctx._dps = prec_to_dps(n)
615
+
616
+ def _set_dps(ctx, n):
617
+ ctx._prec = ctx._prec_rounding[0] = dps_to_prec(n)
618
+ ctx._dps = max(1, int(n))
619
+
620
+ prec = property(lambda ctx: ctx._prec, _set_prec)
621
+ dps = property(lambda ctx: ctx._dps, _set_dps)
622
+
623
+ def convert(ctx, x, strings=True):
624
+ """
625
+ Converts *x* to an ``mpf`` or ``mpc``. If *x* is of type ``mpf``,
626
+ ``mpc``, ``int``, ``float``, ``complex``, the conversion
627
+ will be performed losslessly.
628
+
629
+ If *x* is a string, the result will be rounded to the present
630
+ working precision. Strings representing fractions or complex
631
+ numbers are permitted.
632
+
633
+ >>> from mpmath import *
634
+ >>> mp.dps = 15; mp.pretty = False
635
+ >>> mpmathify(3.5)
636
+ mpf('3.5')
637
+ >>> mpmathify('2.1')
638
+ mpf('2.1000000000000001')
639
+ >>> mpmathify('3/4')
640
+ mpf('0.75')
641
+ >>> mpmathify('2+3j')
642
+ mpc(real='2.0', imag='3.0')
643
+
644
+ """
645
+ if type(x) in ctx.types: return x
646
+ if isinstance(x, int_types): return ctx.make_mpf(from_int(x))
647
+ if isinstance(x, float): return ctx.make_mpf(from_float(x))
648
+ if isinstance(x, complex):
649
+ return ctx.make_mpc((from_float(x.real), from_float(x.imag)))
650
+ if type(x).__module__ == 'numpy': return ctx.npconvert(x)
651
+ if isinstance(x, numbers.Rational): # e.g. Fraction
652
+ try: x = rational.mpq(int(x.numerator), int(x.denominator))
653
+ except: pass
654
+ prec, rounding = ctx._prec_rounding
655
+ if isinstance(x, rational.mpq):
656
+ p, q = x._mpq_
657
+ return ctx.make_mpf(from_rational(p, q, prec))
658
+ if strings and isinstance(x, basestring):
659
+ try:
660
+ _mpf_ = from_str(x, prec, rounding)
661
+ return ctx.make_mpf(_mpf_)
662
+ except ValueError:
663
+ pass
664
+ if hasattr(x, '_mpf_'): return ctx.make_mpf(x._mpf_)
665
+ if hasattr(x, '_mpc_'): return ctx.make_mpc(x._mpc_)
666
+ if hasattr(x, '_mpmath_'):
667
+ return ctx.convert(x._mpmath_(prec, rounding))
668
+ if type(x).__module__ == 'decimal':
669
+ try: return ctx.make_mpf(from_Decimal(x, prec, rounding))
670
+ except: pass
671
+ return ctx._convert_fallback(x, strings)
672
+
673
+ def npconvert(ctx, x):
674
+ """
675
+ Converts *x* to an ``mpf`` or ``mpc``. *x* should be a numpy
676
+ scalar.
677
+ """
678
+ import numpy as np
679
+ if isinstance(x, np.integer): return ctx.make_mpf(from_int(int(x)))
680
+ if isinstance(x, np.floating): return ctx.make_mpf(from_npfloat(x))
681
+ if isinstance(x, np.complexfloating):
682
+ return ctx.make_mpc((from_npfloat(x.real), from_npfloat(x.imag)))
683
+ raise TypeError("cannot create mpf from " + repr(x))
684
+
685
+ def isnan(ctx, x):
686
+ """
687
+ Return *True* if *x* is a NaN (not-a-number), or for a complex
688
+ number, whether either the real or complex part is NaN;
689
+ otherwise return *False*::
690
+
691
+ >>> from mpmath import *
692
+ >>> isnan(3.14)
693
+ False
694
+ >>> isnan(nan)
695
+ True
696
+ >>> isnan(mpc(3.14,2.72))
697
+ False
698
+ >>> isnan(mpc(3.14,nan))
699
+ True
700
+
701
+ """
702
+ if hasattr(x, "_mpf_"):
703
+ return x._mpf_ == fnan
704
+ if hasattr(x, "_mpc_"):
705
+ return fnan in x._mpc_
706
+ if isinstance(x, int_types) or isinstance(x, rational.mpq):
707
+ return False
708
+ x = ctx.convert(x)
709
+ if hasattr(x, '_mpf_') or hasattr(x, '_mpc_'):
710
+ return ctx.isnan(x)
711
+ raise TypeError("isnan() needs a number as input")
712
+
713
+ def isinf(ctx, x):
714
+ """
715
+ Return *True* if the absolute value of *x* is infinite;
716
+ otherwise return *False*::
717
+
718
+ >>> from mpmath import *
719
+ >>> isinf(inf)
720
+ True
721
+ >>> isinf(-inf)
722
+ True
723
+ >>> isinf(3)
724
+ False
725
+ >>> isinf(3+4j)
726
+ False
727
+ >>> isinf(mpc(3,inf))
728
+ True
729
+ >>> isinf(mpc(inf,3))
730
+ True
731
+
732
+ """
733
+ if hasattr(x, "_mpf_"):
734
+ return x._mpf_ in (finf, fninf)
735
+ if hasattr(x, "_mpc_"):
736
+ re, im = x._mpc_
737
+ return re in (finf, fninf) or im in (finf, fninf)
738
+ if isinstance(x, int_types) or isinstance(x, rational.mpq):
739
+ return False
740
+ x = ctx.convert(x)
741
+ if hasattr(x, '_mpf_') or hasattr(x, '_mpc_'):
742
+ return ctx.isinf(x)
743
+ raise TypeError("isinf() needs a number as input")
744
+
745
+ def isnormal(ctx, x):
746
+ """
747
+ Determine whether *x* is "normal" in the sense of floating-point
748
+ representation; that is, return *False* if *x* is zero, an
749
+ infinity or NaN; otherwise return *True*. By extension, a
750
+ complex number *x* is considered "normal" if its magnitude is
751
+ normal::
752
+
753
+ >>> from mpmath import *
754
+ >>> isnormal(3)
755
+ True
756
+ >>> isnormal(0)
757
+ False
758
+ >>> isnormal(inf); isnormal(-inf); isnormal(nan)
759
+ False
760
+ False
761
+ False
762
+ >>> isnormal(0+0j)
763
+ False
764
+ >>> isnormal(0+3j)
765
+ True
766
+ >>> isnormal(mpc(2,nan))
767
+ False
768
+ """
769
+ if hasattr(x, "_mpf_"):
770
+ return bool(x._mpf_[1])
771
+ if hasattr(x, "_mpc_"):
772
+ re, im = x._mpc_
773
+ re_normal = bool(re[1])
774
+ im_normal = bool(im[1])
775
+ if re == fzero: return im_normal
776
+ if im == fzero: return re_normal
777
+ return re_normal and im_normal
778
+ if isinstance(x, int_types) or isinstance(x, rational.mpq):
779
+ return bool(x)
780
+ x = ctx.convert(x)
781
+ if hasattr(x, '_mpf_') or hasattr(x, '_mpc_'):
782
+ return ctx.isnormal(x)
783
+ raise TypeError("isnormal() needs a number as input")
784
+
785
+ def isint(ctx, x, gaussian=False):
786
+ """
787
+ Return *True* if *x* is integer-valued; otherwise return
788
+ *False*::
789
+
790
+ >>> from mpmath import *
791
+ >>> isint(3)
792
+ True
793
+ >>> isint(mpf(3))
794
+ True
795
+ >>> isint(3.2)
796
+ False
797
+ >>> isint(inf)
798
+ False
799
+
800
+ Optionally, Gaussian integers can be checked for::
801
+
802
+ >>> isint(3+0j)
803
+ True
804
+ >>> isint(3+2j)
805
+ False
806
+ >>> isint(3+2j, gaussian=True)
807
+ True
808
+
809
+ """
810
+ if isinstance(x, int_types):
811
+ return True
812
+ if hasattr(x, "_mpf_"):
813
+ sign, man, exp, bc = xval = x._mpf_
814
+ return bool((man and exp >= 0) or xval == fzero)
815
+ if hasattr(x, "_mpc_"):
816
+ re, im = x._mpc_
817
+ rsign, rman, rexp, rbc = re
818
+ isign, iman, iexp, ibc = im
819
+ re_isint = (rman and rexp >= 0) or re == fzero
820
+ if gaussian:
821
+ im_isint = (iman and iexp >= 0) or im == fzero
822
+ return re_isint and im_isint
823
+ return re_isint and im == fzero
824
+ if isinstance(x, rational.mpq):
825
+ p, q = x._mpq_
826
+ return p % q == 0
827
+ x = ctx.convert(x)
828
+ if hasattr(x, '_mpf_') or hasattr(x, '_mpc_'):
829
+ return ctx.isint(x, gaussian)
830
+ raise TypeError("isint() needs a number as input")
831
+
832
+ def fsum(ctx, terms, absolute=False, squared=False):
833
+ """
834
+ Calculates a sum containing a finite number of terms (for infinite
835
+ series, see :func:`~mpmath.nsum`). The terms will be converted to
836
+ mpmath numbers. For len(terms) > 2, this function is generally
837
+ faster and produces more accurate results than the builtin
838
+ Python function :func:`sum`.
839
+
840
+ >>> from mpmath import *
841
+ >>> mp.dps = 15; mp.pretty = False
842
+ >>> fsum([1, 2, 0.5, 7])
843
+ mpf('10.5')
844
+
845
+ With squared=True each term is squared, and with absolute=True
846
+ the absolute value of each term is used.
847
+ """
848
+ prec, rnd = ctx._prec_rounding
849
+ real = []
850
+ imag = []
851
+ for term in terms:
852
+ reval = imval = 0
853
+ if hasattr(term, "_mpf_"):
854
+ reval = term._mpf_
855
+ elif hasattr(term, "_mpc_"):
856
+ reval, imval = term._mpc_
857
+ else:
858
+ term = ctx.convert(term)
859
+ if hasattr(term, "_mpf_"):
860
+ reval = term._mpf_
861
+ elif hasattr(term, "_mpc_"):
862
+ reval, imval = term._mpc_
863
+ else:
864
+ raise NotImplementedError
865
+ if imval:
866
+ if squared:
867
+ if absolute:
868
+ real.append(mpf_mul(reval,reval))
869
+ real.append(mpf_mul(imval,imval))
870
+ else:
871
+ reval, imval = mpc_pow_int((reval,imval),2,prec+10)
872
+ real.append(reval)
873
+ imag.append(imval)
874
+ elif absolute:
875
+ real.append(mpc_abs((reval,imval), prec))
876
+ else:
877
+ real.append(reval)
878
+ imag.append(imval)
879
+ else:
880
+ if squared:
881
+ reval = mpf_mul(reval, reval)
882
+ elif absolute:
883
+ reval = mpf_abs(reval)
884
+ real.append(reval)
885
+ s = mpf_sum(real, prec, rnd, absolute)
886
+ if imag:
887
+ s = ctx.make_mpc((s, mpf_sum(imag, prec, rnd)))
888
+ else:
889
+ s = ctx.make_mpf(s)
890
+ return s
891
+
892
+ def fdot(ctx, A, B=None, conjugate=False):
893
+ r"""
894
+ Computes the dot product of the iterables `A` and `B`,
895
+
896
+ .. math ::
897
+
898
+ \sum_{k=0} A_k B_k.
899
+
900
+ Alternatively, :func:`~mpmath.fdot` accepts a single iterable of pairs.
901
+ In other words, ``fdot(A,B)`` and ``fdot(zip(A,B))`` are equivalent.
902
+ The elements are automatically converted to mpmath numbers.
903
+
904
+ With ``conjugate=True``, the elements in the second vector
905
+ will be conjugated:
906
+
907
+ .. math ::
908
+
909
+ \sum_{k=0} A_k \overline{B_k}
910
+
911
+ **Examples**
912
+
913
+ >>> from mpmath import *
914
+ >>> mp.dps = 15; mp.pretty = False
915
+ >>> A = [2, 1.5, 3]
916
+ >>> B = [1, -1, 2]
917
+ >>> fdot(A, B)
918
+ mpf('6.5')
919
+ >>> list(zip(A, B))
920
+ [(2, 1), (1.5, -1), (3, 2)]
921
+ >>> fdot(_)
922
+ mpf('6.5')
923
+ >>> A = [2, 1.5, 3j]
924
+ >>> B = [1+j, 3, -1-j]
925
+ >>> fdot(A, B)
926
+ mpc(real='9.5', imag='-1.0')
927
+ >>> fdot(A, B, conjugate=True)
928
+ mpc(real='3.5', imag='-5.0')
929
+
930
+ """
931
+ if B is not None:
932
+ A = zip(A, B)
933
+ prec, rnd = ctx._prec_rounding
934
+ real = []
935
+ imag = []
936
+ hasattr_ = hasattr
937
+ types = (ctx.mpf, ctx.mpc)
938
+ for a, b in A:
939
+ if type(a) not in types: a = ctx.convert(a)
940
+ if type(b) not in types: b = ctx.convert(b)
941
+ a_real = hasattr_(a, "_mpf_")
942
+ b_real = hasattr_(b, "_mpf_")
943
+ if a_real and b_real:
944
+ real.append(mpf_mul(a._mpf_, b._mpf_))
945
+ continue
946
+ a_complex = hasattr_(a, "_mpc_")
947
+ b_complex = hasattr_(b, "_mpc_")
948
+ if a_real and b_complex:
949
+ aval = a._mpf_
950
+ bre, bim = b._mpc_
951
+ if conjugate:
952
+ bim = mpf_neg(bim)
953
+ real.append(mpf_mul(aval, bre))
954
+ imag.append(mpf_mul(aval, bim))
955
+ elif b_real and a_complex:
956
+ are, aim = a._mpc_
957
+ bval = b._mpf_
958
+ real.append(mpf_mul(are, bval))
959
+ imag.append(mpf_mul(aim, bval))
960
+ elif a_complex and b_complex:
961
+ #re, im = mpc_mul(a._mpc_, b._mpc_, prec+20)
962
+ are, aim = a._mpc_
963
+ bre, bim = b._mpc_
964
+ if conjugate:
965
+ bim = mpf_neg(bim)
966
+ real.append(mpf_mul(are, bre))
967
+ real.append(mpf_neg(mpf_mul(aim, bim)))
968
+ imag.append(mpf_mul(are, bim))
969
+ imag.append(mpf_mul(aim, bre))
970
+ else:
971
+ raise NotImplementedError
972
+ s = mpf_sum(real, prec, rnd)
973
+ if imag:
974
+ s = ctx.make_mpc((s, mpf_sum(imag, prec, rnd)))
975
+ else:
976
+ s = ctx.make_mpf(s)
977
+ return s
978
+
979
+ def _wrap_libmp_function(ctx, mpf_f, mpc_f=None, mpi_f=None, doc="<no doc>"):
980
+ """
981
+ Given a low-level mpf_ function, and optionally similar functions
982
+ for mpc_ and mpi_, defines the function as a context method.
983
+
984
+ It is assumed that the return type is the same as that of
985
+ the input; the exception is that propagation from mpf to mpc is possible
986
+ by raising ComplexResult.
987
+
988
+ """
989
+ def f(x, **kwargs):
990
+ if type(x) not in ctx.types:
991
+ x = ctx.convert(x)
992
+ prec, rounding = ctx._prec_rounding
993
+ if kwargs:
994
+ prec = kwargs.get('prec', prec)
995
+ if 'dps' in kwargs:
996
+ prec = dps_to_prec(kwargs['dps'])
997
+ rounding = kwargs.get('rounding', rounding)
998
+ if hasattr(x, '_mpf_'):
999
+ try:
1000
+ return ctx.make_mpf(mpf_f(x._mpf_, prec, rounding))
1001
+ except ComplexResult:
1002
+ # Handle propagation to complex
1003
+ if ctx.trap_complex:
1004
+ raise
1005
+ return ctx.make_mpc(mpc_f((x._mpf_, fzero), prec, rounding))
1006
+ elif hasattr(x, '_mpc_'):
1007
+ return ctx.make_mpc(mpc_f(x._mpc_, prec, rounding))
1008
+ raise NotImplementedError("%s of a %s" % (name, type(x)))
1009
+ name = mpf_f.__name__[4:]
1010
+ f.__doc__ = function_docs.__dict__.get(name, "Computes the %s of x" % doc)
1011
+ return f
1012
+
1013
+ # Called by SpecialFunctions.__init__()
1014
+ @classmethod
1015
+ def _wrap_specfun(cls, name, f, wrap):
1016
+ if wrap:
1017
+ def f_wrapped(ctx, *args, **kwargs):
1018
+ convert = ctx.convert
1019
+ args = [convert(a) for a in args]
1020
+ prec = ctx.prec
1021
+ try:
1022
+ ctx.prec += 10
1023
+ retval = f(ctx, *args, **kwargs)
1024
+ finally:
1025
+ ctx.prec = prec
1026
+ return +retval
1027
+ else:
1028
+ f_wrapped = f
1029
+ f_wrapped.__doc__ = function_docs.__dict__.get(name, f.__doc__)
1030
+ setattr(cls, name, f_wrapped)
1031
+
1032
+ def _convert_param(ctx, x):
1033
+ if hasattr(x, "_mpc_"):
1034
+ v, im = x._mpc_
1035
+ if im != fzero:
1036
+ return x, 'C'
1037
+ elif hasattr(x, "_mpf_"):
1038
+ v = x._mpf_
1039
+ else:
1040
+ if type(x) in int_types:
1041
+ return int(x), 'Z'
1042
+ p = None
1043
+ if isinstance(x, tuple):
1044
+ p, q = x
1045
+ elif hasattr(x, '_mpq_'):
1046
+ p, q = x._mpq_
1047
+ elif isinstance(x, basestring) and '/' in x:
1048
+ p, q = x.split('/')
1049
+ p = int(p)
1050
+ q = int(q)
1051
+ if p is not None:
1052
+ if not p % q:
1053
+ return p // q, 'Z'
1054
+ return ctx.mpq(p,q), 'Q'
1055
+ x = ctx.convert(x)
1056
+ if hasattr(x, "_mpc_"):
1057
+ v, im = x._mpc_
1058
+ if im != fzero:
1059
+ return x, 'C'
1060
+ elif hasattr(x, "_mpf_"):
1061
+ v = x._mpf_
1062
+ else:
1063
+ return x, 'U'
1064
+ sign, man, exp, bc = v
1065
+ if man:
1066
+ if exp >= -4:
1067
+ if sign:
1068
+ man = -man
1069
+ if exp >= 0:
1070
+ return int(man) << exp, 'Z'
1071
+ if exp >= -4:
1072
+ p, q = int(man), (1<<(-exp))
1073
+ return ctx.mpq(p,q), 'Q'
1074
+ x = ctx.make_mpf(v)
1075
+ return x, 'R'
1076
+ elif not exp:
1077
+ return 0, 'Z'
1078
+ else:
1079
+ return x, 'U'
1080
+
1081
+ def _mpf_mag(ctx, x):
1082
+ sign, man, exp, bc = x
1083
+ if man:
1084
+ return exp+bc
1085
+ if x == fzero:
1086
+ return ctx.ninf
1087
+ if x == finf or x == fninf:
1088
+ return ctx.inf
1089
+ return ctx.nan
1090
+
1091
+ def mag(ctx, x):
1092
+ """
1093
+ Quick logarithmic magnitude estimate of a number. Returns an
1094
+ integer or infinity `m` such that `|x| <= 2^m`. It is not
1095
+ guaranteed that `m` is an optimal bound, but it will never
1096
+ be too large by more than 2 (and probably not more than 1).
1097
+
1098
+ **Examples**
1099
+
1100
+ >>> from mpmath import *
1101
+ >>> mp.pretty = True
1102
+ >>> mag(10), mag(10.0), mag(mpf(10)), int(ceil(log(10,2)))
1103
+ (4, 4, 4, 4)
1104
+ >>> mag(10j), mag(10+10j)
1105
+ (4, 5)
1106
+ >>> mag(0.01), int(ceil(log(0.01,2)))
1107
+ (-6, -6)
1108
+ >>> mag(0), mag(inf), mag(-inf), mag(nan)
1109
+ (-inf, +inf, +inf, nan)
1110
+
1111
+ """
1112
+ if hasattr(x, "_mpf_"):
1113
+ return ctx._mpf_mag(x._mpf_)
1114
+ elif hasattr(x, "_mpc_"):
1115
+ r, i = x._mpc_
1116
+ if r == fzero:
1117
+ return ctx._mpf_mag(i)
1118
+ if i == fzero:
1119
+ return ctx._mpf_mag(r)
1120
+ return 1+max(ctx._mpf_mag(r), ctx._mpf_mag(i))
1121
+ elif isinstance(x, int_types):
1122
+ if x:
1123
+ return bitcount(abs(x))
1124
+ return ctx.ninf
1125
+ elif isinstance(x, rational.mpq):
1126
+ p, q = x._mpq_
1127
+ if p:
1128
+ return 1 + bitcount(abs(p)) - bitcount(q)
1129
+ return ctx.ninf
1130
+ else:
1131
+ x = ctx.convert(x)
1132
+ if hasattr(x, "_mpf_") or hasattr(x, "_mpc_"):
1133
+ return ctx.mag(x)
1134
+ else:
1135
+ raise TypeError("requires an mpf/mpc")
1136
+
1137
+
1138
+ # Register with "numbers" ABC
1139
+ # We do not subclass, hence we do not use the @abstractmethod checks. While
1140
+ # this is less invasive it may turn out that we do not actually support
1141
+ # parts of the expected interfaces. See
1142
+ # http://docs.python.org/2/library/numbers.html for list of abstract
1143
+ # methods.
1144
+ try:
1145
+ import numbers
1146
+ numbers.Complex.register(_mpc)
1147
+ numbers.Real.register(_mpf)
1148
+ except ImportError:
1149
+ pass
llmeval-env/lib/python3.10/site-packages/mpmath/function_docs.py ADDED
The diff for this file is too large to render. See raw diff
 
llmeval-env/lib/python3.10/site-packages/mpmath/identification.py ADDED
@@ -0,0 +1,844 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Implements the PSLQ algorithm for integer relation detection,
3
+ and derivative algorithms for constant recognition.
4
+ """
5
+
6
+ from .libmp.backend import xrange
7
+ from .libmp import int_types, sqrt_fixed
8
+
9
+ # round to nearest integer (can be done more elegantly...)
10
+ def round_fixed(x, prec):
11
+ return ((x + (1<<(prec-1))) >> prec) << prec
12
+
13
+ class IdentificationMethods(object):
14
+ pass
15
+
16
+
17
+ def pslq(ctx, x, tol=None, maxcoeff=1000, maxsteps=100, verbose=False):
18
+ r"""
19
+ Given a vector of real numbers `x = [x_0, x_1, ..., x_n]`, ``pslq(x)``
20
+ uses the PSLQ algorithm to find a list of integers
21
+ `[c_0, c_1, ..., c_n]` such that
22
+
23
+ .. math ::
24
+
25
+ |c_1 x_1 + c_2 x_2 + ... + c_n x_n| < \mathrm{tol}
26
+
27
+ and such that `\max |c_k| < \mathrm{maxcoeff}`. If no such vector
28
+ exists, :func:`~mpmath.pslq` returns ``None``. The tolerance defaults to
29
+ 3/4 of the working precision.
30
+
31
+ **Examples**
32
+
33
+ Find rational approximations for `\pi`::
34
+
35
+ >>> from mpmath import *
36
+ >>> mp.dps = 15; mp.pretty = True
37
+ >>> pslq([-1, pi], tol=0.01)
38
+ [22, 7]
39
+ >>> pslq([-1, pi], tol=0.001)
40
+ [355, 113]
41
+ >>> mpf(22)/7; mpf(355)/113; +pi
42
+ 3.14285714285714
43
+ 3.14159292035398
44
+ 3.14159265358979
45
+
46
+ Pi is not a rational number with denominator less than 1000::
47
+
48
+ >>> pslq([-1, pi])
49
+ >>>
50
+
51
+ To within the standard precision, it can however be approximated
52
+ by at least one rational number with denominator less than `10^{12}`::
53
+
54
+ >>> p, q = pslq([-1, pi], maxcoeff=10**12)
55
+ >>> print(p); print(q)
56
+ 238410049439
57
+ 75888275702
58
+ >>> mpf(p)/q
59
+ 3.14159265358979
60
+
61
+ The PSLQ algorithm can be applied to long vectors. For example,
62
+ we can investigate the rational (in)dependence of integer square
63
+ roots::
64
+
65
+ >>> mp.dps = 30
66
+ >>> pslq([sqrt(n) for n in range(2, 5+1)])
67
+ >>>
68
+ >>> pslq([sqrt(n) for n in range(2, 6+1)])
69
+ >>>
70
+ >>> pslq([sqrt(n) for n in range(2, 8+1)])
71
+ [2, 0, 0, 0, 0, 0, -1]
72
+
73
+ **Machin formulas**
74
+
75
+ A famous formula for `\pi` is Machin's,
76
+
77
+ .. math ::
78
+
79
+ \frac{\pi}{4} = 4 \operatorname{acot} 5 - \operatorname{acot} 239
80
+
81
+ There are actually infinitely many formulas of this type. Two
82
+ others are
83
+
84
+ .. math ::
85
+
86
+ \frac{\pi}{4} = \operatorname{acot} 1
87
+
88
+ \frac{\pi}{4} = 12 \operatorname{acot} 49 + 32 \operatorname{acot} 57
89
+ + 5 \operatorname{acot} 239 + 12 \operatorname{acot} 110443
90
+
91
+ We can easily verify the formulas using the PSLQ algorithm::
92
+
93
+ >>> mp.dps = 30
94
+ >>> pslq([pi/4, acot(1)])
95
+ [1, -1]
96
+ >>> pslq([pi/4, acot(5), acot(239)])
97
+ [1, -4, 1]
98
+ >>> pslq([pi/4, acot(49), acot(57), acot(239), acot(110443)])
99
+ [1, -12, -32, 5, -12]
100
+
101
+ We could try to generate a custom Machin-like formula by running
102
+ the PSLQ algorithm with a few inverse cotangent values, for example
103
+ acot(2), acot(3) ... acot(10). Unfortunately, there is a linear
104
+ dependence among these values, resulting in only that dependence
105
+ being detected, with a zero coefficient for `\pi`::
106
+
107
+ >>> pslq([pi] + [acot(n) for n in range(2,11)])
108
+ [0, 1, -1, 0, 0, 0, -1, 0, 0, 0]
109
+
110
+ We get better luck by removing linearly dependent terms::
111
+
112
+ >>> pslq([pi] + [acot(n) for n in range(2,11) if n not in (3, 5)])
113
+ [1, -8, 0, 0, 4, 0, 0, 0]
114
+
115
+ In other words, we found the following formula::
116
+
117
+ >>> 8*acot(2) - 4*acot(7)
118
+ 3.14159265358979323846264338328
119
+ >>> +pi
120
+ 3.14159265358979323846264338328
121
+
122
+ **Algorithm**
123
+
124
+ This is a fairly direct translation to Python of the pseudocode given by
125
+ David Bailey, "The PSLQ Integer Relation Algorithm":
126
+ http://www.cecm.sfu.ca/organics/papers/bailey/paper/html/node3.html
127
+
128
+ The present implementation uses fixed-point instead of floating-point
129
+ arithmetic, since this is significantly (about 7x) faster.
130
+ """
131
+
132
+ n = len(x)
133
+ if n < 2:
134
+ raise ValueError("n cannot be less than 2")
135
+
136
+ # At too low precision, the algorithm becomes meaningless
137
+ prec = ctx.prec
138
+ if prec < 53:
139
+ raise ValueError("prec cannot be less than 53")
140
+
141
+ if verbose and prec // max(2,n) < 5:
142
+ print("Warning: precision for PSLQ may be too low")
143
+
144
+ target = int(prec * 0.75)
145
+
146
+ if tol is None:
147
+ tol = ctx.mpf(2)**(-target)
148
+ else:
149
+ tol = ctx.convert(tol)
150
+
151
+ extra = 60
152
+ prec += extra
153
+
154
+ if verbose:
155
+ print("PSLQ using prec %i and tol %s" % (prec, ctx.nstr(tol)))
156
+
157
+ tol = ctx.to_fixed(tol, prec)
158
+ assert tol
159
+
160
+ # Convert to fixed-point numbers. The dummy None is added so we can
161
+ # use 1-based indexing. (This just allows us to be consistent with
162
+ # Bailey's indexing. The algorithm is 100 lines long, so debugging
163
+ # a single wrong index can be painful.)
164
+ x = [None] + [ctx.to_fixed(ctx.mpf(xk), prec) for xk in x]
165
+
166
+ # Sanity check on magnitudes
167
+ minx = min(abs(xx) for xx in x[1:])
168
+ if not minx:
169
+ raise ValueError("PSLQ requires a vector of nonzero numbers")
170
+ if minx < tol//100:
171
+ if verbose:
172
+ print("STOPPING: (one number is too small)")
173
+ return None
174
+
175
+ g = sqrt_fixed((4<<prec)//3, prec)
176
+ A = {}
177
+ B = {}
178
+ H = {}
179
+ # Initialization
180
+ # step 1
181
+ for i in xrange(1, n+1):
182
+ for j in xrange(1, n+1):
183
+ A[i,j] = B[i,j] = (i==j) << prec
184
+ H[i,j] = 0
185
+ # step 2
186
+ s = [None] + [0] * n
187
+ for k in xrange(1, n+1):
188
+ t = 0
189
+ for j in xrange(k, n+1):
190
+ t += (x[j]**2 >> prec)
191
+ s[k] = sqrt_fixed(t, prec)
192
+ t = s[1]
193
+ y = x[:]
194
+ for k in xrange(1, n+1):
195
+ y[k] = (x[k] << prec) // t
196
+ s[k] = (s[k] << prec) // t
197
+ # step 3
198
+ for i in xrange(1, n+1):
199
+ for j in xrange(i+1, n):
200
+ H[i,j] = 0
201
+ if i <= n-1:
202
+ if s[i]:
203
+ H[i,i] = (s[i+1] << prec) // s[i]
204
+ else:
205
+ H[i,i] = 0
206
+ for j in range(1, i):
207
+ sjj1 = s[j]*s[j+1]
208
+ if sjj1:
209
+ H[i,j] = ((-y[i]*y[j])<<prec)//sjj1
210
+ else:
211
+ H[i,j] = 0
212
+ # step 4
213
+ for i in xrange(2, n+1):
214
+ for j in xrange(i-1, 0, -1):
215
+ #t = floor(H[i,j]/H[j,j] + 0.5)
216
+ if H[j,j]:
217
+ t = round_fixed((H[i,j] << prec)//H[j,j], prec)
218
+ else:
219
+ #t = 0
220
+ continue
221
+ y[j] = y[j] + (t*y[i] >> prec)
222
+ for k in xrange(1, j+1):
223
+ H[i,k] = H[i,k] - (t*H[j,k] >> prec)
224
+ for k in xrange(1, n+1):
225
+ A[i,k] = A[i,k] - (t*A[j,k] >> prec)
226
+ B[k,j] = B[k,j] + (t*B[k,i] >> prec)
227
+ # Main algorithm
228
+ for REP in range(maxsteps):
229
+ # Step 1
230
+ m = -1
231
+ szmax = -1
232
+ for i in range(1, n):
233
+ h = H[i,i]
234
+ sz = (g**i * abs(h)) >> (prec*(i-1))
235
+ if sz > szmax:
236
+ m = i
237
+ szmax = sz
238
+ # Step 2
239
+ y[m], y[m+1] = y[m+1], y[m]
240
+ for i in xrange(1,n+1): H[m,i], H[m+1,i] = H[m+1,i], H[m,i]
241
+ for i in xrange(1,n+1): A[m,i], A[m+1,i] = A[m+1,i], A[m,i]
242
+ for i in xrange(1,n+1): B[i,m], B[i,m+1] = B[i,m+1], B[i,m]
243
+ # Step 3
244
+ if m <= n - 2:
245
+ t0 = sqrt_fixed((H[m,m]**2 + H[m,m+1]**2)>>prec, prec)
246
+ # A zero element probably indicates that the precision has
247
+ # been exhausted. XXX: this could be spurious, due to
248
+ # using fixed-point arithmetic
249
+ if not t0:
250
+ break
251
+ t1 = (H[m,m] << prec) // t0
252
+ t2 = (H[m,m+1] << prec) // t0
253
+ for i in xrange(m, n+1):
254
+ t3 = H[i,m]
255
+ t4 = H[i,m+1]
256
+ H[i,m] = (t1*t3+t2*t4) >> prec
257
+ H[i,m+1] = (-t2*t3+t1*t4) >> prec
258
+ # Step 4
259
+ for i in xrange(m+1, n+1):
260
+ for j in xrange(min(i-1, m+1), 0, -1):
261
+ try:
262
+ t = round_fixed((H[i,j] << prec)//H[j,j], prec)
263
+ # Precision probably exhausted
264
+ except ZeroDivisionError:
265
+ break
266
+ y[j] = y[j] + ((t*y[i]) >> prec)
267
+ for k in xrange(1, j+1):
268
+ H[i,k] = H[i,k] - (t*H[j,k] >> prec)
269
+ for k in xrange(1, n+1):
270
+ A[i,k] = A[i,k] - (t*A[j,k] >> prec)
271
+ B[k,j] = B[k,j] + (t*B[k,i] >> prec)
272
+ # Until a relation is found, the error typically decreases
273
+ # slowly (e.g. a factor 1-10) with each step TODO: we could
274
+ # compare err from two successive iterations. If there is a
275
+ # large drop (several orders of magnitude), that indicates a
276
+ # "high quality" relation was detected. Reporting this to
277
+ # the user somehow might be useful.
278
+ best_err = maxcoeff<<prec
279
+ for i in xrange(1, n+1):
280
+ err = abs(y[i])
281
+ # Maybe we are done?
282
+ if err < tol:
283
+ # We are done if the coefficients are acceptable
284
+ vec = [int(round_fixed(B[j,i], prec) >> prec) for j in \
285
+ range(1,n+1)]
286
+ if max(abs(v) for v in vec) < maxcoeff:
287
+ if verbose:
288
+ print("FOUND relation at iter %i/%i, error: %s" % \
289
+ (REP, maxsteps, ctx.nstr(err / ctx.mpf(2)**prec, 1)))
290
+ return vec
291
+ best_err = min(err, best_err)
292
+ # Calculate a lower bound for the norm. We could do this
293
+ # more exactly (using the Euclidean norm) but there is probably
294
+ # no practical benefit.
295
+ recnorm = max(abs(h) for h in H.values())
296
+ if recnorm:
297
+ norm = ((1 << (2*prec)) // recnorm) >> prec
298
+ norm //= 100
299
+ else:
300
+ norm = ctx.inf
301
+ if verbose:
302
+ print("%i/%i: Error: %8s Norm: %s" % \
303
+ (REP, maxsteps, ctx.nstr(best_err / ctx.mpf(2)**prec, 1), norm))
304
+ if norm >= maxcoeff:
305
+ break
306
+ if verbose:
307
+ print("CANCELLING after step %i/%i." % (REP, maxsteps))
308
+ print("Could not find an integer relation. Norm bound: %s" % norm)
309
+ return None
310
+
311
+ def findpoly(ctx, x, n=1, **kwargs):
312
+ r"""
313
+ ``findpoly(x, n)`` returns the coefficients of an integer
314
+ polynomial `P` of degree at most `n` such that `P(x) \approx 0`.
315
+ If no polynomial having `x` as a root can be found,
316
+ :func:`~mpmath.findpoly` returns ``None``.
317
+
318
+ :func:`~mpmath.findpoly` works by successively calling :func:`~mpmath.pslq` with
319
+ the vectors `[1, x]`, `[1, x, x^2]`, `[1, x, x^2, x^3]`, ...,
320
+ `[1, x, x^2, .., x^n]` as input. Keyword arguments given to
321
+ :func:`~mpmath.findpoly` are forwarded verbatim to :func:`~mpmath.pslq`. In
322
+ particular, you can specify a tolerance for `P(x)` with ``tol``
323
+ and a maximum permitted coefficient size with ``maxcoeff``.
324
+
325
+ For large values of `n`, it is recommended to run :func:`~mpmath.findpoly`
326
+ at high precision; preferably 50 digits or more.
327
+
328
+ **Examples**
329
+
330
+ By default (degree `n = 1`), :func:`~mpmath.findpoly` simply finds a linear
331
+ polynomial with a rational root::
332
+
333
+ >>> from mpmath import *
334
+ >>> mp.dps = 15; mp.pretty = True
335
+ >>> findpoly(0.7)
336
+ [-10, 7]
337
+
338
+ The generated coefficient list is valid input to ``polyval`` and
339
+ ``polyroots``::
340
+
341
+ >>> nprint(polyval(findpoly(phi, 2), phi), 1)
342
+ -2.0e-16
343
+ >>> for r in polyroots(findpoly(phi, 2)):
344
+ ... print(r)
345
+ ...
346
+ -0.618033988749895
347
+ 1.61803398874989
348
+
349
+ Numbers of the form `m + n \sqrt p` for integers `(m, n, p)` are
350
+ solutions to quadratic equations. As we find here, `1+\sqrt 2`
351
+ is a root of the polynomial `x^2 - 2x - 1`::
352
+
353
+ >>> findpoly(1+sqrt(2), 2)
354
+ [1, -2, -1]
355
+ >>> findroot(lambda x: x**2 - 2*x - 1, 1)
356
+ 2.4142135623731
357
+
358
+ Despite only containing square roots, the following number results
359
+ in a polynomial of degree 4::
360
+
361
+ >>> findpoly(sqrt(2)+sqrt(3), 4)
362
+ [1, 0, -10, 0, 1]
363
+
364
+ In fact, `x^4 - 10x^2 + 1` is the *minimal polynomial* of
365
+ `r = \sqrt 2 + \sqrt 3`, meaning that a rational polynomial of
366
+ lower degree having `r` as a root does not exist. Given sufficient
367
+ precision, :func:`~mpmath.findpoly` will usually find the correct
368
+ minimal polynomial of a given algebraic number.
369
+
370
+ **Non-algebraic numbers**
371
+
372
+ If :func:`~mpmath.findpoly` fails to find a polynomial with given
373
+ coefficient size and tolerance constraints, that means no such
374
+ polynomial exists.
375
+
376
+ We can verify that `\pi` is not an algebraic number of degree 3 with
377
+ coefficients less than 1000::
378
+
379
+ >>> mp.dps = 15
380
+ >>> findpoly(pi, 3)
381
+ >>>
382
+
383
+ It is always possible to find an algebraic approximation of a number
384
+ using one (or several) of the following methods:
385
+
386
+ 1. Increasing the permitted degree
387
+ 2. Allowing larger coefficients
388
+ 3. Reducing the tolerance
389
+
390
+ One example of each method is shown below::
391
+
392
+ >>> mp.dps = 15
393
+ >>> findpoly(pi, 4)
394
+ [95, -545, 863, -183, -298]
395
+ >>> findpoly(pi, 3, maxcoeff=10000)
396
+ [836, -1734, -2658, -457]
397
+ >>> findpoly(pi, 3, tol=1e-7)
398
+ [-4, 22, -29, -2]
399
+
400
+ It is unknown whether Euler's constant is transcendental (or even
401
+ irrational). We can use :func:`~mpmath.findpoly` to check that if is
402
+ an algebraic number, its minimal polynomial must have degree
403
+ at least 7 and a coefficient of magnitude at least 1000000::
404
+
405
+ >>> mp.dps = 200
406
+ >>> findpoly(euler, 6, maxcoeff=10**6, tol=1e-100, maxsteps=1000)
407
+ >>>
408
+
409
+ Note that the high precision and strict tolerance is necessary
410
+ for such high-degree runs, since otherwise unwanted low-accuracy
411
+ approximations will be detected. It may also be necessary to set
412
+ maxsteps high to prevent a premature exit (before the coefficient
413
+ bound has been reached). Running with ``verbose=True`` to get an
414
+ idea what is happening can be useful.
415
+ """
416
+ x = ctx.mpf(x)
417
+ if n < 1:
418
+ raise ValueError("n cannot be less than 1")
419
+ if x == 0:
420
+ return [1, 0]
421
+ xs = [ctx.mpf(1)]
422
+ for i in range(1,n+1):
423
+ xs.append(x**i)
424
+ a = ctx.pslq(xs, **kwargs)
425
+ if a is not None:
426
+ return a[::-1]
427
+
428
+ def fracgcd(p, q):
429
+ x, y = p, q
430
+ while y:
431
+ x, y = y, x % y
432
+ if x != 1:
433
+ p //= x
434
+ q //= x
435
+ if q == 1:
436
+ return p
437
+ return p, q
438
+
439
+ def pslqstring(r, constants):
440
+ q = r[0]
441
+ r = r[1:]
442
+ s = []
443
+ for i in range(len(r)):
444
+ p = r[i]
445
+ if p:
446
+ z = fracgcd(-p,q)
447
+ cs = constants[i][1]
448
+ if cs == '1':
449
+ cs = ''
450
+ else:
451
+ cs = '*' + cs
452
+ if isinstance(z, int_types):
453
+ if z > 0: term = str(z) + cs
454
+ else: term = ("(%s)" % z) + cs
455
+ else:
456
+ term = ("(%s/%s)" % z) + cs
457
+ s.append(term)
458
+ s = ' + '.join(s)
459
+ if '+' in s or '*' in s:
460
+ s = '(' + s + ')'
461
+ return s or '0'
462
+
463
+ def prodstring(r, constants):
464
+ q = r[0]
465
+ r = r[1:]
466
+ num = []
467
+ den = []
468
+ for i in range(len(r)):
469
+ p = r[i]
470
+ if p:
471
+ z = fracgcd(-p,q)
472
+ cs = constants[i][1]
473
+ if isinstance(z, int_types):
474
+ if abs(z) == 1: t = cs
475
+ else: t = '%s**%s' % (cs, abs(z))
476
+ ([num,den][z<0]).append(t)
477
+ else:
478
+ t = '%s**(%s/%s)' % (cs, abs(z[0]), z[1])
479
+ ([num,den][z[0]<0]).append(t)
480
+ num = '*'.join(num)
481
+ den = '*'.join(den)
482
+ if num and den: return "(%s)/(%s)" % (num, den)
483
+ if num: return num
484
+ if den: return "1/(%s)" % den
485
+
486
+ def quadraticstring(ctx,t,a,b,c):
487
+ if c < 0:
488
+ a,b,c = -a,-b,-c
489
+ u1 = (-b+ctx.sqrt(b**2-4*a*c))/(2*c)
490
+ u2 = (-b-ctx.sqrt(b**2-4*a*c))/(2*c)
491
+ if abs(u1-t) < abs(u2-t):
492
+ if b: s = '((%s+sqrt(%s))/%s)' % (-b,b**2-4*a*c,2*c)
493
+ else: s = '(sqrt(%s)/%s)' % (-4*a*c,2*c)
494
+ else:
495
+ if b: s = '((%s-sqrt(%s))/%s)' % (-b,b**2-4*a*c,2*c)
496
+ else: s = '(-sqrt(%s)/%s)' % (-4*a*c,2*c)
497
+ return s
498
+
499
+ # Transformation y = f(x,c), with inverse function x = f(y,c)
500
+ # The third entry indicates whether the transformation is
501
+ # redundant when c = 1
502
+ transforms = [
503
+ (lambda ctx,x,c: x*c, '$y/$c', 0),
504
+ (lambda ctx,x,c: x/c, '$c*$y', 1),
505
+ (lambda ctx,x,c: c/x, '$c/$y', 0),
506
+ (lambda ctx,x,c: (x*c)**2, 'sqrt($y)/$c', 0),
507
+ (lambda ctx,x,c: (x/c)**2, '$c*sqrt($y)', 1),
508
+ (lambda ctx,x,c: (c/x)**2, '$c/sqrt($y)', 0),
509
+ (lambda ctx,x,c: c*x**2, 'sqrt($y)/sqrt($c)', 1),
510
+ (lambda ctx,x,c: x**2/c, 'sqrt($c)*sqrt($y)', 1),
511
+ (lambda ctx,x,c: c/x**2, 'sqrt($c)/sqrt($y)', 1),
512
+ (lambda ctx,x,c: ctx.sqrt(x*c), '$y**2/$c', 0),
513
+ (lambda ctx,x,c: ctx.sqrt(x/c), '$c*$y**2', 1),
514
+ (lambda ctx,x,c: ctx.sqrt(c/x), '$c/$y**2', 0),
515
+ (lambda ctx,x,c: c*ctx.sqrt(x), '$y**2/$c**2', 1),
516
+ (lambda ctx,x,c: ctx.sqrt(x)/c, '$c**2*$y**2', 1),
517
+ (lambda ctx,x,c: c/ctx.sqrt(x), '$c**2/$y**2', 1),
518
+ (lambda ctx,x,c: ctx.exp(x*c), 'log($y)/$c', 0),
519
+ (lambda ctx,x,c: ctx.exp(x/c), '$c*log($y)', 1),
520
+ (lambda ctx,x,c: ctx.exp(c/x), '$c/log($y)', 0),
521
+ (lambda ctx,x,c: c*ctx.exp(x), 'log($y/$c)', 1),
522
+ (lambda ctx,x,c: ctx.exp(x)/c, 'log($c*$y)', 1),
523
+ (lambda ctx,x,c: c/ctx.exp(x), 'log($c/$y)', 0),
524
+ (lambda ctx,x,c: ctx.ln(x*c), 'exp($y)/$c', 0),
525
+ (lambda ctx,x,c: ctx.ln(x/c), '$c*exp($y)', 1),
526
+ (lambda ctx,x,c: ctx.ln(c/x), '$c/exp($y)', 0),
527
+ (lambda ctx,x,c: c*ctx.ln(x), 'exp($y/$c)', 1),
528
+ (lambda ctx,x,c: ctx.ln(x)/c, 'exp($c*$y)', 1),
529
+ (lambda ctx,x,c: c/ctx.ln(x), 'exp($c/$y)', 0),
530
+ ]
531
+
532
+ def identify(ctx, x, constants=[], tol=None, maxcoeff=1000, full=False,
533
+ verbose=False):
534
+ r"""
535
+ Given a real number `x`, ``identify(x)`` attempts to find an exact
536
+ formula for `x`. This formula is returned as a string. If no match
537
+ is found, ``None`` is returned. With ``full=True``, a list of
538
+ matching formulas is returned.
539
+
540
+ As a simple example, :func:`~mpmath.identify` will find an algebraic
541
+ formula for the golden ratio::
542
+
543
+ >>> from mpmath import *
544
+ >>> mp.dps = 15; mp.pretty = True
545
+ >>> identify(phi)
546
+ '((1+sqrt(5))/2)'
547
+
548
+ :func:`~mpmath.identify` can identify simple algebraic numbers and simple
549
+ combinations of given base constants, as well as certain basic
550
+ transformations thereof. More specifically, :func:`~mpmath.identify`
551
+ looks for the following:
552
+
553
+ 1. Fractions
554
+ 2. Quadratic algebraic numbers
555
+ 3. Rational linear combinations of the base constants
556
+ 4. Any of the above after first transforming `x` into `f(x)` where
557
+ `f(x)` is `1/x`, `\sqrt x`, `x^2`, `\log x` or `\exp x`, either
558
+ directly or with `x` or `f(x)` multiplied or divided by one of
559
+ the base constants
560
+ 5. Products of fractional powers of the base constants and
561
+ small integers
562
+
563
+ Base constants can be given as a list of strings representing mpmath
564
+ expressions (:func:`~mpmath.identify` will ``eval`` the strings to numerical
565
+ values and use the original strings for the output), or as a dict of
566
+ formula:value pairs.
567
+
568
+ In order not to produce spurious results, :func:`~mpmath.identify` should
569
+ be used with high precision; preferably 50 digits or more.
570
+
571
+ **Examples**
572
+
573
+ Simple identifications can be performed safely at standard
574
+ precision. Here the default recognition of rational, algebraic,
575
+ and exp/log of algebraic numbers is demonstrated::
576
+
577
+ >>> mp.dps = 15
578
+ >>> identify(0.22222222222222222)
579
+ '(2/9)'
580
+ >>> identify(1.9662210973805663)
581
+ 'sqrt(((24+sqrt(48))/8))'
582
+ >>> identify(4.1132503787829275)
583
+ 'exp((sqrt(8)/2))'
584
+ >>> identify(0.881373587019543)
585
+ 'log(((2+sqrt(8))/2))'
586
+
587
+ By default, :func:`~mpmath.identify` does not recognize `\pi`. At standard
588
+ precision it finds a not too useful approximation. At slightly
589
+ increased precision, this approximation is no longer accurate
590
+ enough and :func:`~mpmath.identify` more correctly returns ``None``::
591
+
592
+ >>> identify(pi)
593
+ '(2**(176/117)*3**(20/117)*5**(35/39))/(7**(92/117))'
594
+ >>> mp.dps = 30
595
+ >>> identify(pi)
596
+ >>>
597
+
598
+ Numbers such as `\pi`, and simple combinations of user-defined
599
+ constants, can be identified if they are provided explicitly::
600
+
601
+ >>> identify(3*pi-2*e, ['pi', 'e'])
602
+ '(3*pi + (-2)*e)'
603
+
604
+ Here is an example using a dict of constants. Note that the
605
+ constants need not be "atomic"; :func:`~mpmath.identify` can just
606
+ as well express the given number in terms of expressions
607
+ given by formulas::
608
+
609
+ >>> identify(pi+e, {'a':pi+2, 'b':2*e})
610
+ '((-2) + 1*a + (1/2)*b)'
611
+
612
+ Next, we attempt some identifications with a set of base constants.
613
+ It is necessary to increase the precision a bit.
614
+
615
+ >>> mp.dps = 50
616
+ >>> base = ['sqrt(2)','pi','log(2)']
617
+ >>> identify(0.25, base)
618
+ '(1/4)'
619
+ >>> identify(3*pi + 2*sqrt(2) + 5*log(2)/7, base)
620
+ '(2*sqrt(2) + 3*pi + (5/7)*log(2))'
621
+ >>> identify(exp(pi+2), base)
622
+ 'exp((2 + 1*pi))'
623
+ >>> identify(1/(3+sqrt(2)), base)
624
+ '((3/7) + (-1/7)*sqrt(2))'
625
+ >>> identify(sqrt(2)/(3*pi+4), base)
626
+ 'sqrt(2)/(4 + 3*pi)'
627
+ >>> identify(5**(mpf(1)/3)*pi*log(2)**2, base)
628
+ '5**(1/3)*pi*log(2)**2'
629
+
630
+ An example of an erroneous solution being found when too low
631
+ precision is used::
632
+
633
+ >>> mp.dps = 15
634
+ >>> identify(1/(3*pi-4*e+sqrt(8)), ['pi', 'e', 'sqrt(2)'])
635
+ '((11/25) + (-158/75)*pi + (76/75)*e + (44/15)*sqrt(2))'
636
+ >>> mp.dps = 50
637
+ >>> identify(1/(3*pi-4*e+sqrt(8)), ['pi', 'e', 'sqrt(2)'])
638
+ '1/(3*pi + (-4)*e + 2*sqrt(2))'
639
+
640
+ **Finding approximate solutions**
641
+
642
+ The tolerance ``tol`` defaults to 3/4 of the working precision.
643
+ Lowering the tolerance is useful for finding approximate matches.
644
+ We can for example try to generate approximations for pi::
645
+
646
+ >>> mp.dps = 15
647
+ >>> identify(pi, tol=1e-2)
648
+ '(22/7)'
649
+ >>> identify(pi, tol=1e-3)
650
+ '(355/113)'
651
+ >>> identify(pi, tol=1e-10)
652
+ '(5**(339/269))/(2**(64/269)*3**(13/269)*7**(92/269))'
653
+
654
+ With ``full=True``, and by supplying a few base constants,
655
+ ``identify`` can generate almost endless lists of approximations
656
+ for any number (the output below has been truncated to show only
657
+ the first few)::
658
+
659
+ >>> for p in identify(pi, ['e', 'catalan'], tol=1e-5, full=True):
660
+ ... print(p)
661
+ ... # doctest: +ELLIPSIS
662
+ e/log((6 + (-4/3)*e))
663
+ (3**3*5*e*catalan**2)/(2*7**2)
664
+ sqrt(((-13) + 1*e + 22*catalan))
665
+ log(((-6) + 24*e + 4*catalan)/e)
666
+ exp(catalan*((-1/5) + (8/15)*e))
667
+ catalan*(6 + (-6)*e + 15*catalan)
668
+ sqrt((5 + 26*e + (-3)*catalan))/e
669
+ e*sqrt(((-27) + 2*e + 25*catalan))
670
+ log(((-1) + (-11)*e + 59*catalan))
671
+ ((3/20) + (21/20)*e + (3/20)*catalan)
672
+ ...
673
+
674
+ The numerical values are roughly as close to `\pi` as permitted by the
675
+ specified tolerance:
676
+
677
+ >>> e/log(6-4*e/3)
678
+ 3.14157719846001
679
+ >>> 135*e*catalan**2/98
680
+ 3.14166950419369
681
+ >>> sqrt(e-13+22*catalan)
682
+ 3.14158000062992
683
+ >>> log(24*e-6+4*catalan)-1
684
+ 3.14158791577159
685
+
686
+ **Symbolic processing**
687
+
688
+ The output formula can be evaluated as a Python expression.
689
+ Note however that if fractions (like '2/3') are present in
690
+ the formula, Python's :func:`~mpmath.eval()` may erroneously perform
691
+ integer division. Note also that the output is not necessarily
692
+ in the algebraically simplest form::
693
+
694
+ >>> identify(sqrt(2))
695
+ '(sqrt(8)/2)'
696
+
697
+ As a solution to both problems, consider using SymPy's
698
+ :func:`~mpmath.sympify` to convert the formula into a symbolic expression.
699
+ SymPy can be used to pretty-print or further simplify the formula
700
+ symbolically::
701
+
702
+ >>> from sympy import sympify # doctest: +SKIP
703
+ >>> sympify(identify(sqrt(2))) # doctest: +SKIP
704
+ 2**(1/2)
705
+
706
+ Sometimes :func:`~mpmath.identify` can simplify an expression further than
707
+ a symbolic algorithm::
708
+
709
+ >>> from sympy import simplify # doctest: +SKIP
710
+ >>> x = sympify('-1/(-3/2+(1/2)*5**(1/2))*(3/2-1/2*5**(1/2))**(1/2)') # doctest: +SKIP
711
+ >>> x # doctest: +SKIP
712
+ (3/2 - 5**(1/2)/2)**(-1/2)
713
+ >>> x = simplify(x) # doctest: +SKIP
714
+ >>> x # doctest: +SKIP
715
+ 2/(6 - 2*5**(1/2))**(1/2)
716
+ >>> mp.dps = 30 # doctest: +SKIP
717
+ >>> x = sympify(identify(x.evalf(30))) # doctest: +SKIP
718
+ >>> x # doctest: +SKIP
719
+ 1/2 + 5**(1/2)/2
720
+
721
+ (In fact, this functionality is available directly in SymPy as the
722
+ function :func:`~mpmath.nsimplify`, which is essentially a wrapper for
723
+ :func:`~mpmath.identify`.)
724
+
725
+ **Miscellaneous issues and limitations**
726
+
727
+ The input `x` must be a real number. All base constants must be
728
+ positive real numbers and must not be rationals or rational linear
729
+ combinations of each other.
730
+
731
+ The worst-case computation time grows quickly with the number of
732
+ base constants. Already with 3 or 4 base constants,
733
+ :func:`~mpmath.identify` may require several seconds to finish. To search
734
+ for relations among a large number of constants, you should
735
+ consider using :func:`~mpmath.pslq` directly.
736
+
737
+ The extended transformations are applied to x, not the constants
738
+ separately. As a result, ``identify`` will for example be able to
739
+ recognize ``exp(2*pi+3)`` with ``pi`` given as a base constant, but
740
+ not ``2*exp(pi)+3``. It will be able to recognize the latter if
741
+ ``exp(pi)`` is given explicitly as a base constant.
742
+
743
+ """
744
+
745
+ solutions = []
746
+
747
+ def addsolution(s):
748
+ if verbose: print("Found: ", s)
749
+ solutions.append(s)
750
+
751
+ x = ctx.mpf(x)
752
+
753
+ # Further along, x will be assumed positive
754
+ if x == 0:
755
+ if full: return ['0']
756
+ else: return '0'
757
+ if x < 0:
758
+ sol = ctx.identify(-x, constants, tol, maxcoeff, full, verbose)
759
+ if sol is None:
760
+ return sol
761
+ if full:
762
+ return ["-(%s)"%s for s in sol]
763
+ else:
764
+ return "-(%s)" % sol
765
+
766
+ if tol:
767
+ tol = ctx.mpf(tol)
768
+ else:
769
+ tol = ctx.eps**0.7
770
+ M = maxcoeff
771
+
772
+ if constants:
773
+ if isinstance(constants, dict):
774
+ constants = [(ctx.mpf(v), name) for (name, v) in sorted(constants.items())]
775
+ else:
776
+ namespace = dict((name, getattr(ctx,name)) for name in dir(ctx))
777
+ constants = [(eval(p, namespace), p) for p in constants]
778
+ else:
779
+ constants = []
780
+
781
+ # We always want to find at least rational terms
782
+ if 1 not in [value for (name, value) in constants]:
783
+ constants = [(ctx.mpf(1), '1')] + constants
784
+
785
+ # PSLQ with simple algebraic and functional transformations
786
+ for ft, ftn, red in transforms:
787
+ for c, cn in constants:
788
+ if red and cn == '1':
789
+ continue
790
+ t = ft(ctx,x,c)
791
+ # Prevent exponential transforms from wreaking havoc
792
+ if abs(t) > M**2 or abs(t) < tol:
793
+ continue
794
+ # Linear combination of base constants
795
+ r = ctx.pslq([t] + [a[0] for a in constants], tol, M)
796
+ s = None
797
+ if r is not None and max(abs(uw) for uw in r) <= M and r[0]:
798
+ s = pslqstring(r, constants)
799
+ # Quadratic algebraic numbers
800
+ else:
801
+ q = ctx.pslq([ctx.one, t, t**2], tol, M)
802
+ if q is not None and len(q) == 3 and q[2]:
803
+ aa, bb, cc = q
804
+ if max(abs(aa),abs(bb),abs(cc)) <= M:
805
+ s = quadraticstring(ctx,t,aa,bb,cc)
806
+ if s:
807
+ if cn == '1' and ('/$c' in ftn):
808
+ s = ftn.replace('$y', s).replace('/$c', '')
809
+ else:
810
+ s = ftn.replace('$y', s).replace('$c', cn)
811
+ addsolution(s)
812
+ if not full: return solutions[0]
813
+
814
+ if verbose:
815
+ print(".")
816
+
817
+ # Check for a direct multiplicative formula
818
+ if x != 1:
819
+ # Allow fractional powers of fractions
820
+ ilogs = [2,3,5,7]
821
+ # Watch out for existing fractional powers of fractions
822
+ logs = []
823
+ for a, s in constants:
824
+ if not sum(bool(ctx.findpoly(ctx.ln(a)/ctx.ln(i),1)) for i in ilogs):
825
+ logs.append((ctx.ln(a), s))
826
+ logs = [(ctx.ln(i),str(i)) for i in ilogs] + logs
827
+ r = ctx.pslq([ctx.ln(x)] + [a[0] for a in logs], tol, M)
828
+ if r is not None and max(abs(uw) for uw in r) <= M and r[0]:
829
+ addsolution(prodstring(r, logs))
830
+ if not full: return solutions[0]
831
+
832
+ if full:
833
+ return sorted(solutions, key=len)
834
+ else:
835
+ return None
836
+
837
+ IdentificationMethods.pslq = pslq
838
+ IdentificationMethods.findpoly = findpoly
839
+ IdentificationMethods.identify = identify
840
+
841
+
842
+ if __name__ == '__main__':
843
+ import doctest
844
+ doctest.testmod()
llmeval-env/lib/python3.10/site-packages/mpmath/math2.py ADDED
@@ -0,0 +1,672 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ This module complements the math and cmath builtin modules by providing
3
+ fast machine precision versions of some additional functions (gamma, ...)
4
+ and wrapping math/cmath functions so that they can be called with either
5
+ real or complex arguments.
6
+ """
7
+
8
+ import operator
9
+ import math
10
+ import cmath
11
+
12
+ # Irrational (?) constants
13
+ pi = 3.1415926535897932385
14
+ e = 2.7182818284590452354
15
+ sqrt2 = 1.4142135623730950488
16
+ sqrt5 = 2.2360679774997896964
17
+ phi = 1.6180339887498948482
18
+ ln2 = 0.69314718055994530942
19
+ ln10 = 2.302585092994045684
20
+ euler = 0.57721566490153286061
21
+ catalan = 0.91596559417721901505
22
+ khinchin = 2.6854520010653064453
23
+ apery = 1.2020569031595942854
24
+
25
+ logpi = 1.1447298858494001741
26
+
27
+ def _mathfun_real(f_real, f_complex):
28
+ def f(x, **kwargs):
29
+ if type(x) is float:
30
+ return f_real(x)
31
+ if type(x) is complex:
32
+ return f_complex(x)
33
+ try:
34
+ x = float(x)
35
+ return f_real(x)
36
+ except (TypeError, ValueError):
37
+ x = complex(x)
38
+ return f_complex(x)
39
+ f.__name__ = f_real.__name__
40
+ return f
41
+
42
+ def _mathfun(f_real, f_complex):
43
+ def f(x, **kwargs):
44
+ if type(x) is complex:
45
+ return f_complex(x)
46
+ try:
47
+ return f_real(float(x))
48
+ except (TypeError, ValueError):
49
+ return f_complex(complex(x))
50
+ f.__name__ = f_real.__name__
51
+ return f
52
+
53
+ def _mathfun_n(f_real, f_complex):
54
+ def f(*args, **kwargs):
55
+ try:
56
+ return f_real(*(float(x) for x in args))
57
+ except (TypeError, ValueError):
58
+ return f_complex(*(complex(x) for x in args))
59
+ f.__name__ = f_real.__name__
60
+ return f
61
+
62
+ # Workaround for non-raising log and sqrt in Python 2.5 and 2.4
63
+ # on Unix system
64
+ try:
65
+ math.log(-2.0)
66
+ def math_log(x):
67
+ if x <= 0.0:
68
+ raise ValueError("math domain error")
69
+ return math.log(x)
70
+ def math_sqrt(x):
71
+ if x < 0.0:
72
+ raise ValueError("math domain error")
73
+ return math.sqrt(x)
74
+ except (ValueError, TypeError):
75
+ math_log = math.log
76
+ math_sqrt = math.sqrt
77
+
78
+ pow = _mathfun_n(operator.pow, lambda x, y: complex(x)**y)
79
+ log = _mathfun_n(math_log, cmath.log)
80
+ sqrt = _mathfun(math_sqrt, cmath.sqrt)
81
+ exp = _mathfun_real(math.exp, cmath.exp)
82
+
83
+ cos = _mathfun_real(math.cos, cmath.cos)
84
+ sin = _mathfun_real(math.sin, cmath.sin)
85
+ tan = _mathfun_real(math.tan, cmath.tan)
86
+
87
+ acos = _mathfun(math.acos, cmath.acos)
88
+ asin = _mathfun(math.asin, cmath.asin)
89
+ atan = _mathfun_real(math.atan, cmath.atan)
90
+
91
+ cosh = _mathfun_real(math.cosh, cmath.cosh)
92
+ sinh = _mathfun_real(math.sinh, cmath.sinh)
93
+ tanh = _mathfun_real(math.tanh, cmath.tanh)
94
+
95
+ floor = _mathfun_real(math.floor,
96
+ lambda z: complex(math.floor(z.real), math.floor(z.imag)))
97
+ ceil = _mathfun_real(math.ceil,
98
+ lambda z: complex(math.ceil(z.real), math.ceil(z.imag)))
99
+
100
+
101
+ cos_sin = _mathfun_real(lambda x: (math.cos(x), math.sin(x)),
102
+ lambda z: (cmath.cos(z), cmath.sin(z)))
103
+
104
+ cbrt = _mathfun(lambda x: x**(1./3), lambda z: z**(1./3))
105
+
106
+ def nthroot(x, n):
107
+ r = 1./n
108
+ try:
109
+ return float(x) ** r
110
+ except (ValueError, TypeError):
111
+ return complex(x) ** r
112
+
113
+ def _sinpi_real(x):
114
+ if x < 0:
115
+ return -_sinpi_real(-x)
116
+ n, r = divmod(x, 0.5)
117
+ r *= pi
118
+ n %= 4
119
+ if n == 0: return math.sin(r)
120
+ if n == 1: return math.cos(r)
121
+ if n == 2: return -math.sin(r)
122
+ if n == 3: return -math.cos(r)
123
+
124
+ def _cospi_real(x):
125
+ if x < 0:
126
+ x = -x
127
+ n, r = divmod(x, 0.5)
128
+ r *= pi
129
+ n %= 4
130
+ if n == 0: return math.cos(r)
131
+ if n == 1: return -math.sin(r)
132
+ if n == 2: return -math.cos(r)
133
+ if n == 3: return math.sin(r)
134
+
135
+ def _sinpi_complex(z):
136
+ if z.real < 0:
137
+ return -_sinpi_complex(-z)
138
+ n, r = divmod(z.real, 0.5)
139
+ z = pi*complex(r, z.imag)
140
+ n %= 4
141
+ if n == 0: return cmath.sin(z)
142
+ if n == 1: return cmath.cos(z)
143
+ if n == 2: return -cmath.sin(z)
144
+ if n == 3: return -cmath.cos(z)
145
+
146
+ def _cospi_complex(z):
147
+ if z.real < 0:
148
+ z = -z
149
+ n, r = divmod(z.real, 0.5)
150
+ z = pi*complex(r, z.imag)
151
+ n %= 4
152
+ if n == 0: return cmath.cos(z)
153
+ if n == 1: return -cmath.sin(z)
154
+ if n == 2: return -cmath.cos(z)
155
+ if n == 3: return cmath.sin(z)
156
+
157
+ cospi = _mathfun_real(_cospi_real, _cospi_complex)
158
+ sinpi = _mathfun_real(_sinpi_real, _sinpi_complex)
159
+
160
+ def tanpi(x):
161
+ try:
162
+ return sinpi(x) / cospi(x)
163
+ except OverflowError:
164
+ if complex(x).imag > 10:
165
+ return 1j
166
+ if complex(x).imag < 10:
167
+ return -1j
168
+ raise
169
+
170
+ def cotpi(x):
171
+ try:
172
+ return cospi(x) / sinpi(x)
173
+ except OverflowError:
174
+ if complex(x).imag > 10:
175
+ return -1j
176
+ if complex(x).imag < 10:
177
+ return 1j
178
+ raise
179
+
180
+ INF = 1e300*1e300
181
+ NINF = -INF
182
+ NAN = INF-INF
183
+ EPS = 2.2204460492503131e-16
184
+
185
+ _exact_gamma = (INF, 1.0, 1.0, 2.0, 6.0, 24.0, 120.0, 720.0, 5040.0, 40320.0,
186
+ 362880.0, 3628800.0, 39916800.0, 479001600.0, 6227020800.0, 87178291200.0,
187
+ 1307674368000.0, 20922789888000.0, 355687428096000.0, 6402373705728000.0,
188
+ 121645100408832000.0, 2432902008176640000.0)
189
+
190
+ _max_exact_gamma = len(_exact_gamma)-1
191
+
192
+ # Lanczos coefficients used by the GNU Scientific Library
193
+ _lanczos_g = 7
194
+ _lanczos_p = (0.99999999999980993, 676.5203681218851, -1259.1392167224028,
195
+ 771.32342877765313, -176.61502916214059, 12.507343278686905,
196
+ -0.13857109526572012, 9.9843695780195716e-6, 1.5056327351493116e-7)
197
+
198
+ def _gamma_real(x):
199
+ _intx = int(x)
200
+ if _intx == x:
201
+ if _intx <= 0:
202
+ #return (-1)**_intx * INF
203
+ raise ZeroDivisionError("gamma function pole")
204
+ if _intx <= _max_exact_gamma:
205
+ return _exact_gamma[_intx]
206
+ if x < 0.5:
207
+ # TODO: sinpi
208
+ return pi / (_sinpi_real(x)*_gamma_real(1-x))
209
+ else:
210
+ x -= 1.0
211
+ r = _lanczos_p[0]
212
+ for i in range(1, _lanczos_g+2):
213
+ r += _lanczos_p[i]/(x+i)
214
+ t = x + _lanczos_g + 0.5
215
+ return 2.506628274631000502417 * t**(x+0.5) * math.exp(-t) * r
216
+
217
+ def _gamma_complex(x):
218
+ if not x.imag:
219
+ return complex(_gamma_real(x.real))
220
+ if x.real < 0.5:
221
+ # TODO: sinpi
222
+ return pi / (_sinpi_complex(x)*_gamma_complex(1-x))
223
+ else:
224
+ x -= 1.0
225
+ r = _lanczos_p[0]
226
+ for i in range(1, _lanczos_g+2):
227
+ r += _lanczos_p[i]/(x+i)
228
+ t = x + _lanczos_g + 0.5
229
+ return 2.506628274631000502417 * t**(x+0.5) * cmath.exp(-t) * r
230
+
231
+ gamma = _mathfun_real(_gamma_real, _gamma_complex)
232
+
233
+ def rgamma(x):
234
+ try:
235
+ return 1./gamma(x)
236
+ except ZeroDivisionError:
237
+ return x*0.0
238
+
239
+ def factorial(x):
240
+ return gamma(x+1.0)
241
+
242
+ def arg(x):
243
+ if type(x) is float:
244
+ return math.atan2(0.0,x)
245
+ return math.atan2(x.imag,x.real)
246
+
247
+ # XXX: broken for negatives
248
+ def loggamma(x):
249
+ if type(x) not in (float, complex):
250
+ try:
251
+ x = float(x)
252
+ except (ValueError, TypeError):
253
+ x = complex(x)
254
+ try:
255
+ xreal = x.real
256
+ ximag = x.imag
257
+ except AttributeError: # py2.5
258
+ xreal = x
259
+ ximag = 0.0
260
+ # Reflection formula
261
+ # http://functions.wolfram.com/GammaBetaErf/LogGamma/16/01/01/0003/
262
+ if xreal < 0.0:
263
+ if abs(x) < 0.5:
264
+ v = log(gamma(x))
265
+ if ximag == 0:
266
+ v = v.conjugate()
267
+ return v
268
+ z = 1-x
269
+ try:
270
+ re = z.real
271
+ im = z.imag
272
+ except AttributeError: # py2.5
273
+ re = z
274
+ im = 0.0
275
+ refloor = floor(re)
276
+ if im == 0.0:
277
+ imsign = 0
278
+ elif im < 0.0:
279
+ imsign = -1
280
+ else:
281
+ imsign = 1
282
+ return (-pi*1j)*abs(refloor)*(1-abs(imsign)) + logpi - \
283
+ log(sinpi(z-refloor)) - loggamma(z) + 1j*pi*refloor*imsign
284
+ if x == 1.0 or x == 2.0:
285
+ return x*0
286
+ p = 0.
287
+ while abs(x) < 11:
288
+ p -= log(x)
289
+ x += 1.0
290
+ s = 0.918938533204672742 + (x-0.5)*log(x) - x
291
+ r = 1./x
292
+ r2 = r*r
293
+ s += 0.083333333333333333333*r; r *= r2
294
+ s += -0.0027777777777777777778*r; r *= r2
295
+ s += 0.00079365079365079365079*r; r *= r2
296
+ s += -0.0005952380952380952381*r; r *= r2
297
+ s += 0.00084175084175084175084*r; r *= r2
298
+ s += -0.0019175269175269175269*r; r *= r2
299
+ s += 0.0064102564102564102564*r; r *= r2
300
+ s += -0.02955065359477124183*r
301
+ return s + p
302
+
303
+ _psi_coeff = [
304
+ 0.083333333333333333333,
305
+ -0.0083333333333333333333,
306
+ 0.003968253968253968254,
307
+ -0.0041666666666666666667,
308
+ 0.0075757575757575757576,
309
+ -0.021092796092796092796,
310
+ 0.083333333333333333333,
311
+ -0.44325980392156862745,
312
+ 3.0539543302701197438,
313
+ -26.456212121212121212]
314
+
315
+ def _digamma_real(x):
316
+ _intx = int(x)
317
+ if _intx == x:
318
+ if _intx <= 0:
319
+ raise ZeroDivisionError("polygamma pole")
320
+ if x < 0.5:
321
+ x = 1.0-x
322
+ s = pi*cotpi(x)
323
+ else:
324
+ s = 0.0
325
+ while x < 10.0:
326
+ s -= 1.0/x
327
+ x += 1.0
328
+ x2 = x**-2
329
+ t = x2
330
+ for c in _psi_coeff:
331
+ s -= c*t
332
+ if t < 1e-20:
333
+ break
334
+ t *= x2
335
+ return s + math_log(x) - 0.5/x
336
+
337
+ def _digamma_complex(x):
338
+ if not x.imag:
339
+ return complex(_digamma_real(x.real))
340
+ if x.real < 0.5:
341
+ x = 1.0-x
342
+ s = pi*cotpi(x)
343
+ else:
344
+ s = 0.0
345
+ while abs(x) < 10.0:
346
+ s -= 1.0/x
347
+ x += 1.0
348
+ x2 = x**-2
349
+ t = x2
350
+ for c in _psi_coeff:
351
+ s -= c*t
352
+ if abs(t) < 1e-20:
353
+ break
354
+ t *= x2
355
+ return s + cmath.log(x) - 0.5/x
356
+
357
+ digamma = _mathfun_real(_digamma_real, _digamma_complex)
358
+
359
+ # TODO: could implement complex erf and erfc here. Need
360
+ # to find an accurate method (avoiding cancellation)
361
+ # for approx. 1 < abs(x) < 9.
362
+
363
+ _erfc_coeff_P = [
364
+ 1.0000000161203922312,
365
+ 2.1275306946297962644,
366
+ 2.2280433377390253297,
367
+ 1.4695509105618423961,
368
+ 0.66275911699770787537,
369
+ 0.20924776504163751585,
370
+ 0.045459713768411264339,
371
+ 0.0063065951710717791934,
372
+ 0.00044560259661560421715][::-1]
373
+
374
+ _erfc_coeff_Q = [
375
+ 1.0000000000000000000,
376
+ 3.2559100272784894318,
377
+ 4.9019435608903239131,
378
+ 4.4971472894498014205,
379
+ 2.7845640601891186528,
380
+ 1.2146026030046904138,
381
+ 0.37647108453729465912,
382
+ 0.080970149639040548613,
383
+ 0.011178148899483545902,
384
+ 0.00078981003831980423513][::-1]
385
+
386
+ def _polyval(coeffs, x):
387
+ p = coeffs[0]
388
+ for c in coeffs[1:]:
389
+ p = c + x*p
390
+ return p
391
+
392
+ def _erf_taylor(x):
393
+ # Taylor series assuming 0 <= x <= 1
394
+ x2 = x*x
395
+ s = t = x
396
+ n = 1
397
+ while abs(t) > 1e-17:
398
+ t *= x2/n
399
+ s -= t/(n+n+1)
400
+ n += 1
401
+ t *= x2/n
402
+ s += t/(n+n+1)
403
+ n += 1
404
+ return 1.1283791670955125739*s
405
+
406
+ def _erfc_mid(x):
407
+ # Rational approximation assuming 0 <= x <= 9
408
+ return exp(-x*x)*_polyval(_erfc_coeff_P,x)/_polyval(_erfc_coeff_Q,x)
409
+
410
+ def _erfc_asymp(x):
411
+ # Asymptotic expansion assuming x >= 9
412
+ x2 = x*x
413
+ v = exp(-x2)/x*0.56418958354775628695
414
+ r = t = 0.5 / x2
415
+ s = 1.0
416
+ for n in range(1,22,4):
417
+ s -= t
418
+ t *= r * (n+2)
419
+ s += t
420
+ t *= r * (n+4)
421
+ if abs(t) < 1e-17:
422
+ break
423
+ return s * v
424
+
425
+ def erf(x):
426
+ """
427
+ erf of a real number.
428
+ """
429
+ x = float(x)
430
+ if x != x:
431
+ return x
432
+ if x < 0.0:
433
+ return -erf(-x)
434
+ if x >= 1.0:
435
+ if x >= 6.0:
436
+ return 1.0
437
+ return 1.0 - _erfc_mid(x)
438
+ return _erf_taylor(x)
439
+
440
+ def erfc(x):
441
+ """
442
+ erfc of a real number.
443
+ """
444
+ x = float(x)
445
+ if x != x:
446
+ return x
447
+ if x < 0.0:
448
+ if x < -6.0:
449
+ return 2.0
450
+ return 2.0-erfc(-x)
451
+ if x > 9.0:
452
+ return _erfc_asymp(x)
453
+ if x >= 1.0:
454
+ return _erfc_mid(x)
455
+ return 1.0 - _erf_taylor(x)
456
+
457
+ gauss42 = [\
458
+ (0.99839961899006235, 0.0041059986046490839),
459
+ (-0.99839961899006235, 0.0041059986046490839),
460
+ (0.9915772883408609, 0.009536220301748501),
461
+ (-0.9915772883408609,0.009536220301748501),
462
+ (0.97934250806374812, 0.014922443697357493),
463
+ (-0.97934250806374812, 0.014922443697357493),
464
+ (0.96175936533820439,0.020227869569052644),
465
+ (-0.96175936533820439, 0.020227869569052644),
466
+ (0.93892355735498811, 0.025422959526113047),
467
+ (-0.93892355735498811,0.025422959526113047),
468
+ (0.91095972490412735, 0.030479240699603467),
469
+ (-0.91095972490412735, 0.030479240699603467),
470
+ (0.87802056981217269,0.03536907109759211),
471
+ (-0.87802056981217269, 0.03536907109759211),
472
+ (0.8402859832618168, 0.040065735180692258),
473
+ (-0.8402859832618168,0.040065735180692258),
474
+ (0.7979620532554873, 0.044543577771965874),
475
+ (-0.7979620532554873, 0.044543577771965874),
476
+ (0.75127993568948048,0.048778140792803244),
477
+ (-0.75127993568948048, 0.048778140792803244),
478
+ (0.70049459055617114, 0.052746295699174064),
479
+ (-0.70049459055617114,0.052746295699174064),
480
+ (0.64588338886924779, 0.056426369358018376),
481
+ (-0.64588338886924779, 0.056426369358018376),
482
+ (0.58774459748510932, 0.059798262227586649),
483
+ (-0.58774459748510932, 0.059798262227586649),
484
+ (0.5263957499311922, 0.062843558045002565),
485
+ (-0.5263957499311922, 0.062843558045002565),
486
+ (0.46217191207042191, 0.065545624364908975),
487
+ (-0.46217191207042191, 0.065545624364908975),
488
+ (0.39542385204297503, 0.067889703376521934),
489
+ (-0.39542385204297503, 0.067889703376521934),
490
+ (0.32651612446541151, 0.069862992492594159),
491
+ (-0.32651612446541151, 0.069862992492594159),
492
+ (0.25582507934287907, 0.071454714265170971),
493
+ (-0.25582507934287907, 0.071454714265170971),
494
+ (0.18373680656485453, 0.072656175243804091),
495
+ (-0.18373680656485453, 0.072656175243804091),
496
+ (0.11064502720851986, 0.073460813453467527),
497
+ (-0.11064502720851986, 0.073460813453467527),
498
+ (0.036948943165351772, 0.073864234232172879),
499
+ (-0.036948943165351772, 0.073864234232172879)]
500
+
501
+ EI_ASYMP_CONVERGENCE_RADIUS = 40.0
502
+
503
+ def ei_asymp(z, _e1=False):
504
+ r = 1./z
505
+ s = t = 1.0
506
+ k = 1
507
+ while 1:
508
+ t *= k*r
509
+ s += t
510
+ if abs(t) < 1e-16:
511
+ break
512
+ k += 1
513
+ v = s*exp(z)/z
514
+ if _e1:
515
+ if type(z) is complex:
516
+ zreal = z.real
517
+ zimag = z.imag
518
+ else:
519
+ zreal = z
520
+ zimag = 0.0
521
+ if zimag == 0.0 and zreal > 0.0:
522
+ v += pi*1j
523
+ else:
524
+ if type(z) is complex:
525
+ if z.imag > 0:
526
+ v += pi*1j
527
+ if z.imag < 0:
528
+ v -= pi*1j
529
+ return v
530
+
531
+ def ei_taylor(z, _e1=False):
532
+ s = t = z
533
+ k = 2
534
+ while 1:
535
+ t = t*z/k
536
+ term = t/k
537
+ if abs(term) < 1e-17:
538
+ break
539
+ s += term
540
+ k += 1
541
+ s += euler
542
+ if _e1:
543
+ s += log(-z)
544
+ else:
545
+ if type(z) is float or z.imag == 0.0:
546
+ s += math_log(abs(z))
547
+ else:
548
+ s += cmath.log(z)
549
+ return s
550
+
551
+ def ei(z, _e1=False):
552
+ typez = type(z)
553
+ if typez not in (float, complex):
554
+ try:
555
+ z = float(z)
556
+ typez = float
557
+ except (TypeError, ValueError):
558
+ z = complex(z)
559
+ typez = complex
560
+ if not z:
561
+ return -INF
562
+ absz = abs(z)
563
+ if absz > EI_ASYMP_CONVERGENCE_RADIUS:
564
+ return ei_asymp(z, _e1)
565
+ elif absz <= 2.0 or (typez is float and z > 0.0):
566
+ return ei_taylor(z, _e1)
567
+ # Integrate, starting from whichever is smaller of a Taylor
568
+ # series value or an asymptotic series value
569
+ if typez is complex and z.real > 0.0:
570
+ zref = z / absz
571
+ ref = ei_taylor(zref, _e1)
572
+ else:
573
+ zref = EI_ASYMP_CONVERGENCE_RADIUS * z / absz
574
+ ref = ei_asymp(zref, _e1)
575
+ C = (zref-z)*0.5
576
+ D = (zref+z)*0.5
577
+ s = 0.0
578
+ if type(z) is complex:
579
+ _exp = cmath.exp
580
+ else:
581
+ _exp = math.exp
582
+ for x,w in gauss42:
583
+ t = C*x+D
584
+ s += w*_exp(t)/t
585
+ ref -= C*s
586
+ return ref
587
+
588
+ def e1(z):
589
+ # hack to get consistent signs if the imaginary part if 0
590
+ # and signed
591
+ typez = type(z)
592
+ if type(z) not in (float, complex):
593
+ try:
594
+ z = float(z)
595
+ typez = float
596
+ except (TypeError, ValueError):
597
+ z = complex(z)
598
+ typez = complex
599
+ if typez is complex and not z.imag:
600
+ z = complex(z.real, 0.0)
601
+ # end hack
602
+ return -ei(-z, _e1=True)
603
+
604
+ _zeta_int = [\
605
+ -0.5,
606
+ 0.0,
607
+ 1.6449340668482264365,1.2020569031595942854,1.0823232337111381915,
608
+ 1.0369277551433699263,1.0173430619844491397,1.0083492773819228268,
609
+ 1.0040773561979443394,1.0020083928260822144,1.0009945751278180853,
610
+ 1.0004941886041194646,1.0002460865533080483,1.0001227133475784891,
611
+ 1.0000612481350587048,1.0000305882363070205,1.0000152822594086519,
612
+ 1.0000076371976378998,1.0000038172932649998,1.0000019082127165539,
613
+ 1.0000009539620338728,1.0000004769329867878,1.0000002384505027277,
614
+ 1.0000001192199259653,1.0000000596081890513,1.0000000298035035147,
615
+ 1.0000000149015548284]
616
+
617
+ _zeta_P = [-3.50000000087575873, -0.701274355654678147,
618
+ -0.0672313458590012612, -0.00398731457954257841,
619
+ -0.000160948723019303141, -4.67633010038383371e-6,
620
+ -1.02078104417700585e-7, -1.68030037095896287e-9,
621
+ -1.85231868742346722e-11][::-1]
622
+
623
+ _zeta_Q = [1.00000000000000000, -0.936552848762465319,
624
+ -0.0588835413263763741, -0.00441498861482948666,
625
+ -0.000143416758067432622, -5.10691659585090782e-6,
626
+ -9.58813053268913799e-8, -1.72963791443181972e-9,
627
+ -1.83527919681474132e-11][::-1]
628
+
629
+ _zeta_1 = [3.03768838606128127e-10, -1.21924525236601262e-8,
630
+ 2.01201845887608893e-7, -1.53917240683468381e-6,
631
+ -5.09890411005967954e-7, 0.000122464707271619326,
632
+ -0.000905721539353130232, -0.00239315326074843037,
633
+ 0.084239750013159168, 0.418938517907442414, 0.500000001921884009]
634
+
635
+ _zeta_0 = [-3.46092485016748794e-10, -6.42610089468292485e-9,
636
+ 1.76409071536679773e-7, -1.47141263991560698e-6, -6.38880222546167613e-7,
637
+ 0.000122641099800668209, -0.000905894913516772796, -0.00239303348507992713,
638
+ 0.0842396947501199816, 0.418938533204660256, 0.500000000000000052]
639
+
640
+ def zeta(s):
641
+ """
642
+ Riemann zeta function, real argument
643
+ """
644
+ if not isinstance(s, (float, int)):
645
+ try:
646
+ s = float(s)
647
+ except (ValueError, TypeError):
648
+ try:
649
+ s = complex(s)
650
+ if not s.imag:
651
+ return complex(zeta(s.real))
652
+ except (ValueError, TypeError):
653
+ pass
654
+ raise NotImplementedError
655
+ if s == 1:
656
+ raise ValueError("zeta(1) pole")
657
+ if s >= 27:
658
+ return 1.0 + 2.0**(-s) + 3.0**(-s)
659
+ n = int(s)
660
+ if n == s:
661
+ if n >= 0:
662
+ return _zeta_int[n]
663
+ if not (n % 2):
664
+ return 0.0
665
+ if s <= 0.0:
666
+ return 2.**s*pi**(s-1)*_sinpi_real(0.5*s)*_gamma_real(1-s)*zeta(1-s)
667
+ if s <= 2.0:
668
+ if s <= 1.0:
669
+ return _polyval(_zeta_0,s)/(s-1)
670
+ return _polyval(_zeta_1,s)/(s-1)
671
+ z = _polyval(_zeta_P,s) / _polyval(_zeta_Q,s)
672
+ return 1.0 + 2.0**(-s) + 3.0**(-s) + 4.0**(-s)*z
llmeval-env/lib/python3.10/site-packages/mpmath/rational.py ADDED
@@ -0,0 +1,240 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import operator
2
+ import sys
3
+ from .libmp import int_types, mpf_hash, bitcount, from_man_exp, HASH_MODULUS
4
+
5
+ new = object.__new__
6
+
7
+ def create_reduced(p, q, _cache={}):
8
+ key = p, q
9
+ if key in _cache:
10
+ return _cache[key]
11
+ x, y = p, q
12
+ while y:
13
+ x, y = y, x % y
14
+ if x != 1:
15
+ p //= x
16
+ q //= x
17
+ v = new(mpq)
18
+ v._mpq_ = p, q
19
+ # Speedup integers, half-integers and other small fractions
20
+ if q <= 4 and abs(key[0]) < 100:
21
+ _cache[key] = v
22
+ return v
23
+
24
+ class mpq(object):
25
+ """
26
+ Exact rational type, currently only intended for internal use.
27
+ """
28
+
29
+ __slots__ = ["_mpq_"]
30
+
31
+ def __new__(cls, p, q=1):
32
+ if type(p) is tuple:
33
+ p, q = p
34
+ elif hasattr(p, '_mpq_'):
35
+ p, q = p._mpq_
36
+ return create_reduced(p, q)
37
+
38
+ def __repr__(s):
39
+ return "mpq(%s,%s)" % s._mpq_
40
+
41
+ def __str__(s):
42
+ return "(%s/%s)" % s._mpq_
43
+
44
+ def __int__(s):
45
+ a, b = s._mpq_
46
+ return a // b
47
+
48
+ def __nonzero__(s):
49
+ return bool(s._mpq_[0])
50
+
51
+ __bool__ = __nonzero__
52
+
53
+ def __hash__(s):
54
+ a, b = s._mpq_
55
+ if sys.version_info >= (3, 2):
56
+ inverse = pow(b, HASH_MODULUS-2, HASH_MODULUS)
57
+ if not inverse:
58
+ h = sys.hash_info.inf
59
+ else:
60
+ h = (abs(a) * inverse) % HASH_MODULUS
61
+ if a < 0: h = -h
62
+ if h == -1: h = -2
63
+ return h
64
+ else:
65
+ if b == 1:
66
+ return hash(a)
67
+ # Power of two: mpf compatible hash
68
+ if not (b & (b-1)):
69
+ return mpf_hash(from_man_exp(a, 1-bitcount(b)))
70
+ return hash((a,b))
71
+
72
+ def __eq__(s, t):
73
+ ttype = type(t)
74
+ if ttype is mpq:
75
+ return s._mpq_ == t._mpq_
76
+ if ttype in int_types:
77
+ a, b = s._mpq_
78
+ if b != 1:
79
+ return False
80
+ return a == t
81
+ return NotImplemented
82
+
83
+ def __ne__(s, t):
84
+ ttype = type(t)
85
+ if ttype is mpq:
86
+ return s._mpq_ != t._mpq_
87
+ if ttype in int_types:
88
+ a, b = s._mpq_
89
+ if b != 1:
90
+ return True
91
+ return a != t
92
+ return NotImplemented
93
+
94
+ def _cmp(s, t, op):
95
+ ttype = type(t)
96
+ if ttype in int_types:
97
+ a, b = s._mpq_
98
+ return op(a, t*b)
99
+ if ttype is mpq:
100
+ a, b = s._mpq_
101
+ c, d = t._mpq_
102
+ return op(a*d, b*c)
103
+ return NotImplementedError
104
+
105
+ def __lt__(s, t): return s._cmp(t, operator.lt)
106
+ def __le__(s, t): return s._cmp(t, operator.le)
107
+ def __gt__(s, t): return s._cmp(t, operator.gt)
108
+ def __ge__(s, t): return s._cmp(t, operator.ge)
109
+
110
+ def __abs__(s):
111
+ a, b = s._mpq_
112
+ if a >= 0:
113
+ return s
114
+ v = new(mpq)
115
+ v._mpq_ = -a, b
116
+ return v
117
+
118
+ def __neg__(s):
119
+ a, b = s._mpq_
120
+ v = new(mpq)
121
+ v._mpq_ = -a, b
122
+ return v
123
+
124
+ def __pos__(s):
125
+ return s
126
+
127
+ def __add__(s, t):
128
+ ttype = type(t)
129
+ if ttype is mpq:
130
+ a, b = s._mpq_
131
+ c, d = t._mpq_
132
+ return create_reduced(a*d+b*c, b*d)
133
+ if ttype in int_types:
134
+ a, b = s._mpq_
135
+ v = new(mpq)
136
+ v._mpq_ = a+b*t, b
137
+ return v
138
+ return NotImplemented
139
+
140
+ __radd__ = __add__
141
+
142
+ def __sub__(s, t):
143
+ ttype = type(t)
144
+ if ttype is mpq:
145
+ a, b = s._mpq_
146
+ c, d = t._mpq_
147
+ return create_reduced(a*d-b*c, b*d)
148
+ if ttype in int_types:
149
+ a, b = s._mpq_
150
+ v = new(mpq)
151
+ v._mpq_ = a-b*t, b
152
+ return v
153
+ return NotImplemented
154
+
155
+ def __rsub__(s, t):
156
+ ttype = type(t)
157
+ if ttype is mpq:
158
+ a, b = s._mpq_
159
+ c, d = t._mpq_
160
+ return create_reduced(b*c-a*d, b*d)
161
+ if ttype in int_types:
162
+ a, b = s._mpq_
163
+ v = new(mpq)
164
+ v._mpq_ = b*t-a, b
165
+ return v
166
+ return NotImplemented
167
+
168
+ def __mul__(s, t):
169
+ ttype = type(t)
170
+ if ttype is mpq:
171
+ a, b = s._mpq_
172
+ c, d = t._mpq_
173
+ return create_reduced(a*c, b*d)
174
+ if ttype in int_types:
175
+ a, b = s._mpq_
176
+ return create_reduced(a*t, b)
177
+ return NotImplemented
178
+
179
+ __rmul__ = __mul__
180
+
181
+ def __div__(s, t):
182
+ ttype = type(t)
183
+ if ttype is mpq:
184
+ a, b = s._mpq_
185
+ c, d = t._mpq_
186
+ return create_reduced(a*d, b*c)
187
+ if ttype in int_types:
188
+ a, b = s._mpq_
189
+ return create_reduced(a, b*t)
190
+ return NotImplemented
191
+
192
+ def __rdiv__(s, t):
193
+ ttype = type(t)
194
+ if ttype is mpq:
195
+ a, b = s._mpq_
196
+ c, d = t._mpq_
197
+ return create_reduced(b*c, a*d)
198
+ if ttype in int_types:
199
+ a, b = s._mpq_
200
+ return create_reduced(b*t, a)
201
+ return NotImplemented
202
+
203
+ def __pow__(s, t):
204
+ ttype = type(t)
205
+ if ttype in int_types:
206
+ a, b = s._mpq_
207
+ if t:
208
+ if t < 0:
209
+ a, b, t = b, a, -t
210
+ v = new(mpq)
211
+ v._mpq_ = a**t, b**t
212
+ return v
213
+ raise ZeroDivisionError
214
+ return NotImplemented
215
+
216
+
217
+ mpq_1 = mpq((1,1))
218
+ mpq_0 = mpq((0,1))
219
+ mpq_1_2 = mpq((1,2))
220
+ mpq_3_2 = mpq((3,2))
221
+ mpq_1_4 = mpq((1,4))
222
+ mpq_1_16 = mpq((1,16))
223
+ mpq_3_16 = mpq((3,16))
224
+ mpq_5_2 = mpq((5,2))
225
+ mpq_3_4 = mpq((3,4))
226
+ mpq_7_4 = mpq((7,4))
227
+ mpq_5_4 = mpq((5,4))
228
+
229
+
230
+ # Register with "numbers" ABC
231
+ # We do not subclass, hence we do not use the @abstractmethod checks. While
232
+ # this is less invasive it may turn out that we do not actually support
233
+ # parts of the expected interfaces. See
234
+ # http://docs.python.org/2/library/numbers.html for list of abstract
235
+ # methods.
236
+ try:
237
+ import numbers
238
+ numbers.Rational.register(mpq)
239
+ except ImportError:
240
+ pass
llmeval-env/lib/python3.10/site-packages/mpmath/tests/__init__.py ADDED
File without changes
llmeval-env/lib/python3.10/site-packages/mpmath/tests/__pycache__/test_basic_ops.cpython-310.pyc ADDED
Binary file (14 kB). View file
 
llmeval-env/lib/python3.10/site-packages/mpmath/tests/__pycache__/test_convert.cpython-310.pyc ADDED
Binary file (9.52 kB). View file
 
llmeval-env/lib/python3.10/site-packages/mpmath/tests/__pycache__/test_diff.cpython-310.pyc ADDED
Binary file (3.65 kB). View file
 
llmeval-env/lib/python3.10/site-packages/mpmath/tests/__pycache__/test_division.cpython-310.pyc ADDED
Binary file (4.46 kB). View file
 
llmeval-env/lib/python3.10/site-packages/mpmath/tests/__pycache__/test_eigen.cpython-310.pyc ADDED
Binary file (3.58 kB). View file
 
llmeval-env/lib/python3.10/site-packages/mpmath/tests/__pycache__/test_functions2.cpython-310.pyc ADDED
Binary file (66.8 kB). View file
 
llmeval-env/lib/python3.10/site-packages/mpmath/tests/__pycache__/test_hp.cpython-310.pyc ADDED
Binary file (8.73 kB). View file
 
llmeval-env/lib/python3.10/site-packages/mpmath/tests/__pycache__/test_identify.cpython-310.pyc ADDED
Binary file (1.07 kB). View file
 
llmeval-env/lib/python3.10/site-packages/mpmath/tests/__pycache__/test_levin.cpython-310.pyc ADDED
Binary file (5.97 kB). View file
 
llmeval-env/lib/python3.10/site-packages/mpmath/tests/__pycache__/test_linalg.cpython-310.pyc ADDED
Binary file (11.1 kB). View file
 
llmeval-env/lib/python3.10/site-packages/mpmath/tests/__pycache__/test_ode.cpython-310.pyc ADDED
Binary file (1.64 kB). View file
 
llmeval-env/lib/python3.10/site-packages/mpmath/tests/extratest_gamma.py ADDED
@@ -0,0 +1,215 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from mpmath import *
2
+ from mpmath.libmp import ifac
3
+
4
+ import sys
5
+ if "-dps" in sys.argv:
6
+ maxdps = int(sys.argv[sys.argv.index("-dps")+1])
7
+ else:
8
+ maxdps = 1000
9
+
10
+ raise_ = "-raise" in sys.argv
11
+
12
+ errcount = 0
13
+
14
+ def check(name, func, z, y):
15
+ global errcount
16
+ try:
17
+ x = func(z)
18
+ except:
19
+ errcount += 1
20
+ if raise_:
21
+ raise
22
+ print()
23
+ print(name)
24
+ print("EXCEPTION")
25
+ import traceback
26
+ traceback.print_tb(sys.exc_info()[2])
27
+ print()
28
+ return
29
+ xre = x.real
30
+ xim = x.imag
31
+ yre = y.real
32
+ yim = y.imag
33
+ tol = eps*8
34
+ err = 0
35
+ if abs(xre-yre) > abs(yre)*tol:
36
+ err = 1
37
+ print()
38
+ print("Error! %s (re = %s, wanted %s, err=%s)" % (name, nstr(xre,10), nstr(yre,10), nstr(abs(xre-yre))))
39
+ errcount += 1
40
+ if raise_:
41
+ raise SystemExit
42
+ if abs(xim-yim) > abs(yim)*tol:
43
+ err = 1
44
+ print()
45
+ print("Error! %s (im = %s, wanted %s, err=%s)" % (name, nstr(xim,10), nstr(yim,10), nstr(abs(xim-yim))))
46
+ errcount += 1
47
+ if raise_:
48
+ raise SystemExit
49
+ if not err:
50
+ sys.stdout.write("%s ok; " % name)
51
+
52
+ def testcase(case):
53
+ z, result = case
54
+ print("Testing z =", z)
55
+ mp.dps = 1010
56
+ z = eval(z)
57
+ mp.dps = maxdps + 50
58
+ if result is None:
59
+ gamma_val = gamma(z)
60
+ loggamma_val = loggamma(z)
61
+ factorial_val = factorial(z)
62
+ rgamma_val = rgamma(z)
63
+ else:
64
+ loggamma_val = eval(result)
65
+ gamma_val = exp(loggamma_val)
66
+ factorial_val = z * gamma_val
67
+ rgamma_val = 1/gamma_val
68
+ for dps in [5, 10, 15, 25, 40, 60, 90, 120, 250, 600, 1000, 1800, 3600]:
69
+ if dps > maxdps:
70
+ break
71
+ mp.dps = dps
72
+ print("dps = %s" % dps)
73
+ check("gamma", gamma, z, gamma_val)
74
+ check("rgamma", rgamma, z, rgamma_val)
75
+ check("loggamma", loggamma, z, loggamma_val)
76
+ check("factorial", factorial, z, factorial_val)
77
+ print()
78
+ mp.dps = 15
79
+
80
+ testcases = []
81
+
82
+ # Basic values
83
+ for n in list(range(1,200)) + list(range(201,2000,17)):
84
+ testcases.append(["%s" % n, None])
85
+ for n in range(-200,200):
86
+ testcases.append(["%s+0.5" % n, None])
87
+ testcases.append(["%s+0.37" % n, None])
88
+
89
+ testcases += [\
90
+ ["(0.1+1j)", None],
91
+ ["(-0.1+1j)", None],
92
+ ["(0.1-1j)", None],
93
+ ["(-0.1-1j)", None],
94
+ ["10j", None],
95
+ ["-10j", None],
96
+ ["100j", None],
97
+ ["10000j", None],
98
+ ["-10000000j", None],
99
+ ["(10**100)*j", None],
100
+ ["125+(10**100)*j", None],
101
+ ["-125+(10**100)*j", None],
102
+ ["(10**10)*(1+j)", None],
103
+ ["(10**10)*(-1+j)", None],
104
+ ["(10**100)*(1+j)", None],
105
+ ["(10**100)*(-1+j)", None],
106
+ ["(1.5-1j)", None],
107
+ ["(6+4j)", None],
108
+ ["(4+1j)", None],
109
+ ["(3.5+2j)", None],
110
+ ["(1.5-1j)", None],
111
+ ["(-6-4j)", None],
112
+ ["(-2-3j)", None],
113
+ ["(-2.5-2j)", None],
114
+ ["(4+1j)", None],
115
+ ["(3+3j)", None],
116
+ ["(2-2j)", None],
117
+ ["1", "0"],
118
+ ["2", "0"],
119
+ ["3", "log(2)"],
120
+ ["4", "log(6)"],
121
+ ["5", "log(24)"],
122
+ ["0.5", "log(pi)/2"],
123
+ ["1.5", "log(sqrt(pi)/2)"],
124
+ ["2.5", "log(3*sqrt(pi)/4)"],
125
+ ["mpf('0.37')", None],
126
+ ["0.25", "log(sqrt(2*sqrt(2*pi**3)/agm(1,sqrt(2))))"],
127
+ ["-0.4", None],
128
+ ["mpf('-1.9')", None],
129
+ ["mpf('12.8')", None],
130
+ ["mpf('33.7')", None],
131
+ ["mpf('95.2')", None],
132
+ ["mpf('160.3')", None],
133
+ ["mpf('2057.8')", None],
134
+ ["25", "log(ifac(24))"],
135
+ ["80", "log(ifac(79))"],
136
+ ["500", "log(ifac(500-1))"],
137
+ ["8000", "log(ifac(8000-1))"],
138
+ ["8000.5", None],
139
+ ["mpf('8000.1')", None],
140
+ ["mpf('1.37e10')", None],
141
+ ["mpf('1.37e10')*(1+j)", None],
142
+ ["mpf('1.37e10')*(-1+j)", None],
143
+ ["mpf('1.37e10')*(-1-j)", None],
144
+ ["mpf('1.37e10')*(-1+j)", None],
145
+ ["mpf('1.37e100')", None],
146
+ ["mpf('1.37e100')*(1+j)", None],
147
+ ["mpf('1.37e100')*(-1+j)", None],
148
+ ["mpf('1.37e100')*(-1-j)", None],
149
+ ["mpf('1.37e100')*(-1+j)", None],
150
+ ["3+4j",
151
+ "mpc('"
152
+ "-1.7566267846037841105306041816232757851567066070613445016197619371316057169"
153
+ "4723618263960834804618463052988607348289672535780644470689771115236512106002"
154
+ "5970873471563240537307638968509556191696167970488390423963867031934333890838"
155
+ "8009531786948197210025029725361069435208930363494971027388382086721660805397"
156
+ "9163230643216054580167976201709951509519218635460317367338612500626714783631"
157
+ "7498317478048447525674016344322545858832610325861086336204591943822302971823"
158
+ "5161814175530618223688296232894588415495615809337292518431903058265147109853"
159
+ "1710568942184987827643886816200452860853873815413367529829631430146227470517"
160
+ "6579967222200868632179482214312673161276976117132204633283806161971389519137"
161
+ "1243359764435612951384238091232760634271570950240717650166551484551654327989"
162
+ "9360285030081716934130446150245110557038117075172576825490035434069388648124"
163
+ "6678152254554001586736120762641422590778766100376515737713938521275749049949"
164
+ "1284143906816424244705094759339932733567910991920631339597278805393743140853"
165
+ "391550313363278558195609260225928','"
166
+ "4.74266443803465792819488940755002274088830335171164611359052405215840070271"
167
+ "5906813009373171139767051863542508136875688550817670379002790304870822775498"
168
+ "2809996675877564504192565392367259119610438951593128982646945990372179860613"
169
+ "4294436498090428077839141927485901735557543641049637962003652638924845391650"
170
+ "9546290137755550107224907606529385248390667634297183361902055842228798984200"
171
+ "9591180450211798341715874477629099687609819466457990642030707080894518168924"
172
+ "6805549314043258530272479246115112769957368212585759640878745385160943755234"
173
+ "9398036774908108204370323896757543121853650025529763655312360354244898913463"
174
+ "7115955702828838923393113618205074162812089732064414530813087483533203244056"
175
+ "0546577484241423134079056537777170351934430586103623577814746004431994179990"
176
+ "5318522939077992613855205801498201930221975721246498720895122345420698451980"
177
+ "0051215797310305885845964334761831751370672996984756815410977750799748813563"
178
+ "8784405288158432214886648743541773208808731479748217023665577802702269468013"
179
+ "673719173759245720489020315779001')"],
180
+ ]
181
+
182
+ for z in [4, 14, 34, 64]:
183
+ testcases.append(["(2+j)*%s/3" % z, None])
184
+ testcases.append(["(-2+j)*%s/3" % z, None])
185
+ testcases.append(["(1+2*j)*%s/3" % z, None])
186
+ testcases.append(["(2-j)*%s/3" % z, None])
187
+ testcases.append(["(20+j)*%s/3" % z, None])
188
+ testcases.append(["(-20+j)*%s/3" % z, None])
189
+ testcases.append(["(1+20*j)*%s/3" % z, None])
190
+ testcases.append(["(20-j)*%s/3" % z, None])
191
+ testcases.append(["(200+j)*%s/3" % z, None])
192
+ testcases.append(["(-200+j)*%s/3" % z, None])
193
+ testcases.append(["(1+200*j)*%s/3" % z, None])
194
+ testcases.append(["(200-j)*%s/3" % z, None])
195
+
196
+ # Poles
197
+ for n in [0,1,2,3,4,25,-1,-2,-3,-4,-20,-21,-50,-51,-200,-201,-20000,-20001]:
198
+ for t in ['1e-5', '1e-20', '1e-100', '1e-10000']:
199
+ testcases.append(["fadd(%s,'%s',exact=True)" % (n, t), None])
200
+ testcases.append(["fsub(%s,'%s',exact=True)" % (n, t), None])
201
+ testcases.append(["fadd(%s,'%sj',exact=True)" % (n, t), None])
202
+ testcases.append(["fsub(%s,'%sj',exact=True)" % (n, t), None])
203
+
204
+ if __name__ == "__main__":
205
+ from timeit import default_timer as clock
206
+ tot_time = 0.0
207
+ for case in testcases:
208
+ t1 = clock()
209
+ testcase(case)
210
+ t2 = clock()
211
+ print("Test time:", t2-t1)
212
+ print()
213
+ tot_time += (t2-t1)
214
+ print("Total time:", tot_time)
215
+ print("Errors:", errcount)
llmeval-env/lib/python3.10/site-packages/mpmath/tests/extratest_zeta.py ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from mpmath import zetazero
2
+ from timeit import default_timer as clock
3
+
4
+ def test_zetazero():
5
+ cases = [\
6
+ (399999999, 156762524.6750591511),
7
+ (241389216, 97490234.2276711795),
8
+ (526196239, 202950727.691229534),
9
+ (542964976, 209039046.578535272),
10
+ (1048449112, 388858885.231056486),
11
+ (1048449113, 388858885.384337406),
12
+ (1048449114, 388858886.002285122),
13
+ (1048449115, 388858886.00239369),
14
+ (1048449116, 388858886.690745053)
15
+ ]
16
+ for n, v in cases:
17
+ print(n, v)
18
+ t1 = clock()
19
+ ok = zetazero(n).ae(complex(0.5,v))
20
+ t2 = clock()
21
+ print("ok =", ok, ("(time = %s)" % round(t2-t1,3)))
22
+ print("Now computing two huge zeros (this may take hours)")
23
+ print("Computing zetazero(8637740722917)")
24
+ ok = zetazero(8637740722917).ae(complex(0.5,2124447368584.39296466152))
25
+ print("ok =", ok)
26
+ ok = zetazero(8637740722918).ae(complex(0.5,2124447368584.39298170604))
27
+ print("ok =", ok)
28
+
29
+ if __name__ == "__main__":
30
+ test_zetazero()
llmeval-env/lib/python3.10/site-packages/mpmath/tests/runtests.py ADDED
@@ -0,0 +1,161 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+
3
+ """
4
+ python runtests.py -py
5
+ Use py.test to run tests (more useful for debugging)
6
+
7
+ python runtests.py -coverage
8
+ Generate test coverage report. Statistics are written to /tmp
9
+
10
+ python runtests.py -profile
11
+ Generate profile stats (this is much slower)
12
+
13
+ python runtests.py -nogmpy
14
+ Run tests without using GMPY even if it exists
15
+
16
+ python runtests.py -strict
17
+ Enforce extra tests in normalize()
18
+
19
+ python runtests.py -local
20
+ Insert '../..' at the beginning of sys.path to use local mpmath
21
+
22
+ python runtests.py -skip ...
23
+ Skip tests from the listed modules
24
+
25
+ Additional arguments are used to filter the tests to run. Only files that have
26
+ one of the arguments in their name are executed.
27
+
28
+ """
29
+
30
+ import sys, os, traceback
31
+
32
+ profile = False
33
+ if "-profile" in sys.argv:
34
+ sys.argv.remove('-profile')
35
+ profile = True
36
+
37
+ coverage = False
38
+ if "-coverage" in sys.argv:
39
+ sys.argv.remove('-coverage')
40
+ coverage = True
41
+
42
+ if "-nogmpy" in sys.argv:
43
+ sys.argv.remove('-nogmpy')
44
+ os.environ['MPMATH_NOGMPY'] = 'Y'
45
+
46
+ if "-strict" in sys.argv:
47
+ sys.argv.remove('-strict')
48
+ os.environ['MPMATH_STRICT'] = 'Y'
49
+
50
+ if "-local" in sys.argv:
51
+ sys.argv.remove('-local')
52
+ importdir = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]),
53
+ '../..'))
54
+ else:
55
+ importdir = ''
56
+
57
+ # TODO: add a flag for this
58
+ testdir = ''
59
+
60
+ def testit(importdir='', testdir='', exit_on_fail=False):
61
+ """Run all tests in testdir while importing from importdir."""
62
+ if importdir:
63
+ sys.path.insert(1, importdir)
64
+ if testdir:
65
+ sys.path.insert(1, testdir)
66
+ import os.path
67
+ import mpmath
68
+ print("mpmath imported from %s" % os.path.dirname(mpmath.__file__))
69
+ print("mpmath backend: %s" % mpmath.libmp.backend.BACKEND)
70
+ print("mpmath mp class: %s" % repr(mpmath.mp))
71
+ print("mpmath version: %s" % mpmath.__version__)
72
+ print("Python version: %s" % sys.version)
73
+ print("")
74
+ if "-py" in sys.argv:
75
+ sys.argv.remove('-py')
76
+ import py
77
+ py.test.cmdline.main()
78
+ else:
79
+ import glob
80
+ from timeit import default_timer as clock
81
+ modules = []
82
+ args = sys.argv[1:]
83
+ excluded = []
84
+ if '-skip' in args:
85
+ excluded = args[args.index('-skip')+1:]
86
+ args = args[:args.index('-skip')]
87
+ # search for tests in directory of this file if not otherwise specified
88
+ if not testdir:
89
+ pattern = os.path.dirname(sys.argv[0])
90
+ else:
91
+ pattern = testdir
92
+ if pattern:
93
+ pattern += '/'
94
+ pattern += 'test*.py'
95
+ # look for tests (respecting specified filter)
96
+ for f in glob.glob(pattern):
97
+ name = os.path.splitext(os.path.basename(f))[0]
98
+ # If run as a script, only run tests given as args, if any are given
99
+ if args and __name__ == "__main__":
100
+ ok = False
101
+ for arg in args:
102
+ if arg in name:
103
+ ok = True
104
+ break
105
+ if not ok:
106
+ continue
107
+ elif name in excluded:
108
+ continue
109
+ module = __import__(name)
110
+ priority = module.__dict__.get('priority', 100)
111
+ if priority == 666:
112
+ modules = [[priority, name, module]]
113
+ break
114
+ modules.append([priority, name, module])
115
+ # execute tests
116
+ modules.sort()
117
+ tstart = clock()
118
+ for priority, name, module in modules:
119
+ print(name)
120
+ for f in sorted(module.__dict__.keys()):
121
+ if f.startswith('test_'):
122
+ if coverage and ('numpy' in f):
123
+ continue
124
+ sys.stdout.write(" " + f[5:].ljust(25) + " ")
125
+ t1 = clock()
126
+ try:
127
+ module.__dict__[f]()
128
+ except:
129
+ etype, evalue, trb = sys.exc_info()
130
+ if etype in (KeyboardInterrupt, SystemExit):
131
+ raise
132
+ print("")
133
+ print("TEST FAILED!")
134
+ print("")
135
+ traceback.print_exc()
136
+ if exit_on_fail:
137
+ return
138
+ t2 = clock()
139
+ print("ok " + " " + ("%.7f" % (t2-t1)) + " s")
140
+ tend = clock()
141
+ print("")
142
+ print("finished tests in " + ("%.2f" % (tend-tstart)) + " seconds")
143
+ # clean sys.path
144
+ if importdir:
145
+ sys.path.remove(importdir)
146
+ if testdir:
147
+ sys.path.remove(testdir)
148
+
149
+ if __name__ == '__main__':
150
+ if profile:
151
+ import cProfile
152
+ cProfile.run("testit('%s', '%s')" % (importdir, testdir), sort=1)
153
+ elif coverage:
154
+ import trace
155
+ tracer = trace.Trace(ignoredirs=[sys.prefix, sys.exec_prefix],
156
+ trace=0, count=1)
157
+ tracer.run('testit(importdir, testdir)')
158
+ r = tracer.results()
159
+ r.write_results(show_missing=True, summary=True, coverdir="/tmp")
160
+ else:
161
+ testit(importdir, testdir)
llmeval-env/lib/python3.10/site-packages/mpmath/tests/test_basic_ops.py ADDED
@@ -0,0 +1,451 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import mpmath
2
+ from mpmath import *
3
+ from mpmath.libmp import *
4
+ import random
5
+ import sys
6
+
7
+ try:
8
+ long = long
9
+ except NameError:
10
+ long = int
11
+
12
+ def test_type_compare():
13
+ assert mpf(2) == mpc(2,0)
14
+ assert mpf(0) == mpc(0)
15
+ assert mpf(2) != mpc(2, 0.00001)
16
+ assert mpf(2) == 2.0
17
+ assert mpf(2) != 3.0
18
+ assert mpf(2) == 2
19
+ assert mpf(2) != '2.0'
20
+ assert mpc(2) != '2.0'
21
+
22
+ def test_add():
23
+ assert mpf(2.5) + mpf(3) == 5.5
24
+ assert mpf(2.5) + 3 == 5.5
25
+ assert mpf(2.5) + 3.0 == 5.5
26
+ assert 3 + mpf(2.5) == 5.5
27
+ assert 3.0 + mpf(2.5) == 5.5
28
+ assert (3+0j) + mpf(2.5) == 5.5
29
+ assert mpc(2.5) + mpf(3) == 5.5
30
+ assert mpc(2.5) + 3 == 5.5
31
+ assert mpc(2.5) + 3.0 == 5.5
32
+ assert mpc(2.5) + (3+0j) == 5.5
33
+ assert 3 + mpc(2.5) == 5.5
34
+ assert 3.0 + mpc(2.5) == 5.5
35
+ assert (3+0j) + mpc(2.5) == 5.5
36
+
37
+ def test_sub():
38
+ assert mpf(2.5) - mpf(3) == -0.5
39
+ assert mpf(2.5) - 3 == -0.5
40
+ assert mpf(2.5) - 3.0 == -0.5
41
+ assert 3 - mpf(2.5) == 0.5
42
+ assert 3.0 - mpf(2.5) == 0.5
43
+ assert (3+0j) - mpf(2.5) == 0.5
44
+ assert mpc(2.5) - mpf(3) == -0.5
45
+ assert mpc(2.5) - 3 == -0.5
46
+ assert mpc(2.5) - 3.0 == -0.5
47
+ assert mpc(2.5) - (3+0j) == -0.5
48
+ assert 3 - mpc(2.5) == 0.5
49
+ assert 3.0 - mpc(2.5) == 0.5
50
+ assert (3+0j) - mpc(2.5) == 0.5
51
+
52
+ def test_mul():
53
+ assert mpf(2.5) * mpf(3) == 7.5
54
+ assert mpf(2.5) * 3 == 7.5
55
+ assert mpf(2.5) * 3.0 == 7.5
56
+ assert 3 * mpf(2.5) == 7.5
57
+ assert 3.0 * mpf(2.5) == 7.5
58
+ assert (3+0j) * mpf(2.5) == 7.5
59
+ assert mpc(2.5) * mpf(3) == 7.5
60
+ assert mpc(2.5) * 3 == 7.5
61
+ assert mpc(2.5) * 3.0 == 7.5
62
+ assert mpc(2.5) * (3+0j) == 7.5
63
+ assert 3 * mpc(2.5) == 7.5
64
+ assert 3.0 * mpc(2.5) == 7.5
65
+ assert (3+0j) * mpc(2.5) == 7.5
66
+
67
+ def test_div():
68
+ assert mpf(6) / mpf(3) == 2.0
69
+ assert mpf(6) / 3 == 2.0
70
+ assert mpf(6) / 3.0 == 2.0
71
+ assert 6 / mpf(3) == 2.0
72
+ assert 6.0 / mpf(3) == 2.0
73
+ assert (6+0j) / mpf(3.0) == 2.0
74
+ assert mpc(6) / mpf(3) == 2.0
75
+ assert mpc(6) / 3 == 2.0
76
+ assert mpc(6) / 3.0 == 2.0
77
+ assert mpc(6) / (3+0j) == 2.0
78
+ assert 6 / mpc(3) == 2.0
79
+ assert 6.0 / mpc(3) == 2.0
80
+ assert (6+0j) / mpc(3) == 2.0
81
+
82
+ def test_pow():
83
+ assert mpf(6) ** mpf(3) == 216.0
84
+ assert mpf(6) ** 3 == 216.0
85
+ assert mpf(6) ** 3.0 == 216.0
86
+ assert 6 ** mpf(3) == 216.0
87
+ assert 6.0 ** mpf(3) == 216.0
88
+ assert (6+0j) ** mpf(3.0) == 216.0
89
+ assert mpc(6) ** mpf(3) == 216.0
90
+ assert mpc(6) ** 3 == 216.0
91
+ assert mpc(6) ** 3.0 == 216.0
92
+ assert mpc(6) ** (3+0j) == 216.0
93
+ assert 6 ** mpc(3) == 216.0
94
+ assert 6.0 ** mpc(3) == 216.0
95
+ assert (6+0j) ** mpc(3) == 216.0
96
+
97
+ def test_mixed_misc():
98
+ assert 1 + mpf(3) == mpf(3) + 1 == 4
99
+ assert 1 - mpf(3) == -(mpf(3) - 1) == -2
100
+ assert 3 * mpf(2) == mpf(2) * 3 == 6
101
+ assert 6 / mpf(2) == mpf(6) / 2 == 3
102
+ assert 1.0 + mpf(3) == mpf(3) + 1.0 == 4
103
+ assert 1.0 - mpf(3) == -(mpf(3) - 1.0) == -2
104
+ assert 3.0 * mpf(2) == mpf(2) * 3.0 == 6
105
+ assert 6.0 / mpf(2) == mpf(6) / 2.0 == 3
106
+
107
+ def test_add_misc():
108
+ mp.dps = 15
109
+ assert mpf(4) + mpf(-70) == -66
110
+ assert mpf(1) + mpf(1.1)/80 == 1 + 1.1/80
111
+ assert mpf((1, 10000000000)) + mpf(3) == mpf((1, 10000000000))
112
+ assert mpf(3) + mpf((1, 10000000000)) == mpf((1, 10000000000))
113
+ assert mpf((1, -10000000000)) + mpf(3) == mpf(3)
114
+ assert mpf(3) + mpf((1, -10000000000)) == mpf(3)
115
+ assert mpf(1) + 1e-15 != 1
116
+ assert mpf(1) + 1e-20 == 1
117
+ assert mpf(1.07e-22) + 0 == mpf(1.07e-22)
118
+ assert mpf(0) + mpf(1.07e-22) == mpf(1.07e-22)
119
+
120
+ def test_complex_misc():
121
+ # many more tests needed
122
+ assert 1 + mpc(2) == 3
123
+ assert not mpc(2).ae(2 + 1e-13)
124
+ assert mpc(2+1e-15j).ae(2)
125
+
126
+ def test_complex_zeros():
127
+ for a in [0,2]:
128
+ for b in [0,3]:
129
+ for c in [0,4]:
130
+ for d in [0,5]:
131
+ assert mpc(a,b)*mpc(c,d) == complex(a,b)*complex(c,d)
132
+
133
+ def test_hash():
134
+ for i in range(-256, 256):
135
+ assert hash(mpf(i)) == hash(i)
136
+ assert hash(mpf(0.5)) == hash(0.5)
137
+ assert hash(mpc(2,3)) == hash(2+3j)
138
+ # Check that this doesn't fail
139
+ assert hash(inf)
140
+ # Check that overflow doesn't assign equal hashes to large numbers
141
+ assert hash(mpf('1e1000')) != hash('1e10000')
142
+ assert hash(mpc(100,'1e1000')) != hash(mpc(200,'1e1000'))
143
+ from mpmath.rational import mpq
144
+ assert hash(mp.mpq(1,3))
145
+ assert hash(mp.mpq(0,1)) == 0
146
+ assert hash(mp.mpq(-1,1)) == hash(-1)
147
+ assert hash(mp.mpq(1,1)) == hash(1)
148
+ assert hash(mp.mpq(5,1)) == hash(5)
149
+ assert hash(mp.mpq(1,2)) == hash(0.5)
150
+ if sys.version_info >= (3, 2):
151
+ assert hash(mpf(1)*2**2000) == hash(2**2000)
152
+ assert hash(mpf(1)/2**2000) == hash(mpq(1,2**2000))
153
+
154
+ # Advanced rounding test
155
+ def test_add_rounding():
156
+ mp.dps = 15
157
+ a = from_float(1e-50)
158
+ assert mpf_sub(mpf_add(fone, a, 53, round_up), fone, 53, round_up) == from_float(2.2204460492503131e-16)
159
+ assert mpf_sub(fone, a, 53, round_up) == fone
160
+ assert mpf_sub(fone, mpf_sub(fone, a, 53, round_down), 53, round_down) == from_float(1.1102230246251565e-16)
161
+ assert mpf_add(fone, a, 53, round_down) == fone
162
+
163
+ def test_almost_equal():
164
+ assert mpf(1.2).ae(mpf(1.20000001), 1e-7)
165
+ assert not mpf(1.2).ae(mpf(1.20000001), 1e-9)
166
+ assert not mpf(-0.7818314824680298).ae(mpf(-0.774695868667929))
167
+
168
+ def test_arithmetic_functions():
169
+ import operator
170
+ ops = [(operator.add, fadd), (operator.sub, fsub), (operator.mul, fmul),
171
+ (operator.truediv, fdiv)]
172
+ a = mpf(0.27)
173
+ b = mpf(1.13)
174
+ c = mpc(0.51+2.16j)
175
+ d = mpc(1.08-0.99j)
176
+ for x in [a,b,c,d]:
177
+ for y in [a,b,c,d]:
178
+ for op, fop in ops:
179
+ if fop is not fdiv:
180
+ mp.prec = 200
181
+ z0 = op(x,y)
182
+ mp.prec = 60
183
+ z1 = op(x,y)
184
+ mp.prec = 53
185
+ z2 = op(x,y)
186
+ assert fop(x, y, prec=60) == z1
187
+ assert fop(x, y) == z2
188
+ if fop is not fdiv:
189
+ assert fop(x, y, prec=inf) == z0
190
+ assert fop(x, y, dps=inf) == z0
191
+ assert fop(x, y, exact=True) == z0
192
+ assert fneg(fneg(z1, exact=True), prec=inf) == z1
193
+ assert fneg(z1) == -(+z1)
194
+ mp.dps = 15
195
+
196
+ def test_exact_integer_arithmetic():
197
+ # XXX: re-fix this so that all operations are tested with all rounding modes
198
+ random.seed(0)
199
+ for prec in [6, 10, 25, 40, 100, 250, 725]:
200
+ for rounding in ['d', 'u', 'f', 'c', 'n']:
201
+ mp.dps = prec
202
+ M = 10**(prec-2)
203
+ M2 = 10**(prec//2-2)
204
+ for i in range(10):
205
+ a = random.randint(-M, M)
206
+ b = random.randint(-M, M)
207
+ assert mpf(a, rounding=rounding) == a
208
+ assert int(mpf(a, rounding=rounding)) == a
209
+ assert int(mpf(str(a), rounding=rounding)) == a
210
+ assert mpf(a) + mpf(b) == a + b
211
+ assert mpf(a) - mpf(b) == a - b
212
+ assert -mpf(a) == -a
213
+ a = random.randint(-M2, M2)
214
+ b = random.randint(-M2, M2)
215
+ assert mpf(a) * mpf(b) == a*b
216
+ assert mpf_mul(from_int(a), from_int(b), mp.prec, rounding) == from_int(a*b)
217
+ mp.dps = 15
218
+
219
+ def test_odd_int_bug():
220
+ assert to_int(from_int(3), round_nearest) == 3
221
+
222
+ def test_str_1000_digits():
223
+ mp.dps = 1001
224
+ # last digit may be wrong
225
+ assert str(mpf(2)**0.5)[-10:-1] == '9518488472'[:9]
226
+ assert str(pi)[-10:-1] == '2164201989'[:9]
227
+ mp.dps = 15
228
+
229
+ def test_str_10000_digits():
230
+ mp.dps = 10001
231
+ # last digit may be wrong
232
+ assert str(mpf(2)**0.5)[-10:-1] == '5873258351'[:9]
233
+ assert str(pi)[-10:-1] == '5256375678'[:9]
234
+ mp.dps = 15
235
+
236
+ def test_monitor():
237
+ f = lambda x: x**2
238
+ a = []
239
+ b = []
240
+ g = monitor(f, a.append, b.append)
241
+ assert g(3) == 9
242
+ assert g(4) == 16
243
+ assert a[0] == ((3,), {})
244
+ assert b[0] == 9
245
+
246
+ def test_nint_distance():
247
+ assert nint_distance(mpf(-3)) == (-3, -inf)
248
+ assert nint_distance(mpc(-3)) == (-3, -inf)
249
+ assert nint_distance(mpf(-3.1)) == (-3, -3)
250
+ assert nint_distance(mpf(-3.01)) == (-3, -6)
251
+ assert nint_distance(mpf(-3.001)) == (-3, -9)
252
+ assert nint_distance(mpf(-3.0001)) == (-3, -13)
253
+ assert nint_distance(mpf(-2.9)) == (-3, -3)
254
+ assert nint_distance(mpf(-2.99)) == (-3, -6)
255
+ assert nint_distance(mpf(-2.999)) == (-3, -9)
256
+ assert nint_distance(mpf(-2.9999)) == (-3, -13)
257
+ assert nint_distance(mpc(-3+0.1j)) == (-3, -3)
258
+ assert nint_distance(mpc(-3+0.01j)) == (-3, -6)
259
+ assert nint_distance(mpc(-3.1+0.1j)) == (-3, -3)
260
+ assert nint_distance(mpc(-3.01+0.01j)) == (-3, -6)
261
+ assert nint_distance(mpc(-3.001+0.001j)) == (-3, -9)
262
+ assert nint_distance(mpf(0)) == (0, -inf)
263
+ assert nint_distance(mpf(0.01)) == (0, -6)
264
+ assert nint_distance(mpf('1e-100')) == (0, -332)
265
+
266
+ def test_floor_ceil_nint_frac():
267
+ mp.dps = 15
268
+ for n in range(-10,10):
269
+ assert floor(n) == n
270
+ assert floor(n+0.5) == n
271
+ assert ceil(n) == n
272
+ assert ceil(n+0.5) == n+1
273
+ assert nint(n) == n
274
+ # nint rounds to even
275
+ if n % 2 == 1:
276
+ assert nint(n+0.5) == n+1
277
+ else:
278
+ assert nint(n+0.5) == n
279
+ assert floor(inf) == inf
280
+ assert floor(ninf) == ninf
281
+ assert isnan(floor(nan))
282
+ assert ceil(inf) == inf
283
+ assert ceil(ninf) == ninf
284
+ assert isnan(ceil(nan))
285
+ assert nint(inf) == inf
286
+ assert nint(ninf) == ninf
287
+ assert isnan(nint(nan))
288
+ assert floor(0.1) == 0
289
+ assert floor(0.9) == 0
290
+ assert floor(-0.1) == -1
291
+ assert floor(-0.9) == -1
292
+ assert floor(10000000000.1) == 10000000000
293
+ assert floor(10000000000.9) == 10000000000
294
+ assert floor(-10000000000.1) == -10000000000-1
295
+ assert floor(-10000000000.9) == -10000000000-1
296
+ assert floor(1e-100) == 0
297
+ assert floor(-1e-100) == -1
298
+ assert floor(1e100) == 1e100
299
+ assert floor(-1e100) == -1e100
300
+ assert ceil(0.1) == 1
301
+ assert ceil(0.9) == 1
302
+ assert ceil(-0.1) == 0
303
+ assert ceil(-0.9) == 0
304
+ assert ceil(10000000000.1) == 10000000000+1
305
+ assert ceil(10000000000.9) == 10000000000+1
306
+ assert ceil(-10000000000.1) == -10000000000
307
+ assert ceil(-10000000000.9) == -10000000000
308
+ assert ceil(1e-100) == 1
309
+ assert ceil(-1e-100) == 0
310
+ assert ceil(1e100) == 1e100
311
+ assert ceil(-1e100) == -1e100
312
+ assert nint(0.1) == 0
313
+ assert nint(0.9) == 1
314
+ assert nint(-0.1) == 0
315
+ assert nint(-0.9) == -1
316
+ assert nint(10000000000.1) == 10000000000
317
+ assert nint(10000000000.9) == 10000000000+1
318
+ assert nint(-10000000000.1) == -10000000000
319
+ assert nint(-10000000000.9) == -10000000000-1
320
+ assert nint(1e-100) == 0
321
+ assert nint(-1e-100) == 0
322
+ assert nint(1e100) == 1e100
323
+ assert nint(-1e100) == -1e100
324
+ assert floor(3.2+4.6j) == 3+4j
325
+ assert ceil(3.2+4.6j) == 4+5j
326
+ assert nint(3.2+4.6j) == 3+5j
327
+ for n in range(-10,10):
328
+ assert frac(n) == 0
329
+ assert frac(0.25) == 0.25
330
+ assert frac(1.25) == 0.25
331
+ assert frac(2.25) == 0.25
332
+ assert frac(-0.25) == 0.75
333
+ assert frac(-1.25) == 0.75
334
+ assert frac(-2.25) == 0.75
335
+ assert frac('1e100000000000000') == 0
336
+ u = mpf('1e-100000000000000')
337
+ assert frac(u) == u
338
+ assert frac(-u) == 1 # rounding!
339
+ u = mpf('1e-400')
340
+ assert frac(-u, prec=0) == fsub(1, u, exact=True)
341
+ assert frac(3.25+4.75j) == 0.25+0.75j
342
+
343
+ def test_isnan_etc():
344
+ from mpmath.rational import mpq
345
+ assert isnan(nan) == True
346
+ assert isnan(3) == False
347
+ assert isnan(mpf(3)) == False
348
+ assert isnan(inf) == False
349
+ assert isnan(mpc(2,nan)) == True
350
+ assert isnan(mpc(2,nan)) == True
351
+ assert isnan(mpc(nan,nan)) == True
352
+ assert isnan(mpc(2,2)) == False
353
+ assert isnan(mpc(nan,inf)) == True
354
+ assert isnan(mpc(inf,inf)) == False
355
+ assert isnan(mpq((3,2))) == False
356
+ assert isnan(mpq((0,1))) == False
357
+ assert isinf(inf) == True
358
+ assert isinf(-inf) == True
359
+ assert isinf(3) == False
360
+ assert isinf(nan) == False
361
+ assert isinf(3+4j) == False
362
+ assert isinf(mpc(inf)) == True
363
+ assert isinf(mpc(3,inf)) == True
364
+ assert isinf(mpc(inf,3)) == True
365
+ assert isinf(mpc(inf,inf)) == True
366
+ assert isinf(mpc(nan,inf)) == True
367
+ assert isinf(mpc(inf,nan)) == True
368
+ assert isinf(mpc(nan,nan)) == False
369
+ assert isinf(mpq((3,2))) == False
370
+ assert isinf(mpq((0,1))) == False
371
+ assert isnormal(3) == True
372
+ assert isnormal(3.5) == True
373
+ assert isnormal(mpf(3.5)) == True
374
+ assert isnormal(0) == False
375
+ assert isnormal(mpf(0)) == False
376
+ assert isnormal(0.0) == False
377
+ assert isnormal(inf) == False
378
+ assert isnormal(-inf) == False
379
+ assert isnormal(nan) == False
380
+ assert isnormal(float(inf)) == False
381
+ assert isnormal(mpc(0,0)) == False
382
+ assert isnormal(mpc(3,0)) == True
383
+ assert isnormal(mpc(0,3)) == True
384
+ assert isnormal(mpc(3,3)) == True
385
+ assert isnormal(mpc(0,nan)) == False
386
+ assert isnormal(mpc(0,inf)) == False
387
+ assert isnormal(mpc(3,nan)) == False
388
+ assert isnormal(mpc(3,inf)) == False
389
+ assert isnormal(mpc(3,-inf)) == False
390
+ assert isnormal(mpc(nan,0)) == False
391
+ assert isnormal(mpc(inf,0)) == False
392
+ assert isnormal(mpc(nan,3)) == False
393
+ assert isnormal(mpc(inf,3)) == False
394
+ assert isnormal(mpc(inf,nan)) == False
395
+ assert isnormal(mpc(nan,inf)) == False
396
+ assert isnormal(mpc(nan,nan)) == False
397
+ assert isnormal(mpc(inf,inf)) == False
398
+ assert isnormal(mpq((3,2))) == True
399
+ assert isnormal(mpq((0,1))) == False
400
+ assert isint(3) == True
401
+ assert isint(0) == True
402
+ assert isint(long(3)) == True
403
+ assert isint(long(0)) == True
404
+ assert isint(mpf(3)) == True
405
+ assert isint(mpf(0)) == True
406
+ assert isint(mpf(-3)) == True
407
+ assert isint(mpf(3.2)) == False
408
+ assert isint(3.2) == False
409
+ assert isint(nan) == False
410
+ assert isint(inf) == False
411
+ assert isint(-inf) == False
412
+ assert isint(mpc(0)) == True
413
+ assert isint(mpc(3)) == True
414
+ assert isint(mpc(3.2)) == False
415
+ assert isint(mpc(3,inf)) == False
416
+ assert isint(mpc(inf)) == False
417
+ assert isint(mpc(3,2)) == False
418
+ assert isint(mpc(0,2)) == False
419
+ assert isint(mpc(3,2),gaussian=True) == True
420
+ assert isint(mpc(3,0),gaussian=True) == True
421
+ assert isint(mpc(0,3),gaussian=True) == True
422
+ assert isint(3+4j) == False
423
+ assert isint(3+4j, gaussian=True) == True
424
+ assert isint(3+0j) == True
425
+ assert isint(mpq((3,2))) == False
426
+ assert isint(mpq((3,9))) == False
427
+ assert isint(mpq((9,3))) == True
428
+ assert isint(mpq((0,4))) == True
429
+ assert isint(mpq((1,1))) == True
430
+ assert isint(mpq((-1,1))) == True
431
+ assert mp.isnpint(0) == True
432
+ assert mp.isnpint(1) == False
433
+ assert mp.isnpint(-1) == True
434
+ assert mp.isnpint(-1.1) == False
435
+ assert mp.isnpint(-1.0) == True
436
+ assert mp.isnpint(mp.mpq(1,2)) == False
437
+ assert mp.isnpint(mp.mpq(-1,2)) == False
438
+ assert mp.isnpint(mp.mpq(-3,1)) == True
439
+ assert mp.isnpint(mp.mpq(0,1)) == True
440
+ assert mp.isnpint(mp.mpq(1,1)) == False
441
+ assert mp.isnpint(0+0j) == True
442
+ assert mp.isnpint(-1+0j) == True
443
+ assert mp.isnpint(-1.1+0j) == False
444
+ assert mp.isnpint(-1+0.1j) == False
445
+ assert mp.isnpint(0+0.1j) == False
446
+
447
+
448
+ def test_issue_438():
449
+ assert mpf(finf) == mpf('inf')
450
+ assert mpf(fninf) == mpf('-inf')
451
+ assert mpf(fnan)._mpf_ == mpf('nan')._mpf_
llmeval-env/lib/python3.10/site-packages/mpmath/tests/test_bitwise.py ADDED
@@ -0,0 +1,188 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Test bit-level integer and mpf operations
3
+ """
4
+
5
+ from mpmath import *
6
+ from mpmath.libmp import *
7
+
8
+ def test_bitcount():
9
+ assert bitcount(0) == 0
10
+ assert bitcount(1) == 1
11
+ assert bitcount(7) == 3
12
+ assert bitcount(8) == 4
13
+ assert bitcount(2**100) == 101
14
+ assert bitcount(2**100-1) == 100
15
+
16
+ def test_trailing():
17
+ assert trailing(0) == 0
18
+ assert trailing(1) == 0
19
+ assert trailing(2) == 1
20
+ assert trailing(7) == 0
21
+ assert trailing(8) == 3
22
+ assert trailing(2**100) == 100
23
+ assert trailing(2**100-1) == 0
24
+
25
+ def test_round_down():
26
+ assert from_man_exp(0, -4, 4, round_down)[:3] == (0, 0, 0)
27
+ assert from_man_exp(0xf0, -4, 4, round_down)[:3] == (0, 15, 0)
28
+ assert from_man_exp(0xf1, -4, 4, round_down)[:3] == (0, 15, 0)
29
+ assert from_man_exp(0xff, -4, 4, round_down)[:3] == (0, 15, 0)
30
+ assert from_man_exp(-0xf0, -4, 4, round_down)[:3] == (1, 15, 0)
31
+ assert from_man_exp(-0xf1, -4, 4, round_down)[:3] == (1, 15, 0)
32
+ assert from_man_exp(-0xff, -4, 4, round_down)[:3] == (1, 15, 0)
33
+
34
+ def test_round_up():
35
+ assert from_man_exp(0, -4, 4, round_up)[:3] == (0, 0, 0)
36
+ assert from_man_exp(0xf0, -4, 4, round_up)[:3] == (0, 15, 0)
37
+ assert from_man_exp(0xf1, -4, 4, round_up)[:3] == (0, 1, 4)
38
+ assert from_man_exp(0xff, -4, 4, round_up)[:3] == (0, 1, 4)
39
+ assert from_man_exp(-0xf0, -4, 4, round_up)[:3] == (1, 15, 0)
40
+ assert from_man_exp(-0xf1, -4, 4, round_up)[:3] == (1, 1, 4)
41
+ assert from_man_exp(-0xff, -4, 4, round_up)[:3] == (1, 1, 4)
42
+
43
+ def test_round_floor():
44
+ assert from_man_exp(0, -4, 4, round_floor)[:3] == (0, 0, 0)
45
+ assert from_man_exp(0xf0, -4, 4, round_floor)[:3] == (0, 15, 0)
46
+ assert from_man_exp(0xf1, -4, 4, round_floor)[:3] == (0, 15, 0)
47
+ assert from_man_exp(0xff, -4, 4, round_floor)[:3] == (0, 15, 0)
48
+ assert from_man_exp(-0xf0, -4, 4, round_floor)[:3] == (1, 15, 0)
49
+ assert from_man_exp(-0xf1, -4, 4, round_floor)[:3] == (1, 1, 4)
50
+ assert from_man_exp(-0xff, -4, 4, round_floor)[:3] == (1, 1, 4)
51
+
52
+ def test_round_ceiling():
53
+ assert from_man_exp(0, -4, 4, round_ceiling)[:3] == (0, 0, 0)
54
+ assert from_man_exp(0xf0, -4, 4, round_ceiling)[:3] == (0, 15, 0)
55
+ assert from_man_exp(0xf1, -4, 4, round_ceiling)[:3] == (0, 1, 4)
56
+ assert from_man_exp(0xff, -4, 4, round_ceiling)[:3] == (0, 1, 4)
57
+ assert from_man_exp(-0xf0, -4, 4, round_ceiling)[:3] == (1, 15, 0)
58
+ assert from_man_exp(-0xf1, -4, 4, round_ceiling)[:3] == (1, 15, 0)
59
+ assert from_man_exp(-0xff, -4, 4, round_ceiling)[:3] == (1, 15, 0)
60
+
61
+ def test_round_nearest():
62
+ assert from_man_exp(0, -4, 4, round_nearest)[:3] == (0, 0, 0)
63
+ assert from_man_exp(0xf0, -4, 4, round_nearest)[:3] == (0, 15, 0)
64
+ assert from_man_exp(0xf7, -4, 4, round_nearest)[:3] == (0, 15, 0)
65
+ assert from_man_exp(0xf8, -4, 4, round_nearest)[:3] == (0, 1, 4) # 1111.1000 -> 10000.0
66
+ assert from_man_exp(0xf9, -4, 4, round_nearest)[:3] == (0, 1, 4) # 1111.1001 -> 10000.0
67
+ assert from_man_exp(0xe8, -4, 4, round_nearest)[:3] == (0, 7, 1) # 1110.1000 -> 1110.0
68
+ assert from_man_exp(0xe9, -4, 4, round_nearest)[:3] == (0, 15, 0) # 1110.1001 -> 1111.0
69
+ assert from_man_exp(-0xf0, -4, 4, round_nearest)[:3] == (1, 15, 0)
70
+ assert from_man_exp(-0xf7, -4, 4, round_nearest)[:3] == (1, 15, 0)
71
+ assert from_man_exp(-0xf8, -4, 4, round_nearest)[:3] == (1, 1, 4)
72
+ assert from_man_exp(-0xf9, -4, 4, round_nearest)[:3] == (1, 1, 4)
73
+ assert from_man_exp(-0xe8, -4, 4, round_nearest)[:3] == (1, 7, 1)
74
+ assert from_man_exp(-0xe9, -4, 4, round_nearest)[:3] == (1, 15, 0)
75
+
76
+ def test_rounding_bugs():
77
+ # 1 less than power-of-two cases
78
+ assert from_man_exp(72057594037927935, -56, 53, round_up) == (0, 1, 0, 1)
79
+ assert from_man_exp(73786976294838205979, -65, 53, round_nearest) == (0, 1, 1, 1)
80
+ assert from_man_exp(31, 0, 4, round_up) == (0, 1, 5, 1)
81
+ assert from_man_exp(-31, 0, 4, round_floor) == (1, 1, 5, 1)
82
+ assert from_man_exp(255, 0, 7, round_up) == (0, 1, 8, 1)
83
+ assert from_man_exp(-255, 0, 7, round_floor) == (1, 1, 8, 1)
84
+
85
+ def test_rounding_issue_200():
86
+ a = from_man_exp(9867,-100)
87
+ b = from_man_exp(9867,-200)
88
+ c = from_man_exp(-1,0)
89
+ z = (1, 1023, -10, 10)
90
+ assert mpf_add(a, c, 10, 'd') == z
91
+ assert mpf_add(b, c, 10, 'd') == z
92
+ assert mpf_add(c, a, 10, 'd') == z
93
+ assert mpf_add(c, b, 10, 'd') == z
94
+
95
+ def test_perturb():
96
+ a = fone
97
+ b = from_float(0.99999999999999989)
98
+ c = from_float(1.0000000000000002)
99
+ assert mpf_perturb(a, 0, 53, round_nearest) == a
100
+ assert mpf_perturb(a, 1, 53, round_nearest) == a
101
+ assert mpf_perturb(a, 0, 53, round_up) == c
102
+ assert mpf_perturb(a, 0, 53, round_ceiling) == c
103
+ assert mpf_perturb(a, 0, 53, round_down) == a
104
+ assert mpf_perturb(a, 0, 53, round_floor) == a
105
+ assert mpf_perturb(a, 1, 53, round_up) == a
106
+ assert mpf_perturb(a, 1, 53, round_ceiling) == a
107
+ assert mpf_perturb(a, 1, 53, round_down) == b
108
+ assert mpf_perturb(a, 1, 53, round_floor) == b
109
+ a = mpf_neg(a)
110
+ b = mpf_neg(b)
111
+ c = mpf_neg(c)
112
+ assert mpf_perturb(a, 0, 53, round_nearest) == a
113
+ assert mpf_perturb(a, 1, 53, round_nearest) == a
114
+ assert mpf_perturb(a, 0, 53, round_up) == a
115
+ assert mpf_perturb(a, 0, 53, round_floor) == a
116
+ assert mpf_perturb(a, 0, 53, round_down) == b
117
+ assert mpf_perturb(a, 0, 53, round_ceiling) == b
118
+ assert mpf_perturb(a, 1, 53, round_up) == c
119
+ assert mpf_perturb(a, 1, 53, round_floor) == c
120
+ assert mpf_perturb(a, 1, 53, round_down) == a
121
+ assert mpf_perturb(a, 1, 53, round_ceiling) == a
122
+
123
+ def test_add_exact():
124
+ ff = from_float
125
+ assert mpf_add(ff(3.0), ff(2.5)) == ff(5.5)
126
+ assert mpf_add(ff(3.0), ff(-2.5)) == ff(0.5)
127
+ assert mpf_add(ff(-3.0), ff(2.5)) == ff(-0.5)
128
+ assert mpf_add(ff(-3.0), ff(-2.5)) == ff(-5.5)
129
+ assert mpf_sub(mpf_add(fone, ff(1e-100)), fone) == ff(1e-100)
130
+ assert mpf_sub(mpf_add(ff(1e-100), fone), fone) == ff(1e-100)
131
+ assert mpf_sub(mpf_add(fone, ff(-1e-100)), fone) == ff(-1e-100)
132
+ assert mpf_sub(mpf_add(ff(-1e-100), fone), fone) == ff(-1e-100)
133
+ assert mpf_add(fone, fzero) == fone
134
+ assert mpf_add(fzero, fone) == fone
135
+ assert mpf_add(fzero, fzero) == fzero
136
+
137
+ def test_long_exponent_shifts():
138
+ mp.dps = 15
139
+ # Check for possible bugs due to exponent arithmetic overflow
140
+ # in a C implementation
141
+ x = mpf(1)
142
+ for p in [32, 64]:
143
+ a = ldexp(1,2**(p-1))
144
+ b = ldexp(1,2**p)
145
+ c = ldexp(1,2**(p+1))
146
+ d = ldexp(1,-2**(p-1))
147
+ e = ldexp(1,-2**p)
148
+ f = ldexp(1,-2**(p+1))
149
+ assert (x+a) == a
150
+ assert (x+b) == b
151
+ assert (x+c) == c
152
+ assert (x+d) == x
153
+ assert (x+e) == x
154
+ assert (x+f) == x
155
+ assert (a+x) == a
156
+ assert (b+x) == b
157
+ assert (c+x) == c
158
+ assert (d+x) == x
159
+ assert (e+x) == x
160
+ assert (f+x) == x
161
+ assert (x-a) == -a
162
+ assert (x-b) == -b
163
+ assert (x-c) == -c
164
+ assert (x-d) == x
165
+ assert (x-e) == x
166
+ assert (x-f) == x
167
+ assert (a-x) == a
168
+ assert (b-x) == b
169
+ assert (c-x) == c
170
+ assert (d-x) == -x
171
+ assert (e-x) == -x
172
+ assert (f-x) == -x
173
+
174
+ def test_float_rounding():
175
+ mp.prec = 64
176
+ for x in [mpf(1), mpf(1)+eps, mpf(1)-eps, -mpf(1)+eps, -mpf(1)-eps]:
177
+ fa = float(x)
178
+ fb = float(fadd(x,0,prec=53,rounding='n'))
179
+ assert fa == fb
180
+ z = mpc(x,x)
181
+ ca = complex(z)
182
+ cb = complex(fadd(z,0,prec=53,rounding='n'))
183
+ assert ca == cb
184
+ for rnd in ['n', 'd', 'u', 'f', 'c']:
185
+ fa = to_float(x._mpf_, rnd=rnd)
186
+ fb = to_float(fadd(x,0,prec=53,rounding=rnd)._mpf_, rnd=rnd)
187
+ assert fa == fb
188
+ mp.prec = 53
llmeval-env/lib/python3.10/site-packages/mpmath/tests/test_calculus.py ADDED
@@ -0,0 +1,216 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pytest
2
+ from mpmath import *
3
+
4
+ def test_approximation():
5
+ mp.dps = 15
6
+ f = lambda x: cos(2-2*x)/x
7
+ p, err = chebyfit(f, [2, 4], 8, error=True)
8
+ assert err < 1e-5
9
+ for i in range(10):
10
+ x = 2 + i/5.
11
+ assert abs(polyval(p, x) - f(x)) < err
12
+
13
+ def test_limits():
14
+ mp.dps = 15
15
+ assert limit(lambda x: (x-sin(x))/x**3, 0).ae(mpf(1)/6)
16
+ assert limit(lambda n: (1+1/n)**n, inf).ae(e)
17
+
18
+ def test_polyval():
19
+ assert polyval([], 3) == 0
20
+ assert polyval([0], 3) == 0
21
+ assert polyval([5], 3) == 5
22
+ # 4x^3 - 2x + 5
23
+ p = [4, 0, -2, 5]
24
+ assert polyval(p,4) == 253
25
+ assert polyval(p,4,derivative=True) == (253, 190)
26
+
27
+ def test_polyroots():
28
+ p = polyroots([1,-4])
29
+ assert p[0].ae(4)
30
+ p, q = polyroots([1,2,3])
31
+ assert p.ae(-1 - sqrt(2)*j)
32
+ assert q.ae(-1 + sqrt(2)*j)
33
+ #this is not a real test, it only tests a specific case
34
+ assert polyroots([1]) == []
35
+ pytest.raises(ValueError, lambda: polyroots([0]))
36
+
37
+ def test_polyroots_legendre():
38
+ n = 64
39
+ coeffs = [11975573020964041433067793888190275875, 0,
40
+ -190100434726484311252477736051902332000, 0,
41
+ 1437919688271127330313741595496589239248, 0,
42
+ -6897338342113537600691931230430793911840, 0,
43
+ 23556405536185284408974715545252277554280, 0,
44
+ -60969520211303089058522793175947071316960, 0,
45
+ 124284021969194758465450309166353645376880, 0,
46
+ -204721258548015217049921875719981284186016, 0,
47
+ 277415422258095841688223780704620656114900, 0,
48
+ -313237834141273382807123548182995095192800, 0,
49
+ 297432255354328395601259515935229287637200, 0,
50
+ -239057700565161140389797367947941296605600, 0,
51
+ 163356095386193445933028201431093219347160, 0,
52
+ -95158890516229191805647495979277603503200, 0,
53
+ 47310254620162038075933656063247634556400, 0,
54
+ -20071017111583894941305187420771723751200, 0,
55
+ 7255051932731034189479516844750603752850, 0,
56
+ -2228176940331017311443863996901733412640, 0,
57
+ 579006552594977616773047095969088431600, 0,
58
+ -126584428502545713788439446082310831200, 0,
59
+ 23112325428835593809686977515028663000, 0,
60
+ -3491517141958743235617737161547844000, 0,
61
+ 431305058712550634988073414073557200, 0,
62
+ -42927166660756742088912492757452000, 0,
63
+ 3378527005707706553294038781836500, 0,
64
+ -205277590220215081719131470288800, 0,
65
+ 9330799555464321896324157740400, 0,
66
+ -304114948474392713657972548576, 0,
67
+ 6695289961520387531608984680, 0,
68
+ -91048139350447232095702560, 0,
69
+ 659769125727878493447120, 0,
70
+ -1905929106580294155360, 0,
71
+ 916312070471295267]
72
+
73
+ with mp.workdps(3):
74
+ with pytest.raises(mp.NoConvergence):
75
+ polyroots(coeffs, maxsteps=5, cleanup=True, error=False,
76
+ extraprec=n*10)
77
+
78
+ roots = polyroots(coeffs, maxsteps=50, cleanup=True, error=False,
79
+ extraprec=n*10)
80
+ roots = [str(r) for r in roots]
81
+ assert roots == \
82
+ ['-0.999', '-0.996', '-0.991', '-0.983', '-0.973', '-0.961',
83
+ '-0.946', '-0.93', '-0.911', '-0.889', '-0.866', '-0.841',
84
+ '-0.813', '-0.784', '-0.753', '-0.72', '-0.685', '-0.649',
85
+ '-0.611', '-0.572', '-0.531', '-0.489', '-0.446', '-0.402',
86
+ '-0.357', '-0.311', '-0.265', '-0.217', '-0.17', '-0.121',
87
+ '-0.073', '-0.0243', '0.0243', '0.073', '0.121', '0.17', '0.217',
88
+ '0.265', '0.311', '0.357', '0.402', '0.446', '0.489', '0.531',
89
+ '0.572', '0.611', '0.649', '0.685', '0.72', '0.753', '0.784',
90
+ '0.813', '0.841', '0.866', '0.889', '0.911', '0.93', '0.946',
91
+ '0.961', '0.973', '0.983', '0.991', '0.996', '0.999']
92
+
93
+ def test_polyroots_legendre_init():
94
+ extra_prec = 100
95
+ coeffs = [11975573020964041433067793888190275875, 0,
96
+ -190100434726484311252477736051902332000, 0,
97
+ 1437919688271127330313741595496589239248, 0,
98
+ -6897338342113537600691931230430793911840, 0,
99
+ 23556405536185284408974715545252277554280, 0,
100
+ -60969520211303089058522793175947071316960, 0,
101
+ 124284021969194758465450309166353645376880, 0,
102
+ -204721258548015217049921875719981284186016, 0,
103
+ 277415422258095841688223780704620656114900, 0,
104
+ -313237834141273382807123548182995095192800, 0,
105
+ 297432255354328395601259515935229287637200, 0,
106
+ -239057700565161140389797367947941296605600, 0,
107
+ 163356095386193445933028201431093219347160, 0,
108
+ -95158890516229191805647495979277603503200, 0,
109
+ 47310254620162038075933656063247634556400, 0,
110
+ -20071017111583894941305187420771723751200, 0,
111
+ 7255051932731034189479516844750603752850, 0,
112
+ -2228176940331017311443863996901733412640, 0,
113
+ 579006552594977616773047095969088431600, 0,
114
+ -126584428502545713788439446082310831200, 0,
115
+ 23112325428835593809686977515028663000, 0,
116
+ -3491517141958743235617737161547844000, 0,
117
+ 431305058712550634988073414073557200, 0,
118
+ -42927166660756742088912492757452000, 0,
119
+ 3378527005707706553294038781836500, 0,
120
+ -205277590220215081719131470288800, 0,
121
+ 9330799555464321896324157740400, 0,
122
+ -304114948474392713657972548576, 0,
123
+ 6695289961520387531608984680, 0,
124
+ -91048139350447232095702560, 0,
125
+ 659769125727878493447120, 0,
126
+ -1905929106580294155360, 0,
127
+ 916312070471295267]
128
+
129
+ roots_init = matrix(['-0.999', '-0.996', '-0.991', '-0.983', '-0.973',
130
+ '-0.961', '-0.946', '-0.93', '-0.911', '-0.889',
131
+ '-0.866', '-0.841', '-0.813', '-0.784', '-0.753',
132
+ '-0.72', '-0.685', '-0.649', '-0.611', '-0.572',
133
+ '-0.531', '-0.489', '-0.446', '-0.402', '-0.357',
134
+ '-0.311', '-0.265', '-0.217', '-0.17', '-0.121',
135
+ '-0.073', '-0.0243', '0.0243', '0.073', '0.121',
136
+ '0.17', '0.217', '0.265', ' 0.311', '0.357',
137
+ '0.402', '0.446', '0.489', '0.531', '0.572',
138
+ '0.611', '0.649', '0.685', '0.72', '0.753',
139
+ '0.784', '0.813', '0.841', '0.866', '0.889',
140
+ '0.911', '0.93', '0.946', '0.961', '0.973',
141
+ '0.983', '0.991', '0.996', '0.999', '1.0'])
142
+ with mp.workdps(2*mp.dps):
143
+ roots_exact = polyroots(coeffs, maxsteps=50, cleanup=True, error=False,
144
+ extraprec=2*extra_prec)
145
+ with pytest.raises(mp.NoConvergence):
146
+ polyroots(coeffs, maxsteps=5, cleanup=True, error=False,
147
+ extraprec=extra_prec)
148
+ roots,err = polyroots(coeffs, maxsteps=5, cleanup=True, error=True,
149
+ extraprec=extra_prec,roots_init=roots_init)
150
+ assert max(matrix(roots_exact)-matrix(roots).apply(abs)) < err
151
+ roots1,err1 = polyroots(coeffs, maxsteps=25, cleanup=True, error=True,
152
+ extraprec=extra_prec,roots_init=roots_init[:60])
153
+ assert max(matrix(roots_exact)-matrix(roots1).apply(abs)) < err1
154
+
155
+ def test_pade():
156
+ one = mpf(1)
157
+ mp.dps = 20
158
+ N = 10
159
+ a = [one]
160
+ k = 1
161
+ for i in range(1, N+1):
162
+ k *= i
163
+ a.append(one/k)
164
+ p, q = pade(a, N//2, N//2)
165
+ for x in arange(0, 1, 0.1):
166
+ r = polyval(p[::-1], x)/polyval(q[::-1], x)
167
+ assert(r.ae(exp(x), 1.0e-10))
168
+ mp.dps = 15
169
+
170
+ def test_fourier():
171
+ mp.dps = 15
172
+ c, s = fourier(lambda x: x+1, [-1, 2], 2)
173
+ #plot([lambda x: x+1, lambda x: fourierval((c, s), [-1, 2], x)], [-1, 2])
174
+ assert c[0].ae(1.5)
175
+ assert c[1].ae(-3*sqrt(3)/(2*pi))
176
+ assert c[2].ae(3*sqrt(3)/(4*pi))
177
+ assert s[0] == 0
178
+ assert s[1].ae(3/(2*pi))
179
+ assert s[2].ae(3/(4*pi))
180
+ assert fourierval((c, s), [-1, 2], 1).ae(1.9134966715663442)
181
+
182
+ def test_differint():
183
+ mp.dps = 15
184
+ assert differint(lambda t: t, 2, -0.5).ae(8*sqrt(2/pi)/3)
185
+
186
+ def test_invlap():
187
+ mp.dps = 15
188
+ t = 0.01
189
+ fp = lambda p: 1/(p+1)**2
190
+ ft = lambda t: t*exp(-t)
191
+ ftt = ft(t)
192
+ assert invertlaplace(fp,t,method='talbot').ae(ftt)
193
+ assert invertlaplace(fp,t,method='stehfest').ae(ftt)
194
+ assert invertlaplace(fp,t,method='dehoog').ae(ftt)
195
+ assert invertlaplace(fp,t,method='cohen').ae(ftt)
196
+ t = 1.0
197
+ ftt = ft(t)
198
+ assert invertlaplace(fp,t,method='talbot').ae(ftt)
199
+ assert invertlaplace(fp,t,method='stehfest').ae(ftt)
200
+ assert invertlaplace(fp,t,method='dehoog').ae(ftt)
201
+ assert invertlaplace(fp,t,method='cohen').ae(ftt)
202
+
203
+ t = 0.01
204
+ fp = lambda p: log(p)/p
205
+ ft = lambda t: -euler-log(t)
206
+ ftt = ft(t)
207
+ assert invertlaplace(fp,t,method='talbot').ae(ftt)
208
+ assert invertlaplace(fp,t,method='stehfest').ae(ftt)
209
+ assert invertlaplace(fp,t,method='dehoog').ae(ftt)
210
+ assert invertlaplace(fp,t,method='cohen').ae(ftt)
211
+ t = 1.0
212
+ ftt = ft(t)
213
+ assert invertlaplace(fp,t,method='talbot').ae(ftt)
214
+ assert invertlaplace(fp,t,method='stehfest').ae(ftt)
215
+ assert invertlaplace(fp,t,method='dehoog').ae(ftt)
216
+ assert invertlaplace(fp,t,method='cohen').ae(ftt)
llmeval-env/lib/python3.10/site-packages/mpmath/tests/test_compatibility.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from mpmath import *
2
+ from random import seed, randint, random
3
+ import math
4
+
5
+ # Test compatibility with Python floats, which are
6
+ # IEEE doubles (53-bit)
7
+
8
+ N = 5000
9
+ seed(1)
10
+
11
+ # Choosing exponents between roughly -140, 140 ensures that
12
+ # the Python floats don't overflow or underflow
13
+ xs = [(random()-1) * 10**randint(-140, 140) for x in range(N)]
14
+ ys = [(random()-1) * 10**randint(-140, 140) for x in range(N)]
15
+
16
+ # include some equal values
17
+ ys[int(N*0.8):] = xs[int(N*0.8):]
18
+
19
+ # Detect whether Python is compiled to use 80-bit floating-point
20
+ # instructions, in which case the double compatibility test breaks
21
+ uses_x87 = -4.1974624032366689e+117 / -8.4657370748010221e-47 \
22
+ == 4.9581771393902231e+163
23
+
24
+ def test_double_compatibility():
25
+ mp.prec = 53
26
+ for x, y in zip(xs, ys):
27
+ mpx = mpf(x)
28
+ mpy = mpf(y)
29
+ assert mpf(x) == x
30
+ assert (mpx < mpy) == (x < y)
31
+ assert (mpx > mpy) == (x > y)
32
+ assert (mpx == mpy) == (x == y)
33
+ assert (mpx != mpy) == (x != y)
34
+ assert (mpx <= mpy) == (x <= y)
35
+ assert (mpx >= mpy) == (x >= y)
36
+ assert mpx == mpx
37
+ if uses_x87:
38
+ mp.prec = 64
39
+ a = mpx + mpy
40
+ b = mpx * mpy
41
+ c = mpx / mpy
42
+ d = mpx % mpy
43
+ mp.prec = 53
44
+ assert +a == x + y
45
+ assert +b == x * y
46
+ assert +c == x / y
47
+ assert +d == x % y
48
+ else:
49
+ assert mpx + mpy == x + y
50
+ assert mpx * mpy == x * y
51
+ assert mpx / mpy == x / y
52
+ assert mpx % mpy == x % y
53
+ assert abs(mpx) == abs(x)
54
+ assert mpf(repr(x)) == x
55
+ assert ceil(mpx) == math.ceil(x)
56
+ assert floor(mpx) == math.floor(x)
57
+
58
+ def test_sqrt():
59
+ # this fails quite often. it appers to be float
60
+ # that rounds the wrong way, not mpf
61
+ fail = 0
62
+ mp.prec = 53
63
+ for x in xs:
64
+ x = abs(x)
65
+ mp.prec = 100
66
+ mp_high = mpf(x)**0.5
67
+ mp.prec = 53
68
+ mp_low = mpf(x)**0.5
69
+ fp = x**0.5
70
+ assert abs(mp_low-mp_high) <= abs(fp-mp_high)
71
+ fail += mp_low != fp
72
+ assert fail < N/10
73
+
74
+ def test_bugs():
75
+ # particular bugs
76
+ assert mpf(4.4408920985006262E-16) < mpf(1.7763568394002505E-15)
77
+ assert mpf(-4.4408920985006262E-16) > mpf(-1.7763568394002505E-15)
llmeval-env/lib/python3.10/site-packages/mpmath/tests/test_convert.py ADDED
@@ -0,0 +1,233 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import random
2
+ from mpmath import *
3
+ from mpmath.libmp import *
4
+
5
+
6
+ def test_basic_string():
7
+ """
8
+ Test basic string conversion
9
+ """
10
+ mp.dps = 15
11
+ assert mpf('3') == mpf('3.0') == mpf('0003.') == mpf('0.03e2') == mpf(3.0)
12
+ assert mpf('30') == mpf('30.0') == mpf('00030.') == mpf(30.0)
13
+ for i in range(10):
14
+ for j in range(10):
15
+ assert mpf('%ie%i' % (i,j)) == i * 10**j
16
+ assert str(mpf('25000.0')) == '25000.0'
17
+ assert str(mpf('2500.0')) == '2500.0'
18
+ assert str(mpf('250.0')) == '250.0'
19
+ assert str(mpf('25.0')) == '25.0'
20
+ assert str(mpf('2.5')) == '2.5'
21
+ assert str(mpf('0.25')) == '0.25'
22
+ assert str(mpf('0.025')) == '0.025'
23
+ assert str(mpf('0.0025')) == '0.0025'
24
+ assert str(mpf('0.00025')) == '0.00025'
25
+ assert str(mpf('0.000025')) == '2.5e-5'
26
+ assert str(mpf(0)) == '0.0'
27
+ assert str(mpf('2.5e1000000000000000000000')) == '2.5e+1000000000000000000000'
28
+ assert str(mpf('2.6e-1000000000000000000000')) == '2.6e-1000000000000000000000'
29
+ assert str(mpf(1.23402834e-15)) == '1.23402834e-15'
30
+ assert str(mpf(-1.23402834e-15)) == '-1.23402834e-15'
31
+ assert str(mpf(-1.2344e-15)) == '-1.2344e-15'
32
+ assert repr(mpf(-1.2344e-15)) == "mpf('-1.2343999999999999e-15')"
33
+ assert str(mpf("2163048125L")) == '2163048125.0'
34
+ assert str(mpf("-2163048125l")) == '-2163048125.0'
35
+ assert str(mpf("-2163048125L/1088391168")) == '-1.98738118113799'
36
+ assert str(mpf("2163048125/1088391168l")) == '1.98738118113799'
37
+
38
+ def test_pretty():
39
+ mp.pretty = True
40
+ assert repr(mpf(2.5)) == '2.5'
41
+ assert repr(mpc(2.5,3.5)) == '(2.5 + 3.5j)'
42
+ mp.pretty = False
43
+ iv.pretty = True
44
+ assert repr(mpi(2.5,3.5)) == '[2.5, 3.5]'
45
+ iv.pretty = False
46
+
47
+ def test_str_whitespace():
48
+ assert mpf('1.26 ') == 1.26
49
+
50
+ def test_unicode():
51
+ mp.dps = 15
52
+ try:
53
+ unicode = unicode
54
+ except NameError:
55
+ unicode = str
56
+ assert mpf(unicode('2.76')) == 2.76
57
+ assert mpf(unicode('inf')) == inf
58
+
59
+ def test_str_format():
60
+ assert to_str(from_float(0.1),15,strip_zeros=False) == '0.100000000000000'
61
+ assert to_str(from_float(0.0),15,show_zero_exponent=True) == '0.0e+0'
62
+ assert to_str(from_float(0.0),0,show_zero_exponent=True) == '.0e+0'
63
+ assert to_str(from_float(0.0),0,show_zero_exponent=False) == '.0'
64
+ assert to_str(from_float(0.0),1,show_zero_exponent=True) == '0.0e+0'
65
+ assert to_str(from_float(0.0),1,show_zero_exponent=False) == '0.0'
66
+ assert to_str(from_float(1.23),3,show_zero_exponent=True) == '1.23e+0'
67
+ assert to_str(from_float(1.23456789000000e-2),15,strip_zeros=False,min_fixed=0,max_fixed=0) == '1.23456789000000e-2'
68
+ assert to_str(from_float(1.23456789000000e+2),15,strip_zeros=False,min_fixed=0,max_fixed=0) == '1.23456789000000e+2'
69
+ assert to_str(from_float(2.1287e14), 15, max_fixed=1000) == '212870000000000.0'
70
+ assert to_str(from_float(2.1287e15), 15, max_fixed=1000) == '2128700000000000.0'
71
+ assert to_str(from_float(2.1287e16), 15, max_fixed=1000) == '21287000000000000.0'
72
+ assert to_str(from_float(2.1287e30), 15, max_fixed=1000) == '2128700000000000000000000000000.0'
73
+
74
+ def test_tight_string_conversion():
75
+ mp.dps = 15
76
+ # In an old version, '0.5' wasn't recognized as representing
77
+ # an exact binary number and was erroneously rounded up or down
78
+ assert from_str('0.5', 10, round_floor) == fhalf
79
+ assert from_str('0.5', 10, round_ceiling) == fhalf
80
+
81
+ def test_eval_repr_invariant():
82
+ """Test that eval(repr(x)) == x"""
83
+ random.seed(123)
84
+ for dps in [10, 15, 20, 50, 100]:
85
+ mp.dps = dps
86
+ for i in range(1000):
87
+ a = mpf(random.random())**0.5 * 10**random.randint(-100, 100)
88
+ assert eval(repr(a)) == a
89
+ mp.dps = 15
90
+
91
+ def test_str_bugs():
92
+ mp.dps = 15
93
+ # Decimal rounding used to give the wrong exponent in some cases
94
+ assert str(mpf('1e600')) == '1.0e+600'
95
+ assert str(mpf('1e10000')) == '1.0e+10000'
96
+
97
+ def test_str_prec0():
98
+ assert to_str(from_float(1.234), 0) == '.0e+0'
99
+ assert to_str(from_float(1e-15), 0) == '.0e-15'
100
+ assert to_str(from_float(1e+15), 0) == '.0e+15'
101
+ assert to_str(from_float(-1e-15), 0) == '-.0e-15'
102
+ assert to_str(from_float(-1e+15), 0) == '-.0e+15'
103
+
104
+ def test_convert_rational():
105
+ mp.dps = 15
106
+ assert from_rational(30, 5, 53, round_nearest) == (0, 3, 1, 2)
107
+ assert from_rational(-7, 4, 53, round_nearest) == (1, 7, -2, 3)
108
+ assert to_rational((0, 1, -1, 1)) == (1, 2)
109
+
110
+ def test_custom_class():
111
+ class mympf:
112
+ @property
113
+ def _mpf_(self):
114
+ return mpf(3.5)._mpf_
115
+ class mympc:
116
+ @property
117
+ def _mpc_(self):
118
+ return mpf(3.5)._mpf_, mpf(2.5)._mpf_
119
+ assert mpf(2) + mympf() == 5.5
120
+ assert mympf() + mpf(2) == 5.5
121
+ assert mpf(mympf()) == 3.5
122
+ assert mympc() + mpc(2) == mpc(5.5, 2.5)
123
+ assert mpc(2) + mympc() == mpc(5.5, 2.5)
124
+ assert mpc(mympc()) == (3.5+2.5j)
125
+
126
+ def test_conversion_methods():
127
+ class SomethingRandom:
128
+ pass
129
+ class SomethingReal:
130
+ def _mpmath_(self, prec, rounding):
131
+ return mp.make_mpf(from_str('1.3', prec, rounding))
132
+ class SomethingComplex:
133
+ def _mpmath_(self, prec, rounding):
134
+ return mp.make_mpc((from_str('1.3', prec, rounding), \
135
+ from_str('1.7', prec, rounding)))
136
+ x = mpf(3)
137
+ z = mpc(3)
138
+ a = SomethingRandom()
139
+ y = SomethingReal()
140
+ w = SomethingComplex()
141
+ for d in [15, 45]:
142
+ mp.dps = d
143
+ assert (x+y).ae(mpf('4.3'))
144
+ assert (y+x).ae(mpf('4.3'))
145
+ assert (x+w).ae(mpc('4.3', '1.7'))
146
+ assert (w+x).ae(mpc('4.3', '1.7'))
147
+ assert (z+y).ae(mpc('4.3'))
148
+ assert (y+z).ae(mpc('4.3'))
149
+ assert (z+w).ae(mpc('4.3', '1.7'))
150
+ assert (w+z).ae(mpc('4.3', '1.7'))
151
+ x-y; y-x; x-w; w-x; z-y; y-z; z-w; w-z
152
+ x*y; y*x; x*w; w*x; z*y; y*z; z*w; w*z
153
+ x/y; y/x; x/w; w/x; z/y; y/z; z/w; w/z
154
+ x**y; y**x; x**w; w**x; z**y; y**z; z**w; w**z
155
+ x==y; y==x; x==w; w==x; z==y; y==z; z==w; w==z
156
+ mp.dps = 15
157
+ assert x.__add__(a) is NotImplemented
158
+ assert x.__radd__(a) is NotImplemented
159
+ assert x.__lt__(a) is NotImplemented
160
+ assert x.__gt__(a) is NotImplemented
161
+ assert x.__le__(a) is NotImplemented
162
+ assert x.__ge__(a) is NotImplemented
163
+ assert x.__eq__(a) is NotImplemented
164
+ assert x.__ne__(a) is NotImplemented
165
+ # implementation detail
166
+ if hasattr(x, "__cmp__"):
167
+ assert x.__cmp__(a) is NotImplemented
168
+ assert x.__sub__(a) is NotImplemented
169
+ assert x.__rsub__(a) is NotImplemented
170
+ assert x.__mul__(a) is NotImplemented
171
+ assert x.__rmul__(a) is NotImplemented
172
+ assert x.__div__(a) is NotImplemented
173
+ assert x.__rdiv__(a) is NotImplemented
174
+ assert x.__mod__(a) is NotImplemented
175
+ assert x.__rmod__(a) is NotImplemented
176
+ assert x.__pow__(a) is NotImplemented
177
+ assert x.__rpow__(a) is NotImplemented
178
+ assert z.__add__(a) is NotImplemented
179
+ assert z.__radd__(a) is NotImplemented
180
+ assert z.__eq__(a) is NotImplemented
181
+ assert z.__ne__(a) is NotImplemented
182
+ assert z.__sub__(a) is NotImplemented
183
+ assert z.__rsub__(a) is NotImplemented
184
+ assert z.__mul__(a) is NotImplemented
185
+ assert z.__rmul__(a) is NotImplemented
186
+ assert z.__div__(a) is NotImplemented
187
+ assert z.__rdiv__(a) is NotImplemented
188
+ assert z.__pow__(a) is NotImplemented
189
+ assert z.__rpow__(a) is NotImplemented
190
+
191
+ def test_mpmathify():
192
+ assert mpmathify('1/2') == 0.5
193
+ assert mpmathify('(1.0+1.0j)') == mpc(1, 1)
194
+ assert mpmathify('(1.2e-10 - 3.4e5j)') == mpc('1.2e-10', '-3.4e5')
195
+ assert mpmathify('1j') == mpc(1j)
196
+
197
+ def test_issue548():
198
+ try:
199
+ # This expression is invalid, but may trigger the ReDOS vulnerability
200
+ # in the regular expression for parsing complex numbers.
201
+ mpmathify('(' + '1' * 5000 + '!j')
202
+ except:
203
+ return
204
+ # The expression is invalid and should raise an exception.
205
+ assert False
206
+
207
+ def test_compatibility():
208
+ try:
209
+ import numpy as np
210
+ from fractions import Fraction
211
+ from decimal import Decimal
212
+ import decimal
213
+ except ImportError:
214
+ return
215
+ # numpy types
216
+ for nptype in np.core.numerictypes.typeDict.values():
217
+ if issubclass(nptype, np.complexfloating):
218
+ x = nptype(complex(0.5, -0.5))
219
+ elif issubclass(nptype, np.floating):
220
+ x = nptype(0.5)
221
+ elif issubclass(nptype, np.integer):
222
+ x = nptype(2)
223
+ # Handle the weird types
224
+ try: diff = np.abs(type(np.sqrt(x))(sqrt(x)) - np.sqrt(x))
225
+ except: continue
226
+ assert diff < 2.0**-53
227
+ #Fraction and Decimal
228
+ oldprec = mp.prec
229
+ mp.prec = 1000
230
+ decimal.getcontext().prec = mp.dps
231
+ assert sqrt(Fraction(2, 3)).ae(sqrt(mpf('2/3')))
232
+ assert sqrt(Decimal(2)/Decimal(3)).ae(sqrt(mpf('2/3')))
233
+ mp.prec = oldprec
llmeval-env/lib/python3.10/site-packages/mpmath/tests/test_diff.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from mpmath import *
2
+
3
+ def test_diff():
4
+ mp.dps = 15
5
+ assert diff(log, 2.0, n=0).ae(log(2))
6
+ assert diff(cos, 1.0).ae(-sin(1))
7
+ assert diff(abs, 0.0) == 0
8
+ assert diff(abs, 0.0, direction=1) == 1
9
+ assert diff(abs, 0.0, direction=-1) == -1
10
+ assert diff(exp, 1.0).ae(e)
11
+ assert diff(exp, 1.0, n=5).ae(e)
12
+ assert diff(exp, 2.0, n=5, direction=3*j).ae(e**2)
13
+ assert diff(lambda x: x**2, 3.0, method='quad').ae(6)
14
+ assert diff(lambda x: 3+x**5, 3.0, n=2, method='quad').ae(540)
15
+ assert diff(lambda x: 3+x**5, 3.0, n=2, method='step').ae(540)
16
+ assert diffun(sin)(2).ae(cos(2))
17
+ assert diffun(sin, n=2)(2).ae(-sin(2))
18
+
19
+ def test_diffs():
20
+ mp.dps = 15
21
+ assert [chop(d) for d in diffs(sin, 0, 1)] == [0, 1]
22
+ assert [chop(d) for d in diffs(sin, 0, 1, method='quad')] == [0, 1]
23
+ assert [chop(d) for d in diffs(sin, 0, 2)] == [0, 1, 0]
24
+ assert [chop(d) for d in diffs(sin, 0, 2, method='quad')] == [0, 1, 0]
25
+
26
+ def test_taylor():
27
+ mp.dps = 15
28
+ # Easy to test since the coefficients are exact in floating-point
29
+ assert taylor(sqrt, 1, 4) == [1, 0.5, -0.125, 0.0625, -0.0390625]
30
+
31
+ def test_diff_partial():
32
+ mp.dps = 15
33
+ x,y,z = xyz = 2,3,7
34
+ f = lambda x,y,z: 3*x**2 * (y+2)**3 * z**5
35
+ assert diff(f, xyz, (0,0,0)).ae(25210500)
36
+ assert diff(f, xyz, (0,0,1)).ae(18007500)
37
+ assert diff(f, xyz, (0,0,2)).ae(10290000)
38
+ assert diff(f, xyz, (0,1,0)).ae(15126300)
39
+ assert diff(f, xyz, (0,1,1)).ae(10804500)
40
+ assert diff(f, xyz, (0,1,2)).ae(6174000)
41
+ assert diff(f, xyz, (0,2,0)).ae(6050520)
42
+ assert diff(f, xyz, (0,2,1)).ae(4321800)
43
+ assert diff(f, xyz, (0,2,2)).ae(2469600)
44
+ assert diff(f, xyz, (1,0,0)).ae(25210500)
45
+ assert diff(f, xyz, (1,0,1)).ae(18007500)
46
+ assert diff(f, xyz, (1,0,2)).ae(10290000)
47
+ assert diff(f, xyz, (1,1,0)).ae(15126300)
48
+ assert diff(f, xyz, (1,1,1)).ae(10804500)
49
+ assert diff(f, xyz, (1,1,2)).ae(6174000)
50
+ assert diff(f, xyz, (1,2,0)).ae(6050520)
51
+ assert diff(f, xyz, (1,2,1)).ae(4321800)
52
+ assert diff(f, xyz, (1,2,2)).ae(2469600)
53
+ assert diff(f, xyz, (2,0,0)).ae(12605250)
54
+ assert diff(f, xyz, (2,0,1)).ae(9003750)
55
+ assert diff(f, xyz, (2,0,2)).ae(5145000)
56
+ assert diff(f, xyz, (2,1,0)).ae(7563150)
57
+ assert diff(f, xyz, (2,1,1)).ae(5402250)
58
+ assert diff(f, xyz, (2,1,2)).ae(3087000)
59
+ assert diff(f, xyz, (2,2,0)).ae(3025260)
60
+ assert diff(f, xyz, (2,2,1)).ae(2160900)
61
+ assert diff(f, xyz, (2,2,2)).ae(1234800)
llmeval-env/lib/python3.10/site-packages/mpmath/tests/test_division.py ADDED
@@ -0,0 +1,143 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from mpmath.libmp import *
2
+ from mpmath import mpf, mp
3
+
4
+ from random import randint, choice, seed
5
+
6
+ all_modes = [round_floor, round_ceiling, round_down, round_up, round_nearest]
7
+
8
+ fb = from_bstr
9
+ fi = from_int
10
+ ff = from_float
11
+
12
+
13
+ def test_div_1_3():
14
+ a = fi(1)
15
+ b = fi(3)
16
+ c = fi(-1)
17
+
18
+ # floor rounds down, ceiling rounds up
19
+ assert mpf_div(a, b, 7, round_floor) == fb('0.01010101')
20
+ assert mpf_div(a, b, 7, round_ceiling) == fb('0.01010110')
21
+ assert mpf_div(a, b, 7, round_down) == fb('0.01010101')
22
+ assert mpf_div(a, b, 7, round_up) == fb('0.01010110')
23
+ assert mpf_div(a, b, 7, round_nearest) == fb('0.01010101')
24
+
25
+ # floor rounds up, ceiling rounds down
26
+ assert mpf_div(c, b, 7, round_floor) == fb('-0.01010110')
27
+ assert mpf_div(c, b, 7, round_ceiling) == fb('-0.01010101')
28
+ assert mpf_div(c, b, 7, round_down) == fb('-0.01010101')
29
+ assert mpf_div(c, b, 7, round_up) == fb('-0.01010110')
30
+ assert mpf_div(c, b, 7, round_nearest) == fb('-0.01010101')
31
+
32
+ def test_mpf_divi_1_3():
33
+ a = 1
34
+ b = fi(3)
35
+ c = -1
36
+ assert mpf_rdiv_int(a, b, 7, round_floor) == fb('0.01010101')
37
+ assert mpf_rdiv_int(a, b, 7, round_ceiling) == fb('0.01010110')
38
+ assert mpf_rdiv_int(a, b, 7, round_down) == fb('0.01010101')
39
+ assert mpf_rdiv_int(a, b, 7, round_up) == fb('0.01010110')
40
+ assert mpf_rdiv_int(a, b, 7, round_nearest) == fb('0.01010101')
41
+ assert mpf_rdiv_int(c, b, 7, round_floor) == fb('-0.01010110')
42
+ assert mpf_rdiv_int(c, b, 7, round_ceiling) == fb('-0.01010101')
43
+ assert mpf_rdiv_int(c, b, 7, round_down) == fb('-0.01010101')
44
+ assert mpf_rdiv_int(c, b, 7, round_up) == fb('-0.01010110')
45
+ assert mpf_rdiv_int(c, b, 7, round_nearest) == fb('-0.01010101')
46
+
47
+
48
+ def test_div_300():
49
+
50
+ q = fi(1000000)
51
+ a = fi(300499999) # a/q is a little less than a half-integer
52
+ b = fi(300500000) # b/q exactly a half-integer
53
+ c = fi(300500001) # c/q is a little more than a half-integer
54
+
55
+ # Check nearest integer rounding (prec=9 as 2**8 < 300 < 2**9)
56
+
57
+ assert mpf_div(a, q, 9, round_down) == fi(300)
58
+ assert mpf_div(b, q, 9, round_down) == fi(300)
59
+ assert mpf_div(c, q, 9, round_down) == fi(300)
60
+ assert mpf_div(a, q, 9, round_up) == fi(301)
61
+ assert mpf_div(b, q, 9, round_up) == fi(301)
62
+ assert mpf_div(c, q, 9, round_up) == fi(301)
63
+
64
+ # Nearest even integer is down
65
+ assert mpf_div(a, q, 9, round_nearest) == fi(300)
66
+ assert mpf_div(b, q, 9, round_nearest) == fi(300)
67
+ assert mpf_div(c, q, 9, round_nearest) == fi(301)
68
+
69
+ # Nearest even integer is up
70
+ a = fi(301499999)
71
+ b = fi(301500000)
72
+ c = fi(301500001)
73
+ assert mpf_div(a, q, 9, round_nearest) == fi(301)
74
+ assert mpf_div(b, q, 9, round_nearest) == fi(302)
75
+ assert mpf_div(c, q, 9, round_nearest) == fi(302)
76
+
77
+
78
+ def test_tight_integer_division():
79
+ # Test that integer division at tightest possible precision is exact
80
+ N = 100
81
+ seed(1)
82
+ for i in range(N):
83
+ a = choice([1, -1]) * randint(1, 1<<randint(10, 100))
84
+ b = choice([1, -1]) * randint(1, 1<<randint(10, 100))
85
+ p = a * b
86
+ width = bitcount(abs(b)) - trailing(b)
87
+ a = fi(a); b = fi(b); p = fi(p)
88
+ for mode in all_modes:
89
+ assert mpf_div(p, a, width, mode) == b
90
+
91
+
92
+ def test_epsilon_rounding():
93
+ # Verify that mpf_div uses infinite precision; this result will
94
+ # appear to be exactly 0.101 to a near-sighted algorithm
95
+
96
+ a = fb('0.101' + ('0'*200) + '1')
97
+ b = fb('1.10101')
98
+ c = mpf_mul(a, b, 250, round_floor) # exact
99
+ assert mpf_div(c, b, bitcount(a[1]), round_floor) == a # exact
100
+
101
+ assert mpf_div(c, b, 2, round_down) == fb('0.10')
102
+ assert mpf_div(c, b, 3, round_down) == fb('0.101')
103
+ assert mpf_div(c, b, 2, round_up) == fb('0.11')
104
+ assert mpf_div(c, b, 3, round_up) == fb('0.110')
105
+ assert mpf_div(c, b, 2, round_floor) == fb('0.10')
106
+ assert mpf_div(c, b, 3, round_floor) == fb('0.101')
107
+ assert mpf_div(c, b, 2, round_ceiling) == fb('0.11')
108
+ assert mpf_div(c, b, 3, round_ceiling) == fb('0.110')
109
+
110
+ # The same for negative numbers
111
+ a = fb('-0.101' + ('0'*200) + '1')
112
+ b = fb('1.10101')
113
+ c = mpf_mul(a, b, 250, round_floor)
114
+ assert mpf_div(c, b, bitcount(a[1]), round_floor) == a
115
+
116
+ assert mpf_div(c, b, 2, round_down) == fb('-0.10')
117
+ assert mpf_div(c, b, 3, round_up) == fb('-0.110')
118
+
119
+ # Floor goes up, ceiling goes down
120
+ assert mpf_div(c, b, 2, round_floor) == fb('-0.11')
121
+ assert mpf_div(c, b, 3, round_floor) == fb('-0.110')
122
+ assert mpf_div(c, b, 2, round_ceiling) == fb('-0.10')
123
+ assert mpf_div(c, b, 3, round_ceiling) == fb('-0.101')
124
+
125
+
126
+ def test_mod():
127
+ mp.dps = 15
128
+ assert mpf(234) % 1 == 0
129
+ assert mpf(-3) % 256 == 253
130
+ assert mpf(0.25) % 23490.5 == 0.25
131
+ assert mpf(0.25) % -23490.5 == -23490.25
132
+ assert mpf(-0.25) % 23490.5 == 23490.25
133
+ assert mpf(-0.25) % -23490.5 == -0.25
134
+ # Check that these cases are handled efficiently
135
+ assert mpf('1e10000000000') % 1 == 0
136
+ assert mpf('1.23e-1000000000') % 1 == mpf('1.23e-1000000000')
137
+ # test __rmod__
138
+ assert 3 % mpf('1.75') == 1.25
139
+
140
+ def test_div_negative_rnd_bug():
141
+ mp.dps = 15
142
+ assert (-3) / mpf('0.1531879017645047') == mpf('-19.583791966887116')
143
+ assert mpf('-2.6342475750861301') / mpf('0.35126216427941814') == mpf('-7.4993775104985909')
llmeval-env/lib/python3.10/site-packages/mpmath/tests/test_eigen.py ADDED
@@ -0,0 +1,179 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/python
2
+ # -*- coding: utf-8 -*-
3
+
4
+ from mpmath import mp
5
+ from mpmath import libmp
6
+
7
+ xrange = libmp.backend.xrange
8
+
9
+ def run_hessenberg(A, verbose = 0):
10
+ if verbose > 1:
11
+ print("original matrix (hessenberg):\n", A)
12
+
13
+ n = A.rows
14
+
15
+ Q, H = mp.hessenberg(A)
16
+
17
+ if verbose > 1:
18
+ print("Q:\n",Q)
19
+ print("H:\n",H)
20
+
21
+ B = Q * H * Q.transpose_conj()
22
+
23
+ eps = mp.exp(0.8 * mp.log(mp.eps))
24
+
25
+ err0 = 0
26
+ for x in xrange(n):
27
+ for y in xrange(n):
28
+ err0 += abs(A[y,x] - B[y,x])
29
+ err0 /= n * n
30
+
31
+ err1 = 0
32
+ for x in xrange(n):
33
+ for y in xrange(x + 2, n):
34
+ err1 += abs(H[y,x])
35
+
36
+ if verbose > 0:
37
+ print("difference (H):", err0, err1)
38
+
39
+ if verbose > 1:
40
+ print("B:\n", B)
41
+
42
+ assert err0 < eps
43
+ assert err1 == 0
44
+
45
+
46
+ def run_schur(A, verbose = 0):
47
+ if verbose > 1:
48
+ print("original matrix (schur):\n", A)
49
+
50
+ n = A.rows
51
+
52
+ Q, R = mp.schur(A)
53
+
54
+ if verbose > 1:
55
+ print("Q:\n", Q)
56
+ print("R:\n", R)
57
+
58
+ B = Q * R * Q.transpose_conj()
59
+ C = Q * Q.transpose_conj()
60
+
61
+ eps = mp.exp(0.8 * mp.log(mp.eps))
62
+
63
+ err0 = 0
64
+ for x in xrange(n):
65
+ for y in xrange(n):
66
+ err0 += abs(A[y,x] - B[y,x])
67
+ err0 /= n * n
68
+
69
+ err1 = 0
70
+ for x in xrange(n):
71
+ for y in xrange(n):
72
+ if x == y:
73
+ C[y,x] -= 1
74
+ err1 += abs(C[y,x])
75
+ err1 /= n * n
76
+
77
+ err2 = 0
78
+ for x in xrange(n):
79
+ for y in xrange(x + 1, n):
80
+ err2 += abs(R[y,x])
81
+
82
+ if verbose > 0:
83
+ print("difference (S):", err0, err1, err2)
84
+
85
+ if verbose > 1:
86
+ print("B:\n", B)
87
+
88
+ assert err0 < eps
89
+ assert err1 < eps
90
+ assert err2 == 0
91
+
92
+ def run_eig(A, verbose = 0):
93
+ if verbose > 1:
94
+ print("original matrix (eig):\n", A)
95
+
96
+ n = A.rows
97
+
98
+ E, EL, ER = mp.eig(A, left = True, right = True)
99
+
100
+ if verbose > 1:
101
+ print("E:\n", E)
102
+ print("EL:\n", EL)
103
+ print("ER:\n", ER)
104
+
105
+ eps = mp.exp(0.8 * mp.log(mp.eps))
106
+
107
+ err0 = 0
108
+ for i in xrange(n):
109
+ B = A * ER[:,i] - E[i] * ER[:,i]
110
+ err0 = max(err0, mp.mnorm(B))
111
+
112
+ B = EL[i,:] * A - EL[i,:] * E[i]
113
+ err0 = max(err0, mp.mnorm(B))
114
+
115
+ err0 /= n * n
116
+
117
+ if verbose > 0:
118
+ print("difference (E):", err0)
119
+
120
+ assert err0 < eps
121
+
122
+ #####################
123
+
124
+ def test_eig_dyn():
125
+ v = 0
126
+ for i in xrange(5):
127
+ n = 1 + int(mp.rand() * 5)
128
+ if mp.rand() > 0.5:
129
+ # real
130
+ A = 2 * mp.randmatrix(n, n) - 1
131
+ if mp.rand() > 0.5:
132
+ A *= 10
133
+ for x in xrange(n):
134
+ for y in xrange(n):
135
+ A[x,y] = int(A[x,y])
136
+ else:
137
+ A = (2 * mp.randmatrix(n, n) - 1) + 1j * (2 * mp.randmatrix(n, n) - 1)
138
+ if mp.rand() > 0.5:
139
+ A *= 10
140
+ for x in xrange(n):
141
+ for y in xrange(n):
142
+ A[x,y] = int(mp.re(A[x,y])) + 1j * int(mp.im(A[x,y]))
143
+
144
+ run_hessenberg(A, verbose = v)
145
+ run_schur(A, verbose = v)
146
+ run_eig(A, verbose = v)
147
+
148
+ def test_eig():
149
+ v = 0
150
+ AS = []
151
+
152
+ A = mp.matrix([[2, 1, 0], # jordan block of size 3
153
+ [0, 2, 1],
154
+ [0, 0, 2]])
155
+ AS.append(A)
156
+ AS.append(A.transpose())
157
+
158
+ A = mp.matrix([[2, 0, 0], # jordan block of size 2
159
+ [0, 2, 1],
160
+ [0, 0, 2]])
161
+ AS.append(A)
162
+ AS.append(A.transpose())
163
+
164
+ A = mp.matrix([[2, 0, 1], # jordan block of size 2
165
+ [0, 2, 0],
166
+ [0, 0, 2]])
167
+ AS.append(A)
168
+ AS.append(A.transpose())
169
+
170
+ A= mp.matrix([[0, 0, 1], # cyclic
171
+ [1, 0, 0],
172
+ [0, 1, 0]])
173
+ AS.append(A)
174
+ AS.append(A.transpose())
175
+
176
+ for A in AS:
177
+ run_hessenberg(A, verbose = v)
178
+ run_schur(A, verbose = v)
179
+ run_eig(A, verbose = v)
llmeval-env/lib/python3.10/site-packages/mpmath/tests/test_eigen_symmetric.py ADDED
@@ -0,0 +1,357 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/python
2
+ # -*- coding: utf-8 -*-
3
+
4
+ from mpmath import mp
5
+ from mpmath import libmp
6
+
7
+ xrange = libmp.backend.xrange
8
+
9
+ def run_eigsy(A, verbose = False):
10
+ if verbose:
11
+ print("original matrix:\n", str(A))
12
+
13
+ D, Q = mp.eigsy(A)
14
+ B = Q * mp.diag(D) * Q.transpose()
15
+ C = A - B
16
+ E = Q * Q.transpose() - mp.eye(A.rows)
17
+
18
+ if verbose:
19
+ print("eigenvalues:\n", D)
20
+ print("eigenvectors:\n", Q)
21
+
22
+ NC = mp.mnorm(C)
23
+ NE = mp.mnorm(E)
24
+
25
+ if verbose:
26
+ print("difference:", NC, "\n", C, "\n")
27
+ print("difference:", NE, "\n", E, "\n")
28
+
29
+ eps = mp.exp( 0.8 * mp.log(mp.eps))
30
+
31
+ assert NC < eps
32
+ assert NE < eps
33
+
34
+ return NC
35
+
36
+ def run_eighe(A, verbose = False):
37
+ if verbose:
38
+ print("original matrix:\n", str(A))
39
+
40
+ D, Q = mp.eighe(A)
41
+ B = Q * mp.diag(D) * Q.transpose_conj()
42
+ C = A - B
43
+ E = Q * Q.transpose_conj() - mp.eye(A.rows)
44
+
45
+ if verbose:
46
+ print("eigenvalues:\n", D)
47
+ print("eigenvectors:\n", Q)
48
+
49
+ NC = mp.mnorm(C)
50
+ NE = mp.mnorm(E)
51
+
52
+ if verbose:
53
+ print("difference:", NC, "\n", C, "\n")
54
+ print("difference:", NE, "\n", E, "\n")
55
+
56
+ eps = mp.exp( 0.8 * mp.log(mp.eps))
57
+
58
+ assert NC < eps
59
+ assert NE < eps
60
+
61
+ return NC
62
+
63
+ def run_svd_r(A, full_matrices = False, verbose = True):
64
+
65
+ m, n = A.rows, A.cols
66
+
67
+ eps = mp.exp(0.8 * mp.log(mp.eps))
68
+
69
+ if verbose:
70
+ print("original matrix:\n", str(A))
71
+ print("full", full_matrices)
72
+
73
+ U, S0, V = mp.svd_r(A, full_matrices = full_matrices)
74
+
75
+ S = mp.zeros(U.cols, V.rows)
76
+ for j in xrange(min(m, n)):
77
+ S[j,j] = S0[j]
78
+
79
+ if verbose:
80
+ print("U:\n", str(U))
81
+ print("S:\n", str(S0))
82
+ print("V:\n", str(V))
83
+
84
+ C = U * S * V - A
85
+ err = mp.mnorm(C)
86
+ if verbose:
87
+ print("C\n", str(C), "\n", err)
88
+ assert err < eps
89
+
90
+ D = V * V.transpose() - mp.eye(V.rows)
91
+ err = mp.mnorm(D)
92
+ if verbose:
93
+ print("D:\n", str(D), "\n", err)
94
+ assert err < eps
95
+
96
+ E = U.transpose() * U - mp.eye(U.cols)
97
+ err = mp.mnorm(E)
98
+ if verbose:
99
+ print("E:\n", str(E), "\n", err)
100
+ assert err < eps
101
+
102
+ def run_svd_c(A, full_matrices = False, verbose = True):
103
+
104
+ m, n = A.rows, A.cols
105
+
106
+ eps = mp.exp(0.8 * mp.log(mp.eps))
107
+
108
+ if verbose:
109
+ print("original matrix:\n", str(A))
110
+ print("full", full_matrices)
111
+
112
+ U, S0, V = mp.svd_c(A, full_matrices = full_matrices)
113
+
114
+ S = mp.zeros(U.cols, V.rows)
115
+ for j in xrange(min(m, n)):
116
+ S[j,j] = S0[j]
117
+
118
+ if verbose:
119
+ print("U:\n", str(U))
120
+ print("S:\n", str(S0))
121
+ print("V:\n", str(V))
122
+
123
+ C = U * S * V - A
124
+ err = mp.mnorm(C)
125
+ if verbose:
126
+ print("C\n", str(C), "\n", err)
127
+ assert err < eps
128
+
129
+ D = V * V.transpose_conj() - mp.eye(V.rows)
130
+ err = mp.mnorm(D)
131
+ if verbose:
132
+ print("D:\n", str(D), "\n", err)
133
+ assert err < eps
134
+
135
+ E = U.transpose_conj() * U - mp.eye(U.cols)
136
+ err = mp.mnorm(E)
137
+ if verbose:
138
+ print("E:\n", str(E), "\n", err)
139
+ assert err < eps
140
+
141
+ def run_gauss(qtype, a, b):
142
+ eps = 1e-5
143
+
144
+ d, e = mp.gauss_quadrature(len(a), qtype)
145
+ d -= mp.matrix(a)
146
+ e -= mp.matrix(b)
147
+
148
+ assert mp.mnorm(d) < eps
149
+ assert mp.mnorm(e) < eps
150
+
151
+ def irandmatrix(n, range = 10):
152
+ """
153
+ random matrix with integer entries
154
+ """
155
+ A = mp.matrix(n, n)
156
+ for i in xrange(n):
157
+ for j in xrange(n):
158
+ A[i,j]=int( (2 * mp.rand() - 1) * range)
159
+ return A
160
+
161
+ #######################
162
+
163
+ def test_eighe_fixed_matrix():
164
+ A = mp.matrix([[2, 3], [3, 5]])
165
+ run_eigsy(A)
166
+ run_eighe(A)
167
+
168
+ A = mp.matrix([[7, -11], [-11, 13]])
169
+ run_eigsy(A)
170
+ run_eighe(A)
171
+
172
+ A = mp.matrix([[2, 11, 7], [11, 3, 13], [7, 13, 5]])
173
+ run_eigsy(A)
174
+ run_eighe(A)
175
+
176
+ A = mp.matrix([[2, 0, 7], [0, 3, 1], [7, 1, 5]])
177
+ run_eigsy(A)
178
+ run_eighe(A)
179
+
180
+ #
181
+
182
+ A = mp.matrix([[2, 3+7j], [3-7j, 5]])
183
+ run_eighe(A)
184
+
185
+ A = mp.matrix([[2, -11j, 0], [+11j, 3, 29j], [0, -29j, 5]])
186
+ run_eighe(A)
187
+
188
+ A = mp.matrix([[2, 11 + 17j, 7 + 19j], [11 - 17j, 3, -13 + 23j], [7 - 19j, -13 - 23j, 5]])
189
+ run_eighe(A)
190
+
191
+ def test_eigsy_randmatrix():
192
+ N = 5
193
+
194
+ for a in xrange(10):
195
+ A = 2 * mp.randmatrix(N, N) - 1
196
+
197
+ for i in xrange(0, N):
198
+ for j in xrange(i + 1, N):
199
+ A[j,i] = A[i,j]
200
+
201
+ run_eigsy(A)
202
+
203
+ def test_eighe_randmatrix():
204
+ N = 5
205
+
206
+ for a in xrange(10):
207
+ A = (2 * mp.randmatrix(N, N) - 1) + 1j * (2 * mp.randmatrix(N, N) - 1)
208
+
209
+ for i in xrange(0, N):
210
+ A[i,i] = mp.re(A[i,i])
211
+ for j in xrange(i + 1, N):
212
+ A[j,i] = mp.conj(A[i,j])
213
+
214
+ run_eighe(A)
215
+
216
+ def test_eigsy_irandmatrix():
217
+ N = 4
218
+ R = 4
219
+
220
+ for a in xrange(10):
221
+ A=irandmatrix(N, R)
222
+
223
+ for i in xrange(0, N):
224
+ for j in xrange(i + 1, N):
225
+ A[j,i] = A[i,j]
226
+
227
+ run_eigsy(A)
228
+
229
+ def test_eighe_irandmatrix():
230
+ N = 4
231
+ R = 4
232
+
233
+ for a in xrange(10):
234
+ A=irandmatrix(N, R) + 1j * irandmatrix(N, R)
235
+
236
+ for i in xrange(0, N):
237
+ A[i,i] = mp.re(A[i,i])
238
+ for j in xrange(i + 1, N):
239
+ A[j,i] = mp.conj(A[i,j])
240
+
241
+ run_eighe(A)
242
+
243
+ def test_svd_r_rand():
244
+ for i in xrange(5):
245
+ full = mp.rand() > 0.5
246
+ m = 1 + int(mp.rand() * 10)
247
+ n = 1 + int(mp.rand() * 10)
248
+ A = 2 * mp.randmatrix(m, n) - 1
249
+ if mp.rand() > 0.5:
250
+ A *= 10
251
+ for x in xrange(m):
252
+ for y in xrange(n):
253
+ A[x,y]=int(A[x,y])
254
+
255
+ run_svd_r(A, full_matrices = full, verbose = False)
256
+
257
+ def test_svd_c_rand():
258
+ for i in xrange(5):
259
+ full = mp.rand() > 0.5
260
+ m = 1 + int(mp.rand() * 10)
261
+ n = 1 + int(mp.rand() * 10)
262
+ A = (2 * mp.randmatrix(m, n) - 1) + 1j * (2 * mp.randmatrix(m, n) - 1)
263
+ if mp.rand() > 0.5:
264
+ A *= 10
265
+ for x in xrange(m):
266
+ for y in xrange(n):
267
+ A[x,y]=int(mp.re(A[x,y])) + 1j * int(mp.im(A[x,y]))
268
+
269
+ run_svd_c(A, full_matrices=full, verbose=False)
270
+
271
+ def test_svd_test_case():
272
+ # a test case from Golub and Reinsch
273
+ # (see wilkinson/reinsch: handbook for auto. comp., vol ii-linear algebra, 134-151(1971).)
274
+
275
+ eps = mp.exp(0.8 * mp.log(mp.eps))
276
+
277
+ a = [[22, 10, 2, 3, 7],
278
+ [14, 7, 10, 0, 8],
279
+ [-1, 13, -1, -11, 3],
280
+ [-3, -2, 13, -2, 4],
281
+ [ 9, 8, 1, -2, 4],
282
+ [ 9, 1, -7, 5, -1],
283
+ [ 2, -6, 6, 5, 1],
284
+ [ 4, 5, 0, -2, 2]]
285
+
286
+ a = mp.matrix(a)
287
+ b = mp.matrix([mp.sqrt(1248), 20, mp.sqrt(384), 0, 0])
288
+
289
+ S = mp.svd_r(a, compute_uv = False)
290
+ S -= b
291
+ assert mp.mnorm(S) < eps
292
+
293
+ S = mp.svd_c(a, compute_uv = False)
294
+ S -= b
295
+ assert mp.mnorm(S) < eps
296
+
297
+
298
+ def test_gauss_quadrature_static():
299
+ a = [-0.57735027, 0.57735027]
300
+ b = [ 1, 1]
301
+ run_gauss("legendre", a , b)
302
+
303
+ a = [ -0.906179846, -0.538469310, 0, 0.538469310, 0.906179846]
304
+ b = [ 0.23692689, 0.47862867, 0.56888889, 0.47862867, 0.23692689]
305
+ run_gauss("legendre", a , b)
306
+
307
+ a = [ 0.06943184, 0.33000948, 0.66999052, 0.93056816]
308
+ b = [ 0.17392742, 0.32607258, 0.32607258, 0.17392742]
309
+ run_gauss("legendre01", a , b)
310
+
311
+ a = [-0.70710678, 0.70710678]
312
+ b = [ 0.88622693, 0.88622693]
313
+ run_gauss("hermite", a , b)
314
+
315
+ a = [ -2.02018287, -0.958572465, 0, 0.958572465, 2.02018287]
316
+ b = [ 0.01995324, 0.39361932, 0.94530872, 0.39361932, 0.01995324]
317
+ run_gauss("hermite", a , b)
318
+
319
+ a = [ 0.41577456, 2.29428036, 6.28994508]
320
+ b = [ 0.71109301, 0.27851773, 0.01038926]
321
+ run_gauss("laguerre", a , b)
322
+
323
+ def test_gauss_quadrature_dynamic(verbose = False):
324
+ n = 5
325
+
326
+ A = mp.randmatrix(2 * n, 1)
327
+
328
+ def F(x):
329
+ r = 0
330
+ for i in xrange(len(A) - 1, -1, -1):
331
+ r = r * x + A[i]
332
+ return r
333
+
334
+ def run(qtype, FW, R, alpha = 0, beta = 0):
335
+ X, W = mp.gauss_quadrature(n, qtype, alpha = alpha, beta = beta)
336
+
337
+ a = 0
338
+ for i in xrange(len(X)):
339
+ a += W[i] * F(X[i])
340
+
341
+ b = mp.quad(lambda x: FW(x) * F(x), R)
342
+
343
+ c = mp.fabs(a - b)
344
+
345
+ if verbose:
346
+ print(qtype, c, a, b)
347
+
348
+ assert c < 1e-5
349
+
350
+ run("legendre", lambda x: 1, [-1, 1])
351
+ run("legendre01", lambda x: 1, [0, 1])
352
+ run("hermite", lambda x: mp.exp(-x*x), [-mp.inf, mp.inf])
353
+ run("laguerre", lambda x: mp.exp(-x), [0, mp.inf])
354
+ run("glaguerre", lambda x: mp.sqrt(x)*mp.exp(-x), [0, mp.inf], alpha = 1 / mp.mpf(2))
355
+ run("chebyshev1", lambda x: 1/mp.sqrt(1-x*x), [-1, 1])
356
+ run("chebyshev2", lambda x: mp.sqrt(1-x*x), [-1, 1])
357
+ run("jacobi", lambda x: (1-x)**(1/mp.mpf(3)) * (1+x)**(1/mp.mpf(5)), [-1, 1], alpha = 1 / mp.mpf(3), beta = 1 / mp.mpf(5) )
llmeval-env/lib/python3.10/site-packages/mpmath/tests/test_elliptic.py ADDED
@@ -0,0 +1,670 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 mpmath
17
+ import random
18
+ import pytest
19
+
20
+ from mpmath import *
21
+
22
+ def mpc_ae(a, b, eps=eps):
23
+ res = True
24
+ res = res and a.real.ae(b.real, eps)
25
+ res = res and a.imag.ae(b.imag, eps)
26
+ return res
27
+
28
+ zero = mpf(0)
29
+ one = mpf(1)
30
+
31
+ jsn = ellipfun('sn')
32
+ jcn = ellipfun('cn')
33
+ jdn = ellipfun('dn')
34
+
35
+ calculate_nome = lambda k: qfrom(k=k)
36
+
37
+ def test_ellipfun():
38
+ mp.dps = 15
39
+ assert ellipfun('ss', 0, 0) == 1
40
+ assert ellipfun('cc', 0, 0) == 1
41
+ assert ellipfun('dd', 0, 0) == 1
42
+ assert ellipfun('nn', 0, 0) == 1
43
+ assert ellipfun('sn', 0.25, 0).ae(sin(0.25))
44
+ assert ellipfun('cn', 0.25, 0).ae(cos(0.25))
45
+ assert ellipfun('dn', 0.25, 0).ae(1)
46
+ assert ellipfun('ns', 0.25, 0).ae(csc(0.25))
47
+ assert ellipfun('nc', 0.25, 0).ae(sec(0.25))
48
+ assert ellipfun('nd', 0.25, 0).ae(1)
49
+ assert ellipfun('sc', 0.25, 0).ae(tan(0.25))
50
+ assert ellipfun('sd', 0.25, 0).ae(sin(0.25))
51
+ assert ellipfun('cd', 0.25, 0).ae(cos(0.25))
52
+ assert ellipfun('cs', 0.25, 0).ae(cot(0.25))
53
+ assert ellipfun('dc', 0.25, 0).ae(sec(0.25))
54
+ assert ellipfun('ds', 0.25, 0).ae(csc(0.25))
55
+ assert ellipfun('sn', 0.25, 1).ae(tanh(0.25))
56
+ assert ellipfun('cn', 0.25, 1).ae(sech(0.25))
57
+ assert ellipfun('dn', 0.25, 1).ae(sech(0.25))
58
+ assert ellipfun('ns', 0.25, 1).ae(coth(0.25))
59
+ assert ellipfun('nc', 0.25, 1).ae(cosh(0.25))
60
+ assert ellipfun('nd', 0.25, 1).ae(cosh(0.25))
61
+ assert ellipfun('sc', 0.25, 1).ae(sinh(0.25))
62
+ assert ellipfun('sd', 0.25, 1).ae(sinh(0.25))
63
+ assert ellipfun('cd', 0.25, 1).ae(1)
64
+ assert ellipfun('cs', 0.25, 1).ae(csch(0.25))
65
+ assert ellipfun('dc', 0.25, 1).ae(1)
66
+ assert ellipfun('ds', 0.25, 1).ae(csch(0.25))
67
+ assert ellipfun('sn', 0.25, 0.5).ae(0.24615967096986145833)
68
+ assert ellipfun('cn', 0.25, 0.5).ae(0.96922928989378439337)
69
+ assert ellipfun('dn', 0.25, 0.5).ae(0.98473484156599474563)
70
+ assert ellipfun('ns', 0.25, 0.5).ae(4.0624038700573130369)
71
+ assert ellipfun('nc', 0.25, 0.5).ae(1.0317476065024692949)
72
+ assert ellipfun('nd', 0.25, 0.5).ae(1.0155017958029488665)
73
+ assert ellipfun('sc', 0.25, 0.5).ae(0.25397465134058993408)
74
+ assert ellipfun('sd', 0.25, 0.5).ae(0.24997558792415733063)
75
+ assert ellipfun('cd', 0.25, 0.5).ae(0.98425408443195497052)
76
+ assert ellipfun('cs', 0.25, 0.5).ae(3.9374008182374110826)
77
+ assert ellipfun('dc', 0.25, 0.5).ae(1.0159978158253033913)
78
+ assert ellipfun('ds', 0.25, 0.5).ae(4.0003906313579720593)
79
+
80
+
81
+
82
+
83
+ def test_calculate_nome():
84
+ mp.dps = 100
85
+
86
+ q = calculate_nome(zero)
87
+ assert(q == zero)
88
+
89
+ mp.dps = 25
90
+ # used Mathematica's EllipticNomeQ[m]
91
+ math1 = [(mpf(1)/10, mpf('0.006584651553858370274473060')),
92
+ (mpf(2)/10, mpf('0.01394285727531826872146409')),
93
+ (mpf(3)/10, mpf('0.02227743615715350822901627')),
94
+ (mpf(4)/10, mpf('0.03188334731336317755064299')),
95
+ (mpf(5)/10, mpf('0.04321391826377224977441774')),
96
+ (mpf(6)/10, mpf('0.05702025781460967637754953')),
97
+ (mpf(7)/10, mpf('0.07468994353717944761143751')),
98
+ (mpf(8)/10, mpf('0.09927369733882489703607378')),
99
+ (mpf(9)/10, mpf('0.1401731269542615524091055')),
100
+ (mpf(9)/10, mpf('0.1401731269542615524091055'))]
101
+
102
+ for i in math1:
103
+ m = i[0]
104
+ q = calculate_nome(sqrt(m))
105
+ assert q.ae(i[1])
106
+
107
+ mp.dps = 15
108
+
109
+ def test_jtheta():
110
+ mp.dps = 25
111
+
112
+ z = q = zero
113
+ for n in range(1,5):
114
+ value = jtheta(n, z, q)
115
+ assert(value == (n-1)//2)
116
+
117
+ for q in [one, mpf(2)]:
118
+ for n in range(1,5):
119
+ pytest.raises(ValueError, lambda: jtheta(n, z, q))
120
+
121
+ z = one/10
122
+ q = one/11
123
+
124
+ # Mathematical N[EllipticTheta[1, 1/10, 1/11], 25]
125
+ res = mpf('0.1069552990104042681962096')
126
+ result = jtheta(1, z, q)
127
+ assert(result.ae(res))
128
+
129
+ # Mathematica N[EllipticTheta[2, 1/10, 1/11], 25]
130
+ res = mpf('1.101385760258855791140606')
131
+ result = jtheta(2, z, q)
132
+ assert(result.ae(res))
133
+
134
+ # Mathematica N[EllipticTheta[3, 1/10, 1/11], 25]
135
+ res = mpf('1.178319743354331061795905')
136
+ result = jtheta(3, z, q)
137
+ assert(result.ae(res))
138
+
139
+ # Mathematica N[EllipticTheta[4, 1/10, 1/11], 25]
140
+ res = mpf('0.8219318954665153577314573')
141
+ result = jtheta(4, z, q)
142
+ assert(result.ae(res))
143
+
144
+ # test for sin zeros for jtheta(1, z, q)
145
+ # test for cos zeros for jtheta(2, z, q)
146
+ z1 = pi
147
+ z2 = pi/2
148
+ for i in range(10):
149
+ qstring = str(random.random())
150
+ q = mpf(qstring)
151
+ result = jtheta(1, z1, q)
152
+ assert(result.ae(0))
153
+ result = jtheta(2, z2, q)
154
+ assert(result.ae(0))
155
+ mp.dps = 15
156
+
157
+
158
+ def test_jtheta_issue_79():
159
+ # near the circle of covergence |q| = 1 the convergence slows
160
+ # down; for |q| > Q_LIM the theta functions raise ValueError
161
+ mp.dps = 30
162
+ mp.dps += 30
163
+ q = mpf(6)/10 - one/10**6 - mpf(8)/10 * j
164
+ mp.dps -= 30
165
+ # Mathematica run first
166
+ # N[EllipticTheta[3, 1, 6/10 - 10^-6 - 8/10*I], 2000]
167
+ # then it works:
168
+ # N[EllipticTheta[3, 1, 6/10 - 10^-6 - 8/10*I], 30]
169
+ res = mpf('32.0031009628901652627099524264') + \
170
+ mpf('16.6153027998236087899308935624') * j
171
+ result = jtheta(3, 1, q)
172
+ # check that for abs(q) > Q_LIM a ValueError exception is raised
173
+ mp.dps += 30
174
+ q = mpf(6)/10 - one/10**7 - mpf(8)/10 * j
175
+ mp.dps -= 30
176
+ pytest.raises(ValueError, lambda: jtheta(3, 1, q))
177
+
178
+ # bug reported in issue 79
179
+ mp.dps = 100
180
+ z = (1+j)/3
181
+ q = mpf(368983957219251)/10**15 + mpf(636363636363636)/10**15 * j
182
+ # Mathematica N[EllipticTheta[1, z, q], 35]
183
+ res = mpf('2.4439389177990737589761828991467471') + \
184
+ mpf('0.5446453005688226915290954851851490') *j
185
+ mp.dps = 30
186
+ result = jtheta(1, z, q)
187
+ assert(result.ae(res))
188
+ mp.dps = 80
189
+ z = 3 + 4*j
190
+ q = 0.5 + 0.5*j
191
+ r1 = jtheta(1, z, q)
192
+ mp.dps = 15
193
+ r2 = jtheta(1, z, q)
194
+ assert r1.ae(r2)
195
+ mp.dps = 80
196
+ z = 3 + j
197
+ q1 = exp(j*3)
198
+ # longer test
199
+ # for n in range(1, 6)
200
+ for n in range(1, 2):
201
+ mp.dps = 80
202
+ q = q1*(1 - mpf(1)/10**n)
203
+ r1 = jtheta(1, z, q)
204
+ mp.dps = 15
205
+ r2 = jtheta(1, z, q)
206
+ assert r1.ae(r2)
207
+ mp.dps = 15
208
+ # issue 79 about high derivatives
209
+ assert jtheta(3, 4.5, 0.25, 9).ae(1359.04892680683)
210
+ assert jtheta(3, 4.5, 0.25, 50).ae(-6.14832772630905e+33)
211
+ mp.dps = 50
212
+ r = jtheta(3, 4.5, 0.25, 9)
213
+ assert r.ae('1359.048926806828939547859396600218966947753213803')
214
+ r = jtheta(3, 4.5, 0.25, 50)
215
+ assert r.ae('-6148327726309051673317975084654262.4119215720343656')
216
+
217
+ def test_jtheta_identities():
218
+ """
219
+ Tests the some of the jacobi identidies found in Abramowitz,
220
+ Sec. 16.28, Pg. 576. The identities are tested to 1 part in 10^98.
221
+ """
222
+ mp.dps = 110
223
+ eps1 = ldexp(eps, 30)
224
+
225
+ for i in range(10):
226
+ qstring = str(random.random())
227
+ q = mpf(qstring)
228
+
229
+ zstring = str(10*random.random())
230
+ z = mpf(zstring)
231
+ # Abramowitz 16.28.1
232
+ # v_1(z, q)**2 * v_4(0, q)**2 = v_3(z, q)**2 * v_2(0, q)**2
233
+ # - v_2(z, q)**2 * v_3(0, q)**2
234
+ term1 = (jtheta(1, z, q)**2) * (jtheta(4, zero, q)**2)
235
+ term2 = (jtheta(3, z, q)**2) * (jtheta(2, zero, q)**2)
236
+ term3 = (jtheta(2, z, q)**2) * (jtheta(3, zero, q)**2)
237
+ equality = term1 - term2 + term3
238
+ assert(equality.ae(0, eps1))
239
+
240
+ zstring = str(100*random.random())
241
+ z = mpf(zstring)
242
+ # Abramowitz 16.28.2
243
+ # v_2(z, q)**2 * v_4(0, q)**2 = v_4(z, q)**2 * v_2(0, q)**2
244
+ # - v_1(z, q)**2 * v_3(0, q)**2
245
+ term1 = (jtheta(2, z, q)**2) * (jtheta(4, zero, q)**2)
246
+ term2 = (jtheta(4, z, q)**2) * (jtheta(2, zero, q)**2)
247
+ term3 = (jtheta(1, z, q)**2) * (jtheta(3, zero, q)**2)
248
+ equality = term1 - term2 + term3
249
+ assert(equality.ae(0, eps1))
250
+
251
+ # Abramowitz 16.28.3
252
+ # v_3(z, q)**2 * v_4(0, q)**2 = v_4(z, q)**2 * v_3(0, q)**2
253
+ # - v_1(z, q)**2 * v_2(0, q)**2
254
+ term1 = (jtheta(3, z, q)**2) * (jtheta(4, zero, q)**2)
255
+ term2 = (jtheta(4, z, q)**2) * (jtheta(3, zero, q)**2)
256
+ term3 = (jtheta(1, z, q)**2) * (jtheta(2, zero, q)**2)
257
+ equality = term1 - term2 + term3
258
+ assert(equality.ae(0, eps1))
259
+
260
+ # Abramowitz 16.28.4
261
+ # v_4(z, q)**2 * v_4(0, q)**2 = v_3(z, q)**2 * v_3(0, q)**2
262
+ # - v_2(z, q)**2 * v_2(0, q)**2
263
+ term1 = (jtheta(4, z, q)**2) * (jtheta(4, zero, q)**2)
264
+ term2 = (jtheta(3, z, q)**2) * (jtheta(3, zero, q)**2)
265
+ term3 = (jtheta(2, z, q)**2) * (jtheta(2, zero, q)**2)
266
+ equality = term1 - term2 + term3
267
+ assert(equality.ae(0, eps1))
268
+
269
+ # Abramowitz 16.28.5
270
+ # v_2(0, q)**4 + v_4(0, q)**4 == v_3(0, q)**4
271
+ term1 = (jtheta(2, zero, q))**4
272
+ term2 = (jtheta(4, zero, q))**4
273
+ term3 = (jtheta(3, zero, q))**4
274
+ equality = term1 + term2 - term3
275
+ assert(equality.ae(0, eps1))
276
+ mp.dps = 15
277
+
278
+ def test_jtheta_complex():
279
+ mp.dps = 30
280
+ z = mpf(1)/4 + j/8
281
+ q = mpf(1)/3 + j/7
282
+ # Mathematica N[EllipticTheta[1, 1/4 + I/8, 1/3 + I/7], 35]
283
+ res = mpf('0.31618034835986160705729105731678285') + \
284
+ mpf('0.07542013825835103435142515194358975') * j
285
+ r = jtheta(1, z, q)
286
+ assert(mpc_ae(r, res))
287
+
288
+ # Mathematica N[EllipticTheta[2, 1/4 + I/8, 1/3 + I/7], 35]
289
+ res = mpf('1.6530986428239765928634711417951828') + \
290
+ mpf('0.2015344864707197230526742145361455') * j
291
+ r = jtheta(2, z, q)
292
+ assert(mpc_ae(r, res))
293
+
294
+ # Mathematica N[EllipticTheta[3, 1/4 + I/8, 1/3 + I/7], 35]
295
+ res = mpf('1.6520564411784228184326012700348340') + \
296
+ mpf('0.1998129119671271328684690067401823') * j
297
+ r = jtheta(3, z, q)
298
+ assert(mpc_ae(r, res))
299
+
300
+ # Mathematica N[EllipticTheta[4, 1/4 + I/8, 1/3 + I/7], 35]
301
+ res = mpf('0.37619082382228348252047624089973824') - \
302
+ mpf('0.15623022130983652972686227200681074') * j
303
+ r = jtheta(4, z, q)
304
+ assert(mpc_ae(r, res))
305
+
306
+ # check some theta function identities
307
+ mp.dos = 100
308
+ z = mpf(1)/4 + j/8
309
+ q = mpf(1)/3 + j/7
310
+ mp.dps += 10
311
+ a = [0,0, jtheta(2, 0, q), jtheta(3, 0, q), jtheta(4, 0, q)]
312
+ t = [0, jtheta(1, z, q), jtheta(2, z, q), jtheta(3, z, q), jtheta(4, z, q)]
313
+ r = [(t[2]*a[4])**2 - (t[4]*a[2])**2 + (t[1] *a[3])**2,
314
+ (t[3]*a[4])**2 - (t[4]*a[3])**2 + (t[1] *a[2])**2,
315
+ (t[1]*a[4])**2 - (t[3]*a[2])**2 + (t[2] *a[3])**2,
316
+ (t[4]*a[4])**2 - (t[3]*a[3])**2 + (t[2] *a[2])**2,
317
+ a[2]**4 + a[4]**4 - a[3]**4]
318
+ mp.dps -= 10
319
+ for x in r:
320
+ assert(mpc_ae(x, mpc(0)))
321
+ mp.dps = 15
322
+
323
+ def test_djtheta():
324
+ mp.dps = 30
325
+
326
+ z = one/7 + j/3
327
+ q = one/8 + j/5
328
+ # Mathematica N[EllipticThetaPrime[1, 1/7 + I/3, 1/8 + I/5], 35]
329
+ res = mpf('1.5555195883277196036090928995803201') - \
330
+ mpf('0.02439761276895463494054149673076275') * j
331
+ result = jtheta(1, z, q, 1)
332
+ assert(mpc_ae(result, res))
333
+
334
+ # Mathematica N[EllipticThetaPrime[2, 1/7 + I/3, 1/8 + I/5], 35]
335
+ res = mpf('0.19825296689470982332701283509685662') - \
336
+ mpf('0.46038135182282106983251742935250009') * j
337
+ result = jtheta(2, z, q, 1)
338
+ assert(mpc_ae(result, res))
339
+
340
+ # Mathematica N[EllipticThetaPrime[3, 1/7 + I/3, 1/8 + I/5], 35]
341
+ res = mpf('0.36492498415476212680896699407390026') - \
342
+ mpf('0.57743812698666990209897034525640369') * j
343
+ result = jtheta(3, z, q, 1)
344
+ assert(mpc_ae(result, res))
345
+
346
+ # Mathematica N[EllipticThetaPrime[4, 1/7 + I/3, 1/8 + I/5], 35]
347
+ res = mpf('-0.38936892528126996010818803742007352') + \
348
+ mpf('0.66549886179739128256269617407313625') * j
349
+ result = jtheta(4, z, q, 1)
350
+ assert(mpc_ae(result, res))
351
+
352
+ for i in range(10):
353
+ q = (one*random.random() + j*random.random())/2
354
+ # identity in Wittaker, Watson &21.41
355
+ a = jtheta(1, 0, q, 1)
356
+ b = jtheta(2, 0, q)*jtheta(3, 0, q)*jtheta(4, 0, q)
357
+ assert(a.ae(b))
358
+
359
+ # test higher derivatives
360
+ mp.dps = 20
361
+ for q,z in [(one/3, one/5), (one/3 + j/8, one/5),
362
+ (one/3, one/5 + j/8), (one/3 + j/7, one/5 + j/8)]:
363
+ for n in [1, 2, 3, 4]:
364
+ r = jtheta(n, z, q, 2)
365
+ r1 = diff(lambda zz: jtheta(n, zz, q), z, n=2)
366
+ assert r.ae(r1)
367
+ r = jtheta(n, z, q, 3)
368
+ r1 = diff(lambda zz: jtheta(n, zz, q), z, n=3)
369
+ assert r.ae(r1)
370
+
371
+ # identity in Wittaker, Watson &21.41
372
+ q = one/3
373
+ z = zero
374
+ a = [0]*5
375
+ a[1] = jtheta(1, z, q, 3)/jtheta(1, z, q, 1)
376
+ for n in [2,3,4]:
377
+ a[n] = jtheta(n, z, q, 2)/jtheta(n, z, q)
378
+ equality = a[2] + a[3] + a[4] - a[1]
379
+ assert(equality.ae(0))
380
+ mp.dps = 15
381
+
382
+ def test_jsn():
383
+ """
384
+ Test some special cases of the sn(z, q) function.
385
+ """
386
+ mp.dps = 100
387
+
388
+ # trival case
389
+ result = jsn(zero, zero)
390
+ assert(result == zero)
391
+
392
+ # Abramowitz Table 16.5
393
+ #
394
+ # sn(0, m) = 0
395
+
396
+ for i in range(10):
397
+ qstring = str(random.random())
398
+ q = mpf(qstring)
399
+
400
+ equality = jsn(zero, q)
401
+ assert(equality.ae(0))
402
+
403
+ # Abramowitz Table 16.6.1
404
+ #
405
+ # sn(z, 0) = sin(z), m == 0
406
+ #
407
+ # sn(z, 1) = tanh(z), m == 1
408
+ #
409
+ # It would be nice to test these, but I find that they run
410
+ # in to numerical trouble. I'm currently treating as a boundary
411
+ # case for sn function.
412
+
413
+ mp.dps = 25
414
+ arg = one/10
415
+ #N[JacobiSN[1/10, 2^-100], 25]
416
+ res = mpf('0.09983341664682815230681420')
417
+ m = ldexp(one, -100)
418
+ result = jsn(arg, m)
419
+ assert(result.ae(res))
420
+
421
+ # N[JacobiSN[1/10, 1/10], 25]
422
+ res = mpf('0.09981686718599080096451168')
423
+ result = jsn(arg, arg)
424
+ assert(result.ae(res))
425
+ mp.dps = 15
426
+
427
+ def test_jcn():
428
+ """
429
+ Test some special cases of the cn(z, q) function.
430
+ """
431
+ mp.dps = 100
432
+
433
+ # Abramowitz Table 16.5
434
+ # cn(0, q) = 1
435
+ qstring = str(random.random())
436
+ q = mpf(qstring)
437
+ cn = jcn(zero, q)
438
+ assert(cn.ae(one))
439
+
440
+ # Abramowitz Table 16.6.2
441
+ #
442
+ # cn(u, 0) = cos(u), m == 0
443
+ #
444
+ # cn(u, 1) = sech(z), m == 1
445
+ #
446
+ # It would be nice to test these, but I find that they run
447
+ # in to numerical trouble. I'm currently treating as a boundary
448
+ # case for cn function.
449
+
450
+ mp.dps = 25
451
+ arg = one/10
452
+ m = ldexp(one, -100)
453
+ #N[JacobiCN[1/10, 2^-100], 25]
454
+ res = mpf('0.9950041652780257660955620')
455
+ result = jcn(arg, m)
456
+ assert(result.ae(res))
457
+
458
+ # N[JacobiCN[1/10, 1/10], 25]
459
+ res = mpf('0.9950058256237368748520459')
460
+ result = jcn(arg, arg)
461
+ assert(result.ae(res))
462
+ mp.dps = 15
463
+
464
+ def test_jdn():
465
+ """
466
+ Test some special cases of the dn(z, q) function.
467
+ """
468
+ mp.dps = 100
469
+
470
+ # Abramowitz Table 16.5
471
+ # dn(0, q) = 1
472
+ mstring = str(random.random())
473
+ m = mpf(mstring)
474
+
475
+ dn = jdn(zero, m)
476
+ assert(dn.ae(one))
477
+
478
+ mp.dps = 25
479
+ # N[JacobiDN[1/10, 1/10], 25]
480
+ res = mpf('0.9995017055025556219713297')
481
+ arg = one/10
482
+ result = jdn(arg, arg)
483
+ assert(result.ae(res))
484
+ mp.dps = 15
485
+
486
+
487
+ def test_sn_cn_dn_identities():
488
+ """
489
+ Tests the some of the jacobi elliptic function identities found
490
+ on Mathworld. Haven't found in Abramowitz.
491
+ """
492
+ mp.dps = 100
493
+ N = 5
494
+ for i in range(N):
495
+ qstring = str(random.random())
496
+ q = mpf(qstring)
497
+ zstring = str(100*random.random())
498
+ z = mpf(zstring)
499
+
500
+ # MathWorld
501
+ # sn(z, q)**2 + cn(z, q)**2 == 1
502
+ term1 = jsn(z, q)**2
503
+ term2 = jcn(z, q)**2
504
+ equality = one - term1 - term2
505
+ assert(equality.ae(0))
506
+
507
+ # MathWorld
508
+ # k**2 * sn(z, m)**2 + dn(z, m)**2 == 1
509
+ for i in range(N):
510
+ mstring = str(random.random())
511
+ m = mpf(qstring)
512
+ k = m.sqrt()
513
+ zstring = str(10*random.random())
514
+ z = mpf(zstring)
515
+ term1 = k**2 * jsn(z, m)**2
516
+ term2 = jdn(z, m)**2
517
+ equality = one - term1 - term2
518
+ assert(equality.ae(0))
519
+
520
+
521
+ for i in range(N):
522
+ mstring = str(random.random())
523
+ m = mpf(mstring)
524
+ k = m.sqrt()
525
+ zstring = str(random.random())
526
+ z = mpf(zstring)
527
+
528
+ # MathWorld
529
+ # k**2 * cn(z, m)**2 + (1 - k**2) = dn(z, m)**2
530
+ term1 = k**2 * jcn(z, m)**2
531
+ term2 = 1 - k**2
532
+ term3 = jdn(z, m)**2
533
+ equality = term3 - term1 - term2
534
+ assert(equality.ae(0))
535
+
536
+ K = ellipk(k**2)
537
+ # Abramowitz Table 16.5
538
+ # sn(K, m) = 1; K is K(k), first complete elliptic integral
539
+ r = jsn(K, m)
540
+ assert(r.ae(one))
541
+
542
+ # Abramowitz Table 16.5
543
+ # cn(K, q) = 0; K is K(k), first complete elliptic integral
544
+ equality = jcn(K, m)
545
+ assert(equality.ae(0))
546
+
547
+ # Abramowitz Table 16.6.3
548
+ # dn(z, 0) = 1, m == 0
549
+ z = m
550
+ value = jdn(z, zero)
551
+ assert(value.ae(one))
552
+
553
+ mp.dps = 15
554
+
555
+ def test_sn_cn_dn_complex():
556
+ mp.dps = 30
557
+ # N[JacobiSN[1/4 + I/8, 1/3 + I/7], 35] in Mathematica
558
+ res = mpf('0.2495674401066275492326652143537') + \
559
+ mpf('0.12017344422863833381301051702823') * j
560
+ u = mpf(1)/4 + j/8
561
+ m = mpf(1)/3 + j/7
562
+ r = jsn(u, m)
563
+ assert(mpc_ae(r, res))
564
+
565
+ #N[JacobiCN[1/4 + I/8, 1/3 + I/7], 35]
566
+ res = mpf('0.9762691700944007312693721148331') - \
567
+ mpf('0.0307203994181623243583169154824')*j
568
+ r = jcn(u, m)
569
+ #assert r.real.ae(res.real)
570
+ #assert r.imag.ae(res.imag)
571
+ assert(mpc_ae(r, res))
572
+
573
+ #N[JacobiDN[1/4 + I/8, 1/3 + I/7], 35]
574
+ res = mpf('0.99639490163039577560547478589753039') - \
575
+ mpf('0.01346296520008176393432491077244994')*j
576
+ r = jdn(u, m)
577
+ assert(mpc_ae(r, res))
578
+ mp.dps = 15
579
+
580
+ def test_elliptic_integrals():
581
+ # Test cases from Carlson's paper
582
+ mp.dps = 15
583
+ assert elliprd(0,2,1).ae(1.7972103521033883112)
584
+ assert elliprd(2,3,4).ae(0.16510527294261053349)
585
+ assert elliprd(j,-j,2).ae(0.65933854154219768919)
586
+ assert elliprd(0,j,-j).ae(1.2708196271909686299 + 2.7811120159520578777j)
587
+ assert elliprd(0,j-1,j).ae(-1.8577235439239060056 - 0.96193450888838559989j)
588
+ assert elliprd(-2-j,-j,-1+j).ae(1.8249027393703805305 - 1.2218475784827035855j)
589
+ # extra test cases
590
+ assert elliprg(0,0,0) == 0
591
+ assert elliprg(0,0,16).ae(2)
592
+ assert elliprg(0,16,0).ae(2)
593
+ assert elliprg(16,0,0).ae(2)
594
+ assert elliprg(1,4,0).ae(1.2110560275684595248036)
595
+ assert elliprg(1,0,4).ae(1.2110560275684595248036)
596
+ assert elliprg(0,4,1).ae(1.2110560275684595248036)
597
+ # should be symmetric -- fixes a bug present in the paper
598
+ x,y,z = 1,1j,-1+1j
599
+ assert elliprg(x,y,z).ae(0.64139146875812627545 + 0.58085463774808290907j)
600
+ assert elliprg(x,z,y).ae(0.64139146875812627545 + 0.58085463774808290907j)
601
+ assert elliprg(y,x,z).ae(0.64139146875812627545 + 0.58085463774808290907j)
602
+ assert elliprg(y,z,x).ae(0.64139146875812627545 + 0.58085463774808290907j)
603
+ assert elliprg(z,x,y).ae(0.64139146875812627545 + 0.58085463774808290907j)
604
+ assert elliprg(z,y,x).ae(0.64139146875812627545 + 0.58085463774808290907j)
605
+
606
+ for n in [5, 15, 30, 60, 100]:
607
+ mp.dps = n
608
+ assert elliprf(1,2,0).ae('1.3110287771460599052324197949455597068413774757158115814084108519003952935352071251151477664807145467230678763')
609
+ assert elliprf(0.5,1,0).ae('1.854074677301371918433850347195260046217598823521766905585928045056021776838119978357271861650371897277771871')
610
+ assert elliprf(j,-j,0).ae('1.854074677301371918433850347195260046217598823521766905585928045056021776838119978357271861650371897277771871')
611
+ assert elliprf(j-1,j,0).ae(mpc('0.79612586584233913293056938229563057846592264089185680214929401744498956943287031832657642790719940442165621412',
612
+ '-1.2138566698364959864300942567386038975419875860741507618279563735753073152507112254567291141460317931258599889'))
613
+ assert elliprf(2,3,4).ae('0.58408284167715170669284916892566789240351359699303216166309375305508295130412919665541330837704050454472379308')
614
+ assert elliprf(j,-j,2).ae('1.0441445654064360931078658361850779139591660747973017593275012615517220315993723776182276555339288363064476126')
615
+ assert elliprf(j-1,j,1-j).ae(mpc('0.93912050218619371196624617169781141161485651998254431830645241993282941057500174238125105410055253623847335313',
616
+ '-0.53296252018635269264859303449447908970360344322834582313172115220559316331271520508208025270300138589669326136'))
617
+ assert elliprc(0,0.25).ae(+pi)
618
+ assert elliprc(2.25,2).ae(+ln2)
619
+ assert elliprc(0,j).ae(mpc('1.1107207345395915617539702475151734246536554223439225557713489017391086982748684776438317336911913093408525532',
620
+ '-1.1107207345395915617539702475151734246536554223439225557713489017391086982748684776438317336911913093408525532'))
621
+ assert elliprc(-j,j).ae(mpc('1.2260849569072198222319655083097718755633725139745941606203839524036426936825652935738621522906572884239069297',
622
+ '-0.34471136988767679699935618332997956653521218571295874986708834375026550946053920574015526038040124556716711353'))
623
+ assert elliprc(0.25,-2).ae(ln2/3)
624
+ assert elliprc(j,-1).ae(mpc('0.77778596920447389875196055840799837589537035343923012237628610795937014001905822029050288316217145443865649819',
625
+ '0.1983248499342877364755170948292130095921681309577950696116251029742793455964385947473103628983664877025779304'))
626
+ assert elliprj(0,1,2,3).ae('0.77688623778582332014190282640545501102298064276022952731669118325952563819813258230708177398475643634103990878')
627
+ assert elliprj(2,3,4,5).ae('0.14297579667156753833233879421985774801466647854232626336218889885463800128817976132826443904216546421431528308')
628
+ assert elliprj(2,3,4,-1+j).ae(mpc('0.13613945827770535203521374457913768360237593025944342652613569368333226052158214183059386307242563164036672709',
629
+ '-0.38207561624427164249600936454845112611060375760094156571007648297226090050927156176977091273224510621553615189'))
630
+ assert elliprj(j,-j,0,2).ae('1.6490011662710884518243257224860232300246792717163891216346170272567376981346412066066050103935109581019055806')
631
+ assert elliprj(-1+j,-1-j,1,2).ae('0.94148358841220238083044612133767270187474673547917988681610772381758628963408843935027667916713866133196845063')
632
+ assert elliprj(j,-j,0,1-j).ae(mpc('1.8260115229009316249372594065790946657011067182850435297162034335356430755397401849070610280860044610878657501',
633
+ '1.2290661908643471500163617732957042849283739403009556715926326841959667290840290081010472716420690899886276961'))
634
+ assert elliprj(-1+j,-1-j,1,-3+j).ae(mpc('-0.61127970812028172123588152373622636829986597243716610650831553882054127570542477508023027578037045504958619422',
635
+ '-1.0684038390006807880182112972232562745485871763154040245065581157751693730095703406209466903752930797510491155'))
636
+ assert elliprj(-1+j,-2-j,-j,-1+j).ae(mpc('1.8249027393703805304622013339009022294368078659619988943515764258335975852685224202567854526307030593012768954',
637
+ '-1.2218475784827035854568450371590419833166777535029296025352291308244564398645467465067845461070602841312456831'))
638
+
639
+ assert elliprg(0,16,16).ae(+pi)
640
+ assert elliprg(2,3,4).ae('1.7255030280692277601061148835701141842692457170470456590515892070736643637303053506944907685301315299153040991')
641
+ assert elliprg(0,j,-j).ae('0.42360654239698954330324956174109581824072295516347109253028968632986700241706737986160014699730561497106114281')
642
+ assert elliprg(j-1,j,0).ae(mpc('0.44660591677018372656731970402124510811555212083508861036067729944477855594654762496407405328607219895053798354',
643
+ '0.70768352357515390073102719507612395221369717586839400605901402910893345301718731499237159587077682267374159282'))
644
+ assert elliprg(-j,j-1,j).ae(mpc('0.36023392184473309033675652092928695596803358846377334894215349632203382573844427952830064383286995172598964266',
645
+ '0.40348623401722113740956336997761033878615232917480045914551915169013722542827052849476969199578321834819903921'))
646
+ assert elliprg(0, mpf('0.0796'), 4).ae('1.0284758090288040009838871385180217366569777284430590125081211090574701293154645750017813190805144572673802094')
647
+ mp.dps = 15
648
+
649
+ # more test cases for the branch of ellippi / elliprj
650
+ assert elliprj(-1-0.5j, -10-6j, -10-3j, -5+10j).ae(0.128470516743927699 + 0.102175950778504625j, abs_eps=1e-8)
651
+ assert elliprj(1.987, 4.463 - 1.614j, 0, -3.965).ae(-0.341575118513811305 - 0.394703757004268486j, abs_eps=1e-8)
652
+ assert elliprj(0.3068, -4.037+0.632j, 1.654, -0.9609).ae(-1.14735199581485639 - 0.134450158867472264j, abs_eps=1e-8)
653
+ assert elliprj(0.3068, -4.037-0.632j, 1.654, -0.9609).ae(1.758765901861727 - 0.161002343366626892j, abs_eps=1e-5)
654
+ assert elliprj(0.3068, -4.037+0.0632j, 1.654, -0.9609).ae(-1.17157627949475577 - 0.069182614173988811j, abs_eps=1e-8)
655
+ assert elliprj(0.3068, -4.037+0.00632j, 1.654, -0.9609).ae(-1.17337595670549633 - 0.0623069224526925j, abs_eps=1e-8)
656
+
657
+ # these require accurate integration
658
+ assert elliprj(0.3068, -4.037-0.0632j, 1.654, -0.9609).ae(1.77940452391261626 + 0.0388711305592447234j)
659
+ assert elliprj(0.3068, -4.037-0.00632j, 1.654, -0.9609).ae(1.77806722756403055 + 0.0592749824572262329j)
660
+ # issue #571
661
+ assert ellippi(2.1 + 0.94j, 2.3 + 0.98j, 2.5 + 0.01j).ae(-0.40652414240811963438 + 2.1547659461404749309j)
662
+
663
+ assert ellippi(2.0-1.0j, 2.0+1.0j).ae(1.8578723151271115 - 1.18642180609983531j)
664
+ assert ellippi(2.0-0.5j, 0.5+1.0j).ae(0.936761970766645807 - 1.61876787838890786j)
665
+ assert ellippi(2.0, 1.0+1.0j).ae(0.999881420735506708 - 2.4139272867045391j)
666
+ assert ellippi(2.0+1.0j, 2.0-1.0j).ae(1.8578723151271115 + 1.18642180609983531j)
667
+ assert ellippi(2.0+1.0j, 2.0).ae(2.78474654927885845 + 2.02204728966993314j)
668
+
669
+ def test_issue_238():
670
+ assert isnan(qfrom(m=nan))
llmeval-env/lib/python3.10/site-packages/mpmath/tests/test_fp.py ADDED
@@ -0,0 +1,1671 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Easy-to-use test-generating code:
3
+
4
+ cases = '''
5
+ exp 2.25
6
+ log 2.25
7
+ '''
8
+
9
+ from mpmath import *
10
+ mp.dps = 20
11
+ for test in cases.splitlines():
12
+ if not test:
13
+ continue
14
+ words = test.split()
15
+ fname = words[0]
16
+ args = words[1:]
17
+ argstr = ", ".join(args)
18
+ testline = "%s(%s)" % (fname, argstr)
19
+ ans = str(eval(testline))
20
+ print " assert ae(fp.%s, %s)" % (testline, ans)
21
+
22
+ """
23
+
24
+ from mpmath import fp
25
+
26
+ def ae(x, y, tol=1e-12):
27
+ if x == y:
28
+ return True
29
+ return abs(x-y) <= tol*abs(y)
30
+
31
+ def test_conj():
32
+ assert fp.conj(4) == 4
33
+ assert fp.conj(3+4j) == 3-4j
34
+ assert fp.fdot([1,2],[3,2+1j], conjugate=True) == 7-2j
35
+
36
+ def test_fp_number_parts():
37
+ assert ae(fp.arg(3), 0.0)
38
+ assert ae(fp.arg(-3), 3.1415926535897932385)
39
+ assert ae(fp.arg(3j), 1.5707963267948966192)
40
+ assert ae(fp.arg(-3j), -1.5707963267948966192)
41
+ assert ae(fp.arg(2+3j), 0.98279372324732906799)
42
+ assert ae(fp.arg(-1-1j), -2.3561944901923449288)
43
+ assert ae(fp.re(2.5), 2.5)
44
+ assert ae(fp.re(2.5+3j), 2.5)
45
+ assert ae(fp.im(2.5), 0.0)
46
+ assert ae(fp.im(2.5+3j), 3.0)
47
+ assert ae(fp.floor(2.5), 2.0)
48
+ assert ae(fp.floor(2), 2.0)
49
+ assert ae(fp.floor(2.0+0j), (2.0 + 0.0j))
50
+ assert ae(fp.floor(-1.5-0.5j), (-2.0 - 1.0j))
51
+ assert ae(fp.ceil(2.5), 3.0)
52
+ assert ae(fp.ceil(2), 2.0)
53
+ assert ae(fp.ceil(2.0+0j), (2.0 + 0.0j))
54
+ assert ae(fp.ceil(-1.5-0.5j), (-1.0 + 0.0j))
55
+
56
+ def test_fp_cospi_sinpi():
57
+ assert ae(fp.sinpi(0), 0.0)
58
+ assert ae(fp.sinpi(0.25), 0.7071067811865475244)
59
+ assert ae(fp.sinpi(0.5), 1.0)
60
+ assert ae(fp.sinpi(0.75), 0.7071067811865475244)
61
+ assert ae(fp.sinpi(1), 0.0)
62
+ assert ae(fp.sinpi(1.25), -0.7071067811865475244)
63
+ assert ae(fp.sinpi(1.5), -1.0)
64
+ assert ae(fp.sinpi(1.75), -0.7071067811865475244)
65
+ assert ae(fp.sinpi(2), 0.0)
66
+ assert ae(fp.sinpi(2.25), 0.7071067811865475244)
67
+ assert ae(fp.sinpi(0+3j), (0.0 + 6195.8238636085899556j))
68
+ assert ae(fp.sinpi(0.25+3j), (4381.1091260582448033 + 4381.1090689950686908j))
69
+ assert ae(fp.sinpi(0.5+3j), (6195.8239443081075259 + 0.0j))
70
+ assert ae(fp.sinpi(0.75+3j), (4381.1091260582448033 - 4381.1090689950686908j))
71
+ assert ae(fp.sinpi(1+3j), (0.0 - 6195.8238636085899556j))
72
+ assert ae(fp.sinpi(1.25+3j), (-4381.1091260582448033 - 4381.1090689950686908j))
73
+ assert ae(fp.sinpi(1.5+3j), (-6195.8239443081075259 + 0.0j))
74
+ assert ae(fp.sinpi(1.75+3j), (-4381.1091260582448033 + 4381.1090689950686908j))
75
+ assert ae(fp.sinpi(2+3j), (0.0 + 6195.8238636085899556j))
76
+ assert ae(fp.sinpi(2.25+3j), (4381.1091260582448033 + 4381.1090689950686908j))
77
+ assert ae(fp.sinpi(-0.75), -0.7071067811865475244)
78
+ assert ae(fp.sinpi(-1e-10), -3.1415926535897933529e-10)
79
+ assert ae(fp.sinpi(1e-10), 3.1415926535897933529e-10)
80
+ assert ae(fp.sinpi(1e-10+1e-10j), (3.141592653589793353e-10 + 3.1415926535897933528e-10j))
81
+ assert ae(fp.sinpi(1e-10-1e-10j), (3.141592653589793353e-10 - 3.1415926535897933528e-10j))
82
+ assert ae(fp.sinpi(-1e-10+1e-10j), (-3.141592653589793353e-10 + 3.1415926535897933528e-10j))
83
+ assert ae(fp.sinpi(-1e-10-1e-10j), (-3.141592653589793353e-10 - 3.1415926535897933528e-10j))
84
+ assert ae(fp.cospi(0), 1.0)
85
+ assert ae(fp.cospi(0.25), 0.7071067811865475244)
86
+ assert ae(fp.cospi(0.5), 0.0)
87
+ assert ae(fp.cospi(0.75), -0.7071067811865475244)
88
+ assert ae(fp.cospi(1), -1.0)
89
+ assert ae(fp.cospi(1.25), -0.7071067811865475244)
90
+ assert ae(fp.cospi(1.5), 0.0)
91
+ assert ae(fp.cospi(1.75), 0.7071067811865475244)
92
+ assert ae(fp.cospi(2), 1.0)
93
+ assert ae(fp.cospi(2.25), 0.7071067811865475244)
94
+ assert ae(fp.cospi(0+3j), (6195.8239443081075259 + 0.0j))
95
+ assert ae(fp.cospi(0.25+3j), (4381.1091260582448033 - 4381.1090689950686908j))
96
+ assert ae(fp.cospi(0.5+3j), (0.0 - 6195.8238636085899556j))
97
+ assert ae(fp.cospi(0.75+3j), (-4381.1091260582448033 - 4381.1090689950686908j))
98
+ assert ae(fp.cospi(1+3j), (-6195.8239443081075259 + 0.0j))
99
+ assert ae(fp.cospi(1.25+3j), (-4381.1091260582448033 + 4381.1090689950686908j))
100
+ assert ae(fp.cospi(1.5+3j), (0.0 + 6195.8238636085899556j))
101
+ assert ae(fp.cospi(1.75+3j), (4381.1091260582448033 + 4381.1090689950686908j))
102
+ assert ae(fp.cospi(2+3j), (6195.8239443081075259 + 0.0j))
103
+ assert ae(fp.cospi(2.25+3j), (4381.1091260582448033 - 4381.1090689950686908j))
104
+ assert ae(fp.cospi(-0.75), -0.7071067811865475244)
105
+ assert ae(fp.sinpi(-0.7), -0.80901699437494750611)
106
+ assert ae(fp.cospi(-0.7), -0.5877852522924730163)
107
+ assert ae(fp.cospi(-3+2j), (-267.74676148374822225 + 0.0j))
108
+ assert ae(fp.sinpi(-3+2j), (0.0 - 267.74489404101651426j))
109
+ assert ae(fp.sinpi(-0.7+2j), (-216.6116802292079471 - 157.37650009392034693j))
110
+ assert ae(fp.cospi(-0.7+2j), (-157.37759774921754565 + 216.61016943630197336j))
111
+
112
+ def test_fp_expj():
113
+ assert ae(fp.expj(0), (1.0 + 0.0j))
114
+ assert ae(fp.expj(1), (0.5403023058681397174 + 0.84147098480789650665j))
115
+ assert ae(fp.expj(2), (-0.416146836547142387 + 0.9092974268256816954j))
116
+ assert ae(fp.expj(0.75), (0.73168886887382088631 + 0.68163876002333416673j))
117
+ assert ae(fp.expj(2+3j), (-0.020718731002242879378 + 0.045271253156092975488j))
118
+ assert ae(fp.expjpi(0), (1.0 + 0.0j))
119
+ assert ae(fp.expjpi(1), (-1.0 + 0.0j))
120
+ assert ae(fp.expjpi(2), (1.0 + 0.0j))
121
+ assert ae(fp.expjpi(0.75), (-0.7071067811865475244 + 0.7071067811865475244j))
122
+ assert ae(fp.expjpi(2+3j), (0.000080699517570304599239 + 0.0j))
123
+
124
+ def test_fp_bernoulli():
125
+ assert ae(fp.bernoulli(0), 1.0)
126
+ assert ae(fp.bernoulli(1), -0.5)
127
+ assert ae(fp.bernoulli(2), 0.16666666666666666667)
128
+ assert ae(fp.bernoulli(10), 0.075757575757575757576)
129
+ assert ae(fp.bernoulli(11), 0.0)
130
+
131
+ def test_fp_gamma():
132
+ assert ae(fp.gamma(1), 1.0)
133
+ assert ae(fp.gamma(1.5), 0.88622692545275801365)
134
+ assert ae(fp.gamma(10), 362880.0)
135
+ assert ae(fp.gamma(-0.5), -3.5449077018110320546)
136
+ assert ae(fp.gamma(-7.1), 0.0016478244570263333622)
137
+ assert ae(fp.gamma(12.3), 83385367.899970000963)
138
+ assert ae(fp.gamma(2+0j), (1.0 + 0.0j))
139
+ assert ae(fp.gamma(-2.5+0j), (-0.94530872048294188123 + 0.0j))
140
+ assert ae(fp.gamma(3+4j), (0.0052255384713692141947 - 0.17254707929430018772j))
141
+ assert ae(fp.gamma(-3-4j), (0.00001460997305874775607 - 0.000020760733311509070396j))
142
+ assert ae(fp.fac(0), 1.0)
143
+ assert ae(fp.fac(1), 1.0)
144
+ assert ae(fp.fac(20), 2432902008176640000.0)
145
+ assert ae(fp.fac(-3.5), -0.94530872048294188123)
146
+ assert ae(fp.fac(2+3j), (-0.44011340763700171113 - 0.06363724312631702183j))
147
+ assert ae(fp.loggamma(1.0), 0.0)
148
+ assert ae(fp.loggamma(2.0), 0.0)
149
+ assert ae(fp.loggamma(3.0), 0.69314718055994530942)
150
+ assert ae(fp.loggamma(7.25), 7.0521854507385394449)
151
+ assert ae(fp.loggamma(1000.0), 5905.2204232091812118)
152
+ assert ae(fp.loggamma(1e50), 1.1412925464970229298e+52)
153
+ assert ae(fp.loggamma(1e25+1e25j), (5.6125802751733671621e+26 + 5.7696599078528568383e+26j))
154
+ assert ae(fp.loggamma(3+4j), (-1.7566267846037841105 + 4.7426644380346579282j))
155
+ assert ae(fp.loggamma(-0.5), (1.2655121234846453965 - 3.1415926535897932385j))
156
+ assert ae(fp.loggamma(-1.25), (1.3664317612369762346 - 6.2831853071795864769j))
157
+ assert ae(fp.loggamma(-2.75), (0.0044878975359557733115 - 9.4247779607693797154j))
158
+ assert ae(fp.loggamma(-3.5), (-1.3090066849930420464 - 12.566370614359172954j))
159
+ assert ae(fp.loggamma(-4.5), (-2.8130840817693161197 - 15.707963267948966192j))
160
+ assert ae(fp.loggamma(-2+3j), (-6.776523813485657093 - 4.568791367260286402j))
161
+ assert ae(fp.loggamma(-1000.3), (-5912.8440347785205041 - 3144.7342462433830317j))
162
+ assert ae(fp.loggamma(-100-100j), (-632.35117666833135562 - 158.37641469650352462j))
163
+ assert ae(fp.loggamma(1e-10), 23.025850929882735237)
164
+ assert ae(fp.loggamma(-1e-10), (23.02585092999817837 - 3.1415926535897932385j))
165
+ assert ae(fp.loggamma(1e-10j), (23.025850929940456804 - 1.5707963268526181857j))
166
+ assert ae(fp.loggamma(1e-10j-1e-10), (22.679277339718205716 - 2.3561944902500664954j))
167
+
168
+ def test_fp_psi():
169
+ assert ae(fp.psi(0, 3.7), 1.1671535393615114409)
170
+ assert ae(fp.psi(0, 0.5), -1.9635100260214234794)
171
+ assert ae(fp.psi(0, 1), -0.57721566490153286061)
172
+ assert ae(fp.psi(0, -2.5), 1.1031566406452431872)
173
+ assert ae(fp.psi(0, 12.9), 2.5179671503279156347)
174
+ assert ae(fp.psi(0, 100), 4.6001618527380874002)
175
+ assert ae(fp.psi(0, 2500.3), 7.8239660143238547877)
176
+ assert ae(fp.psi(0, 1e40), 92.103403719761827391)
177
+ assert ae(fp.psi(0, 1e200), 460.51701859880913677)
178
+ assert ae(fp.psi(0, 3.7+0j), (1.1671535393615114409 + 0.0j))
179
+ assert ae(fp.psi(1, 3), 0.39493406684822643647)
180
+ assert ae(fp.psi(3, 2+3j), (-0.05383196209159972116 + 0.0076890935247364805218j))
181
+ assert ae(fp.psi(4, -0.5+1j), (1.2719531355492328195 - 18.211833410936276774j))
182
+ assert ae(fp.harmonic(0), 0.0)
183
+ assert ae(fp.harmonic(1), 1.0)
184
+ assert ae(fp.harmonic(2), 1.5)
185
+ assert ae(fp.harmonic(100), 5.1873775176396202608)
186
+ assert ae(fp.harmonic(-2.5), 1.2803723055467760478)
187
+ assert ae(fp.harmonic(2+3j), (1.9390425294578375875 + 0.87336044981834544043j))
188
+ assert ae(fp.harmonic(-5-4j), (2.3725754822349437733 - 2.4160904444801621j))
189
+
190
+ def test_fp_zeta():
191
+ assert ae(fp.zeta(1e100), 1.0)
192
+ assert ae(fp.zeta(3), 1.2020569031595942854)
193
+ assert ae(fp.zeta(2+0j), (1.6449340668482264365 + 0.0j))
194
+ assert ae(fp.zeta(0.93), -13.713619351638164784)
195
+ assert ae(fp.zeta(1.74), 1.9796863545771774095)
196
+ assert ae(fp.zeta(0.0), -0.5)
197
+ assert ae(fp.zeta(-1.0), -0.083333333333333333333)
198
+ assert ae(fp.zeta(-2.0), 0.0)
199
+ assert ae(fp.zeta(-3.0), 0.0083333333333333333333)
200
+ assert ae(fp.zeta(-500.0), 0.0)
201
+ assert ae(fp.zeta(-7.4), 0.0036537321227995882447)
202
+ assert ae(fp.zeta(2.1), 1.5602165335033620158)
203
+ assert ae(fp.zeta(26.9), 1.0000000079854809935)
204
+ assert ae(fp.zeta(26), 1.0000000149015548284)
205
+ assert ae(fp.zeta(27), 1.0000000074507117898)
206
+ assert ae(fp.zeta(28), 1.0000000037253340248)
207
+ assert ae(fp.zeta(27.1), 1.000000006951755045)
208
+ assert ae(fp.zeta(32.7), 1.0000000001433243232)
209
+ assert ae(fp.zeta(100), 1.0)
210
+ assert ae(fp.altzeta(3.5), 0.92755357777394803511)
211
+ assert ae(fp.altzeta(1), 0.69314718055994530942)
212
+ assert ae(fp.altzeta(2), 0.82246703342411321824)
213
+ assert ae(fp.altzeta(0), 0.5)
214
+ assert ae(fp.zeta(-2+3j, 1), (0.13297115587929864827 + 0.12305330040458776494j))
215
+ assert ae(fp.zeta(-2+3j, 5), (18.384866151867576927 - 11.377015110597711009j))
216
+ assert ae(fp.zeta(1.0000000001), 9999999173.1735741337)
217
+ assert ae(fp.zeta(0.9999999999), -9999999172.0191428039)
218
+ assert ae(fp.zeta(1+0.000000001j), (0.57721566490153286061 - 999999999.99999993765j))
219
+ assert ae(fp.primezeta(2.5+4j), (-0.16922458243438033385 - 0.010847965298387727811j))
220
+ assert ae(fp.primezeta(4), 0.076993139764246844943)
221
+ assert ae(fp.riemannr(3.7), 2.3034079839110855717)
222
+ assert ae(fp.riemannr(8), 3.9011860449341499474)
223
+ assert ae(fp.riemannr(3+4j), (2.2369653314259991796 + 1.6339943856990281694j))
224
+
225
+ def test_fp_hyp2f1():
226
+ assert ae(fp.hyp2f1(1, (3,2), 3.25, 5.0), (-0.46600275923108143059 - 0.74393667908854842325j))
227
+ assert ae(fp.hyp2f1(1+1j, (3,2), 3.25, 5.0), (-5.9208875603806515987 - 2.3813557707889590686j))
228
+ assert ae(fp.hyp2f1(1+1j, (3,2), 3.25, 2+3j), (0.17174552030925080445 + 0.19589781970539389999j))
229
+
230
+ def test_fp_erf():
231
+ assert fp.erf(2) == fp.erf(2.0) == fp.erf(2.0+0.0j)
232
+ assert fp.erf(fp.inf) == 1.0
233
+ assert fp.erf(fp.ninf) == -1.0
234
+ assert ae(fp.erf(0), 0.0)
235
+ assert ae(fp.erf(-0), -0.0)
236
+ assert ae(fp.erf(0.3), 0.32862675945912741619)
237
+ assert ae(fp.erf(-0.3), -0.32862675945912741619)
238
+ assert ae(fp.erf(0.9), 0.79690821242283213966)
239
+ assert ae(fp.erf(-0.9), -0.79690821242283213966)
240
+ assert ae(fp.erf(1.0), 0.84270079294971486934)
241
+ assert ae(fp.erf(-1.0), -0.84270079294971486934)
242
+ assert ae(fp.erf(1.1), 0.88020506957408172966)
243
+ assert ae(fp.erf(-1.1), -0.88020506957408172966)
244
+ assert ae(fp.erf(8.5), 1.0)
245
+ assert ae(fp.erf(-8.5), -1.0)
246
+ assert ae(fp.erf(9.1), 1.0)
247
+ assert ae(fp.erf(-9.1), -1.0)
248
+ assert ae(fp.erf(20.0), 1.0)
249
+ assert ae(fp.erf(-20.0), -1.0)
250
+ assert ae(fp.erf(10000.0), 1.0)
251
+ assert ae(fp.erf(-10000.0), -1.0)
252
+ assert ae(fp.erf(1e+50), 1.0)
253
+ assert ae(fp.erf(-1e+50), -1.0)
254
+ assert ae(fp.erf(1j), 1.650425758797542876j)
255
+ assert ae(fp.erf(-1j), -1.650425758797542876j)
256
+ assert ae(fp.erf((2+3j)), (-20.829461427614568389 + 8.6873182714701631444j))
257
+ assert ae(fp.erf(-(2+3j)), -(-20.829461427614568389 + 8.6873182714701631444j))
258
+ assert ae(fp.erf((8+9j)), (-1072004.2525062051158 + 364149.91954310255423j))
259
+ assert ae(fp.erf(-(8+9j)), -(-1072004.2525062051158 + 364149.91954310255423j))
260
+ assert fp.erfc(fp.inf) == 0.0
261
+ assert fp.erfc(fp.ninf) == 2.0
262
+ assert fp.erfc(0) == 1
263
+ assert fp.erfc(-0.0) == 1
264
+ assert fp.erfc(0+0j) == 1
265
+ assert ae(fp.erfc(0.3), 0.67137324054087258381)
266
+ assert ae(fp.erfc(-0.3), 1.3286267594591274162)
267
+ assert ae(fp.erfc(0.9), 0.20309178757716786034)
268
+ assert ae(fp.erfc(-0.9), 1.7969082124228321397)
269
+ assert ae(fp.erfc(1.0), 0.15729920705028513066)
270
+ assert ae(fp.erfc(-1.0), 1.8427007929497148693)
271
+ assert ae(fp.erfc(1.1), 0.11979493042591827034)
272
+ assert ae(fp.erfc(-1.1), 1.8802050695740817297)
273
+ assert ae(fp.erfc(8.5), 2.7623240713337714461e-33)
274
+ assert ae(fp.erfc(-8.5), 2.0)
275
+ assert ae(fp.erfc(9.1), 6.6969004279886077452e-38)
276
+ assert ae(fp.erfc(-9.1), 2.0)
277
+ assert ae(fp.erfc(20.0), 5.3958656116079009289e-176)
278
+ assert ae(fp.erfc(-20.0), 2.0)
279
+ assert ae(fp.erfc(10000.0), 0.0)
280
+ assert ae(fp.erfc(-10000.0), 2.0)
281
+ assert ae(fp.erfc(1e+50), 0.0)
282
+ assert ae(fp.erfc(-1e+50), 2.0)
283
+ assert ae(fp.erfc(1j), (1.0 - 1.650425758797542876j))
284
+ assert ae(fp.erfc(-1j), (1.0 + 1.650425758797542876j))
285
+ assert ae(fp.erfc((2+3j)), (21.829461427614568389 - 8.6873182714701631444j), 1e-13)
286
+ assert ae(fp.erfc(-(2+3j)), (-19.829461427614568389 + 8.6873182714701631444j), 1e-13)
287
+ assert ae(fp.erfc((8+9j)), (1072005.2525062051158 - 364149.91954310255423j))
288
+ assert ae(fp.erfc(-(8+9j)), (-1072003.2525062051158 + 364149.91954310255423j))
289
+ assert ae(fp.erfc(20+0j), (5.3958656116079009289e-176 + 0.0j))
290
+
291
+ def test_fp_lambertw():
292
+ assert ae(fp.lambertw(0.0), 0.0)
293
+ assert ae(fp.lambertw(1.0), 0.567143290409783873)
294
+ assert ae(fp.lambertw(7.5), 1.5662309537823875394)
295
+ assert ae(fp.lambertw(-0.25), -0.35740295618138890307)
296
+ assert ae(fp.lambertw(-10.0), (1.3699809685212708156 + 2.140194527074713196j))
297
+ assert ae(fp.lambertw(0+0j), (0.0 + 0.0j))
298
+ assert ae(fp.lambertw(4+0j), (1.2021678731970429392 + 0.0j))
299
+ assert ae(fp.lambertw(1000.5), 5.2500227450408980127)
300
+ assert ae(fp.lambertw(1e100), 224.84310644511850156)
301
+ assert ae(fp.lambertw(-1000.0), (5.1501630246362515223 + 2.6641981432905204596j))
302
+ assert ae(fp.lambertw(1e-10), 9.9999999990000003645e-11)
303
+ assert ae(fp.lambertw(1e-10j), (1.0000000000000000728e-20 + 1.0000000000000000364e-10j))
304
+ assert ae(fp.lambertw(3+4j), (1.2815618061237758782 + 0.53309522202097107131j))
305
+ assert ae(fp.lambertw(-3-4j), (1.0750730665692549276 - 1.3251023817343588823j))
306
+ assert ae(fp.lambertw(10000+1000j), (7.2361526563371602186 + 0.087567810943839352034j))
307
+ assert ae(fp.lambertw(0.0, -1), -fp.inf)
308
+ assert ae(fp.lambertw(1.0, -1), (-1.5339133197935745079 - 4.3751851530618983855j))
309
+ assert ae(fp.lambertw(7.5, -1), (0.44125668415098614999 - 4.8039842008452390179j))
310
+ assert ae(fp.lambertw(-0.25, -1), -2.1532923641103496492)
311
+ assert ae(fp.lambertw(-10.0, -1), (1.3699809685212708156 - 2.140194527074713196j))
312
+ assert ae(fp.lambertw(0+0j, -1), -fp.inf)
313
+ assert ae(fp.lambertw(4+0j, -1), (-0.15730793189620765317 - 4.6787800704666656212j))
314
+ assert ae(fp.lambertw(1000.5, -1), (4.9153765415404024736 - 5.4465682700815159569j))
315
+ assert ae(fp.lambertw(1e100, -1), (224.84272130101601052 - 6.2553713838167244141j))
316
+ assert ae(fp.lambertw(-1000.0, -1), (5.1501630246362515223 - 2.6641981432905204596j))
317
+ assert ae(fp.lambertw(1e-10, -1), (-26.303186778379041521 - 3.2650939117038283975j))
318
+ assert ae(fp.lambertw(1e-10j, -1), (-26.297238779529035028 - 1.6328071613455765135j))
319
+ assert ae(fp.lambertw(3+4j, -1), (0.25856740686699741676 - 3.8521166861614355895j))
320
+ assert ae(fp.lambertw(-3-4j, -1), (-0.32028750204310768396 - 6.8801677192091972343j))
321
+ assert ae(fp.lambertw(10000+1000j, -1), (7.0255308742285435567 - 5.5177506835734067601j))
322
+ assert ae(fp.lambertw(0.0, 2), -fp.inf)
323
+ assert ae(fp.lambertw(1.0, 2), (-2.4015851048680028842 + 10.776299516115070898j))
324
+ assert ae(fp.lambertw(7.5, 2), (-0.38003357962843791529 + 10.960916473368746184j))
325
+ assert ae(fp.lambertw(-0.25, 2), (-4.0558735269061511898 + 13.852334658567271386j))
326
+ assert ae(fp.lambertw(-10.0, 2), (-0.34479123764318858696 + 14.112740596763592363j))
327
+ assert ae(fp.lambertw(0+0j, 2), -fp.inf)
328
+ assert ae(fp.lambertw(4+0j, 2), (-1.0070343323804262788 + 10.903476551861683082j))
329
+ assert ae(fp.lambertw(1000.5, 2), (4.4076185165459395295 + 11.365524591091402177j))
330
+ assert ae(fp.lambertw(1e100, 2), (224.84156762724875878 + 12.510785262632255672j))
331
+ assert ae(fp.lambertw(-1000.0, 2), (4.1984245610246530756 + 14.420478573754313845j))
332
+ assert ae(fp.lambertw(1e-10, 2), (-26.362258095445866488 + 9.7800247407031482519j))
333
+ assert ae(fp.lambertw(1e-10j, 2), (-26.384250801683084252 + 11.403535950607739763j))
334
+ assert ae(fp.lambertw(3+4j, 2), (-0.86554679943333993562 + 11.849956798331992027j))
335
+ assert ae(fp.lambertw(-3-4j, 2), (-0.55792273874679112639 + 8.7173627024159324811j))
336
+ assert ae(fp.lambertw(10000+1000j, 2), (6.6223802254585662734 + 11.61348646825020766j))
337
+
338
+ def test_fp_stress_ei_e1():
339
+ # Can be tightened on recent Pythons with more accurate math/cmath
340
+ ATOL = 1e-13
341
+ PTOL = 1e-12
342
+ v = fp.e1(1.1641532182693481445e-10)
343
+ assert ae(v, 22.296641293693077672, tol=ATOL)
344
+ assert type(v) is float
345
+ v = fp.e1(0.25)
346
+ assert ae(v, 1.0442826344437381945, tol=ATOL)
347
+ assert type(v) is float
348
+ v = fp.e1(1.0)
349
+ assert ae(v, 0.21938393439552027368, tol=ATOL)
350
+ assert type(v) is float
351
+ v = fp.e1(2.0)
352
+ assert ae(v, 0.048900510708061119567, tol=ATOL)
353
+ assert type(v) is float
354
+ v = fp.e1(5.0)
355
+ assert ae(v, 0.0011482955912753257973, tol=ATOL)
356
+ assert type(v) is float
357
+ v = fp.e1(20.0)
358
+ assert ae(v, 9.8355252906498816904e-11, tol=ATOL)
359
+ assert type(v) is float
360
+ v = fp.e1(30.0)
361
+ assert ae(v, 3.0215520106888125448e-15, tol=ATOL)
362
+ assert type(v) is float
363
+ v = fp.e1(40.0)
364
+ assert ae(v, 1.0367732614516569722e-19, tol=ATOL)
365
+ assert type(v) is float
366
+ v = fp.e1(50.0)
367
+ assert ae(v, 3.7832640295504590187e-24, tol=ATOL)
368
+ assert type(v) is float
369
+ v = fp.e1(80.0)
370
+ assert ae(v, 2.2285432586884729112e-37, tol=ATOL)
371
+ assert type(v) is float
372
+ v = fp.e1((1.1641532182693481445e-10 + 0.0j))
373
+ assert ae(v, (22.296641293693077672 + 0.0j), tol=ATOL)
374
+ assert ae(v.real, 22.296641293693077672, tol=PTOL)
375
+ assert v.imag == 0
376
+ v = fp.e1((0.25 + 0.0j))
377
+ assert ae(v, (1.0442826344437381945 + 0.0j), tol=ATOL)
378
+ assert ae(v.real, 1.0442826344437381945, tol=PTOL)
379
+ assert v.imag == 0
380
+ v = fp.e1((1.0 + 0.0j))
381
+ assert ae(v, (0.21938393439552027368 + 0.0j), tol=ATOL)
382
+ assert ae(v.real, 0.21938393439552027368, tol=PTOL)
383
+ assert v.imag == 0
384
+ v = fp.e1((2.0 + 0.0j))
385
+ assert ae(v, (0.048900510708061119567 + 0.0j), tol=ATOL)
386
+ assert ae(v.real, 0.048900510708061119567, tol=PTOL)
387
+ assert v.imag == 0
388
+ v = fp.e1((5.0 + 0.0j))
389
+ assert ae(v, (0.0011482955912753257973 + 0.0j), tol=ATOL)
390
+ assert ae(v.real, 0.0011482955912753257973, tol=PTOL)
391
+ assert v.imag == 0
392
+ v = fp.e1((20.0 + 0.0j))
393
+ assert ae(v, (9.8355252906498816904e-11 + 0.0j), tol=ATOL)
394
+ assert ae(v.real, 9.8355252906498816904e-11, tol=PTOL)
395
+ assert v.imag == 0
396
+ v = fp.e1((30.0 + 0.0j))
397
+ assert ae(v, (3.0215520106888125448e-15 + 0.0j), tol=ATOL)
398
+ assert ae(v.real, 3.0215520106888125448e-15, tol=PTOL)
399
+ assert v.imag == 0
400
+ v = fp.e1((40.0 + 0.0j))
401
+ assert ae(v, (1.0367732614516569722e-19 + 0.0j), tol=ATOL)
402
+ assert ae(v.real, 1.0367732614516569722e-19, tol=PTOL)
403
+ assert v.imag == 0
404
+ v = fp.e1((50.0 + 0.0j))
405
+ assert ae(v, (3.7832640295504590187e-24 + 0.0j), tol=ATOL)
406
+ assert ae(v.real, 3.7832640295504590187e-24, tol=PTOL)
407
+ assert v.imag == 0
408
+ v = fp.e1((80.0 + 0.0j))
409
+ assert ae(v, (2.2285432586884729112e-37 + 0.0j), tol=ATOL)
410
+ assert ae(v.real, 2.2285432586884729112e-37, tol=PTOL)
411
+ assert v.imag == 0
412
+ v = fp.e1((4.6566128730773925781e-10 + 1.1641532182693481445e-10j))
413
+ assert ae(v, (20.880034622014215597 - 0.24497866301044883237j), tol=ATOL)
414
+ assert ae(v.real, 20.880034622014215597, tol=PTOL)
415
+ assert ae(v.imag, -0.24497866301044883237, tol=PTOL)
416
+ v = fp.e1((1.0 + 0.25j))
417
+ assert ae(v, (0.19731063945004229095 - 0.087366045774299963672j), tol=ATOL)
418
+ assert ae(v.real, 0.19731063945004229095, tol=PTOL)
419
+ assert ae(v.imag, -0.087366045774299963672, tol=PTOL)
420
+ v = fp.e1((4.0 + 1.0j))
421
+ assert ae(v, (0.0013106173980145506944 - 0.0034542480199350626699j), tol=ATOL)
422
+ assert ae(v.real, 0.0013106173980145506944, tol=PTOL)
423
+ assert ae(v.imag, -0.0034542480199350626699, tol=PTOL)
424
+ v = fp.e1((8.0 + 2.0j))
425
+ assert ae(v, (-0.000022278049065270225945 - 0.000029191940456521555288j), tol=ATOL)
426
+ assert ae(v.real, -0.000022278049065270225945, tol=PTOL)
427
+ assert ae(v.imag, -0.000029191940456521555288, tol=PTOL)
428
+ v = fp.e1((20.0 + 5.0j))
429
+ assert ae(v, (4.7711374515765346894e-11 + 8.2902652405126947359e-11j), tol=ATOL)
430
+ assert ae(v.real, 4.7711374515765346894e-11, tol=PTOL)
431
+ assert ae(v.imag, 8.2902652405126947359e-11, tol=PTOL)
432
+ v = fp.e1((80.0 + 20.0j))
433
+ assert ae(v, (3.8353473865788235787e-38 - 2.129247592349605139e-37j), tol=ATOL)
434
+ assert ae(v.real, 3.8353473865788235787e-38, tol=PTOL)
435
+ assert ae(v.imag, -2.129247592349605139e-37, tol=PTOL)
436
+ v = fp.e1((120.0 + 30.0j))
437
+ assert ae(v, (2.3836002337480334716e-55 + 5.6704043587126198306e-55j), tol=ATOL)
438
+ assert ae(v.real, 2.3836002337480334716e-55, tol=PTOL)
439
+ assert ae(v.imag, 5.6704043587126198306e-55, tol=PTOL)
440
+ v = fp.e1((160.0 + 40.0j))
441
+ assert ae(v, (-1.6238022898654510661e-72 - 1.104172355572287367e-72j), tol=ATOL)
442
+ assert ae(v.real, -1.6238022898654510661e-72, tol=PTOL)
443
+ assert ae(v.imag, -1.104172355572287367e-72, tol=PTOL)
444
+ v = fp.e1((200.0 + 50.0j))
445
+ assert ae(v, (6.6800061461666228487e-90 + 1.4473816083541016115e-91j), tol=ATOL)
446
+ assert ae(v.real, 6.6800061461666228487e-90, tol=PTOL)
447
+ assert ae(v.imag, 1.4473816083541016115e-91, tol=PTOL)
448
+ v = fp.e1((320.0 + 80.0j))
449
+ assert ae(v, (4.2737871527778786157e-143 + 3.1789935525785660314e-142j), tol=ATOL)
450
+ assert ae(v.real, 4.2737871527778786157e-143, tol=PTOL)
451
+ assert ae(v.imag, 3.1789935525785660314e-142, tol=PTOL)
452
+ v = fp.e1((1.1641532182693481445e-10 + 1.1641532182693481445e-10j))
453
+ assert ae(v, (21.950067703413105017 - 0.7853981632810329878j), tol=ATOL)
454
+ assert ae(v.real, 21.950067703413105017, tol=PTOL)
455
+ assert ae(v.imag, -0.7853981632810329878, tol=PTOL)
456
+ v = fp.e1((0.25 + 0.25j))
457
+ assert ae(v, (0.71092525792923287894 - 0.56491812441304194711j), tol=ATOL)
458
+ assert ae(v.real, 0.71092525792923287894, tol=PTOL)
459
+ assert ae(v.imag, -0.56491812441304194711, tol=PTOL)
460
+ v = fp.e1((1.0 + 1.0j))
461
+ assert ae(v, (0.00028162445198141832551 - 0.17932453503935894015j), tol=ATOL)
462
+ assert ae(v.real, 0.00028162445198141832551, tol=PTOL)
463
+ assert ae(v.imag, -0.17932453503935894015, tol=PTOL)
464
+ v = fp.e1((2.0 + 2.0j))
465
+ assert ae(v, (-0.033767089606562004246 - 0.018599414169750541925j), tol=ATOL)
466
+ assert ae(v.real, -0.033767089606562004246, tol=PTOL)
467
+ assert ae(v.imag, -0.018599414169750541925, tol=PTOL)
468
+ v = fp.e1((5.0 + 5.0j))
469
+ assert ae(v, (0.0007266506660356393891 + 0.00047102780163522245054j), tol=ATOL)
470
+ assert ae(v.real, 0.0007266506660356393891, tol=PTOL)
471
+ assert ae(v.imag, 0.00047102780163522245054, tol=PTOL)
472
+ v = fp.e1((20.0 + 20.0j))
473
+ assert ae(v, (-2.3824537449367396579e-11 - 6.6969873156525615158e-11j), tol=ATOL)
474
+ assert ae(v.real, -2.3824537449367396579e-11, tol=PTOL)
475
+ assert ae(v.imag, -6.6969873156525615158e-11, tol=PTOL)
476
+ v = fp.e1((30.0 + 30.0j))
477
+ assert ae(v, (1.7316045841744061617e-15 + 1.3065678019487308689e-15j), tol=ATOL)
478
+ assert ae(v.real, 1.7316045841744061617e-15, tol=PTOL)
479
+ assert ae(v.imag, 1.3065678019487308689e-15, tol=PTOL)
480
+ v = fp.e1((40.0 + 40.0j))
481
+ assert ae(v, (-7.4001043002899232182e-20 - 4.991847855336816304e-21j), tol=ATOL)
482
+ assert ae(v.real, -7.4001043002899232182e-20, tol=PTOL)
483
+ assert ae(v.imag, -4.991847855336816304e-21, tol=PTOL)
484
+ v = fp.e1((50.0 + 50.0j))
485
+ assert ae(v, (2.3566128324644641219e-24 - 1.3188326726201614778e-24j), tol=ATOL)
486
+ assert ae(v.real, 2.3566128324644641219e-24, tol=PTOL)
487
+ assert ae(v.imag, -1.3188326726201614778e-24, tol=PTOL)
488
+ v = fp.e1((80.0 + 80.0j))
489
+ assert ae(v, (9.8279750572186526673e-38 + 1.243952841288868831e-37j), tol=ATOL)
490
+ assert ae(v.real, 9.8279750572186526673e-38, tol=PTOL)
491
+ assert ae(v.imag, 1.243952841288868831e-37, tol=PTOL)
492
+ v = fp.e1((1.1641532182693481445e-10 + 4.6566128730773925781e-10j))
493
+ assert ae(v, (20.880034621664969632 - 1.3258176632023711778j), tol=ATOL)
494
+ assert ae(v.real, 20.880034621664969632, tol=PTOL)
495
+ assert ae(v.imag, -1.3258176632023711778, tol=PTOL)
496
+ v = fp.e1((0.25 + 1.0j))
497
+ assert ae(v, (-0.16868306393667788761 - 0.4858011885947426971j), tol=ATOL)
498
+ assert ae(v.real, -0.16868306393667788761, tol=PTOL)
499
+ assert ae(v.imag, -0.4858011885947426971, tol=PTOL)
500
+ v = fp.e1((1.0 + 4.0j))
501
+ assert ae(v, (0.03373591813926547318 + 0.073523452241083821877j), tol=ATOL)
502
+ assert ae(v.real, 0.03373591813926547318, tol=PTOL)
503
+ assert ae(v.imag, 0.073523452241083821877, tol=PTOL)
504
+ v = fp.e1((2.0 + 8.0j))
505
+ assert ae(v, (-0.015392833434733785143 - 0.0031747121557605415914j), tol=ATOL)
506
+ assert ae(v.real, -0.015392833434733785143, tol=PTOL)
507
+ assert ae(v.imag, -0.0031747121557605415914, tol=PTOL)
508
+ v = fp.e1((5.0 + 20.0j))
509
+ assert ae(v, (-0.00024419662286542966525 - 0.00021008322966152755674j), tol=ATOL)
510
+ assert ae(v.real, -0.00024419662286542966525, tol=PTOL)
511
+ assert ae(v.imag, -0.00021008322966152755674, tol=PTOL)
512
+ v = fp.e1((20.0 + 80.0j))
513
+ assert ae(v, (2.3255552781051330088e-11 + 8.9463918891349438007e-12j), tol=ATOL)
514
+ assert ae(v.real, 2.3255552781051330088e-11, tol=PTOL)
515
+ assert ae(v.imag, 8.9463918891349438007e-12, tol=PTOL)
516
+ v = fp.e1((30.0 + 120.0j))
517
+ assert ae(v, (-2.7068919097124652332e-16 - 7.0477762411705130239e-16j), tol=ATOL)
518
+ assert ae(v.real, -2.7068919097124652332e-16, tol=PTOL)
519
+ assert ae(v.imag, -7.0477762411705130239e-16, tol=PTOL)
520
+ v = fp.e1((40.0 + 160.0j))
521
+ assert ae(v, (-1.1695597827678024687e-20 + 2.2907401455645736661e-20j), tol=ATOL)
522
+ assert ae(v.real, -1.1695597827678024687e-20, tol=PTOL)
523
+ assert ae(v.imag, 2.2907401455645736661e-20, tol=PTOL)
524
+ v = fp.e1((50.0 + 200.0j))
525
+ assert ae(v, (9.0323746914410162531e-25 - 2.3950601790033530935e-25j), tol=ATOL)
526
+ assert ae(v.real, 9.0323746914410162531e-25, tol=PTOL)
527
+ assert ae(v.imag, -2.3950601790033530935e-25, tol=PTOL)
528
+ v = fp.e1((80.0 + 320.0j))
529
+ assert ae(v, (3.4819106748728063576e-38 - 4.215653005615772724e-38j), tol=ATOL)
530
+ assert ae(v.real, 3.4819106748728063576e-38, tol=PTOL)
531
+ assert ae(v.imag, -4.215653005615772724e-38, tol=PTOL)
532
+ v = fp.e1((0.0 + 1.1641532182693481445e-10j))
533
+ assert ae(v, (22.29664129357666235 - 1.5707963266784812974j), tol=ATOL)
534
+ assert ae(v.real, 22.29664129357666235, tol=PTOL)
535
+ assert ae(v.imag, -1.5707963266784812974, tol=PTOL)
536
+ v = fp.e1((0.0 + 0.25j))
537
+ assert ae(v, (0.82466306258094565309 - 1.3216627564751394551j), tol=ATOL)
538
+ assert ae(v.real, 0.82466306258094565309, tol=PTOL)
539
+ assert ae(v.imag, -1.3216627564751394551, tol=PTOL)
540
+ v = fp.e1((0.0 + 1.0j))
541
+ assert ae(v, (-0.33740392290096813466 - 0.62471325642771360429j), tol=ATOL)
542
+ assert ae(v.real, -0.33740392290096813466, tol=PTOL)
543
+ assert ae(v.imag, -0.62471325642771360429, tol=PTOL)
544
+ v = fp.e1((0.0 + 2.0j))
545
+ assert ae(v, (-0.4229808287748649957 + 0.034616650007798229345j), tol=ATOL)
546
+ assert ae(v.real, -0.4229808287748649957, tol=PTOL)
547
+ assert ae(v.imag, 0.034616650007798229345, tol=PTOL)
548
+ v = fp.e1((0.0 + 5.0j))
549
+ assert ae(v, (0.19002974965664387862 - 0.020865081850222481957j), tol=ATOL)
550
+ assert ae(v.real, 0.19002974965664387862, tol=PTOL)
551
+ assert ae(v.imag, -0.020865081850222481957, tol=PTOL)
552
+ v = fp.e1((0.0 + 20.0j))
553
+ assert ae(v, (-0.04441982084535331654 - 0.022554625751456779068j), tol=ATOL)
554
+ assert ae(v.real, -0.04441982084535331654, tol=PTOL)
555
+ assert ae(v.imag, -0.022554625751456779068, tol=PTOL)
556
+ v = fp.e1((0.0 + 30.0j))
557
+ assert ae(v, (0.033032417282071143779 - 0.0040397867645455082476j), tol=ATOL)
558
+ assert ae(v.real, 0.033032417282071143779, tol=PTOL)
559
+ assert ae(v.imag, -0.0040397867645455082476, tol=PTOL)
560
+ v = fp.e1((0.0 + 40.0j))
561
+ assert ae(v, (-0.019020007896208766962 + 0.016188792559887887544j), tol=ATOL)
562
+ assert ae(v.real, -0.019020007896208766962, tol=PTOL)
563
+ assert ae(v.imag, 0.016188792559887887544, tol=PTOL)
564
+ v = fp.e1((0.0 + 50.0j))
565
+ assert ae(v, (0.0056283863241163054402 - 0.019179254308960724503j), tol=ATOL)
566
+ assert ae(v.real, 0.0056283863241163054402, tol=PTOL)
567
+ assert ae(v.imag, -0.019179254308960724503, tol=PTOL)
568
+ v = fp.e1((0.0 + 80.0j))
569
+ assert ae(v, (0.012402501155070958192 + 0.0015345601175906961199j), tol=ATOL)
570
+ assert ae(v.real, 0.012402501155070958192, tol=PTOL)
571
+ assert ae(v.imag, 0.0015345601175906961199, tol=PTOL)
572
+ v = fp.e1((-1.1641532182693481445e-10 + 4.6566128730773925781e-10j))
573
+ assert ae(v, (20.880034621432138988 - 1.8157749894560994861j), tol=ATOL)
574
+ assert ae(v.real, 20.880034621432138988, tol=PTOL)
575
+ assert ae(v.imag, -1.8157749894560994861, tol=PTOL)
576
+ v = fp.e1((-0.25 + 1.0j))
577
+ assert ae(v, (-0.59066621214766308594 - 0.74474454765205036972j), tol=ATOL)
578
+ assert ae(v.real, -0.59066621214766308594, tol=PTOL)
579
+ assert ae(v.imag, -0.74474454765205036972, tol=PTOL)
580
+ v = fp.e1((-1.0 + 4.0j))
581
+ assert ae(v, (0.49739047283060471093 + 0.41543605404038863174j), tol=ATOL)
582
+ assert ae(v.real, 0.49739047283060471093, tol=PTOL)
583
+ assert ae(v.imag, 0.41543605404038863174, tol=PTOL)
584
+ v = fp.e1((-2.0 + 8.0j))
585
+ assert ae(v, (-0.8705211147733730969 + 0.24099328498605539667j), tol=ATOL)
586
+ assert ae(v.real, -0.8705211147733730969, tol=PTOL)
587
+ assert ae(v.imag, 0.24099328498605539667, tol=PTOL)
588
+ v = fp.e1((-5.0 + 20.0j))
589
+ assert ae(v, (-7.0789514293925893007 - 1.6102177171960790536j), tol=ATOL)
590
+ assert ae(v.real, -7.0789514293925893007, tol=PTOL)
591
+ assert ae(v.imag, -1.6102177171960790536, tol=PTOL)
592
+ v = fp.e1((-20.0 + 80.0j))
593
+ assert ae(v, (5855431.4907298084434 - 720920.93315409165707j), tol=ATOL)
594
+ assert ae(v.real, 5855431.4907298084434, tol=PTOL)
595
+ assert ae(v.imag, -720920.93315409165707, tol=PTOL)
596
+ v = fp.e1((-30.0 + 120.0j))
597
+ assert ae(v, (-65402491644.703470747 - 56697658399.657460294j), tol=ATOL)
598
+ assert ae(v.real, -65402491644.703470747, tol=PTOL)
599
+ assert ae(v.imag, -56697658399.657460294, tol=PTOL)
600
+ v = fp.e1((-40.0 + 160.0j))
601
+ assert ae(v, (25504929379604.776769 + 1429035198630573.2463j), tol=ATOL)
602
+ assert ae(v.real, 25504929379604.776769, tol=PTOL)
603
+ assert ae(v.imag, 1429035198630573.2463, tol=PTOL)
604
+ v = fp.e1((-50.0 + 200.0j))
605
+ assert ae(v, (18437746526988116954.0 - 17146362239046152345.0j), tol=ATOL)
606
+ assert ae(v.real, 18437746526988116954.0, tol=PTOL)
607
+ assert ae(v.imag, -17146362239046152345.0, tol=PTOL)
608
+ v = fp.e1((-80.0 + 320.0j))
609
+ assert ae(v, (3.3464697299634526706e+31 - 1.6473152633843023919e+32j), tol=ATOL)
610
+ assert ae(v.real, 3.3464697299634526706e+31, tol=PTOL)
611
+ assert ae(v.imag, -1.6473152633843023919e+32, tol=PTOL)
612
+ v = fp.e1((-4.6566128730773925781e-10 + 1.1641532182693481445e-10j))
613
+ assert ae(v, (20.880034621082893023 - 2.8966139903465137624j), tol=ATOL)
614
+ assert ae(v.real, 20.880034621082893023, tol=PTOL)
615
+ assert ae(v.imag, -2.8966139903465137624, tol=PTOL)
616
+ v = fp.e1((-1.0 + 0.25j))
617
+ assert ae(v, (-1.8942716983721074932 - 2.4689102827070540799j), tol=ATOL)
618
+ assert ae(v.real, -1.8942716983721074932, tol=PTOL)
619
+ assert ae(v.imag, -2.4689102827070540799, tol=PTOL)
620
+ v = fp.e1((-4.0 + 1.0j))
621
+ assert ae(v, (-14.806699492675420438 + 9.1384225230837893776j), tol=ATOL)
622
+ assert ae(v.real, -14.806699492675420438, tol=PTOL)
623
+ assert ae(v.imag, 9.1384225230837893776, tol=PTOL)
624
+ v = fp.e1((-8.0 + 2.0j))
625
+ assert ae(v, (54.633252667426386294 + 413.20318163814670688j), tol=ATOL)
626
+ assert ae(v.real, 54.633252667426386294, tol=PTOL)
627
+ assert ae(v.imag, 413.20318163814670688, tol=PTOL)
628
+ v = fp.e1((-20.0 + 5.0j))
629
+ assert ae(v, (-711836.97165402624643 - 24745250.939695900956j), tol=ATOL)
630
+ assert ae(v.real, -711836.97165402624643, tol=PTOL)
631
+ assert ae(v.imag, -24745250.939695900956, tol=PTOL)
632
+ v = fp.e1((-80.0 + 20.0j))
633
+ assert ae(v, (-4.2139911108612653091e+32 + 5.3367124741918251637e+32j), tol=ATOL)
634
+ assert ae(v.real, -4.2139911108612653091e+32, tol=PTOL)
635
+ assert ae(v.imag, 5.3367124741918251637e+32, tol=PTOL)
636
+ v = fp.e1((-120.0 + 30.0j))
637
+ assert ae(v, (9.7760616203707508892e+48 - 1.058257682317195792e+50j), tol=ATOL)
638
+ assert ae(v.real, 9.7760616203707508892e+48, tol=PTOL)
639
+ assert ae(v.imag, -1.058257682317195792e+50, tol=PTOL)
640
+ v = fp.e1((-160.0 + 40.0j))
641
+ assert ae(v, (8.7065541466623638861e+66 + 1.6577106725141739889e+67j), tol=ATOL)
642
+ assert ae(v.real, 8.7065541466623638861e+66, tol=PTOL)
643
+ assert ae(v.imag, 1.6577106725141739889e+67, tol=PTOL)
644
+ v = fp.e1((-200.0 + 50.0j))
645
+ assert ae(v, (-3.070744996327018106e+84 - 1.7243244846769415903e+84j), tol=ATOL)
646
+ assert ae(v.real, -3.070744996327018106e+84, tol=PTOL)
647
+ assert ae(v.imag, -1.7243244846769415903e+84, tol=PTOL)
648
+ v = fp.e1((-320.0 + 80.0j))
649
+ assert ae(v, (9.9960598637998647276e+135 - 2.6855081527595608863e+136j), tol=ATOL)
650
+ assert ae(v.real, 9.9960598637998647276e+135, tol=PTOL)
651
+ assert ae(v.imag, -2.6855081527595608863e+136, tol=PTOL)
652
+ v = fp.e1(-1.1641532182693481445e-10)
653
+ assert ae(v, (22.296641293460247028 - 3.1415926535897932385j), tol=ATOL)
654
+ assert ae(v.real, 22.296641293460247028, tol=PTOL)
655
+ assert ae(v.imag, -3.1415926535897932385, tol=PTOL)
656
+ v = fp.e1(-0.25)
657
+ assert ae(v, (0.54254326466191372953 - 3.1415926535897932385j), tol=ATOL)
658
+ assert ae(v.real, 0.54254326466191372953, tol=PTOL)
659
+ assert ae(v.imag, -3.1415926535897932385, tol=PTOL)
660
+ v = fp.e1(-1.0)
661
+ assert ae(v, (-1.8951178163559367555 - 3.1415926535897932385j), tol=ATOL)
662
+ assert ae(v.real, -1.8951178163559367555, tol=PTOL)
663
+ assert ae(v.imag, -3.1415926535897932385, tol=PTOL)
664
+ v = fp.e1(-2.0)
665
+ assert ae(v, (-4.9542343560018901634 - 3.1415926535897932385j), tol=ATOL)
666
+ assert ae(v.real, -4.9542343560018901634, tol=PTOL)
667
+ assert ae(v.imag, -3.1415926535897932385, tol=PTOL)
668
+ v = fp.e1(-5.0)
669
+ assert ae(v, (-40.185275355803177455 - 3.1415926535897932385j), tol=ATOL)
670
+ assert ae(v.real, -40.185275355803177455, tol=PTOL)
671
+ assert ae(v.imag, -3.1415926535897932385, tol=PTOL)
672
+ v = fp.e1(-20.0)
673
+ assert ae(v, (-25615652.66405658882 - 3.1415926535897932385j), tol=ATOL)
674
+ assert ae(v.real, -25615652.66405658882, tol=PTOL)
675
+ assert ae(v.imag, -3.1415926535897932385, tol=PTOL)
676
+ v = fp.e1(-30.0)
677
+ assert ae(v, (-368973209407.27419706 - 3.1415926535897932385j), tol=ATOL)
678
+ assert ae(v.real, -368973209407.27419706, tol=PTOL)
679
+ assert ae(v.imag, -3.1415926535897932385, tol=PTOL)
680
+ v = fp.e1(-40.0)
681
+ assert ae(v, (-6039718263611241.5784 - 3.1415926535897932385j), tol=ATOL)
682
+ assert ae(v.real, -6039718263611241.5784, tol=PTOL)
683
+ assert ae(v.imag, -3.1415926535897932385, tol=PTOL)
684
+ v = fp.e1(-50.0)
685
+ assert ae(v, (-1.0585636897131690963e+20 - 3.1415926535897932385j), tol=ATOL)
686
+ assert ae(v.real, -1.0585636897131690963e+20, tol=PTOL)
687
+ assert ae(v.imag, -3.1415926535897932385, tol=PTOL)
688
+ v = fp.e1(-80.0)
689
+ assert ae(v, (-7.0146000049047999696e+32 - 3.1415926535897932385j), tol=ATOL)
690
+ assert ae(v.real, -7.0146000049047999696e+32, tol=PTOL)
691
+ assert ae(v.imag, -3.1415926535897932385, tol=PTOL)
692
+ v = fp.e1((-1.1641532182693481445e-10 + 0.0j))
693
+ assert ae(v, (22.296641293460247028 - 3.1415926535897932385j), tol=ATOL)
694
+ assert ae(v.real, 22.296641293460247028, tol=PTOL)
695
+ assert ae(v.imag, -3.1415926535897932385, tol=PTOL)
696
+ v = fp.e1((-0.25 + 0.0j))
697
+ assert ae(v, (0.54254326466191372953 - 3.1415926535897932385j), tol=ATOL)
698
+ assert ae(v.real, 0.54254326466191372953, tol=PTOL)
699
+ assert ae(v.imag, -3.1415926535897932385, tol=PTOL)
700
+ v = fp.e1((-1.0 + 0.0j))
701
+ assert ae(v, (-1.8951178163559367555 - 3.1415926535897932385j), tol=ATOL)
702
+ assert ae(v.real, -1.8951178163559367555, tol=PTOL)
703
+ assert ae(v.imag, -3.1415926535897932385, tol=PTOL)
704
+ v = fp.e1((-2.0 + 0.0j))
705
+ assert ae(v, (-4.9542343560018901634 - 3.1415926535897932385j), tol=ATOL)
706
+ assert ae(v.real, -4.9542343560018901634, tol=PTOL)
707
+ assert ae(v.imag, -3.1415926535897932385, tol=PTOL)
708
+ v = fp.e1((-5.0 + 0.0j))
709
+ assert ae(v, (-40.185275355803177455 - 3.1415926535897932385j), tol=ATOL)
710
+ assert ae(v.real, -40.185275355803177455, tol=PTOL)
711
+ assert ae(v.imag, -3.1415926535897932385, tol=PTOL)
712
+ v = fp.e1((-20.0 + 0.0j))
713
+ assert ae(v, (-25615652.66405658882 - 3.1415926535897932385j), tol=ATOL)
714
+ assert ae(v.real, -25615652.66405658882, tol=PTOL)
715
+ assert ae(v.imag, -3.1415926535897932385, tol=PTOL)
716
+ v = fp.e1((-30.0 + 0.0j))
717
+ assert ae(v, (-368973209407.27419706 - 3.1415926535897932385j), tol=ATOL)
718
+ assert ae(v.real, -368973209407.27419706, tol=PTOL)
719
+ assert ae(v.imag, -3.1415926535897932385, tol=PTOL)
720
+ v = fp.e1((-40.0 + 0.0j))
721
+ assert ae(v, (-6039718263611241.5784 - 3.1415926535897932385j), tol=ATOL)
722
+ assert ae(v.real, -6039718263611241.5784, tol=PTOL)
723
+ assert ae(v.imag, -3.1415926535897932385, tol=PTOL)
724
+ v = fp.e1((-50.0 + 0.0j))
725
+ assert ae(v, (-1.0585636897131690963e+20 - 3.1415926535897932385j), tol=ATOL)
726
+ assert ae(v.real, -1.0585636897131690963e+20, tol=PTOL)
727
+ assert ae(v.imag, -3.1415926535897932385, tol=PTOL)
728
+ v = fp.e1((-80.0 + 0.0j))
729
+ assert ae(v, (-7.0146000049047999696e+32 - 3.1415926535897932385j), tol=ATOL)
730
+ assert ae(v.real, -7.0146000049047999696e+32, tol=PTOL)
731
+ assert ae(v.imag, -3.1415926535897932385, tol=PTOL)
732
+ v = fp.e1((-4.6566128730773925781e-10 - 1.1641532182693481445e-10j))
733
+ assert ae(v, (20.880034621082893023 + 2.8966139903465137624j), tol=ATOL)
734
+ assert ae(v.real, 20.880034621082893023, tol=PTOL)
735
+ assert ae(v.imag, 2.8966139903465137624, tol=PTOL)
736
+ v = fp.e1((-1.0 - 0.25j))
737
+ assert ae(v, (-1.8942716983721074932 + 2.4689102827070540799j), tol=ATOL)
738
+ assert ae(v.real, -1.8942716983721074932, tol=PTOL)
739
+ assert ae(v.imag, 2.4689102827070540799, tol=PTOL)
740
+ v = fp.e1((-4.0 - 1.0j))
741
+ assert ae(v, (-14.806699492675420438 - 9.1384225230837893776j), tol=ATOL)
742
+ assert ae(v.real, -14.806699492675420438, tol=PTOL)
743
+ assert ae(v.imag, -9.1384225230837893776, tol=PTOL)
744
+ v = fp.e1((-8.0 - 2.0j))
745
+ assert ae(v, (54.633252667426386294 - 413.20318163814670688j), tol=ATOL)
746
+ assert ae(v.real, 54.633252667426386294, tol=PTOL)
747
+ assert ae(v.imag, -413.20318163814670688, tol=PTOL)
748
+ v = fp.e1((-20.0 - 5.0j))
749
+ assert ae(v, (-711836.97165402624643 + 24745250.939695900956j), tol=ATOL)
750
+ assert ae(v.real, -711836.97165402624643, tol=PTOL)
751
+ assert ae(v.imag, 24745250.939695900956, tol=PTOL)
752
+ v = fp.e1((-80.0 - 20.0j))
753
+ assert ae(v, (-4.2139911108612653091e+32 - 5.3367124741918251637e+32j), tol=ATOL)
754
+ assert ae(v.real, -4.2139911108612653091e+32, tol=PTOL)
755
+ assert ae(v.imag, -5.3367124741918251637e+32, tol=PTOL)
756
+ v = fp.e1((-120.0 - 30.0j))
757
+ assert ae(v, (9.7760616203707508892e+48 + 1.058257682317195792e+50j), tol=ATOL)
758
+ assert ae(v.real, 9.7760616203707508892e+48, tol=PTOL)
759
+ assert ae(v.imag, 1.058257682317195792e+50, tol=PTOL)
760
+ v = fp.e1((-160.0 - 40.0j))
761
+ assert ae(v, (8.7065541466623638861e+66 - 1.6577106725141739889e+67j), tol=ATOL)
762
+ assert ae(v.real, 8.7065541466623638861e+66, tol=PTOL)
763
+ assert ae(v.imag, -1.6577106725141739889e+67, tol=PTOL)
764
+ v = fp.e1((-200.0 - 50.0j))
765
+ assert ae(v, (-3.070744996327018106e+84 + 1.7243244846769415903e+84j), tol=ATOL)
766
+ assert ae(v.real, -3.070744996327018106e+84, tol=PTOL)
767
+ assert ae(v.imag, 1.7243244846769415903e+84, tol=PTOL)
768
+ v = fp.e1((-320.0 - 80.0j))
769
+ assert ae(v, (9.9960598637998647276e+135 + 2.6855081527595608863e+136j), tol=ATOL)
770
+ assert ae(v.real, 9.9960598637998647276e+135, tol=PTOL)
771
+ assert ae(v.imag, 2.6855081527595608863e+136, tol=PTOL)
772
+ v = fp.e1((-1.1641532182693481445e-10 - 1.1641532182693481445e-10j))
773
+ assert ae(v, (21.950067703180274374 + 2.356194490075929607j), tol=ATOL)
774
+ assert ae(v.real, 21.950067703180274374, tol=PTOL)
775
+ assert ae(v.imag, 2.356194490075929607, tol=PTOL)
776
+ v = fp.e1((-0.25 - 0.25j))
777
+ assert ae(v, (0.21441047326710323254 + 2.0732153554307936389j), tol=ATOL)
778
+ assert ae(v.real, 0.21441047326710323254, tol=PTOL)
779
+ assert ae(v.imag, 2.0732153554307936389, tol=PTOL)
780
+ v = fp.e1((-1.0 - 1.0j))
781
+ assert ae(v, (-1.7646259855638540684 + 0.7538228020792708192j), tol=ATOL)
782
+ assert ae(v.real, -1.7646259855638540684, tol=PTOL)
783
+ assert ae(v.imag, 0.7538228020792708192, tol=PTOL)
784
+ v = fp.e1((-2.0 - 2.0j))
785
+ assert ae(v, (-1.8920781621855474089 - 2.1753697842428647236j), tol=ATOL)
786
+ assert ae(v.real, -1.8920781621855474089, tol=PTOL)
787
+ assert ae(v.imag, -2.1753697842428647236, tol=PTOL)
788
+ v = fp.e1((-5.0 - 5.0j))
789
+ assert ae(v, (13.470936071475245856 + 18.464085049321024206j), tol=ATOL)
790
+ assert ae(v.real, 13.470936071475245856, tol=PTOL)
791
+ assert ae(v.imag, 18.464085049321024206, tol=PTOL)
792
+ v = fp.e1((-20.0 - 20.0j))
793
+ assert ae(v, (-16589317.398788971896 - 5831702.3296441771206j), tol=ATOL)
794
+ assert ae(v.real, -16589317.398788971896, tol=PTOL)
795
+ assert ae(v.imag, -5831702.3296441771206, tol=PTOL)
796
+ v = fp.e1((-30.0 - 30.0j))
797
+ assert ae(v, (154596484273.69322527 + 204179357837.41389696j), tol=ATOL)
798
+ assert ae(v.real, 154596484273.69322527, tol=PTOL)
799
+ assert ae(v.imag, 204179357837.41389696, tol=PTOL)
800
+ v = fp.e1((-40.0 - 40.0j))
801
+ assert ae(v, (-287512180321448.45408 - 4203502407932314.974j), tol=ATOL)
802
+ assert ae(v.real, -287512180321448.45408, tol=PTOL)
803
+ assert ae(v.imag, -4203502407932314.974, tol=PTOL)
804
+ v = fp.e1((-50.0 - 50.0j))
805
+ assert ae(v, (-36128528616649268826.0 + 64648801861338741963.0j), tol=ATOL)
806
+ assert ae(v.real, -36128528616649268826.0, tol=PTOL)
807
+ assert ae(v.imag, 64648801861338741963.0, tol=PTOL)
808
+ v = fp.e1((-80.0 - 80.0j))
809
+ assert ae(v, (3.8674816337930010217e+32 + 3.0540709639658071041e+32j), tol=ATOL)
810
+ assert ae(v.real, 3.8674816337930010217e+32, tol=PTOL)
811
+ assert ae(v.imag, 3.0540709639658071041e+32, tol=PTOL)
812
+ v = fp.e1((-1.1641532182693481445e-10 - 4.6566128730773925781e-10j))
813
+ assert ae(v, (20.880034621432138988 + 1.8157749894560994861j), tol=ATOL)
814
+ assert ae(v.real, 20.880034621432138988, tol=PTOL)
815
+ assert ae(v.imag, 1.8157749894560994861, tol=PTOL)
816
+ v = fp.e1((-0.25 - 1.0j))
817
+ assert ae(v, (-0.59066621214766308594 + 0.74474454765205036972j), tol=ATOL)
818
+ assert ae(v.real, -0.59066621214766308594, tol=PTOL)
819
+ assert ae(v.imag, 0.74474454765205036972, tol=PTOL)
820
+ v = fp.e1((-1.0 - 4.0j))
821
+ assert ae(v, (0.49739047283060471093 - 0.41543605404038863174j), tol=ATOL)
822
+ assert ae(v.real, 0.49739047283060471093, tol=PTOL)
823
+ assert ae(v.imag, -0.41543605404038863174, tol=PTOL)
824
+ v = fp.e1((-2.0 - 8.0j))
825
+ assert ae(v, (-0.8705211147733730969 - 0.24099328498605539667j), tol=ATOL)
826
+ assert ae(v.real, -0.8705211147733730969, tol=PTOL)
827
+ assert ae(v.imag, -0.24099328498605539667, tol=PTOL)
828
+ v = fp.e1((-5.0 - 20.0j))
829
+ assert ae(v, (-7.0789514293925893007 + 1.6102177171960790536j), tol=ATOL)
830
+ assert ae(v.real, -7.0789514293925893007, tol=PTOL)
831
+ assert ae(v.imag, 1.6102177171960790536, tol=PTOL)
832
+ v = fp.e1((-20.0 - 80.0j))
833
+ assert ae(v, (5855431.4907298084434 + 720920.93315409165707j), tol=ATOL)
834
+ assert ae(v.real, 5855431.4907298084434, tol=PTOL)
835
+ assert ae(v.imag, 720920.93315409165707, tol=PTOL)
836
+ v = fp.e1((-30.0 - 120.0j))
837
+ assert ae(v, (-65402491644.703470747 + 56697658399.657460294j), tol=ATOL)
838
+ assert ae(v.real, -65402491644.703470747, tol=PTOL)
839
+ assert ae(v.imag, 56697658399.657460294, tol=PTOL)
840
+ v = fp.e1((-40.0 - 160.0j))
841
+ assert ae(v, (25504929379604.776769 - 1429035198630573.2463j), tol=ATOL)
842
+ assert ae(v.real, 25504929379604.776769, tol=PTOL)
843
+ assert ae(v.imag, -1429035198630573.2463, tol=PTOL)
844
+ v = fp.e1((-50.0 - 200.0j))
845
+ assert ae(v, (18437746526988116954.0 + 17146362239046152345.0j), tol=ATOL)
846
+ assert ae(v.real, 18437746526988116954.0, tol=PTOL)
847
+ assert ae(v.imag, 17146362239046152345.0, tol=PTOL)
848
+ v = fp.e1((-80.0 - 320.0j))
849
+ assert ae(v, (3.3464697299634526706e+31 + 1.6473152633843023919e+32j), tol=ATOL)
850
+ assert ae(v.real, 3.3464697299634526706e+31, tol=PTOL)
851
+ assert ae(v.imag, 1.6473152633843023919e+32, tol=PTOL)
852
+ v = fp.e1((0.0 - 1.1641532182693481445e-10j))
853
+ assert ae(v, (22.29664129357666235 + 1.5707963266784812974j), tol=ATOL)
854
+ assert ae(v.real, 22.29664129357666235, tol=PTOL)
855
+ assert ae(v.imag, 1.5707963266784812974, tol=PTOL)
856
+ v = fp.e1((0.0 - 0.25j))
857
+ assert ae(v, (0.82466306258094565309 + 1.3216627564751394551j), tol=ATOL)
858
+ assert ae(v.real, 0.82466306258094565309, tol=PTOL)
859
+ assert ae(v.imag, 1.3216627564751394551, tol=PTOL)
860
+ v = fp.e1((0.0 - 1.0j))
861
+ assert ae(v, (-0.33740392290096813466 + 0.62471325642771360429j), tol=ATOL)
862
+ assert ae(v.real, -0.33740392290096813466, tol=PTOL)
863
+ assert ae(v.imag, 0.62471325642771360429, tol=PTOL)
864
+ v = fp.e1((0.0 - 2.0j))
865
+ assert ae(v, (-0.4229808287748649957 - 0.034616650007798229345j), tol=ATOL)
866
+ assert ae(v.real, -0.4229808287748649957, tol=PTOL)
867
+ assert ae(v.imag, -0.034616650007798229345, tol=PTOL)
868
+ v = fp.e1((0.0 - 5.0j))
869
+ assert ae(v, (0.19002974965664387862 + 0.020865081850222481957j), tol=ATOL)
870
+ assert ae(v.real, 0.19002974965664387862, tol=PTOL)
871
+ assert ae(v.imag, 0.020865081850222481957, tol=PTOL)
872
+ v = fp.e1((0.0 - 20.0j))
873
+ assert ae(v, (-0.04441982084535331654 + 0.022554625751456779068j), tol=ATOL)
874
+ assert ae(v.real, -0.04441982084535331654, tol=PTOL)
875
+ assert ae(v.imag, 0.022554625751456779068, tol=PTOL)
876
+ v = fp.e1((0.0 - 30.0j))
877
+ assert ae(v, (0.033032417282071143779 + 0.0040397867645455082476j), tol=ATOL)
878
+ assert ae(v.real, 0.033032417282071143779, tol=PTOL)
879
+ assert ae(v.imag, 0.0040397867645455082476, tol=PTOL)
880
+ v = fp.e1((0.0 - 40.0j))
881
+ assert ae(v, (-0.019020007896208766962 - 0.016188792559887887544j), tol=ATOL)
882
+ assert ae(v.real, -0.019020007896208766962, tol=PTOL)
883
+ assert ae(v.imag, -0.016188792559887887544, tol=PTOL)
884
+ v = fp.e1((0.0 - 50.0j))
885
+ assert ae(v, (0.0056283863241163054402 + 0.019179254308960724503j), tol=ATOL)
886
+ assert ae(v.real, 0.0056283863241163054402, tol=PTOL)
887
+ assert ae(v.imag, 0.019179254308960724503, tol=PTOL)
888
+ v = fp.e1((0.0 - 80.0j))
889
+ assert ae(v, (0.012402501155070958192 - 0.0015345601175906961199j), tol=ATOL)
890
+ assert ae(v.real, 0.012402501155070958192, tol=PTOL)
891
+ assert ae(v.imag, -0.0015345601175906961199, tol=PTOL)
892
+ v = fp.e1((1.1641532182693481445e-10 - 4.6566128730773925781e-10j))
893
+ assert ae(v, (20.880034621664969632 + 1.3258176632023711778j), tol=ATOL)
894
+ assert ae(v.real, 20.880034621664969632, tol=PTOL)
895
+ assert ae(v.imag, 1.3258176632023711778, tol=PTOL)
896
+ v = fp.e1((0.25 - 1.0j))
897
+ assert ae(v, (-0.16868306393667788761 + 0.4858011885947426971j), tol=ATOL)
898
+ assert ae(v.real, -0.16868306393667788761, tol=PTOL)
899
+ assert ae(v.imag, 0.4858011885947426971, tol=PTOL)
900
+ v = fp.e1((1.0 - 4.0j))
901
+ assert ae(v, (0.03373591813926547318 - 0.073523452241083821877j), tol=ATOL)
902
+ assert ae(v.real, 0.03373591813926547318, tol=PTOL)
903
+ assert ae(v.imag, -0.073523452241083821877, tol=PTOL)
904
+ v = fp.e1((2.0 - 8.0j))
905
+ assert ae(v, (-0.015392833434733785143 + 0.0031747121557605415914j), tol=ATOL)
906
+ assert ae(v.real, -0.015392833434733785143, tol=PTOL)
907
+ assert ae(v.imag, 0.0031747121557605415914, tol=PTOL)
908
+ v = fp.e1((5.0 - 20.0j))
909
+ assert ae(v, (-0.00024419662286542966525 + 0.00021008322966152755674j), tol=ATOL)
910
+ assert ae(v.real, -0.00024419662286542966525, tol=PTOL)
911
+ assert ae(v.imag, 0.00021008322966152755674, tol=PTOL)
912
+ v = fp.e1((20.0 - 80.0j))
913
+ assert ae(v, (2.3255552781051330088e-11 - 8.9463918891349438007e-12j), tol=ATOL)
914
+ assert ae(v.real, 2.3255552781051330088e-11, tol=PTOL)
915
+ assert ae(v.imag, -8.9463918891349438007e-12, tol=PTOL)
916
+ v = fp.e1((30.0 - 120.0j))
917
+ assert ae(v, (-2.7068919097124652332e-16 + 7.0477762411705130239e-16j), tol=ATOL)
918
+ assert ae(v.real, -2.7068919097124652332e-16, tol=PTOL)
919
+ assert ae(v.imag, 7.0477762411705130239e-16, tol=PTOL)
920
+ v = fp.e1((40.0 - 160.0j))
921
+ assert ae(v, (-1.1695597827678024687e-20 - 2.2907401455645736661e-20j), tol=ATOL)
922
+ assert ae(v.real, -1.1695597827678024687e-20, tol=PTOL)
923
+ assert ae(v.imag, -2.2907401455645736661e-20, tol=PTOL)
924
+ v = fp.e1((50.0 - 200.0j))
925
+ assert ae(v, (9.0323746914410162531e-25 + 2.3950601790033530935e-25j), tol=ATOL)
926
+ assert ae(v.real, 9.0323746914410162531e-25, tol=PTOL)
927
+ assert ae(v.imag, 2.3950601790033530935e-25, tol=PTOL)
928
+ v = fp.e1((80.0 - 320.0j))
929
+ assert ae(v, (3.4819106748728063576e-38 + 4.215653005615772724e-38j), tol=ATOL)
930
+ assert ae(v.real, 3.4819106748728063576e-38, tol=PTOL)
931
+ assert ae(v.imag, 4.215653005615772724e-38, tol=PTOL)
932
+ v = fp.e1((1.1641532182693481445e-10 - 1.1641532182693481445e-10j))
933
+ assert ae(v, (21.950067703413105017 + 0.7853981632810329878j), tol=ATOL)
934
+ assert ae(v.real, 21.950067703413105017, tol=PTOL)
935
+ assert ae(v.imag, 0.7853981632810329878, tol=PTOL)
936
+ v = fp.e1((0.25 - 0.25j))
937
+ assert ae(v, (0.71092525792923287894 + 0.56491812441304194711j), tol=ATOL)
938
+ assert ae(v.real, 0.71092525792923287894, tol=PTOL)
939
+ assert ae(v.imag, 0.56491812441304194711, tol=PTOL)
940
+ v = fp.e1((1.0 - 1.0j))
941
+ assert ae(v, (0.00028162445198141832551 + 0.17932453503935894015j), tol=ATOL)
942
+ assert ae(v.real, 0.00028162445198141832551, tol=PTOL)
943
+ assert ae(v.imag, 0.17932453503935894015, tol=PTOL)
944
+ v = fp.e1((2.0 - 2.0j))
945
+ assert ae(v, (-0.033767089606562004246 + 0.018599414169750541925j), tol=ATOL)
946
+ assert ae(v.real, -0.033767089606562004246, tol=PTOL)
947
+ assert ae(v.imag, 0.018599414169750541925, tol=PTOL)
948
+ v = fp.e1((5.0 - 5.0j))
949
+ assert ae(v, (0.0007266506660356393891 - 0.00047102780163522245054j), tol=ATOL)
950
+ assert ae(v.real, 0.0007266506660356393891, tol=PTOL)
951
+ assert ae(v.imag, -0.00047102780163522245054, tol=PTOL)
952
+ v = fp.e1((20.0 - 20.0j))
953
+ assert ae(v, (-2.3824537449367396579e-11 + 6.6969873156525615158e-11j), tol=ATOL)
954
+ assert ae(v.real, -2.3824537449367396579e-11, tol=PTOL)
955
+ assert ae(v.imag, 6.6969873156525615158e-11, tol=PTOL)
956
+ v = fp.e1((30.0 - 30.0j))
957
+ assert ae(v, (1.7316045841744061617e-15 - 1.3065678019487308689e-15j), tol=ATOL)
958
+ assert ae(v.real, 1.7316045841744061617e-15, tol=PTOL)
959
+ assert ae(v.imag, -1.3065678019487308689e-15, tol=PTOL)
960
+ v = fp.e1((40.0 - 40.0j))
961
+ assert ae(v, (-7.4001043002899232182e-20 + 4.991847855336816304e-21j), tol=ATOL)
962
+ assert ae(v.real, -7.4001043002899232182e-20, tol=PTOL)
963
+ assert ae(v.imag, 4.991847855336816304e-21, tol=PTOL)
964
+ v = fp.e1((50.0 - 50.0j))
965
+ assert ae(v, (2.3566128324644641219e-24 + 1.3188326726201614778e-24j), tol=ATOL)
966
+ assert ae(v.real, 2.3566128324644641219e-24, tol=PTOL)
967
+ assert ae(v.imag, 1.3188326726201614778e-24, tol=PTOL)
968
+ v = fp.e1((80.0 - 80.0j))
969
+ assert ae(v, (9.8279750572186526673e-38 - 1.243952841288868831e-37j), tol=ATOL)
970
+ assert ae(v.real, 9.8279750572186526673e-38, tol=PTOL)
971
+ assert ae(v.imag, -1.243952841288868831e-37, tol=PTOL)
972
+ v = fp.e1((4.6566128730773925781e-10 - 1.1641532182693481445e-10j))
973
+ assert ae(v, (20.880034622014215597 + 0.24497866301044883237j), tol=ATOL)
974
+ assert ae(v.real, 20.880034622014215597, tol=PTOL)
975
+ assert ae(v.imag, 0.24497866301044883237, tol=PTOL)
976
+ v = fp.e1((1.0 - 0.25j))
977
+ assert ae(v, (0.19731063945004229095 + 0.087366045774299963672j), tol=ATOL)
978
+ assert ae(v.real, 0.19731063945004229095, tol=PTOL)
979
+ assert ae(v.imag, 0.087366045774299963672, tol=PTOL)
980
+ v = fp.e1((4.0 - 1.0j))
981
+ assert ae(v, (0.0013106173980145506944 + 0.0034542480199350626699j), tol=ATOL)
982
+ assert ae(v.real, 0.0013106173980145506944, tol=PTOL)
983
+ assert ae(v.imag, 0.0034542480199350626699, tol=PTOL)
984
+ v = fp.e1((8.0 - 2.0j))
985
+ assert ae(v, (-0.000022278049065270225945 + 0.000029191940456521555288j), tol=ATOL)
986
+ assert ae(v.real, -0.000022278049065270225945, tol=PTOL)
987
+ assert ae(v.imag, 0.000029191940456521555288, tol=PTOL)
988
+ v = fp.e1((20.0 - 5.0j))
989
+ assert ae(v, (4.7711374515765346894e-11 - 8.2902652405126947359e-11j), tol=ATOL)
990
+ assert ae(v.real, 4.7711374515765346894e-11, tol=PTOL)
991
+ assert ae(v.imag, -8.2902652405126947359e-11, tol=PTOL)
992
+ v = fp.e1((80.0 - 20.0j))
993
+ assert ae(v, (3.8353473865788235787e-38 + 2.129247592349605139e-37j), tol=ATOL)
994
+ assert ae(v.real, 3.8353473865788235787e-38, tol=PTOL)
995
+ assert ae(v.imag, 2.129247592349605139e-37, tol=PTOL)
996
+ v = fp.e1((120.0 - 30.0j))
997
+ assert ae(v, (2.3836002337480334716e-55 - 5.6704043587126198306e-55j), tol=ATOL)
998
+ assert ae(v.real, 2.3836002337480334716e-55, tol=PTOL)
999
+ assert ae(v.imag, -5.6704043587126198306e-55, tol=PTOL)
1000
+ v = fp.e1((160.0 - 40.0j))
1001
+ assert ae(v, (-1.6238022898654510661e-72 + 1.104172355572287367e-72j), tol=ATOL)
1002
+ assert ae(v.real, -1.6238022898654510661e-72, tol=PTOL)
1003
+ assert ae(v.imag, 1.104172355572287367e-72, tol=PTOL)
1004
+ v = fp.e1((200.0 - 50.0j))
1005
+ assert ae(v, (6.6800061461666228487e-90 - 1.4473816083541016115e-91j), tol=ATOL)
1006
+ assert ae(v.real, 6.6800061461666228487e-90, tol=PTOL)
1007
+ assert ae(v.imag, -1.4473816083541016115e-91, tol=PTOL)
1008
+ v = fp.e1((320.0 - 80.0j))
1009
+ assert ae(v, (4.2737871527778786157e-143 - 3.1789935525785660314e-142j), tol=ATOL)
1010
+ assert ae(v.real, 4.2737871527778786157e-143, tol=PTOL)
1011
+ assert ae(v.imag, -3.1789935525785660314e-142, tol=PTOL)
1012
+ v = fp.ei(1.1641532182693481445e-10)
1013
+ assert ae(v, -22.296641293460247028, tol=ATOL)
1014
+ assert type(v) is float
1015
+ v = fp.ei(0.25)
1016
+ assert ae(v, -0.54254326466191372953, tol=ATOL)
1017
+ assert type(v) is float
1018
+ v = fp.ei(1.0)
1019
+ assert ae(v, 1.8951178163559367555, tol=ATOL)
1020
+ assert type(v) is float
1021
+ v = fp.ei(2.0)
1022
+ assert ae(v, 4.9542343560018901634, tol=ATOL)
1023
+ assert type(v) is float
1024
+ v = fp.ei(5.0)
1025
+ assert ae(v, 40.185275355803177455, tol=ATOL)
1026
+ assert type(v) is float
1027
+ v = fp.ei(20.0)
1028
+ assert ae(v, 25615652.66405658882, tol=ATOL)
1029
+ assert type(v) is float
1030
+ v = fp.ei(30.0)
1031
+ assert ae(v, 368973209407.27419706, tol=ATOL)
1032
+ assert type(v) is float
1033
+ v = fp.ei(40.0)
1034
+ assert ae(v, 6039718263611241.5784, tol=ATOL)
1035
+ assert type(v) is float
1036
+ v = fp.ei(50.0)
1037
+ assert ae(v, 1.0585636897131690963e+20, tol=ATOL)
1038
+ assert type(v) is float
1039
+ v = fp.ei(80.0)
1040
+ assert ae(v, 7.0146000049047999696e+32, tol=ATOL)
1041
+ assert type(v) is float
1042
+ v = fp.ei((1.1641532182693481445e-10 + 0.0j))
1043
+ assert ae(v, (-22.296641293460247028 + 0.0j), tol=ATOL)
1044
+ assert ae(v.real, -22.296641293460247028, tol=PTOL)
1045
+ assert v.imag == 0
1046
+ v = fp.ei((0.25 + 0.0j))
1047
+ assert ae(v, (-0.54254326466191372953 + 0.0j), tol=ATOL)
1048
+ assert ae(v.real, -0.54254326466191372953, tol=PTOL)
1049
+ assert v.imag == 0
1050
+ v = fp.ei((1.0 + 0.0j))
1051
+ assert ae(v, (1.8951178163559367555 + 0.0j), tol=ATOL)
1052
+ assert ae(v.real, 1.8951178163559367555, tol=PTOL)
1053
+ assert v.imag == 0
1054
+ v = fp.ei((2.0 + 0.0j))
1055
+ assert ae(v, (4.9542343560018901634 + 0.0j), tol=ATOL)
1056
+ assert ae(v.real, 4.9542343560018901634, tol=PTOL)
1057
+ assert v.imag == 0
1058
+ v = fp.ei((5.0 + 0.0j))
1059
+ assert ae(v, (40.185275355803177455 + 0.0j), tol=ATOL)
1060
+ assert ae(v.real, 40.185275355803177455, tol=PTOL)
1061
+ assert v.imag == 0
1062
+ v = fp.ei((20.0 + 0.0j))
1063
+ assert ae(v, (25615652.66405658882 + 0.0j), tol=ATOL)
1064
+ assert ae(v.real, 25615652.66405658882, tol=PTOL)
1065
+ assert v.imag == 0
1066
+ v = fp.ei((30.0 + 0.0j))
1067
+ assert ae(v, (368973209407.27419706 + 0.0j), tol=ATOL)
1068
+ assert ae(v.real, 368973209407.27419706, tol=PTOL)
1069
+ assert v.imag == 0
1070
+ v = fp.ei((40.0 + 0.0j))
1071
+ assert ae(v, (6039718263611241.5784 + 0.0j), tol=ATOL)
1072
+ assert ae(v.real, 6039718263611241.5784, tol=PTOL)
1073
+ assert v.imag == 0
1074
+ v = fp.ei((50.0 + 0.0j))
1075
+ assert ae(v, (1.0585636897131690963e+20 + 0.0j), tol=ATOL)
1076
+ assert ae(v.real, 1.0585636897131690963e+20, tol=PTOL)
1077
+ assert v.imag == 0
1078
+ v = fp.ei((80.0 + 0.0j))
1079
+ assert ae(v, (7.0146000049047999696e+32 + 0.0j), tol=ATOL)
1080
+ assert ae(v.real, 7.0146000049047999696e+32, tol=PTOL)
1081
+ assert v.imag == 0
1082
+ v = fp.ei((4.6566128730773925781e-10 + 1.1641532182693481445e-10j))
1083
+ assert ae(v, (-20.880034621082893023 + 0.24497866324327947603j), tol=ATOL)
1084
+ assert ae(v.real, -20.880034621082893023, tol=PTOL)
1085
+ assert ae(v.imag, 0.24497866324327947603, tol=PTOL)
1086
+ v = fp.ei((1.0 + 0.25j))
1087
+ assert ae(v, (1.8942716983721074932 + 0.67268237088273915854j), tol=ATOL)
1088
+ assert ae(v.real, 1.8942716983721074932, tol=PTOL)
1089
+ assert ae(v.imag, 0.67268237088273915854, tol=PTOL)
1090
+ v = fp.ei((4.0 + 1.0j))
1091
+ assert ae(v, (14.806699492675420438 + 12.280015176673582616j), tol=ATOL)
1092
+ assert ae(v.real, 14.806699492675420438, tol=PTOL)
1093
+ assert ae(v.imag, 12.280015176673582616, tol=PTOL)
1094
+ v = fp.ei((8.0 + 2.0j))
1095
+ assert ae(v, (-54.633252667426386294 + 416.34477429173650012j), tol=ATOL)
1096
+ assert ae(v.real, -54.633252667426386294, tol=PTOL)
1097
+ assert ae(v.imag, 416.34477429173650012, tol=PTOL)
1098
+ v = fp.ei((20.0 + 5.0j))
1099
+ assert ae(v, (711836.97165402624643 - 24745247.798103247366j), tol=ATOL)
1100
+ assert ae(v.real, 711836.97165402624643, tol=PTOL)
1101
+ assert ae(v.imag, -24745247.798103247366, tol=PTOL)
1102
+ v = fp.ei((80.0 + 20.0j))
1103
+ assert ae(v, (4.2139911108612653091e+32 + 5.3367124741918251637e+32j), tol=ATOL)
1104
+ assert ae(v.real, 4.2139911108612653091e+32, tol=PTOL)
1105
+ assert ae(v.imag, 5.3367124741918251637e+32, tol=PTOL)
1106
+ v = fp.ei((120.0 + 30.0j))
1107
+ assert ae(v, (-9.7760616203707508892e+48 - 1.058257682317195792e+50j), tol=ATOL)
1108
+ assert ae(v.real, -9.7760616203707508892e+48, tol=PTOL)
1109
+ assert ae(v.imag, -1.058257682317195792e+50, tol=PTOL)
1110
+ v = fp.ei((160.0 + 40.0j))
1111
+ assert ae(v, (-8.7065541466623638861e+66 + 1.6577106725141739889e+67j), tol=ATOL)
1112
+ assert ae(v.real, -8.7065541466623638861e+66, tol=PTOL)
1113
+ assert ae(v.imag, 1.6577106725141739889e+67, tol=PTOL)
1114
+ v = fp.ei((200.0 + 50.0j))
1115
+ assert ae(v, (3.070744996327018106e+84 - 1.7243244846769415903e+84j), tol=ATOL)
1116
+ assert ae(v.real, 3.070744996327018106e+84, tol=PTOL)
1117
+ assert ae(v.imag, -1.7243244846769415903e+84, tol=PTOL)
1118
+ v = fp.ei((320.0 + 80.0j))
1119
+ assert ae(v, (-9.9960598637998647276e+135 - 2.6855081527595608863e+136j), tol=ATOL)
1120
+ assert ae(v.real, -9.9960598637998647276e+135, tol=PTOL)
1121
+ assert ae(v.imag, -2.6855081527595608863e+136, tol=PTOL)
1122
+ v = fp.ei((1.1641532182693481445e-10 + 1.1641532182693481445e-10j))
1123
+ assert ae(v, (-21.950067703180274374 + 0.78539816351386363145j), tol=ATOL)
1124
+ assert ae(v.real, -21.950067703180274374, tol=PTOL)
1125
+ assert ae(v.imag, 0.78539816351386363145, tol=PTOL)
1126
+ v = fp.ei((0.25 + 0.25j))
1127
+ assert ae(v, (-0.21441047326710323254 + 1.0683772981589995996j), tol=ATOL)
1128
+ assert ae(v.real, -0.21441047326710323254, tol=PTOL)
1129
+ assert ae(v.imag, 1.0683772981589995996, tol=PTOL)
1130
+ v = fp.ei((1.0 + 1.0j))
1131
+ assert ae(v, (1.7646259855638540684 + 2.3877698515105224193j), tol=ATOL)
1132
+ assert ae(v.real, 1.7646259855638540684, tol=PTOL)
1133
+ assert ae(v.imag, 2.3877698515105224193, tol=PTOL)
1134
+ v = fp.ei((2.0 + 2.0j))
1135
+ assert ae(v, (1.8920781621855474089 + 5.3169624378326579621j), tol=ATOL)
1136
+ assert ae(v.real, 1.8920781621855474089, tol=PTOL)
1137
+ assert ae(v.imag, 5.3169624378326579621, tol=PTOL)
1138
+ v = fp.ei((5.0 + 5.0j))
1139
+ assert ae(v, (-13.470936071475245856 - 15.322492395731230968j), tol=ATOL)
1140
+ assert ae(v.real, -13.470936071475245856, tol=PTOL)
1141
+ assert ae(v.imag, -15.322492395731230968, tol=PTOL)
1142
+ v = fp.ei((20.0 + 20.0j))
1143
+ assert ae(v, (16589317.398788971896 + 5831705.4712368307104j), tol=ATOL)
1144
+ assert ae(v.real, 16589317.398788971896, tol=PTOL)
1145
+ assert ae(v.imag, 5831705.4712368307104, tol=PTOL)
1146
+ v = fp.ei((30.0 + 30.0j))
1147
+ assert ae(v, (-154596484273.69322527 - 204179357834.2723043j), tol=ATOL)
1148
+ assert ae(v.real, -154596484273.69322527, tol=PTOL)
1149
+ assert ae(v.imag, -204179357834.2723043, tol=PTOL)
1150
+ v = fp.ei((40.0 + 40.0j))
1151
+ assert ae(v, (287512180321448.45408 + 4203502407932318.1156j), tol=ATOL)
1152
+ assert ae(v.real, 287512180321448.45408, tol=PTOL)
1153
+ assert ae(v.imag, 4203502407932318.1156, tol=PTOL)
1154
+ v = fp.ei((50.0 + 50.0j))
1155
+ assert ae(v, (36128528616649268826.0 - 64648801861338741960.0j), tol=ATOL)
1156
+ assert ae(v.real, 36128528616649268826.0, tol=PTOL)
1157
+ assert ae(v.imag, -64648801861338741960.0, tol=PTOL)
1158
+ v = fp.ei((80.0 + 80.0j))
1159
+ assert ae(v, (-3.8674816337930010217e+32 - 3.0540709639658071041e+32j), tol=ATOL)
1160
+ assert ae(v.real, -3.8674816337930010217e+32, tol=PTOL)
1161
+ assert ae(v.imag, -3.0540709639658071041e+32, tol=PTOL)
1162
+ v = fp.ei((1.1641532182693481445e-10 + 4.6566128730773925781e-10j))
1163
+ assert ae(v, (-20.880034621432138988 + 1.3258176641336937524j), tol=ATOL)
1164
+ assert ae(v.real, -20.880034621432138988, tol=PTOL)
1165
+ assert ae(v.imag, 1.3258176641336937524, tol=PTOL)
1166
+ v = fp.ei((0.25 + 1.0j))
1167
+ assert ae(v, (0.59066621214766308594 + 2.3968481059377428687j), tol=ATOL)
1168
+ assert ae(v.real, 0.59066621214766308594, tol=PTOL)
1169
+ assert ae(v.imag, 2.3968481059377428687, tol=PTOL)
1170
+ v = fp.ei((1.0 + 4.0j))
1171
+ assert ae(v, (-0.49739047283060471093 + 3.5570287076301818702j), tol=ATOL)
1172
+ assert ae(v.real, -0.49739047283060471093, tol=PTOL)
1173
+ assert ae(v.imag, 3.5570287076301818702, tol=PTOL)
1174
+ v = fp.ei((2.0 + 8.0j))
1175
+ assert ae(v, (0.8705211147733730969 + 3.3825859385758486351j), tol=ATOL)
1176
+ assert ae(v.real, 0.8705211147733730969, tol=PTOL)
1177
+ assert ae(v.imag, 3.3825859385758486351, tol=PTOL)
1178
+ v = fp.ei((5.0 + 20.0j))
1179
+ assert ae(v, (7.0789514293925893007 + 1.5313749363937141849j), tol=ATOL)
1180
+ assert ae(v.real, 7.0789514293925893007, tol=PTOL)
1181
+ assert ae(v.imag, 1.5313749363937141849, tol=PTOL)
1182
+ v = fp.ei((20.0 + 80.0j))
1183
+ assert ae(v, (-5855431.4907298084434 - 720917.79156143806727j), tol=ATOL)
1184
+ assert ae(v.real, -5855431.4907298084434, tol=PTOL)
1185
+ assert ae(v.imag, -720917.79156143806727, tol=PTOL)
1186
+ v = fp.ei((30.0 + 120.0j))
1187
+ assert ae(v, (65402491644.703470747 - 56697658396.51586764j), tol=ATOL)
1188
+ assert ae(v.real, 65402491644.703470747, tol=PTOL)
1189
+ assert ae(v.imag, -56697658396.51586764, tol=PTOL)
1190
+ v = fp.ei((40.0 + 160.0j))
1191
+ assert ae(v, (-25504929379604.776769 + 1429035198630576.3879j), tol=ATOL)
1192
+ assert ae(v.real, -25504929379604.776769, tol=PTOL)
1193
+ assert ae(v.imag, 1429035198630576.3879, tol=PTOL)
1194
+ v = fp.ei((50.0 + 200.0j))
1195
+ assert ae(v, (-18437746526988116954.0 - 17146362239046152342.0j), tol=ATOL)
1196
+ assert ae(v.real, -18437746526988116954.0, tol=PTOL)
1197
+ assert ae(v.imag, -17146362239046152342.0, tol=PTOL)
1198
+ v = fp.ei((80.0 + 320.0j))
1199
+ assert ae(v, (-3.3464697299634526706e+31 - 1.6473152633843023919e+32j), tol=ATOL)
1200
+ assert ae(v.real, -3.3464697299634526706e+31, tol=PTOL)
1201
+ assert ae(v.imag, -1.6473152633843023919e+32, tol=PTOL)
1202
+ v = fp.ei((0.0 + 1.1641532182693481445e-10j))
1203
+ assert ae(v, (-22.29664129357666235 + 1.5707963269113119411j), tol=ATOL)
1204
+ assert ae(v.real, -22.29664129357666235, tol=PTOL)
1205
+ assert ae(v.imag, 1.5707963269113119411, tol=PTOL)
1206
+ v = fp.ei((0.0 + 0.25j))
1207
+ assert ae(v, (-0.82466306258094565309 + 1.8199298971146537833j), tol=ATOL)
1208
+ assert ae(v.real, -0.82466306258094565309, tol=PTOL)
1209
+ assert ae(v.imag, 1.8199298971146537833, tol=PTOL)
1210
+ v = fp.ei((0.0 + 1.0j))
1211
+ assert ae(v, (0.33740392290096813466 + 2.5168793971620796342j), tol=ATOL)
1212
+ assert ae(v.real, 0.33740392290096813466, tol=PTOL)
1213
+ assert ae(v.imag, 2.5168793971620796342, tol=PTOL)
1214
+ v = fp.ei((0.0 + 2.0j))
1215
+ assert ae(v, (0.4229808287748649957 + 3.1762093035975914678j), tol=ATOL)
1216
+ assert ae(v.real, 0.4229808287748649957, tol=PTOL)
1217
+ assert ae(v.imag, 3.1762093035975914678, tol=PTOL)
1218
+ v = fp.ei((0.0 + 5.0j))
1219
+ assert ae(v, (-0.19002974965664387862 + 3.1207275717395707565j), tol=ATOL)
1220
+ assert ae(v.real, -0.19002974965664387862, tol=PTOL)
1221
+ assert ae(v.imag, 3.1207275717395707565, tol=PTOL)
1222
+ v = fp.ei((0.0 + 20.0j))
1223
+ assert ae(v, (0.04441982084535331654 + 3.1190380278383364594j), tol=ATOL)
1224
+ assert ae(v.real, 0.04441982084535331654, tol=PTOL)
1225
+ assert ae(v.imag, 3.1190380278383364594, tol=PTOL)
1226
+ v = fp.ei((0.0 + 30.0j))
1227
+ assert ae(v, (-0.033032417282071143779 + 3.1375528668252477302j), tol=ATOL)
1228
+ assert ae(v.real, -0.033032417282071143779, tol=PTOL)
1229
+ assert ae(v.imag, 3.1375528668252477302, tol=PTOL)
1230
+ v = fp.ei((0.0 + 40.0j))
1231
+ assert ae(v, (0.019020007896208766962 + 3.157781446149681126j), tol=ATOL)
1232
+ assert ae(v.real, 0.019020007896208766962, tol=PTOL)
1233
+ assert ae(v.imag, 3.157781446149681126, tol=PTOL)
1234
+ v = fp.ei((0.0 + 50.0j))
1235
+ assert ae(v, (-0.0056283863241163054402 + 3.122413399280832514j), tol=ATOL)
1236
+ assert ae(v.real, -0.0056283863241163054402, tol=PTOL)
1237
+ assert ae(v.imag, 3.122413399280832514, tol=PTOL)
1238
+ v = fp.ei((0.0 + 80.0j))
1239
+ assert ae(v, (-0.012402501155070958192 + 3.1431272137073839346j), tol=ATOL)
1240
+ assert ae(v.real, -0.012402501155070958192, tol=PTOL)
1241
+ assert ae(v.imag, 3.1431272137073839346, tol=PTOL)
1242
+ v = fp.ei((-1.1641532182693481445e-10 + 4.6566128730773925781e-10j))
1243
+ assert ae(v, (-20.880034621664969632 + 1.8157749903874220607j), tol=ATOL)
1244
+ assert ae(v.real, -20.880034621664969632, tol=PTOL)
1245
+ assert ae(v.imag, 1.8157749903874220607, tol=PTOL)
1246
+ v = fp.ei((-0.25 + 1.0j))
1247
+ assert ae(v, (0.16868306393667788761 + 2.6557914649950505414j), tol=ATOL)
1248
+ assert ae(v.real, 0.16868306393667788761, tol=PTOL)
1249
+ assert ae(v.imag, 2.6557914649950505414, tol=PTOL)
1250
+ v = fp.ei((-1.0 + 4.0j))
1251
+ assert ae(v, (-0.03373591813926547318 + 3.2151161058308770603j), tol=ATOL)
1252
+ assert ae(v.real, -0.03373591813926547318, tol=PTOL)
1253
+ assert ae(v.imag, 3.2151161058308770603, tol=PTOL)
1254
+ v = fp.ei((-2.0 + 8.0j))
1255
+ assert ae(v, (0.015392833434733785143 + 3.1384179414340326969j), tol=ATOL)
1256
+ assert ae(v.real, 0.015392833434733785143, tol=PTOL)
1257
+ assert ae(v.imag, 3.1384179414340326969, tol=PTOL)
1258
+ v = fp.ei((-5.0 + 20.0j))
1259
+ assert ae(v, (0.00024419662286542966525 + 3.1413825703601317109j), tol=ATOL)
1260
+ assert ae(v.real, 0.00024419662286542966525, tol=PTOL)
1261
+ assert ae(v.imag, 3.1413825703601317109, tol=PTOL)
1262
+ v = fp.ei((-20.0 + 80.0j))
1263
+ assert ae(v, (-2.3255552781051330088e-11 + 3.1415926535987396304j), tol=ATOL)
1264
+ assert ae(v.real, -2.3255552781051330088e-11, tol=PTOL)
1265
+ assert ae(v.imag, 3.1415926535987396304, tol=PTOL)
1266
+ v = fp.ei((-30.0 + 120.0j))
1267
+ assert ae(v, (2.7068919097124652332e-16 + 3.1415926535897925337j), tol=ATOL)
1268
+ assert ae(v.real, 2.7068919097124652332e-16, tol=PTOL)
1269
+ assert ae(v.imag, 3.1415926535897925337, tol=PTOL)
1270
+ v = fp.ei((-40.0 + 160.0j))
1271
+ assert ae(v, (1.1695597827678024687e-20 + 3.1415926535897932385j), tol=ATOL)
1272
+ assert ae(v.real, 1.1695597827678024687e-20, tol=PTOL)
1273
+ assert ae(v.imag, 3.1415926535897932385, tol=PTOL)
1274
+ v = fp.ei((-50.0 + 200.0j))
1275
+ assert ae(v, (-9.0323746914410162531e-25 + 3.1415926535897932385j), tol=ATOL)
1276
+ assert ae(v.real, -9.0323746914410162531e-25, tol=PTOL)
1277
+ assert ae(v.imag, 3.1415926535897932385, tol=PTOL)
1278
+ v = fp.ei((-80.0 + 320.0j))
1279
+ assert ae(v, (-3.4819106748728063576e-38 + 3.1415926535897932385j), tol=ATOL)
1280
+ assert ae(v.real, -3.4819106748728063576e-38, tol=PTOL)
1281
+ assert ae(v.imag, 3.1415926535897932385, tol=PTOL)
1282
+ v = fp.ei((-4.6566128730773925781e-10 + 1.1641532182693481445e-10j))
1283
+ assert ae(v, (-20.880034622014215597 + 2.8966139905793444061j), tol=ATOL)
1284
+ assert ae(v.real, -20.880034622014215597, tol=PTOL)
1285
+ assert ae(v.imag, 2.8966139905793444061, tol=PTOL)
1286
+ v = fp.ei((-1.0 + 0.25j))
1287
+ assert ae(v, (-0.19731063945004229095 + 3.0542266078154932748j), tol=ATOL)
1288
+ assert ae(v.real, -0.19731063945004229095, tol=PTOL)
1289
+ assert ae(v.imag, 3.0542266078154932748, tol=PTOL)
1290
+ v = fp.ei((-4.0 + 1.0j))
1291
+ assert ae(v, (-0.0013106173980145506944 + 3.1381384055698581758j), tol=ATOL)
1292
+ assert ae(v.real, -0.0013106173980145506944, tol=PTOL)
1293
+ assert ae(v.imag, 3.1381384055698581758, tol=PTOL)
1294
+ v = fp.ei((-8.0 + 2.0j))
1295
+ assert ae(v, (0.000022278049065270225945 + 3.1415634616493367169j), tol=ATOL)
1296
+ assert ae(v.real, 0.000022278049065270225945, tol=PTOL)
1297
+ assert ae(v.imag, 3.1415634616493367169, tol=PTOL)
1298
+ v = fp.ei((-20.0 + 5.0j))
1299
+ assert ae(v, (-4.7711374515765346894e-11 + 3.1415926536726958909j), tol=ATOL)
1300
+ assert ae(v.real, -4.7711374515765346894e-11, tol=PTOL)
1301
+ assert ae(v.imag, 3.1415926536726958909, tol=PTOL)
1302
+ v = fp.ei((-80.0 + 20.0j))
1303
+ assert ae(v, (-3.8353473865788235787e-38 + 3.1415926535897932385j), tol=ATOL)
1304
+ assert ae(v.real, -3.8353473865788235787e-38, tol=PTOL)
1305
+ assert ae(v.imag, 3.1415926535897932385, tol=PTOL)
1306
+ v = fp.ei((-120.0 + 30.0j))
1307
+ assert ae(v, (-2.3836002337480334716e-55 + 3.1415926535897932385j), tol=ATOL)
1308
+ assert ae(v.real, -2.3836002337480334716e-55, tol=PTOL)
1309
+ assert ae(v.imag, 3.1415926535897932385, tol=PTOL)
1310
+ v = fp.ei((-160.0 + 40.0j))
1311
+ assert ae(v, (1.6238022898654510661e-72 + 3.1415926535897932385j), tol=ATOL)
1312
+ assert ae(v.real, 1.6238022898654510661e-72, tol=PTOL)
1313
+ assert ae(v.imag, 3.1415926535897932385, tol=PTOL)
1314
+ v = fp.ei((-200.0 + 50.0j))
1315
+ assert ae(v, (-6.6800061461666228487e-90 + 3.1415926535897932385j), tol=ATOL)
1316
+ assert ae(v.real, -6.6800061461666228487e-90, tol=PTOL)
1317
+ assert ae(v.imag, 3.1415926535897932385, tol=PTOL)
1318
+ v = fp.ei((-320.0 + 80.0j))
1319
+ assert ae(v, (-4.2737871527778786157e-143 + 3.1415926535897932385j), tol=ATOL)
1320
+ assert ae(v.real, -4.2737871527778786157e-143, tol=PTOL)
1321
+ assert ae(v.imag, 3.1415926535897932385, tol=PTOL)
1322
+ v = fp.ei(-1.1641532182693481445e-10)
1323
+ assert ae(v, -22.296641293693077672, tol=ATOL)
1324
+ assert type(v) is float
1325
+ v = fp.ei(-0.25)
1326
+ assert ae(v, -1.0442826344437381945, tol=ATOL)
1327
+ assert type(v) is float
1328
+ v = fp.ei(-1.0)
1329
+ assert ae(v, -0.21938393439552027368, tol=ATOL)
1330
+ assert type(v) is float
1331
+ v = fp.ei(-2.0)
1332
+ assert ae(v, -0.048900510708061119567, tol=ATOL)
1333
+ assert type(v) is float
1334
+ v = fp.ei(-5.0)
1335
+ assert ae(v, -0.0011482955912753257973, tol=ATOL)
1336
+ assert type(v) is float
1337
+ v = fp.ei(-20.0)
1338
+ assert ae(v, -9.8355252906498816904e-11, tol=ATOL)
1339
+ assert type(v) is float
1340
+ v = fp.ei(-30.0)
1341
+ assert ae(v, -3.0215520106888125448e-15, tol=ATOL)
1342
+ assert type(v) is float
1343
+ v = fp.ei(-40.0)
1344
+ assert ae(v, -1.0367732614516569722e-19, tol=ATOL)
1345
+ assert type(v) is float
1346
+ v = fp.ei(-50.0)
1347
+ assert ae(v, -3.7832640295504590187e-24, tol=ATOL)
1348
+ assert type(v) is float
1349
+ v = fp.ei(-80.0)
1350
+ assert ae(v, -2.2285432586884729112e-37, tol=ATOL)
1351
+ assert type(v) is float
1352
+ v = fp.ei((-1.1641532182693481445e-10 + 0.0j))
1353
+ assert ae(v, (-22.296641293693077672 + 0.0j), tol=ATOL)
1354
+ assert ae(v.real, -22.296641293693077672, tol=PTOL)
1355
+ assert v.imag == 0
1356
+ v = fp.ei((-0.25 + 0.0j))
1357
+ assert ae(v, (-1.0442826344437381945 + 0.0j), tol=ATOL)
1358
+ assert ae(v.real, -1.0442826344437381945, tol=PTOL)
1359
+ assert v.imag == 0
1360
+ v = fp.ei((-1.0 + 0.0j))
1361
+ assert ae(v, (-0.21938393439552027368 + 0.0j), tol=ATOL)
1362
+ assert ae(v.real, -0.21938393439552027368, tol=PTOL)
1363
+ assert v.imag == 0
1364
+ v = fp.ei((-2.0 + 0.0j))
1365
+ assert ae(v, (-0.048900510708061119567 + 0.0j), tol=ATOL)
1366
+ assert ae(v.real, -0.048900510708061119567, tol=PTOL)
1367
+ assert v.imag == 0
1368
+ v = fp.ei((-5.0 + 0.0j))
1369
+ assert ae(v, (-0.0011482955912753257973 + 0.0j), tol=ATOL)
1370
+ assert ae(v.real, -0.0011482955912753257973, tol=PTOL)
1371
+ assert v.imag == 0
1372
+ v = fp.ei((-20.0 + 0.0j))
1373
+ assert ae(v, (-9.8355252906498816904e-11 + 0.0j), tol=ATOL)
1374
+ assert ae(v.real, -9.8355252906498816904e-11, tol=PTOL)
1375
+ assert v.imag == 0
1376
+ v = fp.ei((-30.0 + 0.0j))
1377
+ assert ae(v, (-3.0215520106888125448e-15 + 0.0j), tol=ATOL)
1378
+ assert ae(v.real, -3.0215520106888125448e-15, tol=PTOL)
1379
+ assert v.imag == 0
1380
+ v = fp.ei((-40.0 + 0.0j))
1381
+ assert ae(v, (-1.0367732614516569722e-19 + 0.0j), tol=ATOL)
1382
+ assert ae(v.real, -1.0367732614516569722e-19, tol=PTOL)
1383
+ assert v.imag == 0
1384
+ v = fp.ei((-50.0 + 0.0j))
1385
+ assert ae(v, (-3.7832640295504590187e-24 + 0.0j), tol=ATOL)
1386
+ assert ae(v.real, -3.7832640295504590187e-24, tol=PTOL)
1387
+ assert v.imag == 0
1388
+ v = fp.ei((-80.0 + 0.0j))
1389
+ assert ae(v, (-2.2285432586884729112e-37 + 0.0j), tol=ATOL)
1390
+ assert ae(v.real, -2.2285432586884729112e-37, tol=PTOL)
1391
+ assert v.imag == 0
1392
+ v = fp.ei((-4.6566128730773925781e-10 - 1.1641532182693481445e-10j))
1393
+ assert ae(v, (-20.880034622014215597 - 2.8966139905793444061j), tol=ATOL)
1394
+ assert ae(v.real, -20.880034622014215597, tol=PTOL)
1395
+ assert ae(v.imag, -2.8966139905793444061, tol=PTOL)
1396
+ v = fp.ei((-1.0 - 0.25j))
1397
+ assert ae(v, (-0.19731063945004229095 - 3.0542266078154932748j), tol=ATOL)
1398
+ assert ae(v.real, -0.19731063945004229095, tol=PTOL)
1399
+ assert ae(v.imag, -3.0542266078154932748, tol=PTOL)
1400
+ v = fp.ei((-4.0 - 1.0j))
1401
+ assert ae(v, (-0.0013106173980145506944 - 3.1381384055698581758j), tol=ATOL)
1402
+ assert ae(v.real, -0.0013106173980145506944, tol=PTOL)
1403
+ assert ae(v.imag, -3.1381384055698581758, tol=PTOL)
1404
+ v = fp.ei((-8.0 - 2.0j))
1405
+ assert ae(v, (0.000022278049065270225945 - 3.1415634616493367169j), tol=ATOL)
1406
+ assert ae(v.real, 0.000022278049065270225945, tol=PTOL)
1407
+ assert ae(v.imag, -3.1415634616493367169, tol=PTOL)
1408
+ v = fp.ei((-20.0 - 5.0j))
1409
+ assert ae(v, (-4.7711374515765346894e-11 - 3.1415926536726958909j), tol=ATOL)
1410
+ assert ae(v.real, -4.7711374515765346894e-11, tol=PTOL)
1411
+ assert ae(v.imag, -3.1415926536726958909, tol=PTOL)
1412
+ v = fp.ei((-80.0 - 20.0j))
1413
+ assert ae(v, (-3.8353473865788235787e-38 - 3.1415926535897932385j), tol=ATOL)
1414
+ assert ae(v.real, -3.8353473865788235787e-38, tol=PTOL)
1415
+ assert ae(v.imag, -3.1415926535897932385, tol=PTOL)
1416
+ v = fp.ei((-120.0 - 30.0j))
1417
+ assert ae(v, (-2.3836002337480334716e-55 - 3.1415926535897932385j), tol=ATOL)
1418
+ assert ae(v.real, -2.3836002337480334716e-55, tol=PTOL)
1419
+ assert ae(v.imag, -3.1415926535897932385, tol=PTOL)
1420
+ v = fp.ei((-160.0 - 40.0j))
1421
+ assert ae(v, (1.6238022898654510661e-72 - 3.1415926535897932385j), tol=ATOL)
1422
+ assert ae(v.real, 1.6238022898654510661e-72, tol=PTOL)
1423
+ assert ae(v.imag, -3.1415926535897932385, tol=PTOL)
1424
+ v = fp.ei((-200.0 - 50.0j))
1425
+ assert ae(v, (-6.6800061461666228487e-90 - 3.1415926535897932385j), tol=ATOL)
1426
+ assert ae(v.real, -6.6800061461666228487e-90, tol=PTOL)
1427
+ assert ae(v.imag, -3.1415926535897932385, tol=PTOL)
1428
+ v = fp.ei((-320.0 - 80.0j))
1429
+ assert ae(v, (-4.2737871527778786157e-143 - 3.1415926535897932385j), tol=ATOL)
1430
+ assert ae(v.real, -4.2737871527778786157e-143, tol=PTOL)
1431
+ assert ae(v.imag, -3.1415926535897932385, tol=PTOL)
1432
+ v = fp.ei((-1.1641532182693481445e-10 - 1.1641532182693481445e-10j))
1433
+ assert ae(v, (-21.950067703413105017 - 2.3561944903087602507j), tol=ATOL)
1434
+ assert ae(v.real, -21.950067703413105017, tol=PTOL)
1435
+ assert ae(v.imag, -2.3561944903087602507, tol=PTOL)
1436
+ v = fp.ei((-0.25 - 0.25j))
1437
+ assert ae(v, (-0.71092525792923287894 - 2.5766745291767512913j), tol=ATOL)
1438
+ assert ae(v.real, -0.71092525792923287894, tol=PTOL)
1439
+ assert ae(v.imag, -2.5766745291767512913, tol=PTOL)
1440
+ v = fp.ei((-1.0 - 1.0j))
1441
+ assert ae(v, (-0.00028162445198141832551 - 2.9622681185504342983j), tol=ATOL)
1442
+ assert ae(v.real, -0.00028162445198141832551, tol=PTOL)
1443
+ assert ae(v.imag, -2.9622681185504342983, tol=PTOL)
1444
+ v = fp.ei((-2.0 - 2.0j))
1445
+ assert ae(v, (0.033767089606562004246 - 3.1229932394200426965j), tol=ATOL)
1446
+ assert ae(v.real, 0.033767089606562004246, tol=PTOL)
1447
+ assert ae(v.imag, -3.1229932394200426965, tol=PTOL)
1448
+ v = fp.ei((-5.0 - 5.0j))
1449
+ assert ae(v, (-0.0007266506660356393891 - 3.1420636813914284609j), tol=ATOL)
1450
+ assert ae(v.real, -0.0007266506660356393891, tol=PTOL)
1451
+ assert ae(v.imag, -3.1420636813914284609, tol=PTOL)
1452
+ v = fp.ei((-20.0 - 20.0j))
1453
+ assert ae(v, (2.3824537449367396579e-11 - 3.1415926535228233653j), tol=ATOL)
1454
+ assert ae(v.real, 2.3824537449367396579e-11, tol=PTOL)
1455
+ assert ae(v.imag, -3.1415926535228233653, tol=PTOL)
1456
+ v = fp.ei((-30.0 - 30.0j))
1457
+ assert ae(v, (-1.7316045841744061617e-15 - 3.141592653589794545j), tol=ATOL)
1458
+ assert ae(v.real, -1.7316045841744061617e-15, tol=PTOL)
1459
+ assert ae(v.imag, -3.141592653589794545, tol=PTOL)
1460
+ v = fp.ei((-40.0 - 40.0j))
1461
+ assert ae(v, (7.4001043002899232182e-20 - 3.1415926535897932385j), tol=ATOL)
1462
+ assert ae(v.real, 7.4001043002899232182e-20, tol=PTOL)
1463
+ assert ae(v.imag, -3.1415926535897932385, tol=PTOL)
1464
+ v = fp.ei((-50.0 - 50.0j))
1465
+ assert ae(v, (-2.3566128324644641219e-24 - 3.1415926535897932385j), tol=ATOL)
1466
+ assert ae(v.real, -2.3566128324644641219e-24, tol=PTOL)
1467
+ assert ae(v.imag, -3.1415926535897932385, tol=PTOL)
1468
+ v = fp.ei((-80.0 - 80.0j))
1469
+ assert ae(v, (-9.8279750572186526673e-38 - 3.1415926535897932385j), tol=ATOL)
1470
+ assert ae(v.real, -9.8279750572186526673e-38, tol=PTOL)
1471
+ assert ae(v.imag, -3.1415926535897932385, tol=PTOL)
1472
+ v = fp.ei((-1.1641532182693481445e-10 - 4.6566128730773925781e-10j))
1473
+ assert ae(v, (-20.880034621664969632 - 1.8157749903874220607j), tol=ATOL)
1474
+ assert ae(v.real, -20.880034621664969632, tol=PTOL)
1475
+ assert ae(v.imag, -1.8157749903874220607, tol=PTOL)
1476
+ v = fp.ei((-0.25 - 1.0j))
1477
+ assert ae(v, (0.16868306393667788761 - 2.6557914649950505414j), tol=ATOL)
1478
+ assert ae(v.real, 0.16868306393667788761, tol=PTOL)
1479
+ assert ae(v.imag, -2.6557914649950505414, tol=PTOL)
1480
+ v = fp.ei((-1.0 - 4.0j))
1481
+ assert ae(v, (-0.03373591813926547318 - 3.2151161058308770603j), tol=ATOL)
1482
+ assert ae(v.real, -0.03373591813926547318, tol=PTOL)
1483
+ assert ae(v.imag, -3.2151161058308770603, tol=PTOL)
1484
+ v = fp.ei((-2.0 - 8.0j))
1485
+ assert ae(v, (0.015392833434733785143 - 3.1384179414340326969j), tol=ATOL)
1486
+ assert ae(v.real, 0.015392833434733785143, tol=PTOL)
1487
+ assert ae(v.imag, -3.1384179414340326969, tol=PTOL)
1488
+ v = fp.ei((-5.0 - 20.0j))
1489
+ assert ae(v, (0.00024419662286542966525 - 3.1413825703601317109j), tol=ATOL)
1490
+ assert ae(v.real, 0.00024419662286542966525, tol=PTOL)
1491
+ assert ae(v.imag, -3.1413825703601317109, tol=PTOL)
1492
+ v = fp.ei((-20.0 - 80.0j))
1493
+ assert ae(v, (-2.3255552781051330088e-11 - 3.1415926535987396304j), tol=ATOL)
1494
+ assert ae(v.real, -2.3255552781051330088e-11, tol=PTOL)
1495
+ assert ae(v.imag, -3.1415926535987396304, tol=PTOL)
1496
+ v = fp.ei((-30.0 - 120.0j))
1497
+ assert ae(v, (2.7068919097124652332e-16 - 3.1415926535897925337j), tol=ATOL)
1498
+ assert ae(v.real, 2.7068919097124652332e-16, tol=PTOL)
1499
+ assert ae(v.imag, -3.1415926535897925337, tol=PTOL)
1500
+ v = fp.ei((-40.0 - 160.0j))
1501
+ assert ae(v, (1.1695597827678024687e-20 - 3.1415926535897932385j), tol=ATOL)
1502
+ assert ae(v.real, 1.1695597827678024687e-20, tol=PTOL)
1503
+ assert ae(v.imag, -3.1415926535897932385, tol=PTOL)
1504
+ v = fp.ei((-50.0 - 200.0j))
1505
+ assert ae(v, (-9.0323746914410162531e-25 - 3.1415926535897932385j), tol=ATOL)
1506
+ assert ae(v.real, -9.0323746914410162531e-25, tol=PTOL)
1507
+ assert ae(v.imag, -3.1415926535897932385, tol=PTOL)
1508
+ v = fp.ei((-80.0 - 320.0j))
1509
+ assert ae(v, (-3.4819106748728063576e-38 - 3.1415926535897932385j), tol=ATOL)
1510
+ assert ae(v.real, -3.4819106748728063576e-38, tol=PTOL)
1511
+ assert ae(v.imag, -3.1415926535897932385, tol=PTOL)
1512
+ v = fp.ei((0.0 - 1.1641532182693481445e-10j))
1513
+ assert ae(v, (-22.29664129357666235 - 1.5707963269113119411j), tol=ATOL)
1514
+ assert ae(v.real, -22.29664129357666235, tol=PTOL)
1515
+ assert ae(v.imag, -1.5707963269113119411, tol=PTOL)
1516
+ v = fp.ei((0.0 - 0.25j))
1517
+ assert ae(v, (-0.82466306258094565309 - 1.8199298971146537833j), tol=ATOL)
1518
+ assert ae(v.real, -0.82466306258094565309, tol=PTOL)
1519
+ assert ae(v.imag, -1.8199298971146537833, tol=PTOL)
1520
+ v = fp.ei((0.0 - 1.0j))
1521
+ assert ae(v, (0.33740392290096813466 - 2.5168793971620796342j), tol=ATOL)
1522
+ assert ae(v.real, 0.33740392290096813466, tol=PTOL)
1523
+ assert ae(v.imag, -2.5168793971620796342, tol=PTOL)
1524
+ v = fp.ei((0.0 - 2.0j))
1525
+ assert ae(v, (0.4229808287748649957 - 3.1762093035975914678j), tol=ATOL)
1526
+ assert ae(v.real, 0.4229808287748649957, tol=PTOL)
1527
+ assert ae(v.imag, -3.1762093035975914678, tol=PTOL)
1528
+ v = fp.ei((0.0 - 5.0j))
1529
+ assert ae(v, (-0.19002974965664387862 - 3.1207275717395707565j), tol=ATOL)
1530
+ assert ae(v.real, -0.19002974965664387862, tol=PTOL)
1531
+ assert ae(v.imag, -3.1207275717395707565, tol=PTOL)
1532
+ v = fp.ei((0.0 - 20.0j))
1533
+ assert ae(v, (0.04441982084535331654 - 3.1190380278383364594j), tol=ATOL)
1534
+ assert ae(v.real, 0.04441982084535331654, tol=PTOL)
1535
+ assert ae(v.imag, -3.1190380278383364594, tol=PTOL)
1536
+ v = fp.ei((0.0 - 30.0j))
1537
+ assert ae(v, (-0.033032417282071143779 - 3.1375528668252477302j), tol=ATOL)
1538
+ assert ae(v.real, -0.033032417282071143779, tol=PTOL)
1539
+ assert ae(v.imag, -3.1375528668252477302, tol=PTOL)
1540
+ v = fp.ei((0.0 - 40.0j))
1541
+ assert ae(v, (0.019020007896208766962 - 3.157781446149681126j), tol=ATOL)
1542
+ assert ae(v.real, 0.019020007896208766962, tol=PTOL)
1543
+ assert ae(v.imag, -3.157781446149681126, tol=PTOL)
1544
+ v = fp.ei((0.0 - 50.0j))
1545
+ assert ae(v, (-0.0056283863241163054402 - 3.122413399280832514j), tol=ATOL)
1546
+ assert ae(v.real, -0.0056283863241163054402, tol=PTOL)
1547
+ assert ae(v.imag, -3.122413399280832514, tol=PTOL)
1548
+ v = fp.ei((0.0 - 80.0j))
1549
+ assert ae(v, (-0.012402501155070958192 - 3.1431272137073839346j), tol=ATOL)
1550
+ assert ae(v.real, -0.012402501155070958192, tol=PTOL)
1551
+ assert ae(v.imag, -3.1431272137073839346, tol=PTOL)
1552
+ v = fp.ei((1.1641532182693481445e-10 - 4.6566128730773925781e-10j))
1553
+ assert ae(v, (-20.880034621432138988 - 1.3258176641336937524j), tol=ATOL)
1554
+ assert ae(v.real, -20.880034621432138988, tol=PTOL)
1555
+ assert ae(v.imag, -1.3258176641336937524, tol=PTOL)
1556
+ v = fp.ei((0.25 - 1.0j))
1557
+ assert ae(v, (0.59066621214766308594 - 2.3968481059377428687j), tol=ATOL)
1558
+ assert ae(v.real, 0.59066621214766308594, tol=PTOL)
1559
+ assert ae(v.imag, -2.3968481059377428687, tol=PTOL)
1560
+ v = fp.ei((1.0 - 4.0j))
1561
+ assert ae(v, (-0.49739047283060471093 - 3.5570287076301818702j), tol=ATOL)
1562
+ assert ae(v.real, -0.49739047283060471093, tol=PTOL)
1563
+ assert ae(v.imag, -3.5570287076301818702, tol=PTOL)
1564
+ v = fp.ei((2.0 - 8.0j))
1565
+ assert ae(v, (0.8705211147733730969 - 3.3825859385758486351j), tol=ATOL)
1566
+ assert ae(v.real, 0.8705211147733730969, tol=PTOL)
1567
+ assert ae(v.imag, -3.3825859385758486351, tol=PTOL)
1568
+ v = fp.ei((5.0 - 20.0j))
1569
+ assert ae(v, (7.0789514293925893007 - 1.5313749363937141849j), tol=ATOL)
1570
+ assert ae(v.real, 7.0789514293925893007, tol=PTOL)
1571
+ assert ae(v.imag, -1.5313749363937141849, tol=PTOL)
1572
+ v = fp.ei((20.0 - 80.0j))
1573
+ assert ae(v, (-5855431.4907298084434 + 720917.79156143806727j), tol=ATOL)
1574
+ assert ae(v.real, -5855431.4907298084434, tol=PTOL)
1575
+ assert ae(v.imag, 720917.79156143806727, tol=PTOL)
1576
+ v = fp.ei((30.0 - 120.0j))
1577
+ assert ae(v, (65402491644.703470747 + 56697658396.51586764j), tol=ATOL)
1578
+ assert ae(v.real, 65402491644.703470747, tol=PTOL)
1579
+ assert ae(v.imag, 56697658396.51586764, tol=PTOL)
1580
+ v = fp.ei((40.0 - 160.0j))
1581
+ assert ae(v, (-25504929379604.776769 - 1429035198630576.3879j), tol=ATOL)
1582
+ assert ae(v.real, -25504929379604.776769, tol=PTOL)
1583
+ assert ae(v.imag, -1429035198630576.3879, tol=PTOL)
1584
+ v = fp.ei((50.0 - 200.0j))
1585
+ assert ae(v, (-18437746526988116954.0 + 17146362239046152342.0j), tol=ATOL)
1586
+ assert ae(v.real, -18437746526988116954.0, tol=PTOL)
1587
+ assert ae(v.imag, 17146362239046152342.0, tol=PTOL)
1588
+ v = fp.ei((80.0 - 320.0j))
1589
+ assert ae(v, (-3.3464697299634526706e+31 + 1.6473152633843023919e+32j), tol=ATOL)
1590
+ assert ae(v.real, -3.3464697299634526706e+31, tol=PTOL)
1591
+ assert ae(v.imag, 1.6473152633843023919e+32, tol=PTOL)
1592
+ v = fp.ei((1.1641532182693481445e-10 - 1.1641532182693481445e-10j))
1593
+ assert ae(v, (-21.950067703180274374 - 0.78539816351386363145j), tol=ATOL)
1594
+ assert ae(v.real, -21.950067703180274374, tol=PTOL)
1595
+ assert ae(v.imag, -0.78539816351386363145, tol=PTOL)
1596
+ v = fp.ei((0.25 - 0.25j))
1597
+ assert ae(v, (-0.21441047326710323254 - 1.0683772981589995996j), tol=ATOL)
1598
+ assert ae(v.real, -0.21441047326710323254, tol=PTOL)
1599
+ assert ae(v.imag, -1.0683772981589995996, tol=PTOL)
1600
+ v = fp.ei((1.0 - 1.0j))
1601
+ assert ae(v, (1.7646259855638540684 - 2.3877698515105224193j), tol=ATOL)
1602
+ assert ae(v.real, 1.7646259855638540684, tol=PTOL)
1603
+ assert ae(v.imag, -2.3877698515105224193, tol=PTOL)
1604
+ v = fp.ei((2.0 - 2.0j))
1605
+ assert ae(v, (1.8920781621855474089 - 5.3169624378326579621j), tol=ATOL)
1606
+ assert ae(v.real, 1.8920781621855474089, tol=PTOL)
1607
+ assert ae(v.imag, -5.3169624378326579621, tol=PTOL)
1608
+ v = fp.ei((5.0 - 5.0j))
1609
+ assert ae(v, (-13.470936071475245856 + 15.322492395731230968j), tol=ATOL)
1610
+ assert ae(v.real, -13.470936071475245856, tol=PTOL)
1611
+ assert ae(v.imag, 15.322492395731230968, tol=PTOL)
1612
+ v = fp.ei((20.0 - 20.0j))
1613
+ assert ae(v, (16589317.398788971896 - 5831705.4712368307104j), tol=ATOL)
1614
+ assert ae(v.real, 16589317.398788971896, tol=PTOL)
1615
+ assert ae(v.imag, -5831705.4712368307104, tol=PTOL)
1616
+ v = fp.ei((30.0 - 30.0j))
1617
+ assert ae(v, (-154596484273.69322527 + 204179357834.2723043j), tol=ATOL)
1618
+ assert ae(v.real, -154596484273.69322527, tol=PTOL)
1619
+ assert ae(v.imag, 204179357834.2723043, tol=PTOL)
1620
+ v = fp.ei((40.0 - 40.0j))
1621
+ assert ae(v, (287512180321448.45408 - 4203502407932318.1156j), tol=ATOL)
1622
+ assert ae(v.real, 287512180321448.45408, tol=PTOL)
1623
+ assert ae(v.imag, -4203502407932318.1156, tol=PTOL)
1624
+ v = fp.ei((50.0 - 50.0j))
1625
+ assert ae(v, (36128528616649268826.0 + 64648801861338741960.0j), tol=ATOL)
1626
+ assert ae(v.real, 36128528616649268826.0, tol=PTOL)
1627
+ assert ae(v.imag, 64648801861338741960.0, tol=PTOL)
1628
+ v = fp.ei((80.0 - 80.0j))
1629
+ assert ae(v, (-3.8674816337930010217e+32 + 3.0540709639658071041e+32j), tol=ATOL)
1630
+ assert ae(v.real, -3.8674816337930010217e+32, tol=PTOL)
1631
+ assert ae(v.imag, 3.0540709639658071041e+32, tol=PTOL)
1632
+ v = fp.ei((4.6566128730773925781e-10 - 1.1641532182693481445e-10j))
1633
+ assert ae(v, (-20.880034621082893023 - 0.24497866324327947603j), tol=ATOL)
1634
+ assert ae(v.real, -20.880034621082893023, tol=PTOL)
1635
+ assert ae(v.imag, -0.24497866324327947603, tol=PTOL)
1636
+ v = fp.ei((1.0 - 0.25j))
1637
+ assert ae(v, (1.8942716983721074932 - 0.67268237088273915854j), tol=ATOL)
1638
+ assert ae(v.real, 1.8942716983721074932, tol=PTOL)
1639
+ assert ae(v.imag, -0.67268237088273915854, tol=PTOL)
1640
+ v = fp.ei((4.0 - 1.0j))
1641
+ assert ae(v, (14.806699492675420438 - 12.280015176673582616j), tol=ATOL)
1642
+ assert ae(v.real, 14.806699492675420438, tol=PTOL)
1643
+ assert ae(v.imag, -12.280015176673582616, tol=PTOL)
1644
+ v = fp.ei((8.0 - 2.0j))
1645
+ assert ae(v, (-54.633252667426386294 - 416.34477429173650012j), tol=ATOL)
1646
+ assert ae(v.real, -54.633252667426386294, tol=PTOL)
1647
+ assert ae(v.imag, -416.34477429173650012, tol=PTOL)
1648
+ v = fp.ei((20.0 - 5.0j))
1649
+ assert ae(v, (711836.97165402624643 + 24745247.798103247366j), tol=ATOL)
1650
+ assert ae(v.real, 711836.97165402624643, tol=PTOL)
1651
+ assert ae(v.imag, 24745247.798103247366, tol=PTOL)
1652
+ v = fp.ei((80.0 - 20.0j))
1653
+ assert ae(v, (4.2139911108612653091e+32 - 5.3367124741918251637e+32j), tol=ATOL)
1654
+ assert ae(v.real, 4.2139911108612653091e+32, tol=PTOL)
1655
+ assert ae(v.imag, -5.3367124741918251637e+32, tol=PTOL)
1656
+ v = fp.ei((120.0 - 30.0j))
1657
+ assert ae(v, (-9.7760616203707508892e+48 + 1.058257682317195792e+50j), tol=ATOL)
1658
+ assert ae(v.real, -9.7760616203707508892e+48, tol=PTOL)
1659
+ assert ae(v.imag, 1.058257682317195792e+50, tol=PTOL)
1660
+ v = fp.ei((160.0 - 40.0j))
1661
+ assert ae(v, (-8.7065541466623638861e+66 - 1.6577106725141739889e+67j), tol=ATOL)
1662
+ assert ae(v.real, -8.7065541466623638861e+66, tol=PTOL)
1663
+ assert ae(v.imag, -1.6577106725141739889e+67, tol=PTOL)
1664
+ v = fp.ei((200.0 - 50.0j))
1665
+ assert ae(v, (3.070744996327018106e+84 + 1.7243244846769415903e+84j), tol=ATOL)
1666
+ assert ae(v.real, 3.070744996327018106e+84, tol=PTOL)
1667
+ assert ae(v.imag, 1.7243244846769415903e+84, tol=PTOL)
1668
+ v = fp.ei((320.0 - 80.0j))
1669
+ assert ae(v, (-9.9960598637998647276e+135 + 2.6855081527595608863e+136j), tol=ATOL)
1670
+ assert ae(v.real, -9.9960598637998647276e+135, tol=PTOL)
1671
+ assert ae(v.imag, 2.6855081527595608863e+136, tol=PTOL)
llmeval-env/lib/python3.10/site-packages/mpmath/tests/test_functions.py ADDED
@@ -0,0 +1,920 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from mpmath.libmp import *
2
+ from mpmath import *
3
+ import random
4
+ import time
5
+ import math
6
+ import cmath
7
+
8
+ def mpc_ae(a, b, eps=eps):
9
+ res = True
10
+ res = res and a.real.ae(b.real, eps)
11
+ res = res and a.imag.ae(b.imag, eps)
12
+ return res
13
+
14
+ #----------------------------------------------------------------------------
15
+ # Constants and functions
16
+ #
17
+
18
+ tpi = "3.1415926535897932384626433832795028841971693993751058209749445923078\
19
+ 1640628620899862803482534211706798"
20
+ te = "2.71828182845904523536028747135266249775724709369995957496696762772407\
21
+ 663035354759457138217852516642743"
22
+ tdegree = "0.017453292519943295769236907684886127134428718885417254560971914\
23
+ 4017100911460344944368224156963450948221"
24
+ teuler = "0.5772156649015328606065120900824024310421593359399235988057672348\
25
+ 84867726777664670936947063291746749516"
26
+ tln2 = "0.693147180559945309417232121458176568075500134360255254120680009493\
27
+ 393621969694715605863326996418687542"
28
+ tln10 = "2.30258509299404568401799145468436420760110148862877297603332790096\
29
+ 757260967735248023599720508959829834"
30
+ tcatalan = "0.91596559417721901505460351493238411077414937428167213426649811\
31
+ 9621763019776254769479356512926115106249"
32
+ tkhinchin = "2.6854520010653064453097148354817956938203822939944629530511523\
33
+ 4555721885953715200280114117493184769800"
34
+ tglaisher = "1.2824271291006226368753425688697917277676889273250011920637400\
35
+ 2174040630885882646112973649195820237439420646"
36
+ tapery = "1.2020569031595942853997381615114499907649862923404988817922715553\
37
+ 4183820578631309018645587360933525815"
38
+ tphi = "1.618033988749894848204586834365638117720309179805762862135448622705\
39
+ 26046281890244970720720418939113748475"
40
+ tmertens = "0.26149721284764278375542683860869585905156664826119920619206421\
41
+ 3924924510897368209714142631434246651052"
42
+ ttwinprime = "0.660161815846869573927812110014555778432623360284733413319448\
43
+ 423335405642304495277143760031413839867912"
44
+
45
+ def test_constants():
46
+ for prec in [3, 7, 10, 15, 20, 37, 80, 100, 29]:
47
+ mp.dps = prec
48
+ assert pi == mpf(tpi)
49
+ assert e == mpf(te)
50
+ assert degree == mpf(tdegree)
51
+ assert euler == mpf(teuler)
52
+ assert ln2 == mpf(tln2)
53
+ assert ln10 == mpf(tln10)
54
+ assert catalan == mpf(tcatalan)
55
+ assert khinchin == mpf(tkhinchin)
56
+ assert glaisher == mpf(tglaisher)
57
+ assert phi == mpf(tphi)
58
+ if prec < 50:
59
+ assert mertens == mpf(tmertens)
60
+ assert twinprime == mpf(ttwinprime)
61
+ mp.dps = 15
62
+ assert pi >= -1
63
+ assert pi > 2
64
+ assert pi > 3
65
+ assert pi < 4
66
+
67
+ def test_exact_sqrts():
68
+ for i in range(20000):
69
+ assert sqrt(mpf(i*i)) == i
70
+ random.seed(1)
71
+ for prec in [100, 300, 1000, 10000]:
72
+ mp.dps = prec
73
+ for i in range(20):
74
+ A = random.randint(10**(prec//2-2), 10**(prec//2-1))
75
+ assert sqrt(mpf(A*A)) == A
76
+ mp.dps = 15
77
+ for i in range(100):
78
+ for a in [1, 8, 25, 112307]:
79
+ assert sqrt(mpf((a*a, 2*i))) == mpf((a, i))
80
+ assert sqrt(mpf((a*a, -2*i))) == mpf((a, -i))
81
+
82
+ def test_sqrt_rounding():
83
+ for i in [2, 3, 5, 6, 7, 8, 10, 11, 12, 13, 14, 15]:
84
+ i = from_int(i)
85
+ for dps in [7, 15, 83, 106, 2000]:
86
+ mp.dps = dps
87
+ a = mpf_pow_int(mpf_sqrt(i, mp.prec, round_down), 2, mp.prec, round_down)
88
+ b = mpf_pow_int(mpf_sqrt(i, mp.prec, round_up), 2, mp.prec, round_up)
89
+ assert mpf_lt(a, i)
90
+ assert mpf_gt(b, i)
91
+ random.seed(1234)
92
+ prec = 100
93
+ for rnd in [round_down, round_nearest, round_ceiling]:
94
+ for i in range(100):
95
+ a = mpf_rand(prec)
96
+ b = mpf_mul(a, a)
97
+ assert mpf_sqrt(b, prec, rnd) == a
98
+ # Test some extreme cases
99
+ mp.dps = 100
100
+ a = mpf(9) + 1e-90
101
+ b = mpf(9) - 1e-90
102
+ mp.dps = 15
103
+ assert sqrt(a, rounding='d') == 3
104
+ assert sqrt(a, rounding='n') == 3
105
+ assert sqrt(a, rounding='u') > 3
106
+ assert sqrt(b, rounding='d') < 3
107
+ assert sqrt(b, rounding='n') == 3
108
+ assert sqrt(b, rounding='u') == 3
109
+ # A worst case, from the MPFR test suite
110
+ assert sqrt(mpf('7.0503726185518891')) == mpf('2.655253776675949')
111
+
112
+ def test_float_sqrt():
113
+ mp.dps = 15
114
+ # These should round identically
115
+ for x in [0, 1e-7, 0.1, 0.5, 1, 2, 3, 4, 5, 0.333, 76.19]:
116
+ assert sqrt(mpf(x)) == float(x)**0.5
117
+ assert sqrt(-1) == 1j
118
+ assert sqrt(-2).ae(cmath.sqrt(-2))
119
+ assert sqrt(-3).ae(cmath.sqrt(-3))
120
+ assert sqrt(-100).ae(cmath.sqrt(-100))
121
+ assert sqrt(1j).ae(cmath.sqrt(1j))
122
+ assert sqrt(-1j).ae(cmath.sqrt(-1j))
123
+ assert sqrt(math.pi + math.e*1j).ae(cmath.sqrt(math.pi + math.e*1j))
124
+ assert sqrt(math.pi - math.e*1j).ae(cmath.sqrt(math.pi - math.e*1j))
125
+
126
+ def test_hypot():
127
+ assert hypot(0, 0) == 0
128
+ assert hypot(0, 0.33) == mpf(0.33)
129
+ assert hypot(0.33, 0) == mpf(0.33)
130
+ assert hypot(-0.33, 0) == mpf(0.33)
131
+ assert hypot(3, 4) == mpf(5)
132
+
133
+ def test_exact_cbrt():
134
+ for i in range(0, 20000, 200):
135
+ assert cbrt(mpf(i*i*i)) == i
136
+ random.seed(1)
137
+ for prec in [100, 300, 1000, 10000]:
138
+ mp.dps = prec
139
+ A = random.randint(10**(prec//2-2), 10**(prec//2-1))
140
+ assert cbrt(mpf(A*A*A)) == A
141
+ mp.dps = 15
142
+
143
+ def test_exp():
144
+ assert exp(0) == 1
145
+ assert exp(10000).ae(mpf('8.8068182256629215873e4342'))
146
+ assert exp(-10000).ae(mpf('1.1354838653147360985e-4343'))
147
+ a = exp(mpf((1, 8198646019315405, -53, 53)))
148
+ assert(a.bc == bitcount(a.man))
149
+ mp.prec = 67
150
+ a = exp(mpf((1, 1781864658064754565, -60, 61)))
151
+ assert(a.bc == bitcount(a.man))
152
+ mp.prec = 53
153
+ assert exp(ln2 * 10).ae(1024)
154
+ assert exp(2+2j).ae(cmath.exp(2+2j))
155
+
156
+ def test_issue_73():
157
+ mp.dps = 512
158
+ a = exp(-1)
159
+ b = exp(1)
160
+ mp.dps = 15
161
+ assert (+a).ae(0.36787944117144233)
162
+ assert (+b).ae(2.7182818284590451)
163
+
164
+ def test_log():
165
+ mp.dps = 15
166
+ assert log(1) == 0
167
+ for x in [0.5, 1.5, 2.0, 3.0, 100, 10**50, 1e-50]:
168
+ assert log(x).ae(math.log(x))
169
+ assert log(x, x) == 1
170
+ assert log(1024, 2) == 10
171
+ assert log(10**1234, 10) == 1234
172
+ assert log(2+2j).ae(cmath.log(2+2j))
173
+ # Accuracy near 1
174
+ assert (log(0.6+0.8j).real*10**17).ae(2.2204460492503131)
175
+ assert (log(0.6-0.8j).real*10**17).ae(2.2204460492503131)
176
+ assert (log(0.8-0.6j).real*10**17).ae(2.2204460492503131)
177
+ assert (log(1+1e-8j).real*10**16).ae(0.5)
178
+ assert (log(1-1e-8j).real*10**16).ae(0.5)
179
+ assert (log(-1+1e-8j).real*10**16).ae(0.5)
180
+ assert (log(-1-1e-8j).real*10**16).ae(0.5)
181
+ assert (log(1j+1e-8).real*10**16).ae(0.5)
182
+ assert (log(1j-1e-8).real*10**16).ae(0.5)
183
+ assert (log(-1j+1e-8).real*10**16).ae(0.5)
184
+ assert (log(-1j-1e-8).real*10**16).ae(0.5)
185
+ assert (log(1+1e-40j).real*10**80).ae(0.5)
186
+ assert (log(1j+1e-40).real*10**80).ae(0.5)
187
+ # Huge
188
+ assert log(ldexp(1.234,10**20)).ae(log(2)*1e20)
189
+ assert log(ldexp(1.234,10**200)).ae(log(2)*1e200)
190
+ # Some special values
191
+ assert log(mpc(0,0)) == mpc(-inf,0)
192
+ assert isnan(log(mpc(nan,0)).real)
193
+ assert isnan(log(mpc(nan,0)).imag)
194
+ assert isnan(log(mpc(0,nan)).real)
195
+ assert isnan(log(mpc(0,nan)).imag)
196
+ assert isnan(log(mpc(nan,1)).real)
197
+ assert isnan(log(mpc(nan,1)).imag)
198
+ assert isnan(log(mpc(1,nan)).real)
199
+ assert isnan(log(mpc(1,nan)).imag)
200
+
201
+ def test_trig_hyperb_basic():
202
+ for x in (list(range(100)) + list(range(-100,0))):
203
+ t = x / 4.1
204
+ assert cos(mpf(t)).ae(math.cos(t))
205
+ assert sin(mpf(t)).ae(math.sin(t))
206
+ assert tan(mpf(t)).ae(math.tan(t))
207
+ assert cosh(mpf(t)).ae(math.cosh(t))
208
+ assert sinh(mpf(t)).ae(math.sinh(t))
209
+ assert tanh(mpf(t)).ae(math.tanh(t))
210
+ assert sin(1+1j).ae(cmath.sin(1+1j))
211
+ assert sin(-4-3.6j).ae(cmath.sin(-4-3.6j))
212
+ assert cos(1+1j).ae(cmath.cos(1+1j))
213
+ assert cos(-4-3.6j).ae(cmath.cos(-4-3.6j))
214
+
215
+ def test_degrees():
216
+ assert cos(0*degree) == 1
217
+ assert cos(90*degree).ae(0)
218
+ assert cos(180*degree).ae(-1)
219
+ assert cos(270*degree).ae(0)
220
+ assert cos(360*degree).ae(1)
221
+ assert sin(0*degree) == 0
222
+ assert sin(90*degree).ae(1)
223
+ assert sin(180*degree).ae(0)
224
+ assert sin(270*degree).ae(-1)
225
+ assert sin(360*degree).ae(0)
226
+
227
+ def random_complexes(N):
228
+ random.seed(1)
229
+ a = []
230
+ for i in range(N):
231
+ x1 = random.uniform(-10, 10)
232
+ y1 = random.uniform(-10, 10)
233
+ x2 = random.uniform(-10, 10)
234
+ y2 = random.uniform(-10, 10)
235
+ z1 = complex(x1, y1)
236
+ z2 = complex(x2, y2)
237
+ a.append((z1, z2))
238
+ return a
239
+
240
+ def test_complex_powers():
241
+ for dps in [15, 30, 100]:
242
+ # Check accuracy for complex square root
243
+ mp.dps = dps
244
+ a = mpc(1j)**0.5
245
+ assert a.real == a.imag == mpf(2)**0.5 / 2
246
+ mp.dps = 15
247
+ random.seed(1)
248
+ for (z1, z2) in random_complexes(100):
249
+ assert (mpc(z1)**mpc(z2)).ae(z1**z2, 1e-12)
250
+ assert (e**(-pi*1j)).ae(-1)
251
+ mp.dps = 50
252
+ assert (e**(-pi*1j)).ae(-1)
253
+ mp.dps = 15
254
+
255
+ def test_complex_sqrt_accuracy():
256
+ def test_mpc_sqrt(lst):
257
+ for a, b in lst:
258
+ z = mpc(a + j*b)
259
+ assert mpc_ae(sqrt(z*z), z)
260
+ z = mpc(-a + j*b)
261
+ assert mpc_ae(sqrt(z*z), -z)
262
+ z = mpc(a - j*b)
263
+ assert mpc_ae(sqrt(z*z), z)
264
+ z = mpc(-a - j*b)
265
+ assert mpc_ae(sqrt(z*z), -z)
266
+ random.seed(2)
267
+ N = 10
268
+ mp.dps = 30
269
+ dps = mp.dps
270
+ test_mpc_sqrt([(random.uniform(0, 10),random.uniform(0, 10)) for i in range(N)])
271
+ test_mpc_sqrt([(i + 0.1, (i + 0.2)*10**i) for i in range(N)])
272
+ mp.dps = 15
273
+
274
+ def test_atan():
275
+ mp.dps = 15
276
+ assert atan(-2.3).ae(math.atan(-2.3))
277
+ assert atan(1e-50) == 1e-50
278
+ assert atan(1e50).ae(pi/2)
279
+ assert atan(-1e-50) == -1e-50
280
+ assert atan(-1e50).ae(-pi/2)
281
+ assert atan(10**1000).ae(pi/2)
282
+ for dps in [25, 70, 100, 300, 1000]:
283
+ mp.dps = dps
284
+ assert (4*atan(1)).ae(pi)
285
+ mp.dps = 15
286
+ pi2 = pi/2
287
+ assert atan(mpc(inf,-1)).ae(pi2)
288
+ assert atan(mpc(inf,0)).ae(pi2)
289
+ assert atan(mpc(inf,1)).ae(pi2)
290
+ assert atan(mpc(1,inf)).ae(pi2)
291
+ assert atan(mpc(0,inf)).ae(pi2)
292
+ assert atan(mpc(-1,inf)).ae(-pi2)
293
+ assert atan(mpc(-inf,1)).ae(-pi2)
294
+ assert atan(mpc(-inf,0)).ae(-pi2)
295
+ assert atan(mpc(-inf,-1)).ae(-pi2)
296
+ assert atan(mpc(-1,-inf)).ae(-pi2)
297
+ assert atan(mpc(0,-inf)).ae(-pi2)
298
+ assert atan(mpc(1,-inf)).ae(pi2)
299
+
300
+ def test_atan2():
301
+ mp.dps = 15
302
+ assert atan2(1,1).ae(pi/4)
303
+ assert atan2(1,-1).ae(3*pi/4)
304
+ assert atan2(-1,-1).ae(-3*pi/4)
305
+ assert atan2(-1,1).ae(-pi/4)
306
+ assert atan2(-1,0).ae(-pi/2)
307
+ assert atan2(1,0).ae(pi/2)
308
+ assert atan2(0,0) == 0
309
+ assert atan2(inf,0).ae(pi/2)
310
+ assert atan2(-inf,0).ae(-pi/2)
311
+ assert isnan(atan2(inf,inf))
312
+ assert isnan(atan2(-inf,inf))
313
+ assert isnan(atan2(inf,-inf))
314
+ assert isnan(atan2(3,nan))
315
+ assert isnan(atan2(nan,3))
316
+ assert isnan(atan2(0,nan))
317
+ assert isnan(atan2(nan,0))
318
+ assert atan2(0,inf) == 0
319
+ assert atan2(0,-inf).ae(pi)
320
+ assert atan2(10,inf) == 0
321
+ assert atan2(-10,inf) == 0
322
+ assert atan2(-10,-inf).ae(-pi)
323
+ assert atan2(10,-inf).ae(pi)
324
+ assert atan2(inf,10).ae(pi/2)
325
+ assert atan2(inf,-10).ae(pi/2)
326
+ assert atan2(-inf,10).ae(-pi/2)
327
+ assert atan2(-inf,-10).ae(-pi/2)
328
+
329
+ def test_areal_inverses():
330
+ assert asin(mpf(0)) == 0
331
+ assert asinh(mpf(0)) == 0
332
+ assert acosh(mpf(1)) == 0
333
+ assert isinstance(asin(mpf(0.5)), mpf)
334
+ assert isinstance(asin(mpf(2.0)), mpc)
335
+ assert isinstance(acos(mpf(0.5)), mpf)
336
+ assert isinstance(acos(mpf(2.0)), mpc)
337
+ assert isinstance(atanh(mpf(0.1)), mpf)
338
+ assert isinstance(atanh(mpf(1.1)), mpc)
339
+
340
+ random.seed(1)
341
+ for i in range(50):
342
+ x = random.uniform(0, 1)
343
+ assert asin(mpf(x)).ae(math.asin(x))
344
+ assert acos(mpf(x)).ae(math.acos(x))
345
+
346
+ x = random.uniform(-10, 10)
347
+ assert asinh(mpf(x)).ae(cmath.asinh(x).real)
348
+ assert isinstance(asinh(mpf(x)), mpf)
349
+ x = random.uniform(1, 10)
350
+ assert acosh(mpf(x)).ae(cmath.acosh(x).real)
351
+ assert isinstance(acosh(mpf(x)), mpf)
352
+ x = random.uniform(-10, 0.999)
353
+ assert isinstance(acosh(mpf(x)), mpc)
354
+
355
+ x = random.uniform(-1, 1)
356
+ assert atanh(mpf(x)).ae(cmath.atanh(x).real)
357
+ assert isinstance(atanh(mpf(x)), mpf)
358
+
359
+ dps = mp.dps
360
+ mp.dps = 300
361
+ assert isinstance(asin(0.5), mpf)
362
+ mp.dps = 1000
363
+ assert asin(1).ae(pi/2)
364
+ assert asin(-1).ae(-pi/2)
365
+ mp.dps = dps
366
+
367
+ def test_invhyperb_inaccuracy():
368
+ mp.dps = 15
369
+ assert (asinh(1e-5)*10**5).ae(0.99999999998333333)
370
+ assert (asinh(1e-10)*10**10).ae(1)
371
+ assert (asinh(1e-50)*10**50).ae(1)
372
+ assert (asinh(-1e-5)*10**5).ae(-0.99999999998333333)
373
+ assert (asinh(-1e-10)*10**10).ae(-1)
374
+ assert (asinh(-1e-50)*10**50).ae(-1)
375
+ assert asinh(10**20).ae(46.744849040440862)
376
+ assert asinh(-10**20).ae(-46.744849040440862)
377
+ assert (tanh(1e-10)*10**10).ae(1)
378
+ assert (tanh(-1e-10)*10**10).ae(-1)
379
+ assert (atanh(1e-10)*10**10).ae(1)
380
+ assert (atanh(-1e-10)*10**10).ae(-1)
381
+
382
+ def test_complex_functions():
383
+ for x in (list(range(10)) + list(range(-10,0))):
384
+ for y in (list(range(10)) + list(range(-10,0))):
385
+ z = complex(x, y)/4.3 + 0.01j
386
+ assert exp(mpc(z)).ae(cmath.exp(z))
387
+ assert log(mpc(z)).ae(cmath.log(z))
388
+ assert cos(mpc(z)).ae(cmath.cos(z))
389
+ assert sin(mpc(z)).ae(cmath.sin(z))
390
+ assert tan(mpc(z)).ae(cmath.tan(z))
391
+ assert sinh(mpc(z)).ae(cmath.sinh(z))
392
+ assert cosh(mpc(z)).ae(cmath.cosh(z))
393
+ assert tanh(mpc(z)).ae(cmath.tanh(z))
394
+
395
+ def test_complex_inverse_functions():
396
+ mp.dps = 15
397
+ iv.dps = 15
398
+ for (z1, z2) in random_complexes(30):
399
+ # apparently cmath uses a different branch, so we
400
+ # can't use it for comparison
401
+ assert sinh(asinh(z1)).ae(z1)
402
+ #
403
+ assert acosh(z1).ae(cmath.acosh(z1))
404
+ assert atanh(z1).ae(cmath.atanh(z1))
405
+ assert atan(z1).ae(cmath.atan(z1))
406
+ # the reason we set a big eps here is that the cmath
407
+ # functions are inaccurate
408
+ assert asin(z1).ae(cmath.asin(z1), rel_eps=1e-12)
409
+ assert acos(z1).ae(cmath.acos(z1), rel_eps=1e-12)
410
+ one = mpf(1)
411
+ for i in range(-9, 10, 3):
412
+ for k in range(-9, 10, 3):
413
+ a = 0.9*j*10**k + 0.8*one*10**i
414
+ b = cos(acos(a))
415
+ assert b.ae(a)
416
+ b = sin(asin(a))
417
+ assert b.ae(a)
418
+ one = mpf(1)
419
+ err = 2*10**-15
420
+ for i in range(-9, 9, 3):
421
+ for k in range(-9, 9, 3):
422
+ a = -0.9*10**k + j*0.8*one*10**i
423
+ b = cosh(acosh(a))
424
+ assert b.ae(a, err)
425
+ b = sinh(asinh(a))
426
+ assert b.ae(a, err)
427
+
428
+ def test_reciprocal_functions():
429
+ assert sec(3).ae(-1.01010866590799375)
430
+ assert csc(3).ae(7.08616739573718592)
431
+ assert cot(3).ae(-7.01525255143453347)
432
+ assert sech(3).ae(0.0993279274194332078)
433
+ assert csch(3).ae(0.0998215696688227329)
434
+ assert coth(3).ae(1.00496982331368917)
435
+ assert asec(3).ae(1.23095941734077468)
436
+ assert acsc(3).ae(0.339836909454121937)
437
+ assert acot(3).ae(0.321750554396642193)
438
+ assert asech(0.5).ae(1.31695789692481671)
439
+ assert acsch(3).ae(0.327450150237258443)
440
+ assert acoth(3).ae(0.346573590279972655)
441
+ assert acot(0).ae(1.5707963267948966192)
442
+ assert acoth(0).ae(1.5707963267948966192j)
443
+
444
+ def test_ldexp():
445
+ mp.dps = 15
446
+ assert ldexp(mpf(2.5), 0) == 2.5
447
+ assert ldexp(mpf(2.5), -1) == 1.25
448
+ assert ldexp(mpf(2.5), 2) == 10
449
+ assert ldexp(mpf('inf'), 3) == mpf('inf')
450
+
451
+ def test_frexp():
452
+ mp.dps = 15
453
+ assert frexp(0) == (0.0, 0)
454
+ assert frexp(9) == (0.5625, 4)
455
+ assert frexp(1) == (0.5, 1)
456
+ assert frexp(0.2) == (0.8, -2)
457
+ assert frexp(1000) == (0.9765625, 10)
458
+
459
+ def test_aliases():
460
+ assert ln(7) == log(7)
461
+ assert log10(3.75) == log(3.75,10)
462
+ assert degrees(5.6) == 5.6 / degree
463
+ assert radians(5.6) == 5.6 * degree
464
+ assert power(-1,0.5) == j
465
+ assert fmod(25,7) == 4.0 and isinstance(fmod(25,7), mpf)
466
+
467
+ def test_arg_sign():
468
+ assert arg(3) == 0
469
+ assert arg(-3).ae(pi)
470
+ assert arg(j).ae(pi/2)
471
+ assert arg(-j).ae(-pi/2)
472
+ assert arg(0) == 0
473
+ assert isnan(atan2(3,nan))
474
+ assert isnan(atan2(nan,3))
475
+ assert isnan(atan2(0,nan))
476
+ assert isnan(atan2(nan,0))
477
+ assert isnan(atan2(nan,nan))
478
+ assert arg(inf) == 0
479
+ assert arg(-inf).ae(pi)
480
+ assert isnan(arg(nan))
481
+ #assert arg(inf*j).ae(pi/2)
482
+ assert sign(0) == 0
483
+ assert sign(3) == 1
484
+ assert sign(-3) == -1
485
+ assert sign(inf) == 1
486
+ assert sign(-inf) == -1
487
+ assert isnan(sign(nan))
488
+ assert sign(j) == j
489
+ assert sign(-3*j) == -j
490
+ assert sign(1+j).ae((1+j)/sqrt(2))
491
+
492
+ def test_misc_bugs():
493
+ # test that this doesn't raise an exception
494
+ mp.dps = 1000
495
+ log(1302)
496
+ mp.dps = 15
497
+
498
+ def test_arange():
499
+ assert arange(10) == [mpf('0.0'), mpf('1.0'), mpf('2.0'), mpf('3.0'),
500
+ mpf('4.0'), mpf('5.0'), mpf('6.0'), mpf('7.0'),
501
+ mpf('8.0'), mpf('9.0')]
502
+ assert arange(-5, 5) == [mpf('-5.0'), mpf('-4.0'), mpf('-3.0'),
503
+ mpf('-2.0'), mpf('-1.0'), mpf('0.0'),
504
+ mpf('1.0'), mpf('2.0'), mpf('3.0'), mpf('4.0')]
505
+ assert arange(0, 1, 0.1) == [mpf('0.0'), mpf('0.10000000000000001'),
506
+ mpf('0.20000000000000001'),
507
+ mpf('0.30000000000000004'),
508
+ mpf('0.40000000000000002'),
509
+ mpf('0.5'), mpf('0.60000000000000009'),
510
+ mpf('0.70000000000000007'),
511
+ mpf('0.80000000000000004'),
512
+ mpf('0.90000000000000002')]
513
+ assert arange(17, -9, -3) == [mpf('17.0'), mpf('14.0'), mpf('11.0'),
514
+ mpf('8.0'), mpf('5.0'), mpf('2.0'),
515
+ mpf('-1.0'), mpf('-4.0'), mpf('-7.0')]
516
+ assert arange(0.2, 0.1, -0.1) == [mpf('0.20000000000000001')]
517
+ assert arange(0) == []
518
+ assert arange(1000, -1) == []
519
+ assert arange(-1.23, 3.21, -0.0000001) == []
520
+
521
+ def test_linspace():
522
+ assert linspace(2, 9, 7) == [mpf('2.0'), mpf('3.166666666666667'),
523
+ mpf('4.3333333333333339'), mpf('5.5'), mpf('6.666666666666667'),
524
+ mpf('7.8333333333333339'), mpf('9.0')]
525
+ assert linspace(2, 9, 7, endpoint=0) == [mpf('2.0'), mpf('3.0'), mpf('4.0'),
526
+ mpf('5.0'), mpf('6.0'), mpf('7.0'), mpf('8.0')]
527
+ assert linspace(2, 7, 1) == [mpf(2)]
528
+
529
+ def test_float_cbrt():
530
+ mp.dps = 30
531
+ for a in arange(0,10,0.1):
532
+ assert cbrt(a*a*a).ae(a, eps)
533
+ assert cbrt(-1).ae(0.5 + j*sqrt(3)/2)
534
+ one_third = mpf(1)/3
535
+ for a in arange(0,10,2.7) + [0.1 + 10**5]:
536
+ a = mpc(a + 1.1j)
537
+ r1 = cbrt(a)
538
+ mp.dps += 10
539
+ r2 = pow(a, one_third)
540
+ mp.dps -= 10
541
+ assert r1.ae(r2, eps)
542
+ mp.dps = 100
543
+ for n in range(100, 301, 100):
544
+ w = 10**n + j*10**-3
545
+ z = w*w*w
546
+ r = cbrt(z)
547
+ assert mpc_ae(r, w, eps)
548
+ mp.dps = 15
549
+
550
+ def test_root():
551
+ mp.dps = 30
552
+ random.seed(1)
553
+ a = random.randint(0, 10000)
554
+ p = a*a*a
555
+ r = nthroot(mpf(p), 3)
556
+ assert r == a
557
+ for n in range(4, 10):
558
+ p = p*a
559
+ assert nthroot(mpf(p), n) == a
560
+ mp.dps = 40
561
+ for n in range(10, 5000, 100):
562
+ for a in [random.random()*10000, random.random()*10**100]:
563
+ r = nthroot(a, n)
564
+ r1 = pow(a, mpf(1)/n)
565
+ assert r.ae(r1)
566
+ r = nthroot(a, -n)
567
+ r1 = pow(a, -mpf(1)/n)
568
+ assert r.ae(r1)
569
+ # XXX: this is broken right now
570
+ # tests for nthroot rounding
571
+ for rnd in ['nearest', 'up', 'down']:
572
+ mp.rounding = rnd
573
+ for n in [-5, -3, 3, 5]:
574
+ prec = 50
575
+ for i in range(10):
576
+ mp.prec = prec
577
+ a = rand()
578
+ mp.prec = 2*prec
579
+ b = a**n
580
+ mp.prec = prec
581
+ r = nthroot(b, n)
582
+ assert r == a
583
+ mp.dps = 30
584
+ for n in range(3, 21):
585
+ a = (random.random() + j*random.random())
586
+ assert nthroot(a, n).ae(pow(a, mpf(1)/n))
587
+ assert mpc_ae(nthroot(a, n), pow(a, mpf(1)/n))
588
+ a = (random.random()*10**100 + j*random.random())
589
+ r = nthroot(a, n)
590
+ mp.dps += 4
591
+ r1 = pow(a, mpf(1)/n)
592
+ mp.dps -= 4
593
+ assert r.ae(r1)
594
+ assert mpc_ae(r, r1, eps)
595
+ r = nthroot(a, -n)
596
+ mp.dps += 4
597
+ r1 = pow(a, -mpf(1)/n)
598
+ mp.dps -= 4
599
+ assert r.ae(r1)
600
+ assert mpc_ae(r, r1, eps)
601
+ mp.dps = 15
602
+ assert nthroot(4, 1) == 4
603
+ assert nthroot(4, 0) == 1
604
+ assert nthroot(4, -1) == 0.25
605
+ assert nthroot(inf, 1) == inf
606
+ assert nthroot(inf, 2) == inf
607
+ assert nthroot(inf, 3) == inf
608
+ assert nthroot(inf, -1) == 0
609
+ assert nthroot(inf, -2) == 0
610
+ assert nthroot(inf, -3) == 0
611
+ assert nthroot(j, 1) == j
612
+ assert nthroot(j, 0) == 1
613
+ assert nthroot(j, -1) == -j
614
+ assert isnan(nthroot(nan, 1))
615
+ assert isnan(nthroot(nan, 0))
616
+ assert isnan(nthroot(nan, -1))
617
+ assert isnan(nthroot(inf, 0))
618
+ assert root(2,3) == nthroot(2,3)
619
+ assert root(16,4,0) == 2
620
+ assert root(16,4,1) == 2j
621
+ assert root(16,4,2) == -2
622
+ assert root(16,4,3) == -2j
623
+ assert root(16,4,4) == 2
624
+ assert root(-125,3,1) == -5
625
+
626
+ def test_issue_136():
627
+ for dps in [20, 80]:
628
+ mp.dps = dps
629
+ r = nthroot(mpf('-1e-20'), 4)
630
+ assert r.ae(mpf(10)**(-5) * (1 + j) * mpf(2)**(-0.5))
631
+ mp.dps = 80
632
+ assert nthroot('-1e-3', 4).ae(mpf(10)**(-3./4) * (1 + j)/sqrt(2))
633
+ assert nthroot('-1e-6', 4).ae((1 + j)/(10 * sqrt(20)))
634
+ # Check that this doesn't take eternity to compute
635
+ mp.dps = 20
636
+ assert nthroot('-1e100000000', 4).ae((1+j)*mpf('1e25000000')/sqrt(2))
637
+ mp.dps = 15
638
+
639
+ def test_mpcfun_real_imag():
640
+ mp.dps = 15
641
+ x = mpf(0.3)
642
+ y = mpf(0.4)
643
+ assert exp(mpc(x,0)) == exp(x)
644
+ assert exp(mpc(0,y)) == mpc(cos(y),sin(y))
645
+ assert cos(mpc(x,0)) == cos(x)
646
+ assert sin(mpc(x,0)) == sin(x)
647
+ assert cos(mpc(0,y)) == cosh(y)
648
+ assert sin(mpc(0,y)) == mpc(0,sinh(y))
649
+ assert cospi(mpc(x,0)) == cospi(x)
650
+ assert sinpi(mpc(x,0)) == sinpi(x)
651
+ assert cospi(mpc(0,y)).ae(cosh(pi*y))
652
+ assert sinpi(mpc(0,y)).ae(mpc(0,sinh(pi*y)))
653
+ c, s = cospi_sinpi(mpc(x,0))
654
+ assert c == cospi(x)
655
+ assert s == sinpi(x)
656
+ c, s = cospi_sinpi(mpc(0,y))
657
+ assert c.ae(cosh(pi*y))
658
+ assert s.ae(mpc(0,sinh(pi*y)))
659
+ c, s = cos_sin(mpc(x,0))
660
+ assert c == cos(x)
661
+ assert s == sin(x)
662
+ c, s = cos_sin(mpc(0,y))
663
+ assert c == cosh(y)
664
+ assert s == mpc(0,sinh(y))
665
+
666
+ def test_perturbation_rounding():
667
+ mp.dps = 100
668
+ a = pi/10**50
669
+ b = -pi/10**50
670
+ c = 1 + a
671
+ d = 1 + b
672
+ mp.dps = 15
673
+ assert exp(a) == 1
674
+ assert exp(a, rounding='c') > 1
675
+ assert exp(b, rounding='c') == 1
676
+ assert exp(a, rounding='f') == 1
677
+ assert exp(b, rounding='f') < 1
678
+ assert cos(a) == 1
679
+ assert cos(a, rounding='c') == 1
680
+ assert cos(b, rounding='c') == 1
681
+ assert cos(a, rounding='f') < 1
682
+ assert cos(b, rounding='f') < 1
683
+ for f in [sin, atan, asinh, tanh]:
684
+ assert f(a) == +a
685
+ assert f(a, rounding='c') > a
686
+ assert f(a, rounding='f') < a
687
+ assert f(b) == +b
688
+ assert f(b, rounding='c') > b
689
+ assert f(b, rounding='f') < b
690
+ for f in [asin, tan, sinh, atanh]:
691
+ assert f(a) == +a
692
+ assert f(b) == +b
693
+ assert f(a, rounding='c') > a
694
+ assert f(b, rounding='c') > b
695
+ assert f(a, rounding='f') < a
696
+ assert f(b, rounding='f') < b
697
+ assert ln(c) == +a
698
+ assert ln(d) == +b
699
+ assert ln(c, rounding='c') > a
700
+ assert ln(c, rounding='f') < a
701
+ assert ln(d, rounding='c') > b
702
+ assert ln(d, rounding='f') < b
703
+ assert cosh(a) == 1
704
+ assert cosh(b) == 1
705
+ assert cosh(a, rounding='c') > 1
706
+ assert cosh(b, rounding='c') > 1
707
+ assert cosh(a, rounding='f') == 1
708
+ assert cosh(b, rounding='f') == 1
709
+
710
+ def test_integer_parts():
711
+ assert floor(3.2) == 3
712
+ assert ceil(3.2) == 4
713
+ assert floor(3.2+5j) == 3+5j
714
+ assert ceil(3.2+5j) == 4+5j
715
+
716
+ def test_complex_parts():
717
+ assert fabs('3') == 3
718
+ assert fabs(3+4j) == 5
719
+ assert re(3) == 3
720
+ assert re(1+4j) == 1
721
+ assert im(3) == 0
722
+ assert im(1+4j) == 4
723
+ assert conj(3) == 3
724
+ assert conj(3+4j) == 3-4j
725
+ assert mpf(3).conjugate() == 3
726
+
727
+ def test_cospi_sinpi():
728
+ assert sinpi(0) == 0
729
+ assert sinpi(0.5) == 1
730
+ assert sinpi(1) == 0
731
+ assert sinpi(1.5) == -1
732
+ assert sinpi(2) == 0
733
+ assert sinpi(2.5) == 1
734
+ assert sinpi(-0.5) == -1
735
+ assert cospi(0) == 1
736
+ assert cospi(0.5) == 0
737
+ assert cospi(1) == -1
738
+ assert cospi(1.5) == 0
739
+ assert cospi(2) == 1
740
+ assert cospi(2.5) == 0
741
+ assert cospi(-0.5) == 0
742
+ assert cospi(100000000000.25).ae(sqrt(2)/2)
743
+ a = cospi(2+3j)
744
+ assert a.real.ae(cos((2+3j)*pi).real)
745
+ assert a.imag == 0
746
+ b = sinpi(2+3j)
747
+ assert b.imag.ae(sin((2+3j)*pi).imag)
748
+ assert b.real == 0
749
+ mp.dps = 35
750
+ x1 = mpf(10000) - mpf('1e-15')
751
+ x2 = mpf(10000) + mpf('1e-15')
752
+ x3 = mpf(10000.5) - mpf('1e-15')
753
+ x4 = mpf(10000.5) + mpf('1e-15')
754
+ x5 = mpf(10001) - mpf('1e-15')
755
+ x6 = mpf(10001) + mpf('1e-15')
756
+ x7 = mpf(10001.5) - mpf('1e-15')
757
+ x8 = mpf(10001.5) + mpf('1e-15')
758
+ mp.dps = 15
759
+ M = 10**15
760
+ assert (sinpi(x1)*M).ae(-pi)
761
+ assert (sinpi(x2)*M).ae(pi)
762
+ assert (cospi(x3)*M).ae(pi)
763
+ assert (cospi(x4)*M).ae(-pi)
764
+ assert (sinpi(x5)*M).ae(pi)
765
+ assert (sinpi(x6)*M).ae(-pi)
766
+ assert (cospi(x7)*M).ae(-pi)
767
+ assert (cospi(x8)*M).ae(pi)
768
+ assert 0.999 < cospi(x1, rounding='d') < 1
769
+ assert 0.999 < cospi(x2, rounding='d') < 1
770
+ assert 0.999 < sinpi(x3, rounding='d') < 1
771
+ assert 0.999 < sinpi(x4, rounding='d') < 1
772
+ assert -1 < cospi(x5, rounding='d') < -0.999
773
+ assert -1 < cospi(x6, rounding='d') < -0.999
774
+ assert -1 < sinpi(x7, rounding='d') < -0.999
775
+ assert -1 < sinpi(x8, rounding='d') < -0.999
776
+ assert (sinpi(1e-15)*M).ae(pi)
777
+ assert (sinpi(-1e-15)*M).ae(-pi)
778
+ assert cospi(1e-15) == 1
779
+ assert cospi(1e-15, rounding='d') < 1
780
+
781
+ def test_expj():
782
+ assert expj(0) == 1
783
+ assert expj(1).ae(exp(j))
784
+ assert expj(j).ae(exp(-1))
785
+ assert expj(1+j).ae(exp(j*(1+j)))
786
+ assert expjpi(0) == 1
787
+ assert expjpi(1).ae(exp(j*pi))
788
+ assert expjpi(j).ae(exp(-pi))
789
+ assert expjpi(1+j).ae(exp(j*pi*(1+j)))
790
+ assert expjpi(-10**15 * j).ae('2.22579818340535731e+1364376353841841')
791
+
792
+ def test_sinc():
793
+ assert sinc(0) == sincpi(0) == 1
794
+ assert sinc(inf) == sincpi(inf) == 0
795
+ assert sinc(-inf) == sincpi(-inf) == 0
796
+ assert sinc(2).ae(0.45464871341284084770)
797
+ assert sinc(2+3j).ae(0.4463290318402435457-2.7539470277436474940j)
798
+ assert sincpi(2) == 0
799
+ assert sincpi(1.5).ae(-0.212206590789193781)
800
+
801
+ def test_fibonacci():
802
+ mp.dps = 15
803
+ assert [fibonacci(n) for n in range(-5, 10)] == \
804
+ [5, -3, 2, -1, 1, 0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
805
+ assert fib(2.5).ae(1.4893065462657091)
806
+ assert fib(3+4j).ae(-5248.51130728372 - 14195.962288353j)
807
+ assert fib(1000).ae(4.3466557686937455e+208)
808
+ assert str(fib(10**100)) == '6.24499112864607e+2089876402499787337692720892375554168224592399182109535392875613974104853496745963277658556235103534'
809
+ mp.dps = 2100
810
+ a = fib(10000)
811
+ assert a % 10**10 == 9947366875
812
+ mp.dps = 15
813
+ assert fibonacci(inf) == inf
814
+ assert fib(3+0j) == 2
815
+
816
+ def test_call_with_dps():
817
+ mp.dps = 15
818
+ assert abs(exp(1, dps=30)-e(dps=35)) < 1e-29
819
+
820
+ def test_tanh():
821
+ mp.dps = 15
822
+ assert tanh(0) == 0
823
+ assert tanh(inf) == 1
824
+ assert tanh(-inf) == -1
825
+ assert isnan(tanh(nan))
826
+ assert tanh(mpc('inf', '0')) == 1
827
+
828
+ def test_atanh():
829
+ mp.dps = 15
830
+ assert atanh(0) == 0
831
+ assert atanh(0.5).ae(0.54930614433405484570)
832
+ assert atanh(-0.5).ae(-0.54930614433405484570)
833
+ assert atanh(1) == inf
834
+ assert atanh(-1) == -inf
835
+ assert isnan(atanh(nan))
836
+ assert isinstance(atanh(1), mpf)
837
+ assert isinstance(atanh(-1), mpf)
838
+ # Limits at infinity
839
+ jpi2 = j*pi/2
840
+ assert atanh(inf).ae(-jpi2)
841
+ assert atanh(-inf).ae(jpi2)
842
+ assert atanh(mpc(inf,-1)).ae(-jpi2)
843
+ assert atanh(mpc(inf,0)).ae(-jpi2)
844
+ assert atanh(mpc(inf,1)).ae(jpi2)
845
+ assert atanh(mpc(1,inf)).ae(jpi2)
846
+ assert atanh(mpc(0,inf)).ae(jpi2)
847
+ assert atanh(mpc(-1,inf)).ae(jpi2)
848
+ assert atanh(mpc(-inf,1)).ae(jpi2)
849
+ assert atanh(mpc(-inf,0)).ae(jpi2)
850
+ assert atanh(mpc(-inf,-1)).ae(-jpi2)
851
+ assert atanh(mpc(-1,-inf)).ae(-jpi2)
852
+ assert atanh(mpc(0,-inf)).ae(-jpi2)
853
+ assert atanh(mpc(1,-inf)).ae(-jpi2)
854
+
855
+ def test_expm1():
856
+ mp.dps = 15
857
+ assert expm1(0) == 0
858
+ assert expm1(3).ae(exp(3)-1)
859
+ assert expm1(inf) == inf
860
+ assert expm1(1e-50).ae(1e-50)
861
+ assert (expm1(1e-10)*1e10).ae(1.00000000005)
862
+
863
+ def test_log1p():
864
+ mp.dps = 15
865
+ assert log1p(0) == 0
866
+ assert log1p(3).ae(log(1+3))
867
+ assert log1p(inf) == inf
868
+ assert log1p(1e-50).ae(1e-50)
869
+ assert (log1p(1e-10)*1e10).ae(0.99999999995)
870
+
871
+ def test_powm1():
872
+ mp.dps = 15
873
+ assert powm1(2,3) == 7
874
+ assert powm1(-1,2) == 0
875
+ assert powm1(-1,0) == 0
876
+ assert powm1(-2,0) == 0
877
+ assert powm1(3+4j,0) == 0
878
+ assert powm1(0,1) == -1
879
+ assert powm1(0,0) == 0
880
+ assert powm1(1,0) == 0
881
+ assert powm1(1,2) == 0
882
+ assert powm1(1,3+4j) == 0
883
+ assert powm1(1,5) == 0
884
+ assert powm1(j,4) == 0
885
+ assert powm1(-j,4) == 0
886
+ assert (powm1(2,1e-100)*1e100).ae(ln2)
887
+ assert powm1(2,'1e-100000000000') != 0
888
+ assert (powm1(fadd(1,1e-100,exact=True), 5)*1e100).ae(5)
889
+
890
+ def test_unitroots():
891
+ assert unitroots(1) == [1]
892
+ assert unitroots(2) == [1, -1]
893
+ a, b, c = unitroots(3)
894
+ assert a == 1
895
+ assert b.ae(-0.5 + 0.86602540378443864676j)
896
+ assert c.ae(-0.5 - 0.86602540378443864676j)
897
+ assert unitroots(1, primitive=True) == [1]
898
+ assert unitroots(2, primitive=True) == [-1]
899
+ assert unitroots(3, primitive=True) == unitroots(3)[1:]
900
+ assert unitroots(4, primitive=True) == [j, -j]
901
+ assert len(unitroots(17, primitive=True)) == 16
902
+ assert len(unitroots(16, primitive=True)) == 8
903
+
904
+ def test_cyclotomic():
905
+ mp.dps = 15
906
+ assert [cyclotomic(n,1) for n in range(31)] == [1,0,2,3,2,5,1,7,2,3,1,11,1,13,1,1,2,17,1,19,1,1,1,23,1,5,1,3,1,29,1]
907
+ assert [cyclotomic(n,-1) for n in range(31)] == [1,-2,0,1,2,1,3,1,2,1,5,1,1,1,7,1,2,1,3,1,1,1,11,1,1,1,13,1,1,1,1]
908
+ assert [cyclotomic(n,j) for n in range(21)] == [1,-1+j,1+j,j,0,1,-j,j,2,-j,1,j,3,1,-j,1,2,1,j,j,5]
909
+ assert [cyclotomic(n,-j) for n in range(21)] == [1,-1-j,1-j,-j,0,1,j,-j,2,j,1,-j,3,1,j,1,2,1,-j,-j,5]
910
+ assert cyclotomic(1624,j) == 1
911
+ assert cyclotomic(33600,j) == 1
912
+ u = sqrt(j, prec=500)
913
+ assert cyclotomic(8, u).ae(0)
914
+ assert cyclotomic(30, u).ae(5.8284271247461900976)
915
+ assert cyclotomic(2040, u).ae(1)
916
+ assert cyclotomic(0,2.5) == 1
917
+ assert cyclotomic(1,2.5) == 2.5-1
918
+ assert cyclotomic(2,2.5) == 2.5+1
919
+ assert cyclotomic(3,2.5) == 2.5**2 + 2.5 + 1
920
+ assert cyclotomic(7,2.5) == 406.234375
llmeval-env/lib/python3.10/site-packages/mpmath/tests/test_functions2.py ADDED
@@ -0,0 +1,2384 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import math
2
+ import pytest
3
+ from mpmath import *
4
+
5
+ def test_bessel():
6
+ mp.dps = 15
7
+ assert j0(1).ae(0.765197686557966551)
8
+ assert j0(pi).ae(-0.304242177644093864)
9
+ assert j0(1000).ae(0.0247866861524201746)
10
+ assert j0(-25).ae(0.0962667832759581162)
11
+ assert j1(1).ae(0.440050585744933516)
12
+ assert j1(pi).ae(0.284615343179752757)
13
+ assert j1(1000).ae(0.00472831190708952392)
14
+ assert j1(-25).ae(0.125350249580289905)
15
+ assert besselj(5,1).ae(0.000249757730211234431)
16
+ assert besselj(5+0j,1).ae(0.000249757730211234431)
17
+ assert besselj(5,pi).ae(0.0521411843671184747)
18
+ assert besselj(5,1000).ae(0.00502540694523318607)
19
+ assert besselj(5,-25).ae(0.0660079953984229934)
20
+ assert besselj(-3,2).ae(-0.128943249474402051)
21
+ assert besselj(-4,2).ae(0.0339957198075684341)
22
+ assert besselj(3,3+2j).ae(0.424718794929639595942 + 0.625665327745785804812j)
23
+ assert besselj(0.25,4).ae(-0.374760630804249715)
24
+ assert besselj(1+2j,3+4j).ae(0.319247428741872131 - 0.669557748880365678j)
25
+ assert (besselj(3, 10**10) * 10**5).ae(0.76765081748139204023)
26
+ assert bessely(-0.5, 0) == 0
27
+ assert bessely(0.5, 0) == -inf
28
+ assert bessely(1.5, 0) == -inf
29
+ assert bessely(0,0) == -inf
30
+ assert bessely(-0.4, 0) == -inf
31
+ assert bessely(-0.6, 0) == inf
32
+ assert bessely(-1, 0) == inf
33
+ assert bessely(-1.4, 0) == inf
34
+ assert bessely(-1.6, 0) == -inf
35
+ assert bessely(-1, 0) == inf
36
+ assert bessely(-2, 0) == -inf
37
+ assert bessely(-3, 0) == inf
38
+ assert bessely(0.5, 0) == -inf
39
+ assert bessely(1, 0) == -inf
40
+ assert bessely(1.5, 0) == -inf
41
+ assert bessely(2, 0) == -inf
42
+ assert bessely(2.5, 0) == -inf
43
+ assert bessely(3, 0) == -inf
44
+ assert bessely(0,0.5).ae(-0.44451873350670655715)
45
+ assert bessely(1,0.5).ae(-1.4714723926702430692)
46
+ assert bessely(-1,0.5).ae(1.4714723926702430692)
47
+ assert bessely(3.5,0.5).ae(-138.86400867242488443)
48
+ assert bessely(0,3+4j).ae(4.6047596915010138655-8.8110771408232264208j)
49
+ assert bessely(0,j).ae(-0.26803248203398854876+1.26606587775200833560j)
50
+ assert (bessely(3, 10**10) * 10**5).ae(0.21755917537013204058)
51
+ assert besseli(0,0) == 1
52
+ assert besseli(1,0) == 0
53
+ assert besseli(2,0) == 0
54
+ assert besseli(-1,0) == 0
55
+ assert besseli(-2,0) == 0
56
+ assert besseli(0,0.5).ae(1.0634833707413235193)
57
+ assert besseli(1,0.5).ae(0.25789430539089631636)
58
+ assert besseli(-1,0.5).ae(0.25789430539089631636)
59
+ assert besseli(3.5,0.5).ae(0.00068103597085793815863)
60
+ assert besseli(0,3+4j).ae(-3.3924877882755196097-1.3239458916287264815j)
61
+ assert besseli(0,j).ae(besselj(0,1))
62
+ assert (besseli(3, 10**10) * mpf(10)**(-4342944813)).ae(4.2996028505491271875)
63
+ assert besselk(0,0) == inf
64
+ assert besselk(1,0) == inf
65
+ assert besselk(2,0) == inf
66
+ assert besselk(-1,0) == inf
67
+ assert besselk(-2,0) == inf
68
+ assert besselk(0,0.5).ae(0.92441907122766586178)
69
+ assert besselk(1,0.5).ae(1.6564411200033008937)
70
+ assert besselk(-1,0.5).ae(1.6564411200033008937)
71
+ assert besselk(3.5,0.5).ae(207.48418747548460607)
72
+ assert besselk(0,3+4j).ae(-0.007239051213570155013+0.026510418350267677215j)
73
+ assert besselk(0,j).ae(-0.13863371520405399968-1.20196971531720649914j)
74
+ assert (besselk(3, 10**10) * mpf(10)**4342944824).ae(1.1628981033356187851)
75
+ # test for issue 331, bug reported by Michael Hartmann
76
+ for n in range(10,100,10):
77
+ mp.dps = n
78
+ assert besseli(91.5,24.7708).ae("4.00830632138673963619656140653537080438462342928377020695738635559218797348548092636896796324190271316137982810144874264e-41")
79
+
80
+ def test_bessel_zeros():
81
+ mp.dps = 15
82
+ assert besseljzero(0,1).ae(2.40482555769577276869)
83
+ assert besseljzero(2,1).ae(5.1356223018406825563)
84
+ assert besseljzero(1,50).ae(157.86265540193029781)
85
+ assert besseljzero(10,1).ae(14.475500686554541220)
86
+ assert besseljzero(0.5,3).ae(9.4247779607693797153)
87
+ assert besseljzero(2,1,1).ae(3.0542369282271403228)
88
+ assert besselyzero(0,1).ae(0.89357696627916752158)
89
+ assert besselyzero(2,1).ae(3.3842417671495934727)
90
+ assert besselyzero(1,50).ae(156.29183520147840108)
91
+ assert besselyzero(10,1).ae(12.128927704415439387)
92
+ assert besselyzero(0.5,3).ae(7.8539816339744830962)
93
+ assert besselyzero(2,1,1).ae(5.0025829314460639452)
94
+
95
+ def test_hankel():
96
+ mp.dps = 15
97
+ assert hankel1(0,0.5).ae(0.93846980724081290423-0.44451873350670655715j)
98
+ assert hankel1(1,0.5).ae(0.2422684576748738864-1.4714723926702430692j)
99
+ assert hankel1(-1,0.5).ae(-0.2422684576748738864+1.4714723926702430692j)
100
+ assert hankel1(1.5,0.5).ae(0.0917016996256513026-2.5214655504213378514j)
101
+ assert hankel1(1.5,3+4j).ae(0.0066806866476728165382-0.0036684231610839127106j)
102
+ assert hankel2(0,0.5).ae(0.93846980724081290423+0.44451873350670655715j)
103
+ assert hankel2(1,0.5).ae(0.2422684576748738864+1.4714723926702430692j)
104
+ assert hankel2(-1,0.5).ae(-0.2422684576748738864-1.4714723926702430692j)
105
+ assert hankel2(1.5,0.5).ae(0.0917016996256513026+2.5214655504213378514j)
106
+ assert hankel2(1.5,3+4j).ae(14.783528526098567526-7.397390270853446512j)
107
+
108
+ def test_struve():
109
+ mp.dps = 15
110
+ assert struveh(2,3).ae(0.74238666967748318564)
111
+ assert struveh(-2.5,3).ae(0.41271003220971599344)
112
+ assert struvel(2,3).ae(1.7476573277362782744)
113
+ assert struvel(-2.5,3).ae(1.5153394466819651377)
114
+
115
+ def test_whittaker():
116
+ mp.dps = 15
117
+ assert whitm(2,3,4).ae(49.753745589025246591)
118
+ assert whitw(2,3,4).ae(14.111656223052932215)
119
+
120
+ def test_kelvin():
121
+ mp.dps = 15
122
+ assert ber(2,3).ae(0.80836846563726819091)
123
+ assert ber(3,4).ae(-0.28262680167242600233)
124
+ assert ber(-3,2).ae(-0.085611448496796363669)
125
+ assert bei(2,3).ae(-0.89102236377977331571)
126
+ assert bei(-3,2).ae(-0.14420994155731828415)
127
+ assert ker(2,3).ae(0.12839126695733458928)
128
+ assert ker(-3,2).ae(-0.29802153400559142783)
129
+ assert ker(0.5,3).ae(-0.085662378535217097524)
130
+ assert kei(2,3).ae(0.036804426134164634000)
131
+ assert kei(-3,2).ae(0.88682069845786731114)
132
+ assert kei(0.5,3).ae(0.013633041571314302948)
133
+
134
+ def test_hyper_misc():
135
+ mp.dps = 15
136
+ assert hyp0f1(1,0) == 1
137
+ assert hyp1f1(1,2,0) == 1
138
+ assert hyp1f2(1,2,3,0) == 1
139
+ assert hyp2f1(1,2,3,0) == 1
140
+ assert hyp2f2(1,2,3,4,0) == 1
141
+ assert hyp2f3(1,2,3,4,5,0) == 1
142
+ # Degenerate case: 0F0
143
+ assert hyper([],[],0) == 1
144
+ assert hyper([],[],-2).ae(exp(-2))
145
+ # Degenerate case: 1F0
146
+ assert hyper([2],[],1.5) == 4
147
+ #
148
+ assert hyp2f1((1,3),(2,3),(5,6),mpf(27)/32).ae(1.6)
149
+ assert hyp2f1((1,4),(1,2),(3,4),mpf(80)/81).ae(1.8)
150
+ assert hyp2f1((2,3),(1,1),(3,2),(2+j)/3).ae(1.327531603558679093+0.439585080092769253j)
151
+ mp.dps = 25
152
+ v = mpc('1.2282306665029814734863026', '-0.1225033830118305184672133')
153
+ assert hyper([(3,4),2+j,1],[1,5,j/3],mpf(1)/5+j/8).ae(v)
154
+ mp.dps = 15
155
+
156
+ def test_elliptic_integrals():
157
+ mp.dps = 15
158
+ assert ellipk(0).ae(pi/2)
159
+ assert ellipk(0.5).ae(gamma(0.25)**2/(4*sqrt(pi)))
160
+ assert ellipk(1) == inf
161
+ assert ellipk(1+0j) == inf
162
+ assert ellipk(-1).ae('1.3110287771460599052')
163
+ assert ellipk(-2).ae('1.1714200841467698589')
164
+ assert isinstance(ellipk(-2), mpf)
165
+ assert isinstance(ellipe(-2), mpf)
166
+ assert ellipk(-50).ae('0.47103424540873331679')
167
+ mp.dps = 30
168
+ n1 = +fraction(99999,100000)
169
+ n2 = +fraction(100001,100000)
170
+ mp.dps = 15
171
+ assert ellipk(n1).ae('7.1427724505817781901')
172
+ assert ellipk(n2).ae(mpc('7.1427417367963090109', '-1.5707923998261688019'))
173
+ assert ellipe(n1).ae('1.0000332138990829170')
174
+ v = ellipe(n2)
175
+ assert v.real.ae('0.999966786328145474069137')
176
+ assert (v.imag*10**6).ae('7.853952181727432')
177
+ assert ellipk(2).ae(mpc('1.3110287771460599052', '-1.3110287771460599052'))
178
+ assert ellipk(50).ae(mpc('0.22326753950210985451', '-0.47434723226254522087'))
179
+ assert ellipk(3+4j).ae(mpc('0.91119556380496500866', '0.63133428324134524388'))
180
+ assert ellipk(3-4j).ae(mpc('0.91119556380496500866', '-0.63133428324134524388'))
181
+ assert ellipk(-3+4j).ae(mpc('0.95357894880405122483', '0.23093044503746114444'))
182
+ assert ellipk(-3-4j).ae(mpc('0.95357894880405122483', '-0.23093044503746114444'))
183
+ assert isnan(ellipk(nan))
184
+ assert isnan(ellipe(nan))
185
+ assert ellipk(inf) == 0
186
+ assert isinstance(ellipk(inf), mpc)
187
+ assert ellipk(-inf) == 0
188
+ assert ellipk(1+0j) == inf
189
+ assert ellipe(0).ae(pi/2)
190
+ assert ellipe(0.5).ae(pi**(mpf(3)/2)/gamma(0.25)**2 +gamma(0.25)**2/(8*sqrt(pi)))
191
+ assert ellipe(1) == 1
192
+ assert ellipe(1+0j) == 1
193
+ assert ellipe(inf) == mpc(0,inf)
194
+ assert ellipe(-inf) == inf
195
+ assert ellipe(3+4j).ae(1.4995535209333469543-1.5778790079127582745j)
196
+ assert ellipe(3-4j).ae(1.4995535209333469543+1.5778790079127582745j)
197
+ assert ellipe(-3+4j).ae(2.5804237855343377803-0.8306096791000413778j)
198
+ assert ellipe(-3-4j).ae(2.5804237855343377803+0.8306096791000413778j)
199
+ assert ellipe(2).ae(0.59907011736779610372+0.59907011736779610372j)
200
+ assert ellipe('1e-1000000000').ae(pi/2)
201
+ assert ellipk('1e-1000000000').ae(pi/2)
202
+ assert ellipe(-pi).ae(2.4535865983838923)
203
+ mp.dps = 50
204
+ assert ellipk(1/pi).ae('1.724756270009501831744438120951614673874904182624739673')
205
+ assert ellipe(1/pi).ae('1.437129808135123030101542922290970050337425479058225712')
206
+ assert ellipk(-10*pi).ae('0.5519067523886233967683646782286965823151896970015484512')
207
+ assert ellipe(-10*pi).ae('5.926192483740483797854383268707108012328213431657645509')
208
+ v = ellipk(pi)
209
+ assert v.real.ae('0.973089521698042334840454592642137667227167622330325225')
210
+ assert v.imag.ae('-1.156151296372835303836814390793087600271609993858798016')
211
+ v = ellipe(pi)
212
+ assert v.real.ae('0.4632848917264710404078033487934663562998345622611263332')
213
+ assert v.imag.ae('1.0637961621753130852473300451583414489944099504180510966')
214
+ mp.dps = 15
215
+
216
+ def test_exp_integrals():
217
+ mp.dps = 15
218
+ x = +e
219
+ z = e + sqrt(3)*j
220
+ assert ei(x).ae(8.21168165538361560)
221
+ assert li(x).ae(1.89511781635593676)
222
+ assert si(x).ae(1.82104026914756705)
223
+ assert ci(x).ae(0.213958001340379779)
224
+ assert shi(x).ae(4.11520706247846193)
225
+ assert chi(x).ae(4.09647459290515367)
226
+ assert fresnels(x).ae(0.437189718149787643)
227
+ assert fresnelc(x).ae(0.401777759590243012)
228
+ assert airyai(x).ae(0.0108502401568586681)
229
+ assert airybi(x).ae(8.98245748585468627)
230
+ assert ei(z).ae(3.72597969491314951 + 7.34213212314224421j)
231
+ assert li(z).ae(2.28662658112562502 + 1.50427225297269364j)
232
+ assert si(z).ae(2.48122029237669054 + 0.12684703275254834j)
233
+ assert ci(z).ae(0.169255590269456633 - 0.892020751420780353j)
234
+ assert shi(z).ae(1.85810366559344468 + 3.66435842914920263j)
235
+ assert chi(z).ae(1.86787602931970484 + 3.67777369399304159j)
236
+ assert fresnels(z/3).ae(0.034534397197008182 + 0.754859844188218737j)
237
+ assert fresnelc(z/3).ae(1.261581645990027372 + 0.417949198775061893j)
238
+ assert airyai(z).ae(-0.0162552579839056062 - 0.0018045715700210556j)
239
+ assert airybi(z).ae(-4.98856113282883371 + 2.08558537872180623j)
240
+ assert li(0) == 0.0
241
+ assert li(1) == -inf
242
+ assert li(inf) == inf
243
+ assert isinstance(li(0.7), mpf)
244
+ assert si(inf).ae(pi/2)
245
+ assert si(-inf).ae(-pi/2)
246
+ assert ci(inf) == 0
247
+ assert ci(0) == -inf
248
+ assert isinstance(ei(-0.7), mpf)
249
+ assert airyai(inf) == 0
250
+ assert airybi(inf) == inf
251
+ assert airyai(-inf) == 0
252
+ assert airybi(-inf) == 0
253
+ assert fresnels(inf) == 0.5
254
+ assert fresnelc(inf) == 0.5
255
+ assert fresnels(-inf) == -0.5
256
+ assert fresnelc(-inf) == -0.5
257
+ assert shi(0) == 0
258
+ assert shi(inf) == inf
259
+ assert shi(-inf) == -inf
260
+ assert chi(0) == -inf
261
+ assert chi(inf) == inf
262
+
263
+ def test_ei():
264
+ mp.dps = 15
265
+ assert ei(0) == -inf
266
+ assert ei(inf) == inf
267
+ assert ei(-inf) == -0.0
268
+ assert ei(20+70j).ae(6.1041351911152984397e6 - 2.7324109310519928872e6j)
269
+ # tests for the asymptotic expansion
270
+ # values checked with Mathematica ExpIntegralEi
271
+ mp.dps = 50
272
+ r = ei(20000)
273
+ s = '3.8781962825045010930273870085501819470698476975019e+8681'
274
+ assert str(r) == s
275
+ r = ei(-200)
276
+ s = '-6.8852261063076355977108174824557929738368086933303e-90'
277
+ assert str(r) == s
278
+ r =ei(20000 + 10*j)
279
+ sre = '-3.255138234032069402493850638874410725961401274106e+8681'
280
+ sim = '-2.1081929993474403520785942429469187647767369645423e+8681'
281
+ assert str(r.real) == sre and str(r.imag) == sim
282
+ mp.dps = 15
283
+ # More asymptotic expansions
284
+ assert chi(-10**6+100j).ae('1.3077239389562548386e+434288 + 7.6808956999707408158e+434287j')
285
+ assert shi(-10**6+100j).ae('-1.3077239389562548386e+434288 - 7.6808956999707408158e+434287j')
286
+ mp.dps = 15
287
+ assert ei(10j).ae(-0.0454564330044553726+3.2291439210137706686j)
288
+ assert ei(100j).ae(-0.0051488251426104921+3.1330217936839529126j)
289
+ u = ei(fmul(10**20, j, exact=True))
290
+ assert u.real.ae(-6.4525128526578084421345e-21, abs_eps=0, rel_eps=8*eps)
291
+ assert u.imag.ae(pi)
292
+ assert ei(-10j).ae(-0.0454564330044553726-3.2291439210137706686j)
293
+ assert ei(-100j).ae(-0.0051488251426104921-3.1330217936839529126j)
294
+ u = ei(fmul(-10**20, j, exact=True))
295
+ assert u.real.ae(-6.4525128526578084421345e-21, abs_eps=0, rel_eps=8*eps)
296
+ assert u.imag.ae(-pi)
297
+ assert ei(10+10j).ae(-1576.1504265768517448+436.9192317011328140j)
298
+ u = ei(-10+10j)
299
+ assert u.real.ae(7.6698978415553488362543e-7, abs_eps=0, rel_eps=8*eps)
300
+ assert u.imag.ae(3.141595611735621062025)
301
+
302
+ def test_e1():
303
+ mp.dps = 15
304
+ assert e1(0) == inf
305
+ assert e1(inf) == 0
306
+ assert e1(-inf) == mpc(-inf, -pi)
307
+ assert e1(10j).ae(0.045456433004455372635 + 0.087551267423977430100j)
308
+ assert e1(100j).ae(0.0051488251426104921444 - 0.0085708599058403258790j)
309
+ assert e1(fmul(10**20, j, exact=True)).ae(6.4525128526578084421e-21 - 7.6397040444172830039e-21j, abs_eps=0, rel_eps=8*eps)
310
+ assert e1(-10j).ae(0.045456433004455372635 - 0.087551267423977430100j)
311
+ assert e1(-100j).ae(0.0051488251426104921444 + 0.0085708599058403258790j)
312
+ assert e1(fmul(-10**20, j, exact=True)).ae(6.4525128526578084421e-21 + 7.6397040444172830039e-21j, abs_eps=0, rel_eps=8*eps)
313
+
314
+ def test_expint():
315
+ mp.dps = 15
316
+ assert expint(0,0) == inf
317
+ assert expint(0,1).ae(1/e)
318
+ assert expint(0,1.5).ae(2/exp(1.5)/3)
319
+ assert expint(1,1).ae(-ei(-1))
320
+ assert expint(2,0).ae(1)
321
+ assert expint(3,0).ae(1/2.)
322
+ assert expint(4,0).ae(1/3.)
323
+ assert expint(-2, 0.5).ae(26/sqrt(e))
324
+ assert expint(-1,-1) == 0
325
+ assert expint(-2,-1).ae(-e)
326
+ assert expint(5.5, 0).ae(2/9.)
327
+ assert expint(2.00000001,0).ae(100000000./100000001)
328
+ assert expint(2+3j,4-j).ae(0.0023461179581675065414+0.0020395540604713669262j)
329
+ assert expint('1.01', '1e-1000').ae(99.9999999899412802)
330
+ assert expint('1.000000000001', 3.5).ae(0.00697013985754701819446)
331
+ assert expint(2,3).ae(3*ei(-3)+exp(-3))
332
+ assert (expint(10,20)*10**10).ae(0.694439055541231353)
333
+ assert expint(3,inf) == 0
334
+ assert expint(3.2,inf) == 0
335
+ assert expint(3.2+2j,inf) == 0
336
+ assert expint(1,3j).ae(-0.11962978600800032763 + 0.27785620120457163717j)
337
+ assert expint(1,3).ae(0.013048381094197037413)
338
+ assert expint(1,-3).ae(-ei(3)-pi*j)
339
+ #assert expint(3) == expint(1,3)
340
+ assert expint(1,-20).ae(-25615652.66405658882 - 3.1415926535897932385j)
341
+ assert expint(1000000,0).ae(1./999999)
342
+ assert expint(0,2+3j).ae(-0.025019798357114678171 + 0.027980439405104419040j)
343
+ assert expint(-1,2+3j).ae(-0.022411973626262070419 + 0.038058922011377716932j)
344
+ assert expint(-1.5,0) == inf
345
+
346
+ def test_trig_integrals():
347
+ mp.dps = 30
348
+ assert si(mpf(1)/1000000).ae('0.000000999999999999944444444444446111')
349
+ assert ci(mpf(1)/1000000).ae('-13.2382948930629912435014366276')
350
+ assert si(10**10).ae('1.5707963267075846569685111517747537')
351
+ assert ci(10**10).ae('-4.87506025174822653785729773959e-11')
352
+ assert si(10**100).ae(pi/2)
353
+ assert (ci(10**100)*10**100).ae('-0.372376123661276688262086695553')
354
+ assert si(-3) == -si(3)
355
+ assert ci(-3).ae(ci(3) + pi*j)
356
+ # Test complex structure
357
+ mp.dps = 15
358
+ assert mp.ci(50).ae(-0.0056283863241163054402)
359
+ assert mp.ci(50+2j).ae(-0.018378282946133067149+0.070352808023688336193j)
360
+ assert mp.ci(20j).ae(1.28078263320282943611e7+1.5707963267949j)
361
+ assert mp.ci(-2+20j).ae(-4.050116856873293505e6+1.207476188206989909e7j)
362
+ assert mp.ci(-50+2j).ae(-0.0183782829461330671+3.0712398455661049023j)
363
+ assert mp.ci(-50).ae(-0.0056283863241163054+3.1415926535897932385j)
364
+ assert mp.ci(-50-2j).ae(-0.0183782829461330671-3.0712398455661049023j)
365
+ assert mp.ci(-2-20j).ae(-4.050116856873293505e6-1.207476188206989909e7j)
366
+ assert mp.ci(-20j).ae(1.28078263320282943611e7-1.5707963267949j)
367
+ assert mp.ci(50-2j).ae(-0.018378282946133067149-0.070352808023688336193j)
368
+ assert mp.si(50).ae(1.5516170724859358947)
369
+ assert mp.si(50+2j).ae(1.497884414277228461-0.017515007378437448j)
370
+ assert mp.si(20j).ae(1.2807826332028294459e7j)
371
+ assert mp.si(-2+20j).ae(-1.20747603112735722103e7-4.050116856873293554e6j)
372
+ assert mp.si(-50+2j).ae(-1.497884414277228461-0.017515007378437448j)
373
+ assert mp.si(-50).ae(-1.5516170724859358947)
374
+ assert mp.si(-50-2j).ae(-1.497884414277228461+0.017515007378437448j)
375
+ assert mp.si(-2-20j).ae(-1.20747603112735722103e7+4.050116856873293554e6j)
376
+ assert mp.si(-20j).ae(-1.2807826332028294459e7j)
377
+ assert mp.si(50-2j).ae(1.497884414277228461+0.017515007378437448j)
378
+ assert mp.chi(50j).ae(-0.0056283863241163054+1.5707963267948966192j)
379
+ assert mp.chi(-2+50j).ae(-0.0183782829461330671+1.6411491348185849554j)
380
+ assert mp.chi(-20).ae(1.28078263320282943611e7+3.1415926535898j)
381
+ assert mp.chi(-20-2j).ae(-4.050116856873293505e6+1.20747571696809187053e7j)
382
+ assert mp.chi(-2-50j).ae(-0.0183782829461330671-1.6411491348185849554j)
383
+ assert mp.chi(-50j).ae(-0.0056283863241163054-1.5707963267948966192j)
384
+ assert mp.chi(2-50j).ae(-0.0183782829461330671-1.500443518771208283j)
385
+ assert mp.chi(20-2j).ae(-4.050116856873293505e6-1.20747603112735722951e7j)
386
+ assert mp.chi(20).ae(1.2807826332028294361e7)
387
+ assert mp.chi(2+50j).ae(-0.0183782829461330671+1.500443518771208283j)
388
+ assert mp.shi(50j).ae(1.5516170724859358947j)
389
+ assert mp.shi(-2+50j).ae(0.017515007378437448+1.497884414277228461j)
390
+ assert mp.shi(-20).ae(-1.2807826332028294459e7)
391
+ assert mp.shi(-20-2j).ae(4.050116856873293554e6-1.20747603112735722103e7j)
392
+ assert mp.shi(-2-50j).ae(0.017515007378437448-1.497884414277228461j)
393
+ assert mp.shi(-50j).ae(-1.5516170724859358947j)
394
+ assert mp.shi(2-50j).ae(-0.017515007378437448-1.497884414277228461j)
395
+ assert mp.shi(20-2j).ae(-4.050116856873293554e6-1.20747603112735722103e7j)
396
+ assert mp.shi(20).ae(1.2807826332028294459e7)
397
+ assert mp.shi(2+50j).ae(-0.017515007378437448+1.497884414277228461j)
398
+ def ae(x,y,tol=1e-12):
399
+ return abs(x-y) <= abs(y)*tol
400
+ assert fp.ci(fp.inf) == 0
401
+ assert ae(fp.ci(fp.ninf), fp.pi*1j)
402
+ assert ae(fp.si(fp.inf), fp.pi/2)
403
+ assert ae(fp.si(fp.ninf), -fp.pi/2)
404
+ assert fp.si(0) == 0
405
+ assert ae(fp.ci(50), -0.0056283863241163054402)
406
+ assert ae(fp.ci(50+2j), -0.018378282946133067149+0.070352808023688336193j)
407
+ assert ae(fp.ci(20j), 1.28078263320282943611e7+1.5707963267949j)
408
+ assert ae(fp.ci(-2+20j), -4.050116856873293505e6+1.207476188206989909e7j)
409
+ assert ae(fp.ci(-50+2j), -0.0183782829461330671+3.0712398455661049023j)
410
+ assert ae(fp.ci(-50), -0.0056283863241163054+3.1415926535897932385j)
411
+ assert ae(fp.ci(-50-2j), -0.0183782829461330671-3.0712398455661049023j)
412
+ assert ae(fp.ci(-2-20j), -4.050116856873293505e6-1.207476188206989909e7j)
413
+ assert ae(fp.ci(-20j), 1.28078263320282943611e7-1.5707963267949j)
414
+ assert ae(fp.ci(50-2j), -0.018378282946133067149-0.070352808023688336193j)
415
+ assert ae(fp.si(50), 1.5516170724859358947)
416
+ assert ae(fp.si(50+2j), 1.497884414277228461-0.017515007378437448j)
417
+ assert ae(fp.si(20j), 1.2807826332028294459e7j)
418
+ assert ae(fp.si(-2+20j), -1.20747603112735722103e7-4.050116856873293554e6j)
419
+ assert ae(fp.si(-50+2j), -1.497884414277228461-0.017515007378437448j)
420
+ assert ae(fp.si(-50), -1.5516170724859358947)
421
+ assert ae(fp.si(-50-2j), -1.497884414277228461+0.017515007378437448j)
422
+ assert ae(fp.si(-2-20j), -1.20747603112735722103e7+4.050116856873293554e6j)
423
+ assert ae(fp.si(-20j), -1.2807826332028294459e7j)
424
+ assert ae(fp.si(50-2j), 1.497884414277228461+0.017515007378437448j)
425
+ assert ae(fp.chi(50j), -0.0056283863241163054+1.5707963267948966192j)
426
+ assert ae(fp.chi(-2+50j), -0.0183782829461330671+1.6411491348185849554j)
427
+ assert ae(fp.chi(-20), 1.28078263320282943611e7+3.1415926535898j)
428
+ assert ae(fp.chi(-20-2j), -4.050116856873293505e6+1.20747571696809187053e7j)
429
+ assert ae(fp.chi(-2-50j), -0.0183782829461330671-1.6411491348185849554j)
430
+ assert ae(fp.chi(-50j), -0.0056283863241163054-1.5707963267948966192j)
431
+ assert ae(fp.chi(2-50j), -0.0183782829461330671-1.500443518771208283j)
432
+ assert ae(fp.chi(20-2j), -4.050116856873293505e6-1.20747603112735722951e7j)
433
+ assert ae(fp.chi(20), 1.2807826332028294361e7)
434
+ assert ae(fp.chi(2+50j), -0.0183782829461330671+1.500443518771208283j)
435
+ assert ae(fp.shi(50j), 1.5516170724859358947j)
436
+ assert ae(fp.shi(-2+50j), 0.017515007378437448+1.497884414277228461j)
437
+ assert ae(fp.shi(-20), -1.2807826332028294459e7)
438
+ assert ae(fp.shi(-20-2j), 4.050116856873293554e6-1.20747603112735722103e7j)
439
+ assert ae(fp.shi(-2-50j), 0.017515007378437448-1.497884414277228461j)
440
+ assert ae(fp.shi(-50j), -1.5516170724859358947j)
441
+ assert ae(fp.shi(2-50j), -0.017515007378437448-1.497884414277228461j)
442
+ assert ae(fp.shi(20-2j), -4.050116856873293554e6-1.20747603112735722103e7j)
443
+ assert ae(fp.shi(20), 1.2807826332028294459e7)
444
+ assert ae(fp.shi(2+50j), -0.017515007378437448+1.497884414277228461j)
445
+
446
+ def test_airy():
447
+ mp.dps = 15
448
+ assert (airyai(10)*10**10).ae(1.1047532552898687)
449
+ assert (airybi(10)/10**9).ae(0.45564115354822515)
450
+ assert (airyai(1000)*10**9158).ae(9.306933063179556004)
451
+ assert (airybi(1000)/10**9154).ae(5.4077118391949465477)
452
+ assert airyai(-1000).ae(0.055971895773019918842)
453
+ assert airybi(-1000).ae(-0.083264574117080633012)
454
+ assert (airyai(100+100j)*10**188).ae(2.9099582462207032076 + 2.353013591706178756j)
455
+ assert (airybi(100+100j)/10**185).ae(1.7086751714463652039 - 3.1416590020830804578j)
456
+
457
+ def test_hyper_0f1():
458
+ mp.dps = 15
459
+ v = 8.63911136507950465
460
+ assert hyper([],[(1,3)],1.5).ae(v)
461
+ assert hyper([],[1/3.],1.5).ae(v)
462
+ assert hyp0f1(1/3.,1.5).ae(v)
463
+ assert hyp0f1((1,3),1.5).ae(v)
464
+ # Asymptotic expansion
465
+ assert hyp0f1(3,1e9).ae('4.9679055380347771271e+27455')
466
+ assert hyp0f1(3,1e9j).ae('-2.1222788784457702157e+19410 + 5.0840597555401854116e+19410j')
467
+
468
+ def test_hyper_1f1():
469
+ mp.dps = 15
470
+ v = 1.2917526488617656673
471
+ assert hyper([(1,2)],[(3,2)],0.7).ae(v)
472
+ assert hyper([(1,2)],[(3,2)],0.7+0j).ae(v)
473
+ assert hyper([0.5],[(3,2)],0.7).ae(v)
474
+ assert hyper([0.5],[1.5],0.7).ae(v)
475
+ assert hyper([0.5],[(3,2)],0.7+0j).ae(v)
476
+ assert hyper([0.5],[1.5],0.7+0j).ae(v)
477
+ assert hyper([(1,2)],[1.5+0j],0.7).ae(v)
478
+ assert hyper([0.5+0j],[1.5],0.7).ae(v)
479
+ assert hyper([0.5+0j],[1.5+0j],0.7+0j).ae(v)
480
+ assert hyp1f1(0.5,1.5,0.7).ae(v)
481
+ assert hyp1f1((1,2),1.5,0.7).ae(v)
482
+ # Asymptotic expansion
483
+ assert hyp1f1(2,3,1e10).ae('2.1555012157015796988e+4342944809')
484
+ assert (hyp1f1(2,3,1e10j)*10**10).ae(-0.97501205020039745852 - 1.7462392454512132074j)
485
+ # Shouldn't use asymptotic expansion
486
+ assert hyp1f1(-2, 1, 10000).ae(49980001)
487
+ # Bug
488
+ assert hyp1f1(1j,fraction(1,3),0.415-69.739j).ae(25.857588206024346592 + 15.738060264515292063j)
489
+
490
+ def test_hyper_2f1():
491
+ mp.dps = 15
492
+ v = 1.0652207633823291032
493
+ assert hyper([(1,2), (3,4)], [2], 0.3).ae(v)
494
+ assert hyper([(1,2), 0.75], [2], 0.3).ae(v)
495
+ assert hyper([0.5, 0.75], [2.0], 0.3).ae(v)
496
+ assert hyper([0.5, 0.75], [2.0], 0.3+0j).ae(v)
497
+ assert hyper([0.5+0j, (3,4)], [2.0], 0.3+0j).ae(v)
498
+ assert hyper([0.5+0j, (3,4)], [2.0], 0.3).ae(v)
499
+ assert hyper([0.5, (3,4)], [2.0+0j], 0.3).ae(v)
500
+ assert hyper([0.5+0j, 0.75+0j], [2.0+0j], 0.3+0j).ae(v)
501
+ v = 1.09234681096223231717 + 0.18104859169479360380j
502
+ assert hyper([(1,2),0.75+j], [2], 0.5).ae(v)
503
+ assert hyper([0.5,0.75+j], [2.0], 0.5).ae(v)
504
+ assert hyper([0.5,0.75+j], [2.0], 0.5+0j).ae(v)
505
+ assert hyper([0.5,0.75+j], [2.0+0j], 0.5+0j).ae(v)
506
+ v = 0.9625 - 0.125j
507
+ assert hyper([(3,2),-1],[4], 0.1+j/3).ae(v)
508
+ assert hyper([1.5,-1.0],[4], 0.1+j/3).ae(v)
509
+ assert hyper([1.5,-1.0],[4+0j], 0.1+j/3).ae(v)
510
+ assert hyper([1.5+0j,-1.0+0j],[4+0j], 0.1+j/3).ae(v)
511
+ v = 1.02111069501693445001 - 0.50402252613466859521j
512
+ assert hyper([(2,10),(3,10)],[(4,10)],1.5).ae(v)
513
+ assert hyper([0.2,(3,10)],[0.4+0j],1.5).ae(v)
514
+ assert hyper([0.2,(3,10)],[0.4+0j],1.5+0j).ae(v)
515
+ v = 0.76922501362865848528 + 0.32640579593235886194j
516
+ assert hyper([(2,10),(3,10)],[(4,10)],4+2j).ae(v)
517
+ assert hyper([0.2,(3,10)],[0.4+0j],4+2j).ae(v)
518
+ assert hyper([0.2,(3,10)],[(4,10)],4+2j).ae(v)
519
+
520
+ def test_hyper_2f1_hard():
521
+ mp.dps = 15
522
+ # Singular cases
523
+ assert hyp2f1(2,-1,-1,3).ae(7)
524
+ assert hyp2f1(2,-1,-1,3,eliminate_all=True).ae(0.25)
525
+ assert hyp2f1(2,-2,-2,3).ae(34)
526
+ assert hyp2f1(2,-2,-2,3,eliminate_all=True).ae(0.25)
527
+ assert hyp2f1(2,-2,-3,3) == 14
528
+ assert hyp2f1(2,-3,-2,3) == inf
529
+ assert hyp2f1(2,-1.5,-1.5,3) == 0.25
530
+ assert hyp2f1(1,2,3,0) == 1
531
+ assert hyp2f1(0,1,0,0) == 1
532
+ assert hyp2f1(0,0,0,0) == 1
533
+ assert isnan(hyp2f1(1,1,0,0))
534
+ assert hyp2f1(2,-1,-5, 0.25+0.25j).ae(1.1+0.1j)
535
+ assert hyp2f1(2,-5,-5, 0.25+0.25j, eliminate=False).ae(163./128 + 125./128*j)
536
+ assert hyp2f1(0.7235, -1, -5, 0.3).ae(1.04341)
537
+ assert hyp2f1(0.7235, -5, -5, 0.3, eliminate=False).ae(1.2939225017815903812)
538
+ assert hyp2f1(-1,-2,4,1) == 1.5
539
+ assert hyp2f1(1,2,-3,1) == inf
540
+ assert hyp2f1(-2,-2,1,1) == 6
541
+ assert hyp2f1(1,-2,-4,1).ae(5./3)
542
+ assert hyp2f1(0,-6,-4,1) == 1
543
+ assert hyp2f1(0,-3,-4,1) == 1
544
+ assert hyp2f1(0,0,0,1) == 1
545
+ assert hyp2f1(1,0,0,1,eliminate=False) == 1
546
+ assert hyp2f1(1,1,0,1) == inf
547
+ assert hyp2f1(1,-6,-4,1) == inf
548
+ assert hyp2f1(-7.2,-0.5,-4.5,1) == 0
549
+ assert hyp2f1(-7.2,-1,-2,1).ae(-2.6)
550
+ assert hyp2f1(1,-0.5,-4.5, 1) == inf
551
+ assert hyp2f1(1,0.5,-4.5, 1) == -inf
552
+ # Check evaluation on / close to unit circle
553
+ z = exp(j*pi/3)
554
+ w = (nthroot(2,3)+1)*exp(j*pi/12)/nthroot(3,4)**3
555
+ assert hyp2f1('1/2','1/6','1/3', z).ae(w)
556
+ assert hyp2f1('1/2','1/6','1/3', z.conjugate()).ae(w.conjugate())
557
+ assert hyp2f1(0.25, (1,3), 2, '0.999').ae(1.06826449496030635)
558
+ assert hyp2f1(0.25, (1,3), 2, '1.001').ae(1.06867299254830309446-0.00001446586793975874j)
559
+ assert hyp2f1(0.25, (1,3), 2, -1).ae(0.96656584492524351673)
560
+ assert hyp2f1(0.25, (1,3), 2, j).ae(0.99041766248982072266+0.03777135604180735522j)
561
+ assert hyp2f1(2,3,5,'0.99').ae(27.699347904322690602)
562
+ assert hyp2f1((3,2),-0.5,3,'0.99').ae(0.68403036843911661388)
563
+ assert hyp2f1(2,3,5,1j).ae(0.37290667145974386127+0.59210004902748285917j)
564
+ assert fsum([hyp2f1((7,10),(2,3),(-1,2), 0.95*exp(j*k)) for k in range(1,15)]).ae(52.851400204289452922+6.244285013912953225j)
565
+ assert fsum([hyp2f1((7,10),(2,3),(-1,2), 1.05*exp(j*k)) for k in range(1,15)]).ae(54.506013786220655330-3.000118813413217097j)
566
+ assert fsum([hyp2f1((7,10),(2,3),(-1,2), exp(j*k)) for k in range(1,15)]).ae(55.792077935955314887+1.731986485778500241j)
567
+ assert hyp2f1(2,2.5,-3.25,0.999).ae(218373932801217082543180041.33)
568
+ # Branches
569
+ assert hyp2f1(1,1,2,1.01).ae(4.5595744415723676911-3.1104877758314784539j)
570
+ assert hyp2f1(1,1,2,1.01+0.1j).ae(2.4149427480552782484+1.4148224796836938829j)
571
+ assert hyp2f1(1,1,2,3+4j).ae(0.14576709331407297807+0.48379185417980360773j)
572
+ assert hyp2f1(1,1,2,4).ae(-0.27465307216702742285 - 0.78539816339744830962j)
573
+ assert hyp2f1(1,1,2,-4).ae(0.40235947810852509365)
574
+ # Other:
575
+ # Cancellation with a large parameter involved (bug reported on sage-devel)
576
+ assert hyp2f1(112, (51,10), (-9,10), -0.99999).ae(-1.6241361047970862961e-24, abs_eps=0, rel_eps=eps*16)
577
+
578
+ def test_hyper_3f2_etc():
579
+ assert hyper([1,2,3],[1.5,8],-1).ae(0.67108992351533333030)
580
+ assert hyper([1,2,3,4],[5,6,7], -1).ae(0.90232988035425506008)
581
+ assert hyper([1,2,3],[1.25,5], 1).ae(28.924181329701905701)
582
+ assert hyper([1,2,3,4],[5,6,7],5).ae(1.5192307344006649499-1.1529845225075537461j)
583
+ assert hyper([1,2,3,4,5],[6,7,8,9],-1).ae(0.96288759462882357253)
584
+ assert hyper([1,2,3,4,5],[6,7,8,9],1).ae(1.0428697385885855841)
585
+ assert hyper([1,2,3,4,5],[6,7,8,9],5).ae(1.33980653631074769423-0.07143405251029226699j)
586
+ assert hyper([1,2.79,3.08,4.37],[5.2,6.1,7.3],5).ae(1.0996321464692607231-1.7748052293979985001j)
587
+ assert hyper([1,1,1],[1,2],1) == inf
588
+ assert hyper([1,1,1],[2,(101,100)],1).ae(100.01621213528313220)
589
+ # slow -- covered by doctests
590
+ #assert hyper([1,1,1],[2,3],0.9999).ae(1.2897972005319693905)
591
+
592
+ def test_hyper_u():
593
+ mp.dps = 15
594
+ assert hyperu(2,-3,0).ae(0.05)
595
+ assert hyperu(2,-3.5,0).ae(4./99)
596
+ assert hyperu(2,0,0) == 0.5
597
+ assert hyperu(-5,1,0) == -120
598
+ assert hyperu(-5,2,0) == inf
599
+ assert hyperu(-5,-2,0) == 0
600
+ assert hyperu(7,7,3).ae(0.00014681269365593503986) #exp(3)*gammainc(-6,3)
601
+ assert hyperu(2,-3,4).ae(0.011836478100271995559)
602
+ assert hyperu(3,4,5).ae(1./125)
603
+ assert hyperu(2,3,0.0625) == 256
604
+ assert hyperu(-1,2,0.25+0.5j) == -1.75+0.5j
605
+ assert hyperu(0.5,1.5,7.25).ae(2/sqrt(29))
606
+ assert hyperu(2,6,pi).ae(0.55804439825913399130)
607
+ assert (hyperu((3,2),8,100+201j)*10**4).ae(-0.3797318333856738798 - 2.9974928453561707782j)
608
+ assert (hyperu((5,2),(-1,2),-5000)*10**10).ae(-5.6681877926881664678j)
609
+ # XXX: fails because of undetected cancellation in low level series code
610
+ # Alternatively: could use asymptotic series here, if convergence test
611
+ # tweaked back to recognize this one
612
+ #assert (hyperu((5,2),(-1,2),-500)*10**7).ae(-1.82526906001593252847j)
613
+
614
+ def test_hyper_2f0():
615
+ mp.dps = 15
616
+ assert hyper([1,2],[],3) == hyp2f0(1,2,3)
617
+ assert hyp2f0(2,3,7).ae(0.0116108068639728714668 - 0.0073727413865865802130j)
618
+ assert hyp2f0(2,3,0) == 1
619
+ assert hyp2f0(0,0,0) == 1
620
+ assert hyp2f0(-1,-1,1).ae(2)
621
+ assert hyp2f0(-4,1,1.5).ae(62.5)
622
+ assert hyp2f0(-4,1,50).ae(147029801)
623
+ assert hyp2f0(-4,1,0.0001).ae(0.99960011997600240000)
624
+ assert hyp2f0(0.5,0.25,0.001).ae(1.0001251174078538115)
625
+ assert hyp2f0(0.5,0.25,3+4j).ae(0.85548875824755163518 + 0.21636041283392292973j)
626
+ # Important: cancellation check
627
+ assert hyp2f0((1,6),(5,6),-0.02371708245126284498).ae(0.996785723120804309)
628
+ # Should be exact; polynomial case
629
+ assert hyp2f0(-2,1,0.5+0.5j,zeroprec=200) == 0
630
+ assert hyp2f0(1,-2,0.5+0.5j,zeroprec=200) == 0
631
+ # There used to be a bug in thresholds that made one of the following hang
632
+ for d in [15, 50, 80]:
633
+ mp.dps = d
634
+ assert hyp2f0(1.5, 0.5, 0.009).ae('1.006867007239309717945323585695344927904000945829843527398772456281301440034218290443367270629519483 + 1.238277162240704919639384945859073461954721356062919829456053965502443570466701567100438048602352623e-46j')
635
+
636
+ def test_hyper_1f2():
637
+ mp.dps = 15
638
+ assert hyper([1],[2,3],4) == hyp1f2(1,2,3,4)
639
+ a1,b1,b2 = (1,10),(2,3),1./16
640
+ assert hyp1f2(a1,b1,b2,10).ae(298.7482725554557568)
641
+ assert hyp1f2(a1,b1,b2,100).ae(224128961.48602947604)
642
+ assert hyp1f2(a1,b1,b2,1000).ae(1.1669528298622675109e+27)
643
+ assert hyp1f2(a1,b1,b2,10000).ae(2.4780514622487212192e+86)
644
+ assert hyp1f2(a1,b1,b2,100000).ae(1.3885391458871523997e+274)
645
+ assert hyp1f2(a1,b1,b2,1000000).ae('9.8851796978960318255e+867')
646
+ assert hyp1f2(a1,b1,b2,10**7).ae('1.1505659189516303646e+2746')
647
+ assert hyp1f2(a1,b1,b2,10**8).ae('1.4672005404314334081e+8685')
648
+ assert hyp1f2(a1,b1,b2,10**20).ae('3.6888217332150976493e+8685889636')
649
+ assert hyp1f2(a1,b1,b2,10*j).ae(-16.163252524618572878 - 44.321567896480184312j)
650
+ assert hyp1f2(a1,b1,b2,100*j).ae(61938.155294517848171 + 637349.45215942348739j)
651
+ assert hyp1f2(a1,b1,b2,1000*j).ae(8455057657257695958.7 + 6261969266997571510.6j)
652
+ assert hyp1f2(a1,b1,b2,10000*j).ae(-8.9771211184008593089e+60 + 4.6550528111731631456e+59j)
653
+ assert hyp1f2(a1,b1,b2,100000*j).ae(2.6398091437239324225e+193 + 4.1658080666870618332e+193j)
654
+ assert hyp1f2(a1,b1,b2,1000000*j).ae('3.5999042951925965458e+613 + 1.5026014707128947992e+613j')
655
+ assert hyp1f2(a1,b1,b2,10**7*j).ae('-8.3208715051623234801e+1939 - 3.6752883490851869429e+1941j')
656
+ assert hyp1f2(a1,b1,b2,10**8*j).ae('2.0724195707891484454e+6140 - 1.3276619482724266387e+6141j')
657
+ assert hyp1f2(a1,b1,b2,10**20*j).ae('-1.1734497974795488504e+6141851462 + 1.1498106965385471542e+6141851462j')
658
+
659
+ def test_hyper_2f3():
660
+ mp.dps = 15
661
+ assert hyper([1,2],[3,4,5],6) == hyp2f3(1,2,3,4,5,6)
662
+ a1,a2,b1,b2,b3 = (1,10),(2,3),(3,10), 2, 1./16
663
+ # Check asymptotic expansion
664
+ assert hyp2f3(a1,a2,b1,b2,b3,10).ae(128.98207160698659976)
665
+ assert hyp2f3(a1,a2,b1,b2,b3,1000).ae(6.6309632883131273141e25)
666
+ assert hyp2f3(a1,a2,b1,b2,b3,10000).ae(4.6863639362713340539e84)
667
+ assert hyp2f3(a1,a2,b1,b2,b3,100000).ae(8.6632451236103084119e271)
668
+ assert hyp2f3(a1,a2,b1,b2,b3,10**6).ae('2.0291718386574980641e865')
669
+ assert hyp2f3(a1,a2,b1,b2,b3,10**7).ae('7.7639836665710030977e2742')
670
+ assert hyp2f3(a1,a2,b1,b2,b3,10**8).ae('3.2537462584071268759e8681')
671
+ assert hyp2f3(a1,a2,b1,b2,b3,10**20).ae('1.2966030542911614163e+8685889627')
672
+ assert hyp2f3(a1,a2,b1,b2,b3,10*j).ae(-18.551602185587547854 - 13.348031097874113552j)
673
+ assert hyp2f3(a1,a2,b1,b2,b3,100*j).ae(78634.359124504488695 + 74459.535945281973996j)
674
+ assert hyp2f3(a1,a2,b1,b2,b3,1000*j).ae(597682550276527901.59 - 65136194809352613.078j)
675
+ assert hyp2f3(a1,a2,b1,b2,b3,10000*j).ae(-1.1779696326238582496e+59 + 1.2297607505213133872e+59j)
676
+ assert hyp2f3(a1,a2,b1,b2,b3,100000*j).ae(2.9844228969804380301e+191 + 7.5587163231490273296e+190j)
677
+ assert hyp2f3(a1,a2,b1,b2,b3,1000000*j).ae('7.4859161049322370311e+610 - 2.8467477015940090189e+610j')
678
+ assert hyp2f3(a1,a2,b1,b2,b3,10**7*j).ae('-1.7477645579418800826e+1938 - 1.7606522995808116405e+1938j')
679
+ assert hyp2f3(a1,a2,b1,b2,b3,10**8*j).ae('-1.6932731942958401784e+6137 - 2.4521909113114629368e+6137j')
680
+ assert hyp2f3(a1,a2,b1,b2,b3,10**20*j).ae('-2.0988815677627225449e+6141851451 + 5.7708223542739208681e+6141851452j')
681
+
682
+ def test_hyper_2f2():
683
+ mp.dps = 15
684
+ assert hyper([1,2],[3,4],5) == hyp2f2(1,2,3,4,5)
685
+ a1,a2,b1,b2 = (3,10),4,(1,2),1./16
686
+ assert hyp2f2(a1,a2,b1,b2,10).ae(448225936.3377556696)
687
+ assert hyp2f2(a1,a2,b1,b2,10000).ae('1.2012553712966636711e+4358')
688
+ assert hyp2f2(a1,a2,b1,b2,-20000).ae(-0.04182343755661214626)
689
+ assert hyp2f2(a1,a2,b1,b2,10**20).ae('1.1148680024303263661e+43429448190325182840')
690
+
691
+ def test_orthpoly():
692
+ mp.dps = 15
693
+ assert jacobi(-4,2,3,0.7).ae(22800./4913)
694
+ assert jacobi(3,2,4,5.5) == 4133.125
695
+ assert jacobi(1.5,5/6.,4,0).ae(-1.0851951434075508417)
696
+ assert jacobi(-2, 1, 2, 4).ae(-0.16)
697
+ assert jacobi(2, -1, 2.5, 4).ae(34.59375)
698
+ #assert jacobi(2, -1, 2, 4) == 28.5
699
+ assert legendre(5, 7) == 129367
700
+ assert legendre(0.5,0).ae(0.53935260118837935667)
701
+ assert legendre(-1,-1) == 1
702
+ assert legendre(0,-1) == 1
703
+ assert legendre(0, 1) == 1
704
+ assert legendre(1, -1) == -1
705
+ assert legendre(7, 1) == 1
706
+ assert legendre(7, -1) == -1
707
+ assert legendre(8,1.5).ae(15457523./32768)
708
+ assert legendre(j,-j).ae(2.4448182735671431011 + 0.6928881737669934843j)
709
+ assert chebyu(5,1) == 6
710
+ assert chebyt(3,2) == 26
711
+ assert legendre(3.5,-1) == inf
712
+ assert legendre(4.5,-1) == -inf
713
+ assert legendre(3.5+1j,-1) == mpc(inf,inf)
714
+ assert legendre(4.5+1j,-1) == mpc(-inf,-inf)
715
+ assert laguerre(4, -2, 3).ae(-1.125)
716
+ assert laguerre(3, 1+j, 0.5).ae(0.2291666666666666667 + 2.5416666666666666667j)
717
+
718
+ def test_hermite():
719
+ mp.dps = 15
720
+ assert hermite(-2, 0).ae(0.5)
721
+ assert hermite(-1, 0).ae(0.88622692545275801365)
722
+ assert hermite(0, 0).ae(1)
723
+ assert hermite(1, 0) == 0
724
+ assert hermite(2, 0).ae(-2)
725
+ assert hermite(0, 2).ae(1)
726
+ assert hermite(1, 2).ae(4)
727
+ assert hermite(1, -2).ae(-4)
728
+ assert hermite(2, -2).ae(14)
729
+ assert hermite(0.5, 0).ae(0.69136733903629335053)
730
+ assert hermite(9, 0) == 0
731
+ assert hermite(4,4).ae(3340)
732
+ assert hermite(3,4).ae(464)
733
+ assert hermite(-4,4).ae(0.00018623860287512396181)
734
+ assert hermite(-3,4).ae(0.0016540169879668766270)
735
+ assert hermite(9, 2.5j).ae(13638725j)
736
+ assert hermite(9, -2.5j).ae(-13638725j)
737
+ assert hermite(9, 100).ae(511078883759363024000)
738
+ assert hermite(9, -100).ae(-511078883759363024000)
739
+ assert hermite(9, 100j).ae(512922083920643024000j)
740
+ assert hermite(9, -100j).ae(-512922083920643024000j)
741
+ assert hermite(-9.5, 2.5j).ae(-2.9004951258126778174e-6 + 1.7601372934039951100e-6j)
742
+ assert hermite(-9.5, -2.5j).ae(-2.9004951258126778174e-6 - 1.7601372934039951100e-6j)
743
+ assert hermite(-9.5, 100).ae(1.3776300722767084162e-22, abs_eps=0, rel_eps=eps)
744
+ assert hermite(-9.5, -100).ae('1.3106082028470671626e4355')
745
+ assert hermite(-9.5, 100j).ae(-9.7900218581864768430e-23 - 9.7900218581864768430e-23j, abs_eps=0, rel_eps=eps)
746
+ assert hermite(-9.5, -100j).ae(-9.7900218581864768430e-23 + 9.7900218581864768430e-23j, abs_eps=0, rel_eps=eps)
747
+ assert hermite(2+3j, -1-j).ae(851.3677063883687676 - 1496.4373467871007997j)
748
+
749
+ def test_gegenbauer():
750
+ mp.dps = 15
751
+ assert gegenbauer(1,2,3).ae(12)
752
+ assert gegenbauer(2,3,4).ae(381)
753
+ assert gegenbauer(0,0,0) == 0
754
+ assert gegenbauer(2,-1,3) == 0
755
+ assert gegenbauer(-7, 0.5, 3).ae(8989)
756
+ assert gegenbauer(1, -0.5, 3).ae(-3)
757
+ assert gegenbauer(1, -1.5, 3).ae(-9)
758
+ assert gegenbauer(1, -0.5, 3).ae(-3)
759
+ assert gegenbauer(-0.5, -0.5, 3).ae(-2.6383553159023906245)
760
+ assert gegenbauer(2+3j, 1-j, 3+4j).ae(14.880536623203696780 + 20.022029711598032898j)
761
+ #assert gegenbauer(-2, -0.5, 3).ae(-12)
762
+
763
+ def test_legenp():
764
+ mp.dps = 15
765
+ assert legenp(2,0,4) == legendre(2,4)
766
+ assert legenp(-2, -1, 0.5).ae(0.43301270189221932338)
767
+ assert legenp(-2, -1, 0.5, type=3).ae(0.43301270189221932338j)
768
+ assert legenp(-2, 1, 0.5).ae(-0.86602540378443864676)
769
+ assert legenp(2+j, 3+4j, -j).ae(134742.98773236786148 + 429782.72924463851745j)
770
+ assert legenp(2+j, 3+4j, -j, type=3).ae(802.59463394152268507 - 251.62481308942906447j)
771
+ assert legenp(2,4,3).ae(0)
772
+ assert legenp(2,4,3,type=3).ae(0)
773
+ assert legenp(2,1,0.5).ae(-1.2990381056766579701)
774
+ assert legenp(2,1,0.5,type=3).ae(1.2990381056766579701j)
775
+ assert legenp(3,2,3).ae(-360)
776
+ assert legenp(3,3,3).ae(240j*2**0.5)
777
+ assert legenp(3,4,3).ae(0)
778
+ assert legenp(0,0.5,2).ae(0.52503756790433198939 - 0.52503756790433198939j)
779
+ assert legenp(-1,-0.5,2).ae(0.60626116232846498110 + 0.60626116232846498110j)
780
+ assert legenp(-2,0.5,2).ae(1.5751127037129959682 - 1.5751127037129959682j)
781
+ assert legenp(-2,0.5,-0.5).ae(-0.85738275810499171286)
782
+
783
+ def test_legenq():
784
+ mp.dps = 15
785
+ f = legenq
786
+ # Evaluation at poles
787
+ assert isnan(f(3,2,1))
788
+ assert isnan(f(3,2,-1))
789
+ assert isnan(f(3,2,1,type=3))
790
+ assert isnan(f(3,2,-1,type=3))
791
+ # Evaluation at 0
792
+ assert f(0,1,0,type=2).ae(-1)
793
+ assert f(-2,2,0,type=2,zeroprec=200).ae(0)
794
+ assert f(1.5,3,0,type=2).ae(-2.2239343475841951023)
795
+ assert f(0,1,0,type=3).ae(j)
796
+ assert f(-2,2,0,type=3,zeroprec=200).ae(0)
797
+ assert f(1.5,3,0,type=3).ae(2.2239343475841951022*(1-1j))
798
+ # Standard case, degree 0
799
+ assert f(0,0,-1.5).ae(-0.8047189562170501873 + 1.5707963267948966192j)
800
+ assert f(0,0,-0.5).ae(-0.54930614433405484570)
801
+ assert f(0,0,0,zeroprec=200).ae(0)
802
+ assert f(0,0,0.5).ae(0.54930614433405484570)
803
+ assert f(0,0,1.5).ae(0.8047189562170501873 - 1.5707963267948966192j)
804
+ assert f(0,0,-1.5,type=3).ae(-0.80471895621705018730)
805
+ assert f(0,0,-0.5,type=3).ae(-0.5493061443340548457 - 1.5707963267948966192j)
806
+ assert f(0,0,0,type=3).ae(-1.5707963267948966192j)
807
+ assert f(0,0,0.5,type=3).ae(0.5493061443340548457 - 1.5707963267948966192j)
808
+ assert f(0,0,1.5,type=3).ae(0.80471895621705018730)
809
+ # Standard case, degree 1
810
+ assert f(1,0,-1.5).ae(0.2070784343255752810 - 2.3561944901923449288j)
811
+ assert f(1,0,-0.5).ae(-0.72534692783297257715)
812
+ assert f(1,0,0).ae(-1)
813
+ assert f(1,0,0.5).ae(-0.72534692783297257715)
814
+ assert f(1,0,1.5).ae(0.2070784343255752810 - 2.3561944901923449288j)
815
+ # Standard case, degree 2
816
+ assert f(2,0,-1.5).ae(-0.0635669991240192885 + 4.5160394395353277803j)
817
+ assert f(2,0,-0.5).ae(0.81866326804175685571)
818
+ assert f(2,0,0,zeroprec=200).ae(0)
819
+ assert f(2,0,0.5).ae(-0.81866326804175685571)
820
+ assert f(2,0,1.5).ae(0.0635669991240192885 - 4.5160394395353277803j)
821
+ # Misc orders and degrees
822
+ assert f(2,3,1.5,type=2).ae(-5.7243340223994616228j)
823
+ assert f(2,3,1.5,type=3).ae(-5.7243340223994616228)
824
+ assert f(2,3,0.5,type=2).ae(-12.316805742712016310)
825
+ assert f(2,3,0.5,type=3).ae(-12.316805742712016310j)
826
+ assert f(2,3,-1.5,type=2).ae(-5.7243340223994616228j)
827
+ assert f(2,3,-1.5,type=3).ae(5.7243340223994616228)
828
+ assert f(2,3,-0.5,type=2).ae(-12.316805742712016310)
829
+ assert f(2,3,-0.5,type=3).ae(-12.316805742712016310j)
830
+ assert f(2+3j, 3+4j, 0.5, type=3).ae(0.0016119404873235186807 - 0.0005885900510718119836j)
831
+ assert f(2+3j, 3+4j, -1.5, type=3).ae(0.008451400254138808670 + 0.020645193304593235298j)
832
+ assert f(-2.5,1,-1.5).ae(3.9553395527435335749j)
833
+ assert f(-2.5,1,-0.5).ae(1.9290561746445456908)
834
+ assert f(-2.5,1,0).ae(1.2708196271909686299)
835
+ assert f(-2.5,1,0.5).ae(-0.31584812990742202869)
836
+ assert f(-2.5,1,1.5).ae(-3.9553395527435335742 + 0.2993235655044701706j)
837
+ assert f(-2.5,1,-1.5,type=3).ae(0.29932356550447017254j)
838
+ assert f(-2.5,1,-0.5,type=3).ae(-0.3158481299074220287 - 1.9290561746445456908j)
839
+ assert f(-2.5,1,0,type=3).ae(1.2708196271909686292 - 1.2708196271909686299j)
840
+ assert f(-2.5,1,0.5,type=3).ae(1.9290561746445456907 + 0.3158481299074220287j)
841
+ assert f(-2.5,1,1.5,type=3).ae(-0.29932356550447017254)
842
+
843
+ def test_agm():
844
+ mp.dps = 15
845
+ assert agm(0,0) == 0
846
+ assert agm(0,1) == 0
847
+ assert agm(1,1) == 1
848
+ assert agm(7,7) == 7
849
+ assert agm(j,j) == j
850
+ assert (1/agm(1,sqrt(2))).ae(0.834626841674073186)
851
+ assert agm(1,2).ae(1.4567910310469068692)
852
+ assert agm(1,3).ae(1.8636167832448965424)
853
+ assert agm(1,j).ae(0.599070117367796104+0.599070117367796104j)
854
+ assert agm(2) == agm(1,2)
855
+ assert agm(-3,4).ae(0.63468509766550907+1.3443087080896272j)
856
+
857
+ def test_gammainc():
858
+ mp.dps = 15
859
+ assert gammainc(2,5).ae(6*exp(-5))
860
+ assert gammainc(2,0,5).ae(1-6*exp(-5))
861
+ assert gammainc(2,3,5).ae(-6*exp(-5)+4*exp(-3))
862
+ assert gammainc(-2.5,-0.5).ae(-0.9453087204829418812-5.3164237738936178621j)
863
+ assert gammainc(0,2,4).ae(0.045121158298212213088)
864
+ assert gammainc(0,3).ae(0.013048381094197037413)
865
+ assert gammainc(0,2+j,1-j).ae(0.00910653685850304839-0.22378752918074432574j)
866
+ assert gammainc(0,1-j).ae(0.00028162445198141833+0.17932453503935894015j)
867
+ assert gammainc(3,4,5,True).ae(0.11345128607046320253)
868
+ assert gammainc(3.5,0,inf).ae(gamma(3.5))
869
+ assert gammainc(-150.5,500).ae('6.9825435345798951153e-627')
870
+ assert gammainc(-150.5,800).ae('4.6885137549474089431e-788')
871
+ assert gammainc(-3.5, -20.5).ae(0.27008820585226911 - 1310.31447140574997636j)
872
+ assert gammainc(-3.5, -200.5).ae(0.27008820585226911 - 5.3264597096208368435e76j) # XXX real part
873
+ assert gammainc(0,0,2) == inf
874
+ assert gammainc(1,b=1).ae(0.6321205588285576784)
875
+ assert gammainc(3,2,2) == 0
876
+ assert gammainc(2,3+j,3-j).ae(-0.28135485191849314194j)
877
+ assert gammainc(4+0j,1).ae(5.8860710587430771455)
878
+ # GH issue #301
879
+ assert gammainc(-1,-1).ae(-0.8231640121031084799 + 3.1415926535897932385j)
880
+ assert gammainc(-2,-1).ae(1.7707229202810768576 - 1.5707963267948966192j)
881
+ assert gammainc(-3,-1).ae(-1.4963349162467073643 + 0.5235987755982988731j)
882
+ assert gammainc(-4,-1).ae(1.05365418617643814992 - 0.13089969389957471827j)
883
+ # Regularized upper gamma
884
+ assert isnan(gammainc(0, 0, regularized=True))
885
+ assert gammainc(-1, 0, regularized=True) == inf
886
+ assert gammainc(1, 0, regularized=True) == 1
887
+ assert gammainc(0, 5, regularized=True) == 0
888
+ assert gammainc(0, 2+3j, regularized=True) == 0
889
+ assert gammainc(0, 5000, regularized=True) == 0
890
+ assert gammainc(0, 10**30, regularized=True) == 0
891
+ assert gammainc(-1, 5, regularized=True) == 0
892
+ assert gammainc(-1, 5000, regularized=True) == 0
893
+ assert gammainc(-1, 10**30, regularized=True) == 0
894
+ assert gammainc(-1, -5, regularized=True) == 0
895
+ assert gammainc(-1, -5000, regularized=True) == 0
896
+ assert gammainc(-1, -10**30, regularized=True) == 0
897
+ assert gammainc(-1, 3+4j, regularized=True) == 0
898
+ assert gammainc(1, 5, regularized=True).ae(exp(-5))
899
+ assert gammainc(1, 5000, regularized=True).ae(exp(-5000))
900
+ assert gammainc(1, 10**30, regularized=True).ae(exp(-10**30))
901
+ assert gammainc(1, 3+4j, regularized=True).ae(exp(-3-4j))
902
+ assert gammainc(-1000000,2).ae('1.3669297209397347754e-301037', abs_eps=0, rel_eps=8*eps)
903
+ assert gammainc(-1000000,2,regularized=True) == 0
904
+ assert gammainc(-1000000,3+4j).ae('-1.322575609404222361e-698979 - 4.9274570591854533273e-698978j', abs_eps=0, rel_eps=8*eps)
905
+ assert gammainc(-1000000,3+4j,regularized=True) == 0
906
+ assert gammainc(2+3j, 4+5j, regularized=True).ae(0.085422013530993285774-0.052595379150390078503j)
907
+ assert gammainc(1000j, 1000j, regularized=True).ae(0.49702647628921131761 + 0.00297355675013575341j)
908
+ # Generalized
909
+ assert gammainc(3,4,2) == -gammainc(3,2,4)
910
+ assert gammainc(4, 2, 3).ae(1.2593494302978947396)
911
+ assert gammainc(4, 2, 3, regularized=True).ae(0.20989157171631578993)
912
+ assert gammainc(0, 2, 3).ae(0.035852129613864082155)
913
+ assert gammainc(0, 2, 3, regularized=True) == 0
914
+ assert gammainc(-1, 2, 3).ae(0.015219822548487616132)
915
+ assert gammainc(-1, 2, 3, regularized=True) == 0
916
+ assert gammainc(0, 2, 3).ae(0.035852129613864082155)
917
+ assert gammainc(0, 2, 3, regularized=True) == 0
918
+ # Should use upper gammas
919
+ assert gammainc(5, 10000, 12000).ae('1.1359381951461801687e-4327', abs_eps=0, rel_eps=8*eps)
920
+ # Should use lower gammas
921
+ assert gammainc(10000, 2, 3).ae('8.1244514125995785934e4765')
922
+ # GH issue 306
923
+ assert gammainc(3,-1-1j) == 0
924
+ assert gammainc(3,-1+1j) == 0
925
+ assert gammainc(2,-1) == 0
926
+ assert gammainc(2,-1+0j) == 0
927
+ assert gammainc(2+0j,-1) == 0
928
+
929
+ def test_gammainc_expint_n():
930
+ # These tests are intended to check all cases of the low-level code
931
+ # for upper gamma and expint with small integer index.
932
+ # Need to cover positive/negative arguments; small/large/huge arguments
933
+ # for both positive and negative indices, as well as indices 0 and 1
934
+ # which may be special-cased
935
+ mp.dps = 15
936
+ assert expint(-3,3.5).ae(0.021456366563296693987)
937
+ assert expint(-2,3.5).ae(0.014966633183073309405)
938
+ assert expint(-1,3.5).ae(0.011092916359219041088)
939
+ assert expint(0,3.5).ae(0.0086278238349481430685)
940
+ assert expint(1,3.5).ae(0.0069701398575483929193)
941
+ assert expint(2,3.5).ae(0.0058018939208991255223)
942
+ assert expint(3,3.5).ae(0.0049453773495857807058)
943
+ assert expint(-3,-3.5).ae(-4.6618170604073311319)
944
+ assert expint(-2,-3.5).ae(-5.5996974157555515963)
945
+ assert expint(-1,-3.5).ae(-6.7582555017739415818)
946
+ assert expint(0,-3.5).ae(-9.4615577024835182145)
947
+ assert expint(1,-3.5).ae(-13.925353995152335292 - 3.1415926535897932385j)
948
+ assert expint(2,-3.5).ae(-15.62328702434085977 - 10.995574287564276335j)
949
+ assert expint(3,-3.5).ae(-10.783026313250347722 - 19.242255003237483586j)
950
+ assert expint(-3,350).ae(2.8614825451252838069e-155, abs_eps=0, rel_eps=8*eps)
951
+ assert expint(-2,350).ae(2.8532837224504675901e-155, abs_eps=0, rel_eps=8*eps)
952
+ assert expint(-1,350).ae(2.8451316155828634555e-155, abs_eps=0, rel_eps=8*eps)
953
+ assert expint(0,350).ae(2.8370258275042797989e-155, abs_eps=0, rel_eps=8*eps)
954
+ assert expint(1,350).ae(2.8289659656701459404e-155, abs_eps=0, rel_eps=8*eps)
955
+ assert expint(2,350).ae(2.8209516419468505006e-155, abs_eps=0, rel_eps=8*eps)
956
+ assert expint(3,350).ae(2.8129824725501272171e-155, abs_eps=0, rel_eps=8*eps)
957
+ assert expint(-3,-350).ae(-2.8528796154044839443e+149)
958
+ assert expint(-2,-350).ae(-2.8610072121701264351e+149)
959
+ assert expint(-1,-350).ae(-2.8691813842677537647e+149)
960
+ assert expint(0,-350).ae(-2.8774025343659421709e+149)
961
+ u = expint(1,-350)
962
+ assert u.ae(-2.8856710698020863568e+149)
963
+ assert u.imag.ae(-3.1415926535897932385)
964
+ u = expint(2,-350)
965
+ assert u.ae(-2.8939874026504650534e+149)
966
+ assert u.imag.ae(-1099.5574287564276335)
967
+ u = expint(3,-350)
968
+ assert u.ae(-2.9023519497915044349e+149)
969
+ assert u.imag.ae(-192422.55003237483586)
970
+ assert expint(-3,350000000000000000000000).ae('2.1592908471792544286e-152003068666138139677919', abs_eps=0, rel_eps=8*eps)
971
+ assert expint(-2,350000000000000000000000).ae('2.1592908471792544286e-152003068666138139677919', abs_eps=0, rel_eps=8*eps)
972
+ assert expint(-1,350000000000000000000000).ae('2.1592908471792544286e-152003068666138139677919', abs_eps=0, rel_eps=8*eps)
973
+ assert expint(0,350000000000000000000000).ae('2.1592908471792544286e-152003068666138139677919', abs_eps=0, rel_eps=8*eps)
974
+ assert expint(1,350000000000000000000000).ae('2.1592908471792544286e-152003068666138139677919', abs_eps=0, rel_eps=8*eps)
975
+ assert expint(2,350000000000000000000000).ae('2.1592908471792544286e-152003068666138139677919', abs_eps=0, rel_eps=8*eps)
976
+ assert expint(3,350000000000000000000000).ae('2.1592908471792544286e-152003068666138139677919', abs_eps=0, rel_eps=8*eps)
977
+ assert expint(-3,-350000000000000000000000).ae('-3.7805306852415755699e+152003068666138139677871')
978
+ assert expint(-2,-350000000000000000000000).ae('-3.7805306852415755699e+152003068666138139677871')
979
+ assert expint(-1,-350000000000000000000000).ae('-3.7805306852415755699e+152003068666138139677871')
980
+ assert expint(0,-350000000000000000000000).ae('-3.7805306852415755699e+152003068666138139677871')
981
+ u = expint(1,-350000000000000000000000)
982
+ assert u.ae('-3.7805306852415755699e+152003068666138139677871')
983
+ assert u.imag.ae(-3.1415926535897932385)
984
+ u = expint(2,-350000000000000000000000)
985
+ assert u.imag.ae(-1.0995574287564276335e+24)
986
+ assert u.ae('-3.7805306852415755699e+152003068666138139677871')
987
+ u = expint(3,-350000000000000000000000)
988
+ assert u.imag.ae(-1.9242255003237483586e+47)
989
+ assert u.ae('-3.7805306852415755699e+152003068666138139677871')
990
+ # Small case; no branch cut
991
+ assert gammainc(-3,3.5).ae(0.00010020262545203707109)
992
+ assert gammainc(-2,3.5).ae(0.00040370427343557393517)
993
+ assert gammainc(-1,3.5).ae(0.0016576839773997501492)
994
+ assert gammainc(0,3.5).ae(0.0069701398575483929193)
995
+ assert gammainc(1,3.5).ae(0.03019738342231850074)
996
+ assert gammainc(2,3.5).ae(0.13588822540043325333)
997
+ assert gammainc(3,3.5).ae(0.64169439772426814072)
998
+ # Small case; with branch cut
999
+ assert gammainc(-3,-3.5).ae(0.03595832954467563286 + 0.52359877559829887308j)
1000
+ assert gammainc(-2,-3.5).ae(-0.88024704597962022221 - 1.5707963267948966192j)
1001
+ assert gammainc(-1,-3.5).ae(4.4637962926688170771 + 3.1415926535897932385j)
1002
+ assert gammainc(0,-3.5).ae(-13.925353995152335292 - 3.1415926535897932385j)
1003
+ assert gammainc(1,-3.5).ae(33.115451958692313751)
1004
+ assert gammainc(2,-3.5).ae(-82.788629896730784377)
1005
+ assert gammainc(3,-3.5).ae(240.08702670051927469)
1006
+ # Asymptotic case; no branch cut
1007
+ assert gammainc(-3,350).ae(6.5424095113340358813e-163, abs_eps=0, rel_eps=8*eps)
1008
+ assert gammainc(-2,350).ae(2.296312222489899769e-160, abs_eps=0, rel_eps=8*eps)
1009
+ assert gammainc(-1,350).ae(8.059861834133858573e-158, abs_eps=0, rel_eps=8*eps)
1010
+ assert gammainc(0,350).ae(2.8289659656701459404e-155, abs_eps=0, rel_eps=8*eps)
1011
+ assert gammainc(1,350).ae(9.9295903962649792963e-153, abs_eps=0, rel_eps=8*eps)
1012
+ assert gammainc(2,350).ae(3.485286229089007733e-150, abs_eps=0, rel_eps=8*eps)
1013
+ assert gammainc(3,350).ae(1.2233453960006379793e-147, abs_eps=0, rel_eps=8*eps)
1014
+ # Asymptotic case; branch cut
1015
+ u = gammainc(-3,-350)
1016
+ assert u.ae(6.7889565783842895085e+141)
1017
+ assert u.imag.ae(0.52359877559829887308)
1018
+ u = gammainc(-2,-350)
1019
+ assert u.ae(-2.3692668977889832121e+144)
1020
+ assert u.imag.ae(-1.5707963267948966192)
1021
+ u = gammainc(-1,-350)
1022
+ assert u.ae(8.2685354361441858669e+146)
1023
+ assert u.imag.ae(3.1415926535897932385)
1024
+ u = gammainc(0,-350)
1025
+ assert u.ae(-2.8856710698020863568e+149)
1026
+ assert u.imag.ae(-3.1415926535897932385)
1027
+ u = gammainc(1,-350)
1028
+ assert u.ae(1.0070908870280797598e+152)
1029
+ assert u.imag == 0
1030
+ u = gammainc(2,-350)
1031
+ assert u.ae(-3.5147471957279983618e+154)
1032
+ assert u.imag == 0
1033
+ u = gammainc(3,-350)
1034
+ assert u.ae(1.2266568422179417091e+157)
1035
+ assert u.imag == 0
1036
+ # Extreme asymptotic case
1037
+ assert gammainc(-3,350000000000000000000000).ae('5.0362468738874738859e-152003068666138139677990', abs_eps=0, rel_eps=8*eps)
1038
+ assert gammainc(-2,350000000000000000000000).ae('1.7626864058606158601e-152003068666138139677966', abs_eps=0, rel_eps=8*eps)
1039
+ assert gammainc(-1,350000000000000000000000).ae('6.1694024205121555102e-152003068666138139677943', abs_eps=0, rel_eps=8*eps)
1040
+ assert gammainc(0,350000000000000000000000).ae('2.1592908471792544286e-152003068666138139677919', abs_eps=0, rel_eps=8*eps)
1041
+ assert gammainc(1,350000000000000000000000).ae('7.5575179651273905e-152003068666138139677896', abs_eps=0, rel_eps=8*eps)
1042
+ assert gammainc(2,350000000000000000000000).ae('2.645131287794586675e-152003068666138139677872', abs_eps=0, rel_eps=8*eps)
1043
+ assert gammainc(3,350000000000000000000000).ae('9.2579595072810533625e-152003068666138139677849', abs_eps=0, rel_eps=8*eps)
1044
+ u = gammainc(-3,-350000000000000000000000)
1045
+ assert u.ae('8.8175642804468234866e+152003068666138139677800')
1046
+ assert u.imag.ae(0.52359877559829887308)
1047
+ u = gammainc(-2,-350000000000000000000000)
1048
+ assert u.ae('-3.0861474981563882203e+152003068666138139677824')
1049
+ assert u.imag.ae(-1.5707963267948966192)
1050
+ u = gammainc(-1,-350000000000000000000000)
1051
+ assert u.ae('1.0801516243547358771e+152003068666138139677848')
1052
+ assert u.imag.ae(3.1415926535897932385)
1053
+ u = gammainc(0,-350000000000000000000000)
1054
+ assert u.ae('-3.7805306852415755699e+152003068666138139677871')
1055
+ assert u.imag.ae(-3.1415926535897932385)
1056
+ assert gammainc(1,-350000000000000000000000).ae('1.3231857398345514495e+152003068666138139677895')
1057
+ assert gammainc(2,-350000000000000000000000).ae('-4.6311500894209300731e+152003068666138139677918')
1058
+ assert gammainc(3,-350000000000000000000000).ae('1.6209025312973255256e+152003068666138139677942')
1059
+
1060
+ def test_incomplete_beta():
1061
+ mp.dps = 15
1062
+ assert betainc(-2,-3,0.5,0.75).ae(63.4305673311255413583969)
1063
+ assert betainc(4.5,0.5+2j,2.5,6).ae(0.2628801146130621387903065 + 0.5162565234467020592855378j)
1064
+ assert betainc(4,5,0,6).ae(90747.77142857142857142857)
1065
+
1066
+ def test_erf():
1067
+ mp.dps = 15
1068
+ assert erf(0) == 0
1069
+ assert erf(1).ae(0.84270079294971486934)
1070
+ assert erf(3+4j).ae(-120.186991395079444098 - 27.750337293623902498j)
1071
+ assert erf(-4-3j).ae(-0.99991066178539168236 + 0.00004972026054496604j)
1072
+ assert erf(pi).ae(0.99999112385363235839)
1073
+ assert erf(1j).ae(1.6504257587975428760j)
1074
+ assert erf(-1j).ae(-1.6504257587975428760j)
1075
+ assert isinstance(erf(1), mpf)
1076
+ assert isinstance(erf(-1), mpf)
1077
+ assert isinstance(erf(0), mpf)
1078
+ assert isinstance(erf(0j), mpc)
1079
+ assert erf(inf) == 1
1080
+ assert erf(-inf) == -1
1081
+ assert erfi(0) == 0
1082
+ assert erfi(1/pi).ae(0.371682698493894314)
1083
+ assert erfi(inf) == inf
1084
+ assert erfi(-inf) == -inf
1085
+ assert erf(1+0j) == erf(1)
1086
+ assert erfc(1+0j) == erfc(1)
1087
+ assert erf(0.2+0.5j).ae(1 - erfc(0.2+0.5j))
1088
+ assert erfc(0) == 1
1089
+ assert erfc(1).ae(1-erf(1))
1090
+ assert erfc(-1).ae(1-erf(-1))
1091
+ assert erfc(1/pi).ae(1-erf(1/pi))
1092
+ assert erfc(-10) == 2
1093
+ assert erfc(-1000000) == 2
1094
+ assert erfc(-inf) == 2
1095
+ assert erfc(inf) == 0
1096
+ assert isnan(erfc(nan))
1097
+ assert (erfc(10**4)*mpf(10)**43429453).ae('3.63998738656420')
1098
+ assert erf(8+9j).ae(-1072004.2525062051158 + 364149.91954310255423j)
1099
+ assert erfc(8+9j).ae(1072005.2525062051158 - 364149.91954310255423j)
1100
+ assert erfc(-8-9j).ae(-1072003.2525062051158 + 364149.91954310255423j)
1101
+ mp.dps = 50
1102
+ # This one does not use the asymptotic series
1103
+ assert (erfc(10)*10**45).ae('2.0884875837625447570007862949577886115608181193212')
1104
+ # This one does
1105
+ assert (erfc(50)*10**1088).ae('2.0709207788416560484484478751657887929322509209954')
1106
+ mp.dps = 15
1107
+ assert str(erfc(10**50)) == '3.66744826532555e-4342944819032518276511289189166050822943970058036665661144537831658646492088707747292249493384317534'
1108
+ assert erfinv(0) == 0
1109
+ assert erfinv(0.5).ae(0.47693627620446987338)
1110
+ assert erfinv(-0.5).ae(-0.47693627620446987338)
1111
+ assert erfinv(1) == inf
1112
+ assert erfinv(-1) == -inf
1113
+ assert erf(erfinv(0.95)).ae(0.95)
1114
+ assert erf(erfinv(0.999999999995)).ae(0.999999999995)
1115
+ assert erf(erfinv(-0.999999999995)).ae(-0.999999999995)
1116
+ mp.dps = 50
1117
+ assert erf(erfinv('0.99999999999999999999999999999995')).ae('0.99999999999999999999999999999995')
1118
+ assert erf(erfinv('0.999999999999999999999999999999995')).ae('0.999999999999999999999999999999995')
1119
+ assert erf(erfinv('-0.999999999999999999999999999999995')).ae('-0.999999999999999999999999999999995')
1120
+ mp.dps = 15
1121
+ # Complex asymptotic expansions
1122
+ v = erfc(50j)
1123
+ assert v.real == 1
1124
+ assert v.imag.ae('-6.1481820666053078736e+1083')
1125
+ assert erfc(-100+5j).ae(2)
1126
+ assert (erfc(100+5j)*10**4335).ae(2.3973567853824133572 - 3.9339259530609420597j)
1127
+ assert erfc(100+100j).ae(0.00065234366376857698698 - 0.0039357263629214118437j)
1128
+
1129
+ def test_pdf():
1130
+ mp.dps = 15
1131
+ assert npdf(-inf) == 0
1132
+ assert npdf(inf) == 0
1133
+ assert npdf(5,0,2).ae(npdf(5+4,4,2))
1134
+ assert quadts(lambda x: npdf(x,-0.5,0.8), [-inf, inf]) == 1
1135
+ assert ncdf(0) == 0.5
1136
+ assert ncdf(3,3) == 0.5
1137
+ assert ncdf(-inf) == 0
1138
+ assert ncdf(inf) == 1
1139
+ assert ncdf(10) == 1
1140
+ # Verify that this is computed accurately
1141
+ assert (ncdf(-10)*10**24).ae(7.619853024160526)
1142
+
1143
+ def test_lambertw():
1144
+ mp.dps = 15
1145
+ assert lambertw(0) == 0
1146
+ assert lambertw(0+0j) == 0
1147
+ assert lambertw(inf) == inf
1148
+ assert isnan(lambertw(nan))
1149
+ assert lambertw(inf,1).real == inf
1150
+ assert lambertw(inf,1).imag.ae(2*pi)
1151
+ assert lambertw(-inf,1).real == inf
1152
+ assert lambertw(-inf,1).imag.ae(3*pi)
1153
+ assert lambertw(0,-1) == -inf
1154
+ assert lambertw(0,1) == -inf
1155
+ assert lambertw(0,3) == -inf
1156
+ assert lambertw(e).ae(1)
1157
+ assert lambertw(1).ae(0.567143290409783873)
1158
+ assert lambertw(-pi/2).ae(j*pi/2)
1159
+ assert lambertw(-log(2)/2).ae(-log(2))
1160
+ assert lambertw(0.25).ae(0.203888354702240164)
1161
+ assert lambertw(-0.25).ae(-0.357402956181388903)
1162
+ assert lambertw(-1./10000,0).ae(-0.000100010001500266719)
1163
+ assert lambertw(-0.25,-1).ae(-2.15329236411034965)
1164
+ assert lambertw(0.25,-1).ae(-3.00899800997004620-4.07652978899159763j)
1165
+ assert lambertw(-0.25,-1).ae(-2.15329236411034965)
1166
+ assert lambertw(0.25,1).ae(-3.00899800997004620+4.07652978899159763j)
1167
+ assert lambertw(-0.25,1).ae(-3.48973228422959210+7.41405453009603664j)
1168
+ assert lambertw(-4).ae(0.67881197132094523+1.91195078174339937j)
1169
+ assert lambertw(-4,1).ae(-0.66743107129800988+7.76827456802783084j)
1170
+ assert lambertw(-4,-1).ae(0.67881197132094523-1.91195078174339937j)
1171
+ assert lambertw(1000).ae(5.24960285240159623)
1172
+ assert lambertw(1000,1).ae(4.91492239981054535+5.44652615979447070j)
1173
+ assert lambertw(1000,-1).ae(4.91492239981054535-5.44652615979447070j)
1174
+ assert lambertw(1000,5).ae(3.5010625305312892+29.9614548941181328j)
1175
+ assert lambertw(3+4j).ae(1.281561806123775878+0.533095222020971071j)
1176
+ assert lambertw(-0.4+0.4j).ae(-0.10396515323290657+0.61899273315171632j)
1177
+ assert lambertw(3+4j,1).ae(-0.11691092896595324+5.61888039871282334j)
1178
+ assert lambertw(3+4j,-1).ae(0.25856740686699742-3.85211668616143559j)
1179
+ assert lambertw(-0.5,-1).ae(-0.794023632344689368-0.770111750510379110j)
1180
+ assert lambertw(-1./10000,1).ae(-11.82350837248724344+6.80546081842002101j)
1181
+ assert lambertw(-1./10000,-1).ae(-11.6671145325663544)
1182
+ assert lambertw(-1./10000,-2).ae(-11.82350837248724344-6.80546081842002101j)
1183
+ assert lambertw(-1./100000,4).ae(-14.9186890769540539+26.1856750178782046j)
1184
+ assert lambertw(-1./100000,5).ae(-15.0931437726379218666+32.5525721210262290086j)
1185
+ assert lambertw((2+j)/10).ae(0.173704503762911669+0.071781336752835511j)
1186
+ assert lambertw((2+j)/10,1).ae(-3.21746028349820063+4.56175438896292539j)
1187
+ assert lambertw((2+j)/10,-1).ae(-3.03781405002993088-3.53946629633505737j)
1188
+ assert lambertw((2+j)/10,4).ae(-4.6878509692773249+23.8313630697683291j)
1189
+ assert lambertw(-(2+j)/10).ae(-0.226933772515757933-0.164986470020154580j)
1190
+ assert lambertw(-(2+j)/10,1).ae(-2.43569517046110001+0.76974067544756289j)
1191
+ assert lambertw(-(2+j)/10,-1).ae(-3.54858738151989450-6.91627921869943589j)
1192
+ assert lambertw(-(2+j)/10,4).ae(-4.5500846928118151+20.6672982215434637j)
1193
+ mp.dps = 50
1194
+ assert lambertw(pi).ae('1.073658194796149172092178407024821347547745350410314531')
1195
+ mp.dps = 15
1196
+ # Former bug in generated branch
1197
+ assert lambertw(-0.5+0.002j).ae(-0.78917138132659918344 + 0.76743539379990327749j)
1198
+ assert lambertw(-0.5-0.002j).ae(-0.78917138132659918344 - 0.76743539379990327749j)
1199
+ assert lambertw(-0.448+0.4j).ae(-0.11855133765652382241 + 0.66570534313583423116j)
1200
+ assert lambertw(-0.448-0.4j).ae(-0.11855133765652382241 - 0.66570534313583423116j)
1201
+ assert lambertw(-0.65475+0.0001j).ae(-0.61053421111385310898+1.0396534993944097723803j)
1202
+ # Huge branch index
1203
+ w = lambertw(1,10**20)
1204
+ assert w.real.ae(-47.889578926290259164)
1205
+ assert w.imag.ae(6.2831853071795864769e+20)
1206
+
1207
+ def test_lambertw_hard():
1208
+ def check(x,y):
1209
+ y = convert(y)
1210
+ type_ok = True
1211
+ if isinstance(y, mpf):
1212
+ type_ok = isinstance(x, mpf)
1213
+ real_ok = abs(x.real-y.real) <= abs(y.real)*8*eps
1214
+ imag_ok = abs(x.imag-y.imag) <= abs(y.imag)*8*eps
1215
+ #print x, y, abs(x.real-y.real), abs(x.imag-y.imag)
1216
+ return real_ok and imag_ok
1217
+ # Evaluation near 0
1218
+ mp.dps = 15
1219
+ assert check(lambertw(1e-10), 9.999999999000000000e-11)
1220
+ assert check(lambertw(-1e-10), -1.000000000100000000e-10)
1221
+ assert check(lambertw(1e-10j), 9.999999999999999999733e-21 + 9.99999999999999999985e-11j)
1222
+ assert check(lambertw(-1e-10j), 9.999999999999999999733e-21 - 9.99999999999999999985e-11j)
1223
+ assert check(lambertw(1e-10,1), -26.303186778379041559 + 3.265093911703828397j)
1224
+ assert check(lambertw(-1e-10,1), -26.326236166739163892 + 6.526183280686333315j)
1225
+ assert check(lambertw(1e-10j,1), -26.312931726911421551 + 4.896366881798013421j)
1226
+ assert check(lambertw(-1e-10j,1), -26.297238779529035066 + 1.632807161345576513j)
1227
+ assert check(lambertw(1e-10,-1), -26.303186778379041559 - 3.265093911703828397j)
1228
+ assert check(lambertw(-1e-10,-1), -26.295238819246925694)
1229
+ assert check(lambertw(1e-10j,-1), -26.297238779529035028 - 1.6328071613455765135j)
1230
+ assert check(lambertw(-1e-10j,-1), -26.312931726911421551 - 4.896366881798013421j)
1231
+ # Test evaluation very close to the branch point -1/e
1232
+ # on the -1, 0, and 1 branches
1233
+ add = lambda x, y: fadd(x,y,exact=True)
1234
+ sub = lambda x, y: fsub(x,y,exact=True)
1235
+ addj = lambda x, y: fadd(x,fmul(y,1j,exact=True),exact=True)
1236
+ subj = lambda x, y: fadd(x,fmul(y,-1j,exact=True),exact=True)
1237
+ mp.dps = 1500
1238
+ a = -1/e + 10*eps
1239
+ d3 = mpf('1e-3')
1240
+ d10 = mpf('1e-10')
1241
+ d20 = mpf('1e-20')
1242
+ d40 = mpf('1e-40')
1243
+ d80 = mpf('1e-80')
1244
+ d300 = mpf('1e-300')
1245
+ d1000 = mpf('1e-1000')
1246
+ mp.dps = 15
1247
+ # ---- Branch 0 ----
1248
+ # -1/e + eps
1249
+ assert check(lambertw(add(a,d3)), -0.92802015005456704876)
1250
+ assert check(lambertw(add(a,d10)), -0.99997668374140088071)
1251
+ assert check(lambertw(add(a,d20)), -0.99999999976683560186)
1252
+ assert lambertw(add(a,d40)) == -1
1253
+ assert lambertw(add(a,d80)) == -1
1254
+ assert lambertw(add(a,d300)) == -1
1255
+ assert lambertw(add(a,d1000)) == -1
1256
+ # -1/e - eps
1257
+ assert check(lambertw(sub(a,d3)), -0.99819016149860989001+0.07367191188934638577j)
1258
+ assert check(lambertw(sub(a,d10)), -0.9999999998187812114595992+0.0000233164398140346109194j)
1259
+ assert check(lambertw(sub(a,d20)), -0.99999999999999999998187+2.331643981597124203344e-10j)
1260
+ assert check(lambertw(sub(a,d40)), -1.0+2.33164398159712420336e-20j)
1261
+ assert check(lambertw(sub(a,d80)), -1.0+2.33164398159712420336e-40j)
1262
+ assert check(lambertw(sub(a,d300)), -1.0+2.33164398159712420336e-150j)
1263
+ assert check(lambertw(sub(a,d1000)), mpc(-1,'2.33164398159712420336e-500'))
1264
+ # -1/e + eps*j
1265
+ assert check(lambertw(addj(a,d3)), -0.94790387486938526634+0.05036819639190132490j)
1266
+ assert check(lambertw(addj(a,d10)), -0.9999835127872943680999899+0.0000164870314895821225256j)
1267
+ assert check(lambertw(addj(a,d20)), -0.999999999835127872929987+1.64872127051890935830e-10j)
1268
+ assert check(lambertw(addj(a,d40)), -0.9999999999999999999835+1.6487212707001281468305e-20j)
1269
+ assert check(lambertw(addj(a,d80)), -1.0 + 1.64872127070012814684865e-40j)
1270
+ assert check(lambertw(addj(a,d300)), -1.0 + 1.64872127070012814684865e-150j)
1271
+ assert check(lambertw(addj(a,d1000)), mpc(-1.0,'1.64872127070012814684865e-500'))
1272
+ # -1/e - eps*j
1273
+ assert check(lambertw(subj(a,d3)), -0.94790387486938526634-0.05036819639190132490j)
1274
+ assert check(lambertw(subj(a,d10)), -0.9999835127872943680999899-0.0000164870314895821225256j)
1275
+ assert check(lambertw(subj(a,d20)), -0.999999999835127872929987-1.64872127051890935830e-10j)
1276
+ assert check(lambertw(subj(a,d40)), -0.9999999999999999999835-1.6487212707001281468305e-20j)
1277
+ assert check(lambertw(subj(a,d80)), -1.0 - 1.64872127070012814684865e-40j)
1278
+ assert check(lambertw(subj(a,d300)), -1.0 - 1.64872127070012814684865e-150j)
1279
+ assert check(lambertw(subj(a,d1000)), mpc(-1.0,'-1.64872127070012814684865e-500'))
1280
+ # ---- Branch 1 ----
1281
+ assert check(lambertw(addj(a,d3),1), -3.088501303219933378005990 + 7.458676867597474813950098j)
1282
+ assert check(lambertw(addj(a,d80),1), -3.088843015613043855957087 + 7.461489285654254556906117j)
1283
+ assert check(lambertw(addj(a,d300),1), -3.088843015613043855957087 + 7.461489285654254556906117j)
1284
+ assert check(lambertw(addj(a,d1000),1), -3.088843015613043855957087 + 7.461489285654254556906117j)
1285
+ assert check(lambertw(subj(a,d3),1), -1.0520914180450129534365906 + 0.0539925638125450525673175j)
1286
+ assert check(lambertw(subj(a,d10),1), -1.0000164872127056318529390 + 0.000016487393927159250398333077j)
1287
+ assert check(lambertw(subj(a,d20),1), -1.0000000001648721270700128 + 1.64872127088134693542628e-10j)
1288
+ assert check(lambertw(subj(a,d40),1), -1.000000000000000000016487 + 1.64872127070012814686677e-20j)
1289
+ assert check(lambertw(subj(a,d80),1), -1.0 + 1.64872127070012814684865e-40j)
1290
+ assert check(lambertw(subj(a,d300),1), -1.0 + 1.64872127070012814684865e-150j)
1291
+ assert check(lambertw(subj(a,d1000),1), mpc(-1.0, '1.64872127070012814684865e-500'))
1292
+ # ---- Branch -1 ----
1293
+ # -1/e + eps
1294
+ assert check(lambertw(add(a,d3),-1), -1.075608941186624989414945)
1295
+ assert check(lambertw(add(a,d10),-1), -1.000023316621036696460620)
1296
+ assert check(lambertw(add(a,d20),-1), -1.000000000233164398177834)
1297
+ assert lambertw(add(a,d40),-1) == -1
1298
+ assert lambertw(add(a,d80),-1) == -1
1299
+ assert lambertw(add(a,d300),-1) == -1
1300
+ assert lambertw(add(a,d1000),-1) == -1
1301
+ # -1/e - eps
1302
+ assert check(lambertw(sub(a,d3),-1), -0.99819016149860989001-0.07367191188934638577j)
1303
+ assert check(lambertw(sub(a,d10),-1), -0.9999999998187812114595992-0.0000233164398140346109194j)
1304
+ assert check(lambertw(sub(a,d20),-1), -0.99999999999999999998187-2.331643981597124203344e-10j)
1305
+ assert check(lambertw(sub(a,d40),-1), -1.0-2.33164398159712420336e-20j)
1306
+ assert check(lambertw(sub(a,d80),-1), -1.0-2.33164398159712420336e-40j)
1307
+ assert check(lambertw(sub(a,d300),-1), -1.0-2.33164398159712420336e-150j)
1308
+ assert check(lambertw(sub(a,d1000),-1), mpc(-1,'-2.33164398159712420336e-500'))
1309
+ # -1/e + eps*j
1310
+ assert check(lambertw(addj(a,d3),-1), -1.0520914180450129534365906 - 0.0539925638125450525673175j)
1311
+ assert check(lambertw(addj(a,d10),-1), -1.0000164872127056318529390 - 0.0000164873939271592503983j)
1312
+ assert check(lambertw(addj(a,d20),-1), -1.0000000001648721270700 - 1.64872127088134693542628e-10j)
1313
+ assert check(lambertw(addj(a,d40),-1), -1.00000000000000000001648 - 1.6487212707001281468667726e-20j)
1314
+ assert check(lambertw(addj(a,d80),-1), -1.0 - 1.64872127070012814684865e-40j)
1315
+ assert check(lambertw(addj(a,d300),-1), -1.0 - 1.64872127070012814684865e-150j)
1316
+ assert check(lambertw(addj(a,d1000),-1), mpc(-1.0,'-1.64872127070012814684865e-500'))
1317
+ # -1/e - eps*j
1318
+ assert check(lambertw(subj(a,d3),-1), -3.088501303219933378005990-7.458676867597474813950098j)
1319
+ assert check(lambertw(subj(a,d10),-1), -3.088843015579260686911033-7.461489285372968780020716j)
1320
+ assert check(lambertw(subj(a,d20),-1), -3.088843015613043855953708-7.461489285654254556877988j)
1321
+ assert check(lambertw(subj(a,d40),-1), -3.088843015613043855957087-7.461489285654254556906117j)
1322
+ assert check(lambertw(subj(a,d80),-1), -3.088843015613043855957087 - 7.461489285654254556906117j)
1323
+ assert check(lambertw(subj(a,d300),-1), -3.088843015613043855957087 - 7.461489285654254556906117j)
1324
+ assert check(lambertw(subj(a,d1000),-1), -3.088843015613043855957087 - 7.461489285654254556906117j)
1325
+ # One more case, testing higher precision
1326
+ mp.dps = 500
1327
+ x = -1/e + mpf('1e-13')
1328
+ ans = "-0.99999926266961377166355784455394913638782494543377383"\
1329
+ "744978844374498153493943725364881490261187530235150668593869563"\
1330
+ "168276697689459394902153960200361935311512317183678882"
1331
+ mp.dps = 15
1332
+ assert lambertw(x).ae(ans)
1333
+ mp.dps = 50
1334
+ assert lambertw(x).ae(ans)
1335
+ mp.dps = 150
1336
+ assert lambertw(x).ae(ans)
1337
+
1338
+ def test_meijerg():
1339
+ mp.dps = 15
1340
+ assert meijerg([[2,3],[1]],[[0.5,2],[3,4]], 2.5).ae(4.2181028074787439386)
1341
+ assert meijerg([[],[1+j]],[[1],[1]], 3+4j).ae(271.46290321152464592 - 703.03330399954820169j)
1342
+ assert meijerg([[0.25],[1]],[[0.5],[2]],0) == 0
1343
+ assert meijerg([[0],[]],[[0,0,'1/3','2/3'], []], '2/27').ae(2.2019391389653314120)
1344
+ # Verify 1/z series being used
1345
+ assert meijerg([[-3],[-0.5]], [[-1],[-2.5]], -0.5).ae(-1.338096165935754898687431)
1346
+ assert meijerg([[1-(-1)],[1-(-2.5)]], [[1-(-3)],[1-(-0.5)]], -2.0).ae(-1.338096165935754898687431)
1347
+ assert meijerg([[-3],[-0.5]], [[-1],[-2.5]], -1).ae(-(pi+4)/(4*pi))
1348
+ a = 2.5
1349
+ b = 1.25
1350
+ for z in [mpf(0.25), mpf(2)]:
1351
+ x1 = hyp1f1(a,b,z)
1352
+ x2 = gamma(b)/gamma(a)*meijerg([[1-a],[]],[[0],[1-b]],-z)
1353
+ x3 = gamma(b)/gamma(a)*meijerg([[1-0],[1-(1-b)]],[[1-(1-a)],[]],-1/z)
1354
+ assert x1.ae(x2)
1355
+ assert x1.ae(x3)
1356
+
1357
+ def test_appellf1():
1358
+ mp.dps = 15
1359
+ assert appellf1(2,-2,1,1,2,3).ae(-1.75)
1360
+ assert appellf1(2,1,-2,1,2,3).ae(-8)
1361
+ assert appellf1(2,1,-2,1,0.5,0.25).ae(1.5)
1362
+ assert appellf1(-2,1,3,2,3,3).ae(19)
1363
+ assert appellf1(1,2,3,4,0.5,0.125).ae( 1.53843285792549786518)
1364
+
1365
+ def test_coulomb():
1366
+ # Note: most tests are doctests
1367
+ # Test for a bug:
1368
+ mp.dps = 15
1369
+ assert coulombg(mpc(-5,0),2,3).ae(20.087729487721430394)
1370
+
1371
+ def test_hyper_param_accuracy():
1372
+ mp.dps = 15
1373
+ As = [n+1e-10 for n in range(-5,-1)]
1374
+ Bs = [n+1e-10 for n in range(-12,-5)]
1375
+ assert hyper(As,Bs,10).ae(-381757055858.652671927)
1376
+ assert legenp(0.5, 100, 0.25).ae(-2.4124576567211311755e+144)
1377
+ assert (hyp1f1(1000,1,-100)*10**24).ae(5.2589445437370169113)
1378
+ assert (hyp2f1(10, -900, 10.5, 0.99)*10**24).ae(1.9185370579660768203)
1379
+ assert (hyp2f1(1000,1.5,-3.5,-1.5)*10**385).ae(-2.7367529051334000764)
1380
+ assert hyp2f1(-5, 10, 3, 0.5, zeroprec=500) == 0
1381
+ assert (hyp1f1(-10000, 1000, 100)*10**424).ae(-3.1046080515824859974)
1382
+ assert (hyp2f1(1000,1.5,-3.5,-0.75,maxterms=100000)*10**231).ae(-4.0534790813913998643)
1383
+ assert legenp(2, 3, 0.25) == 0
1384
+ pytest.raises(ValueError, lambda: hypercomb(lambda a: [([],[],[],[],[a],[-a],0.5)], [3]))
1385
+ assert hypercomb(lambda a: [([],[],[],[],[a],[-a],0.5)], [3], infprec=200) == inf
1386
+ assert meijerg([[],[]],[[0,0,0,0],[]],0.1).ae(1.5680822343832351418)
1387
+ assert (besselk(400,400)*10**94).ae(1.4387057277018550583)
1388
+ mp.dps = 5
1389
+ (hyp1f1(-5000.5, 1500, 100)*10**185).ae(8.5185229673381935522)
1390
+ (hyp1f1(-5000, 1500, 100)*10**185).ae(9.1501213424563944311)
1391
+ mp.dps = 15
1392
+ (hyp1f1(-5000.5, 1500, 100)*10**185).ae(8.5185229673381935522)
1393
+ (hyp1f1(-5000, 1500, 100)*10**185).ae(9.1501213424563944311)
1394
+ assert hyp0f1(fadd(-20,'1e-100',exact=True), 0.25).ae(1.85014429040102783e+49)
1395
+ assert hyp0f1((-20*10**100+1, 10**100), 0.25).ae(1.85014429040102783e+49)
1396
+
1397
+ def test_hypercomb_zero_pow():
1398
+ # check that 0^0 = 1
1399
+ assert hypercomb(lambda a: (([0],[a],[],[],[],[],0),), [0]) == 1
1400
+ assert meijerg([[-1.5],[]],[[0],[-0.75]],0).ae(1.4464090846320771425)
1401
+
1402
+ def test_spherharm():
1403
+ mp.dps = 15
1404
+ t = 0.5; r = 0.25
1405
+ assert spherharm(0,0,t,r).ae(0.28209479177387814347)
1406
+ assert spherharm(1,-1,t,r).ae(0.16048941205971996369 - 0.04097967481096344271j)
1407
+ assert spherharm(1,0,t,r).ae(0.42878904414183579379)
1408
+ assert spherharm(1,1,t,r).ae(-0.16048941205971996369 - 0.04097967481096344271j)
1409
+ assert spherharm(2,-2,t,r).ae(0.077915886919031181734 - 0.042565643022253962264j)
1410
+ assert spherharm(2,-1,t,r).ae(0.31493387233497459884 - 0.08041582001959297689j)
1411
+ assert spherharm(2,0,t,r).ae(0.41330596756220761898)
1412
+ assert spherharm(2,1,t,r).ae(-0.31493387233497459884 - 0.08041582001959297689j)
1413
+ assert spherharm(2,2,t,r).ae(0.077915886919031181734 + 0.042565643022253962264j)
1414
+ assert spherharm(3,-3,t,r).ae(0.033640236589690881646 - 0.031339125318637082197j)
1415
+ assert spherharm(3,-2,t,r).ae(0.18091018743101461963 - 0.09883168583167010241j)
1416
+ assert spherharm(3,-1,t,r).ae(0.42796713930907320351 - 0.10927795157064962317j)
1417
+ assert spherharm(3,0,t,r).ae(0.27861659336351639787)
1418
+ assert spherharm(3,1,t,r).ae(-0.42796713930907320351 - 0.10927795157064962317j)
1419
+ assert spherharm(3,2,t,r).ae(0.18091018743101461963 + 0.09883168583167010241j)
1420
+ assert spherharm(3,3,t,r).ae(-0.033640236589690881646 - 0.031339125318637082197j)
1421
+ assert spherharm(0,-1,t,r) == 0
1422
+ assert spherharm(0,-2,t,r) == 0
1423
+ assert spherharm(0,1,t,r) == 0
1424
+ assert spherharm(0,2,t,r) == 0
1425
+ assert spherharm(1,2,t,r) == 0
1426
+ assert spherharm(1,3,t,r) == 0
1427
+ assert spherharm(1,-2,t,r) == 0
1428
+ assert spherharm(1,-3,t,r) == 0
1429
+ assert spherharm(2,3,t,r) == 0
1430
+ assert spherharm(2,4,t,r) == 0
1431
+ assert spherharm(2,-3,t,r) == 0
1432
+ assert spherharm(2,-4,t,r) == 0
1433
+ assert spherharm(3,4.5,0.5,0.25).ae(-22.831053442240790148 + 10.910526059510013757j)
1434
+ assert spherharm(2+3j, 1-j, 1+j, 3+4j).ae(-2.6582752037810116935 - 1.0909214905642160211j)
1435
+ assert spherharm(-6,2.5,t,r).ae(0.39383644983851448178 + 0.28414687085358299021j)
1436
+ assert spherharm(-3.5, 3, 0.5, 0.25).ae(0.014516852987544698924 - 0.015582769591477628495j)
1437
+ assert spherharm(-3, 3, 0.5, 0.25) == 0
1438
+ assert spherharm(-6, 3, 0.5, 0.25).ae(-0.16544349818782275459 - 0.15412657723253924562j)
1439
+ assert spherharm(-6, 1.5, 0.5, 0.25).ae(0.032208193499767402477 + 0.012678000924063664921j)
1440
+ assert spherharm(3,0,0,1).ae(0.74635266518023078283)
1441
+ assert spherharm(3,-2,0,1) == 0
1442
+ assert spherharm(3,-2,1,1).ae(-0.16270707338254028971 - 0.35552144137546777097j)
1443
+
1444
+ def test_qfunctions():
1445
+ mp.dps = 15
1446
+ assert qp(2,3,100).ae('2.7291482267247332183e2391')
1447
+
1448
+ def test_issue_239():
1449
+ mp.prec = 150
1450
+ x = ldexp(2476979795053773,-52)
1451
+ assert betainc(206, 385, 0, 0.55, 1).ae('0.99999999999999999999996570910644857895771110649954')
1452
+ mp.dps = 15
1453
+ pytest.raises(ValueError, lambda: hyp2f1(-5,5,0.5,0.5))
1454
+
1455
+ # Extra stress testing for Bessel functions
1456
+ # Reference zeros generated with the aid of scipy.special
1457
+ # jn_zero, jnp_zero, yn_zero, ynp_zero
1458
+
1459
+ V = 15
1460
+ M = 15
1461
+
1462
+ jn_small_zeros = \
1463
+ [[2.4048255576957728,
1464
+ 5.5200781102863106,
1465
+ 8.6537279129110122,
1466
+ 11.791534439014282,
1467
+ 14.930917708487786,
1468
+ 18.071063967910923,
1469
+ 21.211636629879259,
1470
+ 24.352471530749303,
1471
+ 27.493479132040255,
1472
+ 30.634606468431975,
1473
+ 33.775820213573569,
1474
+ 36.917098353664044,
1475
+ 40.058425764628239,
1476
+ 43.19979171317673,
1477
+ 46.341188371661814],
1478
+ [3.8317059702075123,
1479
+ 7.0155866698156188,
1480
+ 10.173468135062722,
1481
+ 13.323691936314223,
1482
+ 16.470630050877633,
1483
+ 19.615858510468242,
1484
+ 22.760084380592772,
1485
+ 25.903672087618383,
1486
+ 29.046828534916855,
1487
+ 32.189679910974404,
1488
+ 35.332307550083865,
1489
+ 38.474766234771615,
1490
+ 41.617094212814451,
1491
+ 44.759318997652822,
1492
+ 47.901460887185447],
1493
+ [5.1356223018406826,
1494
+ 8.4172441403998649,
1495
+ 11.619841172149059,
1496
+ 14.795951782351261,
1497
+ 17.959819494987826,
1498
+ 21.116997053021846,
1499
+ 24.270112313573103,
1500
+ 27.420573549984557,
1501
+ 30.569204495516397,
1502
+ 33.7165195092227,
1503
+ 36.86285651128381,
1504
+ 40.008446733478192,
1505
+ 43.153453778371463,
1506
+ 46.297996677236919,
1507
+ 49.442164110416873],
1508
+ [6.3801618959239835,
1509
+ 9.7610231299816697,
1510
+ 13.015200721698434,
1511
+ 16.223466160318768,
1512
+ 19.409415226435012,
1513
+ 22.582729593104442,
1514
+ 25.748166699294978,
1515
+ 28.908350780921758,
1516
+ 32.064852407097709,
1517
+ 35.218670738610115,
1518
+ 38.370472434756944,
1519
+ 41.520719670406776,
1520
+ 44.669743116617253,
1521
+ 47.817785691533302,
1522
+ 50.965029906205183],
1523
+ [7.5883424345038044,
1524
+ 11.064709488501185,
1525
+ 14.37253667161759,
1526
+ 17.615966049804833,
1527
+ 20.826932956962388,
1528
+ 24.01901952477111,
1529
+ 27.199087765981251,
1530
+ 30.371007667117247,
1531
+ 33.537137711819223,
1532
+ 36.699001128744649,
1533
+ 39.857627302180889,
1534
+ 43.01373772335443,
1535
+ 46.167853512924375,
1536
+ 49.320360686390272,
1537
+ 52.471551398458023],
1538
+ [8.771483815959954,
1539
+ 12.338604197466944,
1540
+ 15.700174079711671,
1541
+ 18.980133875179921,
1542
+ 22.217799896561268,
1543
+ 25.430341154222704,
1544
+ 28.626618307291138,
1545
+ 31.811716724047763,
1546
+ 34.988781294559295,
1547
+ 38.159868561967132,
1548
+ 41.326383254047406,
1549
+ 44.489319123219673,
1550
+ 47.649399806697054,
1551
+ 50.80716520300633,
1552
+ 53.963026558378149],
1553
+ [9.9361095242176849,
1554
+ 13.589290170541217,
1555
+ 17.003819667816014,
1556
+ 20.320789213566506,
1557
+ 23.58608443558139,
1558
+ 26.820151983411405,
1559
+ 30.033722386570469,
1560
+ 33.233041762847123,
1561
+ 36.422019668258457,
1562
+ 39.603239416075404,
1563
+ 42.778481613199507,
1564
+ 45.949015998042603,
1565
+ 49.11577372476426,
1566
+ 52.279453903601052,
1567
+ 55.440592068853149],
1568
+ [11.086370019245084,
1569
+ 14.821268727013171,
1570
+ 18.287582832481726,
1571
+ 21.641541019848401,
1572
+ 24.934927887673022,
1573
+ 28.191188459483199,
1574
+ 31.42279419226558,
1575
+ 34.637089352069324,
1576
+ 37.838717382853611,
1577
+ 41.030773691585537,
1578
+ 44.21540850526126,
1579
+ 47.394165755570512,
1580
+ 50.568184679795566,
1581
+ 53.738325371963291,
1582
+ 56.905249991978781],
1583
+ [12.225092264004655,
1584
+ 16.037774190887709,
1585
+ 19.554536430997055,
1586
+ 22.94517313187462,
1587
+ 26.266814641176644,
1588
+ 29.54565967099855,
1589
+ 32.795800037341462,
1590
+ 36.025615063869571,
1591
+ 39.240447995178135,
1592
+ 42.443887743273558,
1593
+ 45.638444182199141,
1594
+ 48.825930381553857,
1595
+ 52.007691456686903,
1596
+ 55.184747939289049,
1597
+ 58.357889025269694],
1598
+ [13.354300477435331,
1599
+ 17.241220382489128,
1600
+ 20.807047789264107,
1601
+ 24.233885257750552,
1602
+ 27.583748963573006,
1603
+ 30.885378967696675,
1604
+ 34.154377923855096,
1605
+ 37.400099977156589,
1606
+ 40.628553718964528,
1607
+ 43.843801420337347,
1608
+ 47.048700737654032,
1609
+ 50.245326955305383,
1610
+ 53.435227157042058,
1611
+ 56.619580266508436,
1612
+ 59.799301630960228],
1613
+ [14.475500686554541,
1614
+ 18.433463666966583,
1615
+ 22.046985364697802,
1616
+ 25.509450554182826,
1617
+ 28.887375063530457,
1618
+ 32.211856199712731,
1619
+ 35.499909205373851,
1620
+ 38.761807017881651,
1621
+ 42.004190236671805,
1622
+ 45.231574103535045,
1623
+ 48.447151387269394,
1624
+ 51.653251668165858,
1625
+ 54.851619075963349,
1626
+ 58.043587928232478,
1627
+ 61.230197977292681],
1628
+ [15.589847884455485,
1629
+ 19.61596690396692,
1630
+ 23.275853726263409,
1631
+ 26.773322545509539,
1632
+ 30.17906117878486,
1633
+ 33.526364075588624,
1634
+ 36.833571341894905,
1635
+ 40.111823270954241,
1636
+ 43.368360947521711,
1637
+ 46.608132676274944,
1638
+ 49.834653510396724,
1639
+ 53.050498959135054,
1640
+ 56.257604715114484,
1641
+ 59.457456908388002,
1642
+ 62.651217388202912],
1643
+ [16.698249933848246,
1644
+ 20.789906360078443,
1645
+ 24.494885043881354,
1646
+ 28.026709949973129,
1647
+ 31.45996003531804,
1648
+ 34.829986990290238,
1649
+ 38.156377504681354,
1650
+ 41.451092307939681,
1651
+ 44.721943543191147,
1652
+ 47.974293531269048,
1653
+ 51.211967004101068,
1654
+ 54.437776928325074,
1655
+ 57.653844811906946,
1656
+ 60.8618046824805,
1657
+ 64.062937824850136],
1658
+ [17.801435153282442,
1659
+ 21.95624406783631,
1660
+ 25.705103053924724,
1661
+ 29.270630441874802,
1662
+ 32.731053310978403,
1663
+ 36.123657666448762,
1664
+ 39.469206825243883,
1665
+ 42.780439265447158,
1666
+ 46.06571091157561,
1667
+ 49.330780096443524,
1668
+ 52.579769064383396,
1669
+ 55.815719876305778,
1670
+ 59.040934037249271,
1671
+ 62.257189393731728,
1672
+ 65.465883797232125],
1673
+ [18.899997953174024,
1674
+ 23.115778347252756,
1675
+ 26.907368976182104,
1676
+ 30.505950163896036,
1677
+ 33.993184984781542,
1678
+ 37.408185128639695,
1679
+ 40.772827853501868,
1680
+ 44.100590565798301,
1681
+ 47.400347780543231,
1682
+ 50.678236946479898,
1683
+ 53.93866620912693,
1684
+ 57.184898598119301,
1685
+ 60.419409852130297,
1686
+ 63.644117508962281,
1687
+ 66.860533012260103]]
1688
+
1689
+ jnp_small_zeros = \
1690
+ [[0.0,
1691
+ 3.8317059702075123,
1692
+ 7.0155866698156188,
1693
+ 10.173468135062722,
1694
+ 13.323691936314223,
1695
+ 16.470630050877633,
1696
+ 19.615858510468242,
1697
+ 22.760084380592772,
1698
+ 25.903672087618383,
1699
+ 29.046828534916855,
1700
+ 32.189679910974404,
1701
+ 35.332307550083865,
1702
+ 38.474766234771615,
1703
+ 41.617094212814451,
1704
+ 44.759318997652822],
1705
+ [1.8411837813406593,
1706
+ 5.3314427735250326,
1707
+ 8.5363163663462858,
1708
+ 11.706004902592064,
1709
+ 14.863588633909033,
1710
+ 18.015527862681804,
1711
+ 21.16436985918879,
1712
+ 24.311326857210776,
1713
+ 27.457050571059246,
1714
+ 30.601922972669094,
1715
+ 33.746182898667383,
1716
+ 36.889987409236811,
1717
+ 40.033444053350675,
1718
+ 43.176628965448822,
1719
+ 46.319597561173912],
1720
+ [3.0542369282271403,
1721
+ 6.7061331941584591,
1722
+ 9.9694678230875958,
1723
+ 13.170370856016123,
1724
+ 16.347522318321783,
1725
+ 19.512912782488205,
1726
+ 22.671581772477426,
1727
+ 25.826037141785263,
1728
+ 28.977672772993679,
1729
+ 32.127327020443474,
1730
+ 35.275535050674691,
1731
+ 38.422654817555906,
1732
+ 41.568934936074314,
1733
+ 44.714553532819734,
1734
+ 47.859641607992093],
1735
+ [4.2011889412105285,
1736
+ 8.0152365983759522,
1737
+ 11.345924310743006,
1738
+ 14.585848286167028,
1739
+ 17.78874786606647,
1740
+ 20.9724769365377,
1741
+ 24.144897432909265,
1742
+ 27.310057930204349,
1743
+ 30.470268806290424,
1744
+ 33.626949182796679,
1745
+ 36.781020675464386,
1746
+ 39.933108623659488,
1747
+ 43.083652662375079,
1748
+ 46.232971081836478,
1749
+ 49.381300092370349],
1750
+ [5.3175531260839944,
1751
+ 9.2823962852416123,
1752
+ 12.681908442638891,
1753
+ 15.964107037731551,
1754
+ 19.196028800048905,
1755
+ 22.401032267689004,
1756
+ 25.589759681386733,
1757
+ 28.767836217666503,
1758
+ 31.938539340972783,
1759
+ 35.103916677346764,
1760
+ 38.265316987088158,
1761
+ 41.423666498500732,
1762
+ 44.579623137359257,
1763
+ 47.733667523865744,
1764
+ 50.886159153182682],
1765
+ [6.4156163757002403,
1766
+ 10.519860873772308,
1767
+ 13.9871886301403,
1768
+ 17.312842487884625,
1769
+ 20.575514521386888,
1770
+ 23.803581476593863,
1771
+ 27.01030789777772,
1772
+ 30.20284907898166,
1773
+ 33.385443901010121,
1774
+ 36.560777686880356,
1775
+ 39.730640230067416,
1776
+ 42.896273163494417,
1777
+ 46.058566273567043,
1778
+ 49.218174614666636,
1779
+ 52.375591529563596],
1780
+ [7.501266144684147,
1781
+ 11.734935953042708,
1782
+ 15.268181461097873,
1783
+ 18.637443009666202,
1784
+ 21.931715017802236,
1785
+ 25.183925599499626,
1786
+ 28.409776362510085,
1787
+ 31.617875716105035,
1788
+ 34.81339298429743,
1789
+ 37.999640897715301,
1790
+ 41.178849474321413,
1791
+ 44.352579199070217,
1792
+ 47.521956905768113,
1793
+ 50.687817781723741,
1794
+ 53.85079463676896],
1795
+ [8.5778364897140741,
1796
+ 12.932386237089576,
1797
+ 16.529365884366944,
1798
+ 19.941853366527342,
1799
+ 23.268052926457571,
1800
+ 26.545032061823576,
1801
+ 29.790748583196614,
1802
+ 33.015178641375142,
1803
+ 36.224380548787162,
1804
+ 39.422274578939259,
1805
+ 42.611522172286684,
1806
+ 45.793999658055002,
1807
+ 48.971070951900596,
1808
+ 52.143752969301988,
1809
+ 55.312820330403446],
1810
+ [9.6474216519972168,
1811
+ 14.115518907894618,
1812
+ 17.774012366915256,
1813
+ 21.229062622853124,
1814
+ 24.587197486317681,
1815
+ 27.889269427955092,
1816
+ 31.155326556188325,
1817
+ 34.39662855427218,
1818
+ 37.620078044197086,
1819
+ 40.830178681822041,
1820
+ 44.030010337966153,
1821
+ 47.221758471887113,
1822
+ 50.407020967034367,
1823
+ 53.586995435398319,
1824
+ 56.762598475105272],
1825
+ [10.711433970699945,
1826
+ 15.28673766733295,
1827
+ 19.004593537946053,
1828
+ 22.501398726777283,
1829
+ 25.891277276839136,
1830
+ 29.218563499936081,
1831
+ 32.505247352375523,
1832
+ 35.763792928808799,
1833
+ 39.001902811514218,
1834
+ 42.224638430753279,
1835
+ 45.435483097475542,
1836
+ 48.636922645305525,
1837
+ 51.830783925834728,
1838
+ 55.01844255063594,
1839
+ 58.200955824859509],
1840
+ [11.770876674955582,
1841
+ 16.447852748486498,
1842
+ 20.223031412681701,
1843
+ 23.760715860327448,
1844
+ 27.182021527190532,
1845
+ 30.534504754007074,
1846
+ 33.841965775135715,
1847
+ 37.118000423665604,
1848
+ 40.371068905333891,
1849
+ 43.606764901379516,
1850
+ 46.828959446564562,
1851
+ 50.040428970943456,
1852
+ 53.243223214220535,
1853
+ 56.438892058982552,
1854
+ 59.628631306921512],
1855
+ [12.826491228033465,
1856
+ 17.600266557468326,
1857
+ 21.430854238060294,
1858
+ 25.008518704644261,
1859
+ 28.460857279654847,
1860
+ 31.838424458616998,
1861
+ 35.166714427392629,
1862
+ 38.460388720328256,
1863
+ 41.728625562624312,
1864
+ 44.977526250903469,
1865
+ 48.211333836373288,
1866
+ 51.433105171422278,
1867
+ 54.645106240447105,
1868
+ 57.849056857839799,
1869
+ 61.046288512821078],
1870
+ [13.878843069697276,
1871
+ 18.745090916814406,
1872
+ 22.629300302835503,
1873
+ 26.246047773946584,
1874
+ 29.72897816891134,
1875
+ 33.131449953571661,
1876
+ 36.480548302231658,
1877
+ 39.791940718940855,
1878
+ 43.075486800191012,
1879
+ 46.337772104541405,
1880
+ 49.583396417633095,
1881
+ 52.815686826850452,
1882
+ 56.037118687012179,
1883
+ 59.249577075517968,
1884
+ 62.454525995970462],
1885
+ [14.928374492964716,
1886
+ 19.88322436109951,
1887
+ 23.81938909003628,
1888
+ 27.474339750968247,
1889
+ 30.987394331665278,
1890
+ 34.414545662167183,
1891
+ 37.784378506209499,
1892
+ 41.113512376883377,
1893
+ 44.412454519229281,
1894
+ 47.688252845993366,
1895
+ 50.945849245830813,
1896
+ 54.188831071035124,
1897
+ 57.419876154678179,
1898
+ 60.641030026538746,
1899
+ 63.853885828967512],
1900
+ [15.975438807484321,
1901
+ 21.015404934568315,
1902
+ 25.001971500138194,
1903
+ 28.694271223110755,
1904
+ 32.236969407878118,
1905
+ 35.688544091185301,
1906
+ 39.078998185245057,
1907
+ 42.425854432866141,
1908
+ 45.740236776624833,
1909
+ 49.029635055514276,
1910
+ 52.299319390331728,
1911
+ 55.553127779547459,
1912
+ 58.793933759028134,
1913
+ 62.02393848337554,
1914
+ 65.244860767043859]]
1915
+
1916
+ yn_small_zeros = \
1917
+ [[0.89357696627916752,
1918
+ 3.9576784193148579,
1919
+ 7.0860510603017727,
1920
+ 10.222345043496417,
1921
+ 13.361097473872763,
1922
+ 16.500922441528091,
1923
+ 19.64130970088794,
1924
+ 22.782028047291559,
1925
+ 25.922957653180923,
1926
+ 29.064030252728398,
1927
+ 32.205204116493281,
1928
+ 35.346452305214321,
1929
+ 38.487756653081537,
1930
+ 41.629104466213808,
1931
+ 44.770486607221993],
1932
+ [2.197141326031017,
1933
+ 5.4296810407941351,
1934
+ 8.5960058683311689,
1935
+ 11.749154830839881,
1936
+ 14.897442128336725,
1937
+ 18.043402276727856,
1938
+ 21.188068934142213,
1939
+ 24.331942571356912,
1940
+ 27.475294980449224,
1941
+ 30.618286491641115,
1942
+ 33.761017796109326,
1943
+ 36.90355531614295,
1944
+ 40.045944640266876,
1945
+ 43.188218097393211,
1946
+ 46.330399250701687],
1947
+ [3.3842417671495935,
1948
+ 6.7938075132682675,
1949
+ 10.023477979360038,
1950
+ 13.209986710206416,
1951
+ 16.378966558947457,
1952
+ 19.539039990286384,
1953
+ 22.69395593890929,
1954
+ 25.845613720902269,
1955
+ 28.995080395650151,
1956
+ 32.143002257627551,
1957
+ 35.289793869635804,
1958
+ 38.435733485446343,
1959
+ 41.581014867297885,
1960
+ 44.725777117640461,
1961
+ 47.870122696676504],
1962
+ [4.5270246611496439,
1963
+ 8.0975537628604907,
1964
+ 11.396466739595867,
1965
+ 14.623077742393873,
1966
+ 17.81845523294552,
1967
+ 20.997284754187761,
1968
+ 24.166235758581828,
1969
+ 27.328799850405162,
1970
+ 30.486989604098659,
1971
+ 33.642049384702463,
1972
+ 36.794791029185579,
1973
+ 39.945767226378749,
1974
+ 43.095367507846703,
1975
+ 46.2438744334407,
1976
+ 49.391498015725107],
1977
+ [5.6451478942208959,
1978
+ 9.3616206152445429,
1979
+ 12.730144474090465,
1980
+ 15.999627085382479,
1981
+ 19.22442895931681,
1982
+ 22.424810599698521,
1983
+ 25.610267054939328,
1984
+ 28.785893657666548,
1985
+ 31.954686680031668,
1986
+ 35.118529525584828,
1987
+ 38.278668089521758,
1988
+ 41.435960629910073,
1989
+ 44.591018225353424,
1990
+ 47.744288086361052,
1991
+ 50.896105199722123],
1992
+ [6.7471838248710219,
1993
+ 10.597176726782031,
1994
+ 14.033804104911233,
1995
+ 17.347086393228382,
1996
+ 20.602899017175335,
1997
+ 23.826536030287532,
1998
+ 27.030134937138834,
1999
+ 30.220335654231385,
2000
+ 33.401105611047908,
2001
+ 36.574972486670962,
2002
+ 39.743627733020277,
2003
+ 42.908248189569535,
2004
+ 46.069679073215439,
2005
+ 49.228543693445843,
2006
+ 52.385312123112282],
2007
+ [7.8377378223268716,
2008
+ 11.811037107609447,
2009
+ 15.313615118517857,
2010
+ 18.670704965906724,
2011
+ 21.958290897126571,
2012
+ 25.206207715021249,
2013
+ 28.429037095235496,
2014
+ 31.634879502950644,
2015
+ 34.828638524084437,
2016
+ 38.013473399691765,
2017
+ 41.19151880917741,
2018
+ 44.364272633271975,
2019
+ 47.53281875312084,
2020
+ 50.697961822183806,
2021
+ 53.860312300118388],
2022
+ [8.919605734873789,
2023
+ 13.007711435388313,
2024
+ 16.573915129085334,
2025
+ 19.974342312352426,
2026
+ 23.293972585596648,
2027
+ 26.5667563757203,
2028
+ 29.809531451608321,
2029
+ 33.031769327150685,
2030
+ 36.239265816598239,
2031
+ 39.435790312675323,
2032
+ 42.623910919472727,
2033
+ 45.805442883111651,
2034
+ 48.981708325514764,
2035
+ 52.153694518185572,
2036
+ 55.322154420959698],
2037
+ [9.9946283820824834,
2038
+ 14.190361295800141,
2039
+ 17.817887841179873,
2040
+ 21.26093227125945,
2041
+ 24.612576377421522,
2042
+ 27.910524883974868,
2043
+ 31.173701563441602,
2044
+ 34.412862242025045,
2045
+ 37.634648706110989,
2046
+ 40.843415321050884,
2047
+ 44.04214994542435,
2048
+ 47.232978012841169,
2049
+ 50.417456447370186,
2050
+ 53.596753874948731,
2051
+ 56.771765754432457],
2052
+ [11.064090256031013,
2053
+ 15.361301343575925,
2054
+ 19.047949646361388,
2055
+ 22.532765416313869,
2056
+ 25.91620496332662,
2057
+ 29.2394205079349,
2058
+ 32.523270869465881,
2059
+ 35.779715464475261,
2060
+ 39.016196664616095,
2061
+ 42.237627509803703,
2062
+ 45.4474001519274,
2063
+ 48.647941127433196,
2064
+ 51.841036928216499,
2065
+ 55.028034667184916,
2066
+ 58.209970905250097],
2067
+ [12.128927704415439,
2068
+ 16.522284394784426,
2069
+ 20.265984501212254,
2070
+ 23.791669719454272,
2071
+ 27.206568881574774,
2072
+ 30.555020011020762,
2073
+ 33.859683872746356,
2074
+ 37.133649760307504,
2075
+ 40.385117593813002,
2076
+ 43.619533085646856,
2077
+ 46.840676630553575,
2078
+ 50.051265851897857,
2079
+ 53.253310556711732,
2080
+ 56.448332488918971,
2081
+ 59.637507005589829],
2082
+ [13.189846995683845,
2083
+ 17.674674253171487,
2084
+ 21.473493977824902,
2085
+ 25.03913093040942,
2086
+ 28.485081336558058,
2087
+ 31.858644293774859,
2088
+ 35.184165245422787,
2089
+ 38.475796636190897,
2090
+ 41.742455848758449,
2091
+ 44.990096293791186,
2092
+ 48.222870660068338,
2093
+ 51.443777308699826,
2094
+ 54.655042589416311,
2095
+ 57.858358441436511,
2096
+ 61.055036135780528],
2097
+ [14.247395665073945,
2098
+ 18.819555894710682,
2099
+ 22.671697117872794,
2100
+ 26.276375544903892,
2101
+ 29.752925495549038,
2102
+ 33.151412708998983,
2103
+ 36.497763772987645,
2104
+ 39.807134090704376,
2105
+ 43.089121522203808,
2106
+ 46.350163579538652,
2107
+ 49.594769786270069,
2108
+ 52.82620892320143,
2109
+ 56.046916910756961,
2110
+ 59.258751140598783,
2111
+ 62.463155567737854],
2112
+ [15.30200785858925,
2113
+ 19.957808654258601,
2114
+ 23.861599172945054,
2115
+ 27.504429642227545,
2116
+ 31.011103429019229,
2117
+ 34.434283425782942,
2118
+ 37.801385632318459,
2119
+ 41.128514139788358,
2120
+ 44.425913324440663,
2121
+ 47.700482714581842,
2122
+ 50.957073905278458,
2123
+ 54.199216028087261,
2124
+ 57.429547607017405,
2125
+ 60.65008661807661,
2126
+ 63.862406280068586],
2127
+ [16.354034360047551,
2128
+ 21.090156519983806,
2129
+ 25.044040298785627,
2130
+ 28.724161640881914,
2131
+ 32.260472459522644,
2132
+ 35.708083982611664,
2133
+ 39.095820003878235,
2134
+ 42.440684315990936,
2135
+ 45.75353669045622,
2136
+ 49.041718113283529,
2137
+ 52.310408280968073,
2138
+ 55.56338698149062,
2139
+ 58.803488508906895,
2140
+ 62.032886550960831,
2141
+ 65.253280088312461]]
2142
+
2143
+ ynp_small_zeros = \
2144
+ [[2.197141326031017,
2145
+ 5.4296810407941351,
2146
+ 8.5960058683311689,
2147
+ 11.749154830839881,
2148
+ 14.897442128336725,
2149
+ 18.043402276727856,
2150
+ 21.188068934142213,
2151
+ 24.331942571356912,
2152
+ 27.475294980449224,
2153
+ 30.618286491641115,
2154
+ 33.761017796109326,
2155
+ 36.90355531614295,
2156
+ 40.045944640266876,
2157
+ 43.188218097393211,
2158
+ 46.330399250701687],
2159
+ [3.6830228565851777,
2160
+ 6.9414999536541757,
2161
+ 10.123404655436613,
2162
+ 13.285758156782854,
2163
+ 16.440058007293282,
2164
+ 19.590241756629495,
2165
+ 22.738034717396327,
2166
+ 25.884314618788867,
2167
+ 29.029575819372535,
2168
+ 32.174118233366201,
2169
+ 35.318134458192094,
2170
+ 38.461753870997549,
2171
+ 41.605066618873108,
2172
+ 44.74813744908079,
2173
+ 47.891014070791065],
2174
+ [5.0025829314460639,
2175
+ 8.3507247014130795,
2176
+ 11.574195465217647,
2177
+ 14.760909306207676,
2178
+ 17.931285939466855,
2179
+ 21.092894504412739,
2180
+ 24.249231678519058,
2181
+ 27.402145837145258,
2182
+ 30.552708880564553,
2183
+ 33.70158627151572,
2184
+ 36.849213419846257,
2185
+ 39.995887376143356,
2186
+ 43.141817835750686,
2187
+ 46.287157097544201,
2188
+ 49.432018469138281],
2189
+ [6.2536332084598136,
2190
+ 9.6987879841487711,
2191
+ 12.972409052292216,
2192
+ 16.19044719506921,
2193
+ 19.38238844973613,
2194
+ 22.559791857764261,
2195
+ 25.728213194724094,
2196
+ 28.890678419054777,
2197
+ 32.048984005266337,
2198
+ 35.204266606440635,
2199
+ 38.357281675961019,
2200
+ 41.508551443818436,
2201
+ 44.658448731963676,
2202
+ 47.807246956681162,
2203
+ 50.95515126455207],
2204
+ [7.4649217367571329,
2205
+ 11.005169149809189,
2206
+ 14.3317235192331,
2207
+ 17.58443601710272,
2208
+ 20.801062338411128,
2209
+ 23.997004122902644,
2210
+ 27.179886689853435,
2211
+ 30.353960608554323,
2212
+ 33.521797098666792,
2213
+ 36.685048382072301,
2214
+ 39.844826969405863,
2215
+ 43.001910515625288,
2216
+ 46.15685955107263,
2217
+ 49.310088614282257,
2218
+ 52.461911043685864],
2219
+ [8.6495562436971983,
2220
+ 12.280868725807848,
2221
+ 15.660799304540377,
2222
+ 18.949739756016503,
2223
+ 22.192841809428241,
2224
+ 25.409072788867674,
2225
+ 28.608039283077593,
2226
+ 31.795195353138159,
2227
+ 34.973890634255288,
2228
+ 38.14630522169358,
2229
+ 41.313923188794905,
2230
+ 44.477791768537617,
2231
+ 47.638672065035628,
2232
+ 50.797131066967842,
2233
+ 53.953600129601663],
2234
+ [9.8147970120105779,
2235
+ 13.532811875789828,
2236
+ 16.965526446046053,
2237
+ 20.291285512443867,
2238
+ 23.56186260680065,
2239
+ 26.799499736027237,
2240
+ 30.015665481543419,
2241
+ 33.216968050039509,
2242
+ 36.407516858984748,
2243
+ 39.590015243560459,
2244
+ 42.766320595957378,
2245
+ 45.937754257017323,
2246
+ 49.105283450953203,
2247
+ 52.269633324547373,
2248
+ 55.431358715604255],
2249
+ [10.965152105242974,
2250
+ 14.765687379508912,
2251
+ 18.250123150217555,
2252
+ 21.612750053384621,
2253
+ 24.911310600813573,
2254
+ 28.171051927637585,
2255
+ 31.40518108895689,
2256
+ 34.621401012564177,
2257
+ 37.824552065973114,
2258
+ 41.017847386464902,
2259
+ 44.203512240871601,
2260
+ 47.3831408366063,
2261
+ 50.557907466622796,
2262
+ 53.728697478957026,
2263
+ 56.896191727313342],
2264
+ [12.103641941939539,
2265
+ 15.982840905145284,
2266
+ 19.517731005559611,
2267
+ 22.916962141504605,
2268
+ 26.243700855690533,
2269
+ 29.525960140695407,
2270
+ 32.778568197561124,
2271
+ 36.010261572392516,
2272
+ 39.226578757802172,
2273
+ 42.43122493258747,
2274
+ 45.626783824134354,
2275
+ 48.815117837929515,
2276
+ 51.997606404328863,
2277
+ 55.175294723956816,
2278
+ 58.348990221754937],
2279
+ [13.232403808592215,
2280
+ 17.186756572616758,
2281
+ 20.770762917490496,
2282
+ 24.206152448722253,
2283
+ 27.561059462697153,
2284
+ 30.866053571250639,
2285
+ 34.137476603379774,
2286
+ 37.385039772270268,
2287
+ 40.614946085165892,
2288
+ 43.831373184731238,
2289
+ 47.037251786726299,
2290
+ 50.234705848765229,
2291
+ 53.425316228549359,
2292
+ 56.610286079882087,
2293
+ 59.790548623216652],
2294
+ [14.35301374369987,
2295
+ 18.379337301642568,
2296
+ 22.011118775283494,
2297
+ 25.482116178696707,
2298
+ 28.865046588695164,
2299
+ 32.192853922166294,
2300
+ 35.483296655830277,
2301
+ 38.747005493021857,
2302
+ 41.990815194320955,
2303
+ 45.219355876831731,
2304
+ 48.435892856078888,
2305
+ 51.642803925173029,
2306
+ 54.84186659475857,
2307
+ 58.034439083840155,
2308
+ 61.221578745109862],
2309
+ [15.466672066554263,
2310
+ 19.562077985759503,
2311
+ 23.240325531101082,
2312
+ 26.746322986645901,
2313
+ 30.157042415639891,
2314
+ 33.507642948240263,
2315
+ 36.817212798512775,
2316
+ 40.097251300178642,
2317
+ 43.355193847719752,
2318
+ 46.596103410173672,
2319
+ 49.823567279972794,
2320
+ 53.040208868780832,
2321
+ 56.247996968470062,
2322
+ 59.448441365714251,
2323
+ 62.642721301357187],
2324
+ [16.574317035530872,
2325
+ 20.73617763753932,
2326
+ 24.459631728238804,
2327
+ 27.999993668839644,
2328
+ 31.438208790267783,
2329
+ 34.811512070805535,
2330
+ 38.140243708611251,
2331
+ 41.436725143893739,
2332
+ 44.708963264433333,
2333
+ 47.962435051891027,
2334
+ 51.201037321915983,
2335
+ 54.427630745992975,
2336
+ 57.644369734615238,
2337
+ 60.852911791989989,
2338
+ 64.054555435720397],
2339
+ [17.676697936439624,
2340
+ 21.9026148697762,
2341
+ 25.670073356263225,
2342
+ 29.244155124266438,
2343
+ 32.709534477396028,
2344
+ 36.105399554497548,
2345
+ 39.453272918267025,
2346
+ 42.766255701958017,
2347
+ 46.052899215578358,
2348
+ 49.319076602061401,
2349
+ 52.568982147952547,
2350
+ 55.805705507386287,
2351
+ 59.031580956740466,
2352
+ 62.248409689597653,
2353
+ 65.457606670836759],
2354
+ [18.774423978290318,
2355
+ 23.06220035979272,
2356
+ 26.872520985976736,
2357
+ 30.479680663499762,
2358
+ 33.971869047372436,
2359
+ 37.390118854896324,
2360
+ 40.757072537673599,
2361
+ 44.086572292170345,
2362
+ 47.387688809191869,
2363
+ 50.66667461073936,
2364
+ 53.928009929563275,
2365
+ 57.175005343085052,
2366
+ 60.410169281219877,
2367
+ 63.635442539153021,
2368
+ 66.85235358587768]]
2369
+
2370
+ @pytest.mark.slow
2371
+ def test_bessel_zeros_extra():
2372
+ mp.dps = 15
2373
+ for v in range(V):
2374
+ for m in range(1,M+1):
2375
+ print(v, m, "of", V, M)
2376
+ # Twice to test cache (if used)
2377
+ assert besseljzero(v,m).ae(jn_small_zeros[v][m-1])
2378
+ assert besseljzero(v,m).ae(jn_small_zeros[v][m-1])
2379
+ assert besseljzero(v,m,1).ae(jnp_small_zeros[v][m-1])
2380
+ assert besseljzero(v,m,1).ae(jnp_small_zeros[v][m-1])
2381
+ assert besselyzero(v,m).ae(yn_small_zeros[v][m-1])
2382
+ assert besselyzero(v,m).ae(yn_small_zeros[v][m-1])
2383
+ assert besselyzero(v,m,1).ae(ynp_small_zeros[v][m-1])
2384
+ assert besselyzero(v,m,1).ae(ynp_small_zeros[v][m-1])
llmeval-env/lib/python3.10/site-packages/mpmath/tests/test_gammazeta.py ADDED
@@ -0,0 +1,698 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from mpmath import *
2
+ from mpmath.libmp import round_up, from_float, mpf_zeta_int
3
+
4
+ def test_zeta_int_bug():
5
+ assert mpf_zeta_int(0, 10) == from_float(-0.5)
6
+
7
+ def test_bernoulli():
8
+ assert bernfrac(0) == (1,1)
9
+ assert bernfrac(1) == (-1,2)
10
+ assert bernfrac(2) == (1,6)
11
+ assert bernfrac(3) == (0,1)
12
+ assert bernfrac(4) == (-1,30)
13
+ assert bernfrac(5) == (0,1)
14
+ assert bernfrac(6) == (1,42)
15
+ assert bernfrac(8) == (-1,30)
16
+ assert bernfrac(10) == (5,66)
17
+ assert bernfrac(12) == (-691,2730)
18
+ assert bernfrac(18) == (43867,798)
19
+ p, q = bernfrac(228)
20
+ assert p % 10**10 == 164918161
21
+ assert q == 625170
22
+ p, q = bernfrac(1000)
23
+ assert p % 10**10 == 7950421099
24
+ assert q == 342999030
25
+ mp.dps = 15
26
+ assert bernoulli(0) == 1
27
+ assert bernoulli(1) == -0.5
28
+ assert bernoulli(2).ae(1./6)
29
+ assert bernoulli(3) == 0
30
+ assert bernoulli(4).ae(-1./30)
31
+ assert bernoulli(5) == 0
32
+ assert bernoulli(6).ae(1./42)
33
+ assert str(bernoulli(10)) == '0.0757575757575758'
34
+ assert str(bernoulli(234)) == '7.62772793964344e+267'
35
+ assert str(bernoulli(10**5)) == '-5.82229431461335e+376755'
36
+ assert str(bernoulli(10**8+2)) == '1.19570355039953e+676752584'
37
+
38
+ mp.dps = 50
39
+ assert str(bernoulli(10)) == '0.075757575757575757575757575757575757575757575757576'
40
+ assert str(bernoulli(234)) == '7.6277279396434392486994969020496121553385863373331e+267'
41
+ assert str(bernoulli(10**5)) == '-5.8222943146133508236497045360612887555320691004308e+376755'
42
+ assert str(bernoulli(10**8+2)) == '1.1957035503995297272263047884604346914602088317782e+676752584'
43
+
44
+ mp.dps = 1000
45
+ assert bernoulli(10).ae(mpf(5)/66)
46
+
47
+ mp.dps = 50000
48
+ assert bernoulli(10).ae(mpf(5)/66)
49
+
50
+ mp.dps = 15
51
+
52
+ def test_bernpoly_eulerpoly():
53
+ mp.dps = 15
54
+ assert bernpoly(0,-1).ae(1)
55
+ assert bernpoly(0,0).ae(1)
56
+ assert bernpoly(0,'1/2').ae(1)
57
+ assert bernpoly(0,'3/4').ae(1)
58
+ assert bernpoly(0,1).ae(1)
59
+ assert bernpoly(0,2).ae(1)
60
+ assert bernpoly(1,-1).ae('-3/2')
61
+ assert bernpoly(1,0).ae('-1/2')
62
+ assert bernpoly(1,'1/2').ae(0)
63
+ assert bernpoly(1,'3/4').ae('1/4')
64
+ assert bernpoly(1,1).ae('1/2')
65
+ assert bernpoly(1,2).ae('3/2')
66
+ assert bernpoly(2,-1).ae('13/6')
67
+ assert bernpoly(2,0).ae('1/6')
68
+ assert bernpoly(2,'1/2').ae('-1/12')
69
+ assert bernpoly(2,'3/4').ae('-1/48')
70
+ assert bernpoly(2,1).ae('1/6')
71
+ assert bernpoly(2,2).ae('13/6')
72
+ assert bernpoly(3,-1).ae(-3)
73
+ assert bernpoly(3,0).ae(0)
74
+ assert bernpoly(3,'1/2').ae(0)
75
+ assert bernpoly(3,'3/4').ae('-3/64')
76
+ assert bernpoly(3,1).ae(0)
77
+ assert bernpoly(3,2).ae(3)
78
+ assert bernpoly(4,-1).ae('119/30')
79
+ assert bernpoly(4,0).ae('-1/30')
80
+ assert bernpoly(4,'1/2').ae('7/240')
81
+ assert bernpoly(4,'3/4').ae('7/3840')
82
+ assert bernpoly(4,1).ae('-1/30')
83
+ assert bernpoly(4,2).ae('119/30')
84
+ assert bernpoly(5,-1).ae(-5)
85
+ assert bernpoly(5,0).ae(0)
86
+ assert bernpoly(5,'1/2').ae(0)
87
+ assert bernpoly(5,'3/4').ae('25/1024')
88
+ assert bernpoly(5,1).ae(0)
89
+ assert bernpoly(5,2).ae(5)
90
+ assert bernpoly(10,-1).ae('665/66')
91
+ assert bernpoly(10,0).ae('5/66')
92
+ assert bernpoly(10,'1/2').ae('-2555/33792')
93
+ assert bernpoly(10,'3/4').ae('-2555/34603008')
94
+ assert bernpoly(10,1).ae('5/66')
95
+ assert bernpoly(10,2).ae('665/66')
96
+ assert bernpoly(11,-1).ae(-11)
97
+ assert bernpoly(11,0).ae(0)
98
+ assert bernpoly(11,'1/2').ae(0)
99
+ assert bernpoly(11,'3/4').ae('-555731/4194304')
100
+ assert bernpoly(11,1).ae(0)
101
+ assert bernpoly(11,2).ae(11)
102
+ assert eulerpoly(0,-1).ae(1)
103
+ assert eulerpoly(0,0).ae(1)
104
+ assert eulerpoly(0,'1/2').ae(1)
105
+ assert eulerpoly(0,'3/4').ae(1)
106
+ assert eulerpoly(0,1).ae(1)
107
+ assert eulerpoly(0,2).ae(1)
108
+ assert eulerpoly(1,-1).ae('-3/2')
109
+ assert eulerpoly(1,0).ae('-1/2')
110
+ assert eulerpoly(1,'1/2').ae(0)
111
+ assert eulerpoly(1,'3/4').ae('1/4')
112
+ assert eulerpoly(1,1).ae('1/2')
113
+ assert eulerpoly(1,2).ae('3/2')
114
+ assert eulerpoly(2,-1).ae(2)
115
+ assert eulerpoly(2,0).ae(0)
116
+ assert eulerpoly(2,'1/2').ae('-1/4')
117
+ assert eulerpoly(2,'3/4').ae('-3/16')
118
+ assert eulerpoly(2,1).ae(0)
119
+ assert eulerpoly(2,2).ae(2)
120
+ assert eulerpoly(3,-1).ae('-9/4')
121
+ assert eulerpoly(3,0).ae('1/4')
122
+ assert eulerpoly(3,'1/2').ae(0)
123
+ assert eulerpoly(3,'3/4').ae('-11/64')
124
+ assert eulerpoly(3,1).ae('-1/4')
125
+ assert eulerpoly(3,2).ae('9/4')
126
+ assert eulerpoly(4,-1).ae(2)
127
+ assert eulerpoly(4,0).ae(0)
128
+ assert eulerpoly(4,'1/2').ae('5/16')
129
+ assert eulerpoly(4,'3/4').ae('57/256')
130
+ assert eulerpoly(4,1).ae(0)
131
+ assert eulerpoly(4,2).ae(2)
132
+ assert eulerpoly(5,-1).ae('-3/2')
133
+ assert eulerpoly(5,0).ae('-1/2')
134
+ assert eulerpoly(5,'1/2').ae(0)
135
+ assert eulerpoly(5,'3/4').ae('361/1024')
136
+ assert eulerpoly(5,1).ae('1/2')
137
+ assert eulerpoly(5,2).ae('3/2')
138
+ assert eulerpoly(10,-1).ae(2)
139
+ assert eulerpoly(10,0).ae(0)
140
+ assert eulerpoly(10,'1/2').ae('-50521/1024')
141
+ assert eulerpoly(10,'3/4').ae('-36581523/1048576')
142
+ assert eulerpoly(10,1).ae(0)
143
+ assert eulerpoly(10,2).ae(2)
144
+ assert eulerpoly(11,-1).ae('-699/4')
145
+ assert eulerpoly(11,0).ae('691/4')
146
+ assert eulerpoly(11,'1/2').ae(0)
147
+ assert eulerpoly(11,'3/4').ae('-512343611/4194304')
148
+ assert eulerpoly(11,1).ae('-691/4')
149
+ assert eulerpoly(11,2).ae('699/4')
150
+ # Potential accuracy issues
151
+ assert bernpoly(10000,10000).ae('5.8196915936323387117e+39999')
152
+ assert bernpoly(200,17.5).ae(3.8048418524583064909e244)
153
+ assert eulerpoly(200,17.5).ae(-3.7309911582655785929e275)
154
+
155
+ def test_gamma():
156
+ mp.dps = 15
157
+ assert gamma(0.25).ae(3.6256099082219083119)
158
+ assert gamma(0.0001).ae(9999.4228832316241908)
159
+ assert gamma(300).ae('1.0201917073881354535e612')
160
+ assert gamma(-0.5).ae(-3.5449077018110320546)
161
+ assert gamma(-7.43).ae(0.00026524416464197007186)
162
+ #assert gamma(Rational(1,2)) == gamma(0.5)
163
+ #assert gamma(Rational(-7,3)).ae(gamma(mpf(-7)/3))
164
+ assert gamma(1+1j).ae(0.49801566811835604271 - 0.15494982830181068512j)
165
+ assert gamma(-1+0.01j).ae(-0.422733904013474115 + 99.985883082635367436j)
166
+ assert gamma(20+30j).ae(-1453876687.5534810 + 1163777777.8031573j)
167
+ # Should always give exact factorials when they can
168
+ # be represented as mpfs under the current working precision
169
+ fact = 1
170
+ for i in range(1, 18):
171
+ assert gamma(i) == fact
172
+ fact *= i
173
+ for dps in [170, 600]:
174
+ fact = 1
175
+ mp.dps = dps
176
+ for i in range(1, 105):
177
+ assert gamma(i) == fact
178
+ fact *= i
179
+ mp.dps = 100
180
+ assert gamma(0.5).ae(sqrt(pi))
181
+ mp.dps = 15
182
+ assert factorial(0) == fac(0) == 1
183
+ assert factorial(3) == 6
184
+ assert isnan(gamma(nan))
185
+ assert gamma(1100).ae('4.8579168073569433667e2866')
186
+ assert rgamma(0) == 0
187
+ assert rgamma(-1) == 0
188
+ assert rgamma(2) == 1.0
189
+ assert rgamma(3) == 0.5
190
+ assert loggamma(2+8j).ae(-8.5205176753667636926 + 10.8569497125597429366j)
191
+ assert loggamma('1e10000').ae('2.302485092994045684017991e10004')
192
+ assert loggamma('1e10000j').ae(mpc('-1.570796326794896619231322e10000','2.302485092994045684017991e10004'))
193
+
194
+ def test_fac2():
195
+ mp.dps = 15
196
+ assert [fac2(n) for n in range(10)] == [1,1,2,3,8,15,48,105,384,945]
197
+ assert fac2(-5).ae(1./3)
198
+ assert fac2(-11).ae(-1./945)
199
+ assert fac2(50).ae(5.20469842636666623e32)
200
+ assert fac2(0.5+0.75j).ae(0.81546769394688069176-0.34901016085573266889j)
201
+ assert fac2(inf) == inf
202
+ assert isnan(fac2(-inf))
203
+
204
+ def test_gamma_quotients():
205
+ mp.dps = 15
206
+ h = 1e-8
207
+ ep = 1e-4
208
+ G = gamma
209
+ assert gammaprod([-1],[-3,-4]) == 0
210
+ assert gammaprod([-1,0],[-5]) == inf
211
+ assert abs(gammaprod([-1],[-2]) - G(-1+h)/G(-2+h)) < 1e-4
212
+ assert abs(gammaprod([-4,-3],[-2,0]) - G(-4+h)*G(-3+h)/G(-2+h)/G(0+h)) < 1e-4
213
+ assert rf(3,0) == 1
214
+ assert rf(2.5,1) == 2.5
215
+ assert rf(-5,2) == 20
216
+ assert rf(j,j).ae(gamma(2*j)/gamma(j))
217
+ assert rf('-255.5815971722918','-0.5119253100282322').ae('-0.1952720278805729485') # issue 421
218
+ assert ff(-2,0) == 1
219
+ assert ff(-2,1) == -2
220
+ assert ff(4,3) == 24
221
+ assert ff(3,4) == 0
222
+ assert binomial(0,0) == 1
223
+ assert binomial(1,0) == 1
224
+ assert binomial(0,-1) == 0
225
+ assert binomial(3,2) == 3
226
+ assert binomial(5,2) == 10
227
+ assert binomial(5,3) == 10
228
+ assert binomial(5,5) == 1
229
+ assert binomial(-1,0) == 1
230
+ assert binomial(-2,-4) == 3
231
+ assert binomial(4.5, 1.5) == 6.5625
232
+ assert binomial(1100,1) == 1100
233
+ assert binomial(1100,2) == 604450
234
+ assert beta(1,1) == 1
235
+ assert beta(0,0) == inf
236
+ assert beta(3,0) == inf
237
+ assert beta(-1,-1) == inf
238
+ assert beta(1.5,1).ae(2/3.)
239
+ assert beta(1.5,2.5).ae(pi/16)
240
+ assert (10**15*beta(10,100)).ae(2.3455339739604649879)
241
+ assert beta(inf,inf) == 0
242
+ assert isnan(beta(-inf,inf))
243
+ assert isnan(beta(-3,inf))
244
+ assert isnan(beta(0,inf))
245
+ assert beta(inf,0.5) == beta(0.5,inf) == 0
246
+ assert beta(inf,-1.5) == inf
247
+ assert beta(inf,-0.5) == -inf
248
+ assert beta(1+2j,-1-j/2).ae(1.16396542451069943086+0.08511695947832914640j)
249
+ assert beta(-0.5,0.5) == 0
250
+ assert beta(-3,3).ae(-1/3.)
251
+ assert beta('-255.5815971722918','-0.5119253100282322').ae('18.157330562703710339') # issue 421
252
+
253
+ def test_zeta():
254
+ mp.dps = 15
255
+ assert zeta(2).ae(pi**2 / 6)
256
+ assert zeta(2.0).ae(pi**2 / 6)
257
+ assert zeta(mpc(2)).ae(pi**2 / 6)
258
+ assert zeta(100).ae(1)
259
+ assert zeta(0).ae(-0.5)
260
+ assert zeta(0.5).ae(-1.46035450880958681)
261
+ assert zeta(-1).ae(-mpf(1)/12)
262
+ assert zeta(-2) == 0
263
+ assert zeta(-3).ae(mpf(1)/120)
264
+ assert zeta(-4) == 0
265
+ assert zeta(-100) == 0
266
+ assert isnan(zeta(nan))
267
+ assert zeta(1e-30).ae(-0.5)
268
+ assert zeta(-1e-30).ae(-0.5)
269
+ # Zeros in the critical strip
270
+ assert zeta(mpc(0.5, 14.1347251417346937904)).ae(0)
271
+ assert zeta(mpc(0.5, 21.0220396387715549926)).ae(0)
272
+ assert zeta(mpc(0.5, 25.0108575801456887632)).ae(0)
273
+ assert zeta(mpc(1e-30,1e-40)).ae(-0.5)
274
+ assert zeta(mpc(-1e-30,1e-40)).ae(-0.5)
275
+ mp.dps = 50
276
+ im = '236.5242296658162058024755079556629786895294952121891237'
277
+ assert zeta(mpc(0.5, im)).ae(0, 1e-46)
278
+ mp.dps = 15
279
+ # Complex reflection formula
280
+ assert (zeta(-60+3j) / 10**34).ae(8.6270183987866146+15.337398548226238j)
281
+ # issue #358
282
+ assert zeta(0,0.5) == 0
283
+ assert zeta(0,0) == 0.5
284
+ assert zeta(0,0.5,1).ae(-0.34657359027997265)
285
+ # see issue #390
286
+ assert zeta(-1.5,0.5j).ae(-0.13671400162512768475 + 0.11411333638426559139j)
287
+
288
+ def test_altzeta():
289
+ mp.dps = 15
290
+ assert altzeta(-2) == 0
291
+ assert altzeta(-4) == 0
292
+ assert altzeta(-100) == 0
293
+ assert altzeta(0) == 0.5
294
+ assert altzeta(-1) == 0.25
295
+ assert altzeta(-3) == -0.125
296
+ assert altzeta(-5) == 0.25
297
+ assert altzeta(-21) == 1180529130.25
298
+ assert altzeta(1).ae(log(2))
299
+ assert altzeta(2).ae(pi**2/12)
300
+ assert altzeta(10).ae(73*pi**10/6842880)
301
+ assert altzeta(50) < 1
302
+ assert altzeta(60, rounding='d') < 1
303
+ assert altzeta(60, rounding='u') == 1
304
+ assert altzeta(10000, rounding='d') < 1
305
+ assert altzeta(10000, rounding='u') == 1
306
+ assert altzeta(3+0j) == altzeta(3)
307
+ s = 3+4j
308
+ assert altzeta(s).ae((1-2**(1-s))*zeta(s))
309
+ s = -3+4j
310
+ assert altzeta(s).ae((1-2**(1-s))*zeta(s))
311
+ assert altzeta(-100.5).ae(4.58595480083585913e+108)
312
+ assert altzeta(1.3).ae(0.73821404216623045)
313
+ assert altzeta(1e-30).ae(0.5)
314
+ assert altzeta(-1e-30).ae(0.5)
315
+ assert altzeta(mpc(1e-30,1e-40)).ae(0.5)
316
+ assert altzeta(mpc(-1e-30,1e-40)).ae(0.5)
317
+
318
+ def test_zeta_huge():
319
+ mp.dps = 15
320
+ assert zeta(inf) == 1
321
+ mp.dps = 50
322
+ assert zeta(100).ae('1.0000000000000000000000000000007888609052210118073522')
323
+ assert zeta(40*pi).ae('1.0000000000000000000000000000000000000148407238666182')
324
+ mp.dps = 10000
325
+ v = zeta(33000)
326
+ mp.dps = 15
327
+ assert str(v-1) == '1.02363019598118e-9934'
328
+ assert zeta(pi*1000, rounding=round_up) > 1
329
+ assert zeta(3000, rounding=round_up) > 1
330
+ assert zeta(pi*1000) == 1
331
+ assert zeta(3000) == 1
332
+
333
+ def test_zeta_negative():
334
+ mp.dps = 150
335
+ a = -pi*10**40
336
+ mp.dps = 15
337
+ assert str(zeta(a)) == '2.55880492708712e+1233536161668617575553892558646631323374078'
338
+ mp.dps = 50
339
+ assert str(zeta(a)) == '2.5588049270871154960875033337384432038436330847333e+1233536161668617575553892558646631323374078'
340
+ mp.dps = 15
341
+
342
+ def test_polygamma():
343
+ mp.dps = 15
344
+ psi0 = lambda z: psi(0,z)
345
+ psi1 = lambda z: psi(1,z)
346
+ assert psi0(3) == psi(0,3) == digamma(3)
347
+ #assert psi2(3) == psi(2,3) == tetragamma(3)
348
+ #assert psi3(3) == psi(3,3) == pentagamma(3)
349
+ assert psi0(pi).ae(0.97721330794200673)
350
+ assert psi0(-pi).ae(7.8859523853854902)
351
+ assert psi0(-pi+1).ae(7.5676424992016996)
352
+ assert psi0(pi+j).ae(1.04224048313859376 + 0.35853686544063749j)
353
+ assert psi0(-pi-j).ae(1.3404026194821986 - 2.8824392476809402j)
354
+ assert findroot(psi0, 1).ae(1.4616321449683622)
355
+ assert psi0(1e-10).ae(-10000000000.57722)
356
+ assert psi0(1e-40).ae(-1.000000000000000e+40)
357
+ assert psi0(1e-10+1e-10j).ae(-5000000000.577215 + 5000000000.000000j)
358
+ assert psi0(1e-40+1e-40j).ae(-5.000000000000000e+39 + 5.000000000000000e+39j)
359
+ assert psi0(inf) == inf
360
+ assert psi1(inf) == 0
361
+ assert psi(2,inf) == 0
362
+ assert psi1(pi).ae(0.37424376965420049)
363
+ assert psi1(-pi).ae(53.030438740085385)
364
+ assert psi1(pi+j).ae(0.32935710377142464 - 0.12222163911221135j)
365
+ assert psi1(-pi-j).ae(-0.30065008356019703 + 0.01149892486928227j)
366
+ assert (10**6*psi(4,1+10*pi*j)).ae(-6.1491803479004446 - 0.3921316371664063j)
367
+ assert psi0(1+10*pi*j).ae(3.4473994217222650 + 1.5548808324857071j)
368
+ assert isnan(psi0(nan))
369
+ assert isnan(psi0(-inf))
370
+ assert psi0(-100.5).ae(4.615124601338064)
371
+ assert psi0(3+0j).ae(psi0(3))
372
+ assert psi0(-100+3j).ae(4.6106071768714086321+3.1117510556817394626j)
373
+ assert isnan(psi(2,mpc(0,inf)))
374
+ assert isnan(psi(2,mpc(0,nan)))
375
+ assert isnan(psi(2,mpc(0,-inf)))
376
+ assert isnan(psi(2,mpc(1,inf)))
377
+ assert isnan(psi(2,mpc(1,nan)))
378
+ assert isnan(psi(2,mpc(1,-inf)))
379
+ assert isnan(psi(2,mpc(inf,inf)))
380
+ assert isnan(psi(2,mpc(nan,nan)))
381
+ assert isnan(psi(2,mpc(-inf,-inf)))
382
+ mp.dps = 30
383
+ # issue #534
384
+ assert digamma(-0.75+1j).ae(mpc('0.46317279488182026118963809283042317', '2.4821070143037957102007677817351115'))
385
+ mp.dps = 15
386
+
387
+ def test_polygamma_high_prec():
388
+ mp.dps = 100
389
+ assert str(psi(0,pi)) == "0.9772133079420067332920694864061823436408346099943256380095232865318105924777141317302075654362928734"
390
+ assert str(psi(10,pi)) == "-12.98876181434889529310283769414222588307175962213707170773803550518307617769657562747174101900659238"
391
+
392
+ def test_polygamma_identities():
393
+ mp.dps = 15
394
+ psi0 = lambda z: psi(0,z)
395
+ psi1 = lambda z: psi(1,z)
396
+ psi2 = lambda z: psi(2,z)
397
+ assert psi0(0.5).ae(-euler-2*log(2))
398
+ assert psi0(1).ae(-euler)
399
+ assert psi1(0.5).ae(0.5*pi**2)
400
+ assert psi1(1).ae(pi**2/6)
401
+ assert psi1(0.25).ae(pi**2 + 8*catalan)
402
+ assert psi2(1).ae(-2*apery)
403
+ mp.dps = 20
404
+ u = -182*apery+4*sqrt(3)*pi**3
405
+ mp.dps = 15
406
+ assert psi(2,5/6.).ae(u)
407
+ assert psi(3,0.5).ae(pi**4)
408
+
409
+ def test_foxtrot_identity():
410
+ # A test of the complex digamma function.
411
+ # See http://mathworld.wolfram.com/FoxTrotSeries.html and
412
+ # http://mathworld.wolfram.com/DigammaFunction.html
413
+ psi0 = lambda z: psi(0,z)
414
+ mp.dps = 50
415
+ a = (-1)**fraction(1,3)
416
+ b = (-1)**fraction(2,3)
417
+ x = -psi0(0.5*a) - psi0(-0.5*b) + psi0(0.5*(1+a)) + psi0(0.5*(1-b))
418
+ y = 2*pi*sech(0.5*sqrt(3)*pi)
419
+ assert x.ae(y)
420
+ mp.dps = 15
421
+
422
+ def test_polygamma_high_order():
423
+ mp.dps = 100
424
+ assert str(psi(50, pi)) == "-1344100348958402765749252447726432491812.641985273160531055707095989227897753035823152397679626136483"
425
+ assert str(psi(50, pi + 14*e)) == "-0.00000000000000000189793739550804321623512073101895801993019919886375952881053090844591920308111549337295143780341396"
426
+ assert str(psi(50, pi + 14*e*j)) == ("(-0.0000000000000000522516941152169248975225472155683565752375889510631513244785"
427
+ "9377385233700094871256507814151956624433 - 0.00000000000000001813157041407010184"
428
+ "702414110218205348527862196327980417757665282244728963891298080199341480881811613j)")
429
+ mp.dps = 15
430
+ assert str(psi(50, pi)) == "-1.34410034895841e+39"
431
+ assert str(psi(50, pi + 14*e)) == "-1.89793739550804e-18"
432
+ assert str(psi(50, pi + 14*e*j)) == "(-5.2251694115217e-17 - 1.81315704140701e-17j)"
433
+
434
+ def test_harmonic():
435
+ mp.dps = 15
436
+ assert harmonic(0) == 0
437
+ assert harmonic(1) == 1
438
+ assert harmonic(2) == 1.5
439
+ assert harmonic(3).ae(1. + 1./2 + 1./3)
440
+ assert harmonic(10**10).ae(23.603066594891989701)
441
+ assert harmonic(10**1000).ae(2303.162308658947)
442
+ assert harmonic(0.5).ae(2-2*log(2))
443
+ assert harmonic(inf) == inf
444
+ assert harmonic(2+0j) == 1.5+0j
445
+ assert harmonic(1+2j).ae(1.4918071802755104+0.92080728264223022j)
446
+
447
+ def test_gamma_huge_1():
448
+ mp.dps = 500
449
+ x = mpf(10**10) / 7
450
+ mp.dps = 15
451
+ assert str(gamma(x)) == "6.26075321389519e+12458010678"
452
+ mp.dps = 50
453
+ assert str(gamma(x)) == "6.2607532138951929201303779291707455874010420783933e+12458010678"
454
+ mp.dps = 15
455
+
456
+ def test_gamma_huge_2():
457
+ mp.dps = 500
458
+ x = mpf(10**100) / 19
459
+ mp.dps = 15
460
+ assert str(gamma(x)) == (\
461
+ "1.82341134776679e+5172997469323364168990133558175077136829182824042201886051511"
462
+ "9656908623426021308685461258226190190661")
463
+ mp.dps = 50
464
+ assert str(gamma(x)) == (\
465
+ "1.82341134776678875374414910350027596939980412984e+5172997469323364168990133558"
466
+ "1750771368291828240422018860515119656908623426021308685461258226190190661")
467
+
468
+ def test_gamma_huge_3():
469
+ mp.dps = 500
470
+ x = 10**80 // 3 + 10**70*j / 7
471
+ mp.dps = 15
472
+ y = gamma(x)
473
+ assert str(y.real) == (\
474
+ "-6.82925203918106e+2636286142112569524501781477865238132302397236429627932441916"
475
+ "056964386399485392600")
476
+ assert str(y.imag) == (\
477
+ "8.54647143678418e+26362861421125695245017814778652381323023972364296279324419160"
478
+ "56964386399485392600")
479
+ mp.dps = 50
480
+ y = gamma(x)
481
+ assert str(y.real) == (\
482
+ "-6.8292520391810548460682736226799637356016538421817e+26362861421125695245017814"
483
+ "77865238132302397236429627932441916056964386399485392600")
484
+ assert str(y.imag) == (\
485
+ "8.5464714367841748507479306948130687511711420234015e+263628614211256952450178147"
486
+ "7865238132302397236429627932441916056964386399485392600")
487
+
488
+ def test_gamma_huge_4():
489
+ x = 3200+11500j
490
+ mp.dps = 15
491
+ assert str(gamma(x)) == \
492
+ "(8.95783268539713e+5164 - 1.94678798329735e+5164j)"
493
+ mp.dps = 50
494
+ assert str(gamma(x)) == (\
495
+ "(8.9578326853971339570292952697675570822206567327092e+5164"
496
+ " - 1.9467879832973509568895402139429643650329524144794e+51"
497
+ "64j)")
498
+ mp.dps = 15
499
+
500
+ def test_gamma_huge_5():
501
+ mp.dps = 500
502
+ x = 10**60 * j / 3
503
+ mp.dps = 15
504
+ y = gamma(x)
505
+ assert str(y.real) == "-3.27753899634941e-227396058973640224580963937571892628368354580620654233316839"
506
+ assert str(y.imag) == "-7.1519888950416e-227396058973640224580963937571892628368354580620654233316841"
507
+ mp.dps = 50
508
+ y = gamma(x)
509
+ assert str(y.real) == (\
510
+ "-3.2775389963494132168950056995974690946983219123935e-22739605897364022458096393"
511
+ "7571892628368354580620654233316839")
512
+ assert str(y.imag) == (\
513
+ "-7.1519888950415979749736749222530209713136588885897e-22739605897364022458096393"
514
+ "7571892628368354580620654233316841")
515
+ mp.dps = 15
516
+
517
+ def test_gamma_huge_7():
518
+ mp.dps = 100
519
+ a = 3 + j/mpf(10)**1000
520
+ mp.dps = 15
521
+ y = gamma(a)
522
+ assert str(y.real) == "2.0"
523
+ # wrong
524
+ #assert str(y.imag) == "2.16735365342606e-1000"
525
+ assert str(y.imag) == "1.84556867019693e-1000"
526
+ mp.dps = 50
527
+ y = gamma(a)
528
+ assert str(y.real) == "2.0"
529
+ #assert str(y.imag) == "2.1673536534260596065418805612488708028522563689298e-1000"
530
+ assert str(y.imag) == "1.8455686701969342787869758198351951379156813281202e-1000"
531
+
532
+ def test_stieltjes():
533
+ mp.dps = 15
534
+ assert stieltjes(0).ae(+euler)
535
+ mp.dps = 25
536
+ assert stieltjes(1).ae('-0.07281584548367672486058637587')
537
+ assert stieltjes(2).ae('-0.009690363192872318484530386035')
538
+ assert stieltjes(3).ae('0.002053834420303345866160046543')
539
+ assert stieltjes(4).ae('0.002325370065467300057468170178')
540
+ mp.dps = 15
541
+ assert stieltjes(1).ae(-0.07281584548367672486058637587)
542
+ assert stieltjes(2).ae(-0.009690363192872318484530386035)
543
+ assert stieltjes(3).ae(0.002053834420303345866160046543)
544
+ assert stieltjes(4).ae(0.0023253700654673000574681701775)
545
+
546
+ def test_barnesg():
547
+ mp.dps = 15
548
+ assert barnesg(0) == barnesg(-1) == 0
549
+ assert [superfac(i) for i in range(8)] == [1, 1, 2, 12, 288, 34560, 24883200, 125411328000]
550
+ assert str(superfac(1000)) == '3.24570818422368e+1177245'
551
+ assert isnan(barnesg(nan))
552
+ assert isnan(superfac(nan))
553
+ assert isnan(hyperfac(nan))
554
+ assert barnesg(inf) == inf
555
+ assert superfac(inf) == inf
556
+ assert hyperfac(inf) == inf
557
+ assert isnan(superfac(-inf))
558
+ assert barnesg(0.7).ae(0.8068722730141471)
559
+ assert barnesg(2+3j).ae(-0.17810213864082169+0.04504542715447838j)
560
+ assert [hyperfac(n) for n in range(7)] == [1, 1, 4, 108, 27648, 86400000, 4031078400000]
561
+ assert [hyperfac(n) for n in range(0,-7,-1)] == [1,1,-1,-4,108,27648,-86400000]
562
+ a = barnesg(-3+0j)
563
+ assert a == 0 and isinstance(a, mpc)
564
+ a = hyperfac(-3+0j)
565
+ assert a == -4 and isinstance(a, mpc)
566
+
567
+ def test_polylog():
568
+ mp.dps = 15
569
+ zs = [mpmathify(z) for z in [0, 0.5, 0.99, 4, -0.5, -4, 1j, 3+4j]]
570
+ for z in zs: assert polylog(1, z).ae(-log(1-z))
571
+ for z in zs: assert polylog(0, z).ae(z/(1-z))
572
+ for z in zs: assert polylog(-1, z).ae(z/(1-z)**2)
573
+ for z in zs: assert polylog(-2, z).ae(z*(1+z)/(1-z)**3)
574
+ for z in zs: assert polylog(-3, z).ae(z*(1+4*z+z**2)/(1-z)**4)
575
+ assert polylog(3, 7).ae(5.3192579921456754382-5.9479244480803301023j)
576
+ assert polylog(3, -7).ae(-4.5693548977219423182)
577
+ assert polylog(2, 0.9).ae(1.2997147230049587252)
578
+ assert polylog(2, -0.9).ae(-0.75216317921726162037)
579
+ assert polylog(2, 0.9j).ae(-0.17177943786580149299+0.83598828572550503226j)
580
+ assert polylog(2, 1.1).ae(1.9619991013055685931-0.2994257606855892575j)
581
+ assert polylog(2, -1.1).ae(-0.89083809026228260587)
582
+ assert polylog(2, 1.1*sqrt(j)).ae(0.58841571107611387722+1.09962542118827026011j)
583
+ assert polylog(-2, 0.9).ae(1710)
584
+ assert polylog(-2, -0.9).ae(-90/6859.)
585
+ assert polylog(3, 0.9).ae(1.0496589501864398696)
586
+ assert polylog(-3, 0.9).ae(48690)
587
+ assert polylog(-3, -4).ae(-0.0064)
588
+ assert polylog(0.5+j/3, 0.5+j/2).ae(0.31739144796565650535 + 0.99255390416556261437j)
589
+ assert polylog(3+4j,1).ae(zeta(3+4j))
590
+ assert polylog(3+4j,-1).ae(-altzeta(3+4j))
591
+ # issue 390
592
+ assert polylog(1.5, -48.910886523731889).ae(-6.272992229311817)
593
+ assert polylog(1.5, 200).ae(-8.349608319033686529 - 8.159694826434266042j)
594
+ assert polylog(-2+0j, -2).ae(mpf(1)/13.5)
595
+ assert polylog(-2+0j, 1.25).ae(-180)
596
+
597
+ def test_bell_polyexp():
598
+ mp.dps = 15
599
+ # TODO: more tests for polyexp
600
+ assert (polyexp(0,1e-10)*10**10).ae(1.00000000005)
601
+ assert (polyexp(1,1e-10)*10**10).ae(1.0000000001)
602
+ assert polyexp(5,3j).ae(-607.7044517476176454+519.962786482001476087j)
603
+ assert polyexp(-1,3.5).ae(12.09537536175543444)
604
+ # bell(0,x) = 1
605
+ assert bell(0,0) == 1
606
+ assert bell(0,1) == 1
607
+ assert bell(0,2) == 1
608
+ assert bell(0,inf) == 1
609
+ assert bell(0,-inf) == 1
610
+ assert isnan(bell(0,nan))
611
+ # bell(1,x) = x
612
+ assert bell(1,4) == 4
613
+ assert bell(1,0) == 0
614
+ assert bell(1,inf) == inf
615
+ assert bell(1,-inf) == -inf
616
+ assert isnan(bell(1,nan))
617
+ # bell(2,x) = x*(1+x)
618
+ assert bell(2,-1) == 0
619
+ assert bell(2,0) == 0
620
+ # large orders / arguments
621
+ assert bell(10) == 115975
622
+ assert bell(10,1) == 115975
623
+ assert bell(10, -8) == 11054008
624
+ assert bell(5,-50) == -253087550
625
+ assert bell(50,-50).ae('3.4746902914629720259e74')
626
+ mp.dps = 80
627
+ assert bell(50,-50) == 347469029146297202586097646631767227177164818163463279814268368579055777450
628
+ assert bell(40,50) == 5575520134721105844739265207408344706846955281965031698187656176321717550
629
+ assert bell(74) == 5006908024247925379707076470957722220463116781409659160159536981161298714301202
630
+ mp.dps = 15
631
+ assert bell(10,20j) == 7504528595600+15649605360020j
632
+ # continuity of the generalization
633
+ assert bell(0.5,0).ae(sinc(pi*0.5))
634
+
635
+ def test_primezeta():
636
+ mp.dps = 15
637
+ assert primezeta(0.9).ae(1.8388316154446882243 + 3.1415926535897932385j)
638
+ assert primezeta(4).ae(0.076993139764246844943)
639
+ assert primezeta(1) == inf
640
+ assert primezeta(inf) == 0
641
+ assert isnan(primezeta(nan))
642
+
643
+ def test_rs_zeta():
644
+ mp.dps = 15
645
+ assert zeta(0.5+100000j).ae(1.0730320148577531321 + 5.7808485443635039843j)
646
+ assert zeta(0.75+100000j).ae(1.837852337251873704 + 1.9988492668661145358j)
647
+ assert zeta(0.5+1000000j, derivative=3).ae(1647.7744105852674733 - 1423.1270943036622097j)
648
+ assert zeta(1+1000000j, derivative=3).ae(3.4085866124523582894 - 18.179184721525947301j)
649
+ assert zeta(1+1000000j, derivative=1).ae(-0.10423479366985452134 - 0.74728992803359056244j)
650
+ assert zeta(0.5-1000000j, derivative=1).ae(11.636804066002521459 + 17.127254072212996004j)
651
+ # Additional sanity tests using fp arithmetic.
652
+ # Some more high-precision tests are found in the docstrings
653
+ def ae(x, y, tol=1e-6):
654
+ return abs(x-y) < tol*abs(y)
655
+ assert ae(fp.zeta(0.5-100000j), 1.0730320148577531321 - 5.7808485443635039843j)
656
+ assert ae(fp.zeta(0.75-100000j), 1.837852337251873704 - 1.9988492668661145358j)
657
+ assert ae(fp.zeta(0.5+1e6j), 0.076089069738227100006 + 2.8051021010192989554j)
658
+ assert ae(fp.zeta(0.5+1e6j, derivative=1), 11.636804066002521459 - 17.127254072212996004j)
659
+ assert ae(fp.zeta(1+1e6j), 0.94738726251047891048 + 0.59421999312091832833j)
660
+ assert ae(fp.zeta(1+1e6j, derivative=1), -0.10423479366985452134 - 0.74728992803359056244j)
661
+ assert ae(fp.zeta(0.5+100000j, derivative=1), 10.766962036817482375 - 30.92705282105996714j)
662
+ assert ae(fp.zeta(0.5+100000j, derivative=2), -119.40515625740538429 + 217.14780631141830251j)
663
+ assert ae(fp.zeta(0.5+100000j, derivative=3), 1129.7550282628460881 - 1685.4736895169690346j)
664
+ assert ae(fp.zeta(0.5+100000j, derivative=4), -10407.160819314958615 + 13777.786698628045085j)
665
+ assert ae(fp.zeta(0.75+100000j, derivative=1), -0.41742276699594321475 - 6.4453816275049955949j)
666
+ assert ae(fp.zeta(0.75+100000j, derivative=2), -9.214314279161977266 + 35.07290795337967899j)
667
+ assert ae(fp.zeta(0.75+100000j, derivative=3), 110.61331857820103469 - 236.87847130518129926j)
668
+ assert ae(fp.zeta(0.75+100000j, derivative=4), -1054.334275898559401 + 1769.9177890161596383j)
669
+
670
+ def test_siegelz():
671
+ mp.dps = 15
672
+ assert siegelz(100000).ae(5.87959246868176504171)
673
+ assert siegelz(100000, derivative=2).ae(-54.1172711010126452832)
674
+ assert siegelz(100000, derivative=3).ae(-278.930831343966552538)
675
+ assert siegelz(100000+j,derivative=1).ae(678.214511857070283307-379.742160779916375413j)
676
+
677
+
678
+
679
+ def test_zeta_near_1():
680
+ # Test for a former bug in mpf_zeta and mpc_zeta
681
+ mp.dps = 15
682
+ s1 = fadd(1, '1e-10', exact=True)
683
+ s2 = fadd(1, '-1e-10', exact=True)
684
+ s3 = fadd(1, '1e-10j', exact=True)
685
+ assert zeta(s1).ae(1.000000000057721566490881444e10)
686
+ assert zeta(s2).ae(-9.99999999942278433510574872e9)
687
+ z = zeta(s3)
688
+ assert z.real.ae(0.57721566490153286060)
689
+ assert z.imag.ae(-9.9999999999999999999927184e9)
690
+ mp.dps = 30
691
+ s1 = fadd(1, '1e-50', exact=True)
692
+ s2 = fadd(1, '-1e-50', exact=True)
693
+ s3 = fadd(1, '1e-50j', exact=True)
694
+ assert zeta(s1).ae('1e50')
695
+ assert zeta(s2).ae('-1e50')
696
+ z = zeta(s3)
697
+ assert z.real.ae('0.57721566490153286060651209008240243104215933593992')
698
+ assert z.imag.ae('-1e50')
llmeval-env/lib/python3.10/site-packages/mpmath/tests/test_hp.py ADDED
@@ -0,0 +1,291 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Check that the output from irrational functions is accurate for
3
+ high-precision input, from 5 to 200 digits. The reference values were
4
+ verified with Mathematica.
5
+ """
6
+
7
+ import time
8
+ from mpmath import *
9
+
10
+ precs = [5, 15, 28, 35, 57, 80, 100, 150, 200]
11
+
12
+ # sqrt(3) + pi/2
13
+ a = \
14
+ "3.302847134363773912758768033145623809041389953497933538543279275605"\
15
+ "841220051904536395163599428307109666700184672047856353516867399774243594"\
16
+ "67433521615861420725323528325327484262075464241255915238845599752675"
17
+
18
+ # e + 1/euler**2
19
+ b = \
20
+ "5.719681166601007617111261398629939965860873957353320734275716220045750"\
21
+ "31474116300529519620938123730851145473473708966080207482581266469342214"\
22
+ "824842256999042984813905047895479210702109260221361437411947323431"
23
+
24
+ # sqrt(a)
25
+ sqrt_a = \
26
+ "1.817373691447021556327498239690365674922395036495564333152483422755"\
27
+ "144321726165582817927383239308173567921345318453306994746434073691275094"\
28
+ "484777905906961689902608644112196725896908619756404253109722911487"
29
+
30
+ # sqrt(a+b*i).real
31
+ sqrt_abi_real = \
32
+ "2.225720098415113027729407777066107959851146508557282707197601407276"\
33
+ "89160998185797504198062911768240808839104987021515555650875977724230130"\
34
+ "3584116233925658621288393930286871862273400475179312570274423840384"
35
+
36
+ # sqrt(a+b*i).imag
37
+ sqrt_abi_imag = \
38
+ "1.2849057639084690902371581529110949983261182430040898147672052833653668"\
39
+ "0629534491275114877090834296831373498336559849050755848611854282001250"\
40
+ "1924311019152914021365263161630765255610885489295778894976075186"
41
+
42
+ # log(a)
43
+ log_a = \
44
+ "1.194784864491089550288313512105715261520511949410072046160598707069"\
45
+ "4336653155025770546309137440687056366757650909754708302115204338077595203"\
46
+ "83005773986664564927027147084436553262269459110211221152925732612"
47
+
48
+ # log(a+b*i).real
49
+ log_abi_real = \
50
+ "1.8877985921697018111624077550443297276844736840853590212962006811663"\
51
+ "04949387789489704203167470111267581371396245317618589339274243008242708"\
52
+ "014251531496104028712866224020066439049377679709216784954509456421"
53
+
54
+ # log(a+b*i).imag
55
+ log_abi_imag = \
56
+ "1.0471204952840802663567714297078763189256357109769672185219334169734948"\
57
+ "4265809854092437285294686651806426649541504240470168212723133326542181"\
58
+ "8300136462287639956713914482701017346851009323172531601894918640"
59
+
60
+ # exp(a)
61
+ exp_a = \
62
+ "27.18994224087168661137253262213293847994194869430518354305430976149"\
63
+ "382792035050358791398632888885200049857986258414049540376323785711941636"\
64
+ "100358982497583832083513086941635049329804685212200507288797531143"
65
+
66
+ # exp(a+b*i).real
67
+ exp_abi_real = \
68
+ "22.98606617170543596386921087657586890620262522816912505151109385026"\
69
+ "40160179326569526152851983847133513990281518417211964710397233157168852"\
70
+ "4963130831190142571659948419307628119985383887599493378056639916701"
71
+
72
+ # exp(a+b*i).imag
73
+ exp_abi_imag = \
74
+ "-14.523557450291489727214750571590272774669907424478129280902375851196283"\
75
+ "3377162379031724734050088565710975758824441845278120105728824497308303"\
76
+ "6065619788140201636218705414429933685889542661364184694108251449"
77
+
78
+ # a**b
79
+ pow_a_b = \
80
+ "928.7025342285568142947391505837660251004990092821305668257284426997"\
81
+ "361966028275685583421197860603126498884545336686124793155581311527995550"\
82
+ "580229264427202446131740932666832138634013168125809402143796691154"
83
+
84
+ # (a**(a+b*i)).real
85
+ pow_a_abi_real = \
86
+ "44.09156071394489511956058111704382592976814280267142206420038656267"\
87
+ "67707916510652790502399193109819563864568986234654864462095231138500505"\
88
+ "8197456514795059492120303477512711977915544927440682508821426093455"
89
+
90
+ # (a**(a+b*i)).imag
91
+ pow_a_abi_imag = \
92
+ "27.069371511573224750478105146737852141664955461266218367212527612279886"\
93
+ "9322304536553254659049205414427707675802193810711302947536332040474573"\
94
+ "8166261217563960235014674118610092944307893857862518964990092301"
95
+
96
+ # ((a+b*i)**(a+b*i)).real
97
+ pow_abi_abi_real = \
98
+ "-0.15171310677859590091001057734676423076527145052787388589334350524"\
99
+ "8084195882019497779202452975350579073716811284169068082670778986235179"\
100
+ "0813026562962084477640470612184016755250592698408112493759742219150452"\
101
+
102
+ # ((a+b*i)**(a+b*i)).imag
103
+ pow_abi_abi_imag = \
104
+ "1.2697592504953448936553147870155987153192995316950583150964099070426"\
105
+ "4736837932577176947632535475040521749162383347758827307504526525647759"\
106
+ "97547638617201824468382194146854367480471892602963428122896045019902"
107
+
108
+ # sin(a)
109
+ sin_a = \
110
+ "-0.16055653857469062740274792907968048154164433772938156243509084009"\
111
+ "38437090841460493108570147191289893388608611542655654723437248152535114"\
112
+ "528368009465836614227575701220612124204622383149391870684288862269631"
113
+
114
+ # sin(1000*a)
115
+ sin_1000a = \
116
+ "-0.85897040577443833776358106803777589664322997794126153477060795801"\
117
+ "09151695416961724733492511852267067419573754315098042850381158563024337"\
118
+ "216458577140500488715469780315833217177634490142748614625281171216863"
119
+
120
+ # sin(a+b*i)
121
+ sin_abi_real = \
122
+ "-24.4696999681556977743346798696005278716053366404081910969773939630"\
123
+ "7149215135459794473448465734589287491880563183624997435193637389884206"\
124
+ "02151395451271809790360963144464736839412254746645151672423256977064"
125
+
126
+ sin_abi_imag = \
127
+ "-150.42505378241784671801405965872972765595073690984080160750785565810981"\
128
+ "8314482499135443827055399655645954830931316357243750839088113122816583"\
129
+ "7169201254329464271121058839499197583056427233866320456505060735"
130
+
131
+ # cos
132
+ cos_a = \
133
+ "-0.98702664499035378399332439243967038895709261414476495730788864004"\
134
+ "05406821549361039745258003422386169330787395654908532996287293003581554"\
135
+ "257037193284199198069707141161341820684198547572456183525659969145501"
136
+
137
+ cos_1000a = \
138
+ "-0.51202523570982001856195696460663971099692261342827540426136215533"\
139
+ "52686662667660613179619804463250686852463876088694806607652218586060613"\
140
+ "951310588158830695735537073667299449753951774916401887657320950496820"
141
+
142
+ # tan
143
+ tan_a = \
144
+ "0.162666873675188117341401059858835168007137819495998960250142156848"\
145
+ "639654718809412181543343168174807985559916643549174530459883826451064966"\
146
+ "7996119428949951351938178809444268785629011625179962457123195557310"
147
+
148
+ tan_abi_real = \
149
+ "6.822696615947538488826586186310162599974827139564433912601918442911"\
150
+ "1026830824380070400102213741875804368044342309515353631134074491271890"\
151
+ "467615882710035471686578162073677173148647065131872116479947620E-6"
152
+
153
+ tan_abi_imag = \
154
+ "0.9999795833048243692245661011298447587046967777739649018690797625964167"\
155
+ "1446419978852235960862841608081413169601038230073129482874832053357571"\
156
+ "62702259309150715669026865777947502665936317953101462202542168429"
157
+
158
+
159
+ def test_hp():
160
+ for dps in precs:
161
+ mp.dps = dps + 8
162
+ aa = mpf(a)
163
+ bb = mpf(b)
164
+ a1000 = 1000*mpf(a)
165
+ abi = mpc(aa, bb)
166
+ mp.dps = dps
167
+ assert (sqrt(3) + pi/2).ae(aa)
168
+ assert (e + 1/euler**2).ae(bb)
169
+
170
+ assert sqrt(aa).ae(mpf(sqrt_a))
171
+ assert sqrt(abi).ae(mpc(sqrt_abi_real, sqrt_abi_imag))
172
+
173
+ assert log(aa).ae(mpf(log_a))
174
+ assert log(abi).ae(mpc(log_abi_real, log_abi_imag))
175
+
176
+ assert exp(aa).ae(mpf(exp_a))
177
+ assert exp(abi).ae(mpc(exp_abi_real, exp_abi_imag))
178
+
179
+ assert (aa**bb).ae(mpf(pow_a_b))
180
+ assert (aa**abi).ae(mpc(pow_a_abi_real, pow_a_abi_imag))
181
+ assert (abi**abi).ae(mpc(pow_abi_abi_real, pow_abi_abi_imag))
182
+
183
+ assert sin(a).ae(mpf(sin_a))
184
+ assert sin(a1000).ae(mpf(sin_1000a))
185
+ assert sin(abi).ae(mpc(sin_abi_real, sin_abi_imag))
186
+
187
+ assert cos(a).ae(mpf(cos_a))
188
+ assert cos(a1000).ae(mpf(cos_1000a))
189
+
190
+ assert tan(a).ae(mpf(tan_a))
191
+ assert tan(abi).ae(mpc(tan_abi_real, tan_abi_imag))
192
+
193
+ # check that complex cancellation is avoided so that both
194
+ # real and imaginary parts have high relative accuracy.
195
+ # abs_eps should be 0, but has to be set to 1e-205 to pass the
196
+ # 200-digit case, probably due to slight inaccuracy in the
197
+ # precomputed input
198
+ assert (tan(abi).real).ae(mpf(tan_abi_real), abs_eps=1e-205)
199
+ assert (tan(abi).imag).ae(mpf(tan_abi_imag), abs_eps=1e-205)
200
+ mp.dps = 460
201
+ assert str(log(3))[-20:] == '02166121184001409826'
202
+ mp.dps = 15
203
+
204
+ # Since str(a) can differ in the last digit from rounded a, and I want
205
+ # to compare the last digits of big numbers with the results in Mathematica,
206
+ # I made this hack to get the last 20 digits of rounded a
207
+
208
+ def last_digits(a):
209
+ r = repr(a)
210
+ s = str(a)
211
+ #dps = mp.dps
212
+ #mp.dps += 3
213
+ m = 10
214
+ r = r.replace(s[:-m],'')
215
+ r = r.replace("mpf('",'').replace("')",'')
216
+ num0 = 0
217
+ for c in r:
218
+ if c == '0':
219
+ num0 += 1
220
+ else:
221
+ break
222
+ b = float(int(r))/10**(len(r) - m)
223
+ if b >= 10**m - 0.5: # pragma: no cover
224
+ raise NotImplementedError
225
+ n = int(round(b))
226
+ sn = str(n)
227
+ s = s[:-m] + '0'*num0 + sn
228
+ return s[-20:]
229
+
230
+ # values checked with Mathematica
231
+ def test_log_hp():
232
+ mp.dps = 2000
233
+ a = mpf(10)**15000/3
234
+ r = log(a)
235
+ res = last_digits(r)
236
+ # Mathematica N[Log[10^15000/3], 2000]
237
+ # ...7443804441768333470331
238
+ assert res == '43804441768333470331'
239
+
240
+ # see issue 145
241
+ r = log(mpf(3)/2)
242
+ # Mathematica N[Log[3/2], 2000]
243
+ # ...69653749808140753263288
244
+ res = last_digits(r)
245
+ assert res == '53749808140753263288'
246
+
247
+ mp.dps = 10000
248
+ r = log(2)
249
+ res = last_digits(r)
250
+ # Mathematica N[Log[2], 10000]
251
+ # ...695615913401856601359655561
252
+ assert res == '13401856601359655561'
253
+ r = log(mpf(10)**10/3)
254
+ res = last_digits(r)
255
+ # Mathematica N[Log[10^10/3], 10000]
256
+ # ...587087654020631943060007154
257
+ assert res == '54020631943060007154', res
258
+ r = log(mpf(10)**100/3)
259
+ res = last_digits(r)
260
+ # Mathematica N[Log[10^100/3], 10000]
261
+ # ,,,59246336539088351652334666
262
+ assert res == '36539088351652334666', res
263
+ mp.dps += 10
264
+ a = 1 - mpf(1)/10**10
265
+ mp.dps -= 10
266
+ r = log(a)
267
+ res = last_digits(r)
268
+ # ...3310334360482956137216724048322957404
269
+ # 372167240483229574038733026370
270
+ # Mathematica N[Log[1 - 10^-10]*10^10, 10000]
271
+ # ...60482956137216724048322957404
272
+ assert res == '37216724048322957404', res
273
+ mp.dps = 10000
274
+ mp.dps += 100
275
+ a = 1 + mpf(1)/10**100
276
+ mp.dps -= 100
277
+
278
+ r = log(a)
279
+ res = last_digits(+r)
280
+ # Mathematica N[Log[1 + 10^-100]*10^10, 10030]
281
+ # ...3994733877377412241546890854692521568292338268273 10^-91
282
+ assert res == '39947338773774122415', res
283
+
284
+ mp.dps = 15
285
+
286
+ def test_exp_hp():
287
+ mp.dps = 4000
288
+ r = exp(mpf(1)/10)
289
+ # IntegerPart[N[Exp[1/10] * 10^4000, 4000]]
290
+ # ...92167105162069688129
291
+ assert int(r * 10**mp.dps) % 10**20 == 92167105162069688129
llmeval-env/lib/python3.10/site-packages/mpmath/tests/test_identify.py ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from mpmath import *
2
+
3
+ def test_pslq():
4
+ mp.dps = 15
5
+ assert pslq([3*pi+4*e/7, pi, e, log(2)]) == [7, -21, -4, 0]
6
+ assert pslq([4.9999999999999991, 1]) == [1, -5]
7
+ assert pslq([2,1]) == [1, -2]
8
+
9
+ def test_identify():
10
+ mp.dps = 20
11
+ assert identify(zeta(4), ['log(2)', 'pi**4']) == '((1/90)*pi**4)'
12
+ mp.dps = 15
13
+ assert identify(exp(5)) == 'exp(5)'
14
+ assert identify(exp(4)) == 'exp(4)'
15
+ assert identify(log(5)) == 'log(5)'
16
+ assert identify(exp(3*pi), ['pi']) == 'exp((3*pi))'
17
+ assert identify(3, full=True) == ['3', '3', '1/(1/3)', 'sqrt(9)',
18
+ '1/sqrt((1/9))', '(sqrt(12)/2)**2', '1/(sqrt(12)/6)**2']
19
+ assert identify(pi+1, {'a':+pi}) == '(1 + 1*a)'
llmeval-env/lib/python3.10/site-packages/mpmath/tests/test_interval.py ADDED
@@ -0,0 +1,453 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from mpmath import *
2
+
3
+ def test_interval_identity():
4
+ iv.dps = 15
5
+ assert mpi(2) == mpi(2, 2)
6
+ assert mpi(2) != mpi(-2, 2)
7
+ assert not (mpi(2) != mpi(2, 2))
8
+ assert mpi(-1, 1) == mpi(-1, 1)
9
+ assert str(mpi('0.1')) == "[0.099999999999999991673, 0.10000000000000000555]"
10
+ assert repr(mpi('0.1')) == "mpi('0.099999999999999992', '0.10000000000000001')"
11
+ u = mpi(-1, 3)
12
+ assert -1 in u
13
+ assert 2 in u
14
+ assert 3 in u
15
+ assert -1.1 not in u
16
+ assert 3.1 not in u
17
+ assert mpi(-1, 3) in u
18
+ assert mpi(0, 1) in u
19
+ assert mpi(-1.1, 2) not in u
20
+ assert mpi(2.5, 3.1) not in u
21
+ w = mpi(-inf, inf)
22
+ assert mpi(-5, 5) in w
23
+ assert mpi(2, inf) in w
24
+ assert mpi(0, 2) in mpi(0, 10)
25
+ assert not (3 in mpi(-inf, 0))
26
+
27
+ def test_interval_hash():
28
+ assert hash(mpi(3)) == hash(3)
29
+ assert hash(mpi(3.25)) == hash(3.25)
30
+ assert hash(mpi(3,4)) == hash(mpi(3,4))
31
+ assert hash(iv.mpc(3)) == hash(3)
32
+ assert hash(iv.mpc(3,4)) == hash(3+4j)
33
+ assert hash(iv.mpc((1,3),(2,4))) == hash(iv.mpc((1,3),(2,4)))
34
+
35
+ def test_interval_arithmetic():
36
+ iv.dps = 15
37
+ assert mpi(2) + mpi(3,4) == mpi(5,6)
38
+ assert mpi(1, 2)**2 == mpi(1, 4)
39
+ assert mpi(1) + mpi(0, 1e-50) == mpi(1, mpf('1.0000000000000002'))
40
+ x = 1 / (1 / mpi(3))
41
+ assert x.a < 3 < x.b
42
+ x = mpi(2) ** mpi(0.5)
43
+ iv.dps += 5
44
+ sq = iv.sqrt(2)
45
+ iv.dps -= 5
46
+ assert x.a < sq < x.b
47
+ assert mpi(1) / mpi(1, inf)
48
+ assert mpi(2, 3) / inf == mpi(0, 0)
49
+ assert mpi(0) / inf == 0
50
+ assert mpi(0) / 0 == mpi(-inf, inf)
51
+ assert mpi(inf) / 0 == mpi(-inf, inf)
52
+ assert mpi(0) * inf == mpi(-inf, inf)
53
+ assert 1 / mpi(2, inf) == mpi(0, 0.5)
54
+ assert str((mpi(50, 50) * mpi(-10, -10)) / 3) == \
55
+ '[-166.66666666666668561, -166.66666666666665719]'
56
+ assert mpi(0, 4) ** 3 == mpi(0, 64)
57
+ assert mpi(2,4).mid == 3
58
+ iv.dps = 30
59
+ a = mpi(iv.pi)
60
+ iv.dps = 15
61
+ b = +a
62
+ assert b.a < a.a
63
+ assert b.b > a.b
64
+ a = mpi(iv.pi)
65
+ assert a == +a
66
+ assert abs(mpi(-1,2)) == mpi(0,2)
67
+ assert abs(mpi(0.5,2)) == mpi(0.5,2)
68
+ assert abs(mpi(-3,2)) == mpi(0,3)
69
+ assert abs(mpi(-3,-0.5)) == mpi(0.5,3)
70
+ assert mpi(0) * mpi(2,3) == mpi(0)
71
+ assert mpi(2,3) * mpi(0) == mpi(0)
72
+ assert mpi(1,3).delta == 2
73
+ assert mpi(1,2) - mpi(3,4) == mpi(-3,-1)
74
+ assert mpi(-inf,0) - mpi(0,inf) == mpi(-inf,0)
75
+ assert mpi(-inf,0) - mpi(-inf,inf) == mpi(-inf,inf)
76
+ assert mpi(0,inf) - mpi(-inf,1) == mpi(-1,inf)
77
+
78
+ def test_interval_mul():
79
+ assert mpi(-1, 0) * inf == mpi(-inf, 0)
80
+ assert mpi(-1, 0) * -inf == mpi(0, inf)
81
+ assert mpi(0, 1) * inf == mpi(0, inf)
82
+ assert mpi(0, 1) * mpi(0, inf) == mpi(0, inf)
83
+ assert mpi(-1, 1) * inf == mpi(-inf, inf)
84
+ assert mpi(-1, 1) * mpi(0, inf) == mpi(-inf, inf)
85
+ assert mpi(-1, 1) * mpi(-inf, inf) == mpi(-inf, inf)
86
+ assert mpi(-inf, 0) * mpi(0, 1) == mpi(-inf, 0)
87
+ assert mpi(-inf, 0) * mpi(0, 0) * mpi(-inf, 0)
88
+ assert mpi(-inf, 0) * mpi(-inf, inf) == mpi(-inf, inf)
89
+ assert mpi(-5,0)*mpi(-32,28) == mpi(-140,160)
90
+ assert mpi(2,3) * mpi(-1,2) == mpi(-3,6)
91
+ # Should be undefined?
92
+ assert mpi(inf, inf) * 0 == mpi(-inf, inf)
93
+ assert mpi(-inf, -inf) * 0 == mpi(-inf, inf)
94
+ assert mpi(0) * mpi(-inf,2) == mpi(-inf,inf)
95
+ assert mpi(0) * mpi(-2,inf) == mpi(-inf,inf)
96
+ assert mpi(-2,inf) * mpi(0) == mpi(-inf,inf)
97
+ assert mpi(-inf,2) * mpi(0) == mpi(-inf,inf)
98
+
99
+ def test_interval_pow():
100
+ assert mpi(3)**2 == mpi(9, 9)
101
+ assert mpi(-3)**2 == mpi(9, 9)
102
+ assert mpi(-3, 1)**2 == mpi(0, 9)
103
+ assert mpi(-3, -1)**2 == mpi(1, 9)
104
+ assert mpi(-3, -1)**3 == mpi(-27, -1)
105
+ assert mpi(-3, 1)**3 == mpi(-27, 1)
106
+ assert mpi(-2, 3)**2 == mpi(0, 9)
107
+ assert mpi(-3, 2)**2 == mpi(0, 9)
108
+ assert mpi(4) ** -1 == mpi(0.25, 0.25)
109
+ assert mpi(-4) ** -1 == mpi(-0.25, -0.25)
110
+ assert mpi(4) ** -2 == mpi(0.0625, 0.0625)
111
+ assert mpi(-4) ** -2 == mpi(0.0625, 0.0625)
112
+ assert mpi(0, 1) ** inf == mpi(0, 1)
113
+ assert mpi(0, 1) ** -inf == mpi(1, inf)
114
+ assert mpi(0, inf) ** inf == mpi(0, inf)
115
+ assert mpi(0, inf) ** -inf == mpi(0, inf)
116
+ assert mpi(1, inf) ** inf == mpi(1, inf)
117
+ assert mpi(1, inf) ** -inf == mpi(0, 1)
118
+ assert mpi(2, 3) ** 1 == mpi(2, 3)
119
+ assert mpi(2, 3) ** 0 == 1
120
+ assert mpi(1,3) ** mpi(2) == mpi(1,9)
121
+
122
+ def test_interval_sqrt():
123
+ assert mpi(4) ** 0.5 == mpi(2)
124
+
125
+ def test_interval_div():
126
+ assert mpi(0.5, 1) / mpi(-1, 0) == mpi(-inf, -0.5)
127
+ assert mpi(0, 1) / mpi(0, 1) == mpi(0, inf)
128
+ assert mpi(inf, inf) / mpi(inf, inf) == mpi(0, inf)
129
+ assert mpi(inf, inf) / mpi(2, inf) == mpi(0, inf)
130
+ assert mpi(inf, inf) / mpi(2, 2) == mpi(inf, inf)
131
+ assert mpi(0, inf) / mpi(2, inf) == mpi(0, inf)
132
+ assert mpi(0, inf) / mpi(2, 2) == mpi(0, inf)
133
+ assert mpi(2, inf) / mpi(2, 2) == mpi(1, inf)
134
+ assert mpi(2, inf) / mpi(2, inf) == mpi(0, inf)
135
+ assert mpi(-4, 8) / mpi(1, inf) == mpi(-4, 8)
136
+ assert mpi(-4, 8) / mpi(0.5, inf) == mpi(-8, 16)
137
+ assert mpi(-inf, 8) / mpi(0.5, inf) == mpi(-inf, 16)
138
+ assert mpi(-inf, inf) / mpi(0.5, inf) == mpi(-inf, inf)
139
+ assert mpi(8, inf) / mpi(0.5, inf) == mpi(0, inf)
140
+ assert mpi(-8, inf) / mpi(0.5, inf) == mpi(-16, inf)
141
+ assert mpi(-4, 8) / mpi(inf, inf) == mpi(0, 0)
142
+ assert mpi(0, 8) / mpi(inf, inf) == mpi(0, 0)
143
+ assert mpi(0, 0) / mpi(inf, inf) == mpi(0, 0)
144
+ assert mpi(-inf, 0) / mpi(inf, inf) == mpi(-inf, 0)
145
+ assert mpi(-inf, 8) / mpi(inf, inf) == mpi(-inf, 0)
146
+ assert mpi(-inf, inf) / mpi(inf, inf) == mpi(-inf, inf)
147
+ assert mpi(-8, inf) / mpi(inf, inf) == mpi(0, inf)
148
+ assert mpi(0, inf) / mpi(inf, inf) == mpi(0, inf)
149
+ assert mpi(8, inf) / mpi(inf, inf) == mpi(0, inf)
150
+ assert mpi(inf, inf) / mpi(inf, inf) == mpi(0, inf)
151
+ assert mpi(-1, 2) / mpi(0, 1) == mpi(-inf, +inf)
152
+ assert mpi(0, 1) / mpi(0, 1) == mpi(0.0, +inf)
153
+ assert mpi(-1, 0) / mpi(0, 1) == mpi(-inf, 0.0)
154
+ assert mpi(-0.5, -0.25) / mpi(0, 1) == mpi(-inf, -0.25)
155
+ assert mpi(0.5, 1) / mpi(0, 1) == mpi(0.5, +inf)
156
+ assert mpi(0.5, 4) / mpi(0, 1) == mpi(0.5, +inf)
157
+ assert mpi(-1, -0.5) / mpi(0, 1) == mpi(-inf, -0.5)
158
+ assert mpi(-4, -0.5) / mpi(0, 1) == mpi(-inf, -0.5)
159
+ assert mpi(-1, 2) / mpi(-2, 0.5) == mpi(-inf, +inf)
160
+ assert mpi(0, 1) / mpi(-2, 0.5) == mpi(-inf, +inf)
161
+ assert mpi(-1, 0) / mpi(-2, 0.5) == mpi(-inf, +inf)
162
+ assert mpi(-0.5, -0.25) / mpi(-2, 0.5) == mpi(-inf, +inf)
163
+ assert mpi(0.5, 1) / mpi(-2, 0.5) == mpi(-inf, +inf)
164
+ assert mpi(0.5, 4) / mpi(-2, 0.5) == mpi(-inf, +inf)
165
+ assert mpi(-1, -0.5) / mpi(-2, 0.5) == mpi(-inf, +inf)
166
+ assert mpi(-4, -0.5) / mpi(-2, 0.5) == mpi(-inf, +inf)
167
+ assert mpi(-1, 2) / mpi(-1, 0) == mpi(-inf, +inf)
168
+ assert mpi(0, 1) / mpi(-1, 0) == mpi(-inf, 0.0)
169
+ assert mpi(-1, 0) / mpi(-1, 0) == mpi(0.0, +inf)
170
+ assert mpi(-0.5, -0.25) / mpi(-1, 0) == mpi(0.25, +inf)
171
+ assert mpi(0.5, 1) / mpi(-1, 0) == mpi(-inf, -0.5)
172
+ assert mpi(0.5, 4) / mpi(-1, 0) == mpi(-inf, -0.5)
173
+ assert mpi(-1, -0.5) / mpi(-1, 0) == mpi(0.5, +inf)
174
+ assert mpi(-4, -0.5) / mpi(-1, 0) == mpi(0.5, +inf)
175
+ assert mpi(-1, 2) / mpi(0.5, 1) == mpi(-2.0, 4.0)
176
+ assert mpi(0, 1) / mpi(0.5, 1) == mpi(0.0, 2.0)
177
+ assert mpi(-1, 0) / mpi(0.5, 1) == mpi(-2.0, 0.0)
178
+ assert mpi(-0.5, -0.25) / mpi(0.5, 1) == mpi(-1.0, -0.25)
179
+ assert mpi(0.5, 1) / mpi(0.5, 1) == mpi(0.5, 2.0)
180
+ assert mpi(0.5, 4) / mpi(0.5, 1) == mpi(0.5, 8.0)
181
+ assert mpi(-1, -0.5) / mpi(0.5, 1) == mpi(-2.0, -0.5)
182
+ assert mpi(-4, -0.5) / mpi(0.5, 1) == mpi(-8.0, -0.5)
183
+ assert mpi(-1, 2) / mpi(-2, -0.5) == mpi(-4.0, 2.0)
184
+ assert mpi(0, 1) / mpi(-2, -0.5) == mpi(-2.0, 0.0)
185
+ assert mpi(-1, 0) / mpi(-2, -0.5) == mpi(0.0, 2.0)
186
+ assert mpi(-0.5, -0.25) / mpi(-2, -0.5) == mpi(0.125, 1.0)
187
+ assert mpi(0.5, 1) / mpi(-2, -0.5) == mpi(-2.0, -0.25)
188
+ assert mpi(0.5, 4) / mpi(-2, -0.5) == mpi(-8.0, -0.25)
189
+ assert mpi(-1, -0.5) / mpi(-2, -0.5) == mpi(0.25, 2.0)
190
+ assert mpi(-4, -0.5) / mpi(-2, -0.5) == mpi(0.25, 8.0)
191
+ # Should be undefined?
192
+ assert mpi(0, 0) / mpi(0, 0) == mpi(-inf, inf)
193
+ assert mpi(0, 0) / mpi(0, 1) == mpi(-inf, inf)
194
+
195
+ def test_interval_cos_sin():
196
+ iv.dps = 15
197
+ cos = iv.cos
198
+ sin = iv.sin
199
+ tan = iv.tan
200
+ pi = iv.pi
201
+ # Around 0
202
+ assert cos(mpi(0)) == 1
203
+ assert sin(mpi(0)) == 0
204
+ assert cos(mpi(0,1)) == mpi(0.54030230586813965399, 1.0)
205
+ assert sin(mpi(0,1)) == mpi(0, 0.8414709848078966159)
206
+ assert cos(mpi(1,2)) == mpi(-0.4161468365471424069, 0.54030230586813976501)
207
+ assert sin(mpi(1,2)) == mpi(0.84147098480789650488, 1.0)
208
+ assert sin(mpi(1,2.5)) == mpi(0.59847214410395643824, 1.0)
209
+ assert cos(mpi(-1, 1)) == mpi(0.54030230586813965399, 1.0)
210
+ assert cos(mpi(-1, 0.5)) == mpi(0.54030230586813965399, 1.0)
211
+ assert cos(mpi(-1, 1.5)) == mpi(0.070737201667702906405, 1.0)
212
+ assert sin(mpi(-1,1)) == mpi(-0.8414709848078966159, 0.8414709848078966159)
213
+ assert sin(mpi(-1,0.5)) == mpi(-0.8414709848078966159, 0.47942553860420300538)
214
+ assert mpi(-0.8414709848078966159, 1.00000000000000002e-100) in sin(mpi(-1,1e-100))
215
+ assert mpi(-2.00000000000000004e-100, 1.00000000000000002e-100) in sin(mpi(-2e-100,1e-100))
216
+ # Same interval
217
+ assert cos(mpi(2, 2.5))
218
+ assert cos(mpi(3.5, 4)) == mpi(-0.93645668729079634129, -0.65364362086361182946)
219
+ assert cos(mpi(5, 5.5)) == mpi(0.28366218546322624627, 0.70866977429126010168)
220
+ assert mpi(0.59847214410395654927, 0.90929742682568170942) in sin(mpi(2, 2.5))
221
+ assert sin(mpi(3.5, 4)) == mpi(-0.75680249530792831347, -0.35078322768961983646)
222
+ assert sin(mpi(5, 5.5)) == mpi(-0.95892427466313856499, -0.70554032557039181306)
223
+ # Higher roots
224
+ iv.dps = 55
225
+ w = 4*10**50 + mpi(0.5)
226
+ for p in [15, 40, 80]:
227
+ iv.dps = p
228
+ assert 0 in sin(4*mpi(pi))
229
+ assert 0 in sin(4*10**50*mpi(pi))
230
+ assert 0 in cos((4+0.5)*mpi(pi))
231
+ assert 0 in cos(w*mpi(pi))
232
+ assert 1 in cos(4*mpi(pi))
233
+ assert 1 in cos(4*10**50*mpi(pi))
234
+ iv.dps = 15
235
+ assert cos(mpi(2,inf)) == mpi(-1,1)
236
+ assert sin(mpi(2,inf)) == mpi(-1,1)
237
+ assert cos(mpi(-inf,2)) == mpi(-1,1)
238
+ assert sin(mpi(-inf,2)) == mpi(-1,1)
239
+ u = tan(mpi(0.5,1))
240
+ assert mpf(u.a).ae(mp.tan(0.5))
241
+ assert mpf(u.b).ae(mp.tan(1))
242
+ v = iv.cot(mpi(0.5,1))
243
+ assert mpf(v.a).ae(mp.cot(1))
244
+ assert mpf(v.b).ae(mp.cot(0.5))
245
+ # Sanity check of evaluation at n*pi and (n+1/2)*pi
246
+ for n in range(-5,7,2):
247
+ x = iv.cos(n*iv.pi)
248
+ assert -1 in x
249
+ assert x >= -1
250
+ assert x != -1
251
+ x = iv.sin((n+0.5)*iv.pi)
252
+ assert -1 in x
253
+ assert x >= -1
254
+ assert x != -1
255
+ for n in range(-6,8,2):
256
+ x = iv.cos(n*iv.pi)
257
+ assert 1 in x
258
+ assert x <= 1
259
+ if n:
260
+ assert x != 1
261
+ x = iv.sin((n+0.5)*iv.pi)
262
+ assert 1 in x
263
+ assert x <= 1
264
+ assert x != 1
265
+ for n in range(-6,7):
266
+ x = iv.cos((n+0.5)*iv.pi)
267
+ assert x.a < 0 < x.b
268
+ x = iv.sin(n*iv.pi)
269
+ if n:
270
+ assert x.a < 0 < x.b
271
+
272
+ def test_interval_complex():
273
+ # TODO: many more tests
274
+ iv.dps = 15
275
+ mp.dps = 15
276
+ assert iv.mpc(2,3) == 2+3j
277
+ assert iv.mpc(2,3) != 2+4j
278
+ assert iv.mpc(2,3) != 1+3j
279
+ assert 1+3j in iv.mpc([1,2],[3,4])
280
+ assert 2+5j not in iv.mpc([1,2],[3,4])
281
+ assert iv.mpc(1,2) + 1j == 1+3j
282
+ assert iv.mpc([1,2],[2,3]) + 2+3j == iv.mpc([3,4],[5,6])
283
+ assert iv.mpc([2,4],[4,8]) / 2 == iv.mpc([1,2],[2,4])
284
+ assert iv.mpc([1,2],[2,4]) * 2j == iv.mpc([-8,-4],[2,4])
285
+ assert iv.mpc([2,4],[4,8]) / 2j == iv.mpc([2,4],[-2,-1])
286
+ assert iv.exp(2+3j).ae(mp.exp(2+3j))
287
+ assert iv.log(2+3j).ae(mp.log(2+3j))
288
+ assert (iv.mpc(2,3) ** iv.mpc(0.5,2)).ae(mp.mpc(2,3) ** mp.mpc(0.5,2))
289
+ assert 1j in (iv.mpf(-1) ** 0.5)
290
+ assert 1j in (iv.mpc(-1) ** 0.5)
291
+ assert abs(iv.mpc(0)) == 0
292
+ assert abs(iv.mpc(inf)) == inf
293
+ assert abs(iv.mpc(3,4)) == 5
294
+ assert abs(iv.mpc(4)) == 4
295
+ assert abs(iv.mpc(0,4)) == 4
296
+ assert abs(iv.mpc(0,[2,3])) == iv.mpf([2,3])
297
+ assert abs(iv.mpc(0,[-3,2])) == iv.mpf([0,3])
298
+ assert abs(iv.mpc([3,5],[4,12])) == iv.mpf([5,13])
299
+ assert abs(iv.mpc([3,5],[-4,12])) == iv.mpf([3,13])
300
+ assert iv.mpc(2,3) ** 0 == 1
301
+ assert iv.mpc(2,3) ** 1 == (2+3j)
302
+ assert iv.mpc(2,3) ** 2 == (2+3j)**2
303
+ assert iv.mpc(2,3) ** 3 == (2+3j)**3
304
+ assert iv.mpc(2,3) ** 4 == (2+3j)**4
305
+ assert iv.mpc(2,3) ** 5 == (2+3j)**5
306
+ assert iv.mpc(2,2) ** (-1) == (2+2j) ** (-1)
307
+ assert iv.mpc(2,2) ** (-2) == (2+2j) ** (-2)
308
+ assert iv.cos(2).ae(mp.cos(2))
309
+ assert iv.sin(2).ae(mp.sin(2))
310
+ assert iv.cos(2+3j).ae(mp.cos(2+3j))
311
+ assert iv.sin(2+3j).ae(mp.sin(2+3j))
312
+
313
+ def test_interval_complex_arg():
314
+ mp.dps = 15
315
+ iv.dps = 15
316
+ assert iv.arg(3) == 0
317
+ assert iv.arg(0) == 0
318
+ assert iv.arg([0,3]) == 0
319
+ assert iv.arg(-3).ae(pi)
320
+ assert iv.arg(2+3j).ae(iv.arg(2+3j))
321
+ z = iv.mpc([-2,-1],[3,4])
322
+ t = iv.arg(z)
323
+ assert t.a.ae(mp.arg(-1+4j))
324
+ assert t.b.ae(mp.arg(-2+3j))
325
+ z = iv.mpc([-2,1],[3,4])
326
+ t = iv.arg(z)
327
+ assert t.a.ae(mp.arg(1+3j))
328
+ assert t.b.ae(mp.arg(-2+3j))
329
+ z = iv.mpc([1,2],[3,4])
330
+ t = iv.arg(z)
331
+ assert t.a.ae(mp.arg(2+3j))
332
+ assert t.b.ae(mp.arg(1+4j))
333
+ z = iv.mpc([1,2],[-2,3])
334
+ t = iv.arg(z)
335
+ assert t.a.ae(mp.arg(1-2j))
336
+ assert t.b.ae(mp.arg(1+3j))
337
+ z = iv.mpc([1,2],[-4,-3])
338
+ t = iv.arg(z)
339
+ assert t.a.ae(mp.arg(1-4j))
340
+ assert t.b.ae(mp.arg(2-3j))
341
+ z = iv.mpc([-1,2],[-4,-3])
342
+ t = iv.arg(z)
343
+ assert t.a.ae(mp.arg(-1-3j))
344
+ assert t.b.ae(mp.arg(2-3j))
345
+ z = iv.mpc([-2,-1],[-4,-3])
346
+ t = iv.arg(z)
347
+ assert t.a.ae(mp.arg(-2-3j))
348
+ assert t.b.ae(mp.arg(-1-4j))
349
+ z = iv.mpc([-2,-1],[-3,3])
350
+ t = iv.arg(z)
351
+ assert t.a.ae(-mp.pi)
352
+ assert t.b.ae(mp.pi)
353
+ z = iv.mpc([-2,2],[-3,3])
354
+ t = iv.arg(z)
355
+ assert t.a.ae(-mp.pi)
356
+ assert t.b.ae(mp.pi)
357
+
358
+ def test_interval_ae():
359
+ iv.dps = 15
360
+ x = iv.mpf([1,2])
361
+ assert x.ae(1) is None
362
+ assert x.ae(1.5) is None
363
+ assert x.ae(2) is None
364
+ assert x.ae(2.01) is False
365
+ assert x.ae(0.99) is False
366
+ x = iv.mpf(3.5)
367
+ assert x.ae(3.5) is True
368
+ assert x.ae(3.5+1e-15) is True
369
+ assert x.ae(3.5-1e-15) is True
370
+ assert x.ae(3.501) is False
371
+ assert x.ae(3.499) is False
372
+ assert x.ae(iv.mpf([3.5,3.501])) is None
373
+ assert x.ae(iv.mpf([3.5,4.5+1e-15])) is None
374
+
375
+ def test_interval_nstr():
376
+ iv.dps = n = 30
377
+ x = mpi(1, 2)
378
+ # FIXME: error_dps should not be necessary
379
+ assert iv.nstr(x, n, mode='plusminus', error_dps=6) == '1.5 +- 0.5'
380
+ assert iv.nstr(x, n, mode='plusminus', use_spaces=False, error_dps=6) == '1.5+-0.5'
381
+ assert iv.nstr(x, n, mode='percent') == '1.5 (33.33%)'
382
+ assert iv.nstr(x, n, mode='brackets', use_spaces=False) == '[1.0,2.0]'
383
+ assert iv.nstr(x, n, mode='brackets' , brackets=('<', '>')) == '<1.0, 2.0>'
384
+ x = mpi('5.2582327113062393041', '5.2582327113062749951')
385
+ assert iv.nstr(x, n, mode='diff') == '5.2582327113062[393041, 749951]'
386
+ assert iv.nstr(iv.cos(mpi(1)), n, mode='diff', use_spaces=False) == '0.54030230586813971740093660744[2955,3053]'
387
+ assert iv.nstr(mpi('1e123', '1e129'), n, mode='diff') == '[1.0e+123, 1.0e+129]'
388
+ exp = iv.exp
389
+ assert iv.nstr(iv.exp(mpi('5000.1')), n, mode='diff') == '3.2797365856787867069110487[0926, 1191]e+2171'
390
+ iv.dps = 15
391
+
392
+ def test_mpi_from_str():
393
+ iv.dps = 15
394
+ assert iv.convert('1.5 +- 0.5') == mpi(mpf('1.0'), mpf('2.0'))
395
+ assert mpi(1, 2) in iv.convert('1.5 (33.33333333333333333333333333333%)')
396
+ assert iv.convert('[1, 2]') == mpi(1, 2)
397
+ assert iv.convert('1[2, 3]') == mpi(12, 13)
398
+ assert iv.convert('1.[23,46]e-8') == mpi('1.23e-8', '1.46e-8')
399
+ assert iv.convert('12[3.4,5.9]e4') == mpi('123.4e+4', '125.9e4')
400
+
401
+ def test_interval_gamma():
402
+ mp.dps = 15
403
+ iv.dps = 15
404
+ # TODO: need many more tests
405
+ assert iv.rgamma(0) == 0
406
+ assert iv.fac(0) == 1
407
+ assert iv.fac(1) == 1
408
+ assert iv.fac(2) == 2
409
+ assert iv.fac(3) == 6
410
+ assert iv.gamma(0) == [-inf,inf]
411
+ assert iv.gamma(1) == 1
412
+ assert iv.gamma(2) == 1
413
+ assert iv.gamma(3) == 2
414
+ assert -3.5449077018110320546 in iv.gamma(-0.5)
415
+ assert iv.loggamma(1) == 0
416
+ assert iv.loggamma(2) == 0
417
+ assert 0.69314718055994530942 in iv.loggamma(3)
418
+ # Test tight log-gamma endpoints based on monotonicity
419
+ xs = [iv.mpc([2,3],[1,4]),
420
+ iv.mpc([2,3],[-4,-1]),
421
+ iv.mpc([2,3],[-1,4]),
422
+ iv.mpc([2,3],[-4,1]),
423
+ iv.mpc([2,3],[-4,4]),
424
+ iv.mpc([-3,-2],[2,4]),
425
+ iv.mpc([-3,-2],[-4,-2])]
426
+ for x in xs:
427
+ ys = [mp.loggamma(mp.mpc(x.a,x.c)),
428
+ mp.loggamma(mp.mpc(x.b,x.c)),
429
+ mp.loggamma(mp.mpc(x.a,x.d)),
430
+ mp.loggamma(mp.mpc(x.b,x.d))]
431
+ if 0 in x.imag:
432
+ ys += [mp.loggamma(x.a), mp.loggamma(x.b)]
433
+ min_real = min([y.real for y in ys])
434
+ max_real = max([y.real for y in ys])
435
+ min_imag = min([y.imag for y in ys])
436
+ max_imag = max([y.imag for y in ys])
437
+ z = iv.loggamma(x)
438
+ assert z.a.ae(min_real)
439
+ assert z.b.ae(max_real)
440
+ assert z.c.ae(min_imag)
441
+ assert z.d.ae(max_imag)
442
+
443
+ def test_interval_conversions():
444
+ mp.dps = 15
445
+ iv.dps = 15
446
+ for a, b in ((-0.0, 0), (0.0, 0.5), (1.0, 1), \
447
+ ('-inf', 20.5), ('-inf', float(sqrt(2)))):
448
+ r = mpi(a, b)
449
+ assert int(r.b) == int(b)
450
+ assert float(r.a) == float(a)
451
+ assert float(r.b) == float(b)
452
+ assert complex(r.a) == complex(a)
453
+ assert complex(r.b) == complex(b)
llmeval-env/lib/python3.10/site-packages/mpmath/tests/test_levin.py ADDED
@@ -0,0 +1,153 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/python
2
+ # -*- coding: utf-8 -*-
3
+
4
+ from mpmath import mp
5
+ from mpmath import libmp
6
+
7
+ xrange = libmp.backend.xrange
8
+
9
+ # Attention:
10
+ # These tests run with 15-20 decimal digits precision. For higher precision the
11
+ # working precision must be raised.
12
+
13
+ def test_levin_0():
14
+ mp.dps = 17
15
+ eps = mp.mpf(mp.eps)
16
+ with mp.extraprec(2 * mp.prec):
17
+ L = mp.levin(method = "levin", variant = "u")
18
+ S, s, n = [], 0, 1
19
+ while 1:
20
+ s += mp.one / (n * n)
21
+ n += 1
22
+ S.append(s)
23
+ v, e = L.update_psum(S)
24
+ if e < eps:
25
+ break
26
+ if n > 1000: raise RuntimeError("iteration limit exceeded")
27
+ eps = mp.exp(0.9 * mp.log(eps))
28
+ err = abs(v - mp.pi ** 2 / 6)
29
+ assert err < eps
30
+ w = mp.nsum(lambda n: 1/(n * n), [1, mp.inf], method = "levin", levin_variant = "u")
31
+ err = abs(v - w)
32
+ assert err < eps
33
+
34
+ def test_levin_1():
35
+ mp.dps = 17
36
+ eps = mp.mpf(mp.eps)
37
+ with mp.extraprec(2 * mp.prec):
38
+ L = mp.levin(method = "levin", variant = "v")
39
+ A, n = [], 1
40
+ while 1:
41
+ s = mp.mpf(n) ** (2 + 3j)
42
+ n += 1
43
+ A.append(s)
44
+ v, e = L.update(A)
45
+ if e < eps:
46
+ break
47
+ if n > 1000: raise RuntimeError("iteration limit exceeded")
48
+ eps = mp.exp(0.9 * mp.log(eps))
49
+ err = abs(v - mp.zeta(-2-3j))
50
+ assert err < eps
51
+ w = mp.nsum(lambda n: n ** (2 + 3j), [1, mp.inf], method = "levin", levin_variant = "v")
52
+ err = abs(v - w)
53
+ assert err < eps
54
+
55
+ def test_levin_2():
56
+ # [2] A. Sidi - "Pratical Extrapolation Methods" p.373
57
+ mp.dps = 17
58
+ z=mp.mpf(10)
59
+ eps = mp.mpf(mp.eps)
60
+ with mp.extraprec(2 * mp.prec):
61
+ L = mp.levin(method = "sidi", variant = "t")
62
+ n = 0
63
+ while 1:
64
+ s = (-1)**n * mp.fac(n) * z ** (-n)
65
+ v, e = L.step(s)
66
+ n += 1
67
+ if e < eps:
68
+ break
69
+ if n > 1000: raise RuntimeError("iteration limit exceeded")
70
+ eps = mp.exp(0.9 * mp.log(eps))
71
+ exact = mp.quad(lambda x: mp.exp(-x)/(1+x/z),[0,mp.inf])
72
+ # there is also a symbolic expression for the integral:
73
+ # exact = z * mp.exp(z) * mp.expint(1,z)
74
+ err = abs(v - exact)
75
+ assert err < eps
76
+ w = mp.nsum(lambda n: (-1) ** n * mp.fac(n) * z ** (-n), [0, mp.inf], method = "sidi", levin_variant = "t")
77
+ assert err < eps
78
+
79
+ def test_levin_3():
80
+ mp.dps = 17
81
+ z=mp.mpf(2)
82
+ eps = mp.mpf(mp.eps)
83
+ with mp.extraprec(7*mp.prec): # we need copious amount of precision to sum this highly divergent series
84
+ L = mp.levin(method = "levin", variant = "t")
85
+ n, s = 0, 0
86
+ while 1:
87
+ s += (-z)**n * mp.fac(4 * n) / (mp.fac(n) * mp.fac(2 * n) * (4 ** n))
88
+ n += 1
89
+ v, e = L.step_psum(s)
90
+ if e < eps:
91
+ break
92
+ if n > 1000: raise RuntimeError("iteration limit exceeded")
93
+ eps = mp.exp(0.8 * mp.log(eps))
94
+ exact = mp.quad(lambda x: mp.exp( -x * x / 2 - z * x ** 4), [0,mp.inf]) * 2 / mp.sqrt(2 * mp.pi)
95
+ # there is also a symbolic expression for the integral:
96
+ # exact = mp.exp(mp.one / (32 * z)) * mp.besselk(mp.one / 4, mp.one / (32 * z)) / (4 * mp.sqrt(z * mp.pi))
97
+ err = abs(v - exact)
98
+ assert err < eps
99
+ w = mp.nsum(lambda n: (-z)**n * mp.fac(4 * n) / (mp.fac(n) * mp.fac(2 * n) * (4 ** n)), [0, mp.inf], method = "levin", levin_variant = "t", workprec = 8*mp.prec, steps = [2] + [1 for x in xrange(1000)])
100
+ err = abs(v - w)
101
+ assert err < eps
102
+
103
+ def test_levin_nsum():
104
+ mp.dps = 17
105
+
106
+ with mp.extraprec(mp.prec):
107
+ z = mp.mpf(10) ** (-10)
108
+ a = mp.nsum(lambda n: n**(-(1+z)), [1, mp.inf], method = "l") - 1 / z
109
+ assert abs(a - mp.euler) < 1e-10
110
+
111
+ eps = mp.exp(0.8 * mp.log(mp.eps))
112
+
113
+ a = mp.nsum(lambda n: (-1)**(n-1) / n, [1, mp.inf], method = "sidi")
114
+ assert abs(a - mp.log(2)) < eps
115
+
116
+ z = 2 + 1j
117
+ f = lambda n: mp.rf(2 / mp.mpf(3), n) * mp.rf(4 / mp.mpf(3), n) * z**n / (mp.rf(1 / mp.mpf(3), n) * mp.fac(n))
118
+ v = mp.nsum(f, [0, mp.inf], method = "levin", steps = [10 for x in xrange(1000)])
119
+ exact = mp.hyp2f1(2 / mp.mpf(3), 4 / mp.mpf(3), 1 / mp.mpf(3), z)
120
+ assert abs(exact - v) < eps
121
+
122
+ def test_cohen_alt_0():
123
+ mp.dps = 17
124
+ AC = mp.cohen_alt()
125
+ S, s, n = [], 0, 1
126
+ while 1:
127
+ s += -((-1) ** n) * mp.one / (n * n)
128
+ n += 1
129
+ S.append(s)
130
+ v, e = AC.update_psum(S)
131
+ if e < mp.eps:
132
+ break
133
+ if n > 1000: raise RuntimeError("iteration limit exceeded")
134
+ eps = mp.exp(0.9 * mp.log(mp.eps))
135
+ err = abs(v - mp.pi ** 2 / 12)
136
+ assert err < eps
137
+
138
+ def test_cohen_alt_1():
139
+ mp.dps = 17
140
+ A = []
141
+ AC = mp.cohen_alt()
142
+ n = 1
143
+ while 1:
144
+ A.append( mp.loggamma(1 + mp.one / (2 * n - 1)))
145
+ A.append(-mp.loggamma(1 + mp.one / (2 * n)))
146
+ n += 1
147
+ v, e = AC.update(A)
148
+ if e < mp.eps:
149
+ break
150
+ if n > 1000: raise RuntimeError("iteration limit exceeded")
151
+ v = mp.exp(v)
152
+ err = abs(v - 1.06215090557106)
153
+ assert err < 1e-12
llmeval-env/lib/python3.10/site-packages/mpmath/tests/test_linalg.py ADDED
@@ -0,0 +1,332 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # TODO: don't use round
2
+
3
+ from __future__ import division
4
+
5
+ import pytest
6
+ from mpmath import *
7
+ xrange = libmp.backend.xrange
8
+
9
+ # XXX: these shouldn't be visible(?)
10
+ LU_decomp = mp.LU_decomp
11
+ L_solve = mp.L_solve
12
+ U_solve = mp.U_solve
13
+ householder = mp.householder
14
+ improve_solution = mp.improve_solution
15
+
16
+ A1 = matrix([[3, 1, 6],
17
+ [2, 1, 3],
18
+ [1, 1, 1]])
19
+ b1 = [2, 7, 4]
20
+
21
+ A2 = matrix([[ 2, -1, -1, 2],
22
+ [ 6, -2, 3, -1],
23
+ [-4, 2, 3, -2],
24
+ [ 2, 0, 4, -3]])
25
+ b2 = [3, -3, -2, -1]
26
+
27
+ A3 = matrix([[ 1, 0, -1, -1, 0],
28
+ [ 0, 1, 1, 0, -1],
29
+ [ 4, -5, 2, 0, 0],
30
+ [ 0, 0, -2, 9,-12],
31
+ [ 0, 5, 0, 0, 12]])
32
+ b3 = [0, 0, 0, 0, 50]
33
+
34
+ A4 = matrix([[10.235, -4.56, 0., -0.035, 5.67],
35
+ [-2.463, 1.27, 3.97, -8.63, 1.08],
36
+ [-6.58, 0.86, -0.257, 9.32, -43.6 ],
37
+ [ 9.83, 7.39, -17.25, 0.036, 24.86],
38
+ [-9.31, 34.9, 78.56, 1.07, 65.8 ]])
39
+ b4 = [8.95, 20.54, 7.42, 5.60, 58.43]
40
+
41
+ A5 = matrix([[ 1, 2, -4],
42
+ [-2, -3, 5],
43
+ [ 3, 5, -8]])
44
+
45
+ A6 = matrix([[ 1.377360, 2.481400, 5.359190],
46
+ [ 2.679280, -1.229560, 25.560210],
47
+ [-1.225280+1.e6, 9.910180, -35.049900-1.e6]])
48
+ b6 = [23.500000, -15.760000, 2.340000]
49
+
50
+ A7 = matrix([[1, -0.5],
51
+ [2, 1],
52
+ [-2, 6]])
53
+ b7 = [3, 2, -4]
54
+
55
+ A8 = matrix([[1, 2, 3],
56
+ [-1, 0, 1],
57
+ [-1, -2, -1],
58
+ [1, 0, -1]])
59
+ b8 = [1, 2, 3, 4]
60
+
61
+ A9 = matrix([[ 4, 2, -2],
62
+ [ 2, 5, -4],
63
+ [-2, -4, 5.5]])
64
+ b9 = [10, 16, -15.5]
65
+
66
+ A10 = matrix([[1.0 + 1.0j, 2.0, 2.0],
67
+ [4.0, 5.0, 6.0],
68
+ [7.0, 8.0, 9.0]])
69
+ b10 = [1.0, 1.0 + 1.0j, 1.0]
70
+
71
+
72
+ def test_LU_decomp():
73
+ A = A3.copy()
74
+ b = b3
75
+ A, p = LU_decomp(A)
76
+ y = L_solve(A, b, p)
77
+ x = U_solve(A, y)
78
+ assert p == [2, 1, 2, 3]
79
+ assert [round(i, 14) for i in x] == [3.78953107960742, 2.9989094874591098,
80
+ -0.081788440567070006, 3.8713195201744801, 2.9171210468920399]
81
+ A = A4.copy()
82
+ b = b4
83
+ A, p = LU_decomp(A)
84
+ y = L_solve(A, b, p)
85
+ x = U_solve(A, y)
86
+ assert p == [0, 3, 4, 3]
87
+ assert [round(i, 14) for i in x] == [2.6383625899619201, 2.6643834462368399,
88
+ 0.79208015947958998, -2.5088376454101899, -1.0567657691375001]
89
+ A = randmatrix(3)
90
+ bak = A.copy()
91
+ LU_decomp(A, overwrite=1)
92
+ assert A != bak
93
+
94
+ def test_inverse():
95
+ for A in [A1, A2, A5]:
96
+ inv = inverse(A)
97
+ assert mnorm(A*inv - eye(A.rows), 1) < 1.e-14
98
+
99
+ def test_householder():
100
+ mp.dps = 15
101
+ A, b = A8, b8
102
+ H, p, x, r = householder(extend(A, b))
103
+ assert H == matrix(
104
+ [[mpf('3.0'), mpf('-2.0'), mpf('-1.0'), 0],
105
+ [-1.0,mpf('3.333333333333333'),mpf('-2.9999999999999991'),mpf('2.0')],
106
+ [-1.0, mpf('-0.66666666666666674'),mpf('2.8142135623730948'),
107
+ mpf('-2.8284271247461898')],
108
+ [1.0, mpf('-1.3333333333333333'),mpf('-0.20000000000000018'),
109
+ mpf('4.2426406871192857')]])
110
+ assert p == [-2, -2, mpf('-1.4142135623730949')]
111
+ assert round(norm(r, 2), 10) == 4.2426406870999998
112
+
113
+ y = [102.102, 58.344, 36.463, 24.310, 17.017, 12.376, 9.282, 7.140, 5.610,
114
+ 4.488, 3.6465, 3.003]
115
+
116
+ def coeff(n):
117
+ # similiar to Hilbert matrix
118
+ A = []
119
+ for i in range(1, 13):
120
+ A.append([1. / (i + j - 1) for j in range(1, n + 1)])
121
+ return matrix(A)
122
+
123
+ residuals = []
124
+ refres = []
125
+ for n in range(2, 7):
126
+ A = coeff(n)
127
+ H, p, x, r = householder(extend(A, y))
128
+ x = matrix(x)
129
+ y = matrix(y)
130
+ residuals.append(norm(r, 2))
131
+ refres.append(norm(residual(A, x, y), 2))
132
+ assert [round(res, 10) for res in residuals] == [15.1733888877,
133
+ 0.82378073210000002, 0.302645887, 0.0260109244,
134
+ 0.00058653999999999998]
135
+ assert norm(matrix(residuals) - matrix(refres), inf) < 1.e-13
136
+
137
+ def hilbert_cmplx(n):
138
+ # Complexified Hilbert matrix
139
+ A = hilbert(2*n,n)
140
+ v = randmatrix(2*n, 2, min=-1, max=1)
141
+ v = v.apply(lambda x: exp(1J*pi()*x))
142
+ A = diag(v[:,0])*A*diag(v[:n,1])
143
+ return A
144
+
145
+ residuals_cmplx = []
146
+ refres_cmplx = []
147
+ for n in range(2, 10):
148
+ A = hilbert_cmplx(n)
149
+ H, p, x, r = householder(A.copy())
150
+ residuals_cmplx.append(norm(r, 2))
151
+ refres_cmplx.append(norm(residual(A[:,:n-1], x, A[:,n-1]), 2))
152
+ assert norm(matrix(residuals_cmplx) - matrix(refres_cmplx), inf) < 1.e-13
153
+
154
+ def test_factorization():
155
+ A = randmatrix(5)
156
+ P, L, U = lu(A)
157
+ assert mnorm(P*A - L*U, 1) < 1.e-15
158
+
159
+ def test_solve():
160
+ assert norm(residual(A6, lu_solve(A6, b6), b6), inf) < 1.e-10
161
+ assert norm(residual(A7, lu_solve(A7, b7), b7), inf) < 1.5
162
+ assert norm(residual(A8, lu_solve(A8, b8), b8), inf) <= 3 + 1.e-10
163
+ assert norm(residual(A6, qr_solve(A6, b6)[0], b6), inf) < 1.e-10
164
+ assert norm(residual(A7, qr_solve(A7, b7)[0], b7), inf) < 1.5
165
+ assert norm(residual(A8, qr_solve(A8, b8)[0], b8), 2) <= 4.3
166
+ assert norm(residual(A10, lu_solve(A10, b10), b10), 2) < 1.e-10
167
+ assert norm(residual(A10, qr_solve(A10, b10)[0], b10), 2) < 1.e-10
168
+
169
+ def test_solve_overdet_complex():
170
+ A = matrix([[1, 2j], [3, 4j], [5, 6]])
171
+ b = matrix([1 + j, 2, -j])
172
+ assert norm(residual(A, lu_solve(A, b), b)) < 1.0208
173
+
174
+ def test_singular():
175
+ mp.dps = 15
176
+ A = [[5.6, 1.2], [7./15, .1]]
177
+ B = repr(zeros(2))
178
+ b = [1, 2]
179
+ for i in ['lu_solve(%s, %s)' % (A, b), 'lu_solve(%s, %s)' % (B, b),
180
+ 'qr_solve(%s, %s)' % (A, b), 'qr_solve(%s, %s)' % (B, b)]:
181
+ pytest.raises((ZeroDivisionError, ValueError), lambda: eval(i))
182
+
183
+ def test_cholesky():
184
+ assert fp.cholesky(fp.matrix(A9)) == fp.matrix([[2, 0, 0], [1, 2, 0], [-1, -3/2, 3/2]])
185
+ x = fp.cholesky_solve(A9, b9)
186
+ assert fp.norm(fp.residual(A9, x, b9), fp.inf) == 0
187
+
188
+ def test_det():
189
+ assert det(A1) == 1
190
+ assert round(det(A2), 14) == 8
191
+ assert round(det(A3)) == 1834
192
+ assert round(det(A4)) == 4443376
193
+ assert det(A5) == 1
194
+ assert round(det(A6)) == 78356463
195
+ assert det(zeros(3)) == 0
196
+
197
+ def test_cond():
198
+ mp.dps = 15
199
+ A = matrix([[1.2969, 0.8648], [0.2161, 0.1441]])
200
+ assert cond(A, lambda x: mnorm(x,1)) == mpf('327065209.73817754')
201
+ assert cond(A, lambda x: mnorm(x,inf)) == mpf('327065209.73817754')
202
+ assert cond(A, lambda x: mnorm(x,'F')) == mpf('249729266.80008656')
203
+
204
+ @extradps(50)
205
+ def test_precision():
206
+ A = randmatrix(10, 10)
207
+ assert mnorm(inverse(inverse(A)) - A, 1) < 1.e-45
208
+
209
+ def test_interval_matrix():
210
+ mp.dps = 15
211
+ iv.dps = 15
212
+ a = iv.matrix([['0.1','0.3','1.0'],['7.1','5.5','4.8'],['3.2','4.4','5.6']])
213
+ b = iv.matrix(['4','0.6','0.5'])
214
+ c = iv.lu_solve(a, b)
215
+ assert c[0].delta < 1e-13
216
+ assert c[1].delta < 1e-13
217
+ assert c[2].delta < 1e-13
218
+ assert 5.25823271130625686059275 in c[0]
219
+ assert -13.155049396267837541163 in c[1]
220
+ assert 7.42069154774972557628979 in c[2]
221
+
222
+ def test_LU_cache():
223
+ A = randmatrix(3)
224
+ LU = LU_decomp(A)
225
+ assert A._LU == LU_decomp(A)
226
+ A[0,0] = -1000
227
+ assert A._LU is None
228
+
229
+ def test_improve_solution():
230
+ A = randmatrix(5, min=1e-20, max=1e20)
231
+ b = randmatrix(5, 1, min=-1000, max=1000)
232
+ x1 = lu_solve(A, b) + randmatrix(5, 1, min=-1e-5, max=1.e-5)
233
+ x2 = improve_solution(A, x1, b)
234
+ assert norm(residual(A, x2, b), 2) < norm(residual(A, x1, b), 2)
235
+
236
+ def test_exp_pade():
237
+ for i in range(3):
238
+ dps = 15
239
+ extra = 15
240
+ mp.dps = dps + extra
241
+ dm = 0
242
+ N = 3
243
+ dg = range(1,N+1)
244
+ a = diag(dg)
245
+ expa = diag([exp(x) for x in dg])
246
+ # choose a random matrix not close to be singular
247
+ # to avoid adding too much extra precision in computing
248
+ # m**-1 * M * m
249
+ while abs(dm) < 0.01:
250
+ m = randmatrix(N)
251
+ dm = det(m)
252
+ m = m/dm
253
+ a1 = m**-1 * a * m
254
+ e2 = m**-1 * expa * m
255
+ mp.dps = dps
256
+ e1 = expm(a1, method='pade')
257
+ mp.dps = dps + extra
258
+ d = e2 - e1
259
+ #print d
260
+ mp.dps = dps
261
+ assert norm(d, inf).ae(0)
262
+ mp.dps = 15
263
+
264
+ def test_qr():
265
+ mp.dps = 15 # used default value for dps
266
+ lowlimit = -9 # lower limit of matrix element value
267
+ uplimit = 9 # uppter limit of matrix element value
268
+ maxm = 4 # max matrix size
269
+ flg = False # toggle to create real vs complex matrix
270
+ zero = mpf('0.0')
271
+
272
+ for k in xrange(0,10):
273
+ exdps = 0
274
+ mode = 'full'
275
+ flg = bool(k % 2)
276
+
277
+ # generate arbitrary matrix size (2 to maxm)
278
+ num1 = nint(maxm*rand())
279
+ num2 = nint(maxm*rand())
280
+ m = int(max(num1, num2))
281
+ n = int(min(num1, num2))
282
+
283
+ # create matrix
284
+ A = mp.matrix(m,n)
285
+
286
+ # populate matrix values with arbitrary integers
287
+ if flg:
288
+ flg = False
289
+ dtype = 'complex'
290
+ for j in xrange(0,n):
291
+ for i in xrange(0,m):
292
+ val = nint(lowlimit + (uplimit-lowlimit)*rand())
293
+ val2 = nint(lowlimit + (uplimit-lowlimit)*rand())
294
+ A[i,j] = mpc(val, val2)
295
+ else:
296
+ flg = True
297
+ dtype = 'real'
298
+ for j in xrange(0,n):
299
+ for i in xrange(0,m):
300
+ val = nint(lowlimit + (uplimit-lowlimit)*rand())
301
+ A[i,j] = mpf(val)
302
+
303
+ # perform A -> QR decomposition
304
+ Q, R = qr(A, mode, edps = exdps)
305
+
306
+ #print('\n\n A = \n', nstr(A, 4))
307
+ #print('\n Q = \n', nstr(Q, 4))
308
+ #print('\n R = \n', nstr(R, 4))
309
+ #print('\n Q*R = \n', nstr(Q*R, 4))
310
+
311
+ maxnorm = mpf('1.0E-11')
312
+ n1 = norm(A - Q * R)
313
+ #print '\n Norm of A - Q * R = ', n1
314
+ assert n1 <= maxnorm
315
+
316
+ if dtype == 'real':
317
+ n1 = norm(eye(m) - Q.T * Q)
318
+ #print ' Norm of I - Q.T * Q = ', n1
319
+ assert n1 <= maxnorm
320
+
321
+ n1 = norm(eye(m) - Q * Q.T)
322
+ #print ' Norm of I - Q * Q.T = ', n1
323
+ assert n1 <= maxnorm
324
+
325
+ if dtype == 'complex':
326
+ n1 = norm(eye(m) - Q.T * Q.conjugate())
327
+ #print ' Norm of I - Q.T * Q.conjugate() = ', n1
328
+ assert n1 <= maxnorm
329
+
330
+ n1 = norm(eye(m) - Q.conjugate() * Q.T)
331
+ #print ' Norm of I - Q.conjugate() * Q.T = ', n1
332
+ assert n1 <= maxnorm
llmeval-env/lib/python3.10/site-packages/mpmath/tests/test_matrices.py ADDED
@@ -0,0 +1,253 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pytest
2
+ import sys
3
+ from mpmath import *
4
+
5
+ def test_matrix_basic():
6
+ A1 = matrix(3)
7
+ for i in range(3):
8
+ A1[i,i] = 1
9
+ assert A1 == eye(3)
10
+ assert A1 == matrix(A1)
11
+ A2 = matrix(3, 2)
12
+ assert not A2._matrix__data
13
+ A3 = matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
14
+ assert list(A3) == list(range(1, 10))
15
+ A3[1,1] = 0
16
+ assert not (1, 1) in A3._matrix__data
17
+ A4 = matrix([[1, 2, 3], [4, 5, 6]])
18
+ A5 = matrix([[6, -1], [3, 2], [0, -3]])
19
+ assert A4 * A5 == matrix([[12, -6], [39, -12]])
20
+ assert A1 * A3 == A3 * A1 == A3
21
+ pytest.raises(ValueError, lambda: A2*A2)
22
+ l = [[10, 20, 30], [40, 0, 60], [70, 80, 90]]
23
+ A6 = matrix(l)
24
+ assert A6.tolist() == l
25
+ assert A6 == eval(repr(A6))
26
+ A6 = fp.matrix(A6)
27
+ assert A6 == eval(repr(A6))
28
+ assert A6*1j == eval(repr(A6*1j))
29
+ assert A3 * 10 == 10 * A3 == A6
30
+ assert A2.rows == 3
31
+ assert A2.cols == 2
32
+ A3.rows = 2
33
+ A3.cols = 2
34
+ assert len(A3._matrix__data) == 3
35
+ assert A4 + A4 == 2*A4
36
+ pytest.raises(ValueError, lambda: A4 + A2)
37
+ assert sum(A1 - A1) == 0
38
+ A7 = matrix([[1, 2], [3, 4], [5, 6], [7, 8]])
39
+ x = matrix([10, -10])
40
+ assert A7*x == matrix([-10, -10, -10, -10])
41
+ A8 = ones(5)
42
+ assert sum((A8 + 1) - (2 - zeros(5))) == 0
43
+ assert (1 + ones(4)) / 2 - 1 == zeros(4)
44
+ assert eye(3)**10 == eye(3)
45
+ pytest.raises(ValueError, lambda: A7**2)
46
+ A9 = randmatrix(3)
47
+ A10 = matrix(A9)
48
+ A9[0,0] = -100
49
+ assert A9 != A10
50
+ assert nstr(A9)
51
+
52
+ def test_matmul():
53
+ """
54
+ Test the PEP465 "@" matrix multiplication syntax.
55
+ To avoid syntax errors when importing this file in Python 3.5 and below, we have to use exec() - sorry for that.
56
+ """
57
+ # TODO remove exec() wrapper as soon as we drop support for Python <= 3.5
58
+ if sys.hexversion < 0x30500f0:
59
+ # we are on Python < 3.5
60
+ pytest.skip("'@' (__matmul__) is only supported in Python 3.5 or newer")
61
+ A4 = matrix([[1, 2, 3], [4, 5, 6]])
62
+ A5 = matrix([[6, -1], [3, 2], [0, -3]])
63
+ exec("assert A4 @ A5 == A4 * A5")
64
+
65
+ def test_matrix_slices():
66
+ A = matrix([ [1, 2, 3],
67
+ [4, 5 ,6],
68
+ [7, 8 ,9]])
69
+ V = matrix([1,2,3,4,5])
70
+
71
+ # Get slice
72
+ assert A[:,:] == A
73
+ assert A[:,1] == matrix([[2],[5],[8]])
74
+ assert A[2,:] == matrix([[7, 8 ,9]])
75
+ assert A[1:3,1:3] == matrix([[5,6],[8,9]])
76
+ assert V[2:4] == matrix([3,4])
77
+ pytest.raises(IndexError, lambda: A[:,1:6])
78
+
79
+ # Assign slice with matrix
80
+ A1 = matrix(3)
81
+ A1[:,:] = A
82
+ assert A1[:,:] == matrix([[1, 2, 3],
83
+ [4, 5 ,6],
84
+ [7, 8 ,9]])
85
+ A1[0,:] = matrix([[10, 11, 12]])
86
+ assert A1 == matrix([ [10, 11, 12],
87
+ [4, 5 ,6],
88
+ [7, 8 ,9]])
89
+ A1[:,2] = matrix([[13], [14], [15]])
90
+ assert A1 == matrix([ [10, 11, 13],
91
+ [4, 5 ,14],
92
+ [7, 8 ,15]])
93
+ A1[:2,:2] = matrix([[16, 17], [18 , 19]])
94
+ assert A1 == matrix([ [16, 17, 13],
95
+ [18, 19 ,14],
96
+ [7, 8 ,15]])
97
+ V[1:3] = 10
98
+ assert V == matrix([1,10,10,4,5])
99
+ with pytest.raises(ValueError):
100
+ A1[2,:] = A[:,1]
101
+
102
+ with pytest.raises(IndexError):
103
+ A1[2,1:20] = A[:,:]
104
+
105
+ # Assign slice with scalar
106
+ A1[:,2] = 10
107
+ assert A1 == matrix([ [16, 17, 10],
108
+ [18, 19 ,10],
109
+ [7, 8 ,10]])
110
+ A1[:,:] = 40
111
+ for x in A1:
112
+ assert x == 40
113
+
114
+
115
+ def test_matrix_power():
116
+ A = matrix([[1, 2], [3, 4]])
117
+ assert A**2 == A*A
118
+ assert A**3 == A*A*A
119
+ assert A**-1 == inverse(A)
120
+ assert A**-2 == inverse(A*A)
121
+
122
+ def test_matrix_transform():
123
+ A = matrix([[1, 2], [3, 4], [5, 6]])
124
+ assert A.T == A.transpose() == matrix([[1, 3, 5], [2, 4, 6]])
125
+ swap_row(A, 1, 2)
126
+ assert A == matrix([[1, 2], [5, 6], [3, 4]])
127
+ l = [1, 2]
128
+ swap_row(l, 0, 1)
129
+ assert l == [2, 1]
130
+ assert extend(eye(3), [1,2,3]) == matrix([[1,0,0,1],[0,1,0,2],[0,0,1,3]])
131
+
132
+ def test_matrix_conjugate():
133
+ A = matrix([[1 + j, 0], [2, j]])
134
+ assert A.conjugate() == matrix([[mpc(1, -1), 0], [2, mpc(0, -1)]])
135
+ assert A.transpose_conj() == A.H == matrix([[mpc(1, -1), 2],
136
+ [0, mpc(0, -1)]])
137
+
138
+ def test_matrix_creation():
139
+ assert diag([1, 2, 3]) == matrix([[1, 0, 0], [0, 2, 0], [0, 0, 3]])
140
+ A1 = ones(2, 3)
141
+ assert A1.rows == 2 and A1.cols == 3
142
+ for a in A1:
143
+ assert a == 1
144
+ A2 = zeros(3, 2)
145
+ assert A2.rows == 3 and A2.cols == 2
146
+ for a in A2:
147
+ assert a == 0
148
+ assert randmatrix(10) != randmatrix(10)
149
+ one = mpf(1)
150
+ assert hilbert(3) == matrix([[one, one/2, one/3],
151
+ [one/2, one/3, one/4],
152
+ [one/3, one/4, one/5]])
153
+
154
+ def test_norms():
155
+ # matrix norms
156
+ A = matrix([[1, -2], [-3, -1], [2, 1]])
157
+ assert mnorm(A,1) == 6
158
+ assert mnorm(A,inf) == 4
159
+ assert mnorm(A,'F') == sqrt(20)
160
+ # vector norms
161
+ assert norm(-3) == 3
162
+ x = [1, -2, 7, -12]
163
+ assert norm(x, 1) == 22
164
+ assert round(norm(x, 2), 10) == 14.0712472795
165
+ assert round(norm(x, 10), 10) == 12.0054633727
166
+ assert norm(x, inf) == 12
167
+
168
+ def test_vector():
169
+ x = matrix([0, 1, 2, 3, 4])
170
+ assert x == matrix([[0], [1], [2], [3], [4]])
171
+ assert x[3] == 3
172
+ assert len(x._matrix__data) == 4
173
+ assert list(x) == list(range(5))
174
+ x[0] = -10
175
+ x[4] = 0
176
+ assert x[0] == -10
177
+ assert len(x) == len(x.T) == 5
178
+ assert x.T*x == matrix([[114]])
179
+
180
+ def test_matrix_copy():
181
+ A = ones(6)
182
+ B = A.copy()
183
+ C = +A
184
+ assert A == B
185
+ assert A == C
186
+ B[0,0] = 0
187
+ assert A != B
188
+ C[0,0] = 42
189
+ assert A != C
190
+
191
+ def test_matrix_numpy():
192
+ try:
193
+ import numpy
194
+ except ImportError:
195
+ return
196
+ l = [[1, 2], [3, 4], [5, 6]]
197
+ a = numpy.array(l)
198
+ assert matrix(l) == matrix(a)
199
+
200
+ def test_interval_matrix_scalar_mult():
201
+ """Multiplication of iv.matrix and any scalar type"""
202
+ a = mpi(-1, 1)
203
+ b = a + a * 2j
204
+ c = mpf(42)
205
+ d = c + c * 2j
206
+ e = 1.234
207
+ f = fp.convert(e)
208
+ g = e + e * 3j
209
+ h = fp.convert(g)
210
+ M = iv.ones(1)
211
+ for x in [a, b, c, d, e, f, g, h]:
212
+ assert x * M == iv.matrix([x])
213
+ assert M * x == iv.matrix([x])
214
+
215
+ @pytest.mark.xfail()
216
+ def test_interval_matrix_matrix_mult():
217
+ """Multiplication of iv.matrix and other matrix types"""
218
+ A = ones(1)
219
+ B = fp.ones(1)
220
+ M = iv.ones(1)
221
+ for X in [A, B, M]:
222
+ assert X * M == iv.matrix(X)
223
+ assert X * M == X
224
+ assert M * X == iv.matrix(X)
225
+ assert M * X == X
226
+
227
+ def test_matrix_conversion_to_iv():
228
+ # Test that matrices with foreign datatypes are properly converted
229
+ for other_type_eye in [eye(3), fp.eye(3), iv.eye(3)]:
230
+ A = iv.matrix(other_type_eye)
231
+ B = iv.eye(3)
232
+ assert type(A[0,0]) == type(B[0,0])
233
+ assert A.tolist() == B.tolist()
234
+
235
+ def test_interval_matrix_mult_bug():
236
+ # regression test for interval matrix multiplication:
237
+ # result must be nonzero-width and contain the exact result
238
+ x = convert('1.00000000000001') # note: this is implicitly rounded to some near mpf float value
239
+ A = matrix([[x]])
240
+ B = iv.matrix(A)
241
+ C = iv.matrix([[x]])
242
+ assert B == C
243
+ B = B * B
244
+ C = C * C
245
+ assert B == C
246
+ assert B[0, 0].delta > 1e-16
247
+ assert B[0, 0].delta < 3e-16
248
+ assert C[0, 0].delta > 1e-16
249
+ assert C[0, 0].delta < 3e-16
250
+ assert mp.mpf('1.00000000000001998401444325291756783368705994138804689654') in B[0, 0]
251
+ assert mp.mpf('1.00000000000001998401444325291756783368705994138804689654') in C[0, 0]
252
+ # the following caused an error before the bug was fixed
253
+ assert iv.matrix(mp.eye(2)) * (iv.ones(2) + mpi(1, 2)) == iv.matrix([[mpi(2, 3), mpi(2, 3)], [mpi(2, 3), mpi(2, 3)]])
llmeval-env/lib/python3.10/site-packages/mpmath/tests/test_mpmath.py ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ from mpmath.libmp import *
2
+ from mpmath import *
3
+
4
+ def test_newstyle_classes():
5
+ for cls in [mp, fp, iv, mpf, mpc]:
6
+ for s in cls.__class__.__mro__:
7
+ assert isinstance(s, type)
llmeval-env/lib/python3.10/site-packages/mpmath/tests/test_ode.py ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #from mpmath.calculus import ODE_step_euler, ODE_step_rk4, odeint, arange
2
+ from mpmath import odefun, cos, sin, mpf, sinc, mp
3
+
4
+ '''
5
+ solvers = [ODE_step_euler, ODE_step_rk4]
6
+
7
+ def test_ode1():
8
+ """
9
+ Let's solve:
10
+
11
+ x'' + w**2 * x = 0
12
+
13
+ i.e. x1 = x, x2 = x1':
14
+
15
+ x1' = x2
16
+ x2' = -x1
17
+ """
18
+ def derivs((x1, x2), t):
19
+ return x2, -x1
20
+
21
+ for solver in solvers:
22
+ t = arange(0, 3.1415926, 0.005)
23
+ sol = odeint(derivs, (0., 1.), t, solver)
24
+ x1 = [a[0] for a in sol]
25
+ x2 = [a[1] for a in sol]
26
+ # the result is x1 = sin(t), x2 = cos(t)
27
+ # let's just check the end points for t = pi
28
+ assert abs(x1[-1]) < 1e-2
29
+ assert abs(x2[-1] - (-1)) < 1e-2
30
+
31
+ def test_ode2():
32
+ """
33
+ Let's solve:
34
+
35
+ x' - x = 0
36
+
37
+ i.e. x = exp(x)
38
+
39
+ """
40
+ def derivs((x), t):
41
+ return x
42
+
43
+ for solver in solvers:
44
+ t = arange(0, 1, 1e-3)
45
+ sol = odeint(derivs, (1.,), t, solver)
46
+ x = [a[0] for a in sol]
47
+ # the result is x = exp(t)
48
+ # let's just check the end point for t = 1, i.e. x = e
49
+ assert abs(x[-1] - 2.718281828) < 1e-2
50
+ '''
51
+
52
+ def test_odefun_rational():
53
+ mp.dps = 15
54
+ # A rational function
55
+ f = lambda t: 1/(1+mpf(t)**2)
56
+ g = odefun(lambda x, y: [-2*x*y[0]**2], 0, [f(0)])
57
+ assert f(2).ae(g(2)[0])
58
+
59
+ def test_odefun_sinc_large():
60
+ mp.dps = 15
61
+ # Sinc function; test for large x
62
+ f = sinc
63
+ g = odefun(lambda x, y: [(cos(x)-y[0])/x], 1, [f(1)], tol=0.01, degree=5)
64
+ assert abs(f(100) - g(100)[0])/f(100) < 0.01
65
+
66
+ def test_odefun_harmonic():
67
+ mp.dps = 15
68
+ # Harmonic oscillator
69
+ f = odefun(lambda x, y: [-y[1], y[0]], 0, [1, 0])
70
+ for x in [0, 1, 2.5, 8, 3.7]: # we go back to 3.7 to check caching
71
+ c, s = f(x)
72
+ assert c.ae(cos(x))
73
+ assert s.ae(sin(x))
llmeval-env/lib/python3.10/site-packages/mpmath/tests/test_pickle.py ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import tempfile
3
+ import pickle
4
+
5
+ from mpmath import *
6
+
7
+ def pickler(obj):
8
+ fn = tempfile.mktemp()
9
+
10
+ f = open(fn, 'wb')
11
+ pickle.dump(obj, f)
12
+ f.close()
13
+
14
+ f = open(fn, 'rb')
15
+ obj2 = pickle.load(f)
16
+ f.close()
17
+ os.remove(fn)
18
+
19
+ return obj2
20
+
21
+ def test_pickle():
22
+
23
+ obj = mpf('0.5')
24
+ assert obj == pickler(obj)
25
+
26
+ obj = mpc('0.5','0.2')
27
+ assert obj == pickler(obj)
llmeval-env/lib/python3.10/site-packages/mpmath/tests/test_power.py ADDED
@@ -0,0 +1,156 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from mpmath import *
2
+ from mpmath.libmp import *
3
+
4
+ import random
5
+
6
+ def test_fractional_pow():
7
+ mp.dps = 15
8
+ assert mpf(16) ** 2.5 == 1024
9
+ assert mpf(64) ** 0.5 == 8
10
+ assert mpf(64) ** -0.5 == 0.125
11
+ assert mpf(16) ** -2.5 == 0.0009765625
12
+ assert (mpf(10) ** 0.5).ae(3.1622776601683791)
13
+ assert (mpf(10) ** 2.5).ae(316.2277660168379)
14
+ assert (mpf(10) ** -0.5).ae(0.31622776601683794)
15
+ assert (mpf(10) ** -2.5).ae(0.0031622776601683794)
16
+ assert (mpf(10) ** 0.3).ae(1.9952623149688795)
17
+ assert (mpf(10) ** -0.3).ae(0.50118723362727224)
18
+
19
+ def test_pow_integer_direction():
20
+ """
21
+ Test that inexact integer powers are rounded in the right
22
+ direction.
23
+ """
24
+ random.seed(1234)
25
+ for prec in [10, 53, 200]:
26
+ for i in range(50):
27
+ a = random.randint(1<<(prec-1), 1<<prec)
28
+ b = random.randint(2, 100)
29
+ ab = a**b
30
+ # note: could actually be exact, but that's very unlikely!
31
+ assert to_int(mpf_pow(from_int(a), from_int(b), prec, round_down)) < ab
32
+ assert to_int(mpf_pow(from_int(a), from_int(b), prec, round_up)) > ab
33
+
34
+
35
+ def test_pow_epsilon_rounding():
36
+ """
37
+ Stress test directed rounding for powers with integer exponents.
38
+ Basically, we look at the following cases:
39
+
40
+ >>> 1.0001 ** -5 # doctest: +SKIP
41
+ 0.99950014996500702
42
+ >>> 0.9999 ** -5 # doctest: +SKIP
43
+ 1.000500150035007
44
+ >>> (-1.0001) ** -5 # doctest: +SKIP
45
+ -0.99950014996500702
46
+ >>> (-0.9999) ** -5 # doctest: +SKIP
47
+ -1.000500150035007
48
+
49
+ >>> 1.0001 ** -6 # doctest: +SKIP
50
+ 0.99940020994401269
51
+ >>> 0.9999 ** -6 # doctest: +SKIP
52
+ 1.0006002100560125
53
+ >>> (-1.0001) ** -6 # doctest: +SKIP
54
+ 0.99940020994401269
55
+ >>> (-0.9999) ** -6 # doctest: +SKIP
56
+ 1.0006002100560125
57
+
58
+ etc.
59
+
60
+ We run the tests with values a very small epsilon away from 1:
61
+ small enough that the result is indistinguishable from 1 when
62
+ rounded to nearest at the output precision. We check that the
63
+ result is not erroneously rounded to 1 in cases where the
64
+ rounding should be done strictly away from 1.
65
+ """
66
+
67
+ def powr(x, n, r):
68
+ return make_mpf(mpf_pow_int(x._mpf_, n, mp.prec, r))
69
+
70
+ for (inprec, outprec) in [(100, 20), (5000, 3000)]:
71
+
72
+ mp.prec = inprec
73
+
74
+ pos10001 = mpf(1) + mpf(2)**(-inprec+5)
75
+ pos09999 = mpf(1) - mpf(2)**(-inprec+5)
76
+ neg10001 = -pos10001
77
+ neg09999 = -pos09999
78
+
79
+ mp.prec = outprec
80
+ r = round_up
81
+ assert powr(pos10001, 5, r) > 1
82
+ assert powr(pos09999, 5, r) == 1
83
+ assert powr(neg10001, 5, r) < -1
84
+ assert powr(neg09999, 5, r) == -1
85
+ assert powr(pos10001, 6, r) > 1
86
+ assert powr(pos09999, 6, r) == 1
87
+ assert powr(neg10001, 6, r) > 1
88
+ assert powr(neg09999, 6, r) == 1
89
+
90
+ assert powr(pos10001, -5, r) == 1
91
+ assert powr(pos09999, -5, r) > 1
92
+ assert powr(neg10001, -5, r) == -1
93
+ assert powr(neg09999, -5, r) < -1
94
+ assert powr(pos10001, -6, r) == 1
95
+ assert powr(pos09999, -6, r) > 1
96
+ assert powr(neg10001, -6, r) == 1
97
+ assert powr(neg09999, -6, r) > 1
98
+
99
+ r = round_down
100
+ assert powr(pos10001, 5, r) == 1
101
+ assert powr(pos09999, 5, r) < 1
102
+ assert powr(neg10001, 5, r) == -1
103
+ assert powr(neg09999, 5, r) > -1
104
+ assert powr(pos10001, 6, r) == 1
105
+ assert powr(pos09999, 6, r) < 1
106
+ assert powr(neg10001, 6, r) == 1
107
+ assert powr(neg09999, 6, r) < 1
108
+
109
+ assert powr(pos10001, -5, r) < 1
110
+ assert powr(pos09999, -5, r) == 1
111
+ assert powr(neg10001, -5, r) > -1
112
+ assert powr(neg09999, -5, r) == -1
113
+ assert powr(pos10001, -6, r) < 1
114
+ assert powr(pos09999, -6, r) == 1
115
+ assert powr(neg10001, -6, r) < 1
116
+ assert powr(neg09999, -6, r) == 1
117
+
118
+ r = round_ceiling
119
+ assert powr(pos10001, 5, r) > 1
120
+ assert powr(pos09999, 5, r) == 1
121
+ assert powr(neg10001, 5, r) == -1
122
+ assert powr(neg09999, 5, r) > -1
123
+ assert powr(pos10001, 6, r) > 1
124
+ assert powr(pos09999, 6, r) == 1
125
+ assert powr(neg10001, 6, r) > 1
126
+ assert powr(neg09999, 6, r) == 1
127
+
128
+ assert powr(pos10001, -5, r) == 1
129
+ assert powr(pos09999, -5, r) > 1
130
+ assert powr(neg10001, -5, r) > -1
131
+ assert powr(neg09999, -5, r) == -1
132
+ assert powr(pos10001, -6, r) == 1
133
+ assert powr(pos09999, -6, r) > 1
134
+ assert powr(neg10001, -6, r) == 1
135
+ assert powr(neg09999, -6, r) > 1
136
+
137
+ r = round_floor
138
+ assert powr(pos10001, 5, r) == 1
139
+ assert powr(pos09999, 5, r) < 1
140
+ assert powr(neg10001, 5, r) < -1
141
+ assert powr(neg09999, 5, r) == -1
142
+ assert powr(pos10001, 6, r) == 1
143
+ assert powr(pos09999, 6, r) < 1
144
+ assert powr(neg10001, 6, r) == 1
145
+ assert powr(neg09999, 6, r) < 1
146
+
147
+ assert powr(pos10001, -5, r) < 1
148
+ assert powr(pos09999, -5, r) == 1
149
+ assert powr(neg10001, -5, r) == -1
150
+ assert powr(neg09999, -5, r) < -1
151
+ assert powr(pos10001, -6, r) < 1
152
+ assert powr(pos09999, -6, r) == 1
153
+ assert powr(neg10001, -6, r) < 1
154
+ assert powr(neg09999, -6, r) == 1
155
+
156
+ mp.dps = 15
llmeval-env/lib/python3.10/site-packages/mpmath/tests/test_quad.py ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pytest
2
+ from mpmath import *
3
+
4
+ def ae(a, b):
5
+ return abs(a-b) < 10**(-mp.dps+5)
6
+
7
+ def test_basic_integrals():
8
+ for prec in [15, 30, 100]:
9
+ mp.dps = prec
10
+ assert ae(quadts(lambda x: x**3 - 3*x**2, [-2, 4]), -12)
11
+ assert ae(quadgl(lambda x: x**3 - 3*x**2, [-2, 4]), -12)
12
+ assert ae(quadts(sin, [0, pi]), 2)
13
+ assert ae(quadts(sin, [0, 2*pi]), 0)
14
+ assert ae(quadts(exp, [-inf, -1]), 1/e)
15
+ assert ae(quadts(lambda x: exp(-x), [0, inf]), 1)
16
+ assert ae(quadts(lambda x: exp(-x*x), [-inf, inf]), sqrt(pi))
17
+ assert ae(quadts(lambda x: 1/(1+x*x), [-1, 1]), pi/2)
18
+ assert ae(quadts(lambda x: 1/(1+x*x), [-inf, inf]), pi)
19
+ assert ae(quadts(lambda x: 2*sqrt(1-x*x), [-1, 1]), pi)
20
+ mp.dps = 15
21
+
22
+ def test_multiple_intervals():
23
+ y,err = quad(lambda x: sign(x), [-0.5, 0.9, 1], maxdegree=2, error=True)
24
+ assert abs(y-0.5) < 2*err
25
+
26
+ def test_quad_symmetry():
27
+ assert quadts(sin, [-1, 1]) == 0
28
+ assert quadgl(sin, [-1, 1]) == 0
29
+
30
+ def test_quad_infinite_mirror():
31
+ # Check mirrored infinite interval
32
+ assert ae(quad(lambda x: exp(-x*x), [inf,-inf]), -sqrt(pi))
33
+ assert ae(quad(lambda x: exp(x), [0,-inf]), -1)
34
+
35
+ def test_quadgl_linear():
36
+ assert quadgl(lambda x: x, [0, 1], maxdegree=1).ae(0.5)
37
+
38
+ def test_complex_integration():
39
+ assert quadts(lambda x: x, [0, 1+j]).ae(j)
40
+
41
+ def test_quadosc():
42
+ mp.dps = 15
43
+ assert quadosc(lambda x: sin(x)/x, [0, inf], period=2*pi).ae(pi/2)
44
+
45
+ # Double integrals
46
+ def test_double_trivial():
47
+ assert ae(quadts(lambda x, y: x, [0, 1], [0, 1]), 0.5)
48
+ assert ae(quadts(lambda x, y: x, [-1, 1], [-1, 1]), 0.0)
49
+
50
+ def test_double_1():
51
+ assert ae(quadts(lambda x, y: cos(x+y/2), [-pi/2, pi/2], [0, pi]), 4)
52
+
53
+ def test_double_2():
54
+ assert ae(quadts(lambda x, y: (x-1)/((1-x*y)*log(x*y)), [0, 1], [0, 1]), euler)
55
+
56
+ def test_double_3():
57
+ assert ae(quadts(lambda x, y: 1/sqrt(1+x*x+y*y), [-1, 1], [-1, 1]), 4*log(2+sqrt(3))-2*pi/3)
58
+
59
+ def test_double_4():
60
+ assert ae(quadts(lambda x, y: 1/(1-x*x * y*y), [0, 1], [0, 1]), pi**2 / 8)
61
+
62
+ def test_double_5():
63
+ assert ae(quadts(lambda x, y: 1/(1-x*y), [0, 1], [0, 1]), pi**2 / 6)
64
+
65
+ def test_double_6():
66
+ assert ae(quadts(lambda x, y: exp(-(x+y)), [0, inf], [0, inf]), 1)
67
+
68
+ def test_double_7():
69
+ assert ae(quadts(lambda x, y: exp(-x*x-y*y), [-inf, inf], [-inf, inf]), pi)
70
+
71
+
72
+ # Test integrals from "Experimentation in Mathematics" by Borwein,
73
+ # Bailey & Girgensohn
74
+ def test_expmath_integrals():
75
+ for prec in [15, 30, 50]:
76
+ mp.dps = prec
77
+ assert ae(quadts(lambda x: x/sinh(x), [0, inf]), pi**2 / 4)
78
+ assert ae(quadts(lambda x: log(x)**2 / (1+x**2), [0, inf]), pi**3 / 8)
79
+ assert ae(quadts(lambda x: (1+x**2)/(1+x**4), [0, inf]), pi/sqrt(2))
80
+ assert ae(quadts(lambda x: log(x)/cosh(x)**2, [0, inf]), log(pi)-2*log(2)-euler)
81
+ assert ae(quadts(lambda x: log(1+x**3)/(1-x+x**2), [0, inf]), 2*pi*log(3)/sqrt(3))
82
+ assert ae(quadts(lambda x: log(x)**2 / (x**2+x+1), [0, 1]), 8*pi**3 / (81*sqrt(3)))
83
+ assert ae(quadts(lambda x: log(cos(x))**2, [0, pi/2]), pi/2 * (log(2)**2+pi**2/12))
84
+ assert ae(quadts(lambda x: x**2 / sin(x)**2, [0, pi/2]), pi*log(2))
85
+ assert ae(quadts(lambda x: x**2/sqrt(exp(x)-1), [0, inf]), 4*pi*(log(2)**2 + pi**2/12))
86
+ assert ae(quadts(lambda x: x*exp(-x)*sqrt(1-exp(-2*x)), [0, inf]), pi*(1+2*log(2))/8)
87
+ mp.dps = 15
88
+
89
+ # Do not reach full accuracy
90
+ @pytest.mark.xfail
91
+ def test_expmath_fail():
92
+ assert ae(quadts(lambda x: sqrt(tan(x)), [0, pi/2]), pi*sqrt(2)/2)
93
+ assert ae(quadts(lambda x: atan(x)/(x*sqrt(1-x**2)), [0, 1]), pi*log(1+sqrt(2))/2)
94
+ assert ae(quadts(lambda x: log(1+x**2)/x**2, [0, 1]), pi/2-log(2))
95
+ assert ae(quadts(lambda x: x**2/((1+x**4)*sqrt(1-x**4)), [0, 1]), pi/8)