test_evalf.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  1. import math
  2. from sympy.concrete.products import (Product, product)
  3. from sympy.concrete.summations import Sum
  4. from sympy.core.add import Add
  5. from sympy.core.evalf import N
  6. from sympy.core.function import (Function, nfloat)
  7. from sympy.core.mul import Mul
  8. from sympy.core import (GoldenRatio)
  9. from sympy.core.numbers import (AlgebraicNumber, E, Float, I, Rational,
  10. oo, zoo, nan, pi)
  11. from sympy.core.power import Pow
  12. from sympy.core.relational import Eq
  13. from sympy.core.singleton import S
  14. from sympy.core.symbol import Symbol
  15. from sympy.core.sympify import sympify
  16. from sympy.functions.combinatorial.factorials import factorial
  17. from sympy.functions.combinatorial.numbers import fibonacci
  18. from sympy.functions.elementary.complexes import (Abs, re, im)
  19. from sympy.functions.elementary.exponential import (exp, log)
  20. from sympy.functions.elementary.hyperbolic import (acosh, cosh)
  21. from sympy.functions.elementary.integers import (ceiling, floor)
  22. from sympy.functions.elementary.miscellaneous import (Max, sqrt)
  23. from sympy.functions.elementary.trigonometric import (acos, atan, cos, sin, tan)
  24. from sympy.integrals.integrals import (Integral, integrate)
  25. from sympy.polys.polytools import factor
  26. from sympy.polys.rootoftools import CRootOf
  27. from sympy.polys.specialpolys import cyclotomic_poly
  28. from sympy.printing import srepr
  29. from sympy.printing.str import sstr
  30. from sympy.simplify.simplify import simplify
  31. from sympy.core.numbers import comp
  32. from sympy.core.evalf import (complex_accuracy, PrecisionExhausted,
  33. scaled_zero, get_integer_part, as_mpmath, evalf, _evalf_with_bounded_error)
  34. from mpmath import inf, ninf, make_mpc
  35. from mpmath.libmp.libmpf import from_float, fzero
  36. from sympy.core.expr import unchanged
  37. from sympy.testing.pytest import raises, XFAIL
  38. from sympy.abc import n, x, y
  39. def NS(e, n=15, **options):
  40. return sstr(sympify(e).evalf(n, **options), full_prec=True)
  41. def test_evalf_helpers():
  42. from mpmath.libmp import finf
  43. assert complex_accuracy((from_float(2.0), None, 35, None)) == 35
  44. assert complex_accuracy((from_float(2.0), from_float(10.0), 35, 100)) == 37
  45. assert complex_accuracy(
  46. (from_float(2.0), from_float(1000.0), 35, 100)) == 43
  47. assert complex_accuracy((from_float(2.0), from_float(10.0), 100, 35)) == 35
  48. assert complex_accuracy(
  49. (from_float(2.0), from_float(1000.0), 100, 35)) == 35
  50. assert complex_accuracy(finf) == math.inf
  51. assert complex_accuracy(zoo) == math.inf
  52. raises(ValueError, lambda: get_integer_part(zoo, 1, {}))
  53. def test_evalf_basic():
  54. assert NS('pi', 15) == '3.14159265358979'
  55. assert NS('2/3', 10) == '0.6666666667'
  56. assert NS('355/113-pi', 6) == '2.66764e-7'
  57. assert NS('16*atan(1/5)-4*atan(1/239)', 15) == '3.14159265358979'
  58. def test_cancellation():
  59. assert NS(Add(pi, Rational(1, 10**1000), -pi, evaluate=False), 15,
  60. maxn=1200) == '1.00000000000000e-1000'
  61. def test_evalf_powers():
  62. assert NS('pi**(10**20)', 10) == '1.339148777e+49714987269413385435'
  63. assert NS(pi**(10**100), 10) == ('4.946362032e+4971498726941338543512682882'
  64. '9089887365167832438044244613405349992494711208'
  65. '95526746555473864642912223')
  66. assert NS('2**(1/10**50)', 15) == '1.00000000000000'
  67. assert NS('2**(1/10**50)-1', 15) == '6.93147180559945e-51'
  68. # Evaluation of Rump's ill-conditioned polynomial
  69. def test_evalf_rump():
  70. a = 1335*y**6/4 + x**2*(11*x**2*y**2 - y**6 - 121*y**4 - 2) + 11*y**8/2 + x/(2*y)
  71. assert NS(a, 15, subs={x: 77617, y: 33096}) == '-0.827396059946821'
  72. def test_evalf_complex():
  73. assert NS('2*sqrt(pi)*I', 10) == '3.544907702*I'
  74. assert NS('3+3*I', 15) == '3.00000000000000 + 3.00000000000000*I'
  75. assert NS('E+pi*I', 15) == '2.71828182845905 + 3.14159265358979*I'
  76. assert NS('pi * (3+4*I)', 15) == '9.42477796076938 + 12.5663706143592*I'
  77. assert NS('I*(2+I)', 15) == '-1.00000000000000 + 2.00000000000000*I'
  78. @XFAIL
  79. def test_evalf_complex_bug():
  80. assert NS('(pi+E*I)*(E+pi*I)', 15) in ('0.e-15 + 17.25866050002*I',
  81. '0.e-17 + 17.25866050002*I', '-0.e-17 + 17.25866050002*I')
  82. def test_evalf_complex_powers():
  83. assert NS('(E+pi*I)**100000000000000000') == \
  84. '-3.58896782867793e+61850354284995199 + 4.58581754997159e+61850354284995199*I'
  85. # XXX: rewrite if a+a*I simplification introduced in SymPy
  86. #assert NS('(pi + pi*I)**2') in ('0.e-15 + 19.7392088021787*I', '0.e-16 + 19.7392088021787*I')
  87. assert NS('(pi + pi*I)**2', chop=True) == '19.7392088021787*I'
  88. assert NS(
  89. '(pi + 1/10**8 + pi*I)**2') == '6.2831853e-8 + 19.7392088650106*I'
  90. assert NS('(pi + 1/10**12 + pi*I)**2') == '6.283e-12 + 19.7392088021850*I'
  91. assert NS('(pi + pi*I)**4', chop=True) == '-389.636364136010'
  92. assert NS(
  93. '(pi + 1/10**8 + pi*I)**4') == '-389.636366616512 + 2.4805021e-6*I'
  94. assert NS('(pi + 1/10**12 + pi*I)**4') == '-389.636364136258 + 2.481e-10*I'
  95. assert NS(
  96. '(10000*pi + 10000*pi*I)**4', chop=True) == '-3.89636364136010e+18'
  97. @XFAIL
  98. def test_evalf_complex_powers_bug():
  99. assert NS('(pi + pi*I)**4') == '-389.63636413601 + 0.e-14*I'
  100. def test_evalf_exponentiation():
  101. assert NS(sqrt(-pi)) == '1.77245385090552*I'
  102. assert NS(Pow(pi*I, Rational(
  103. 1, 2), evaluate=False)) == '1.25331413731550 + 1.25331413731550*I'
  104. assert NS(pi**I) == '0.413292116101594 + 0.910598499212615*I'
  105. assert NS(pi**(E + I/3)) == '20.8438653991931 + 8.36343473930031*I'
  106. assert NS((pi + I/3)**(E + I/3)) == '17.2442906093590 + 13.6839376767037*I'
  107. assert NS(exp(pi)) == '23.1406926327793'
  108. assert NS(exp(pi + E*I)) == '-21.0981542849657 + 9.50576358282422*I'
  109. assert NS(pi**pi) == '36.4621596072079'
  110. assert NS((-pi)**pi) == '-32.9138577418939 - 15.6897116534332*I'
  111. assert NS((-pi)**(-pi)) == '-0.0247567717232697 + 0.0118013091280262*I'
  112. # An example from Smith, "Multiple Precision Complex Arithmetic and Functions"
  113. def test_evalf_complex_cancellation():
  114. A = Rational('63287/100000')
  115. B = Rational('52498/100000')
  116. C = Rational('69301/100000')
  117. D = Rational('83542/100000')
  118. F = Rational('2231321613/2500000000')
  119. # XXX: the number of returned mantissa digits in the real part could
  120. # change with the implementation. What matters is that the returned digits are
  121. # correct; those that are showing now are correct.
  122. # >>> ((A+B*I)*(C+D*I)).expand()
  123. # 64471/10000000000 + 2231321613*I/2500000000
  124. # >>> 2231321613*4
  125. # 8925286452L
  126. assert NS((A + B*I)*(C + D*I), 6) == '6.44710e-6 + 0.892529*I'
  127. assert NS((A + B*I)*(C + D*I), 10) == '6.447100000e-6 + 0.8925286452*I'
  128. assert NS((A + B*I)*(
  129. C + D*I) - F*I, 5) in ('6.4471e-6 + 0.e-14*I', '6.4471e-6 - 0.e-14*I')
  130. def test_evalf_logs():
  131. assert NS("log(3+pi*I)", 15) == '1.46877619736226 + 0.808448792630022*I'
  132. assert NS("log(pi*I)", 15) == '1.14472988584940 + 1.57079632679490*I'
  133. assert NS('log(-1 + 0.00001)', 2) == '-1.0e-5 + 3.1*I'
  134. assert NS('log(100, 10, evaluate=False)', 15) == '2.00000000000000'
  135. assert NS('-2*I*log(-(-1)**(S(1)/9))', 15) == '-5.58505360638185'
  136. def test_evalf_trig():
  137. assert NS('sin(1)', 15) == '0.841470984807897'
  138. assert NS('cos(1)', 15) == '0.540302305868140'
  139. assert NS('sin(10**-6)', 15) == '9.99999999999833e-7'
  140. assert NS('cos(10**-6)', 15) == '0.999999999999500'
  141. assert NS('sin(E*10**100)', 15) == '0.409160531722613'
  142. # Some input near roots
  143. assert NS(sin(exp(pi*sqrt(163))*pi), 15) == '-2.35596641936785e-12'
  144. assert NS(sin(pi*10**100 + Rational(7, 10**5), evaluate=False), 15, maxn=120) == \
  145. '6.99999999428333e-5'
  146. assert NS(sin(Rational(7, 10**5), evaluate=False), 15) == \
  147. '6.99999999428333e-5'
  148. # Check detection of various false identities
  149. def test_evalf_near_integers():
  150. # Binet's formula
  151. f = lambda n: ((1 + sqrt(5))**n)/(2**n * sqrt(5))
  152. assert NS(f(5000) - fibonacci(5000), 10, maxn=1500) == '5.156009964e-1046'
  153. # Some near-integer identities from
  154. # http://mathworld.wolfram.com/AlmostInteger.html
  155. assert NS('sin(2017*2**(1/5))', 15) == '-1.00000000000000'
  156. assert NS('sin(2017*2**(1/5))', 20) == '-0.99999999999999997857'
  157. assert NS('1+sin(2017*2**(1/5))', 15) == '2.14322287389390e-17'
  158. assert NS('45 - 613*E/37 + 35/991', 15) == '6.03764498766326e-11'
  159. def test_evalf_ramanujan():
  160. assert NS(exp(pi*sqrt(163)) - 640320**3 - 744, 10) == '-7.499274028e-13'
  161. # A related identity
  162. A = 262537412640768744*exp(-pi*sqrt(163))
  163. B = 196884*exp(-2*pi*sqrt(163))
  164. C = 103378831900730205293632*exp(-3*pi*sqrt(163))
  165. assert NS(1 - A - B + C, 10) == '1.613679005e-59'
  166. # Input that for various reasons have failed at some point
  167. def test_evalf_bugs():
  168. assert NS(sin(1) + exp(-10**10), 10) == NS(sin(1), 10)
  169. assert NS(exp(10**10) + sin(1), 10) == NS(exp(10**10), 10)
  170. assert NS('expand_log(log(1+1/10**50))', 20) == '1.0000000000000000000e-50'
  171. assert NS('log(10**100,10)', 10) == '100.0000000'
  172. assert NS('log(2)', 10) == '0.6931471806'
  173. assert NS(
  174. '(sin(x)-x)/x**3', 15, subs={x: '1/10**50'}) == '-0.166666666666667'
  175. assert NS(sin(1) + Rational(
  176. 1, 10**100)*I, 15) == '0.841470984807897 + 1.00000000000000e-100*I'
  177. assert x.evalf() == x
  178. assert NS((1 + I)**2*I, 6) == '-2.00000'
  179. d = {n: (
  180. -1)**Rational(6, 7), y: (-1)**Rational(4, 7), x: (-1)**Rational(2, 7)}
  181. assert NS((x*(1 + y*(1 + n))).subs(d).evalf(), 6) == '0.346011 + 0.433884*I'
  182. assert NS(((-I - sqrt(2)*I)**2).evalf()) == '-5.82842712474619'
  183. assert NS((1 + I)**2*I, 15) == '-2.00000000000000'
  184. # issue 4758 (1/2):
  185. assert NS(pi.evalf(69) - pi) == '-4.43863937855894e-71'
  186. # issue 4758 (2/2): With the bug present, this still only fails if the
  187. # terms are in the order given here. This is not generally the case,
  188. # because the order depends on the hashes of the terms.
  189. assert NS(20 - 5008329267844*n**25 - 477638700*n**37 - 19*n,
  190. subs={n: .01}) == '19.8100000000000'
  191. assert NS(((x - 1)*(1 - x)**1000).n()
  192. ) == '(1.00000000000000 - x)**1000*(x - 1.00000000000000)'
  193. assert NS((-x).n()) == '-x'
  194. assert NS((-2*x).n()) == '-2.00000000000000*x'
  195. assert NS((-2*x*y).n()) == '-2.00000000000000*x*y'
  196. assert cos(x).n(subs={x: 1+I}) == cos(x).subs(x, 1+I).n()
  197. # issue 6660. Also NaN != mpmath.nan
  198. # In this order:
  199. # 0*nan, 0/nan, 0*inf, 0/inf
  200. # 0+nan, 0-nan, 0+inf, 0-inf
  201. # >>> n = Some Number
  202. # n*nan, n/nan, n*inf, n/inf
  203. # n+nan, n-nan, n+inf, n-inf
  204. assert (0*E**(oo)).n() is S.NaN
  205. assert (0/E**(oo)).n() is S.Zero
  206. assert (0+E**(oo)).n() is S.Infinity
  207. assert (0-E**(oo)).n() is S.NegativeInfinity
  208. assert (5*E**(oo)).n() is S.Infinity
  209. assert (5/E**(oo)).n() is S.Zero
  210. assert (5+E**(oo)).n() is S.Infinity
  211. assert (5-E**(oo)).n() is S.NegativeInfinity
  212. #issue 7416
  213. assert as_mpmath(0.0, 10, {'chop': True}) == 0
  214. #issue 5412
  215. assert ((oo*I).n() == S.Infinity*I)
  216. assert ((oo+oo*I).n() == S.Infinity + S.Infinity*I)
  217. #issue 11518
  218. assert NS(2*x**2.5, 5) == '2.0000*x**2.5000'
  219. #issue 13076
  220. assert NS(Mul(Max(0, y), x, evaluate=False).evalf()) == 'x*Max(0, y)'
  221. #issue 18516
  222. assert NS(log(S(3273390607896141870013189696827599152216642046043064789483291368096133796404674554883270092325904157150886684127560071009217256545885393053328527589376)/36360291795869936842385267079543319118023385026001623040346035832580600191583895484198508262979388783308179702534403855752855931517013066142992430916562025780021771247847643450125342836565813209972590371590152578728008385990139795377610001).evalf(15, chop=True)) == '-oo'
  223. def test_evalf_integer_parts():
  224. a = floor(log(8)/log(2) - exp(-1000), evaluate=False)
  225. b = floor(log(8)/log(2), evaluate=False)
  226. assert a.evalf() == 3.0
  227. assert b.evalf() == 3.0
  228. # equals, as a fallback, can still fail but it might succeed as here
  229. assert ceiling(10*(sin(1)**2 + cos(1)**2)) == 10
  230. assert int(floor(factorial(50)/E, evaluate=False).evalf(70)) == \
  231. int(11188719610782480504630258070757734324011354208865721592720336800)
  232. assert int(ceiling(factorial(50)/E, evaluate=False).evalf(70)) == \
  233. int(11188719610782480504630258070757734324011354208865721592720336801)
  234. assert int(floor(GoldenRatio**999 / sqrt(5) + S.Half)
  235. .evalf(1000)) == fibonacci(999)
  236. assert int(floor(GoldenRatio**1000 / sqrt(5) + S.Half)
  237. .evalf(1000)) == fibonacci(1000)
  238. assert ceiling(x).evalf(subs={x: 3}) == 3.0
  239. assert ceiling(x).evalf(subs={x: 3*I}) == 3.0*I
  240. assert ceiling(x).evalf(subs={x: 2 + 3*I}) == 2.0 + 3.0*I
  241. assert ceiling(x).evalf(subs={x: 3.}) == 3.0
  242. assert ceiling(x).evalf(subs={x: 3.*I}) == 3.0*I
  243. assert ceiling(x).evalf(subs={x: 2. + 3*I}) == 2.0 + 3.0*I
  244. assert float((floor(1.5, evaluate=False)+1/9).evalf()) == 1 + 1/9
  245. assert float((floor(0.5, evaluate=False)+20).evalf()) == 20
  246. # issue 19991
  247. n = 1169809367327212570704813632106852886389036911
  248. r = 744723773141314414542111064094745678855643068
  249. assert floor(n / (pi / 2)) == r
  250. assert floor(80782 * sqrt(2)) == 114242
  251. # issue 20076
  252. assert 260515 - floor(260515/pi + 1/2) * pi == atan(tan(260515))
  253. def test_evalf_trig_zero_detection():
  254. a = sin(160*pi, evaluate=False)
  255. t = a.evalf(maxn=100)
  256. assert abs(t) < 1e-100
  257. assert t._prec < 2
  258. assert a.evalf(chop=True) == 0
  259. raises(PrecisionExhausted, lambda: a.evalf(strict=True))
  260. def test_evalf_sum():
  261. assert Sum(n,(n,1,2)).evalf() == 3.
  262. assert Sum(n,(n,1,2)).doit().evalf() == 3.
  263. # the next test should return instantly
  264. assert Sum(1/n,(n,1,2)).evalf() == 1.5
  265. # issue 8219
  266. assert Sum(E/factorial(n), (n, 0, oo)).evalf() == (E*E).evalf()
  267. # issue 8254
  268. assert Sum(2**n*n/factorial(n), (n, 0, oo)).evalf() == (2*E*E).evalf()
  269. # issue 8411
  270. s = Sum(1/x**2, (x, 100, oo))
  271. assert s.n() == s.doit().n()
  272. def test_evalf_divergent_series():
  273. raises(ValueError, lambda: Sum(1/n, (n, 1, oo)).evalf())
  274. raises(ValueError, lambda: Sum(n/(n**2 + 1), (n, 1, oo)).evalf())
  275. raises(ValueError, lambda: Sum((-1)**n, (n, 1, oo)).evalf())
  276. raises(ValueError, lambda: Sum((-1)**n, (n, 1, oo)).evalf())
  277. raises(ValueError, lambda: Sum(n**2, (n, 1, oo)).evalf())
  278. raises(ValueError, lambda: Sum(2**n, (n, 1, oo)).evalf())
  279. raises(ValueError, lambda: Sum((-2)**n, (n, 1, oo)).evalf())
  280. raises(ValueError, lambda: Sum((2*n + 3)/(3*n**2 + 4), (n, 0, oo)).evalf())
  281. raises(ValueError, lambda: Sum((0.5*n**3)/(n**4 + 1), (n, 0, oo)).evalf())
  282. def test_evalf_product():
  283. assert Product(n, (n, 1, 10)).evalf() == 3628800.
  284. assert comp(Product(1 - S.Half**2/n**2, (n, 1, oo)).n(5), 0.63662)
  285. assert Product(n, (n, -1, 3)).evalf() == 0
  286. def test_evalf_py_methods():
  287. assert abs(float(pi + 1) - 4.1415926535897932) < 1e-10
  288. assert abs(complex(pi + 1) - 4.1415926535897932) < 1e-10
  289. assert abs(
  290. complex(pi + E*I) - (3.1415926535897931 + 2.7182818284590451j)) < 1e-10
  291. raises(TypeError, lambda: float(pi + x))
  292. def test_evalf_power_subs_bugs():
  293. assert (x**2).evalf(subs={x: 0}) == 0
  294. assert sqrt(x).evalf(subs={x: 0}) == 0
  295. assert (x**Rational(2, 3)).evalf(subs={x: 0}) == 0
  296. assert (x**x).evalf(subs={x: 0}) == 1.0
  297. assert (3**x).evalf(subs={x: 0}) == 1.0
  298. assert exp(x).evalf(subs={x: 0}) == 1.0
  299. assert ((2 + I)**x).evalf(subs={x: 0}) == 1.0
  300. assert (0**x).evalf(subs={x: 0}) == 1.0
  301. def test_evalf_arguments():
  302. raises(TypeError, lambda: pi.evalf(method="garbage"))
  303. def test_implemented_function_evalf():
  304. from sympy.utilities.lambdify import implemented_function
  305. f = Function('f')
  306. f = implemented_function(f, lambda x: x + 1)
  307. assert str(f(x)) == "f(x)"
  308. assert str(f(2)) == "f(2)"
  309. assert f(2).evalf() == 3.0
  310. assert f(x).evalf() == f(x)
  311. f = implemented_function(Function('sin'), lambda x: x + 1)
  312. assert f(2).evalf() != sin(2)
  313. del f._imp_ # XXX: due to caching _imp_ would influence all other tests
  314. def test_evaluate_false():
  315. for no in [0, False]:
  316. assert Add(3, 2, evaluate=no).is_Add
  317. assert Mul(3, 2, evaluate=no).is_Mul
  318. assert Pow(3, 2, evaluate=no).is_Pow
  319. assert Pow(y, 2, evaluate=True) - Pow(y, 2, evaluate=True) == 0
  320. def test_evalf_relational():
  321. assert Eq(x/5, y/10).evalf() == Eq(0.2*x, 0.1*y)
  322. # if this first assertion fails it should be replaced with
  323. # one that doesn't
  324. assert unchanged(Eq, (3 - I)**2/2 + I, 0)
  325. assert Eq((3 - I)**2/2 + I, 0).n() is S.false
  326. assert nfloat(Eq((3 - I)**2 + I, 0)) == S.false
  327. def test_issue_5486():
  328. assert not cos(sqrt(0.5 + I)).n().is_Function
  329. def test_issue_5486_bug():
  330. from sympy.core.expr import Expr
  331. from sympy.core.numbers import I
  332. assert abs(Expr._from_mpmath(I._to_mpmath(15), 15) - I) < 1.0e-15
  333. def test_bugs():
  334. from sympy.functions.elementary.complexes import (polar_lift, re)
  335. assert abs(re((1 + I)**2)) < 1e-15
  336. # anything that evalf's to 0 will do in place of polar_lift
  337. assert abs(polar_lift(0)).n() == 0
  338. def test_subs():
  339. assert NS('besseli(-x, y) - besseli(x, y)', subs={x: 3.5, y: 20.0}) == \
  340. '-4.92535585957223e-10'
  341. assert NS('Piecewise((x, x>0)) + Piecewise((1-x, x>0))', subs={x: 0.1}) == \
  342. '1.00000000000000'
  343. raises(TypeError, lambda: x.evalf(subs=(x, 1)))
  344. def test_issue_4956_5204():
  345. # issue 4956
  346. v = S('''(-27*12**(1/3)*sqrt(31)*I +
  347. 27*2**(2/3)*3**(1/3)*sqrt(31)*I)/(-2511*2**(2/3)*3**(1/3) +
  348. (29*18**(1/3) + 9*2**(1/3)*3**(2/3)*sqrt(31)*I +
  349. 87*2**(1/3)*3**(1/6)*I)**2)''')
  350. assert NS(v, 1) == '0.e-118 - 0.e-118*I'
  351. # issue 5204
  352. v = S('''-(357587765856 + 18873261792*249**(1/2) + 56619785376*I*83**(1/2) +
  353. 108755765856*I*3**(1/2) + 41281887168*6**(1/3)*(1422 +
  354. 54*249**(1/2))**(1/3) - 1239810624*6**(1/3)*249**(1/2)*(1422 +
  355. 54*249**(1/2))**(1/3) - 3110400000*I*6**(1/3)*83**(1/2)*(1422 +
  356. 54*249**(1/2))**(1/3) + 13478400000*I*3**(1/2)*6**(1/3)*(1422 +
  357. 54*249**(1/2))**(1/3) + 1274950152*6**(2/3)*(1422 +
  358. 54*249**(1/2))**(2/3) + 32347944*6**(2/3)*249**(1/2)*(1422 +
  359. 54*249**(1/2))**(2/3) - 1758790152*I*3**(1/2)*6**(2/3)*(1422 +
  360. 54*249**(1/2))**(2/3) - 304403832*I*6**(2/3)*83**(1/2)*(1422 +
  361. 4*249**(1/2))**(2/3))/(175732658352 + (1106028 + 25596*249**(1/2) +
  362. 76788*I*83**(1/2))**2)''')
  363. assert NS(v, 5) == '0.077284 + 1.1104*I'
  364. assert NS(v, 1) == '0.08 + 1.*I'
  365. def test_old_docstring():
  366. a = (E + pi*I)*(E - pi*I)
  367. assert NS(a) == '17.2586605000200'
  368. assert a.n() == 17.25866050002001
  369. def test_issue_4806():
  370. assert integrate(atan(x)**2, (x, -1, 1)).evalf().round(1) == Float(0.5, 1)
  371. assert atan(0, evaluate=False).n() == 0
  372. def test_evalf_mul():
  373. # SymPy should not try to expand this; it should be handled term-wise
  374. # in evalf through mpmath
  375. assert NS(product(1 + sqrt(n)*I, (n, 1, 500)), 1) == '5.e+567 + 2.e+568*I'
  376. def test_scaled_zero():
  377. a, b = (([0], 1, 100, 1), -1)
  378. assert scaled_zero(100) == (a, b)
  379. assert scaled_zero(a) == (0, 1, 100, 1)
  380. a, b = (([1], 1, 100, 1), -1)
  381. assert scaled_zero(100, -1) == (a, b)
  382. assert scaled_zero(a) == (1, 1, 100, 1)
  383. raises(ValueError, lambda: scaled_zero(scaled_zero(100)))
  384. raises(ValueError, lambda: scaled_zero(100, 2))
  385. raises(ValueError, lambda: scaled_zero(100, 0))
  386. raises(ValueError, lambda: scaled_zero((1, 5, 1, 3)))
  387. def test_chop_value():
  388. for i in range(-27, 28):
  389. assert (Pow(10, i)*2).n(chop=10**i) and not (Pow(10, i)).n(chop=10**i)
  390. def test_infinities():
  391. assert oo.evalf(chop=True) == inf
  392. assert (-oo).evalf(chop=True) == ninf
  393. def test_to_mpmath():
  394. assert sqrt(3)._to_mpmath(20)._mpf_ == (0, int(908093), -19, 20)
  395. assert S(3.2)._to_mpmath(20)._mpf_ == (0, int(838861), -18, 20)
  396. def test_issue_6632_evalf():
  397. add = (-100000*sqrt(2500000001) + 5000000001)
  398. assert add.n() == 9.999999998e-11
  399. assert (add*add).n() == 9.999999996e-21
  400. def test_issue_4945():
  401. from sympy.abc import H
  402. assert (H/0).evalf(subs={H:1}) == zoo
  403. def test_evalf_integral():
  404. # test that workprec has to increase in order to get a result other than 0
  405. eps = Rational(1, 1000000)
  406. assert Integral(sin(x), (x, -pi, pi + eps)).n(2)._prec == 10
  407. def test_issue_8821_highprec_from_str():
  408. s = str(pi.evalf(128))
  409. p = N(s)
  410. assert Abs(sin(p)) < 1e-15
  411. p = N(s, 64)
  412. assert Abs(sin(p)) < 1e-64
  413. def test_issue_8853():
  414. p = Symbol('x', even=True, positive=True)
  415. assert floor(-p - S.Half).is_even == False
  416. assert floor(-p + S.Half).is_even == True
  417. assert ceiling(p - S.Half).is_even == True
  418. assert ceiling(p + S.Half).is_even == False
  419. assert get_integer_part(S.Half, -1, {}, True) == (0, 0)
  420. assert get_integer_part(S.Half, 1, {}, True) == (1, 0)
  421. assert get_integer_part(Rational(-1, 2), -1, {}, True) == (-1, 0)
  422. assert get_integer_part(Rational(-1, 2), 1, {}, True) == (0, 0)
  423. def test_issue_17681():
  424. class identity_func(Function):
  425. def _eval_evalf(self, *args, **kwargs):
  426. return self.args[0].evalf(*args, **kwargs)
  427. assert floor(identity_func(S(0))) == 0
  428. assert get_integer_part(S(0), 1, {}, True) == (0, 0)
  429. def test_issue_9326():
  430. from sympy.core.symbol import Dummy
  431. d1 = Dummy('d')
  432. d2 = Dummy('d')
  433. e = d1 + d2
  434. assert e.evalf(subs = {d1: 1, d2: 2}) == 3.0
  435. def test_issue_10323():
  436. assert ceiling(sqrt(2**30 + 1)) == 2**15 + 1
  437. def test_AssocOp_Function():
  438. # the first arg of Min is not comparable in the imaginary part
  439. raises(ValueError, lambda: S('''
  440. Min(-sqrt(3)*cos(pi/18)/6 + re(1/((-1/2 - sqrt(3)*I/2)*(1/6 +
  441. sqrt(3)*I/18)**(1/3)))/3 + sin(pi/18)/2 + 2 + I*(-cos(pi/18)/2 -
  442. sqrt(3)*sin(pi/18)/6 + im(1/((-1/2 - sqrt(3)*I/2)*(1/6 +
  443. sqrt(3)*I/18)**(1/3)))/3), re(1/((-1/2 + sqrt(3)*I/2)*(1/6 +
  444. sqrt(3)*I/18)**(1/3)))/3 - sqrt(3)*cos(pi/18)/6 - sin(pi/18)/2 + 2 +
  445. I*(im(1/((-1/2 + sqrt(3)*I/2)*(1/6 + sqrt(3)*I/18)**(1/3)))/3 -
  446. sqrt(3)*sin(pi/18)/6 + cos(pi/18)/2))'''))
  447. # if that is changed so a non-comparable number remains as
  448. # an arg, then the Min/Max instantiation needs to be changed
  449. # to watch out for non-comparable args when making simplifications
  450. # and the following test should be added instead (with e being
  451. # the sympified expression above):
  452. # raises(ValueError, lambda: e._eval_evalf(2))
  453. def test_issue_10395():
  454. eq = x*Max(0, y)
  455. assert nfloat(eq) == eq
  456. eq = x*Max(y, -1.1)
  457. assert nfloat(eq) == eq
  458. assert Max(y, 4).n() == Max(4.0, y)
  459. def test_issue_13098():
  460. assert floor(log(S('9.'+'9'*20), 10)) == 0
  461. assert ceiling(log(S('9.'+'9'*20), 10)) == 1
  462. assert floor(log(20 - S('9.'+'9'*20), 10)) == 1
  463. assert ceiling(log(20 - S('9.'+'9'*20), 10)) == 2
  464. def test_issue_14601():
  465. e = 5*x*y/2 - y*(35*(x**3)/2 - 15*x/2)
  466. subst = {x:0.0, y:0.0}
  467. e2 = e.evalf(subs=subst)
  468. assert float(e2) == 0.0
  469. assert float((x + x*(x**2 + x)).evalf(subs={x: 0.0})) == 0.0
  470. def test_issue_11151():
  471. z = S.Zero
  472. e = Sum(z, (x, 1, 2))
  473. assert e != z # it shouldn't evaluate
  474. # when it does evaluate, this is what it should give
  475. assert evalf(e, 15, {}) == \
  476. evalf(z, 15, {}) == (None, None, 15, None)
  477. # so this shouldn't fail
  478. assert (e/2).n() == 0
  479. # this was where the issue appeared
  480. expr0 = Sum(x**2 + x, (x, 1, 2))
  481. expr1 = Sum(0, (x, 1, 2))
  482. expr2 = expr1/expr0
  483. assert simplify(factor(expr2) - expr2) == 0
  484. def test_issue_13425():
  485. assert N('2**.5', 30) == N('sqrt(2)', 30)
  486. assert N('x - x', 30) == 0
  487. assert abs((N('pi*.1', 22)*10 - pi).n()) < 1e-22
  488. def test_issue_17421():
  489. assert N(acos(-I + acosh(cosh(cosh(1) + I)))) == 1.0*I
  490. def test_issue_20291():
  491. from sympy.sets import EmptySet, Reals
  492. from sympy.sets.sets import (Complement, FiniteSet, Intersection)
  493. a = Symbol('a')
  494. b = Symbol('b')
  495. A = FiniteSet(a, b)
  496. assert A.evalf(subs={a: 1, b: 2}) == FiniteSet(1.0, 2.0)
  497. B = FiniteSet(a-b, 1)
  498. assert B.evalf(subs={a: 1, b: 2}) == FiniteSet(-1.0, 1.0)
  499. sol = Complement(Intersection(FiniteSet(-b/2 - sqrt(b**2-4*pi)/2), Reals), FiniteSet(0))
  500. assert sol.evalf(subs={b: 1}) == EmptySet
  501. def test_evalf_with_zoo():
  502. assert (1/x).evalf(subs={x: 0}) == zoo # issue 8242
  503. assert (-1/x).evalf(subs={x: 0}) == zoo # PR 16150
  504. assert (0 ** x).evalf(subs={x: -1}) == zoo # PR 16150
  505. assert (0 ** x).evalf(subs={x: -1 + I}) == nan
  506. assert Mul(2, Pow(0, -1, evaluate=False), evaluate=False).evalf() == zoo # issue 21147
  507. assert Mul(x, 1/x, evaluate=False).evalf(subs={x: 0}) == Mul(x, 1/x, evaluate=False).subs(x, 0) == nan
  508. assert Mul(1/x, 1/x, evaluate=False).evalf(subs={x: 0}) == zoo
  509. assert Mul(1/x, Abs(1/x), evaluate=False).evalf(subs={x: 0}) == zoo
  510. assert Abs(zoo, evaluate=False).evalf() == oo
  511. assert re(zoo, evaluate=False).evalf() == nan
  512. assert im(zoo, evaluate=False).evalf() == nan
  513. assert Add(zoo, zoo, evaluate=False).evalf() == nan
  514. assert Add(oo, zoo, evaluate=False).evalf() == nan
  515. assert Pow(zoo, -1, evaluate=False).evalf() == 0
  516. assert Pow(zoo, Rational(-1, 3), evaluate=False).evalf() == 0
  517. assert Pow(zoo, Rational(1, 3), evaluate=False).evalf() == zoo
  518. assert Pow(zoo, S.Half, evaluate=False).evalf() == zoo
  519. assert Pow(zoo, 2, evaluate=False).evalf() == zoo
  520. assert Pow(0, zoo, evaluate=False).evalf() == nan
  521. assert log(zoo, evaluate=False).evalf() == zoo
  522. assert zoo.evalf(chop=True) == zoo
  523. assert x.evalf(subs={x: zoo}) == zoo
  524. def test_evalf_with_bounded_error():
  525. cases = [
  526. # zero
  527. (Rational(0), None, 1),
  528. # zero im part
  529. (pi, None, 10),
  530. # zero real part
  531. (pi*I, None, 10),
  532. # re and im nonzero
  533. (2-3*I, None, 5),
  534. # similar tests again, but using eps instead of m
  535. (Rational(0), Rational(1, 2), None),
  536. (pi, Rational(1, 1000), None),
  537. (pi * I, Rational(1, 1000), None),
  538. (2 - 3 * I, Rational(1, 1000), None),
  539. # very large eps
  540. (2 - 3 * I, Rational(1000), None),
  541. # case where x already small, hence some cancellation in p = m + n - 1
  542. (Rational(1234, 10**8), Rational(1, 10**12), None),
  543. ]
  544. for x0, eps, m in cases:
  545. a, b, _, _ = evalf(x0, 53, {})
  546. c, d, _, _ = _evalf_with_bounded_error(x0, eps, m)
  547. if eps is None:
  548. eps = 2**(-m)
  549. z = make_mpc((a or fzero, b or fzero))
  550. w = make_mpc((c or fzero, d or fzero))
  551. assert abs(w - z) < eps
  552. # eps must be positive
  553. raises(ValueError, lambda: _evalf_with_bounded_error(pi, Rational(0)))
  554. raises(ValueError, lambda: _evalf_with_bounded_error(pi, -pi))
  555. raises(ValueError, lambda: _evalf_with_bounded_error(pi, I))
  556. def test_issue_22849():
  557. a = -8 + 3 * sqrt(3)
  558. x = AlgebraicNumber(a)
  559. assert evalf(a, 1, {}) == evalf(x, 1, {})
  560. def test_evalf_real_alg_num():
  561. # This test demonstrates why the entry for `AlgebraicNumber` in
  562. # `sympy.core.evalf._create_evalf_table()` has to use `x.to_root()`,
  563. # instead of `x.as_expr()`. If the latter is used, then `z` will be
  564. # a complex number with `0.e-20` for imaginary part, even though `a5`
  565. # is a real number.
  566. zeta = Symbol('zeta')
  567. a5 = AlgebraicNumber(CRootOf(cyclotomic_poly(5), -1), [-1, -1, 0, 0], alias=zeta)
  568. z = a5.evalf()
  569. assert isinstance(z, Float)
  570. assert not hasattr(z, '_mpc_')
  571. assert hasattr(z, '_mpf_')
  572. def test_issue_20733():
  573. expr = 1/((x - 9)*(x - 8)*(x - 7)*(x - 4)**2*(x - 3)**3*(x - 2))
  574. assert str(expr.evalf(1, subs={x:1})) == '-4.e-5'
  575. assert str(expr.evalf(2, subs={x:1})) == '-4.1e-5'
  576. assert str(expr.evalf(11, subs={x:1})) == '-4.1335978836e-5'
  577. assert str(expr.evalf(20, subs={x:1})) == '-0.000041335978835978835979'
  578. expr = Mul(*((x - i) for i in range(2, 1000)))
  579. assert srepr(expr.evalf(2, subs={x: 1})) == "Float('4.0271e+2561', precision=10)"
  580. assert srepr(expr.evalf(10, subs={x: 1})) == "Float('4.02790050126e+2561', precision=37)"
  581. assert srepr(expr.evalf(53, subs={x: 1})) == "Float('4.0279005012722099453824067459760158730668154575647110393e+2561', precision=179)"