test_summation.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. from mpmath import *
  2. def test_sumem():
  3. mp.dps = 15
  4. assert sumem(lambda k: 1/k**2.5, [50, 100]).ae(0.0012524505324784962)
  5. assert sumem(lambda k: k**4 + 3*k + 1, [10, 100]).ae(2050333103)
  6. def test_nsum():
  7. mp.dps = 15
  8. assert nsum(lambda x: x**2, [1, 3]) == 14
  9. assert nsum(lambda k: 1/factorial(k), [0, inf]).ae(e)
  10. assert nsum(lambda k: (-1)**(k+1) / k, [1, inf]).ae(log(2))
  11. assert nsum(lambda k: (-1)**(k+1) / k**2, [1, inf]).ae(pi**2 / 12)
  12. assert nsum(lambda k: (-1)**k / log(k), [2, inf]).ae(0.9242998972229388)
  13. assert nsum(lambda k: 1/k**2, [1, inf]).ae(pi**2 / 6)
  14. assert nsum(lambda k: 2**k/fac(k), [0, inf]).ae(exp(2))
  15. assert nsum(lambda k: 1/k**2, [4, inf], method='e').ae(0.2838229557371153)
  16. def test_nprod():
  17. mp.dps = 15
  18. assert nprod(lambda k: exp(1/k**2), [1,inf], method='r').ae(exp(pi**2/6))
  19. assert nprod(lambda x: x**2, [1, 3]) == 36
  20. def test_fsum():
  21. mp.dps = 15
  22. assert fsum([]) == 0
  23. assert fsum([-4]) == -4
  24. assert fsum([2,3]) == 5
  25. assert fsum([1e-100,1]) == 1
  26. assert fsum([1,1e-100]) == 1
  27. assert fsum([1e100,1]) == 1e100
  28. assert fsum([1,1e100]) == 1e100
  29. assert fsum([1e-100,0]) == 1e-100
  30. assert fsum([1e-100,1e100,1e-100]) == 1e100
  31. assert fsum([2,1+1j,1]) == 4+1j
  32. assert fsum([2,inf,3]) == inf
  33. assert fsum([2,-1], absolute=1) == 3
  34. assert fsum([2,-1], squared=1) == 5
  35. assert fsum([1,1+j], squared=1) == 1+2j
  36. assert fsum([1,3+4j], absolute=1) == 6
  37. assert fsum([1,2+3j], absolute=1, squared=1) == 14
  38. assert isnan(fsum([inf,-inf]))
  39. assert fsum([inf,-inf], absolute=1) == inf
  40. assert fsum([inf,-inf], squared=1) == inf
  41. assert fsum([inf,-inf], absolute=1, squared=1) == inf
  42. assert iv.fsum([1,mpi(2,3)]) == mpi(3,4)
  43. def test_fprod():
  44. mp.dps = 15
  45. assert fprod([]) == 1
  46. assert fprod([2,3]) == 6