extratest_gamma.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. from mpmath import *
  2. from mpmath.libmp import ifac
  3. import sys
  4. if "-dps" in sys.argv:
  5. maxdps = int(sys.argv[sys.argv.index("-dps")+1])
  6. else:
  7. maxdps = 1000
  8. raise_ = "-raise" in sys.argv
  9. errcount = 0
  10. def check(name, func, z, y):
  11. global errcount
  12. try:
  13. x = func(z)
  14. except:
  15. errcount += 1
  16. if raise_:
  17. raise
  18. print()
  19. print(name)
  20. print("EXCEPTION")
  21. import traceback
  22. traceback.print_tb(sys.exc_info()[2])
  23. print()
  24. return
  25. xre = x.real
  26. xim = x.imag
  27. yre = y.real
  28. yim = y.imag
  29. tol = eps*8
  30. err = 0
  31. if abs(xre-yre) > abs(yre)*tol:
  32. err = 1
  33. print()
  34. print("Error! %s (re = %s, wanted %s, err=%s)" % (name, nstr(xre,10), nstr(yre,10), nstr(abs(xre-yre))))
  35. errcount += 1
  36. if raise_:
  37. raise SystemExit
  38. if abs(xim-yim) > abs(yim)*tol:
  39. err = 1
  40. print()
  41. print("Error! %s (im = %s, wanted %s, err=%s)" % (name, nstr(xim,10), nstr(yim,10), nstr(abs(xim-yim))))
  42. errcount += 1
  43. if raise_:
  44. raise SystemExit
  45. if not err:
  46. sys.stdout.write("%s ok; " % name)
  47. def testcase(case):
  48. z, result = case
  49. print("Testing z =", z)
  50. mp.dps = 1010
  51. z = eval(z)
  52. mp.dps = maxdps + 50
  53. if result is None:
  54. gamma_val = gamma(z)
  55. loggamma_val = loggamma(z)
  56. factorial_val = factorial(z)
  57. rgamma_val = rgamma(z)
  58. else:
  59. loggamma_val = eval(result)
  60. gamma_val = exp(loggamma_val)
  61. factorial_val = z * gamma_val
  62. rgamma_val = 1/gamma_val
  63. for dps in [5, 10, 15, 25, 40, 60, 90, 120, 250, 600, 1000, 1800, 3600]:
  64. if dps > maxdps:
  65. break
  66. mp.dps = dps
  67. print("dps = %s" % dps)
  68. check("gamma", gamma, z, gamma_val)
  69. check("rgamma", rgamma, z, rgamma_val)
  70. check("loggamma", loggamma, z, loggamma_val)
  71. check("factorial", factorial, z, factorial_val)
  72. print()
  73. mp.dps = 15
  74. testcases = []
  75. # Basic values
  76. for n in list(range(1,200)) + list(range(201,2000,17)):
  77. testcases.append(["%s" % n, None])
  78. for n in range(-200,200):
  79. testcases.append(["%s+0.5" % n, None])
  80. testcases.append(["%s+0.37" % n, None])
  81. testcases += [\
  82. ["(0.1+1j)", None],
  83. ["(-0.1+1j)", None],
  84. ["(0.1-1j)", None],
  85. ["(-0.1-1j)", None],
  86. ["10j", None],
  87. ["-10j", None],
  88. ["100j", None],
  89. ["10000j", None],
  90. ["-10000000j", None],
  91. ["(10**100)*j", None],
  92. ["125+(10**100)*j", None],
  93. ["-125+(10**100)*j", None],
  94. ["(10**10)*(1+j)", None],
  95. ["(10**10)*(-1+j)", None],
  96. ["(10**100)*(1+j)", None],
  97. ["(10**100)*(-1+j)", None],
  98. ["(1.5-1j)", None],
  99. ["(6+4j)", None],
  100. ["(4+1j)", None],
  101. ["(3.5+2j)", None],
  102. ["(1.5-1j)", None],
  103. ["(-6-4j)", None],
  104. ["(-2-3j)", None],
  105. ["(-2.5-2j)", None],
  106. ["(4+1j)", None],
  107. ["(3+3j)", None],
  108. ["(2-2j)", None],
  109. ["1", "0"],
  110. ["2", "0"],
  111. ["3", "log(2)"],
  112. ["4", "log(6)"],
  113. ["5", "log(24)"],
  114. ["0.5", "log(pi)/2"],
  115. ["1.5", "log(sqrt(pi)/2)"],
  116. ["2.5", "log(3*sqrt(pi)/4)"],
  117. ["mpf('0.37')", None],
  118. ["0.25", "log(sqrt(2*sqrt(2*pi**3)/agm(1,sqrt(2))))"],
  119. ["-0.4", None],
  120. ["mpf('-1.9')", None],
  121. ["mpf('12.8')", None],
  122. ["mpf('33.7')", None],
  123. ["mpf('95.2')", None],
  124. ["mpf('160.3')", None],
  125. ["mpf('2057.8')", None],
  126. ["25", "log(ifac(24))"],
  127. ["80", "log(ifac(79))"],
  128. ["500", "log(ifac(500-1))"],
  129. ["8000", "log(ifac(8000-1))"],
  130. ["8000.5", None],
  131. ["mpf('8000.1')", None],
  132. ["mpf('1.37e10')", None],
  133. ["mpf('1.37e10')*(1+j)", None],
  134. ["mpf('1.37e10')*(-1+j)", None],
  135. ["mpf('1.37e10')*(-1-j)", None],
  136. ["mpf('1.37e10')*(-1+j)", None],
  137. ["mpf('1.37e100')", None],
  138. ["mpf('1.37e100')*(1+j)", None],
  139. ["mpf('1.37e100')*(-1+j)", None],
  140. ["mpf('1.37e100')*(-1-j)", None],
  141. ["mpf('1.37e100')*(-1+j)", None],
  142. ["3+4j",
  143. "mpc('"
  144. "-1.7566267846037841105306041816232757851567066070613445016197619371316057169"
  145. "4723618263960834804618463052988607348289672535780644470689771115236512106002"
  146. "5970873471563240537307638968509556191696167970488390423963867031934333890838"
  147. "8009531786948197210025029725361069435208930363494971027388382086721660805397"
  148. "9163230643216054580167976201709951509519218635460317367338612500626714783631"
  149. "7498317478048447525674016344322545858832610325861086336204591943822302971823"
  150. "5161814175530618223688296232894588415495615809337292518431903058265147109853"
  151. "1710568942184987827643886816200452860853873815413367529829631430146227470517"
  152. "6579967222200868632179482214312673161276976117132204633283806161971389519137"
  153. "1243359764435612951384238091232760634271570950240717650166551484551654327989"
  154. "9360285030081716934130446150245110557038117075172576825490035434069388648124"
  155. "6678152254554001586736120762641422590778766100376515737713938521275749049949"
  156. "1284143906816424244705094759339932733567910991920631339597278805393743140853"
  157. "391550313363278558195609260225928','"
  158. "4.74266443803465792819488940755002274088830335171164611359052405215840070271"
  159. "5906813009373171139767051863542508136875688550817670379002790304870822775498"
  160. "2809996675877564504192565392367259119610438951593128982646945990372179860613"
  161. "4294436498090428077839141927485901735557543641049637962003652638924845391650"
  162. "9546290137755550107224907606529385248390667634297183361902055842228798984200"
  163. "9591180450211798341715874477629099687609819466457990642030707080894518168924"
  164. "6805549314043258530272479246115112769957368212585759640878745385160943755234"
  165. "9398036774908108204370323896757543121853650025529763655312360354244898913463"
  166. "7115955702828838923393113618205074162812089732064414530813087483533203244056"
  167. "0546577484241423134079056537777170351934430586103623577814746004431994179990"
  168. "5318522939077992613855205801498201930221975721246498720895122345420698451980"
  169. "0051215797310305885845964334761831751370672996984756815410977750799748813563"
  170. "8784405288158432214886648743541773208808731479748217023665577802702269468013"
  171. "673719173759245720489020315779001')"],
  172. ]
  173. for z in [4, 14, 34, 64]:
  174. testcases.append(["(2+j)*%s/3" % z, None])
  175. testcases.append(["(-2+j)*%s/3" % z, None])
  176. testcases.append(["(1+2*j)*%s/3" % z, None])
  177. testcases.append(["(2-j)*%s/3" % z, None])
  178. testcases.append(["(20+j)*%s/3" % z, None])
  179. testcases.append(["(-20+j)*%s/3" % z, None])
  180. testcases.append(["(1+20*j)*%s/3" % z, None])
  181. testcases.append(["(20-j)*%s/3" % z, None])
  182. testcases.append(["(200+j)*%s/3" % z, None])
  183. testcases.append(["(-200+j)*%s/3" % z, None])
  184. testcases.append(["(1+200*j)*%s/3" % z, None])
  185. testcases.append(["(200-j)*%s/3" % z, None])
  186. # Poles
  187. for n in [0,1,2,3,4,25,-1,-2,-3,-4,-20,-21,-50,-51,-200,-201,-20000,-20001]:
  188. for t in ['1e-5', '1e-20', '1e-100', '1e-10000']:
  189. testcases.append(["fadd(%s,'%s',exact=True)" % (n, t), None])
  190. testcases.append(["fsub(%s,'%s',exact=True)" % (n, t), None])
  191. testcases.append(["fadd(%s,'%sj',exact=True)" % (n, t), None])
  192. testcases.append(["fsub(%s,'%sj',exact=True)" % (n, t), None])
  193. if __name__ == "__main__":
  194. from timeit import default_timer as clock
  195. tot_time = 0.0
  196. for case in testcases:
  197. t1 = clock()
  198. testcase(case)
  199. t2 = clock()
  200. print("Test time:", t2-t1)
  201. print()
  202. tot_time += (t2-t1)
  203. print("Total time:", tot_time)
  204. print("Errors:", errcount)