1 from sympy
import Symbol
, Rational
, ln
, exp
, log
, sqrt
, E
, O
, pi
, I
, sinh
, \
2 sin
, cosh
, cos
, tanh
, coth
, asinh
, acosh
, atanh
, acoth
, tan
, Integer
, \
3 PoleError
, floor
, ceiling
4 from sympy
.abc
import x
, y
, z
5 from sympy
.utilities
.pytest
import XFAIL
9 assert x
.nseries(x
, 0, 5) == x
10 assert y
.nseries(x
, 0, 5) == y
11 assert (1/(x
*y
)).nseries(y
, 0, 5) == 1/(x
*y
)
12 assert Rational(3,4).nseries(x
, 0, 5) == Rational(3,4)
15 assert (x
*ln(x
)).nseries(x
, 0, 5) == x
*ln(x
)
18 assert (x
*ln(2+x
)).nseries(x
, 0, 4) == x
*log(2)+x
**2/2-x
**3/8+x
**4/24+ \
20 assert (x
*ln(1+x
)).nseries(x
, 0, 4) == x
**2- x
**3/2 + x
**4/3 + O(x
**5)
23 assert (x
**2).nseries(x
, 0, 5) == x
**2
24 assert (1/x
).nseries(x
, 0, 5) == 1/x
25 assert (1/x
**2).nseries(x
, 0, 5) == 1/x
**2
26 assert (x
**(Rational(2,3))).nseries(x
, 0, 5) == (x
**(Rational(2,3)))
27 assert (x
**(Rational(3,2))).nseries(x
, 0, 5) == (x
**(Rational(3,2)))
30 assert ((1+x
)**2).nseries(x
, 0, 5) == 1+2*x
+x
**2
32 def test_geometric_1():
33 assert (1/(1-x
)).nseries(x
, 0, 5) == 1+x
+x
**2+x
**3+x
**4+O(x
**5)
34 assert (x
/(1-x
)).nseries(x
, 0, 5) == x
+x
**2+x
**3+x
**4+x
**5+O(x
**6)
35 assert (x
**3/(1-x
)).nseries(x
, 0, 5) == x
**3+x
**4+x
**5+x
**6+x
**7+O(x
**8)
38 assert sqrt(1+x
).nseries(x
, 0, 5) == 1+x
/2-x
**2/8+x
**3/16-5*x
**4/128+O(x
**5)
41 assert exp(x
).nseries(x
, 0, 5) == 1+x
+x
**2/2+x
**3/6+x
**4/24 + O(x
**5)
42 assert exp(x
).nseries(x
, 0, 12) == 1+x
+x
**2/2+x
**3/6+x
**4/24+x
**5/120+ \
43 x
**6/720+x
**7/5040+x
**8/40320+x
**9/362880+x
**10/3628800+ \
44 x
**11/39916800 + O(x
**12)
45 assert exp(1/x
).nseries(x
, 0, 5) == exp(1/x
)
46 assert exp(1/(1+x
)).nseries(x
, 0, 4) == \
47 (E
*(1-x
-13*x
**3/6+3*x
**2/2)).expand() + O(x
**4)
48 assert exp(2+x
).nseries(x
, 0, 5) == \
49 (exp(2)*(1+x
+x
**2/2+x
**3/6+x
**4/24)).expand() + O(x
**5)
51 def test_exp_sqrt_1():
52 assert exp(1+sqrt(x
)).nseries(x
, 0, 3) == \
53 (exp(1)*(1+sqrt(x
)+x
/2+sqrt(x
)*x
/6)).expand() + O(sqrt(x
)**3)
55 def test_power_x_x1():
56 assert (exp(x
*ln(x
))).nseries(x
, 0, 4) == \
57 1+x
*log(x
)+x
**2*log(x
)**2/2+x
**3*log(x
)**3/6 + O(x
**4*log(x
)**4)
59 def test_power_x_x2():
60 assert (x
**x
).nseries(x
, 0, 4) == \
61 1+x
*log(x
)+x
**2*log(x
)**2/2+x
**3*log(x
)**3/6 + O(x
**4*log(x
)**4)
63 def test_log_singular1():
64 assert log(1+1/x
).nseries(x
, 0, 5) == x
- log(x
) - x
**2/2 + x
**3/3 - \
67 def test_log_power1():
68 e
= 1 / (1/x
+ x
** (log(3)/log(2)))
69 assert e
.nseries(x
, 0, 5) == x
- x
**(2 + log(3)/log(2)) + O(x
**5)
71 def test_log_series():
73 assert e
.nseries(x
, 0, 5) == -1/log(x
) - log(x
)**(-2) - log(x
)**(-3) - \
74 log(x
)**(-4) + O(log(x
)**(-5))
78 assert e
.nseries(x
, 0, 5) == -log(x
) + log(-1)
82 assert e
.nseries(x
, 0, 4) == -1/log(x
) - pi
*I
*log(x
)**(-2) + \
83 pi
**2*log(x
)**(-3) + O(log(x
)**(-4))
88 assert e
.nseries(x
,0,0) != 0
89 assert e
.nseries(x
,0,0) == O(1, x
)
90 assert e
.nseries(x
,0,1) == O(x
, x
)
91 assert e
.nseries(x
,0,2) == x
+ O(x
**2, x
)
92 assert e
.nseries(x
,0,3) == x
+ O(x
**3, x
)
93 assert e
.nseries(x
,0,4) == x
-x
**3/6 + O(x
**4, x
)
96 assert e
.nseries(x
,0,3) == 1+x
/2+O(x
**2, x
)
98 #assert x.nseries(x,0,0) == O(1, x)
99 #assert x.nseries(x,0,1) == O(x, x)
100 assert x
.nseries(x
,0,2) == x
102 def testseriesbug1():
104 assert (1/x
).nseries(x
,0,3) == 1/x
105 assert (x
+1/x
).nseries(x
,0,3) == x
+1/x
109 assert ((x
+1)**(-2)).nseries(x
,0,4) == 1-2*x
+3*x
**2-4*x
**3+O(x
**4, x
)
110 assert ((x
+1)**(-1)).nseries(x
,0,4) == 1-x
+x
**2-x
**3+O(x
**4, x
)
111 assert ((x
+1)**0).nseries(x
,0,3) == 1
112 assert ((x
+1)**1).nseries(x
,0,3) == 1+x
113 assert ((x
+1)**2).nseries(x
,0,3) == 1+2*x
+x
**2
114 assert ((x
+1)**3).nseries(x
,0,3) == 1+3*x
+3*x
**2+x
**3
116 assert (1/(1+x
)).nseries(x
,0,4) == 1-x
+x
**2-x
**3+O(x
**4, x
)
117 assert (x
+3/(1+2*x
)).nseries(x
,0,4) == 3-5*x
+12*x
**2-24*x
**3+O(x
**4, x
)
119 assert ((1/x
+1)**3).nseries(x
,0,3)== 1+x
**(-3)+3*x
**(-2)+3/x
120 assert (1/(1+1/x
)).nseries(x
,0,4) == x
-x
**2+x
**3-O(x
**4, x
)
121 assert (1/(1+1/x
**2)).nseries(x
,0,6) == x
**2-x
**4+O(x
**6, x
)
123 def test_bug2(): ### 1/log(0) * log(0) problem
125 e
= (w
**(-1)+w
**(-log(3)*log(2)**(-1)))**(-1)*(3*w
**(-log(3)*log(2)**(-1))+2*w
**(-1))
127 #should be 3, but is 2
128 assert e
.nseries(w
,0,4).subs(w
,0)==3
133 assert e
.nseries(x
, 0, 3) == exp(1) - x
*exp(1)/2 + O(x
**2, x
)
138 e
= w
**(1-log(x
)/(log(2) + log(x
)))
139 assert e
.nseries(w
,0,1) == e
143 e
= (2/x
+3/x
**2)/(1/x
+1/x
**2)
144 assert e
.nseries(x
, 0, 3) == 3 + O(x
)
146 def test_generalexponent():
149 e
= (2/x
+3/x
**p
)/(1/x
+1/x
**p
)
150 assert e
.nseries(x
,0,3) == 3 + O(x
)
152 e
= (2/x
+3/x
**p
)/(1/x
+1/x
**p
)
153 assert e
.nseries(x
,0,2) == 2 + sqrt(x
) + O(x
)
156 assert e
.nseries(x
,0,4) == 1+x
**Rational(1,2)
158 # more complicated example
161 e
=1/(1+x
**Rational(1,2))
162 assert e
.nseries(x
,0,2) == \
163 1+x
-x
**Rational(1,2)-x
**Rational(3,2)+O(x
**2, x
)
165 # more complicated example
166 def test_genexp_x2():
169 e
= (2/x
+3/x
**p
)/(1/x
+1/x
**p
)
170 assert e
.nseries(x
,0,3) == 3 - sqrt(x
) + x
+ O(sqrt(x
)**3)
172 def test_seriesbug2():
176 assert e
.nseries(w
,0,1) == 2 + O(w
, w
)
177 assert e
.nseries(w
,0,1).subs(w
,0) == 2
179 def test_seriesbug2b():
183 assert e
.nseries(w
,0,3) == 2 + O(w
**2, w
)
185 def test_seriesbug2d():
186 w
= Symbol("w", real
=True)
188 assert e
.nseries(w
, 0, 5) == log(2) - 2*w
**2/3 - 4*w
**4/45 + O(w
**5)
190 def test_seriesbug2c():
191 w
= Symbol("w", real
=True)
192 #more complicated case, but sin(x)~x, so the result is the same as in (1)
193 e
=(sin(2*w
)/w
)**(1+w
)
194 assert e
.nseries(w
,0,1) == 2 + O(w
)
195 assert e
.nseries(w
,0,3) == 2-Rational(4,3)*w
**2+w
**2*log(2)**2+2*w
*log(2)+O(w
**3, w
)
196 assert e
.nseries(w
,0,2).subs(w
,0) == 2
199 x
= Symbol("x", real
=True)
200 assert (log(sin(2*x
)/x
)*(1+x
)).nseries(x
,0,2) == log(2) + x
*log(2) + O(x
**2, x
)
201 #assert exp(log(2)+O(x)).nseries(x,0,2) == 2 +O(x**2, x)
202 assert exp(log(sin(2*x
)/x
)*(1+x
)).nseries(x
,0,2) == 2 + 2*x
*log(2) + O(x
**2)
203 #assert ((2+O(x))**(1+x)).nseries(x,0,2) == 2 + O(x**2, x)
207 assert log(2+O(x
)).nseries(x
,0,2) == log(2) + O(x
, x
)
211 #assert exp(O(x)).nseries(x,0,2) == 1 + O(x**2, x)
212 assert exp(log(1+x
)/x
).nseries(x
, 0, 3) == exp(1) + -exp(1)*x
/2 + O(x
**2)
214 def test_sinsinbug():
216 assert sin(sin(x
)).nseries(x
,0,8) == x
-x
**3/3+x
**5/10-8*x
**7/315+O(x
**8)
221 assert a
.nseries(x
,0,6) == 1 - x
/2 - x
**4/720 + x
**2/12 + O(x
**5)
225 f
= sin(x
**3)**Rational(1,3)
226 assert f
.nseries(x
,0,17) == x
- x
**7/18 - x
**13/3240 + O(x
**17)
230 f
=(1-y
**(Rational(1)/2))**(Rational(1)/2)
231 assert f
.nseries(y
,0,2) == 1 - sqrt(y
)/2-y
/8-y
**Rational(3,2)/16+O(y
**2)
236 e
= 1/x
*(-log(w
**(1 + 1/log(3)*log(5))) + log(w
+ w
**(1/log(3)*log(5))))
237 e_ser
= -log(5)*log(w
)/(x
*log(3)) + w
**(log(5)/log(3) - 1)/x
- \
238 w
**(2*log(5)/log(3) - 2)/(2*x
) + O(w
)
239 assert e
.nseries(w
, 0, 1) == e_ser
244 assert sin(8*x
).nseries(x
, 0, 4) == 8*x
- 256*x
**3/3 + O(x
**4)
245 assert sin(x
+y
).nseries(x
, 0, 1) == sin(y
) + O(x
)
246 assert sin(x
+y
).nseries(x
, 0, 2) == sin(y
) + cos(y
)*x
+ O(x
**2)
247 assert sin(x
+y
).nseries(x
, 0, 5) == sin(y
) + cos(y
)*x
- sin(y
)*x
**2/2 - \
248 cos(y
)*x
**3/6 + sin(y
)*x
**4/24 + O(x
**5)
253 assert e
.nseries(x
, 0, 6) == 8 - 256*x
**2/3 + 4096*x
**4/15 + O(x
**5)
257 e
= sin(x
)**(-4)*(cos(x
)**Rational(1,2)*sin(x
)**2 - \
258 cos(x
)**Rational(1,3)*sin(x
)**2)
259 assert e
.nseries(x
, 0, 8) == -Rational(1)/12 - 7*x
**2/288 - \
260 43*x
**4/10368 + O(x
**5)
265 e
= x
**(-2)*(x
*sin(a
+ x
) - x
*sin(a
))
266 assert e
.nseries(x
, 0, 5) == cos(a
) - sin(a
)*x
/2 - cos(a
)*x
**2/6 + \
267 sin(a
)*x
**3/24 + O(x
**4)
268 e
= x
**(-2)*(x
*cos(a
+ x
) - x
*cos(a
))
269 assert e
.nseries(x
, 0, 5) == -sin(a
) - cos(a
)*x
/2 + sin(a
)*x
**2/6 + \
270 cos(a
)*x
**3/24 + O(x
**4)
274 e
= sin(5*x
)/sin(2*x
)
275 assert e
.nseries(x
, 0, 2) == Rational(5,2) + O(x
)
276 assert e
.nseries(x
, 0, 6) == Rational(5,2) - 35*x
**2/4 + 329*x
**4/48 + O(x
**5)
280 e
= sin(2 + x
)/(2 + x
)
281 assert e
.nseries(x
, 0, 2) == sin(2)/2 + x
*cos(2)/2 - x
*sin(2)/4 + O(x
**2)
285 e
= (x
+ sin(3*x
))**(-2)*(x
*(x
+ sin(3*x
)) - (x
+ sin(3*x
))*sin(2*x
))
286 assert e
.nseries(x
, 0, 6) == -Rational(1,4) + 5*x
**2/96 + 91*x
**4/768 + O(x
**5)
289 x
= Symbol("x", real
=True)
290 assert log(sin(x
)).nseries(x
, 0, 5) == log(x
) - x
**2/6 - x
**4/180 + O(x
**5)
291 e
= -log(x
) + x
*(-log(x
) + log(sin(2*x
))) + log(sin(2*x
))
292 assert e
.nseries(x
, 0, 5) == log(2)+log(2)*x
-2*x
**2/3-2*x
**3/3-4*x
**4/45+O(x
**5)
296 e
= x
**(-4)*(x
**2 - x
**2*cos(x
)**Rational(1,2))
297 assert e
.nseries(x
, 0, 7) == Rational(1,4) + x
**2/96 + 19*x
**4/5760 + O(x
**5)
301 assert sin(cos(x
)).nseries(x
, 0, 5) == sin(1) -x
**2*cos(1)/2 - x
**4*sin(1)/8 + x
**4*cos(1)/24 + O(x
**5)
303 def test_hyperbolic():
305 assert sinh(x
).nseries(x
, 0, 6) == x
+ x
**3/6 + x
**5/120 + O(x
**6)
306 assert cosh(x
).nseries(x
, 0, 5) == 1 + x
**2/2 + x
**4/24 + O(x
**5)
307 assert tanh(x
).nseries(x
, 0, 6) == x
- x
**3/3 + 2*x
**5/15 + O(x
**6)
308 assert coth(x
).nseries(x
, 0, 6) == 1/x
- x
**3/45 + x
/3 + 2*x
**5/945 + O(x
**6)
309 assert asinh(x
).nseries(x
, 0, 6) == x
- x
**3/6 + 3*x
**5/40 + O(x
**6)
310 assert acosh(x
).nseries(x
, 0, 6) == pi
*I
/2 - I
*x
- 3*I
*x
**5/40 - I
*x
**3/6 + O(x
**6)
311 assert atanh(x
).nseries(x
, 0, 6) == x
+ x
**3/3 + x
**5/5 + O(x
**6)
312 assert acoth(x
).nseries(x
, 0, 6) == x
+ x
**3/3 + x
**5/5 + pi
*I
/2 + O(x
**6)
315 w
= Symbol("w", real
=True)
316 x
= Symbol("x", real
=True)
317 e
= w
**(-2)*(w
*exp(1/x
- w
) - w
*exp(1/x
))
318 assert e
.nseries(w
, 0, 3) == -exp(1/x
) + w
* exp(1/x
) / 2 + O(w
**2)
321 w
= Symbol("w", real
=True)
322 x
= Symbol("x", real
=True)
323 e
= w
**(-6)*(w
**3*tan(w
) - w
**3*sin(w
))
324 assert e
.nseries(w
, 0, 5) == Integer(1)/2 + O(w
**2)
329 e
= x
/(w
**4 + x
**2*w
**4 + 2*x
*w
**4)*w
**4
330 assert e
.nseries(w
, 0, 2) in [x
/(1 + 2*x
+ x
**2), 1/(1+x
/2+1/x
/2)/2, 1/x
/(1 + 2/x
+ x
**(-2))]
335 e
= (-log(w
) + log(1 + w
*log(x
)))**(-2)*w
**(-2)*((-log(w
) + log(1 + \
336 x
*w
))*(-log(w
) + log(1 + w
*log(x
)))*w
- x
*(-log(w
) + log(1 + \
338 assert e
.nseries(w
, 0, 1) == x
/w
/log(w
) + 1/w
+ O(1/log(w
))
339 assert e
.nseries(w
, 0, 2) == x
/w
/log(w
) + 1/w
- x
/log(w
) + 1/log(w
)*log(x
)\
340 + x
*log(x
)/log(w
)**2 + O(w
/log(w
))
343 def test_issue1016():
345 assert ( sin(x
)/(1 - cos(x
)) ).nseries(x
, 0, 2) == O(1/x
)
346 assert ( sin(x
)**2/(1 - cos(x
)) ).nseries(x
, 0, 2) == O(1, x
)
350 py
.test
.raises(PoleError
, "sin(1/x).series(x, 0, 5)")
351 py
.test
.raises(PoleError
, "sin(1+1/x).series(x, 0, 5)")
352 py
.test
.raises(PoleError
, "(x*sin(1/x)).series(x, 0, 5)")
354 def test_expsinbug():
356 assert exp(sin(x
)).series(x
, 0, 0) == O(1, x
)
357 assert exp(sin(x
)).series(x
, 0, 1) == 1+O(x
)
358 assert exp(sin(x
)).series(x
, 0, 2) == 1+x
+O(x
**2)
359 assert exp(sin(x
)).series(x
, 0, 3) == 1+x
+x
**2/2+O(x
**3)
360 assert exp(sin(x
)).series(x
, 0, 4) == 1+x
+x
**2/2+O(x
**4)
361 assert exp(sin(x
)).series(x
, 0, 5) == 1+x
+x
**2/2-x
**4/8+O(x
**5)
365 assert floor(x
).series(x
) == 0
366 assert floor(-x
).series(x
) == -1
367 assert floor(sin(x
)).series(x
) == 0
368 assert floor(sin(-x
)).series(x
) == -1
369 assert floor(x
**3).series(x
) == 0
370 assert floor(-x
**3).series(x
) == -1
371 assert floor(cos(x
)).series(x
) == 0
372 assert floor(cos(-x
)).series(x
) == 0
373 assert floor(5+sin(x
)).series(x
) == 5
374 assert floor(5+sin(-x
)).series(x
) == 4
376 assert floor(x
).series(x
, 2) == 2
377 assert floor(-x
).series(x
, 2) == -3
379 x
= Symbol('x', negative
=True)
380 assert floor(x
+1.5).series(x
) == 1
384 assert ceiling(x
).series(x
) == 1
385 assert ceiling(-x
).series(x
) == 0
386 assert ceiling(sin(x
)).series(x
) == 1
387 assert ceiling(sin(-x
)).series(x
) == 0
388 assert ceiling(1-cos(x
)).series(x
) == 1
389 assert ceiling(1-cos(-x
)).series(x
) == 1
390 assert ceiling(x
).series(x
, 2) == 3
391 assert ceiling(-x
).series(x
, 2) == -2
395 assert abs(x
).nseries(x
, 0, 4) == x
396 assert abs(-x
).nseries(x
, 0, 4) == x
397 assert abs(x
+1).nseries(x
, 0, 4) == x
+1
398 assert abs(sin(x
)).nseries(x
, 0, 4) == x
- Rational(1, 6)*x
**3 + O(x
**4)
399 assert abs(sin(-x
)).nseries(x
, 0, 4) == x
- Rational(1, 6)*x
**3 + O(x
**4)