test_series.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. from sympy.core.evalf import N
  2. from sympy.core.function import (Derivative, Function, PoleError, Subs)
  3. from sympy.core.numbers import (E, Float, Rational, oo, pi, I)
  4. from sympy.core.singleton import S
  5. from sympy.core.symbol import (Symbol, symbols)
  6. from sympy.functions.elementary.exponential import (LambertW, exp, log)
  7. from sympy.functions.elementary.miscellaneous import sqrt
  8. from sympy.functions.elementary.trigonometric import (atan, cos, sin)
  9. from sympy.functions.special.gamma_functions import gamma
  10. from sympy.integrals.integrals import Integral, integrate
  11. from sympy.series.order import O
  12. from sympy.series.series import series
  13. from sympy.abc import x, y, n, k
  14. from sympy.testing.pytest import raises
  15. def test_sin():
  16. e1 = sin(x).series(x, 0)
  17. e2 = series(sin(x), x, 0)
  18. assert e1 == e2
  19. def test_cos():
  20. e1 = cos(x).series(x, 0)
  21. e2 = series(cos(x), x, 0)
  22. assert e1 == e2
  23. def test_exp():
  24. e1 = exp(x).series(x, 0)
  25. e2 = series(exp(x), x, 0)
  26. assert e1 == e2
  27. def test_exp2():
  28. e1 = exp(cos(x)).series(x, 0)
  29. e2 = series(exp(cos(x)), x, 0)
  30. assert e1 == e2
  31. def test_issue_5223():
  32. assert series(1, x) == 1
  33. assert next(S.Zero.lseries(x)) == 0
  34. assert cos(x).series() == cos(x).series(x)
  35. raises(ValueError, lambda: cos(x + y).series())
  36. raises(ValueError, lambda: x.series(dir=""))
  37. assert (cos(x).series(x, 1) -
  38. cos(x + 1).series(x).subs(x, x - 1)).removeO() == 0
  39. e = cos(x).series(x, 1, n=None)
  40. assert [next(e) for i in range(2)] == [cos(1), -((x - 1)*sin(1))]
  41. e = cos(x).series(x, 1, n=None, dir='-')
  42. assert [next(e) for i in range(2)] == [cos(1), (1 - x)*sin(1)]
  43. # the following test is exact so no need for x -> x - 1 replacement
  44. assert abs(x).series(x, 1, dir='-') == x
  45. assert exp(x).series(x, 1, dir='-', n=3).removeO() == \
  46. E - E*(-x + 1) + E*(-x + 1)**2/2
  47. D = Derivative
  48. assert D(x**2 + x**3*y**2, x, 2, y, 1).series(x).doit() == 12*x*y
  49. assert next(D(cos(x), x).lseries()) == D(1, x)
  50. assert D(
  51. exp(x), x).series(n=3) == D(1, x) + D(x, x) + D(x**2/2, x) + D(x**3/6, x) + O(x**3)
  52. assert Integral(x, (x, 1, 3), (y, 1, x)).series(x) == -4 + 4*x
  53. assert (1 + x + O(x**2)).getn() == 2
  54. assert (1 + x).getn() is None
  55. raises(PoleError, lambda: ((1/sin(x))**oo).series())
  56. logx = Symbol('logx')
  57. assert ((sin(x))**y).nseries(x, n=1, logx=logx) == \
  58. exp(y*logx) + O(x*exp(y*logx), x)
  59. assert sin(1/x).series(x, oo, n=5) == 1/x - 1/(6*x**3) + O(x**(-5), (x, oo))
  60. assert abs(x).series(x, oo, n=5, dir='+') == x
  61. assert abs(x).series(x, -oo, n=5, dir='-') == -x
  62. assert abs(-x).series(x, oo, n=5, dir='+') == x
  63. assert abs(-x).series(x, -oo, n=5, dir='-') == -x
  64. assert exp(x*log(x)).series(n=3) == \
  65. 1 + x*log(x) + x**2*log(x)**2/2 + O(x**3*log(x)**3)
  66. # XXX is this right? If not, fix "ngot > n" handling in expr.
  67. p = Symbol('p', positive=True)
  68. assert exp(sqrt(p)**3*log(p)).series(n=3) == \
  69. 1 + p**S('3/2')*log(p) + O(p**3*log(p)**3)
  70. assert exp(sin(x)*log(x)).series(n=2) == 1 + x*log(x) + O(x**2*log(x)**2)
  71. def test_issue_6350():
  72. expr = integrate(exp(k*(y**3 - 3*y)), (y, 0, oo), conds='none')
  73. assert expr.series(k, 0, 3) == -(-1)**(S(2)/3)*sqrt(3)*gamma(S(1)/3)**2*gamma(S(2)/3)/(6*pi*k**(S(1)/3)) - \
  74. sqrt(3)*k*gamma(-S(2)/3)*gamma(-S(1)/3)/(6*pi) - \
  75. (-1)**(S(1)/3)*sqrt(3)*k**(S(1)/3)*gamma(-S(1)/3)*gamma(S(1)/3)*gamma(S(2)/3)/(6*pi) - \
  76. (-1)**(S(2)/3)*sqrt(3)*k**(S(5)/3)*gamma(S(1)/3)**2*gamma(S(2)/3)/(4*pi) - \
  77. (-1)**(S(1)/3)*sqrt(3)*k**(S(7)/3)*gamma(-S(1)/3)*gamma(S(1)/3)*gamma(S(2)/3)/(8*pi) + O(k**3)
  78. def test_issue_11313():
  79. assert Integral(cos(x), x).series(x) == sin(x).series(x)
  80. assert Derivative(sin(x), x).series(x, n=3).doit() == cos(x).series(x, n=3)
  81. assert Derivative(x**3, x).as_leading_term(x) == 3*x**2
  82. assert Derivative(x**3, y).as_leading_term(x) == 0
  83. assert Derivative(sin(x), x).as_leading_term(x) == 1
  84. assert Derivative(cos(x), x).as_leading_term(x) == -x
  85. # This result is equivalent to zero, zero is not return because
  86. # `Expr.series` doesn't currently detect an `x` in its `free_symbol`s.
  87. assert Derivative(1, x).as_leading_term(x) == Derivative(1, x)
  88. assert Derivative(exp(x), x).series(x).doit() == exp(x).series(x)
  89. assert 1 + Integral(exp(x), x).series(x) == exp(x).series(x)
  90. assert Derivative(log(x), x).series(x).doit() == (1/x).series(x)
  91. assert Integral(log(x), x).series(x) == Integral(log(x), x).doit().series(x).removeO()
  92. def test_series_of_Subs():
  93. from sympy.abc import z
  94. subs1 = Subs(sin(x), x, y)
  95. subs2 = Subs(sin(x) * cos(z), x, y)
  96. subs3 = Subs(sin(x * z), (x, z), (y, x))
  97. assert subs1.series(x) == subs1
  98. subs1_series = (Subs(x, x, y) + Subs(-x**3/6, x, y) +
  99. Subs(x**5/120, x, y) + O(y**6))
  100. assert subs1.series() == subs1_series
  101. assert subs1.series(y) == subs1_series
  102. assert subs1.series(z) == subs1
  103. assert subs2.series(z) == (Subs(z**4*sin(x)/24, x, y) +
  104. Subs(-z**2*sin(x)/2, x, y) + Subs(sin(x), x, y) + O(z**6))
  105. assert subs3.series(x).doit() == subs3.doit().series(x)
  106. assert subs3.series(z).doit() == sin(x*y)
  107. raises(ValueError, lambda: Subs(x + 2*y, y, z).series())
  108. assert Subs(x + y, y, z).series(x).doit() == x + z
  109. def test_issue_3978():
  110. f = Function('f')
  111. assert f(x).series(x, 0, 3, dir='-') == \
  112. f(0) + x*Subs(Derivative(f(x), x), x, 0) + \
  113. x**2*Subs(Derivative(f(x), x, x), x, 0)/2 + O(x**3)
  114. assert f(x).series(x, 0, 3) == \
  115. f(0) + x*Subs(Derivative(f(x), x), x, 0) + \
  116. x**2*Subs(Derivative(f(x), x, x), x, 0)/2 + O(x**3)
  117. assert f(x**2).series(x, 0, 3) == \
  118. f(0) + x**2*Subs(Derivative(f(x), x), x, 0) + O(x**3)
  119. assert f(x**2+1).series(x, 0, 3) == \
  120. f(1) + x**2*Subs(Derivative(f(x), x), x, 1) + O(x**3)
  121. class TestF(Function):
  122. pass
  123. assert TestF(x).series(x, 0, 3) == TestF(0) + \
  124. x*Subs(Derivative(TestF(x), x), x, 0) + \
  125. x**2*Subs(Derivative(TestF(x), x, x), x, 0)/2 + O(x**3)
  126. from sympy.series.acceleration import richardson, shanks
  127. from sympy.concrete.summations import Sum
  128. from sympy.core.numbers import Integer
  129. def test_acceleration():
  130. e = (1 + 1/n)**n
  131. assert round(richardson(e, n, 10, 20).evalf(), 10) == round(E.evalf(), 10)
  132. A = Sum(Integer(-1)**(k + 1) / k, (k, 1, n))
  133. assert round(shanks(A, n, 25).evalf(), 4) == round(log(2).evalf(), 4)
  134. assert round(shanks(A, n, 25, 5).evalf(), 10) == round(log(2).evalf(), 10)
  135. def test_issue_5852():
  136. assert series(1/cos(x/log(x)), x, 0) == 1 + x**2/(2*log(x)**2) + \
  137. 5*x**4/(24*log(x)**4) + O(x**6)
  138. def test_issue_4583():
  139. assert cos(1 + x + x**2).series(x, 0, 5) == cos(1) - x*sin(1) + \
  140. x**2*(-sin(1) - cos(1)/2) + x**3*(-cos(1) + sin(1)/6) + \
  141. x**4*(-11*cos(1)/24 + sin(1)/2) + O(x**5)
  142. def test_issue_6318():
  143. eq = (1/x)**Rational(2, 3)
  144. assert (eq + 1).as_leading_term(x) == eq
  145. def test_x_is_base_detection():
  146. eq = (x**2)**Rational(2, 3)
  147. assert eq.series() == x**Rational(4, 3)
  148. def test_issue_7203():
  149. assert series(cos(x), x, pi, 3) == \
  150. -1 + (x - pi)**2/2 + O((x - pi)**3, (x, pi))
  151. def test_exp_product_positive_factors():
  152. a, b = symbols('a, b', positive=True)
  153. x = a * b
  154. assert series(exp(x), x, n=8) == 1 + a*b + a**2*b**2/2 + \
  155. a**3*b**3/6 + a**4*b**4/24 + a**5*b**5/120 + a**6*b**6/720 + \
  156. a**7*b**7/5040 + O(a**8*b**8, a, b)
  157. def test_issue_8805():
  158. assert series(1, n=8) == 1
  159. def test_issue_9549():
  160. y = (x**2 + x + 1) / (x**3 + x**2)
  161. assert series(y, x, oo) == x**(-5) - 1/x**4 + x**(-3) + 1/x + O(x**(-6), (x, oo))
  162. def test_issue_10761():
  163. assert series(1/(x**-2 + x**-3), x, 0) == x**3 - x**4 + x**5 + O(x**6)
  164. def test_issue_12578():
  165. y = (1 - 1/(x/2 - 1/(2*x))**4)**(S(1)/8)
  166. assert y.series(x, 0, n=17) == 1 - 2*x**4 - 8*x**6 - 34*x**8 - 152*x**10 - 714*x**12 - \
  167. 3472*x**14 - 17318*x**16 + O(x**17)
  168. def test_issue_12791():
  169. beta = symbols('beta', positive=True)
  170. theta, varphi = symbols('theta varphi', real=True)
  171. expr = (-beta**2*varphi*sin(theta) + beta**2*cos(theta) + \
  172. beta*varphi*sin(theta) - beta*cos(theta) - beta + 1)/(beta*cos(theta) - 1)**2
  173. sol = 0.5/(0.5*cos(theta) - 1.0)**2 - 0.25*cos(theta)/(0.5*cos(theta)\
  174. - 1.0)**2 + (beta - 0.5)*(-0.25*varphi*sin(2*theta) - 1.5*cos(theta)\
  175. + 0.25*cos(2*theta) + 1.25)/(0.5*cos(theta) - 1.0)**3\
  176. + 0.25*varphi*sin(theta)/(0.5*cos(theta) - 1.0)**2 + O((beta - S.Half)**2, (beta, S.Half))
  177. assert expr.series(beta, 0.5, 2).trigsimp() == sol
  178. def test_issue_14384():
  179. x, a = symbols('x a')
  180. assert series(x**a, x) == x**a
  181. assert series(x**(-2*a), x) == x**(-2*a)
  182. assert series(exp(a*log(x)), x) == exp(a*log(x))
  183. assert series(x**I, x) == x**I
  184. assert series(x**(I + 1), x) == x**(1 + I)
  185. assert series(exp(I*log(x)), x) == exp(I*log(x))
  186. def test_issue_14885():
  187. assert series(x**Rational(-3, 2)*exp(x), x, 0) == (x**Rational(-3, 2) + 1/sqrt(x) +
  188. sqrt(x)/2 + x**Rational(3, 2)/6 + x**Rational(5, 2)/24 + x**Rational(7, 2)/120 +
  189. x**Rational(9, 2)/720 + x**Rational(11, 2)/5040 + O(x**6))
  190. def test_issue_15539():
  191. assert series(atan(x), x, -oo) == (-1/(5*x**5) + 1/(3*x**3) - 1/x - pi/2
  192. + O(x**(-6), (x, -oo)))
  193. assert series(atan(x), x, oo) == (-1/(5*x**5) + 1/(3*x**3) - 1/x + pi/2
  194. + O(x**(-6), (x, oo)))
  195. def test_issue_7259():
  196. assert series(LambertW(x), x) == x - x**2 + 3*x**3/2 - 8*x**4/3 + 125*x**5/24 + O(x**6)
  197. assert series(LambertW(x**2), x, n=8) == x**2 - x**4 + 3*x**6/2 + O(x**8)
  198. assert series(LambertW(sin(x)), x, n=4) == x - x**2 + 4*x**3/3 + O(x**4)
  199. def test_issue_11884():
  200. assert cos(x).series(x, 1, n=1) == cos(1) + O(x - 1, (x, 1))
  201. def test_issue_18008():
  202. y = x*(1 + x*(1 - x))/((1 + x*(1 - x)) - (1 - x)*(1 - x))
  203. assert y.series(x, oo, n=4) == -9/(32*x**3) - 3/(16*x**2) - 1/(8*x) + S(1)/4 + x/2 + \
  204. O(x**(-4), (x, oo))
  205. def test_issue_18842():
  206. f = log(x/(1 - x))
  207. assert f.series(x, 0.491, n=1).removeO().nsimplify() == \
  208. -S(180019443780011)/5000000000000000
  209. def test_issue_19534():
  210. dt = symbols('dt', real=True)
  211. expr = 16*dt*(0.125*dt*(2.0*dt + 1.0) + 0.875*dt + 1.0)/45 + \
  212. 49*dt*(-0.049335189898860408029*dt*(2.0*dt + 1.0) + \
  213. 0.29601113939316244817*dt*(0.125*dt*(2.0*dt + 1.0) + 0.875*dt + 1.0) - \
  214. 0.12564355335492979587*dt*(0.074074074074074074074*dt*(2.0*dt + 1.0) + \
  215. 0.2962962962962962963*dt*(0.125*dt*(2.0*dt + 1.0) + 0.875*dt + 1.0) + \
  216. 0.96296296296296296296*dt + 1.0) + 0.051640768506639183825*dt + \
  217. dt*(1/2 - sqrt(21)/14) + 1.0)/180 + 49*dt*(-0.23637909581542530626*dt*(2.0*dt + 1.0) - \
  218. 0.74817562366625959291*dt*(0.125*dt*(2.0*dt + 1.0) + 0.875*dt + 1.0) + \
  219. 0.88085458023927036857*dt*(0.074074074074074074074*dt*(2.0*dt + 1.0) + \
  220. 0.2962962962962962963*dt*(0.125*dt*(2.0*dt + 1.0) + 0.875*dt + 1.0) + \
  221. 0.96296296296296296296*dt + 1.0) + \
  222. 2.1165151389911680013*dt*(-0.049335189898860408029*dt*(2.0*dt + 1.0) + \
  223. 0.29601113939316244817*dt*(0.125*dt*(2.0*dt + 1.0) + 0.875*dt + 1.0) - \
  224. 0.12564355335492979587*dt*(0.074074074074074074074*dt*(2.0*dt + 1.0) + \
  225. 0.2962962962962962963*dt*(0.125*dt*(2.0*dt + 1.0) + 0.875*dt + 1.0) + \
  226. 0.96296296296296296296*dt + 1.0) + 0.22431393315265061193*dt + 1.0) - \
  227. 1.1854881643947648988*dt + dt*(sqrt(21)/14 + 1/2) + 1.0)/180 + \
  228. dt*(0.66666666666666666667*dt*(2.0*dt + 1.0) + \
  229. 6.0173399699313066769*dt*(0.125*dt*(2.0*dt + 1.0) + 0.875*dt + 1.0) - \
  230. 4.1117044797036320069*dt*(0.074074074074074074074*dt*(2.0*dt + 1.0) + \
  231. 0.2962962962962962963*dt*(0.125*dt*(2.0*dt + 1.0) + 0.875*dt + 1.0) + \
  232. 0.96296296296296296296*dt + 1.0) - \
  233. 7.0189140975801991157*dt*(-0.049335189898860408029*dt*(2.0*dt + 1.0) + \
  234. 0.29601113939316244817*dt*(0.125*dt*(2.0*dt + 1.0) + 0.875*dt + 1.0) - \
  235. 0.12564355335492979587*dt*(0.074074074074074074074*dt*(2.0*dt + 1.0) + \
  236. 0.2962962962962962963*dt*(0.125*dt*(2.0*dt + 1.0) + 0.875*dt + 1.0) + \
  237. 0.96296296296296296296*dt + 1.0) + 0.22431393315265061193*dt + 1.0) + \
  238. 0.94010945196161777522*dt*(-0.23637909581542530626*dt*(2.0*dt + 1.0) - \
  239. 0.74817562366625959291*dt*(0.125*dt*(2.0*dt + 1.0) + 0.875*dt + 1.0) + \
  240. 0.88085458023927036857*dt*(0.074074074074074074074*dt*(2.0*dt + 1.0) + \
  241. 0.2962962962962962963*dt*(0.125*dt*(2.0*dt + 1.0) + 0.875*dt + 1.0) + \
  242. 0.96296296296296296296*dt + 1.0) + \
  243. 2.1165151389911680013*dt*(-0.049335189898860408029*dt*(2.0*dt + 1.0) + \
  244. 0.29601113939316244817*dt*(0.125*dt*(2.0*dt + 1.0) + 0.875*dt + 1.0) - \
  245. 0.12564355335492979587*dt*(0.074074074074074074074*dt*(2.0*dt + 1.0) + \
  246. 0.2962962962962962963*dt*(0.125*dt*(2.0*dt + 1.0) + 0.875*dt + 1.0) + \
  247. 0.96296296296296296296*dt + 1.0) + 0.22431393315265061193*dt + 1.0) - \
  248. 0.35816132904077632692*dt + 1.0) + 5.5065024887242400038*dt + 1.0)/20 + dt/20 + 1
  249. assert N(expr.series(dt, 0, 8), 20) == (
  250. - Float('0.00092592592592592596126289', precision=70) * dt**7
  251. + Float('0.0027777777777777783174695', precision=70) * dt**6
  252. + Float('0.016666666666666656027029', precision=70) * dt**5
  253. + Float('0.083333333333333300951828', precision=70) * dt**4
  254. + Float('0.33333333333333337034077', precision=70) * dt**3
  255. + Float('1.0', precision=70) * dt**2
  256. + Float('1.0', precision=70) * dt
  257. + Float('1.0', precision=70)
  258. )
  259. def test_issue_11407():
  260. a, b, c, x = symbols('a b c x')
  261. assert series(sqrt(a + b + c*x), x, 0, 1) == sqrt(a + b) + O(x)
  262. assert series(sqrt(a + b + c + c*x), x, 0, 1) == sqrt(a + b + c) + O(x)
  263. def test_issue_14037():
  264. assert (sin(x**50)/x**51).series(x, n=0) == 1/x + O(1, x)
  265. def test_issue_20551():
  266. expr = (exp(x)/x).series(x, n=None)
  267. terms = [ next(expr) for i in range(3) ]
  268. assert terms == [1/x, 1, x/2]
  269. def test_issue_20697():
  270. p_0, p_1, p_2, p_3, b_0, b_1, b_2 = symbols('p_0 p_1 p_2 p_3 b_0 b_1 b_2')
  271. Q = (p_0 + (p_1 + (p_2 + p_3/y)/y)/y)/(1 + ((p_3/(b_0*y) + (b_0*p_2\
  272. - b_1*p_3)/b_0**2)/y + (b_0**2*p_1 - b_0*b_1*p_2 - p_3*(b_0*b_2\
  273. - b_1**2))/b_0**3)/y)
  274. assert Q.series(y, n=3).ratsimp() == b_2*y**2 + b_1*y + b_0 + O(y**3)
  275. def test_issue_21245():
  276. fi = (1 + sqrt(5))/2
  277. assert (1/(1 - x - x**2)).series(x, 1/fi, 1).factor() == \
  278. (-4812 - 2152*sqrt(5) + 1686*x + 754*sqrt(5)*x\
  279. + O((x - 2/(1 + sqrt(5)))**2, (x, 2/(1 + sqrt(5)))))/((1 + sqrt(5))\
  280. *(20 + 9*sqrt(5))**2*(x + sqrt(5)*x - 2))
  281. def test_issue_21938():
  282. expr = sin(1/x + exp(-x)) - sin(1/x)
  283. assert expr.series(x, oo) == (1/(24*x**4) - 1/(2*x**2) + 1 + O(x**(-6), (x, oo)))*exp(-x)
  284. def test_issue_23432():
  285. expr = 1/sqrt(1 - x**2)
  286. result = expr.series(x, 0.5)
  287. assert result.is_Add and len(result.args) == 7
  288. def test_issue_23727():
  289. res = series(sqrt(1 - x**2), x, 0.1)
  290. assert res.is_Add == True