Polynomials Module
From SymPy
Contents |
Introduction
This tutorial tries to give an overview of the functionality concerning polynomials within SymPy. All code examples assume:
<source lang="python"> >>> from sympy import * >>> x, y, z = symbols('xyz') </source>
Basic functionality
These functions provide different algorithms dealing with polynomials in the form of SymPy expression, like symbols, sums etc.
Division
The function div() provides division of polynomials with remainder. That is, for polynomials f and g, it computes q and r, such that f == g*q + r and degree(r) < q. For polynomials in one variables with coefficients in a field, say, the rational numbers, q and r are uniquely defined this way.
<source lang="python"> >>> f = 5*x**2 + 10*x + 3 >>> g = 2*x + 2 >>> q, r = div(f, g) >>> q 5 + (5/2)*x >>> r 3 >>> (q*g + r).expand() 13 + 5*x**2 + 15*x </source>
As you can see, q has a non-integer coefficient. If you want to do division only in the ring of polynomials with integer coefficients you can specify an additional parameter.
<source lang="python"> >>> q, r = div(f, g, coeff='int') >>> q 0 >>> r 13 + 5*x**2 + 15*x </source>
But be warned, that this ring is no longer euclidean and that the degree of the remainder doesn't need to be smaller than that of f. Since 2 doesn't divide 5, 2*x doesn't divide 5*x**2, even if the degree is smaller. But:
<source lang="python"> >>> g = 5*x + 1 >>> q, r = div(f, g, coeff='int') >>> q x >>> r 13 + 14*x >>> (q*g + r).expand() 13 + 5*x**2 + 15*x </source>
This also works for polynomials with multiple variables.
<source lang="python"> >>> f = x*y + y*z >>> g = 3*x + 3*z >>> q, r = div(f, g) >>> q (1/3)*y >>> r 0 </source>
In the last examples, all of the three variables x, y and z are assumed to be variables of the polynomials. But if you have some unrelated constant as coefficient, you can specify the variables explicitly:
<source lang="python"> >>> a, b, c = symbols('abc') >>> f = a*x**2 + b*x + c >>> g = 3*x + 2 >>> q, r = div(f, g, var=[x]) >>> q -2/9*a + (1/3)*b + (1/3)*a*x >>> r c - 2/3*b + (4/9)*a </source>
Another option is division by multiple polynomials at the same time. In general, the output is not unique and depends on the order of the divisors and the given monomial order (if specified).
<source lang="python"> >>> f = x*y + y*z + z*x >>> g1 = x + 1 >>> g2 = 2*y + 1 >>> q, r = div(f, [g1, g2]) >>> q [y + z, (-1/2) + (1/2)*z] >>> r 1/2 - 3/2*z >>> (q[0]*g1 + q[1]*g2 + r).expand() x*y + x*z + y*z </source>
<source lang="python"> >>> q, r = div(f, [g2, g1]) >>> q [(1/2)*x + (1/2)*z, (-1/2) + z] >>> r 1/2 - 3/2*z >>> (q[0]*g2 + q[1]*g1 + r).expand() x*y + x*z + y*z </source>
GCD and LCM
With division, there is also the computation of the greatest common divisor and the least common multiple.
When the polynomials have integer coefficients, the contents' gcd is also considered.
<source lang="python"> >>> f = 12*(x + 1)*x >>> g = 16*x**2 >>> gcd(f, g) 4*x </source>
It also works with multiple variables. In this case, the variables are ordered alphabetically, be default, which has influence on the leading coefficient.
<source lang="python"> >>> f = x*y/2 + y**2 >>> g = 3*x + 6*y >>> gcd(f, g) x + 2*y >>> gcd(f, g, var=[y, x]) y + (1/2)*x </source>
The lcm is connected with the gcd and one can be computed using the other.
<source lang="python"> >>> f = x*y**2 + x**2*y >>> g = x**2*y**2 >>> gcd(f, g) x*y >>> lcm(f, g) x**2*y**3 + x**3*y**2 >>> (f*g).expand() x**3*y**4 + x**4*y**3 >>> (gcd(f, g)*lcm(f, g)).expand() x**3*y**4 + x**4*y**3 </source>
Square-free factorization
You can compute the square-free part of a univariate polynomial, which is the product of all the different irreducible divisors.
<source lang="python"> >>> sqf_part(x**3*(x+1)**2) x + x**2 </source>
The square-free factorization of a univariate polynomial is the product of all factors (not irreducible) of degree 1, 2 ...
<source lang="python"> >>> sqf((x + 2)*(x*(x + 1))**2) (x + x**2)**2*(2 + x) </source>
Factorization
This function provides factorization of univariate and multivariate polynomials with rational coefficients.
<source lang="python"> >>> factor(Rational(1,2)*x**4 + Rational(5,12)*x**3 - Rational(1,3)*x**2) -1/12*x**2*(1 - 2*x)*(4 + 3*x) </source>
<source lang="python"> >>> factor(x**2 + 4*x*y + 4*y**2) (x + 2*y)**2 </source>
Groebner bases
Buchberger's algorithm is implemented, supporting various monomial orders.
<source lang="python"> >>> groebner([x**2 + 1, y**4*x + x**3], order='lex') [(-1) + y**4, 1 + x**2] </source>
<source lang="python"> >>> groebner([x**2 + 1, y**4*x + x**3, x*y*z**3], order='grevlex') [(-1) + y**4, z**3, 1 + x**2] </source>
Solving Equations
We have (incomplete) methods to find the complex or even symbolic roots of polynomials and to solve some systems of polynomial equations.
<source lang="python"> >>> roots(x**3 + 2*x + 3) [-1, 1/2 + (1/2)*I*11**(1/2), 1/2 - 1/2*I*11**(1/2)] </source>
<source lang="python"> >>> roots(x**2 + p*x + q, var=[x]) [(1/2)*(p**2 - 4*q)**(1/2) - 1/2*p, -1/2*p - 1/2*(p**2 - 4*q)**(1/2)] </source>
<source lang="python"> >>> solve_system([y - x, x - 5]) [(5, 5)] </source>
<source lang="python"> >>> solve_system([y**2 - x**3 + 1, y*x]) [(0, I), (0, -I), (1, 0), ((-1/2) + (1/2)*I*3**(1/2), 0), ((-1/2) - 1/2*I*3**(1/2), 0)] </source>
The Polynomial class
Coming soon...
