图片解析应用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

354 lines
14 KiB

  1. from sympy.core.evalf import N
  2. from sympy.core.function import (Derivative, Function, PoleError, Subs)
  3. from sympy.core.numbers import (E, Rational, oo, pi)
  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.integrals.integrals import Integral
  10. from sympy.series.order import O
  11. from sympy.series.series import series
  12. from sympy.abc import x, y, n, k
  13. from sympy.testing.pytest import raises
  14. from sympy.series.gruntz import calculate_series
  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_11313():
  72. assert Integral(cos(x), x).series(x) == sin(x).series(x)
  73. assert Derivative(sin(x), x).series(x, n=3).doit() == cos(x).series(x, n=3)
  74. assert Derivative(x**3, x).as_leading_term(x) == 3*x**2
  75. assert Derivative(x**3, y).as_leading_term(x) == 0
  76. assert Derivative(sin(x), x).as_leading_term(x) == 1
  77. assert Derivative(cos(x), x).as_leading_term(x) == -x
  78. # This result is equivalent to zero, zero is not return because
  79. # `Expr.series` doesn't currently detect an `x` in its `free_symbol`s.
  80. assert Derivative(1, x).as_leading_term(x) == Derivative(1, x)
  81. assert Derivative(exp(x), x).series(x).doit() == exp(x).series(x)
  82. assert 1 + Integral(exp(x), x).series(x) == exp(x).series(x)
  83. assert Derivative(log(x), x).series(x).doit() == (1/x).series(x)
  84. assert Integral(log(x), x).series(x) == Integral(log(x), x).doit().series(x).removeO()
  85. def test_series_of_Subs():
  86. from sympy.abc import z
  87. subs1 = Subs(sin(x), x, y)
  88. subs2 = Subs(sin(x) * cos(z), x, y)
  89. subs3 = Subs(sin(x * z), (x, z), (y, x))
  90. assert subs1.series(x) == subs1
  91. subs1_series = (Subs(x, x, y) + Subs(-x**3/6, x, y) +
  92. Subs(x**5/120, x, y) + O(y**6))
  93. assert subs1.series() == subs1_series
  94. assert subs1.series(y) == subs1_series
  95. assert subs1.series(z) == subs1
  96. assert subs2.series(z) == (Subs(z**4*sin(x)/24, x, y) +
  97. Subs(-z**2*sin(x)/2, x, y) + Subs(sin(x), x, y) + O(z**6))
  98. assert subs3.series(x).doit() == subs3.doit().series(x)
  99. assert subs3.series(z).doit() == sin(x*y)
  100. raises(ValueError, lambda: Subs(x + 2*y, y, z).series())
  101. assert Subs(x + y, y, z).series(x).doit() == x + z
  102. def test_issue_3978():
  103. f = Function('f')
  104. assert f(x).series(x, 0, 3, dir='-') == \
  105. f(0) + x*Subs(Derivative(f(x), x), x, 0) + \
  106. x**2*Subs(Derivative(f(x), x, x), x, 0)/2 + O(x**3)
  107. assert f(x).series(x, 0, 3) == \
  108. f(0) + x*Subs(Derivative(f(x), x), x, 0) + \
  109. x**2*Subs(Derivative(f(x), x, x), x, 0)/2 + O(x**3)
  110. assert f(x**2).series(x, 0, 3) == \
  111. f(0) + x**2*Subs(Derivative(f(x), x), x, 0) + O(x**3)
  112. assert f(x**2+1).series(x, 0, 3) == \
  113. f(1) + x**2*Subs(Derivative(f(x), x), x, 1) + O(x**3)
  114. class TestF(Function):
  115. pass
  116. assert TestF(x).series(x, 0, 3) == TestF(0) + \
  117. x*Subs(Derivative(TestF(x), x), x, 0) + \
  118. x**2*Subs(Derivative(TestF(x), x, x), x, 0)/2 + O(x**3)
  119. from sympy.series.acceleration import richardson, shanks
  120. from sympy.concrete.summations import Sum
  121. from sympy.core.numbers import Integer
  122. def test_acceleration():
  123. e = (1 + 1/n)**n
  124. assert round(richardson(e, n, 10, 20).evalf(), 10) == round(E.evalf(), 10)
  125. A = Sum(Integer(-1)**(k + 1) / k, (k, 1, n))
  126. assert round(shanks(A, n, 25).evalf(), 4) == round(log(2).evalf(), 4)
  127. assert round(shanks(A, n, 25, 5).evalf(), 10) == round(log(2).evalf(), 10)
  128. def test_issue_5852():
  129. assert series(1/cos(x/log(x)), x, 0) == 1 + x**2/(2*log(x)**2) + \
  130. 5*x**4/(24*log(x)**4) + O(x**6)
  131. def test_issue_4583():
  132. assert cos(1 + x + x**2).series(x, 0, 5) == cos(1) - x*sin(1) + \
  133. x**2*(-sin(1) - cos(1)/2) + x**3*(-cos(1) + sin(1)/6) + \
  134. x**4*(-11*cos(1)/24 + sin(1)/2) + O(x**5)
  135. def test_issue_6318():
  136. eq = (1/x)**Rational(2, 3)
  137. assert (eq + 1).as_leading_term(x) == eq
  138. def test_x_is_base_detection():
  139. eq = (x**2)**Rational(2, 3)
  140. assert eq.series() == x**Rational(4, 3)
  141. def test_sin_power():
  142. e = sin(x)**1.2
  143. assert calculate_series(e, x) == x**1.2
  144. def test_issue_7203():
  145. assert series(cos(x), x, pi, 3) == \
  146. -1 + (x - pi)**2/2 + O((x - pi)**3, (x, pi))
  147. def test_exp_product_positive_factors():
  148. a, b = symbols('a, b', positive=True)
  149. x = a * b
  150. assert series(exp(x), x, n=8) == 1 + a*b + a**2*b**2/2 + \
  151. a**3*b**3/6 + a**4*b**4/24 + a**5*b**5/120 + a**6*b**6/720 + \
  152. a**7*b**7/5040 + O(a**8*b**8, a, b)
  153. def test_issue_8805():
  154. assert series(1, n=8) == 1
  155. def test_issue_9549():
  156. y = (x**2 + x + 1) / (x**3 + x**2)
  157. assert series(y, x, oo) == x**(-5) - 1/x**4 + x**(-3) + 1/x + O(x**(-6), (x, oo))
  158. def test_issue_10761():
  159. assert series(1/(x**-2 + x**-3), x, 0) == x**3 - x**4 + x**5 + O(x**6)
  160. def test_issue_12578():
  161. y = (1 - 1/(x/2 - 1/(2*x))**4)**(S(1)/8)
  162. assert y.series(x, 0, n=17) == 1 - 2*x**4 - 8*x**6 - 34*x**8 - 152*x**10 - 714*x**12 - \
  163. 3472*x**14 - 17318*x**16 + O(x**17)
  164. def test_issue_12791():
  165. beta = symbols('beta', positive=True)
  166. theta, varphi = symbols('theta varphi', real=True)
  167. expr = (-beta**2*varphi*sin(theta) + beta**2*cos(theta) + \
  168. beta*varphi*sin(theta) - beta*cos(theta) - beta + 1)/(beta*cos(theta) - 1)**2
  169. sol = 0.5/(0.5*cos(theta) - 1.0)**2 - 0.25*cos(theta)/(0.5*cos(theta)\
  170. - 1.0)**2 + (beta - 0.5)*(-0.25*varphi*sin(2*theta) - 1.5*cos(theta)\
  171. + 0.25*cos(2*theta) + 1.25)/(0.5*cos(theta) - 1.0)**3\
  172. + 0.25*varphi*sin(theta)/(0.5*cos(theta) - 1.0)**2 + O((beta - S.Half)**2, (beta, S.Half))
  173. assert expr.series(beta, 0.5, 2).trigsimp() == sol
  174. def test_issue_14885():
  175. assert series(x**Rational(-3, 2)*exp(x), x, 0) == (x**Rational(-3, 2) + 1/sqrt(x) +
  176. sqrt(x)/2 + x**Rational(3, 2)/6 + x**Rational(5, 2)/24 + x**Rational(7, 2)/120 +
  177. x**Rational(9, 2)/720 + x**Rational(11, 2)/5040 + O(x**6))
  178. def test_issue_15539():
  179. assert series(atan(x), x, -oo) == (-1/(5*x**5) + 1/(3*x**3) - 1/x - pi/2
  180. + O(x**(-6), (x, -oo)))
  181. assert series(atan(x), x, oo) == (-1/(5*x**5) + 1/(3*x**3) - 1/x + pi/2
  182. + O(x**(-6), (x, oo)))
  183. def test_issue_7259():
  184. assert series(LambertW(x), x) == x - x**2 + 3*x**3/2 - 8*x**4/3 + 125*x**5/24 + O(x**6)
  185. assert series(LambertW(x**2), x, n=8) == x**2 - x**4 + 3*x**6/2 + O(x**8)
  186. assert series(LambertW(sin(x)), x, n=4) == x - x**2 + 4*x**3/3 + O(x**4)
  187. def test_issue_11884():
  188. assert cos(x).series(x, 1, n=1) == cos(1) + O(x - 1, (x, 1))
  189. def test_issue_18008():
  190. y = x*(1 + x*(1 - x))/((1 + x*(1 - x)) - (1 - x)*(1 - x))
  191. assert y.series(x, oo, n=4) == -9/(32*x**3) - 3/(16*x**2) - 1/(8*x) + S(1)/4 + x/2 + \
  192. O(x**(-4), (x, oo))
  193. def test_issue_18842():
  194. f = log(x/(1 - x))
  195. assert f.series(x, 0.491, n=1).removeO().nsimplify() == \
  196. -S(180019443780011)/5000000000000000
  197. def test_issue_19534():
  198. dt = symbols('dt', real=True)
  199. expr = 16*dt*(0.125*dt*(2.0*dt + 1.0) + 0.875*dt + 1.0)/45 + \
  200. 49*dt*(-0.049335189898860408029*dt*(2.0*dt + 1.0) + \
  201. 0.29601113939316244817*dt*(0.125*dt*(2.0*dt + 1.0) + 0.875*dt + 1.0) - \
  202. 0.12564355335492979587*dt*(0.074074074074074074074*dt*(2.0*dt + 1.0) + \
  203. 0.2962962962962962963*dt*(0.125*dt*(2.0*dt + 1.0) + 0.875*dt + 1.0) + \
  204. 0.96296296296296296296*dt + 1.0) + 0.051640768506639183825*dt + \
  205. dt*(1/2 - sqrt(21)/14) + 1.0)/180 + 49*dt*(-0.23637909581542530626*dt*(2.0*dt + 1.0) - \
  206. 0.74817562366625959291*dt*(0.125*dt*(2.0*dt + 1.0) + 0.875*dt + 1.0) + \
  207. 0.88085458023927036857*dt*(0.074074074074074074074*dt*(2.0*dt + 1.0) + \
  208. 0.2962962962962962963*dt*(0.125*dt*(2.0*dt + 1.0) + 0.875*dt + 1.0) + \
  209. 0.96296296296296296296*dt + 1.0) + \
  210. 2.1165151389911680013*dt*(-0.049335189898860408029*dt*(2.0*dt + 1.0) + \
  211. 0.29601113939316244817*dt*(0.125*dt*(2.0*dt + 1.0) + 0.875*dt + 1.0) - \
  212. 0.12564355335492979587*dt*(0.074074074074074074074*dt*(2.0*dt + 1.0) + \
  213. 0.2962962962962962963*dt*(0.125*dt*(2.0*dt + 1.0) + 0.875*dt + 1.0) + \
  214. 0.96296296296296296296*dt + 1.0) + 0.22431393315265061193*dt + 1.0) - \
  215. 1.1854881643947648988*dt + dt*(sqrt(21)/14 + 1/2) + 1.0)/180 + \
  216. dt*(0.66666666666666666667*dt*(2.0*dt + 1.0) + \
  217. 6.0173399699313066769*dt*(0.125*dt*(2.0*dt + 1.0) + 0.875*dt + 1.0) - \
  218. 4.1117044797036320069*dt*(0.074074074074074074074*dt*(2.0*dt + 1.0) + \
  219. 0.2962962962962962963*dt*(0.125*dt*(2.0*dt + 1.0) + 0.875*dt + 1.0) + \
  220. 0.96296296296296296296*dt + 1.0) - \
  221. 7.0189140975801991157*dt*(-0.049335189898860408029*dt*(2.0*dt + 1.0) + \
  222. 0.29601113939316244817*dt*(0.125*dt*(2.0*dt + 1.0) + 0.875*dt + 1.0) - \
  223. 0.12564355335492979587*dt*(0.074074074074074074074*dt*(2.0*dt + 1.0) + \
  224. 0.2962962962962962963*dt*(0.125*dt*(2.0*dt + 1.0) + 0.875*dt + 1.0) + \
  225. 0.96296296296296296296*dt + 1.0) + 0.22431393315265061193*dt + 1.0) + \
  226. 0.94010945196161777522*dt*(-0.23637909581542530626*dt*(2.0*dt + 1.0) - \
  227. 0.74817562366625959291*dt*(0.125*dt*(2.0*dt + 1.0) + 0.875*dt + 1.0) + \
  228. 0.88085458023927036857*dt*(0.074074074074074074074*dt*(2.0*dt + 1.0) + \
  229. 0.2962962962962962963*dt*(0.125*dt*(2.0*dt + 1.0) + 0.875*dt + 1.0) + \
  230. 0.96296296296296296296*dt + 1.0) + \
  231. 2.1165151389911680013*dt*(-0.049335189898860408029*dt*(2.0*dt + 1.0) + \
  232. 0.29601113939316244817*dt*(0.125*dt*(2.0*dt + 1.0) + 0.875*dt + 1.0) - \
  233. 0.12564355335492979587*dt*(0.074074074074074074074*dt*(2.0*dt + 1.0) + \
  234. 0.2962962962962962963*dt*(0.125*dt*(2.0*dt + 1.0) + 0.875*dt + 1.0) + \
  235. 0.96296296296296296296*dt + 1.0) + 0.22431393315265061193*dt + 1.0) - \
  236. 0.35816132904077632692*dt + 1.0) + 5.5065024887242400038*dt + 1.0)/20 + dt/20 + 1
  237. assert N(expr.series(dt, 0, 8), 20) == -0.00092592592592592596126*dt**7 + 0.0027777777777777783175*dt**6 + \
  238. 0.016666666666666656027*dt**5 + 0.083333333333333300952*dt**4 + 0.33333333333333337034*dt**3 + \
  239. 1.0*dt**2 + 1.0*dt + 1.0
  240. def test_issue_11407():
  241. a, b, c, x = symbols('a b c x')
  242. assert series(sqrt(a + b + c*x), x, 0, 1) == sqrt(a + b) + O(x)
  243. assert series(sqrt(a + b + c + c*x), x, 0, 1) == sqrt(a + b + c) + O(x)
  244. def test_issue_14037():
  245. assert (sin(x**50)/x**51).series(x, n=0) == 1/x + O(1, x)
  246. def test_issue_20551():
  247. expr = (exp(x)/x).series(x, n=None)
  248. terms = [ next(expr) for i in range(3) ]
  249. assert terms == [1/x, 1, x/2]
  250. def test_issue_20697():
  251. 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')
  252. Q = (p_0 + (p_1 + (p_2 + p_3/y)/y)/y)/(1 + ((p_3/(b_0*y) + (b_0*p_2\
  253. - 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\
  254. - b_1**2))/b_0**3)/y)
  255. assert Q.series(y, n=3).ratsimp() == b_2*y**2 + b_1*y + b_0 + O(y**3)
  256. def test_issue_21245():
  257. fi = (1 + sqrt(5))/2
  258. assert (1/(1 - x - x**2)).series(x, 1/fi, 1).factor() == \
  259. (-4812 - 2152*sqrt(5) + 1686*x + 754*sqrt(5)*x\
  260. + O((x - 2/(1 + sqrt(5)))**2, (x, 2/(1 + sqrt(5)))))/((1 + sqrt(5))\
  261. *(20 + 9*sqrt(5))**2*(x + sqrt(5)*x - 2))
  262. def test_issue_21938():
  263. expr = sin(1/x + exp(-x)) - sin(1/x)
  264. assert expr.series(x, oo) == (1/(24*x**4) - 1/(2*x**2) + 1 + O(x**(-6), (x, oo)))*exp(-x)