The speed of integration of polynomials improved (10x).
[sympy.git] / sympy / polynomials / tests / test_polynomials.py
blobf5c52ed775c83a4102e9c5c21b6b060fa8bc95db
1 import py
2 from sympy.utilities.pytest import XFAIL
4 from sympy import Polynomial, pi, symbols, Symbol, Rational, S, sqf_part, sqf,\
5 solve_system, Integer, I, roots, cos, sin, resultant, \
6 count_real_roots, lcm, groebner, gcd, factor, div, Real, \
7 PolynomialException, sqrt
9 def test_Polynomial():
10 x = Symbol("x")
11 y = Symbol("y")
12 z = Symbol('z')
13 f = Polynomial(x+2)
14 g = Polynomial(y**2-1)
15 h = f + g
16 assert f.var == [x]
17 assert f.coeffs == ((1, 1), (2, 0))
18 assert str(f) == "2 + x"
19 assert repr(f) == "Polynomial(2 + x, ((1, 1), (2, 0)), [x], 'grevlex')"
20 assert f == 2 + x
21 assert f == Polynomial(None, f.coeffs, f.var, f.order)
22 assert f.nth_coeff(0) == 2
23 assert f.nth_coeff(2) == 0
24 assert h.var == [x, y]
25 assert h.coeffs == ((1, 0, 2), (1, 1, 0), (1, 0, 0))
26 h = f*Polynomial(y, var=x)
27 assert h.var == [x]
28 assert h.coeffs == ((y, 1), (2*y, 0))
29 h = f*y
30 assert h == (x + 2)*y
31 assert not isinstance(h, Polynomial)
32 h = Polynomial(h, var=y)
33 assert h.var == [y]
34 assert h.coeffs == ((2+x, 1),)
35 assert Polynomial(1, var=x).diff(x) == Polynomial(0, var=x)
36 assert Polynomial(x**3*y).diff(x) == Polynomial(3*x**2*y)
37 assert Polynomial(coeffs=((Integer(1), Integer(1)),), var=x).diff(x) \
38 == Polynomial(1, var=x)
39 assert Polynomial(x**2 + y**2)(3,-4) == 25
40 assert Polynomial(y*x)(-z, z) == -z**2
42 #TODO better test that differs between all orders ?
43 from sympy import sin
44 assert Polynomial(1).coeffs == ((1,),)
45 assert Polynomial(x).coeffs == ((1, 1),)
46 assert Polynomial(x**2+y**3, order='lex').coeffs \
47 == ((1, 2, 0), (1, 0, 3))
48 assert Polynomial(x**2+y**3, var=[y, x]).coeffs == ((1,3,0), (1,0,2))
49 assert Polynomial(x*y).coeffs == ((1, 1, 1),)
50 assert Polynomial(x**2*y**4 + sin(z)*x**3 + x*y**5,
51 var=[x, y], order='lex').coeffs \
52 == ((sin(z), 3, 0), (1, 2, 4), (1, 1, 5))
53 assert Polynomial(x**2*y**4 + sin(z)*x**3 + x*y**5,
54 var=[x, y], order='grlex').coeffs \
55 == ((1, 2, 4), (1, 1, 5), (sin(z), 3, 0))
56 assert Polynomial(x**2*y**4 + sin(z)*x**3 + x**5*y,
57 var=[x, y], order='grevlex').coeffs \
58 == ((1, 5, 1), (1, 2, 4), (sin(z), 3, 0))
59 assert Polynomial(z*x + x**2*y**2 + x**3*y,
60 var=[z, x, y], order='1-el').coeffs \
61 == ((1, 1, 1, 0), (1, 0, 3, 1), (1, 0, 2, 2))
63 py.test.raises(PolynomialException, "Polynomial(sqrt(x),var=x)")
64 py.test.raises(PolynomialException, "Polynomial(sin(x),var=x)")
66 assert 3*x**2 == Polynomial(coeffs=((Integer(3), Integer(2)),),
67 var=x).sympy_expr
68 assert 2*x + 3*x**2 - 5 \
69 == Polynomial(coeffs=((Integer(-5), Integer(0)),
70 (Integer(2), Integer(1)),
71 (Integer(3), Integer(2))),
72 var=[x]).sympy_expr
73 assert 2*x**100 + 3*x**2 - 5 \
74 == Polynomial(coeffs=((Integer(-5), Integer(0)),
75 (Integer(3), Integer(2)),
76 (Integer(2), Integer(100))),
77 var=[x]).sympy_expr
79 assert sqrt(y)*x == Polynomial(coeffs=((sqrt(y), Integer(1)),),
80 var=[x]).sympy_expr
82 def Polynomial1():
83 x, y = symbols()
84 p = Polynomial(x/3 + 12*y + x**2/8)
85 assert p.as_integer() == (24, Polynomial(3*x**2 + 8*x + 288*y))
86 assert p.as_monic() == (Rational(1,8), Polynomial(x**2 + 96*y + 8*x/3))
87 assert p.as_primitive() == (0, p)
88 p = Polynomial(100*x + 12*y + 8*x**2)
89 assert p.as_primitive() == (4, Polynomial(2*x**2 + 3*y + 25*x))
90 assert p.leading_coeff() == Rational(8)
91 assert p.leading_term() == Polynomial(8*x**2)
93 def test_coeff_ring():
94 from sympy.polynomials.base import coeff_ring
95 x = Symbol("x")
96 assert coeff_ring([Rational(2)]) == 'int'
97 assert coeff_ring([Rational(2), Rational(1,2)]) == 'rat'
98 assert coeff_ring([Rational(2)**Rational(1,2)]) == 'real'
99 assert coeff_ring([pi]) == 'real'
100 assert coeff_ring([Real(2.1), Rational(-1)**Rational(1,2)]) == 'cplx'
101 assert coeff_ring([I, x]) == 'sym'
104 ## sympy/modules/polynomials/wrapper.py
106 def test_div():
107 x = Symbol("x")
108 y = Symbol('y')
110 assert div(x**3-12*x**2-42, x-3, x) == (x**2-9*x-27, -123)
111 assert div(x**3-12*x**2-42, x**2+x-3, x) == (x-13, 16*x-81)
113 assert div(2+2*x+x**2, 1, x) == (2+2*x+x**2, 0)
114 assert div(2+2*x+x**2, 2, x, coeff='int') == (1+x, x**2)
116 assert div(3*x**3, x**2, x) == (3*x, 0)
118 assert div(1,1) == (1, 0)
119 assert div(1,x,x) == (0, 1)
120 assert div(x*y+2*x+y,x,x) == (2+y, y)
121 assert div(x*y+2*x+y,x,y) == (2+(1+1/x)*y, 0)
123 assert div(x*y**2 + 1, [x*y+1, y+1], [x,y]) == ([y, -1], 2)
124 assert div(x**2*y+x*y**2+y**2, [x*y-1, y**2-1], [x, y]) \
125 == ([x+y, 1], 1+x+y)
126 assert div(x**2*y+x*y**2+y**2, [y**2-1, x*y-1], [x, y]) \
127 == ([1+x, x], 1+2*x)
129 def test_factor():
130 x = Symbol("x")
131 y = Symbol("y")
132 z = Symbol("z")
133 assert factor(Rational(3, 8)*x**2 - Rational(3, 2)) \
134 == Rational(3, 8)*((x + 2)*(x - 2))
135 assert factor(x**3-1) == (x-1)*(x**2+x+1)
136 assert factor(x**2+2*x+1) == (x+1)**2
137 assert factor(x**3-3*x**2+3*x-1) == (x-1)**3
138 assert factor(x**2+x-2) == (x-1)*(x+2)
139 assert factor(x**3-x) == x*(x-1)*(x+1)
140 assert factor(x**6-1) == (1+x**2-x)*(1+x)*(1+x+x**2)*(-1+x)
141 assert factor(2*x**2+5*x+2) == (2+x)*(1+2*x)
143 #assert factor(x**2 + y**2) == x**2 + y**2
144 #assert factor(x*y + x*z + y*z) == x*y + x*z + y*z
145 #assert factor(x*(y+1) + x*z) == x*(z + y + 1)
146 #assert factor(x**5 - y**2) == x**5 - y**2
148 assert factor(-2) == -2
149 assert factor(-x) == -x
150 assert factor(-2*x**2+x) == x*(1 - 2*x)
152 def test_gcd():
153 x = Symbol("x")
154 y = Symbol("y")
155 z = Symbol("z")
157 assert gcd(x**2, x, x) == x
158 assert gcd(3*x**2, x, x) == x
159 assert gcd(3*x**2, 6*x, x, coeff='rat') == x
160 assert gcd(3*x**2, 6*x, x) == 3*x
161 assert gcd(x**2+2*x+1, x+1, x) == x+1
162 assert gcd(x**2+2*x+2, x+1, x) == 1
164 assert gcd(x**2+2*x+1, 2+2*x, x) == 1+x
165 assert gcd(x**2+2*x+2, 2+2*x, x) == 1
167 assert gcd(4, 6) == Rational(2)
168 assert gcd(6, 4, coeff='rat') == Rational(1)
169 assert gcd(x, y) == Rational(1)
170 assert gcd(sin(z)*(x+y), x**2+2*x*y+y**2, [x, y]) == x+y
172 def test_groebner():
173 x = Symbol('x')
174 y = Symbol('y')
175 z = Symbol('z')
177 assert groebner(y*x, x) == [x]
178 assert groebner(y*x, x, reduced=False) == [x*y]
179 assert groebner(x*y, z) == [1]
181 # This one already is a Groebner base.
182 assert groebner([y-x**2, z-x**3], [y,z,x], 'lex', reduced=False) \
183 == [-x**2+y, z-x**3]
185 assert groebner([x**3-2*x*y, x**2*y-2*y**2+x], [x,y], 'grlex',
186 reduced=False) \
187 == [x**3-2*x*y, x+x**2*y-2*y**2, -x**2, -2*x*y, -2*y**2+x]
188 assert groebner([x**3-2*x*y, x**2*y-2*y**2+x], [x,y], 'grlex') \
189 == [x**2, x*y, Rational(-1,2)*x+y**2]
191 def test_lcm():
192 x = Symbol('x')
193 y = Symbol('y')
195 assert lcm(6, 4) == Rational(12)
196 assert lcm(6, 4, coeff='rat') == Rational(1)
197 assert lcm(4, y) == 4*y
198 assert lcm(x, y) == x*y
199 assert lcm(y*(x+1), x, x) ==x+x**2
200 assert lcm(2*x, x**2) == 2*x**2
202 def test_count_real_roots():
203 x = Symbol('x')
205 f = x-1
206 assert count_real_roots(f) == 1
207 assert count_real_roots(f, None, Rational(0)) == 0
208 assert count_real_roots(f, Rational(0), Rational(1)) == 1
209 assert count_real_roots(f, Rational(1), None) == 0
210 f = x**2 - 4
211 assert count_real_roots(f) == 2
212 assert count_real_roots(f, None, Rational(0)) == 1
213 assert count_real_roots(f, Rational(-1), Rational(1)) == 0
215 def test_resultant():
216 x, a, b, c, = [Symbol(y) for y in ['x', 'a', 'b', 'c']]
218 s_res = resultant(x**2-1, x**3-x**2+2, x, method='sylvester').expand()
219 b_res = resultant(x**2-1, x**3-x**2+2, x, method='bezout').expand()
221 assert b_res == s_res == 0
223 s_res = resultant(3*x**3-x, 5*x**2+1, x, method='sylvester').expand()
224 b_res = resultant(3*x**3-x, 5*x**2+1, x, method='bezout').expand()
226 assert b_res == s_res == 64
228 s_res = resultant(x**2-2*x+7, x**3-x+5, x, method='sylvester').expand()
229 b_res = resultant(x**2-2*x+7, x**3-x+5, x, method='bezout').expand()
231 assert b_res == s_res == 265
233 s_res = resultant((x-a)**2-2, a**2-3, a, method='sylvester').expand()
234 b_res = resultant((x-a)**2-2, a**2-3, a, method='bezout').expand()
236 assert b_res == s_res == 1 - 10*x**2 + x**4
238 s_res = resultant((x-1)*(x-2)*(x-3), (x-4)*(x-5)*(x-6), x, method='sylvester').expand()
239 b_res = resultant((x-1)*(x-2)*(x-3), (x-4)*(x-5)*(x-6), x, method='bezout').expand()
241 assert b_res == s_res == -8640
243 s_res = resultant((x-1)*(x-2)*(x-3), (x-4)*(x-5)*(x-1), x, method='sylvester').expand()
244 b_res = resultant((x-1)*(x-2)*(x-3), (x-4)*(x-5)*(x-1), x, method='bezout').expand()
246 assert b_res == s_res == 0
248 s_res = resultant(x**3-1, x**3+2*x**2+2*x-1, x, method='sylvester').expand()
249 b_res = resultant(x**3-1, x**3+2*x**2+2*x-1, x, method='bezout').expand()
251 assert b_res == s_res == 16
253 s_res = resultant(x**8-2, x-1, x, method='sylvester').expand()
254 b_res = resultant(x**8-2, x-1, x, method='bezout').expand()
256 assert b_res == s_res == -1
258 s_res = resultant(3*x**2+2*a*x+3*a**2-2, 3*x**2-2*a*x+3*a**2-2, x, method='sylvester').expand()
259 b_res = resultant(3*x**2+2*a*x+3*a**2-2, 3*x**2-2*a*x+3*a**2-2, x, method='bezout').expand()
261 assert b_res == s_res == 144*a**4 - 96*a**2
263 s_res = resultant((x-a)*(x-b), x-c, x, method='sylvester').expand()
264 b_res = resultant((x-a)*(x-b), x-c, x, method='bezout').expand()
266 assert b_res == s_res == ((a-c)*(b-c)).expand()
268 def test_roots():
269 a = Symbol("a")
270 b = Symbol("b")
271 c = Symbol("c")
272 x = Symbol("x")
273 assert roots(x**2-3*x+2) == [1, 2]
274 assert roots(x**2-3*x/2+Rational(1,2)) == [Rational(1,2), 1]
275 assert roots(2*x**2-3*x+1) == [Rational(1,2), 1]
276 assert roots(x**2-1) == [1, -1]
277 assert roots(x**2+1) == [I, -I]
278 assert roots(x**3-1) == [1,
279 Rational(-1,2) + I*Rational(1,2)*3**Rational(1,2),
280 Rational(-1,2) - I*Rational(1,2)*3**Rational(1,2)]
281 assert roots(x**3) == [0, 0, 0]
282 assert roots(x**3-x) == [-1, 0, 1]
283 assert roots(Rational(2),x) == []
284 assert roots(a*x**2 + b*x + c, var=[x]) == \
285 [-b/(a*2)+(((b/a)**2-4*c/a)**Rational(1,2))/2,
286 -b/(a*2)-(((b/a)**2-4*c/a)**Rational(1,2))/2]
287 assert roots(x**3 + x**2 + x + 1) == [-1, I, -I]
288 assert roots(x**3 + x**2 - x + 1) == \
289 [Rational(-1,3) - (Rational(19,27) + Rational(1,9)*3**Rational(1,2)*11**Rational(1,2))**Rational(1,3) - Rational(4,9)*(Rational(19,27) + Rational(1,9)*3**Rational(1,2)*11**Rational(1,2))**Rational(-1,3),
290 Rational(-1,3) + Rational(1,2)*(Rational(19,27) + Rational(1,9)*3**Rational(1,2)*11**Rational(1,2))**Rational(1,3) + Rational(4,9)/(Rational(1,2) + Rational(1,2)*I*3**Rational(1,2))*(Rational(19,27) + Rational(1,9)*3**Rational(1,2)*11**Rational(1,2))**Rational(-1,3) + Rational(1,2)*I*3**Rational(1,2)*(Rational(19,27) + Rational(1,9)*3**Rational(1,2)*11**Rational(1,2))**Rational(1,3),
291 Rational(-1,3) + Rational(1,2)*(Rational(19,27) + Rational(1,9)*3**Rational(1,2)*11**Rational(1,2))**Rational(1,3) + Rational(4,9)/(Rational(1,2) - Rational(1,2)*I*3**Rational(1,2))*(Rational(19,27) + Rational(1,9)*3**Rational(1,2)*11**Rational(1,2))**Rational(-1,3) - Rational(1,2)*I*3**Rational(1,2)*(Rational(19,27) + Rational(1,9)*3**Rational(1,2)*11**Rational(1,2))**Rational(1,3)]
292 assert roots(x**4 - 1) == [1, I, -1, -I]
293 assert roots(x**4 + 1) == [((-1)**Rational(1,4)).expand(complex=True),
294 ((-1)**Rational(3,4)).expand(complex=True),
295 (-(-1)**Rational(1,4)).expand(complex=True),
296 (-(-1)**Rational(3,4)).expand(complex=True)]
297 assert roots(x**8 - 1) == [1, 2**Rational(1,2)/2 + I*2**Rational(1,2)/2,
298 I, -2**Rational(1,2)/2 + I*2**Rational(1,2)/2,
299 -1, -2**Rational(1,2)/2 - I*2**Rational(1,2)/2,
300 -I, 2**Rational(1,2)/2 - I*2**Rational(1,2)/2]
301 assert roots(x**5 - Rational(3,2)) == \
302 [Rational(1,2)**Rational(1,5)*3**Rational(1,5),
303 Rational(1,2)**Rational(1,5)*3**Rational(1,5)*cos(2*pi/5)
304 + I*Rational(1,2)**Rational(1,5)*3**Rational(1,5)*sin(2*pi/5),
305 Rational(1,2)**Rational(1,5)*3**Rational(1,5)*cos(4*pi/5)
306 + I*Rational(1,2)**Rational(1,5)*3**Rational(1,5)*sin(4*pi/5),
307 Rational(1,2)**Rational(1,5)*3**Rational(1,5)*cos(6*pi/5)
308 + I*Rational(1,2)**Rational(1,5)*3**Rational(1,5)*sin(6*pi/5),
309 Rational(1,2)**Rational(1,5)*3**Rational(1,5)*cos(8*pi/5)
310 + I*Rational(1,2)**Rational(1,5)*3**Rational(1,5)*sin(8*pi/5)]
312 def test_solve_system():
313 x = Symbol("x")
314 y = Symbol("y")
315 z = Symbol("z")
316 assert solve_system(x-1) == [(S.One,)]
317 assert solve_system([2*x - 3, 3*y/2 - 2*x, z - 5*y]) \
318 == [(Rational(3, 2), Integer(2), Integer(10))]
319 assert solve_system([y - x, y - x - 1]) == []
320 assert solve_system([y - x**2, y + x**2]) == [(S.Zero, S.Zero)]
321 assert solve_system([y - x**2, y + x**2 + 1]) == \
322 [(I*Rational(1,2)**Rational(1,2), Rational(-1,2)),
323 (-I*Rational(1,2)**Rational(1,2), Rational(-1,2))]
325 def test_sqf():
326 x = Symbol("x")
327 assert sqf(3*x**2, x) == 3*x**2
328 assert sqf(x**2+2*x+1, x) == (x+1)**2
329 assert sqf(x**5 - x**4 - x + 1) == (x-1)**2*(x**3 + x**2 + x + 1)
331 def test_sqf_part():
332 x = Symbol('x')
333 assert sqf_part(3*x**2, x) == 3*x
334 assert sqf_part(x**2 + 2*x + 1, x) == x+1
335 assert sqf_part(x**5 - x**4 - x + 1) == x**4 - 1
337 ## sympy/modules/polynomials/ideals.py
339 ## def test_Ideal():
340 ## x = Symbol('x')
341 ## y = Symbol('y')
342 ## z = Symbol('z')
344 ## # TODO: more complete tests?
345 ## assert len(Ideal()) == 1
346 ## assert not x in Ideal()
347 ## I = Ideal([x,y], [x,y])
348 ## assert x*y**2 in I
349 ## assert z*x in I
350 ## assert not z in I
351 ## assert I == I + Ideal()
352 ## assert Ideal() == Ideal()*I
353 ## assert I + I == I
354 ## assert I == I % Ideal()
355 ## assert Ideal() == Ideal(x*y, [x,y]) % I
356 ## assert Ideal(z, [x,y,z]) == Ideal([x,z], [x,y,z]) % Ideal([x,y], [x,y,z])
358 ## sympy/modules/polynomials/roots_.py
360 def test_sturm():
361 from sympy.polynomials import roots_
362 x = Symbol('x')
364 f = Polynomial(Rational(5), var=x)
365 assert roots_.sturm(f) == [f]
366 f = Polynomial(2*x)
367 assert roots_.sturm(f) == [f, Polynomial(2, var=x)]
368 f = Polynomial(x**3 - 2*x**2 + 3*x -5)
369 assert roots_.sturm(f) == \
370 [Polynomial(-5-2*x**2+x**3+3*x), Polynomial(3+3*x**2-4*x),
371 Polynomial(Rational(13,3)-Rational(10,9)*x),
372 Polynomial(Rational(-3303,100), var=x)]
375 ## Issues
377 def test_poly_content():
378 x, y = symbols("xy")
379 assert Polynomial(y**2*x + y, var=x).content() == y
380 assert Polynomial(y**2*x + 2, var=x).content() == 1
382 @XFAIL # see #442
383 def test_poly_content_0():
384 assert Polynomial(sin(y)*x, var=x).content() == sin(y)
386 @XFAIL # see #442
387 def test_poly_content_1():
388 assert Polynomial(sqrt(2)*x, var=x).content() == sqrt(2)
390 def test_poly_integrate():
391 x, y, z = symbols("xyz")
392 assert Polynomial(x**2+1).integrate(x) == x**3/3 + x
393 assert Polynomial(x**2+y*x).integrate(y) == x**2*y + y**2*x/2
394 assert Polynomial(x**2+x).integrate(y) == x**2*y + y*x
395 assert Polynomial(x*(y+x+z)).integrate(x) == x**2*y/2 + x**3/3 + x**2*z/2
396 assert Polynomial(x*(y+x+z)).integrate(z) == x*y*z + x**2*z + x*z**2/2