test_limits.py 47 KB

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