Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. from mpmath import mp
  2.  
  3. # Use 50 decimal digits of precision
  4. mp.dps = 50
  5.  
  6. # Print with 10 digits of precision
  7. out_prec = 10
  8.  
  9. def func(x):
  10. return mp.e - x * x * mp.exp(x)
  11.  
  12. def inv_func(y, sign, k):
  13. return 2 * mp.lambertw(sign * mp.sqrt(mp.e - y) / 2, k=k)
  14.  
  15. r = mp.mpf('.1')
  16. for i in range(-25, 15):
  17. x = i * r
  18. y = func(x)
  19.  
  20. # Determine which square root and branch of the Lambert W
  21. # function we need to get back the x we started with
  22. if x < -2:
  23. sign, k = -1, -1
  24. elif -2 <= x < 0:
  25. sign, k = -1, 0
  26. else:
  27. sign, k = 1, 0
  28.  
  29. xx = inv_func(y, sign, k)
  30. print(x, mp.nstr(y, n=out_prec), mp.nstr(xx, n=out_prec))
  31.  
  32. -2.5 2.205250587 -2.5
  33. -2.4 2.195746418 -2.4
  34. -2.3 2.187912545 -2.3
  35. -2.2 2.181994542 -2.2
  36. -2.1 2.17824898 -2.1
  37. -2.0 2.176940696 -2.0
  38. -1.9 2.178339113 -1.9
  39. -1.8 2.182713431 -1.8
  40. -1.7 2.190326444 -1.7
  41. -1.6 2.201426742 -1.6
  42. -1.5 2.216238968 -1.5
  43. -1.4 2.234951779 -1.4
  44. -1.3 2.257703098 -1.3
  45. -1.2 2.284562163 -1.2
  46. -1.1 2.315507817 -1.1
  47. -1.0 2.350402387 -1.0
  48. -0.9 2.388960404 -0.9
  49. -0.8 2.430711291 -0.8
  50. -0.7 2.47495503 -0.7
  51. -0.6 2.520709639 -0.6
  52. -0.5 2.566649164 -0.5
  53. -0.4 2.611030621 -0.4
  54. -0.3 2.651608189 -0.3
  55. -0.2 2.685532598 -0.2
  56. -0.1 2.709233454 -0.1
  57. 0.0 2.718281828 0.0
  58. 0.1 2.707230119 0.1
  59. 0.2 2.669425718 0.2
  60. 0.3 2.596794536 0.3
  61. 0.4 2.479589877 0.4
  62. 0.5 2.306101511 0.5
  63. 0.6 2.06231906 0.6
  64. 0.7 1.731543002 0.7
  65. 0.8 1.293935634 0.8
  66. 0.9 0.7260033084 0.9
  67. 1.0 0.0 1.0
  68. 1.1 -0.9167590605 1.1
  69. 1.2 -2.06268654 1.2
  70. 1.3 -3.48282954 1.3
  71. 1.4 -5.229910107 1.4
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement