test_calculus.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. import pytest
  2. from mpmath import *
  3. def test_approximation():
  4. mp.dps = 15
  5. f = lambda x: cos(2-2*x)/x
  6. p, err = chebyfit(f, [2, 4], 8, error=True)
  7. assert err < 1e-5
  8. for i in range(10):
  9. x = 2 + i/5.
  10. assert abs(polyval(p, x) - f(x)) < err
  11. def test_limits():
  12. mp.dps = 15
  13. assert limit(lambda x: (x-sin(x))/x**3, 0).ae(mpf(1)/6)
  14. assert limit(lambda n: (1+1/n)**n, inf).ae(e)
  15. def test_polyval():
  16. assert polyval([], 3) == 0
  17. assert polyval([0], 3) == 0
  18. assert polyval([5], 3) == 5
  19. # 4x^3 - 2x + 5
  20. p = [4, 0, -2, 5]
  21. assert polyval(p,4) == 253
  22. assert polyval(p,4,derivative=True) == (253, 190)
  23. def test_polyroots():
  24. p = polyroots([1,-4])
  25. assert p[0].ae(4)
  26. p, q = polyroots([1,2,3])
  27. assert p.ae(-1 - sqrt(2)*j)
  28. assert q.ae(-1 + sqrt(2)*j)
  29. #this is not a real test, it only tests a specific case
  30. assert polyroots([1]) == []
  31. pytest.raises(ValueError, lambda: polyroots([0]))
  32. def test_polyroots_legendre():
  33. n = 64
  34. coeffs = [11975573020964041433067793888190275875, 0,
  35. -190100434726484311252477736051902332000, 0,
  36. 1437919688271127330313741595496589239248, 0,
  37. -6897338342113537600691931230430793911840, 0,
  38. 23556405536185284408974715545252277554280, 0,
  39. -60969520211303089058522793175947071316960, 0,
  40. 124284021969194758465450309166353645376880, 0,
  41. -204721258548015217049921875719981284186016, 0,
  42. 277415422258095841688223780704620656114900, 0,
  43. -313237834141273382807123548182995095192800, 0,
  44. 297432255354328395601259515935229287637200, 0,
  45. -239057700565161140389797367947941296605600, 0,
  46. 163356095386193445933028201431093219347160, 0,
  47. -95158890516229191805647495979277603503200, 0,
  48. 47310254620162038075933656063247634556400, 0,
  49. -20071017111583894941305187420771723751200, 0,
  50. 7255051932731034189479516844750603752850, 0,
  51. -2228176940331017311443863996901733412640, 0,
  52. 579006552594977616773047095969088431600, 0,
  53. -126584428502545713788439446082310831200, 0,
  54. 23112325428835593809686977515028663000, 0,
  55. -3491517141958743235617737161547844000, 0,
  56. 431305058712550634988073414073557200, 0,
  57. -42927166660756742088912492757452000, 0,
  58. 3378527005707706553294038781836500, 0,
  59. -205277590220215081719131470288800, 0,
  60. 9330799555464321896324157740400, 0,
  61. -304114948474392713657972548576, 0,
  62. 6695289961520387531608984680, 0,
  63. -91048139350447232095702560, 0,
  64. 659769125727878493447120, 0,
  65. -1905929106580294155360, 0,
  66. 916312070471295267]
  67. with mp.workdps(3):
  68. with pytest.raises(mp.NoConvergence):
  69. polyroots(coeffs, maxsteps=5, cleanup=True, error=False,
  70. extraprec=n*10)
  71. roots = polyroots(coeffs, maxsteps=50, cleanup=True, error=False,
  72. extraprec=n*10)
  73. roots = [str(r) for r in roots]
  74. assert roots == \
  75. ['-0.999', '-0.996', '-0.991', '-0.983', '-0.973', '-0.961',
  76. '-0.946', '-0.93', '-0.911', '-0.889', '-0.866', '-0.841',
  77. '-0.813', '-0.784', '-0.753', '-0.72', '-0.685', '-0.649',
  78. '-0.611', '-0.572', '-0.531', '-0.489', '-0.446', '-0.402',
  79. '-0.357', '-0.311', '-0.265', '-0.217', '-0.17', '-0.121',
  80. '-0.073', '-0.0243', '0.0243', '0.073', '0.121', '0.17', '0.217',
  81. '0.265', '0.311', '0.357', '0.402', '0.446', '0.489', '0.531',
  82. '0.572', '0.611', '0.649', '0.685', '0.72', '0.753', '0.784',
  83. '0.813', '0.841', '0.866', '0.889', '0.911', '0.93', '0.946',
  84. '0.961', '0.973', '0.983', '0.991', '0.996', '0.999']
  85. def test_polyroots_legendre_init():
  86. extra_prec = 100
  87. coeffs = [11975573020964041433067793888190275875, 0,
  88. -190100434726484311252477736051902332000, 0,
  89. 1437919688271127330313741595496589239248, 0,
  90. -6897338342113537600691931230430793911840, 0,
  91. 23556405536185284408974715545252277554280, 0,
  92. -60969520211303089058522793175947071316960, 0,
  93. 124284021969194758465450309166353645376880, 0,
  94. -204721258548015217049921875719981284186016, 0,
  95. 277415422258095841688223780704620656114900, 0,
  96. -313237834141273382807123548182995095192800, 0,
  97. 297432255354328395601259515935229287637200, 0,
  98. -239057700565161140389797367947941296605600, 0,
  99. 163356095386193445933028201431093219347160, 0,
  100. -95158890516229191805647495979277603503200, 0,
  101. 47310254620162038075933656063247634556400, 0,
  102. -20071017111583894941305187420771723751200, 0,
  103. 7255051932731034189479516844750603752850, 0,
  104. -2228176940331017311443863996901733412640, 0,
  105. 579006552594977616773047095969088431600, 0,
  106. -126584428502545713788439446082310831200, 0,
  107. 23112325428835593809686977515028663000, 0,
  108. -3491517141958743235617737161547844000, 0,
  109. 431305058712550634988073414073557200, 0,
  110. -42927166660756742088912492757452000, 0,
  111. 3378527005707706553294038781836500, 0,
  112. -205277590220215081719131470288800, 0,
  113. 9330799555464321896324157740400, 0,
  114. -304114948474392713657972548576, 0,
  115. 6695289961520387531608984680, 0,
  116. -91048139350447232095702560, 0,
  117. 659769125727878493447120, 0,
  118. -1905929106580294155360, 0,
  119. 916312070471295267]
  120. roots_init = matrix(['-0.999', '-0.996', '-0.991', '-0.983', '-0.973',
  121. '-0.961', '-0.946', '-0.93', '-0.911', '-0.889',
  122. '-0.866', '-0.841', '-0.813', '-0.784', '-0.753',
  123. '-0.72', '-0.685', '-0.649', '-0.611', '-0.572',
  124. '-0.531', '-0.489', '-0.446', '-0.402', '-0.357',
  125. '-0.311', '-0.265', '-0.217', '-0.17', '-0.121',
  126. '-0.073', '-0.0243', '0.0243', '0.073', '0.121',
  127. '0.17', '0.217', '0.265', ' 0.311', '0.357',
  128. '0.402', '0.446', '0.489', '0.531', '0.572',
  129. '0.611', '0.649', '0.685', '0.72', '0.753',
  130. '0.784', '0.813', '0.841', '0.866', '0.889',
  131. '0.911', '0.93', '0.946', '0.961', '0.973',
  132. '0.983', '0.991', '0.996', '0.999', '1.0'])
  133. with mp.workdps(2*mp.dps):
  134. roots_exact = polyroots(coeffs, maxsteps=50, cleanup=True, error=False,
  135. extraprec=2*extra_prec)
  136. with pytest.raises(mp.NoConvergence):
  137. polyroots(coeffs, maxsteps=5, cleanup=True, error=False,
  138. extraprec=extra_prec)
  139. roots,err = polyroots(coeffs, maxsteps=5, cleanup=True, error=True,
  140. extraprec=extra_prec,roots_init=roots_init)
  141. assert max(matrix(roots_exact)-matrix(roots).apply(abs)) < err
  142. roots1,err1 = polyroots(coeffs, maxsteps=25, cleanup=True, error=True,
  143. extraprec=extra_prec,roots_init=roots_init[:60])
  144. assert max(matrix(roots_exact)-matrix(roots1).apply(abs)) < err1
  145. def test_pade():
  146. one = mpf(1)
  147. mp.dps = 20
  148. N = 10
  149. a = [one]
  150. k = 1
  151. for i in range(1, N+1):
  152. k *= i
  153. a.append(one/k)
  154. p, q = pade(a, N//2, N//2)
  155. for x in arange(0, 1, 0.1):
  156. r = polyval(p[::-1], x)/polyval(q[::-1], x)
  157. assert(r.ae(exp(x), 1.0e-10))
  158. mp.dps = 15
  159. def test_fourier():
  160. mp.dps = 15
  161. c, s = fourier(lambda x: x+1, [-1, 2], 2)
  162. #plot([lambda x: x+1, lambda x: fourierval((c, s), [-1, 2], x)], [-1, 2])
  163. assert c[0].ae(1.5)
  164. assert c[1].ae(-3*sqrt(3)/(2*pi))
  165. assert c[2].ae(3*sqrt(3)/(4*pi))
  166. assert s[0] == 0
  167. assert s[1].ae(3/(2*pi))
  168. assert s[2].ae(3/(4*pi))
  169. assert fourierval((c, s), [-1, 2], 1).ae(1.9134966715663442)
  170. def test_differint():
  171. mp.dps = 15
  172. assert differint(lambda t: t, 2, -0.5).ae(8*sqrt(2/pi)/3)
  173. def test_invlap():
  174. mp.dps = 15
  175. t = 0.01
  176. fp = lambda p: 1/(p+1)**2
  177. ft = lambda t: t*exp(-t)
  178. ftt = ft(t)
  179. assert invertlaplace(fp,t,method='talbot').ae(ftt)
  180. assert invertlaplace(fp,t,method='stehfest').ae(ftt)
  181. assert invertlaplace(fp,t,method='dehoog').ae(ftt)
  182. t = 1.0
  183. ftt = ft(t)
  184. assert invertlaplace(fp,t,method='talbot').ae(ftt)
  185. assert invertlaplace(fp,t,method='stehfest').ae(ftt)
  186. assert invertlaplace(fp,t,method='dehoog').ae(ftt)
  187. t = 0.01
  188. fp = lambda p: log(p)/p
  189. ft = lambda t: -euler-log(t)
  190. ftt = ft(t)
  191. assert invertlaplace(fp,t,method='talbot').ae(ftt)
  192. assert invertlaplace(fp,t,method='stehfest').ae(ftt)
  193. assert invertlaplace(fp,t,method='dehoog').ae(ftt)
  194. t = 1.0
  195. ftt = ft(t)
  196. assert invertlaplace(fp,t,method='talbot').ae(ftt)
  197. assert invertlaplace(fp,t,method='stehfest').ae(ftt)
  198. assert invertlaplace(fp,t,method='dehoog').ae(ftt)