test_limits.py 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311
  1. from itertools import product
  2. from sympy.concrete.summations import Sum
  3. from sympy.core.function import (Function, diff)
  4. from sympy.core import EulerGamma
  5. from sympy.core.numbers import (E, I, Rational, oo, pi, zoo)
  6. from sympy.core.singleton import S
  7. from sympy.core.symbol import (Symbol, symbols)
  8. from sympy.functions.combinatorial.factorials import (binomial, factorial, subfactorial)
  9. from sympy.functions.elementary.complexes import (Abs, re, sign)
  10. from sympy.functions.elementary.exponential import (LambertW, exp, log)
  11. from sympy.functions.elementary.hyperbolic import (acosh, acoth, acsch, asech, atanh, sinh, tanh)
  12. from sympy.functions.elementary.integers import (ceiling, floor, frac)
  13. from sympy.functions.elementary.miscellaneous import (cbrt, real_root, sqrt)
  14. from sympy.functions.elementary.piecewise import Piecewise
  15. from sympy.functions.elementary.trigonometric import (acos, acot, acsc, asec, asin,
  16. atan, cos, cot, csc, sec, sin, tan)
  17. from sympy.functions.special.bessel import (besseli, bessely, besselj, besselk)
  18. from sympy.functions.special.error_functions import (Ei, erf, erfc, erfi, fresnelc, fresnels)
  19. from sympy.functions.special.gamma_functions import (digamma, gamma, uppergamma)
  20. from sympy.functions.special.hyper import meijerg
  21. from sympy.integrals.integrals import (Integral, integrate)
  22. from sympy.series.limits import (Limit, limit)
  23. from sympy.simplify.simplify import (logcombine, simplify)
  24. from sympy.simplify.hyperexpand import hyperexpand
  25. from sympy.calculus.accumulationbounds import AccumBounds
  26. from sympy.core.mul import Mul
  27. from sympy.series.limits import heuristics
  28. from sympy.series.order import Order
  29. from sympy.testing.pytest import XFAIL, raises
  30. from sympy.abc import x, y, z, k
  31. n = Symbol('n', integer=True, positive=True)
  32. def test_basic1():
  33. assert limit(x, x, oo) is oo
  34. assert limit(x, x, -oo) is -oo
  35. assert limit(-x, x, oo) is -oo
  36. assert limit(x**2, x, -oo) is oo
  37. assert limit(-x**2, x, oo) is -oo
  38. assert limit(x*log(x), x, 0, dir="+") == 0
  39. assert limit(1/x, x, oo) == 0
  40. assert limit(exp(x), x, oo) is oo
  41. assert limit(-exp(x), x, oo) is -oo
  42. assert limit(exp(x)/x, x, oo) is oo
  43. assert limit(1/x - exp(-x), x, oo) == 0
  44. assert limit(x + 1/x, x, oo) is oo
  45. assert limit(x - x**2, x, oo) is -oo
  46. assert limit((1 + x)**(1 + sqrt(2)), x, 0) == 1
  47. assert limit((1 + x)**oo, x, 0) == Limit((x + 1)**oo, x, 0)
  48. assert limit((1 + x)**oo, x, 0, dir='-') == Limit((x + 1)**oo, x, 0, dir='-')
  49. assert limit((1 + x + y)**oo, x, 0, dir='-') == Limit((1 + x + y)**oo, x, 0, dir='-')
  50. assert limit(y/x/log(x), x, 0) == -oo*sign(y)
  51. assert limit(cos(x + y)/x, x, 0) == sign(cos(y))*oo
  52. assert limit(gamma(1/x + 3), x, oo) == 2
  53. assert limit(S.NaN, x, -oo) is S.NaN
  54. assert limit(Order(2)*x, x, S.NaN) is S.NaN
  55. assert limit(1/(x - 1), x, 1, dir="+") is oo
  56. assert limit(1/(x - 1), x, 1, dir="-") is -oo
  57. assert limit(1/(5 - x)**3, x, 5, dir="+") is -oo
  58. assert limit(1/(5 - x)**3, x, 5, dir="-") is oo
  59. assert limit(1/sin(x), x, pi, dir="+") is -oo
  60. assert limit(1/sin(x), x, pi, dir="-") is oo
  61. assert limit(1/cos(x), x, pi/2, dir="+") is -oo
  62. assert limit(1/cos(x), x, pi/2, dir="-") is oo
  63. assert limit(1/tan(x**3), x, (2*pi)**Rational(1, 3), dir="+") is oo
  64. assert limit(1/tan(x**3), x, (2*pi)**Rational(1, 3), dir="-") is -oo
  65. assert limit(1/cot(x)**3, x, (pi*Rational(3, 2)), dir="+") is -oo
  66. assert limit(1/cot(x)**3, x, (pi*Rational(3, 2)), dir="-") is oo
  67. assert limit(tan(x), x, oo) == AccumBounds(S.NegativeInfinity, S.Infinity)
  68. assert limit(cot(x), x, oo) == AccumBounds(S.NegativeInfinity, S.Infinity)
  69. assert limit(sec(x), x, oo) == AccumBounds(S.NegativeInfinity, S.Infinity)
  70. assert limit(csc(x), x, oo) == AccumBounds(S.NegativeInfinity, S.Infinity)
  71. # test bi-directional limits
  72. assert limit(sin(x)/x, x, 0, dir="+-") == 1
  73. assert limit(x**2, x, 0, dir="+-") == 0
  74. assert limit(1/x**2, x, 0, dir="+-") is oo
  75. # test failing bi-directional limits
  76. assert limit(1/x, x, 0, dir="+-") is zoo
  77. # approaching 0
  78. # from dir="+"
  79. assert limit(1 + 1/x, x, 0) is oo
  80. # from dir='-'
  81. # Add
  82. assert limit(1 + 1/x, x, 0, dir='-') is -oo
  83. # Pow
  84. assert limit(x**(-2), x, 0, dir='-') is oo
  85. assert limit(x**(-3), x, 0, dir='-') is -oo
  86. assert limit(1/sqrt(x), x, 0, dir='-') == (-oo)*I
  87. assert limit(x**2, x, 0, dir='-') == 0
  88. assert limit(sqrt(x), x, 0, dir='-') == 0
  89. assert limit(x**-pi, x, 0, dir='-') == -oo*(-1)**(1 - pi)
  90. assert limit((1 + cos(x))**oo, x, 0) == Limit((cos(x) + 1)**oo, x, 0)
  91. # test pull request 22491
  92. assert limit(1/asin(x), x, 0, dir = '+') == oo
  93. assert limit(1/asin(x), x, 0, dir = '-') == -oo
  94. assert limit(1/sinh(x), x, 0, dir = '+') == oo
  95. assert limit(1/sinh(x), x, 0, dir = '-') == -oo
  96. assert limit(log(1/x) + 1/sin(x), x, 0, dir = '+') == oo
  97. assert limit(log(1/x) + 1/x, x, 0, dir = '+') == oo
  98. def test_basic2():
  99. assert limit(x**x, x, 0, dir="+") == 1
  100. assert limit((exp(x) - 1)/x, x, 0) == 1
  101. assert limit(1 + 1/x, x, oo) == 1
  102. assert limit(-exp(1/x), x, oo) == -1
  103. assert limit(x + exp(-x), x, oo) is oo
  104. assert limit(x + exp(-x**2), x, oo) is oo
  105. assert limit(x + exp(-exp(x)), x, oo) is oo
  106. assert limit(13 + 1/x - exp(-x), x, oo) == 13
  107. def test_basic3():
  108. assert limit(1/x, x, 0, dir="+") is oo
  109. assert limit(1/x, x, 0, dir="-") is -oo
  110. def test_basic4():
  111. assert limit(2*x + y*x, x, 0) == 0
  112. assert limit(2*x + y*x, x, 1) == 2 + y
  113. assert limit(2*x**8 + y*x**(-3), x, -2) == 512 - y/8
  114. assert limit(sqrt(x + 1) - sqrt(x), x, oo) == 0
  115. assert integrate(1/(x**3 + 1), (x, 0, oo)) == 2*pi*sqrt(3)/9
  116. def test_log():
  117. # https://github.com/sympy/sympy/issues/21598
  118. a, b, c = symbols('a b c', positive=True)
  119. A = log(a/b) - (log(a) - log(b))
  120. assert A.limit(a, oo) == 0
  121. assert (A * c).limit(a, oo) == 0
  122. tau, x = symbols('tau x', positive=True)
  123. # The value of manualintegrate in the issue
  124. expr = tau**2*((tau - 1)*(tau + 1)*log(x + 1)/(tau**2 + 1)**2 + 1/((tau**2\
  125. + 1)*(x + 1)) - (-2*tau*atan(x/tau) + (tau**2/2 - 1/2)*log(tau**2\
  126. + x**2))/(tau**2 + 1)**2)
  127. assert limit(expr, x, oo) == pi*tau**3/(tau**2 + 1)**2
  128. def test_piecewise():
  129. # https://github.com/sympy/sympy/issues/18363
  130. assert limit((real_root(x - 6, 3) + 2)/(x + 2), x, -2, '+') == Rational(1, 12)
  131. def test_piecewise2():
  132. func1 = 2*sqrt(x)*Piecewise(((4*x - 2)/Abs(sqrt(4 - 4*(2*x - 1)**2)), 4*x - 2\
  133. >= 0), ((2 - 4*x)/Abs(sqrt(4 - 4*(2*x - 1)**2)), True))
  134. func2 = Piecewise((x**2/2, x <= 0.5), (x/2 - 0.125, True))
  135. func3 = Piecewise(((x - 9) / 5, x < -1), ((x - 9) / 5, x > 4), (sqrt(Abs(x - 3)), True))
  136. assert limit(func1, x, 0) == 1
  137. assert limit(func2, x, 0) == 0
  138. assert limit(func3, x, -1) == 2
  139. def test_basic5():
  140. class my(Function):
  141. @classmethod
  142. def eval(cls, arg):
  143. if arg is S.Infinity:
  144. return S.NaN
  145. assert limit(my(x), x, oo) == Limit(my(x), x, oo)
  146. def test_issue_3885():
  147. assert limit(x*y + x*z, z, 2) == x*y + 2*x
  148. def test_Limit():
  149. assert Limit(sin(x)/x, x, 0) != 1
  150. assert Limit(sin(x)/x, x, 0).doit() == 1
  151. assert Limit(x, x, 0, dir='+-').args == (x, x, 0, Symbol('+-'))
  152. def test_floor():
  153. assert limit(floor(x), x, -2, "+") == -2
  154. assert limit(floor(x), x, -2, "-") == -3
  155. assert limit(floor(x), x, -1, "+") == -1
  156. assert limit(floor(x), x, -1, "-") == -2
  157. assert limit(floor(x), x, 0, "+") == 0
  158. assert limit(floor(x), x, 0, "-") == -1
  159. assert limit(floor(x), x, 1, "+") == 1
  160. assert limit(floor(x), x, 1, "-") == 0
  161. assert limit(floor(x), x, 2, "+") == 2
  162. assert limit(floor(x), x, 2, "-") == 1
  163. assert limit(floor(x), x, 248, "+") == 248
  164. assert limit(floor(x), x, 248, "-") == 247
  165. # https://github.com/sympy/sympy/issues/14478
  166. assert limit(x*floor(3/x)/2, x, 0, '+') == Rational(3, 2)
  167. assert limit(floor(x + 1/2) - floor(x), x, oo) == AccumBounds(-S.Half, S(3)/2)
  168. # test issue 9158
  169. assert limit(floor(atan(x)), x, oo) == 1
  170. assert limit(floor(atan(x)), x, -oo) == -2
  171. assert limit(ceiling(atan(x)), x, oo) == 2
  172. assert limit(ceiling(atan(x)), x, -oo) == -1
  173. def test_floor_requires_robust_assumptions():
  174. assert limit(floor(sin(x)), x, 0, "+") == 0
  175. assert limit(floor(sin(x)), x, 0, "-") == -1
  176. assert limit(floor(cos(x)), x, 0, "+") == 0
  177. assert limit(floor(cos(x)), x, 0, "-") == 0
  178. assert limit(floor(5 + sin(x)), x, 0, "+") == 5
  179. assert limit(floor(5 + sin(x)), x, 0, "-") == 4
  180. assert limit(floor(5 + cos(x)), x, 0, "+") == 5
  181. assert limit(floor(5 + cos(x)), x, 0, "-") == 5
  182. def test_ceiling():
  183. assert limit(ceiling(x), x, -2, "+") == -1
  184. assert limit(ceiling(x), x, -2, "-") == -2
  185. assert limit(ceiling(x), x, -1, "+") == 0
  186. assert limit(ceiling(x), x, -1, "-") == -1
  187. assert limit(ceiling(x), x, 0, "+") == 1
  188. assert limit(ceiling(x), x, 0, "-") == 0
  189. assert limit(ceiling(x), x, 1, "+") == 2
  190. assert limit(ceiling(x), x, 1, "-") == 1
  191. assert limit(ceiling(x), x, 2, "+") == 3
  192. assert limit(ceiling(x), x, 2, "-") == 2
  193. assert limit(ceiling(x), x, 248, "+") == 249
  194. assert limit(ceiling(x), x, 248, "-") == 248
  195. # https://github.com/sympy/sympy/issues/14478
  196. assert limit(x*ceiling(3/x)/2, x, 0, '+') == Rational(3, 2)
  197. assert limit(ceiling(x + 1/2) - ceiling(x), x, oo) == AccumBounds(-S.Half, S(3)/2)
  198. def test_ceiling_requires_robust_assumptions():
  199. assert limit(ceiling(sin(x)), x, 0, "+") == 1
  200. assert limit(ceiling(sin(x)), x, 0, "-") == 0
  201. assert limit(ceiling(cos(x)), x, 0, "+") == 1
  202. assert limit(ceiling(cos(x)), x, 0, "-") == 1
  203. assert limit(ceiling(5 + sin(x)), x, 0, "+") == 6
  204. assert limit(ceiling(5 + sin(x)), x, 0, "-") == 5
  205. assert limit(ceiling(5 + cos(x)), x, 0, "+") == 6
  206. assert limit(ceiling(5 + cos(x)), x, 0, "-") == 6
  207. def test_frac():
  208. assert limit(frac(x), x, oo) == AccumBounds(0, 1)
  209. assert limit(frac(x)**(1/x), x, oo) == AccumBounds(0, 1)
  210. assert limit(frac(x)**(1/x), x, -oo) == AccumBounds(1, oo)
  211. assert limit(frac(x)**x, x, oo) == AccumBounds(0, oo) # wolfram gives (0, 1)
  212. assert limit(frac(sin(x)), x, 0, "+") == 0
  213. assert limit(frac(sin(x)), x, 0, "-") == 1
  214. assert limit(frac(cos(x)), x, 0, "+-") == 1
  215. assert limit(frac(x**2), x, 0, "+-") == 0
  216. raises(ValueError, lambda: limit(frac(x), x, 0, '+-'))
  217. assert limit(frac(-2*x + 1), x, 0, "+") == 1
  218. assert limit(frac(-2*x + 1), x, 0, "-") == 0
  219. assert limit(frac(x + S.Half), x, 0, "+-") == 1/2
  220. assert limit(frac(1/x), x, 0) == AccumBounds(0, 1)
  221. def test_issue_14355():
  222. assert limit(floor(sin(x)/x), x, 0, '+') == 0
  223. assert limit(floor(sin(x)/x), x, 0, '-') == 0
  224. # test comment https://github.com/sympy/sympy/issues/14355#issuecomment-372121314
  225. assert limit(floor(-tan(x)/x), x, 0, '+') == -2
  226. assert limit(floor(-tan(x)/x), x, 0, '-') == -2
  227. def test_atan():
  228. x = Symbol("x", real=True)
  229. assert limit(atan(x)*sin(1/x), x, 0) == 0
  230. assert limit(atan(x) + sqrt(x + 1) - sqrt(x), x, oo) == pi/2
  231. def test_set_signs():
  232. assert limit(abs(x), x, 0) == 0
  233. assert limit(abs(sin(x)), x, 0) == 0
  234. assert limit(abs(cos(x)), x, 0) == 1
  235. assert limit(abs(sin(x + 1)), x, 0) == sin(1)
  236. # https://github.com/sympy/sympy/issues/9449
  237. assert limit((Abs(x + y) - Abs(x - y))/(2*x), x, 0) == sign(y)
  238. # https://github.com/sympy/sympy/issues/12398
  239. assert limit(Abs(log(x)/x**3), x, oo) == 0
  240. assert limit(x*(Abs(log(x)/x**3)/Abs(log(x + 1)/(x + 1)**3) - 1), x, oo) == 3
  241. # https://github.com/sympy/sympy/issues/18501
  242. assert limit(Abs(log(x - 1)**3 - 1), x, 1, '+') == oo
  243. # https://github.com/sympy/sympy/issues/18997
  244. assert limit(Abs(log(x)), x, 0) == oo
  245. assert limit(Abs(log(Abs(x))), x, 0) == oo
  246. # https://github.com/sympy/sympy/issues/19026
  247. z = Symbol('z', positive=True)
  248. assert limit(Abs(log(z) + 1)/log(z), z, oo) == 1
  249. # https://github.com/sympy/sympy/issues/20704
  250. assert limit(z*(Abs(1/z + y) - Abs(y - 1/z))/2, z, 0) == 0
  251. # https://github.com/sympy/sympy/issues/21606
  252. assert limit(cos(z)/sign(z), z, pi, '-') == -1
  253. def test_heuristic():
  254. x = Symbol("x", real=True)
  255. assert heuristics(sin(1/x) + atan(x), x, 0, '+') == AccumBounds(-1, 1)
  256. assert limit(log(2 + sqrt(atan(x))*sqrt(sin(1/x))), x, 0) == log(2)
  257. def test_issue_3871():
  258. z = Symbol("z", positive=True)
  259. f = -1/z*exp(-z*x)
  260. assert limit(f, x, oo) == 0
  261. assert f.limit(x, oo) == 0
  262. def test_exponential():
  263. n = Symbol('n')
  264. x = Symbol('x', real=True)
  265. assert limit((1 + x/n)**n, n, oo) == exp(x)
  266. assert limit((1 + x/(2*n))**n, n, oo) == exp(x/2)
  267. assert limit((1 + x/(2*n + 1))**n, n, oo) == exp(x/2)
  268. assert limit(((x - 1)/(x + 1))**x, x, oo) == exp(-2)
  269. assert limit(1 + (1 + 1/x)**x, x, oo) == 1 + S.Exp1
  270. assert limit((2 + 6*x)**x/(6*x)**x, x, oo) == exp(S('1/3'))
  271. def test_exponential2():
  272. n = Symbol('n')
  273. assert limit((1 + x/(n + sin(n)))**n, n, oo) == exp(x)
  274. def test_doit():
  275. f = Integral(2 * x, x)
  276. l = Limit(f, x, oo)
  277. assert l.doit() is oo
  278. def test_series_AccumBounds():
  279. assert limit(sin(k) - sin(k + 1), k, oo) == AccumBounds(-2, 2)
  280. assert limit(cos(k) - cos(k + 1) + 1, k, oo) == AccumBounds(-1, 3)
  281. # not the exact bound
  282. assert limit(sin(k) - sin(k)*cos(k), k, oo) == AccumBounds(-2, 2)
  283. # test for issue #9934
  284. lo = (-3 + cos(1))/2
  285. hi = (1 + cos(1))/2
  286. t1 = Mul(AccumBounds(lo, hi), 1/(-1 + cos(1)), evaluate=False)
  287. assert limit(simplify(Sum(cos(n).rewrite(exp), (n, 0, k)).doit().rewrite(sin)), k, oo) == t1
  288. t2 = Mul(AccumBounds(-1 + sin(1)/2, sin(1)/2 + 1), 1/(1 - cos(1)))
  289. assert limit(simplify(Sum(sin(n).rewrite(exp), (n, 0, k)).doit().rewrite(sin)), k, oo) == t2
  290. assert limit(((sin(x) + 1)/2)**x, x, oo) == AccumBounds(0, oo) # wolfram says 0
  291. # https://github.com/sympy/sympy/issues/12312
  292. e = 2**(-x)*(sin(x) + 1)**x
  293. assert limit(e, x, oo) == AccumBounds(0, oo)
  294. def test_bessel_functions_at_infinity():
  295. # Pull Request 23844 implements limits for all bessel and modified bessel
  296. # functions approaching infinity along any direction i.e. abs(z0) tends to oo
  297. assert limit(besselj(1, x), x, oo) == 0
  298. assert limit(besselj(1, x), x, -oo) == 0
  299. assert limit(besselj(1, x), x, I*oo) == oo*I
  300. assert limit(besselj(1, x), x, -I*oo) == -oo*I
  301. assert limit(bessely(1, x), x, oo) == 0
  302. assert limit(bessely(1, x), x, -oo) == 0
  303. assert limit(bessely(1, x), x, I*oo) == -oo
  304. assert limit(bessely(1, x), x, -I*oo) == -oo
  305. assert limit(besseli(1, x), x, oo) == oo
  306. assert limit(besseli(1, x), x, -oo) == -oo
  307. assert limit(besseli(1, x), x, I*oo) == 0
  308. assert limit(besseli(1, x), x, -I*oo) == 0
  309. assert limit(besselk(1, x), x, oo) == 0
  310. assert limit(besselk(1, x), x, -oo) == -oo*I
  311. assert limit(besselk(1, x), x, I*oo) == 0
  312. assert limit(besselk(1, x), x, -I*oo) == 0
  313. # test issue 14874
  314. assert limit(besselk(0, x), x, oo) == 0
  315. @XFAIL
  316. def test_doit2():
  317. f = Integral(2 * x, x)
  318. l = Limit(f, x, oo)
  319. # limit() breaks on the contained Integral.
  320. assert l.doit(deep=False) == l
  321. def test_issue_2929():
  322. assert limit((x * exp(x))/(exp(x) - 1), x, -oo) == 0
  323. def test_issue_3792():
  324. assert limit((1 - cos(x))/x**2, x, S.Half) == 4 - 4*cos(S.Half)
  325. assert limit(sin(sin(x + 1) + 1), x, 0) == sin(1 + sin(1))
  326. assert limit(abs(sin(x + 1) + 1), x, 0) == 1 + sin(1)
  327. def test_issue_4090():
  328. assert limit(1/(x + 3), x, 2) == Rational(1, 5)
  329. assert limit(1/(x + pi), x, 2) == S.One/(2 + pi)
  330. assert limit(log(x)/(x**2 + 3), x, 2) == log(2)/7
  331. assert limit(log(x)/(x**2 + pi), x, 2) == log(2)/(4 + pi)
  332. def test_issue_4547():
  333. assert limit(cot(x), x, 0, dir='+') is oo
  334. assert limit(cot(x), x, pi/2, dir='+') == 0
  335. def test_issue_5164():
  336. assert limit(x**0.5, x, oo) == oo**0.5 is oo
  337. assert limit(x**0.5, x, 16) == S(16)**0.5
  338. assert limit(x**0.5, x, 0) == 0
  339. assert limit(x**(-0.5), x, oo) == 0
  340. assert limit(x**(-0.5), x, 4) == S(4)**(-0.5)
  341. def test_issue_5383():
  342. func = (1.0 * 1 + 1.0 * x)**(1.0 * 1 / x)
  343. assert limit(func, x, 0) == E
  344. def test_issue_14793():
  345. expr = ((x + S(1)/2) * log(x) - x + log(2*pi)/2 - \
  346. log(factorial(x)) + S(1)/(12*x))*x**3
  347. assert limit(expr, x, oo) == S(1)/360
  348. def test_issue_5183():
  349. # using list(...) so py.test can recalculate values
  350. tests = list(product([x, -x],
  351. [-1, 1],
  352. [2, 3, S.Half, Rational(2, 3)],
  353. ['-', '+']))
  354. results = (oo, oo, -oo, oo, -oo*I, oo, -oo*(-1)**Rational(1, 3), oo,
  355. 0, 0, 0, 0, 0, 0, 0, 0,
  356. oo, oo, oo, -oo, oo, -oo*I, oo, -oo*(-1)**Rational(1, 3),
  357. 0, 0, 0, 0, 0, 0, 0, 0)
  358. assert len(tests) == len(results)
  359. for i, (args, res) in enumerate(zip(tests, results)):
  360. y, s, e, d = args
  361. eq = y**(s*e)
  362. try:
  363. assert limit(eq, x, 0, dir=d) == res
  364. except AssertionError:
  365. if 0: # change to 1 if you want to see the failing tests
  366. print()
  367. print(i, res, eq, d, limit(eq, x, 0, dir=d))
  368. else:
  369. assert None
  370. def test_issue_5184():
  371. assert limit(sin(x)/x, x, oo) == 0
  372. assert limit(atan(x), x, oo) == pi/2
  373. assert limit(gamma(x), x, oo) is oo
  374. assert limit(cos(x)/x, x, oo) == 0
  375. assert limit(gamma(x), x, S.Half) == sqrt(pi)
  376. r = Symbol('r', real=True)
  377. assert limit(r*sin(1/r), r, 0) == 0
  378. def test_issue_5229():
  379. assert limit((1 + y)**(1/y) - S.Exp1, y, 0) == 0
  380. def test_issue_4546():
  381. # using list(...) so py.test can recalculate values
  382. tests = list(product([cot, tan],
  383. [-pi/2, 0, pi/2, pi, pi*Rational(3, 2)],
  384. ['-', '+']))
  385. results = (0, 0, -oo, oo, 0, 0, -oo, oo, 0, 0,
  386. oo, -oo, 0, 0, oo, -oo, 0, 0, oo, -oo)
  387. assert len(tests) == len(results)
  388. for i, (args, res) in enumerate(zip(tests, results)):
  389. f, l, d = args
  390. eq = f(x)
  391. try:
  392. assert limit(eq, x, l, dir=d) == res
  393. except AssertionError:
  394. if 0: # change to 1 if you want to see the failing tests
  395. print()
  396. print(i, res, eq, l, d, limit(eq, x, l, dir=d))
  397. else:
  398. assert None
  399. def test_issue_3934():
  400. assert limit((1 + x**log(3))**(1/x), x, 0) == 1
  401. assert limit((5**(1/x) + 3**(1/x))**x, x, 0) == 5
  402. def test_calculate_series():
  403. # NOTE
  404. # The calculate_series method is being deprecated and is no longer responsible
  405. # for result being returned. The mrv_leadterm function now uses simple leadterm
  406. # calls rather than calculate_series.
  407. # needs gruntz calculate_series to go to n = 32
  408. assert limit(x**Rational(77, 3)/(1 + x**Rational(77, 3)), x, oo) == 1
  409. # needs gruntz calculate_series to go to n = 128
  410. assert limit(x**101.1/(1 + x**101.1), x, oo) == 1
  411. def test_issue_5955():
  412. assert limit((x**16)/(1 + x**16), x, oo) == 1
  413. assert limit((x**100)/(1 + x**100), x, oo) == 1
  414. assert limit((x**1885)/(1 + x**1885), x, oo) == 1
  415. assert limit((x**1000/((x + 1)**1000 + exp(-x))), x, oo) == 1
  416. def test_newissue():
  417. assert limit(exp(1/sin(x))/exp(cot(x)), x, 0) == 1
  418. def test_extended_real_line():
  419. assert limit(x - oo, x, oo) == Limit(x - oo, x, oo)
  420. assert limit(1/(x + sin(x)) - oo, x, 0) == Limit(1/(x + sin(x)) - oo, x, 0)
  421. assert limit(oo/x, x, oo) == Limit(oo/x, x, oo)
  422. assert limit(x - oo + 1/x, x, oo) == Limit(x - oo + 1/x, x, oo)
  423. @XFAIL
  424. def test_order_oo():
  425. x = Symbol('x', positive=True)
  426. assert Order(x)*oo != Order(1, x)
  427. assert limit(oo/(x**2 - 4), x, oo) is oo
  428. def test_issue_5436():
  429. raises(NotImplementedError, lambda: limit(exp(x*y), x, oo))
  430. raises(NotImplementedError, lambda: limit(exp(-x*y), x, oo))
  431. def test_Limit_dir():
  432. raises(TypeError, lambda: Limit(x, x, 0, dir=0))
  433. raises(ValueError, lambda: Limit(x, x, 0, dir='0'))
  434. def test_polynomial():
  435. assert limit((x + 1)**1000/((x + 1)**1000 + 1), x, oo) == 1
  436. assert limit((x + 1)**1000/((x + 1)**1000 + 1), x, -oo) == 1
  437. def test_rational():
  438. assert limit(1/y - (1/(y + x) + x/(y + x)/y)/z, x, oo) == (z - 1)/(y*z)
  439. assert limit(1/y - (1/(y + x) + x/(y + x)/y)/z, x, -oo) == (z - 1)/(y*z)
  440. def test_issue_5740():
  441. assert limit(log(x)*z - log(2*x)*y, x, 0) == oo*sign(y - z)
  442. def test_issue_6366():
  443. n = Symbol('n', integer=True, positive=True)
  444. r = (n + 1)*x**(n + 1)/(x**(n + 1) - 1) - x/(x - 1)
  445. assert limit(r, x, 1).cancel() == n/2
  446. def test_factorial():
  447. f = factorial(x)
  448. assert limit(f, x, oo) is oo
  449. assert limit(x/f, x, oo) == 0
  450. # see Stirling's approximation:
  451. # https://en.wikipedia.org/wiki/Stirling's_approximation
  452. assert limit(f/(sqrt(2*pi*x)*(x/E)**x), x, oo) == 1
  453. assert limit(f, x, -oo) == gamma(-oo)
  454. def test_issue_6560():
  455. e = (5*x**3/4 - x*Rational(3, 4) + (y*(3*x**2/2 - S.Half) +
  456. 35*x**4/8 - 15*x**2/4 + Rational(3, 8))/(2*(y + 1)))
  457. assert limit(e, y, oo) == 5*x**3/4 + 3*x**2/4 - 3*x/4 - Rational(1, 4)
  458. @XFAIL
  459. def test_issue_5172():
  460. n = Symbol('n')
  461. r = Symbol('r', positive=True)
  462. c = Symbol('c')
  463. p = Symbol('p', positive=True)
  464. m = Symbol('m', negative=True)
  465. expr = ((2*n*(n - r + 1)/(n + r*(n - r + 1)))**c +
  466. (r - 1)*(n*(n - r + 2)/(n + r*(n - r + 1)))**c - n)/(n**c - n)
  467. expr = expr.subs(c, c + 1)
  468. raises(NotImplementedError, lambda: limit(expr, n, oo))
  469. assert limit(expr.subs(c, m), n, oo) == 1
  470. assert limit(expr.subs(c, p), n, oo).simplify() == \
  471. (2**(p + 1) + r - 1)/(r + 1)**(p + 1)
  472. def test_issue_7088():
  473. a = Symbol('a')
  474. assert limit(sqrt(x/(x + a)), x, oo) == 1
  475. def test_branch_cuts():
  476. assert limit(asin(I*x + 2), x, 0) == pi - asin(2)
  477. assert limit(asin(I*x + 2), x, 0, '-') == asin(2)
  478. assert limit(asin(I*x - 2), x, 0) == -asin(2)
  479. assert limit(asin(I*x - 2), x, 0, '-') == -pi + asin(2)
  480. assert limit(acos(I*x + 2), x, 0) == -acos(2)
  481. assert limit(acos(I*x + 2), x, 0, '-') == acos(2)
  482. assert limit(acos(I*x - 2), x, 0) == acos(-2)
  483. assert limit(acos(I*x - 2), x, 0, '-') == 2*pi - acos(-2)
  484. assert limit(atan(x + 2*I), x, 0) == I*atanh(2)
  485. assert limit(atan(x + 2*I), x, 0, '-') == -pi + I*atanh(2)
  486. assert limit(atan(x - 2*I), x, 0) == pi - I*atanh(2)
  487. assert limit(atan(x - 2*I), x, 0, '-') == -I*atanh(2)
  488. assert limit(atan(1/x), x, 0) == pi/2
  489. assert limit(atan(1/x), x, 0, '-') == -pi/2
  490. assert limit(atan(x), x, oo) == pi/2
  491. assert limit(atan(x), x, -oo) == -pi/2
  492. assert limit(acot(x + S(1)/2*I), x, 0) == pi - I*acoth(S(1)/2)
  493. assert limit(acot(x + S(1)/2*I), x, 0, '-') == -I*acoth(S(1)/2)
  494. assert limit(acot(x - S(1)/2*I), x, 0) == I*acoth(S(1)/2)
  495. assert limit(acot(x - S(1)/2*I), x, 0, '-') == -pi + I*acoth(S(1)/2)
  496. assert limit(acot(x), x, 0) == pi/2
  497. assert limit(acot(x), x, 0, '-') == -pi/2
  498. assert limit(asec(I*x + S(1)/2), x, 0) == asec(S(1)/2)
  499. assert limit(asec(I*x + S(1)/2), x, 0, '-') == -asec(S(1)/2)
  500. assert limit(asec(I*x - S(1)/2), x, 0) == 2*pi - asec(-S(1)/2)
  501. assert limit(asec(I*x - S(1)/2), x, 0, '-') == asec(-S(1)/2)
  502. assert limit(acsc(I*x + S(1)/2), x, 0) == acsc(S(1)/2)
  503. assert limit(acsc(I*x + S(1)/2), x, 0, '-') == pi - acsc(S(1)/2)
  504. assert limit(acsc(I*x - S(1)/2), x, 0) == -pi + acsc(S(1)/2)
  505. assert limit(acsc(I*x - S(1)/2), x, 0, '-') == -acsc(S(1)/2)
  506. assert limit(log(I*x - 1), x, 0) == I*pi
  507. assert limit(log(I*x - 1), x, 0, '-') == -I*pi
  508. assert limit(log(-I*x - 1), x, 0) == -I*pi
  509. assert limit(log(-I*x - 1), x, 0, '-') == I*pi
  510. assert limit(sqrt(I*x - 1), x, 0) == I
  511. assert limit(sqrt(I*x - 1), x, 0, '-') == -I
  512. assert limit(sqrt(-I*x - 1), x, 0) == -I
  513. assert limit(sqrt(-I*x - 1), x, 0, '-') == I
  514. assert limit(cbrt(I*x - 1), x, 0) == (-1)**(S(1)/3)
  515. assert limit(cbrt(I*x - 1), x, 0, '-') == -(-1)**(S(2)/3)
  516. assert limit(cbrt(-I*x - 1), x, 0) == -(-1)**(S(2)/3)
  517. assert limit(cbrt(-I*x - 1), x, 0, '-') == (-1)**(S(1)/3)
  518. def test_issue_6364():
  519. a = Symbol('a')
  520. e = z/(1 - sqrt(1 + z)*sin(a)**2 - sqrt(1 - z)*cos(a)**2)
  521. assert limit(e, z, 0) == 1/(cos(a)**2 - S.Half)
  522. def test_issue_6682():
  523. assert limit(exp(2*Ei(-x))/x**2, x, 0) == exp(2*EulerGamma)
  524. def test_issue_4099():
  525. a = Symbol('a')
  526. assert limit(a/x, x, 0) == oo*sign(a)
  527. assert limit(-a/x, x, 0) == -oo*sign(a)
  528. assert limit(-a*x, x, oo) == -oo*sign(a)
  529. assert limit(a*x, x, oo) == oo*sign(a)
  530. def test_issue_4503():
  531. dx = Symbol('dx')
  532. assert limit((sqrt(1 + exp(x + dx)) - sqrt(1 + exp(x)))/dx, dx, 0) == \
  533. exp(x)/(2*sqrt(exp(x) + 1))
  534. def test_issue_6052():
  535. G = meijerg((), (), (1,), (0,), -x)
  536. g = hyperexpand(G)
  537. assert limit(g, x, 0, '+-') == 0
  538. assert limit(g, x, oo) == -oo
  539. def test_issue_7224():
  540. expr = sqrt(x)*besseli(1,sqrt(8*x))
  541. assert limit(x*diff(expr, x, x)/expr, x, 0) == 2
  542. assert limit(x*diff(expr, x, x)/expr, x, 1).evalf() == 2.0
  543. def test_issue_8208():
  544. assert limit(n**(Rational(1, 1e9) - 1), n, oo) == 0
  545. def test_issue_8229():
  546. assert limit((x**Rational(1, 4) - 2)/(sqrt(x) - 4)**Rational(2, 3), x, 16) == 0
  547. def test_issue_8433():
  548. d, t = symbols('d t', positive=True)
  549. assert limit(erf(1 - t/d), t, oo) == -1
  550. def test_issue_8481():
  551. k = Symbol('k', integer=True, nonnegative=True)
  552. lamda = Symbol('lamda', positive=True)
  553. assert limit(lamda**k * exp(-lamda) / factorial(k), k, oo) == 0
  554. def test_issue_8462():
  555. assert limit(binomial(n, n/2), n, oo) == oo
  556. assert limit(binomial(n, n/2) * 3 ** (-n), n, oo) == 0
  557. def test_issue_8634():
  558. n = Symbol('n', integer=True, positive=True)
  559. x = Symbol('x')
  560. assert limit(x**n, x, -oo) == oo*sign((-1)**n)
  561. def test_issue_8635_18176():
  562. x = Symbol('x', real=True)
  563. k = Symbol('k', positive=True)
  564. assert limit(x**n - x**(n - 0), x, oo) == 0
  565. assert limit(x**n - x**(n - 5), x, oo) == oo
  566. assert limit(x**n - x**(n - 2.5), x, oo) == oo
  567. assert limit(x**n - x**(n - k - 1), x, oo) == oo
  568. x = Symbol('x', positive=True)
  569. assert limit(x**n - x**(n - 1), x, oo) == oo
  570. assert limit(x**n - x**(n + 2), x, oo) == -oo
  571. def test_issue_8730():
  572. assert limit(subfactorial(x), x, oo) is oo
  573. def test_issue_9252():
  574. n = Symbol('n', integer=True)
  575. c = Symbol('c', positive=True)
  576. assert limit((log(n))**(n/log(n)) / (1 + c)**n, n, oo) == 0
  577. # limit should depend on the value of c
  578. raises(NotImplementedError, lambda: limit((log(n))**(n/log(n)) / c**n, n, oo))
  579. def test_issue_9558():
  580. assert limit(sin(x)**15, x, 0, '-') == 0
  581. def test_issue_10801():
  582. # make sure limits work with binomial
  583. assert limit(16**k / (k * binomial(2*k, k)**2), k, oo) == pi
  584. def test_issue_10976():
  585. s, x = symbols('s x', real=True)
  586. assert limit(erf(s*x)/erf(s), s, 0) == x
  587. def test_issue_9041():
  588. assert limit(factorial(n) / ((n/exp(1))**n * sqrt(2*pi*n)), n, oo) == 1
  589. def test_issue_9205():
  590. x, y, a = symbols('x, y, a')
  591. assert Limit(x, x, a).free_symbols == {a}
  592. assert Limit(x, x, a, '-').free_symbols == {a}
  593. assert Limit(x + y, x + y, a).free_symbols == {a}
  594. assert Limit(-x**2 + y, x**2, a).free_symbols == {y, a}
  595. def test_issue_9471():
  596. assert limit(((27**(log(n,3)))/n**3),n,oo) == 1
  597. assert limit(((27**(log(n,3)+1))/n**3),n,oo) == 27
  598. def test_issue_11496():
  599. assert limit(erfc(log(1/x)), x, oo) == 2
  600. def test_issue_11879():
  601. assert simplify(limit(((x+y)**n-x**n)/y, y, 0)) == n*x**(n-1)
  602. def test_limit_with_Float():
  603. k = symbols("k")
  604. assert limit(1.0 ** k, k, oo) == 1
  605. assert limit(0.3*1.0**k, k, oo) == Rational(3, 10)
  606. def test_issue_10610():
  607. assert limit(3**x*3**(-x - 1)*(x + 1)**2/x**2, x, oo) == Rational(1, 3)
  608. def test_issue_10868():
  609. assert limit(log(x) + asech(x), x, 0, '+') == log(2)
  610. assert limit(log(x) + asech(x), x, 0, '-') == log(2) + 2*I*pi
  611. raises(ValueError, lambda: limit(log(x) + asech(x), x, 0, '+-'))
  612. assert limit(log(x) + asech(x), x, oo) == oo
  613. assert limit(log(x) + acsch(x), x, 0, '+') == log(2)
  614. assert limit(log(x) + acsch(x), x, 0, '-') == -oo
  615. raises(ValueError, lambda: limit(log(x) + acsch(x), x, 0, '+-'))
  616. assert limit(log(x) + acsch(x), x, oo) == oo
  617. def test_issue_6599():
  618. assert limit((n + cos(n))/n, n, oo) == 1
  619. def test_issue_12555():
  620. assert limit((3**x + 2* x**10) / (x**10 + exp(x)), x, -oo) == 2
  621. assert limit((3**x + 2* x**10) / (x**10 + exp(x)), x, oo) is oo
  622. def test_issue_12769():
  623. r, z, x = symbols('r z x', real=True)
  624. a, b, s0, K, F0, s, T = symbols('a b s0 K F0 s T', positive=True, real=True)
  625. fx = (F0**b*K**b*r*s0 - sqrt((F0**2*K**(2*b)*a**2*(b - 1) + \
  626. F0**(2*b)*K**2*a**2*(b - 1) + F0**(2*b)*K**(2*b)*s0**2*(b - 1)*(b**2 - 2*b + 1) - \
  627. 2*F0**(2*b)*K**(b + 1)*a*r*s0*(b**2 - 2*b + 1) + \
  628. 2*F0**(b + 1)*K**(2*b)*a*r*s0*(b**2 - 2*b + 1) - \
  629. 2*F0**(b + 1)*K**(b + 1)*a**2*(b - 1))/((b - 1)*(b**2 - 2*b + 1))))*(b*r - b - r + 1)
  630. assert fx.subs(K, F0).factor(deep=True) == limit(fx, K, F0).factor(deep=True)
  631. def test_issue_13332():
  632. assert limit(sqrt(30)*5**(-5*x - 1)*(46656*x)**x*(5*x + 2)**(5*x + 5*S.Half) *
  633. (6*x + 2)**(-6*x - 5*S.Half), x, oo) == Rational(25, 36)
  634. def test_issue_12564():
  635. assert limit(x**2 + x*sin(x) + cos(x), x, -oo) is oo
  636. assert limit(x**2 + x*sin(x) + cos(x), x, oo) is oo
  637. assert limit(((x + cos(x))**2).expand(), x, oo) is oo
  638. assert limit(((x + sin(x))**2).expand(), x, oo) is oo
  639. assert limit(((x + cos(x))**2).expand(), x, -oo) is oo
  640. assert limit(((x + sin(x))**2).expand(), x, -oo) is oo
  641. def test_issue_14456():
  642. raises(NotImplementedError, lambda: Limit(exp(x), x, zoo).doit())
  643. raises(NotImplementedError, lambda: Limit(x**2/(x+1), x, zoo).doit())
  644. def test_issue_14411():
  645. assert limit(3*sec(4*pi*x - x/3), x, 3*pi/(24*pi - 2)) is -oo
  646. def test_issue_13382():
  647. assert limit(x*(((x + 1)**2 + 1)/(x**2 + 1) - 1), x, oo) == 2
  648. def test_issue_13403():
  649. assert limit(x*(-1 + (x + log(x + 1) + 1)/(x + log(x))), x, oo) == 1
  650. def test_issue_13416():
  651. assert limit((-x**3*log(x)**3 + (x - 1)*(x + 1)**2*log(x + 1)**3)/(x**2*log(x)**3), x, oo) == 1
  652. def test_issue_13462():
  653. assert limit(n**2*(2*n*(-(1 - 1/(2*n))**x + 1) - x - (-x**2/4 + x/4)/n), n, oo) == x**3/24 - x**2/8 + x/12
  654. def test_issue_13750():
  655. a = Symbol('a')
  656. assert limit(erf(a - x), x, oo) == -1
  657. assert limit(erf(sqrt(x) - x), x, oo) == -1
  658. def test_issue_14276():
  659. assert isinstance(limit(sin(x)**log(x), x, oo), Limit)
  660. assert isinstance(limit(sin(x)**cos(x), x, oo), Limit)
  661. assert isinstance(limit(sin(log(cos(x))), x, oo), Limit)
  662. assert limit((1 + 1/(x**2 + cos(x)))**(x**2 + x), x, oo) == E
  663. def test_issue_14514():
  664. assert limit((1/(log(x)**log(x)))**(1/x), x, oo) == 1
  665. def test_issues_14525():
  666. assert limit(sin(x)**2 - cos(x) + tan(x)*csc(x), x, oo) == AccumBounds(S.NegativeInfinity, S.Infinity)
  667. assert limit(sin(x)**2 - cos(x) + sin(x)*cot(x), x, oo) == AccumBounds(S.NegativeInfinity, S.Infinity)
  668. assert limit(cot(x) - tan(x)**2, x, oo) == AccumBounds(S.NegativeInfinity, S.Infinity)
  669. assert limit(cos(x) - tan(x)**2, x, oo) == AccumBounds(S.NegativeInfinity, S.One)
  670. assert limit(sin(x) - tan(x)**2, x, oo) == AccumBounds(S.NegativeInfinity, S.One)
  671. assert limit(cos(x)**2 - tan(x)**2, x, oo) == AccumBounds(S.NegativeInfinity, S.One)
  672. assert limit(tan(x)**2 + sin(x)**2 - cos(x), x, oo) == AccumBounds(-S.One, S.Infinity)
  673. def test_issue_14574():
  674. assert limit(sqrt(x)*cos(x - x**2) / (x + 1), x, oo) == 0
  675. def test_issue_10102():
  676. assert limit(fresnels(x), x, oo) == S.Half
  677. assert limit(3 + fresnels(x), x, oo) == 3 + S.Half
  678. assert limit(5*fresnels(x), x, oo) == Rational(5, 2)
  679. assert limit(fresnelc(x), x, oo) == S.Half
  680. assert limit(fresnels(x), x, -oo) == Rational(-1, 2)
  681. assert limit(4*fresnelc(x), x, -oo) == -2
  682. def test_issue_14377():
  683. raises(NotImplementedError, lambda: limit(exp(I*x)*sin(pi*x), x, oo))
  684. def test_issue_15146():
  685. e = (x/2) * (-2*x**3 - 2*(x**3 - 1) * x**2 * digamma(x**3 + 1) + \
  686. 2*(x**3 - 1) * x**2 * digamma(x**3 + x + 1) + x + 3)
  687. assert limit(e, x, oo) == S(1)/3
  688. def test_issue_15202():
  689. e = (2**x*(2 + 2**(-x)*(-2*2**x + x + 2))/(x + 1))**(x + 1)
  690. assert limit(e, x, oo) == exp(1)
  691. e = (log(x, 2)**7 + 10*x*factorial(x) + 5**x) / (factorial(x + 1) + 3*factorial(x) + 10**x)
  692. assert limit(e, x, oo) == 10
  693. def test_issue_15282():
  694. assert limit((x**2000 - (x + 1)**2000) / x**1999, x, oo) == -2000
  695. def test_issue_15984():
  696. assert limit((-x + log(exp(x) + 1))/x, x, oo, dir='-') == 0
  697. def test_issue_13571():
  698. assert limit(uppergamma(x, 1) / gamma(x), x, oo) == 1
  699. def test_issue_13575():
  700. assert limit(acos(erfi(x)), x, 1) == acos(erfi(S.One))
  701. def test_issue_17325():
  702. assert Limit(sin(x)/x, x, 0, dir="+-").doit() == 1
  703. assert Limit(x**2, x, 0, dir="+-").doit() == 0
  704. assert Limit(1/x**2, x, 0, dir="+-").doit() is oo
  705. assert Limit(1/x, x, 0, dir="+-").doit() is zoo
  706. def test_issue_10978():
  707. assert LambertW(x).limit(x, 0) == 0
  708. def test_issue_14313_comment():
  709. assert limit(floor(n/2), n, oo) is oo
  710. @XFAIL
  711. def test_issue_15323():
  712. d = ((1 - 1/x)**x).diff(x)
  713. assert limit(d, x, 1, dir='+') == 1
  714. def test_issue_12571():
  715. assert limit(-LambertW(-log(x))/log(x), x, 1) == 1
  716. def test_issue_14590():
  717. assert limit((x**3*((x + 1)/x)**x)/((x + 1)*(x + 2)*(x + 3)), x, oo) == exp(1)
  718. def test_issue_14393():
  719. a, b = symbols('a b')
  720. assert limit((x**b - y**b)/(x**a - y**a), x, y) == b*y**(-a + b)/a
  721. def test_issue_14556():
  722. assert limit(factorial(n + 1)**(1/(n + 1)) - factorial(n)**(1/n), n, oo) == exp(-1)
  723. def test_issue_14811():
  724. assert limit(((1 + ((S(2)/3)**(x + 1)))**(2**x))/(2**((S(4)/3)**(x - 1))), x, oo) == oo
  725. def test_issue_16222():
  726. assert limit(exp(x), x, 1000000000) == exp(1000000000)
  727. def test_issue_16714():
  728. assert limit(((x**(x + 1) + (x + 1)**x) / x**(x + 1))**x, x, oo) == exp(exp(1))
  729. def test_issue_16722():
  730. z = symbols('z', positive=True)
  731. assert limit(binomial(n + z, n)*n**-z, n, oo) == 1/gamma(z + 1)
  732. z = symbols('z', positive=True, integer=True)
  733. assert limit(binomial(n + z, n)*n**-z, n, oo) == 1/gamma(z + 1)
  734. def test_issue_17431():
  735. assert limit(((n + 1) + 1) / (((n + 1) + 2) * factorial(n + 1)) *
  736. (n + 2) * factorial(n) / (n + 1), n, oo) == 0
  737. assert limit((n + 2)**2*factorial(n)/((n + 1)*(n + 3)*factorial(n + 1))
  738. , n, oo) == 0
  739. assert limit((n + 1) * factorial(n) / (n * factorial(n + 1)), n, oo) == 0
  740. def test_issue_17671():
  741. assert limit(Ei(-log(x)) - log(log(x))/x, x, 1) == EulerGamma
  742. def test_issue_17751():
  743. a, b, c, x = symbols('a b c x', positive=True)
  744. assert limit((a + 1)*x - sqrt((a + 1)**2*x**2 + b*x + c), x, oo) == -b/(2*a + 2)
  745. def test_issue_17792():
  746. assert limit(factorial(n)/sqrt(n)*(exp(1)/n)**n, n, oo) == sqrt(2)*sqrt(pi)
  747. def test_issue_18118():
  748. assert limit(sign(sin(x)), x, 0, "-") == -1
  749. assert limit(sign(sin(x)), x, 0, "+") == 1
  750. def test_issue_18306():
  751. assert limit(sin(sqrt(x))/sqrt(sin(x)), x, 0, '+') == 1
  752. def test_issue_18378():
  753. assert limit(log(exp(3*x) + x)/log(exp(x) + x**100), x, oo) == 3
  754. def test_issue_18399():
  755. assert limit((1 - S(1)/2*x)**(3*x), x, oo) is zoo
  756. assert limit((-x)**x, x, oo) is zoo
  757. def test_issue_18442():
  758. assert limit(tan(x)**(2**(sqrt(pi))), x, oo, dir='-') == Limit(tan(x)**(2**(sqrt(pi))), x, oo, dir='-')
  759. def test_issue_18452():
  760. assert limit(abs(log(x))**x, x, 0) == 1
  761. assert limit(abs(log(x))**x, x, 0, "-") == 1
  762. def test_issue_18473():
  763. assert limit(sin(x)**(1/x), x, oo) == Limit(sin(x)**(1/x), x, oo, dir='-')
  764. assert limit(cos(x)**(1/x), x, oo) == Limit(cos(x)**(1/x), x, oo, dir='-')
  765. assert limit(tan(x)**(1/x), x, oo) == Limit(tan(x)**(1/x), x, oo, dir='-')
  766. assert limit((cos(x) + 2)**(1/x), x, oo) == 1
  767. assert limit((sin(x) + 10)**(1/x), x, oo) == 1
  768. assert limit((cos(x) - 2)**(1/x), x, oo) == Limit((cos(x) - 2)**(1/x), x, oo, dir='-')
  769. assert limit((cos(x) + 1)**(1/x), x, oo) == AccumBounds(0, 1)
  770. assert limit((tan(x)**2)**(2/x) , x, oo) == AccumBounds(0, oo)
  771. assert limit((sin(x)**2)**(1/x), x, oo) == AccumBounds(0, 1)
  772. # Tests for issue #23751
  773. assert limit((cos(x) + 1)**(1/x), x, -oo) == AccumBounds(1, oo)
  774. assert limit((sin(x)**2)**(1/x), x, -oo) == AccumBounds(1, oo)
  775. assert limit((tan(x)**2)**(2/x) , x, -oo) == AccumBounds(0, oo)
  776. def test_issue_18482():
  777. assert limit((2*exp(3*x)/(exp(2*x) + 1))**(1/x), x, oo) == exp(1)
  778. def test_issue_18508():
  779. assert limit(sin(x)/sqrt(1-cos(x)), x, 0) == sqrt(2)
  780. assert limit(sin(x)/sqrt(1-cos(x)), x, 0, dir='+') == sqrt(2)
  781. assert limit(sin(x)/sqrt(1-cos(x)), x, 0, dir='-') == -sqrt(2)
  782. def test_issue_18521():
  783. raises(NotImplementedError, lambda: limit(exp((2 - n) * x), x, oo))
  784. def test_issue_18969():
  785. a, b = symbols('a b', positive=True)
  786. assert limit(LambertW(a), a, b) == LambertW(b)
  787. assert limit(exp(LambertW(a)), a, b) == exp(LambertW(b))
  788. def test_issue_18992():
  789. assert limit(n/(factorial(n)**(1/n)), n, oo) == exp(1)
  790. def test_issue_19067():
  791. x = Symbol('x')
  792. assert limit(gamma(x)/(gamma(x - 1)*gamma(x + 2)), x, 0) == -1
  793. def test_issue_19586():
  794. assert limit(x**(2**x*3**(-x)), x, oo) == 1
  795. def test_issue_13715():
  796. n = Symbol('n')
  797. p = Symbol('p', zero=True)
  798. assert limit(n + p, n, 0) == 0
  799. def test_issue_15055():
  800. assert limit(n**3*((-n - 1)*sin(1/n) + (n + 2)*sin(1/(n + 1)))/(-n + 1), n, oo) == 1
  801. def test_issue_16708():
  802. m, vi = symbols('m vi', positive=True)
  803. B, ti, d = symbols('B ti d')
  804. assert limit((B*ti*vi - sqrt(m)*sqrt(-2*B*d*vi + m*(vi)**2) + m*vi)/(B*vi), B, 0) == (d + ti*vi)/vi
  805. def test_issue_19154():
  806. assert limit(besseli(1, 3 *x)/(x *besseli(1, x)**3), x , oo) == 2*sqrt(3)*pi/3
  807. assert limit(besseli(1, 3 *x)/(x *besseli(1, x)**3), x , -oo) == -2*sqrt(3)*pi/3
  808. def test_issue_19453():
  809. beta = Symbol("beta", positive=True)
  810. h = Symbol("h", positive=True)
  811. m = Symbol("m", positive=True)
  812. w = Symbol("omega", positive=True)
  813. g = Symbol("g", positive=True)
  814. e = exp(1)
  815. q = 3*h**2*beta*g*e**(0.5*h*beta*w)
  816. p = m**2*w**2
  817. s = e**(h*beta*w) - 1
  818. Z = -q/(4*p*s) - q/(2*p*s**2) - q*(e**(h*beta*w) + 1)/(2*p*s**3)\
  819. + e**(0.5*h*beta*w)/s
  820. E = -diff(log(Z), beta)
  821. assert limit(E - 0.5*h*w, beta, oo) == 0
  822. assert limit(E.simplify() - 0.5*h*w, beta, oo) == 0
  823. def test_issue_19739():
  824. assert limit((-S(1)/4)**x, x, oo) == 0
  825. def test_issue_19766():
  826. assert limit(2**(-x)*sqrt(4**(x + 1) + 1), x, oo) == 2
  827. def test_issue_19770():
  828. m = Symbol('m')
  829. # the result is not 0 for non-real m
  830. assert limit(cos(m*x)/x, x, oo) == Limit(cos(m*x)/x, x, oo, dir='-')
  831. m = Symbol('m', real=True)
  832. # can be improved to give the correct result 0
  833. assert limit(cos(m*x)/x, x, oo) == Limit(cos(m*x)/x, x, oo, dir='-')
  834. m = Symbol('m', nonzero=True)
  835. assert limit(cos(m*x), x, oo) == AccumBounds(-1, 1)
  836. assert limit(cos(m*x)/x, x, oo) == 0
  837. def test_issue_7535():
  838. assert limit(tan(x)/sin(tan(x)), x, pi/2) == Limit(tan(x)/sin(tan(x)), x, pi/2, dir='+')
  839. assert limit(tan(x)/sin(tan(x)), x, pi/2, dir='-') == Limit(tan(x)/sin(tan(x)), x, pi/2, dir='-')
  840. assert limit(tan(x)/sin(tan(x)), x, pi/2, dir='+-') == Limit(tan(x)/sin(tan(x)), x, pi/2, dir='+-')
  841. assert limit(sin(tan(x)),x,pi/2) == AccumBounds(-1, 1)
  842. assert -oo*(1/sin(-oo)) == AccumBounds(-oo, oo)
  843. assert oo*(1/sin(oo)) == AccumBounds(-oo, oo)
  844. assert oo*(1/sin(-oo)) == AccumBounds(-oo, oo)
  845. assert -oo*(1/sin(oo)) == AccumBounds(-oo, oo)
  846. def test_issue_20365():
  847. assert limit(((x + 1)**(1/x) - E)/x, x, 0) == -E/2
  848. def test_issue_21031():
  849. assert limit(((1 + x)**(1/x) - (1 + 2*x)**(1/(2*x)))/asin(x), x, 0) == E/2
  850. def test_issue_21038():
  851. assert limit(sin(pi*x)/(3*x - 12), x, 4) == pi/3
  852. def test_issue_20578():
  853. expr = abs(x) * sin(1/x)
  854. assert limit(expr,x,0,'+') == 0
  855. assert limit(expr,x,0,'-') == 0
  856. assert limit(expr,x,0,'+-') == 0
  857. def test_issue_21227():
  858. f = log(x)
  859. assert f.nseries(x, logx=y) == y
  860. assert f.nseries(x, logx=-x) == -x
  861. f = log(-log(x))
  862. assert f.nseries(x, logx=y) == log(-y)
  863. assert f.nseries(x, logx=-x) == log(x)
  864. f = log(log(x))
  865. assert f.nseries(x, logx=y) == log(y)
  866. assert f.nseries(x, logx=-x) == log(-x)
  867. assert f.nseries(x, logx=x) == log(x)
  868. f = log(log(log(1/x)))
  869. assert f.nseries(x, logx=y) == log(log(-y))
  870. assert f.nseries(x, logx=-y) == log(log(y))
  871. assert f.nseries(x, logx=x) == log(log(-x))
  872. assert f.nseries(x, logx=-x) == log(log(x))
  873. def test_issue_21415():
  874. exp = (x-1)*cos(1/(x-1))
  875. assert exp.limit(x,1) == 0
  876. assert exp.expand().limit(x,1) == 0
  877. def test_issue_21530():
  878. assert limit(sinh(n + 1)/sinh(n), n, oo) == E
  879. def test_issue_21550():
  880. r = (sqrt(5) - 1)/2
  881. assert limit((x - r)/(x**2 + x - 1), x, r) == sqrt(5)/5
  882. def test_issue_21661():
  883. out = limit((x**(x + 1) * (log(x) + 1) + 1) / x, x, 11)
  884. assert out == S(3138428376722)/11 + 285311670611*log(11)
  885. def test_issue_21701():
  886. assert limit((besselj(z, x)/x**z).subs(z, 7), x, 0) == S(1)/645120
  887. def test_issue_21721():
  888. a = Symbol('a', real=True)
  889. I = integrate(1/(pi*(1 + (x - a)**2)), x)
  890. assert I.limit(x, oo) == S.Half
  891. def test_issue_21756():
  892. term = (1 - exp(-2*I*pi*z))/(1 - exp(-2*I*pi*z/5))
  893. assert term.limit(z, 0) == 5
  894. assert re(term).limit(z, 0) == 5
  895. def test_issue_21785():
  896. a = Symbol('a')
  897. assert sqrt((-a**2 + x**2)/(1 - x**2)).limit(a, 1, '-') == I
  898. def test_issue_22181():
  899. assert limit((-1)**x * 2**(-x), x, oo) == 0
  900. def test_issue_22220():
  901. e1 = sqrt(30)*atan(sqrt(30)*tan(x/2)/6)/30
  902. e2 = sqrt(30)*I*(-log(sqrt(2)*tan(x/2) - 2*sqrt(15)*I/5) +
  903. +log(sqrt(2)*tan(x/2) + 2*sqrt(15)*I/5))/60
  904. assert limit(e1, x, -pi) == -sqrt(30)*pi/60
  905. assert limit(e2, x, -pi) == -sqrt(30)*pi/30
  906. assert limit(e1, x, -pi, '-') == sqrt(30)*pi/60
  907. assert limit(e2, x, -pi, '-') == 0
  908. # test https://github.com/sympy/sympy/issues/22220#issuecomment-972727694
  909. expr = log(x - I) - log(-x - I)
  910. expr2 = logcombine(expr, force=True)
  911. assert limit(expr, x, oo) == limit(expr2, x, oo) == I*pi
  912. # test https://github.com/sympy/sympy/issues/22220#issuecomment-1077618340
  913. expr = expr = (-log(tan(x/2) - I) +log(tan(x/2) + I))
  914. assert limit(expr, x, pi, '+') == 2*I*pi
  915. assert limit(expr, x, pi, '-') == 0
  916. def test_issue_22334():
  917. k, n = symbols('k, n', positive=True)
  918. assert limit((n+1)**k/((n+1)**(k+1) - (n)**(k+1)), n, oo) == 1/(k + 1)
  919. assert limit((n+1)**k/((n+1)**(k+1) - (n)**(k+1)).expand(), n, oo) == 1/(k + 1)
  920. assert limit((n+1)**k/(n*(-n**k + (n + 1)**k) + (n + 1)**k), n, oo) == 1/(k + 1)
  921. def test_sympyissue_22986():
  922. assert limit(acosh(1 + 1/x)*sqrt(x), x, oo) == sqrt(2)
  923. def test_issue_23231():
  924. f = (2**x - 2**(-x))/(2**x + 2**(-x))
  925. assert limit(f, x, -oo) == -1
  926. def test_issue_23596():
  927. assert integrate(((1 + x)/x**2)*exp(-1/x), (x, 0, oo)) == oo
  928. def test_issue_23752():
  929. expr1 = sqrt(-I*x**2 + x - 3)
  930. expr2 = sqrt(-I*x**2 + I*x - 3)
  931. assert limit(expr1, x, 0, '+') == -sqrt(3)*I
  932. assert limit(expr1, x, 0, '-') == -sqrt(3)*I
  933. assert limit(expr2, x, 0, '+') == sqrt(3)*I
  934. assert limit(expr2, x, 0, '-') == -sqrt(3)*I
  935. def test_issue_24276():
  936. fx = log(tan(pi/2*tanh(x))).diff(x)
  937. assert fx.limit(x, oo) == 2
  938. assert fx.simplify().limit(x, oo) == 2
  939. assert fx.rewrite(sin).limit(x, oo) == 2
  940. assert fx.rewrite(sin).simplify().limit(x, oo) == 2