test_gammazeta.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. from mpmath import *
  2. from mpmath.libmp import round_up, from_float, mpf_zeta_int
  3. def test_zeta_int_bug():
  4. assert mpf_zeta_int(0, 10) == from_float(-0.5)
  5. def test_bernoulli():
  6. assert bernfrac(0) == (1,1)
  7. assert bernfrac(1) == (-1,2)
  8. assert bernfrac(2) == (1,6)
  9. assert bernfrac(3) == (0,1)
  10. assert bernfrac(4) == (-1,30)
  11. assert bernfrac(5) == (0,1)
  12. assert bernfrac(6) == (1,42)
  13. assert bernfrac(8) == (-1,30)
  14. assert bernfrac(10) == (5,66)
  15. assert bernfrac(12) == (-691,2730)
  16. assert bernfrac(18) == (43867,798)
  17. p, q = bernfrac(228)
  18. assert p % 10**10 == 164918161
  19. assert q == 625170
  20. p, q = bernfrac(1000)
  21. assert p % 10**10 == 7950421099
  22. assert q == 342999030
  23. mp.dps = 15
  24. assert bernoulli(0) == 1
  25. assert bernoulli(1) == -0.5
  26. assert bernoulli(2).ae(1./6)
  27. assert bernoulli(3) == 0
  28. assert bernoulli(4).ae(-1./30)
  29. assert bernoulli(5) == 0
  30. assert bernoulli(6).ae(1./42)
  31. assert str(bernoulli(10)) == '0.0757575757575758'
  32. assert str(bernoulli(234)) == '7.62772793964344e+267'
  33. assert str(bernoulli(10**5)) == '-5.82229431461335e+376755'
  34. assert str(bernoulli(10**8+2)) == '1.19570355039953e+676752584'
  35. mp.dps = 50
  36. assert str(bernoulli(10)) == '0.075757575757575757575757575757575757575757575757576'
  37. assert str(bernoulli(234)) == '7.6277279396434392486994969020496121553385863373331e+267'
  38. assert str(bernoulli(10**5)) == '-5.8222943146133508236497045360612887555320691004308e+376755'
  39. assert str(bernoulli(10**8+2)) == '1.1957035503995297272263047884604346914602088317782e+676752584'
  40. mp.dps = 1000
  41. assert bernoulli(10).ae(mpf(5)/66)
  42. mp.dps = 50000
  43. assert bernoulli(10).ae(mpf(5)/66)
  44. mp.dps = 15
  45. def test_bernpoly_eulerpoly():
  46. mp.dps = 15
  47. assert bernpoly(0,-1).ae(1)
  48. assert bernpoly(0,0).ae(1)
  49. assert bernpoly(0,'1/2').ae(1)
  50. assert bernpoly(0,'3/4').ae(1)
  51. assert bernpoly(0,1).ae(1)
  52. assert bernpoly(0,2).ae(1)
  53. assert bernpoly(1,-1).ae('-3/2')
  54. assert bernpoly(1,0).ae('-1/2')
  55. assert bernpoly(1,'1/2').ae(0)
  56. assert bernpoly(1,'3/4').ae('1/4')
  57. assert bernpoly(1,1).ae('1/2')
  58. assert bernpoly(1,2).ae('3/2')
  59. assert bernpoly(2,-1).ae('13/6')
  60. assert bernpoly(2,0).ae('1/6')
  61. assert bernpoly(2,'1/2').ae('-1/12')
  62. assert bernpoly(2,'3/4').ae('-1/48')
  63. assert bernpoly(2,1).ae('1/6')
  64. assert bernpoly(2,2).ae('13/6')
  65. assert bernpoly(3,-1).ae(-3)
  66. assert bernpoly(3,0).ae(0)
  67. assert bernpoly(3,'1/2').ae(0)
  68. assert bernpoly(3,'3/4').ae('-3/64')
  69. assert bernpoly(3,1).ae(0)
  70. assert bernpoly(3,2).ae(3)
  71. assert bernpoly(4,-1).ae('119/30')
  72. assert bernpoly(4,0).ae('-1/30')
  73. assert bernpoly(4,'1/2').ae('7/240')
  74. assert bernpoly(4,'3/4').ae('7/3840')
  75. assert bernpoly(4,1).ae('-1/30')
  76. assert bernpoly(4,2).ae('119/30')
  77. assert bernpoly(5,-1).ae(-5)
  78. assert bernpoly(5,0).ae(0)
  79. assert bernpoly(5,'1/2').ae(0)
  80. assert bernpoly(5,'3/4').ae('25/1024')
  81. assert bernpoly(5,1).ae(0)
  82. assert bernpoly(5,2).ae(5)
  83. assert bernpoly(10,-1).ae('665/66')
  84. assert bernpoly(10,0).ae('5/66')
  85. assert bernpoly(10,'1/2').ae('-2555/33792')
  86. assert bernpoly(10,'3/4').ae('-2555/34603008')
  87. assert bernpoly(10,1).ae('5/66')
  88. assert bernpoly(10,2).ae('665/66')
  89. assert bernpoly(11,-1).ae(-11)
  90. assert bernpoly(11,0).ae(0)
  91. assert bernpoly(11,'1/2').ae(0)
  92. assert bernpoly(11,'3/4').ae('-555731/4194304')
  93. assert bernpoly(11,1).ae(0)
  94. assert bernpoly(11,2).ae(11)
  95. assert eulerpoly(0,-1).ae(1)
  96. assert eulerpoly(0,0).ae(1)
  97. assert eulerpoly(0,'1/2').ae(1)
  98. assert eulerpoly(0,'3/4').ae(1)
  99. assert eulerpoly(0,1).ae(1)
  100. assert eulerpoly(0,2).ae(1)
  101. assert eulerpoly(1,-1).ae('-3/2')
  102. assert eulerpoly(1,0).ae('-1/2')
  103. assert eulerpoly(1,'1/2').ae(0)
  104. assert eulerpoly(1,'3/4').ae('1/4')
  105. assert eulerpoly(1,1).ae('1/2')
  106. assert eulerpoly(1,2).ae('3/2')
  107. assert eulerpoly(2,-1).ae(2)
  108. assert eulerpoly(2,0).ae(0)
  109. assert eulerpoly(2,'1/2').ae('-1/4')
  110. assert eulerpoly(2,'3/4').ae('-3/16')
  111. assert eulerpoly(2,1).ae(0)
  112. assert eulerpoly(2,2).ae(2)
  113. assert eulerpoly(3,-1).ae('-9/4')
  114. assert eulerpoly(3,0).ae('1/4')
  115. assert eulerpoly(3,'1/2').ae(0)
  116. assert eulerpoly(3,'3/4').ae('-11/64')
  117. assert eulerpoly(3,1).ae('-1/4')
  118. assert eulerpoly(3,2).ae('9/4')
  119. assert eulerpoly(4,-1).ae(2)
  120. assert eulerpoly(4,0).ae(0)
  121. assert eulerpoly(4,'1/2').ae('5/16')
  122. assert eulerpoly(4,'3/4').ae('57/256')
  123. assert eulerpoly(4,1).ae(0)
  124. assert eulerpoly(4,2).ae(2)
  125. assert eulerpoly(5,-1).ae('-3/2')
  126. assert eulerpoly(5,0).ae('-1/2')
  127. assert eulerpoly(5,'1/2').ae(0)
  128. assert eulerpoly(5,'3/4').ae('361/1024')
  129. assert eulerpoly(5,1).ae('1/2')
  130. assert eulerpoly(5,2).ae('3/2')
  131. assert eulerpoly(10,-1).ae(2)
  132. assert eulerpoly(10,0).ae(0)
  133. assert eulerpoly(10,'1/2').ae('-50521/1024')
  134. assert eulerpoly(10,'3/4').ae('-36581523/1048576')
  135. assert eulerpoly(10,1).ae(0)
  136. assert eulerpoly(10,2).ae(2)
  137. assert eulerpoly(11,-1).ae('-699/4')
  138. assert eulerpoly(11,0).ae('691/4')
  139. assert eulerpoly(11,'1/2').ae(0)
  140. assert eulerpoly(11,'3/4').ae('-512343611/4194304')
  141. assert eulerpoly(11,1).ae('-691/4')
  142. assert eulerpoly(11,2).ae('699/4')
  143. # Potential accuracy issues
  144. assert bernpoly(10000,10000).ae('5.8196915936323387117e+39999')
  145. assert bernpoly(200,17.5).ae(3.8048418524583064909e244)
  146. assert eulerpoly(200,17.5).ae(-3.7309911582655785929e275)
  147. def test_gamma():
  148. mp.dps = 15
  149. assert gamma(0.25).ae(3.6256099082219083119)
  150. assert gamma(0.0001).ae(9999.4228832316241908)
  151. assert gamma(300).ae('1.0201917073881354535e612')
  152. assert gamma(-0.5).ae(-3.5449077018110320546)
  153. assert gamma(-7.43).ae(0.00026524416464197007186)
  154. #assert gamma(Rational(1,2)) == gamma(0.5)
  155. #assert gamma(Rational(-7,3)).ae(gamma(mpf(-7)/3))
  156. assert gamma(1+1j).ae(0.49801566811835604271 - 0.15494982830181068512j)
  157. assert gamma(-1+0.01j).ae(-0.422733904013474115 + 99.985883082635367436j)
  158. assert gamma(20+30j).ae(-1453876687.5534810 + 1163777777.8031573j)
  159. # Should always give exact factorials when they can
  160. # be represented as mpfs under the current working precision
  161. fact = 1
  162. for i in range(1, 18):
  163. assert gamma(i) == fact
  164. fact *= i
  165. for dps in [170, 600]:
  166. fact = 1
  167. mp.dps = dps
  168. for i in range(1, 105):
  169. assert gamma(i) == fact
  170. fact *= i
  171. mp.dps = 100
  172. assert gamma(0.5).ae(sqrt(pi))
  173. mp.dps = 15
  174. assert factorial(0) == fac(0) == 1
  175. assert factorial(3) == 6
  176. assert isnan(gamma(nan))
  177. assert gamma(1100).ae('4.8579168073569433667e2866')
  178. assert rgamma(0) == 0
  179. assert rgamma(-1) == 0
  180. assert rgamma(2) == 1.0
  181. assert rgamma(3) == 0.5
  182. assert loggamma(2+8j).ae(-8.5205176753667636926 + 10.8569497125597429366j)
  183. assert loggamma('1e10000').ae('2.302485092994045684017991e10004')
  184. assert loggamma('1e10000j').ae(mpc('-1.570796326794896619231322e10000','2.302485092994045684017991e10004'))
  185. def test_fac2():
  186. mp.dps = 15
  187. assert [fac2(n) for n in range(10)] == [1,1,2,3,8,15,48,105,384,945]
  188. assert fac2(-5).ae(1./3)
  189. assert fac2(-11).ae(-1./945)
  190. assert fac2(50).ae(5.20469842636666623e32)
  191. assert fac2(0.5+0.75j).ae(0.81546769394688069176-0.34901016085573266889j)
  192. assert fac2(inf) == inf
  193. assert isnan(fac2(-inf))
  194. def test_gamma_quotients():
  195. mp.dps = 15
  196. h = 1e-8
  197. ep = 1e-4
  198. G = gamma
  199. assert gammaprod([-1],[-3,-4]) == 0
  200. assert gammaprod([-1,0],[-5]) == inf
  201. assert abs(gammaprod([-1],[-2]) - G(-1+h)/G(-2+h)) < 1e-4
  202. assert abs(gammaprod([-4,-3],[-2,0]) - G(-4+h)*G(-3+h)/G(-2+h)/G(0+h)) < 1e-4
  203. assert rf(3,0) == 1
  204. assert rf(2.5,1) == 2.5
  205. assert rf(-5,2) == 20
  206. assert rf(j,j).ae(gamma(2*j)/gamma(j))
  207. assert rf('-255.5815971722918','-0.5119253100282322').ae('-0.1952720278805729485') # issue 421
  208. assert ff(-2,0) == 1
  209. assert ff(-2,1) == -2
  210. assert ff(4,3) == 24
  211. assert ff(3,4) == 0
  212. assert binomial(0,0) == 1
  213. assert binomial(1,0) == 1
  214. assert binomial(0,-1) == 0
  215. assert binomial(3,2) == 3
  216. assert binomial(5,2) == 10
  217. assert binomial(5,3) == 10
  218. assert binomial(5,5) == 1
  219. assert binomial(-1,0) == 1
  220. assert binomial(-2,-4) == 3
  221. assert binomial(4.5, 1.5) == 6.5625
  222. assert binomial(1100,1) == 1100
  223. assert binomial(1100,2) == 604450
  224. assert beta(1,1) == 1
  225. assert beta(0,0) == inf
  226. assert beta(3,0) == inf
  227. assert beta(-1,-1) == inf
  228. assert beta(1.5,1).ae(2/3.)
  229. assert beta(1.5,2.5).ae(pi/16)
  230. assert (10**15*beta(10,100)).ae(2.3455339739604649879)
  231. assert beta(inf,inf) == 0
  232. assert isnan(beta(-inf,inf))
  233. assert isnan(beta(-3,inf))
  234. assert isnan(beta(0,inf))
  235. assert beta(inf,0.5) == beta(0.5,inf) == 0
  236. assert beta(inf,-1.5) == inf
  237. assert beta(inf,-0.5) == -inf
  238. assert beta(1+2j,-1-j/2).ae(1.16396542451069943086+0.08511695947832914640j)
  239. assert beta(-0.5,0.5) == 0
  240. assert beta(-3,3).ae(-1/3.)
  241. assert beta('-255.5815971722918','-0.5119253100282322').ae('18.157330562703710339') # issue 421
  242. def test_zeta():
  243. mp.dps = 15
  244. assert zeta(2).ae(pi**2 / 6)
  245. assert zeta(2.0).ae(pi**2 / 6)
  246. assert zeta(mpc(2)).ae(pi**2 / 6)
  247. assert zeta(100).ae(1)
  248. assert zeta(0).ae(-0.5)
  249. assert zeta(0.5).ae(-1.46035450880958681)
  250. assert zeta(-1).ae(-mpf(1)/12)
  251. assert zeta(-2) == 0
  252. assert zeta(-3).ae(mpf(1)/120)
  253. assert zeta(-4) == 0
  254. assert zeta(-100) == 0
  255. assert isnan(zeta(nan))
  256. assert zeta(1e-30).ae(-0.5)
  257. assert zeta(-1e-30).ae(-0.5)
  258. # Zeros in the critical strip
  259. assert zeta(mpc(0.5, 14.1347251417346937904)).ae(0)
  260. assert zeta(mpc(0.5, 21.0220396387715549926)).ae(0)
  261. assert zeta(mpc(0.5, 25.0108575801456887632)).ae(0)
  262. assert zeta(mpc(1e-30,1e-40)).ae(-0.5)
  263. assert zeta(mpc(-1e-30,1e-40)).ae(-0.5)
  264. mp.dps = 50
  265. im = '236.5242296658162058024755079556629786895294952121891237'
  266. assert zeta(mpc(0.5, im)).ae(0, 1e-46)
  267. mp.dps = 15
  268. # Complex reflection formula
  269. assert (zeta(-60+3j) / 10**34).ae(8.6270183987866146+15.337398548226238j)
  270. # issue #358
  271. assert zeta(0,0.5) == 0
  272. assert zeta(0,0) == 0.5
  273. assert zeta(0,0.5,1).ae(-0.34657359027997265)
  274. # see issue #390
  275. assert zeta(-1.5,0.5j).ae(-0.13671400162512768475 + 0.11411333638426559139j)
  276. def test_altzeta():
  277. mp.dps = 15
  278. assert altzeta(-2) == 0
  279. assert altzeta(-4) == 0
  280. assert altzeta(-100) == 0
  281. assert altzeta(0) == 0.5
  282. assert altzeta(-1) == 0.25
  283. assert altzeta(-3) == -0.125
  284. assert altzeta(-5) == 0.25
  285. assert altzeta(-21) == 1180529130.25
  286. assert altzeta(1).ae(log(2))
  287. assert altzeta(2).ae(pi**2/12)
  288. assert altzeta(10).ae(73*pi**10/6842880)
  289. assert altzeta(50) < 1
  290. assert altzeta(60, rounding='d') < 1
  291. assert altzeta(60, rounding='u') == 1
  292. assert altzeta(10000, rounding='d') < 1
  293. assert altzeta(10000, rounding='u') == 1
  294. assert altzeta(3+0j) == altzeta(3)
  295. s = 3+4j
  296. assert altzeta(s).ae((1-2**(1-s))*zeta(s))
  297. s = -3+4j
  298. assert altzeta(s).ae((1-2**(1-s))*zeta(s))
  299. assert altzeta(-100.5).ae(4.58595480083585913e+108)
  300. assert altzeta(1.3).ae(0.73821404216623045)
  301. assert altzeta(1e-30).ae(0.5)
  302. assert altzeta(-1e-30).ae(0.5)
  303. assert altzeta(mpc(1e-30,1e-40)).ae(0.5)
  304. assert altzeta(mpc(-1e-30,1e-40)).ae(0.5)
  305. def test_zeta_huge():
  306. mp.dps = 15
  307. assert zeta(inf) == 1
  308. mp.dps = 50
  309. assert zeta(100).ae('1.0000000000000000000000000000007888609052210118073522')
  310. assert zeta(40*pi).ae('1.0000000000000000000000000000000000000148407238666182')
  311. mp.dps = 10000
  312. v = zeta(33000)
  313. mp.dps = 15
  314. assert str(v-1) == '1.02363019598118e-9934'
  315. assert zeta(pi*1000, rounding=round_up) > 1
  316. assert zeta(3000, rounding=round_up) > 1
  317. assert zeta(pi*1000) == 1
  318. assert zeta(3000) == 1
  319. def test_zeta_negative():
  320. mp.dps = 150
  321. a = -pi*10**40
  322. mp.dps = 15
  323. assert str(zeta(a)) == '2.55880492708712e+1233536161668617575553892558646631323374078'
  324. mp.dps = 50
  325. assert str(zeta(a)) == '2.5588049270871154960875033337384432038436330847333e+1233536161668617575553892558646631323374078'
  326. mp.dps = 15
  327. def test_polygamma():
  328. mp.dps = 15
  329. psi0 = lambda z: psi(0,z)
  330. psi1 = lambda z: psi(1,z)
  331. assert psi0(3) == psi(0,3) == digamma(3)
  332. #assert psi2(3) == psi(2,3) == tetragamma(3)
  333. #assert psi3(3) == psi(3,3) == pentagamma(3)
  334. assert psi0(pi).ae(0.97721330794200673)
  335. assert psi0(-pi).ae(7.8859523853854902)
  336. assert psi0(-pi+1).ae(7.5676424992016996)
  337. assert psi0(pi+j).ae(1.04224048313859376 + 0.35853686544063749j)
  338. assert psi0(-pi-j).ae(1.3404026194821986 - 2.8824392476809402j)
  339. assert findroot(psi0, 1).ae(1.4616321449683622)
  340. assert psi0(1e-10).ae(-10000000000.57722)
  341. assert psi0(1e-40).ae(-1.000000000000000e+40)
  342. assert psi0(1e-10+1e-10j).ae(-5000000000.577215 + 5000000000.000000j)
  343. assert psi0(1e-40+1e-40j).ae(-5.000000000000000e+39 + 5.000000000000000e+39j)
  344. assert psi0(inf) == inf
  345. assert psi1(inf) == 0
  346. assert psi(2,inf) == 0
  347. assert psi1(pi).ae(0.37424376965420049)
  348. assert psi1(-pi).ae(53.030438740085385)
  349. assert psi1(pi+j).ae(0.32935710377142464 - 0.12222163911221135j)
  350. assert psi1(-pi-j).ae(-0.30065008356019703 + 0.01149892486928227j)
  351. assert (10**6*psi(4,1+10*pi*j)).ae(-6.1491803479004446 - 0.3921316371664063j)
  352. assert psi0(1+10*pi*j).ae(3.4473994217222650 + 1.5548808324857071j)
  353. assert isnan(psi0(nan))
  354. assert isnan(psi0(-inf))
  355. assert psi0(-100.5).ae(4.615124601338064)
  356. assert psi0(3+0j).ae(psi0(3))
  357. assert psi0(-100+3j).ae(4.6106071768714086321+3.1117510556817394626j)
  358. assert isnan(psi(2,mpc(0,inf)))
  359. assert isnan(psi(2,mpc(0,nan)))
  360. assert isnan(psi(2,mpc(0,-inf)))
  361. assert isnan(psi(2,mpc(1,inf)))
  362. assert isnan(psi(2,mpc(1,nan)))
  363. assert isnan(psi(2,mpc(1,-inf)))
  364. assert isnan(psi(2,mpc(inf,inf)))
  365. assert isnan(psi(2,mpc(nan,nan)))
  366. assert isnan(psi(2,mpc(-inf,-inf)))
  367. def test_polygamma_high_prec():
  368. mp.dps = 100
  369. assert str(psi(0,pi)) == "0.9772133079420067332920694864061823436408346099943256380095232865318105924777141317302075654362928734"
  370. assert str(psi(10,pi)) == "-12.98876181434889529310283769414222588307175962213707170773803550518307617769657562747174101900659238"
  371. def test_polygamma_identities():
  372. mp.dps = 15
  373. psi0 = lambda z: psi(0,z)
  374. psi1 = lambda z: psi(1,z)
  375. psi2 = lambda z: psi(2,z)
  376. assert psi0(0.5).ae(-euler-2*log(2))
  377. assert psi0(1).ae(-euler)
  378. assert psi1(0.5).ae(0.5*pi**2)
  379. assert psi1(1).ae(pi**2/6)
  380. assert psi1(0.25).ae(pi**2 + 8*catalan)
  381. assert psi2(1).ae(-2*apery)
  382. mp.dps = 20
  383. u = -182*apery+4*sqrt(3)*pi**3
  384. mp.dps = 15
  385. assert psi(2,5/6.).ae(u)
  386. assert psi(3,0.5).ae(pi**4)
  387. def test_foxtrot_identity():
  388. # A test of the complex digamma function.
  389. # See http://mathworld.wolfram.com/FoxTrotSeries.html and
  390. # http://mathworld.wolfram.com/DigammaFunction.html
  391. psi0 = lambda z: psi(0,z)
  392. mp.dps = 50
  393. a = (-1)**fraction(1,3)
  394. b = (-1)**fraction(2,3)
  395. x = -psi0(0.5*a) - psi0(-0.5*b) + psi0(0.5*(1+a)) + psi0(0.5*(1-b))
  396. y = 2*pi*sech(0.5*sqrt(3)*pi)
  397. assert x.ae(y)
  398. mp.dps = 15
  399. def test_polygamma_high_order():
  400. mp.dps = 100
  401. assert str(psi(50, pi)) == "-1344100348958402765749252447726432491812.641985273160531055707095989227897753035823152397679626136483"
  402. assert str(psi(50, pi + 14*e)) == "-0.00000000000000000189793739550804321623512073101895801993019919886375952881053090844591920308111549337295143780341396"
  403. assert str(psi(50, pi + 14*e*j)) == ("(-0.0000000000000000522516941152169248975225472155683565752375889510631513244785"
  404. "9377385233700094871256507814151956624433 - 0.00000000000000001813157041407010184"
  405. "702414110218205348527862196327980417757665282244728963891298080199341480881811613j)")
  406. mp.dps = 15
  407. assert str(psi(50, pi)) == "-1.34410034895841e+39"
  408. assert str(psi(50, pi + 14*e)) == "-1.89793739550804e-18"
  409. assert str(psi(50, pi + 14*e*j)) == "(-5.2251694115217e-17 - 1.81315704140701e-17j)"
  410. def test_harmonic():
  411. mp.dps = 15
  412. assert harmonic(0) == 0
  413. assert harmonic(1) == 1
  414. assert harmonic(2) == 1.5
  415. assert harmonic(3).ae(1. + 1./2 + 1./3)
  416. assert harmonic(10**10).ae(23.603066594891989701)
  417. assert harmonic(10**1000).ae(2303.162308658947)
  418. assert harmonic(0.5).ae(2-2*log(2))
  419. assert harmonic(inf) == inf
  420. assert harmonic(2+0j) == 1.5+0j
  421. assert harmonic(1+2j).ae(1.4918071802755104+0.92080728264223022j)
  422. def test_gamma_huge_1():
  423. mp.dps = 500
  424. x = mpf(10**10) / 7
  425. mp.dps = 15
  426. assert str(gamma(x)) == "6.26075321389519e+12458010678"
  427. mp.dps = 50
  428. assert str(gamma(x)) == "6.2607532138951929201303779291707455874010420783933e+12458010678"
  429. mp.dps = 15
  430. def test_gamma_huge_2():
  431. mp.dps = 500
  432. x = mpf(10**100) / 19
  433. mp.dps = 15
  434. assert str(gamma(x)) == (\
  435. "1.82341134776679e+5172997469323364168990133558175077136829182824042201886051511"
  436. "9656908623426021308685461258226190190661")
  437. mp.dps = 50
  438. assert str(gamma(x)) == (\
  439. "1.82341134776678875374414910350027596939980412984e+5172997469323364168990133558"
  440. "1750771368291828240422018860515119656908623426021308685461258226190190661")
  441. def test_gamma_huge_3():
  442. mp.dps = 500
  443. x = 10**80 // 3 + 10**70*j / 7
  444. mp.dps = 15
  445. y = gamma(x)
  446. assert str(y.real) == (\
  447. "-6.82925203918106e+2636286142112569524501781477865238132302397236429627932441916"
  448. "056964386399485392600")
  449. assert str(y.imag) == (\
  450. "8.54647143678418e+26362861421125695245017814778652381323023972364296279324419160"
  451. "56964386399485392600")
  452. mp.dps = 50
  453. y = gamma(x)
  454. assert str(y.real) == (\
  455. "-6.8292520391810548460682736226799637356016538421817e+26362861421125695245017814"
  456. "77865238132302397236429627932441916056964386399485392600")
  457. assert str(y.imag) == (\
  458. "8.5464714367841748507479306948130687511711420234015e+263628614211256952450178147"
  459. "7865238132302397236429627932441916056964386399485392600")
  460. def test_gamma_huge_4():
  461. x = 3200+11500j
  462. mp.dps = 15
  463. assert str(gamma(x)) == \
  464. "(8.95783268539713e+5164 - 1.94678798329735e+5164j)"
  465. mp.dps = 50
  466. assert str(gamma(x)) == (\
  467. "(8.9578326853971339570292952697675570822206567327092e+5164"
  468. " - 1.9467879832973509568895402139429643650329524144794e+51"
  469. "64j)")
  470. mp.dps = 15
  471. def test_gamma_huge_5():
  472. mp.dps = 500
  473. x = 10**60 * j / 3
  474. mp.dps = 15
  475. y = gamma(x)
  476. assert str(y.real) == "-3.27753899634941e-227396058973640224580963937571892628368354580620654233316839"
  477. assert str(y.imag) == "-7.1519888950416e-227396058973640224580963937571892628368354580620654233316841"
  478. mp.dps = 50
  479. y = gamma(x)
  480. assert str(y.real) == (\
  481. "-3.2775389963494132168950056995974690946983219123935e-22739605897364022458096393"
  482. "7571892628368354580620654233316839")
  483. assert str(y.imag) == (\
  484. "-7.1519888950415979749736749222530209713136588885897e-22739605897364022458096393"
  485. "7571892628368354580620654233316841")
  486. mp.dps = 15
  487. def test_gamma_huge_7():
  488. mp.dps = 100
  489. a = 3 + j/mpf(10)**1000
  490. mp.dps = 15
  491. y = gamma(a)
  492. assert str(y.real) == "2.0"
  493. # wrong
  494. #assert str(y.imag) == "2.16735365342606e-1000"
  495. assert str(y.imag) == "1.84556867019693e-1000"
  496. mp.dps = 50
  497. y = gamma(a)
  498. assert str(y.real) == "2.0"
  499. #assert str(y.imag) == "2.1673536534260596065418805612488708028522563689298e-1000"
  500. assert str(y.imag) == "1.8455686701969342787869758198351951379156813281202e-1000"
  501. def test_stieltjes():
  502. mp.dps = 15
  503. assert stieltjes(0).ae(+euler)
  504. mp.dps = 25
  505. assert stieltjes(1).ae('-0.07281584548367672486058637587')
  506. assert stieltjes(2).ae('-0.009690363192872318484530386035')
  507. assert stieltjes(3).ae('0.002053834420303345866160046543')
  508. assert stieltjes(4).ae('0.002325370065467300057468170178')
  509. mp.dps = 15
  510. assert stieltjes(1).ae(-0.07281584548367672486058637587)
  511. assert stieltjes(2).ae(-0.009690363192872318484530386035)
  512. assert stieltjes(3).ae(0.002053834420303345866160046543)
  513. assert stieltjes(4).ae(0.0023253700654673000574681701775)
  514. def test_barnesg():
  515. mp.dps = 15
  516. assert barnesg(0) == barnesg(-1) == 0
  517. assert [superfac(i) for i in range(8)] == [1, 1, 2, 12, 288, 34560, 24883200, 125411328000]
  518. assert str(superfac(1000)) == '3.24570818422368e+1177245'
  519. assert isnan(barnesg(nan))
  520. assert isnan(superfac(nan))
  521. assert isnan(hyperfac(nan))
  522. assert barnesg(inf) == inf
  523. assert superfac(inf) == inf
  524. assert hyperfac(inf) == inf
  525. assert isnan(superfac(-inf))
  526. assert barnesg(0.7).ae(0.8068722730141471)
  527. assert barnesg(2+3j).ae(-0.17810213864082169+0.04504542715447838j)
  528. assert [hyperfac(n) for n in range(7)] == [1, 1, 4, 108, 27648, 86400000, 4031078400000]
  529. assert [hyperfac(n) for n in range(0,-7,-1)] == [1,1,-1,-4,108,27648,-86400000]
  530. a = barnesg(-3+0j)
  531. assert a == 0 and isinstance(a, mpc)
  532. a = hyperfac(-3+0j)
  533. assert a == -4 and isinstance(a, mpc)
  534. def test_polylog():
  535. mp.dps = 15
  536. zs = [mpmathify(z) for z in [0, 0.5, 0.99, 4, -0.5, -4, 1j, 3+4j]]
  537. for z in zs: assert polylog(1, z).ae(-log(1-z))
  538. for z in zs: assert polylog(0, z).ae(z/(1-z))
  539. for z in zs: assert polylog(-1, z).ae(z/(1-z)**2)
  540. for z in zs: assert polylog(-2, z).ae(z*(1+z)/(1-z)**3)
  541. for z in zs: assert polylog(-3, z).ae(z*(1+4*z+z**2)/(1-z)**4)
  542. assert polylog(3, 7).ae(5.3192579921456754382-5.9479244480803301023j)
  543. assert polylog(3, -7).ae(-4.5693548977219423182)
  544. assert polylog(2, 0.9).ae(1.2997147230049587252)
  545. assert polylog(2, -0.9).ae(-0.75216317921726162037)
  546. assert polylog(2, 0.9j).ae(-0.17177943786580149299+0.83598828572550503226j)
  547. assert polylog(2, 1.1).ae(1.9619991013055685931-0.2994257606855892575j)
  548. assert polylog(2, -1.1).ae(-0.89083809026228260587)
  549. assert polylog(2, 1.1*sqrt(j)).ae(0.58841571107611387722+1.09962542118827026011j)
  550. assert polylog(-2, 0.9).ae(1710)
  551. assert polylog(-2, -0.9).ae(-90/6859.)
  552. assert polylog(3, 0.9).ae(1.0496589501864398696)
  553. assert polylog(-3, 0.9).ae(48690)
  554. assert polylog(-3, -4).ae(-0.0064)
  555. assert polylog(0.5+j/3, 0.5+j/2).ae(0.31739144796565650535 + 0.99255390416556261437j)
  556. assert polylog(3+4j,1).ae(zeta(3+4j))
  557. assert polylog(3+4j,-1).ae(-altzeta(3+4j))
  558. # issue 390
  559. assert polylog(1.5, -48.910886523731889).ae(-6.272992229311817)
  560. assert polylog(1.5, 200).ae(-8.349608319033686529 - 8.159694826434266042j)
  561. def test_bell_polyexp():
  562. mp.dps = 15
  563. # TODO: more tests for polyexp
  564. assert (polyexp(0,1e-10)*10**10).ae(1.00000000005)
  565. assert (polyexp(1,1e-10)*10**10).ae(1.0000000001)
  566. assert polyexp(5,3j).ae(-607.7044517476176454+519.962786482001476087j)
  567. assert polyexp(-1,3.5).ae(12.09537536175543444)
  568. # bell(0,x) = 1
  569. assert bell(0,0) == 1
  570. assert bell(0,1) == 1
  571. assert bell(0,2) == 1
  572. assert bell(0,inf) == 1
  573. assert bell(0,-inf) == 1
  574. assert isnan(bell(0,nan))
  575. # bell(1,x) = x
  576. assert bell(1,4) == 4
  577. assert bell(1,0) == 0
  578. assert bell(1,inf) == inf
  579. assert bell(1,-inf) == -inf
  580. assert isnan(bell(1,nan))
  581. # bell(2,x) = x*(1+x)
  582. assert bell(2,-1) == 0
  583. assert bell(2,0) == 0
  584. # large orders / arguments
  585. assert bell(10) == 115975
  586. assert bell(10,1) == 115975
  587. assert bell(10, -8) == 11054008
  588. assert bell(5,-50) == -253087550
  589. assert bell(50,-50).ae('3.4746902914629720259e74')
  590. mp.dps = 80
  591. assert bell(50,-50) == 347469029146297202586097646631767227177164818163463279814268368579055777450
  592. assert bell(40,50) == 5575520134721105844739265207408344706846955281965031698187656176321717550
  593. assert bell(74) == 5006908024247925379707076470957722220463116781409659160159536981161298714301202
  594. mp.dps = 15
  595. assert bell(10,20j) == 7504528595600+15649605360020j
  596. # continuity of the generalization
  597. assert bell(0.5,0).ae(sinc(pi*0.5))
  598. def test_primezeta():
  599. mp.dps = 15
  600. assert primezeta(0.9).ae(1.8388316154446882243 + 3.1415926535897932385j)
  601. assert primezeta(4).ae(0.076993139764246844943)
  602. assert primezeta(1) == inf
  603. assert primezeta(inf) == 0
  604. assert isnan(primezeta(nan))
  605. def test_rs_zeta():
  606. mp.dps = 15
  607. assert zeta(0.5+100000j).ae(1.0730320148577531321 + 5.7808485443635039843j)
  608. assert zeta(0.75+100000j).ae(1.837852337251873704 + 1.9988492668661145358j)
  609. assert zeta(0.5+1000000j, derivative=3).ae(1647.7744105852674733 - 1423.1270943036622097j)
  610. assert zeta(1+1000000j, derivative=3).ae(3.4085866124523582894 - 18.179184721525947301j)
  611. assert zeta(1+1000000j, derivative=1).ae(-0.10423479366985452134 - 0.74728992803359056244j)
  612. assert zeta(0.5-1000000j, derivative=1).ae(11.636804066002521459 + 17.127254072212996004j)
  613. # Additional sanity tests using fp arithmetic.
  614. # Some more high-precision tests are found in the docstrings
  615. def ae(x, y, tol=1e-6):
  616. return abs(x-y) < tol*abs(y)
  617. assert ae(fp.zeta(0.5-100000j), 1.0730320148577531321 - 5.7808485443635039843j)
  618. assert ae(fp.zeta(0.75-100000j), 1.837852337251873704 - 1.9988492668661145358j)
  619. assert ae(fp.zeta(0.5+1e6j), 0.076089069738227100006 + 2.8051021010192989554j)
  620. assert ae(fp.zeta(0.5+1e6j, derivative=1), 11.636804066002521459 - 17.127254072212996004j)
  621. assert ae(fp.zeta(1+1e6j), 0.94738726251047891048 + 0.59421999312091832833j)
  622. assert ae(fp.zeta(1+1e6j, derivative=1), -0.10423479366985452134 - 0.74728992803359056244j)
  623. assert ae(fp.zeta(0.5+100000j, derivative=1), 10.766962036817482375 - 30.92705282105996714j)
  624. assert ae(fp.zeta(0.5+100000j, derivative=2), -119.40515625740538429 + 217.14780631141830251j)
  625. assert ae(fp.zeta(0.5+100000j, derivative=3), 1129.7550282628460881 - 1685.4736895169690346j)
  626. assert ae(fp.zeta(0.5+100000j, derivative=4), -10407.160819314958615 + 13777.786698628045085j)
  627. assert ae(fp.zeta(0.75+100000j, derivative=1), -0.41742276699594321475 - 6.4453816275049955949j)
  628. assert ae(fp.zeta(0.75+100000j, derivative=2), -9.214314279161977266 + 35.07290795337967899j)
  629. assert ae(fp.zeta(0.75+100000j, derivative=3), 110.61331857820103469 - 236.87847130518129926j)
  630. assert ae(fp.zeta(0.75+100000j, derivative=4), -1054.334275898559401 + 1769.9177890161596383j)
  631. def test_siegelz():
  632. mp.dps = 15
  633. assert siegelz(100000).ae(5.87959246868176504171)
  634. assert siegelz(100000, derivative=2).ae(-54.1172711010126452832)
  635. assert siegelz(100000, derivative=3).ae(-278.930831343966552538)
  636. assert siegelz(100000+j,derivative=1).ae(678.214511857070283307-379.742160779916375413j)
  637. def test_zeta_near_1():
  638. # Test for a former bug in mpf_zeta and mpc_zeta
  639. mp.dps = 15
  640. s1 = fadd(1, '1e-10', exact=True)
  641. s2 = fadd(1, '-1e-10', exact=True)
  642. s3 = fadd(1, '1e-10j', exact=True)
  643. assert zeta(s1).ae(1.000000000057721566490881444e10)
  644. assert zeta(s2).ae(-9.99999999942278433510574872e9)
  645. z = zeta(s3)
  646. assert z.real.ae(0.57721566490153286060)
  647. assert z.imag.ae(-9.9999999999999999999927184e9)
  648. mp.dps = 30
  649. s1 = fadd(1, '1e-50', exact=True)
  650. s2 = fadd(1, '-1e-50', exact=True)
  651. s3 = fadd(1, '1e-50j', exact=True)
  652. assert zeta(s1).ae('1e50')
  653. assert zeta(s2).ae('-1e50')
  654. z = zeta(s3)
  655. assert z.real.ae('0.57721566490153286060651209008240243104215933593992')
  656. assert z.imag.ae('-1e50')